Skip to content

Commit 45268f0

Browse files
committed
Fix an edge case in DOMReader when there's a widget at the end of the content
FIX: Fix an issue where the editor could, in some circumstances, insert a stray newline when typing over a document that ended in a block widget. See https://discuss.codemirror.net/t/unexpected-behaviour-in-mergeview/9519
1 parent 90d7ec6 commit 45268f0

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/domreader.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ export class DOMReader {
3030
let next: Node | null = cur.nextSibling
3131
if (next == end) break
3232
let view = ContentView.get(cur), nextView = ContentView.get(next!)
33-
if (view && nextView ? view.breakAfter :
34-
(view ? view.breakAfter : isBlockElement(cur)) ||
35-
(isBlockElement(next!) && (cur.nodeName != "BR" || (cur as any).cmIgnore) && this.text.length > oldLen))
33+
if ((view && nextView ? view.breakAfter :
34+
(view ? view.breakAfter : isBlockElement(cur)) ||
35+
(isBlockElement(next!) && (cur.nodeName != "BR" || (cur as any).cmIgnore) && this.text.length > oldLen)) &&
36+
!isEmptyToEnd(next, end))
3637
this.lineBreak()
3738
cur = next!
3839
}
@@ -105,6 +106,21 @@ function isAtEnd(parent: Node, node: Node | null, offset: number) {
105106
}
106107
}
107108

109+
function isEmptyToEnd(node: Node | null, end: Node | null) {
110+
let widgets: ContentView[] | undefined
111+
for (;; node = node.nextSibling) {
112+
if (node == end || !node) break
113+
let view = ContentView.get(node)
114+
if (!(view?.isWidget || (node as any).cmIgnore)) return false
115+
if (view) (widgets || (widgets = [])).push(view)
116+
}
117+
if (widgets) for (let w of widgets) {
118+
let override = w.overrideDOMText
119+
if (override?.length) return false
120+
}
121+
return true
122+
}
123+
108124
export class DOMPoint {
109125
pos: number = -1
110126
constructor(readonly node: Node, readonly offset: number) {}

0 commit comments

Comments
 (0)