Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure selection is definitely collapsed - fixes Blink Shadow DOM bug #7

Merged
merged 1 commit into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/domchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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()
Expand Down