Commit 205b64df authored by Robert Knight's avatar Robert Knight

Simplify `range.commonAncestorContainer` update

The test case given in the comment no longer fails in modern versions of Safari
and I was unable to find any open or closed bug reports mentioning
`Node.contains`. Since the comment referenced a very old version of
Safari, I think it is reasonable to assume it has long been resolved.
parent 41d5c153
...@@ -416,32 +416,8 @@ export class SerializedRange { ...@@ -416,32 +416,8 @@ export class SerializedRange {
} }
} }
// Here's an elegant next step...
//
// range.commonAncestorContainer = $(range.startContainer).parents().has(range.endContainer)[0]
//
// ...but unfortunately Node.contains() is broken in Safari 5.1.5 (7534.55.3)
// and presumably other earlier versions of WebKit. In particular, in a
// document like
//
// <p>Hello</p>
//
// the code
//
// p = document.getElementsByTagName('p')[0]
// p.contains(p.firstChild)
//
// returns `false`. Yay.
//
// So instead, we step through the parents from the bottom up and use
// Node.compareDocumentPosition() to decide when to set the
// commonAncestorContainer and bail out.
const contains = (a, b) =>
a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_CONTAINED_BY;
for (let parent of parents(range.startContainer)) { for (let parent of parents(range.startContainer)) {
if (contains(parent, range.endContainer)) { if (parent.contains(range.endContainer)) {
range.commonAncestorContainer = parent; range.commonAncestorContainer = parent;
break; break;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment