Skip to content

Commit

Permalink
Add an additional IMG element after an uneditable widget at end of li…
Browse files Browse the repository at this point in the history
…ne on Safari

To avoid a bug where the browser draws the cursor at the start of the line.

FIX: Work around a Safari bug where it draws the cursor at the start of the line
when it is after an uneditable node at the end of the line.

Issue ProseMirror/prosemirror#1165
  • Loading branch information
marijnh committed Jun 22, 2021
1 parent d2f9b6f commit 68cab14
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/viewdesc.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class ViewDesc {
matchesWidget() { return false }
matchesMark() { return false }
matchesNode() { return false }
matchesHack() { return false }
matchesHack(_nodeName) { return false }

get beforePosition() { return false }

Expand Down Expand Up @@ -851,7 +851,7 @@ class TextViewDesc extends NodeViewDesc {
// around contentEditable terribleness.
class TrailingHackViewDesc extends ViewDesc {
parseRule() { return {ignore: true} }
matchesHack() { return this.dirty == NOT_DIRTY }
matchesHack(nodeName) { return this.dirty == NOT_DIRTY && this.dom.nodeName == nodeName }
get domAtom() { return true }
}

Expand Down Expand Up @@ -1188,13 +1188,20 @@ class ViewTreeUpdater {
if (!lastChild || // Empty textblock
!(lastChild instanceof TextViewDesc) ||
/\n$/.test(lastChild.node.text)) {
if (this.index < this.top.children.length && this.top.children[this.index].matchesHack()) {
this.index++
} else {
let dom = document.createElement("br")
this.top.children.splice(this.index++, 0, new TrailingHackViewDesc(this.top, nothing, dom, null))
this.changed = true
}
// Avoid a bug in Safari's cursor drawing (#1165)
if (browser.safari && lastChild.dom.contentEditable == "false")
this.addHackNode("IMG")
this.addHackNode("BR")
}
}

addHackNode(nodeName) {
if (this.index < this.top.children.length && this.top.children[this.index].matchesHack(nodeName)) {
this.index++
} else {
let dom = document.createElement(nodeName)
this.top.children.splice(this.index++, 0, new TrailingHackViewDesc(this.top, nothing, dom, null))
this.changed = true
}
}
}
Expand Down

0 comments on commit 68cab14

Please sign in to comment.