Skip to content

Commit 4d3e8c8

Browse files
committed
Try to recover the content DOM node during parsing when Chrome recreates it
FIX: Fix an issue where a node view with a separate content wrapper node could sometimes lose its content on Chrome when backspacing out content due to unexpected DOM element recreation. Closes ProseMirror/prosemirror#1257
1 parent 23e468f commit 4d3e8c8

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/domchange.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export function readDOMChange(view, from, to, typeOver, addedNodes) {
132132
return
133133
}
134134
}
135+
console.log("parsed", parse.doc + "", "from", view.dom.innerHTML)
135136
view.domChangeCount++
136137
// Handle the case where overwriting a selection by typing matches
137138
// the start or end of the selected content, creating a change

src/viewdesc.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,23 @@ class NodeViewDesc extends ViewDesc {
672672
// whether this is a problem
673673
let rule = {node: this.node.type.name, attrs: this.node.attrs}
674674
if (this.node.type.whitespace == "pre") rule.preserveWhitespace = "full"
675-
if (this.contentDOM && !this.contentLost) rule.contentElement = this.contentDOM
676-
else rule.getContent = () => this.contentDOM ? Fragment.empty : this.node.content
675+
if (!this.contentDOM) {
676+
rule.getContent = () => this.node.content
677+
} else if (!this.contentLost) {
678+
rule.contentElement = this.contentDOM
679+
} else {
680+
// Chrome likes to randomly recreate parent nodes when
681+
// backspacing things. When that happens, this tries to find the
682+
// new parent.
683+
for (let i = this.children.length - 1; i >= 0; i--) {
684+
let child = this.children[i]
685+
if (this.dom.contains(child.dom.parentNode)) {
686+
rule.contentElement = child.dom.parentNode
687+
break
688+
}
689+
}
690+
if (!rule.contentElement) rule.getContent = () => Fragment.empty
691+
}
677692
return rule
678693
}
679694

0 commit comments

Comments
 (0)