diff options
author | Alex Yatskov <alex@foosoft.net> | 2019-09-05 20:21:07 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2019-09-05 20:21:07 -0700 |
commit | cfc6363a01ee00e89866c54709006d6f55d093de (patch) | |
tree | a03b1f79ba220e93c248ad8284fb01dc95b676a9 /ext/fg/js/source.js | |
parent | 5347da528bd07166b4686f45440d80a77f4888a3 (diff) | |
parent | c53263158b22f4be8e54696c4675f360f1813de8 (diff) |
Merge branch 'master' into testing
Diffstat (limited to 'ext/fg/js/source.js')
-rw-r--r-- | ext/fg/js/source.js | 66 |
1 files changed, 44 insertions, 22 deletions
diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index e724488d..18a1a976 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -59,11 +59,6 @@ class TextSourceRange { return length - state.remainder; } - containsPoint(point) { - const rect = this.getPaddedRect(); - return point.x >= rect.left && point.x <= rect.right; - } - getRect() { return this.range.getBoundingClientRect(); } @@ -72,18 +67,6 @@ class TextSourceRange { return TextSourceRange.getElementWritingMode(TextSourceRange.getParentElement(this.range.startContainer)); } - getPaddedRect() { - const range = this.range.cloneRange(); - const startOffset = range.startOffset; - const endOffset = range.endOffset; - const node = range.startContainer; - - range.setStart(node, Math.max(0, startOffset - 1)); - range.setEnd(node, Math.min(node.length, endOffset + 1)); - - return range.getBoundingClientRect(); - } - select() { const selection = window.getSelection(); selection.removeAllRanges(); @@ -232,6 +215,50 @@ class TextSourceRange { const writingMode = style.writingMode; return typeof writingMode === 'string' ? writingMode : 'horizontal-tb'; } + + static getNodesInRange(range) { + const end = range.endContainer; + const nodes = []; + for (let node = range.startContainer; node !== null; node = TextSourceRange.getNextNode(node)) { + nodes.push(node); + if (node === end) { break; } + } + return nodes; + } + + static getNextNode(node) { + let next = node.firstChild; + if (next === null) { + while (true) { + next = node.nextSibling; + if (next !== null) { break; } + + next = node.parentNode; + if (node === null) { break; } + + node = next; + } + } + return next; + } + + static anyNodeMatchesSelector(nodeList, selector) { + for (const node of nodeList) { + if (TextSourceRange.nodeMatchesSelector(node, selector)) { + return true; + } + } + return false; + } + + static nodeMatchesSelector(node, selector) { + for (; node !== null; node = node.parentNode) { + if (node.nodeType === Node.ELEMENT_NODE) { + return node.matches(selector); + } + } + return false; + } } @@ -290,11 +317,6 @@ class TextSourceElement { return 0; } - containsPoint(point) { - const rect = this.getRect(); - return point.x >= rect.left && point.x <= rect.right; - } - getRect() { return this.element.getBoundingClientRect(); } |