Skip to content

Commit e7d6eeb

Browse files
committed
Clip context positions to the size of the edit context
FIX: Work around another crash caused by incorrect composition positions reported by `EditContext`. See https://discuss.codemirror.net/t/editor-crashing-when-used-as-controlled-component-in-react/8457 Issue codemirror/dev#1472
1 parent f445c3c commit e7d6eeb

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/domobserver.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -592,11 +592,13 @@ class EditContextManager {
592592
for (let format of e.getTextFormats()) {
593593
let lineStyle = format.underlineStyle, thickness = format.underlineThickness
594594
if (lineStyle != "None" && thickness != "None") {
595-
let style = `text-decoration: underline ${
596-
lineStyle == "Dashed" ? "dashed " : lineStyle == "Squiggle" ? "wavy " : ""
597-
}${thickness == "Thin" ? 1 : 2}px`
598-
deco.push(Decoration.mark({attributes: {style}})
599-
.range(this.toEditorPos(format.rangeStart), this.toEditorPos(format.rangeEnd)))
595+
let from = this.toEditorPos(format.rangeStart), to = this.toEditorPos(format.rangeEnd)
596+
if (from < to) {
597+
let style = `text-decoration: underline ${
598+
lineStyle == "Dashed" ? "dashed " : lineStyle == "Squiggle" ? "wavy " : ""
599+
}${thickness == "Thin" ? 1 : 2}px`
600+
deco.push(Decoration.mark({attributes: {style}}).range(from, to))
601+
}
600602
}
601603
}
602604
view.dispatch({effects: setEditContextFormatting.of(Decoration.set(deco))})
@@ -713,6 +715,7 @@ class EditContextManager {
713715
}
714716

715717
toEditorPos(contextPos: number) {
718+
contextPos = Math.min(contextPos, this.to - this.from)
716719
let c = this.composing
717720
return c && c.drifted ? c.editorBase + (contextPos - c.contextBase) : contextPos + this.from
718721
}

0 commit comments

Comments
 (0)