From 6e3d72c112a929dc83280d0cbf3312b50f6965bf Mon Sep 17 00:00:00 2001 From: Bede Overend Date: Mon, 20 Mar 2017 12:22:51 +1100 Subject: [PATCH] Ensure selection is definitely collapsed - fixes Blink Shadow DOM bug --- src/domchange.js | 4 ++-- src/selection.js | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/domchange.js b/src/domchange.js index 035b9989..4b5e32cd 100644 --- a/src/domchange.js +++ b/src/domchange.js @@ -3,7 +3,7 @@ const {Selection} = require("prosemirror-state") const {Mapping} = require("prosemirror-transform") const {TrackMappings} = require("./trackmappings") -const {selectionBetween} = require("./selection") +const {selectionBetween, selectionCollapsed} = require("./selection") class DOMChange { constructor(view, composing) { @@ -109,7 +109,7 @@ function parseBetween(view, oldState, range) { let domSel = view.root.getSelection(), find = null, anchor = domSel.anchorNode if (anchor && view.dom.contains(anchor.nodeType == 1 ? anchor : anchor.parentNode)) { find = [{node: anchor, offset: domSel.anchorOffset}] - if (!domSel.isCollapsed) + if (!selectionCollapsed(domSel)) find.push({node: domSel.focusNode, offset: domSel.focusOffset}) } let startDoc = oldState.doc diff --git a/src/selection.js b/src/selection.js index 7002ad79..7ffb09f2 100644 --- a/src/selection.js +++ b/src/selection.js @@ -67,7 +67,7 @@ class SelectionReader { } let head = this.view.docView.posFromDOM(domSel.focusNode, domSel.focusOffset) let $head = doc.resolve(head), $anchor, selection - if (domSel.isCollapsed) { + if (selectionCollapsed(domSel)) { $anchor = $head while (nearestDesc && !nearestDesc.node) nearestDesc = nearestDesc.parent if (nearestDesc && nearestDesc.node.isAtom && NodeSelection.isSelectable(nearestDesc.node)) { @@ -220,6 +220,21 @@ function temporarilyEditable(view, pos) { } } +function selectionCollapsed(selection) { + if (!selection.isCollapsed) { + return false + } + + for (let i = 0, k = selection.rangeCount; i < k; i++) { + if (!selection.getRangeAt(i).collapsed) { + return false + } + } + + return true +} +exports.selectionCollapsed = selectionCollapsed + function removeClassOnSelectionChange(view) { document.removeEventListener("selectionchange", view.hideSelectionGuard) let domSel = view.root.getSelection()