Skip to content

Commit

Permalink
Clip offsets returned from caretPositionFromPoint
Browse files Browse the repository at this point in the history
FIX: Work around odd behavior in Chrome's newly supported `caretPositionFromPoint`
method, which could cause CodeMirror to crash with a null dereference.
  • Loading branch information
marijnh committed Aug 26, 2024
1 parent e7b62aa commit d25b5ce
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {EditorView} from "./editorview"
import {BlockType} from "./decoration"
import {LineView} from "./blockview"
import {atomicRanges} from "./extension"
import {clientRectsFor, textRange, Rect} from "./dom"
import {clientRectsFor, textRange, Rect, maxOffset} from "./dom"
import {moveVisually, movedOver, Direction} from "./bidi"
import {BlockInfo} from "./heightmap"
import browser from "./browser"
Expand Down Expand Up @@ -184,6 +184,10 @@ export function posAtCoords(view: EditorView, coords: {x: number, y: number}, pr
node = undefined
}
}
// Chrome will return offsets into <input> elements without child
// nodes, which will lead to a null deref below, so clip the
// offset to the node size.
if (node) offset = Math.min(maxOffset(node), offset)
}

// No luck, do our own (potentially expensive) search
Expand Down

0 comments on commit d25b5ce

Please sign in to comment.