summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-09-01 10:28:50 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-09-02 19:33:52 -0400
commitfad53324885c971a1ab1aeee3da5dd60dc5058df (patch)
treefb156e733c2c2113e294cad2afac9b07f73f3683 /ext
parentf4b81eff3054e4277e97d06290535bfecc9b9cc1 (diff)
Move static DOM scanning functions into TextSourceRange
Diffstat (limited to 'ext')
-rw-r--r--ext/fg/js/frontend.js48
-rw-r--r--ext/fg/js/source.js44
2 files changed, 46 insertions, 46 deletions
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
index eea77d2a..b70bf036 100644
--- a/ext/fg/js/frontend.js
+++ b/ext/fg/js/frontend.js
@@ -515,58 +515,14 @@ class Frontend {
length = textSource.text().length;
while (textSource.range && length > 0) {
- const nodes = Frontend.getNodesInRange(textSource.range);
- if (Frontend.isValidScanningNodeList(nodes, this.ignoreNodes)) {
+ const nodes = TextSourceRange.getNodesInRange(textSource.range);
+ if (!TextSourceRange.anyNodeMatchesSelector(nodes, this.ignoreNodes)) {
break;
}
--length;
textSource.setEndOffset(length);
}
}
-
- static getNodesInRange(range) {
- const end = range.endContainer;
- const nodes = [];
- for (let node = range.startContainer; node !== null; node = Frontend.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 isValidScanningNodeList(nodeList, selector) {
- for (const node of nodeList) {
- if (!Frontend.isValidScanningNode(node, selector)) {
- return false;
- }
- }
- return true;
- }
-
- static isValidScanningNode(node, selector) {
- for (; node !== null; node = node.parentNode) {
- if (node.nodeType === Node.ELEMENT_NODE) {
- return !node.matches(selector);
- }
- }
- return true;
- }
}
window.yomichan_frontend = Frontend.create();
diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js
index e724488d..385b5001 100644
--- a/ext/fg/js/source.js
+++ b/ext/fg/js/source.js
@@ -232,6 +232,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;
+ }
}