aboutsummaryrefslogtreecommitdiff
path: root/ext/js/dom/simple-dom-parser.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2022-05-14 18:13:04 -0400
committerGitHub <noreply@github.com>2022-05-14 18:13:04 -0400
commit8b6f526dc69094963f7e0e17e72d3e0cdfd3b41a (patch)
tree9ac087bbe1fe8569e4ed5ff9238e2a55faeaa09b /ext/js/dom/simple-dom-parser.js
parent5dcc2315d242bcec29cc478618d448c941f73ab1 (diff)
Regex optimizations (#2132)
* Remove regex construction for SimpleDOMParser.getElementsByClassName * Remove regex construction for CssStyleApplier._getRulesForClass * Rename, add jsdoc for clarity
Diffstat (limited to 'ext/js/dom/simple-dom-parser.js')
-rw-r--r--ext/js/dom/simple-dom-parser.js18
1 files changed, 16 insertions, 2 deletions
diff --git a/ext/js/dom/simple-dom-parser.js b/ext/js/dom/simple-dom-parser.js
index 09f3e914..bc327f5e 100644
--- a/ext/js/dom/simple-dom-parser.js
+++ b/ext/js/dom/simple-dom-parser.js
@@ -22,6 +22,8 @@
class SimpleDOMParser {
constructor(content) {
this._document = parse5.parse(content);
+ // eslint-disable-next-line no-control-regex
+ this._patternHtmlWhitespace = /[\t\r\n\x0C ]+/g;
}
getElementById(id, root=null) {
@@ -54,11 +56,10 @@ class SimpleDOMParser {
getElementsByClassName(className, root=null) {
const results = [];
- const classNamePattern = new RegExp(`(^|\\s)${escapeRegExp(className)}(\\s|$)`);
for (const node of this._allNodes(root)) {
if (typeof node.tagName === 'string') {
const nodeClassName = this.getAttribute(node, 'class');
- if (nodeClassName !== null && classNamePattern.test(nodeClassName)) {
+ if (nodeClassName !== null && this._hasToken(nodeClassName, className)) {
results.push(node);
}
}
@@ -114,4 +115,17 @@ class SimpleDOMParser {
}
}
}
+
+ _hasToken(tokenListString, token) {
+ let start = 0;
+ const pattern = this._patternHtmlWhitespace;
+ pattern.lastIndex = 0;
+ while (true) {
+ const match = pattern.exec(tokenListString);
+ const end = match === null ? tokenListString.length : match.index;
+ if (end > start && tokenListString.substring(start, end) === token) { return true; }
+ if (match === null) { return false; }
+ start = end + match[0].length;
+ }
+ }
}