aboutsummaryrefslogtreecommitdiff
path: root/ext/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2022-09-24 16:05:19 -0400
committerGitHub <noreply@github.com>2022-09-24 16:05:19 -0400
commit1e91bf151f43ad05138e862ba4a03abad6929e5f (patch)
tree174e3c65f91b094056127dc3d53d99ec6c711a9f /ext/js
parentb27d290509ae2ce9f2158b5d8f0c90e40828043b (diff)
DocumentUtil static (#2232)
* Make all methods static The two non-static methods are kept for temporary compatibility * Use this instead of class name now that functions are static * Update test * Don't instantiate DocumentUtil * Remove temporary non-static methods * Remove unused global declaration
Diffstat (limited to 'ext/js')
-rw-r--r--ext/js/app/frontend.js2
-rw-r--r--ext/js/display/display.js4
-rw-r--r--ext/js/display/query-parser.js4
-rw-r--r--ext/js/dom/document-util.js46
-rw-r--r--ext/js/language/text-scanner.js8
5 files changed, 26 insertions, 38 deletions
diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js
index de3eb7fd..4f704faf 100644
--- a/ext/js/app/frontend.js
+++ b/ext/js/app/frontend.js
@@ -73,13 +73,11 @@ class Frontend {
this._pageZoomFactor = 1.0;
this._contentScale = 1.0;
this._lastShowPromise = Promise.resolve();
- this._documentUtil = new DocumentUtil();
this._textScanner = new TextScanner({
node: window,
ignoreElements: this._ignoreElements.bind(this),
ignorePoint: this._ignorePoint.bind(this),
getSearchContext: this._getSearchContext.bind(this),
- documentUtil: this._documentUtil,
searchTerms: true,
searchKanji: true
});
diff --git a/ext/js/display/display.js b/ext/js/display/display.js
index d286bd5e..21bdc5a4 100644
--- a/ext/js/display/display.js
+++ b/ext/js/display/display.js
@@ -20,7 +20,6 @@
* DisplayGenerator
* DisplayHistory
* DisplayNotification
- * DocumentUtil
* ElementOverflowController
* FrameEndpoint
* Frontend
@@ -121,7 +120,6 @@ class Display extends EventDispatcher {
this._query = '';
this._fullQuery = '';
this._queryOffset = 0;
- this._documentUtil = new DocumentUtil();
this._progressIndicator = document.querySelector('#progress-indicator');
this._progressIndicatorTimer = null;
this._progressIndicatorVisible = new DynamicProperty(false);
@@ -130,7 +128,6 @@ class Display extends EventDispatcher {
this._queryParserContainer = document.querySelector('#query-parser-container');
this._queryParser = new QueryParser({
getSearchContext: this._getSearchContext.bind(this),
- documentUtil: this._documentUtil,
japaneseUtil
});
this._contentScrollElement = document.querySelector('#content-scroll');
@@ -1498,7 +1495,6 @@ class Display extends EventDispatcher {
this._contentTextScanner = new TextScanner({
node: window,
getSearchContext: this._getSearchContext.bind(this),
- documentUtil: this._documentUtil,
searchTerms: true,
searchKanji: false,
searchOnClick: true,
diff --git a/ext/js/display/query-parser.js b/ext/js/display/query-parser.js
index cc193a06..bafa9045 100644
--- a/ext/js/display/query-parser.js
+++ b/ext/js/display/query-parser.js
@@ -20,10 +20,9 @@
*/
class QueryParser extends EventDispatcher {
- constructor({getSearchContext, documentUtil, japaneseUtil}) {
+ constructor({getSearchContext, japaneseUtil}) {
super();
this._getSearchContext = getSearchContext;
- this._documentUtil = documentUtil;
this._japaneseUtil = japaneseUtil;
this._text = '';
this._setTextToken = null;
@@ -39,7 +38,6 @@ class QueryParser extends EventDispatcher {
this._textScanner = new TextScanner({
node: this._queryParser,
getSearchContext,
- documentUtil,
searchTerms: true,
searchKanji: false,
searchOnClick: true
diff --git a/ext/js/dom/document-util.js b/ext/js/dom/document-util.js
index 41f44afe..53297084 100644
--- a/ext/js/dom/document-util.js
+++ b/ext/js/dom/document-util.js
@@ -22,11 +22,7 @@
*/
class DocumentUtil {
- constructor() {
- this._transparentColorPattern = /rgba\s*\([^)]*,\s*0(?:\.0+)?\s*\)/;
- }
-
- getRangeFromPoint(x, y, {deepContentScan, normalizeCssZoom}) {
+ static getRangeFromPoint(x, y, {deepContentScan, normalizeCssZoom}) {
const elements = this._getElementsFromPoint(x, y, deepContentScan);
let imposter = null;
let imposterContainer = null;
@@ -90,7 +86,7 @@ class DocumentUtil {
* ```
* @returns {{sentence: string, offset: number}} The sentence and the offset to the original source.
*/
- extractSentence(source, layoutAwareScan, extent, terminateAtNewlines, terminatorMap, forwardQuoteMap, backwardQuoteMap) {
+ static extractSentence(source, layoutAwareScan, extent, terminateAtNewlines, terminatorMap, forwardQuoteMap, backwardQuoteMap) {
// Scan text
source = source.clone();
const startLength = source.setStartOffset(extent, layoutAwareScan);
@@ -397,11 +393,11 @@ class DocumentUtil {
}
}
- _setImposterStyle(style, propertyName, value) {
+ static _setImposterStyle(style, propertyName, value) {
style.setProperty(propertyName, value, 'important');
}
- _createImposter(element, isTextarea) {
+ static _createImposter(element, isTextarea) {
const body = document.body;
if (body === null) { return [null, null]; }
@@ -477,7 +473,7 @@ class DocumentUtil {
return [imposter, container];
}
- _getElementsFromPoint(x, y, all) {
+ static _getElementsFromPoint(x, y, all) {
if (all) {
// document.elementsFromPoint can return duplicates which must be removed.
const elements = document.elementsFromPoint(x, y);
@@ -488,7 +484,7 @@ class DocumentUtil {
return e !== null ? [e] : [];
}
- _isPointInRange(x, y, range, normalizeCssZoom) {
+ static _isPointInRange(x, y, range, normalizeCssZoom) {
// Require a text node to start
const {startContainer} = range;
if (startContainer.nodeType !== Node.TEXT_NODE) {
@@ -497,7 +493,7 @@ class DocumentUtil {
// Convert CSS zoom coordinates
if (normalizeCssZoom) {
- const scale = DocumentUtil.computeZoomScale(startContainer);
+ const scale = this.computeZoomScale(startContainer);
x /= scale;
y /= scale;
}
@@ -509,7 +505,7 @@ class DocumentUtil {
const {node, offset, content} = new DOMTextScanner(nodePre, offsetPre, true, false).seek(1);
range.setEnd(node, offset);
- if (!this._isWhitespace(content) && DocumentUtil.isPointInAnyRect(x, y, range.getClientRects())) {
+ if (!this._isWhitespace(content) && this.isPointInAnyRect(x, y, range.getClientRects())) {
return true;
}
} finally {
@@ -520,7 +516,7 @@ class DocumentUtil {
const {node, offset, content} = new DOMTextScanner(startContainer, range.startOffset, true, false).seek(-1);
range.setStart(node, offset);
- if (!this._isWhitespace(content) && DocumentUtil.isPointInAnyRect(x, y, range.getClientRects())) {
+ if (!this._isWhitespace(content) && this.isPointInAnyRect(x, y, range.getClientRects())) {
// This purposefully leaves the starting offset as modified and sets the range length to 0.
range.setEnd(node, offset);
return true;
@@ -530,11 +526,11 @@ class DocumentUtil {
return false;
}
- _isWhitespace(string) {
+ static _isWhitespace(string) {
return string.trim().length === 0;
}
- _caretRangeFromPoint(x, y) {
+ static _caretRangeFromPoint(x, y) {
if (typeof document.caretRangeFromPoint === 'function') {
// Chrome, Edge
return document.caretRangeFromPoint(x, y);
@@ -549,7 +545,7 @@ class DocumentUtil {
return null;
}
- _caretPositionFromPoint(x, y) {
+ static _caretPositionFromPoint(x, y) {
const position = document.caretPositionFromPoint(x, y);
if (position === null) {
return null;
@@ -586,7 +582,7 @@ class DocumentUtil {
}
}
- _caretPositionFromPointNormalizeStyles(x, y, nextElement) {
+ static _caretPositionFromPointNormalizeStyles(x, y, nextElement) {
const previousStyles = new Map();
try {
while (true) {
@@ -638,7 +634,7 @@ class DocumentUtil {
}
}
- _caretRangeFromPointExt(x, y, elements, normalizeCssZoom) {
+ static _caretRangeFromPointExt(x, y, elements, normalizeCssZoom) {
let previousStyles = null;
try {
let i = 0;
@@ -670,7 +666,7 @@ class DocumentUtil {
}
}
- _disableTransparentElement(elements, i, previousStyles) {
+ static _disableTransparentElement(elements, i, previousStyles) {
while (true) {
if (i >= elements.length) {
return -1;
@@ -685,13 +681,13 @@ class DocumentUtil {
}
}
- _recordPreviousStyle(previousStyles, element) {
+ static _recordPreviousStyle(previousStyles, element) {
if (previousStyles.has(element)) { return; }
const style = element.hasAttribute('style') ? element.getAttribute('style') : null;
previousStyles.set(element, style);
}
- _revertStyles(previousStyles) {
+ static _revertStyles(previousStyles) {
for (const [element, style] of previousStyles.entries()) {
if (style === null) {
element.removeAttribute('style');
@@ -701,7 +697,7 @@ class DocumentUtil {
}
}
- _isElementTransparent(element) {
+ static _isElementTransparent(element) {
if (
element === document.body ||
element === document.documentElement
@@ -716,13 +712,15 @@ class DocumentUtil {
);
}
- _isColorTransparent(cssColor) {
+ static _isColorTransparent(cssColor) {
return this._transparentColorPattern.test(cssColor);
}
- _isElementUserSelectAll(element) {
+ static _isElementUserSelectAll(element) {
return getComputedStyle(element).userSelect === 'all';
}
}
// eslint-disable-next-line no-underscore-dangle
+DocumentUtil._transparentColorPattern = /rgba\s*\([^)]*,\s*0(?:\.0+)?\s*\)/;
+// eslint-disable-next-line no-underscore-dangle
DocumentUtil._cssZoomSupported = null;
diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js
index 3b8a8b47..a5b35a26 100644
--- a/ext/js/language/text-scanner.js
+++ b/ext/js/language/text-scanner.js
@@ -22,7 +22,6 @@
class TextScanner extends EventDispatcher {
constructor({
node,
- documentUtil,
getSearchContext,
ignoreElements=null,
ignorePoint=null,
@@ -33,7 +32,6 @@ class TextScanner extends EventDispatcher {
}) {
super();
this._node = node;
- this._documentUtil = documentUtil;
this._getSearchContext = getSearchContext;
this._ignoreElements = ignoreElements;
this._ignorePoint = ignorePoint;
@@ -878,7 +876,7 @@ class TextScanner extends EventDispatcher {
if (dictionaryEntries.length === 0) { return null; }
textSource.setEndOffset(originalTextLength, layoutAwareScan, false);
- const sentence = this._documentUtil.extractSentence(
+ const sentence = DocumentUtil.extractSentence(
textSource,
layoutAwareScan,
sentenceScanExtent,
@@ -905,7 +903,7 @@ class TextScanner extends EventDispatcher {
if (dictionaryEntries.length === 0) { return null; }
textSource.setEndOffset(1, layoutAwareScan, false);
- const sentence = this._documentUtil.extractSentence(
+ const sentence = DocumentUtil.extractSentence(
textSource,
layoutAwareScan,
sentenceScanExtent,
@@ -937,7 +935,7 @@ class TextScanner extends EventDispatcher {
return;
}
- const textSource = this._documentUtil.getRangeFromPoint(x, y, {
+ const textSource = DocumentUtil.getRangeFromPoint(x, y, {
deepContentScan: this._deepContentScan,
normalizeCssZoom: this._normalizeCssZoom
});