From 1e91bf151f43ad05138e862ba4a03abad6929e5f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 24 Sep 2022 16:05:19 -0400 Subject: 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 --- ext/js/dom/document-util.js | 46 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'ext/js/dom') 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; -- cgit v1.2.3