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
This commit is contained in:
parent
b27d290509
commit
1e91bf151f
@ -73,13 +73,11 @@ class Frontend {
|
|||||||
this._pageZoomFactor = 1.0;
|
this._pageZoomFactor = 1.0;
|
||||||
this._contentScale = 1.0;
|
this._contentScale = 1.0;
|
||||||
this._lastShowPromise = Promise.resolve();
|
this._lastShowPromise = Promise.resolve();
|
||||||
this._documentUtil = new DocumentUtil();
|
|
||||||
this._textScanner = new TextScanner({
|
this._textScanner = new TextScanner({
|
||||||
node: window,
|
node: window,
|
||||||
ignoreElements: this._ignoreElements.bind(this),
|
ignoreElements: this._ignoreElements.bind(this),
|
||||||
ignorePoint: this._ignorePoint.bind(this),
|
ignorePoint: this._ignorePoint.bind(this),
|
||||||
getSearchContext: this._getSearchContext.bind(this),
|
getSearchContext: this._getSearchContext.bind(this),
|
||||||
documentUtil: this._documentUtil,
|
|
||||||
searchTerms: true,
|
searchTerms: true,
|
||||||
searchKanji: true
|
searchKanji: true
|
||||||
});
|
});
|
||||||
|
@ -20,7 +20,6 @@
|
|||||||
* DisplayGenerator
|
* DisplayGenerator
|
||||||
* DisplayHistory
|
* DisplayHistory
|
||||||
* DisplayNotification
|
* DisplayNotification
|
||||||
* DocumentUtil
|
|
||||||
* ElementOverflowController
|
* ElementOverflowController
|
||||||
* FrameEndpoint
|
* FrameEndpoint
|
||||||
* Frontend
|
* Frontend
|
||||||
@ -121,7 +120,6 @@ class Display extends EventDispatcher {
|
|||||||
this._query = '';
|
this._query = '';
|
||||||
this._fullQuery = '';
|
this._fullQuery = '';
|
||||||
this._queryOffset = 0;
|
this._queryOffset = 0;
|
||||||
this._documentUtil = new DocumentUtil();
|
|
||||||
this._progressIndicator = document.querySelector('#progress-indicator');
|
this._progressIndicator = document.querySelector('#progress-indicator');
|
||||||
this._progressIndicatorTimer = null;
|
this._progressIndicatorTimer = null;
|
||||||
this._progressIndicatorVisible = new DynamicProperty(false);
|
this._progressIndicatorVisible = new DynamicProperty(false);
|
||||||
@ -130,7 +128,6 @@ class Display extends EventDispatcher {
|
|||||||
this._queryParserContainer = document.querySelector('#query-parser-container');
|
this._queryParserContainer = document.querySelector('#query-parser-container');
|
||||||
this._queryParser = new QueryParser({
|
this._queryParser = new QueryParser({
|
||||||
getSearchContext: this._getSearchContext.bind(this),
|
getSearchContext: this._getSearchContext.bind(this),
|
||||||
documentUtil: this._documentUtil,
|
|
||||||
japaneseUtil
|
japaneseUtil
|
||||||
});
|
});
|
||||||
this._contentScrollElement = document.querySelector('#content-scroll');
|
this._contentScrollElement = document.querySelector('#content-scroll');
|
||||||
@ -1498,7 +1495,6 @@ class Display extends EventDispatcher {
|
|||||||
this._contentTextScanner = new TextScanner({
|
this._contentTextScanner = new TextScanner({
|
||||||
node: window,
|
node: window,
|
||||||
getSearchContext: this._getSearchContext.bind(this),
|
getSearchContext: this._getSearchContext.bind(this),
|
||||||
documentUtil: this._documentUtil,
|
|
||||||
searchTerms: true,
|
searchTerms: true,
|
||||||
searchKanji: false,
|
searchKanji: false,
|
||||||
searchOnClick: true,
|
searchOnClick: true,
|
||||||
|
@ -20,10 +20,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class QueryParser extends EventDispatcher {
|
class QueryParser extends EventDispatcher {
|
||||||
constructor({getSearchContext, documentUtil, japaneseUtil}) {
|
constructor({getSearchContext, japaneseUtil}) {
|
||||||
super();
|
super();
|
||||||
this._getSearchContext = getSearchContext;
|
this._getSearchContext = getSearchContext;
|
||||||
this._documentUtil = documentUtil;
|
|
||||||
this._japaneseUtil = japaneseUtil;
|
this._japaneseUtil = japaneseUtil;
|
||||||
this._text = '';
|
this._text = '';
|
||||||
this._setTextToken = null;
|
this._setTextToken = null;
|
||||||
@ -39,7 +38,6 @@ class QueryParser extends EventDispatcher {
|
|||||||
this._textScanner = new TextScanner({
|
this._textScanner = new TextScanner({
|
||||||
node: this._queryParser,
|
node: this._queryParser,
|
||||||
getSearchContext,
|
getSearchContext,
|
||||||
documentUtil,
|
|
||||||
searchTerms: true,
|
searchTerms: true,
|
||||||
searchKanji: false,
|
searchKanji: false,
|
||||||
searchOnClick: true
|
searchOnClick: true
|
||||||
|
@ -22,11 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class DocumentUtil {
|
class DocumentUtil {
|
||||||
constructor() {
|
static getRangeFromPoint(x, y, {deepContentScan, normalizeCssZoom}) {
|
||||||
this._transparentColorPattern = /rgba\s*\([^)]*,\s*0(?:\.0+)?\s*\)/;
|
|
||||||
}
|
|
||||||
|
|
||||||
getRangeFromPoint(x, y, {deepContentScan, normalizeCssZoom}) {
|
|
||||||
const elements = this._getElementsFromPoint(x, y, deepContentScan);
|
const elements = this._getElementsFromPoint(x, y, deepContentScan);
|
||||||
let imposter = null;
|
let imposter = null;
|
||||||
let imposterContainer = null;
|
let imposterContainer = null;
|
||||||
@ -90,7 +86,7 @@ class DocumentUtil {
|
|||||||
* ```
|
* ```
|
||||||
* @returns {{sentence: string, offset: number}} The sentence and the offset to the original source.
|
* @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
|
// Scan text
|
||||||
source = source.clone();
|
source = source.clone();
|
||||||
const startLength = source.setStartOffset(extent, layoutAwareScan);
|
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');
|
style.setProperty(propertyName, value, 'important');
|
||||||
}
|
}
|
||||||
|
|
||||||
_createImposter(element, isTextarea) {
|
static _createImposter(element, isTextarea) {
|
||||||
const body = document.body;
|
const body = document.body;
|
||||||
if (body === null) { return [null, null]; }
|
if (body === null) { return [null, null]; }
|
||||||
|
|
||||||
@ -477,7 +473,7 @@ class DocumentUtil {
|
|||||||
return [imposter, container];
|
return [imposter, container];
|
||||||
}
|
}
|
||||||
|
|
||||||
_getElementsFromPoint(x, y, all) {
|
static _getElementsFromPoint(x, y, all) {
|
||||||
if (all) {
|
if (all) {
|
||||||
// document.elementsFromPoint can return duplicates which must be removed.
|
// document.elementsFromPoint can return duplicates which must be removed.
|
||||||
const elements = document.elementsFromPoint(x, y);
|
const elements = document.elementsFromPoint(x, y);
|
||||||
@ -488,7 +484,7 @@ class DocumentUtil {
|
|||||||
return e !== null ? [e] : [];
|
return e !== null ? [e] : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
_isPointInRange(x, y, range, normalizeCssZoom) {
|
static _isPointInRange(x, y, range, normalizeCssZoom) {
|
||||||
// Require a text node to start
|
// Require a text node to start
|
||||||
const {startContainer} = range;
|
const {startContainer} = range;
|
||||||
if (startContainer.nodeType !== Node.TEXT_NODE) {
|
if (startContainer.nodeType !== Node.TEXT_NODE) {
|
||||||
@ -497,7 +493,7 @@ class DocumentUtil {
|
|||||||
|
|
||||||
// Convert CSS zoom coordinates
|
// Convert CSS zoom coordinates
|
||||||
if (normalizeCssZoom) {
|
if (normalizeCssZoom) {
|
||||||
const scale = DocumentUtil.computeZoomScale(startContainer);
|
const scale = this.computeZoomScale(startContainer);
|
||||||
x /= scale;
|
x /= scale;
|
||||||
y /= scale;
|
y /= scale;
|
||||||
}
|
}
|
||||||
@ -509,7 +505,7 @@ class DocumentUtil {
|
|||||||
const {node, offset, content} = new DOMTextScanner(nodePre, offsetPre, true, false).seek(1);
|
const {node, offset, content} = new DOMTextScanner(nodePre, offsetPre, true, false).seek(1);
|
||||||
range.setEnd(node, offset);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@ -520,7 +516,7 @@ class DocumentUtil {
|
|||||||
const {node, offset, content} = new DOMTextScanner(startContainer, range.startOffset, true, false).seek(-1);
|
const {node, offset, content} = new DOMTextScanner(startContainer, range.startOffset, true, false).seek(-1);
|
||||||
range.setStart(node, offset);
|
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.
|
// This purposefully leaves the starting offset as modified and sets the range length to 0.
|
||||||
range.setEnd(node, offset);
|
range.setEnd(node, offset);
|
||||||
return true;
|
return true;
|
||||||
@ -530,11 +526,11 @@ class DocumentUtil {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_isWhitespace(string) {
|
static _isWhitespace(string) {
|
||||||
return string.trim().length === 0;
|
return string.trim().length === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
_caretRangeFromPoint(x, y) {
|
static _caretRangeFromPoint(x, y) {
|
||||||
if (typeof document.caretRangeFromPoint === 'function') {
|
if (typeof document.caretRangeFromPoint === 'function') {
|
||||||
// Chrome, Edge
|
// Chrome, Edge
|
||||||
return document.caretRangeFromPoint(x, y);
|
return document.caretRangeFromPoint(x, y);
|
||||||
@ -549,7 +545,7 @@ class DocumentUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_caretPositionFromPoint(x, y) {
|
static _caretPositionFromPoint(x, y) {
|
||||||
const position = document.caretPositionFromPoint(x, y);
|
const position = document.caretPositionFromPoint(x, y);
|
||||||
if (position === null) {
|
if (position === null) {
|
||||||
return null;
|
return null;
|
||||||
@ -586,7 +582,7 @@ class DocumentUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_caretPositionFromPointNormalizeStyles(x, y, nextElement) {
|
static _caretPositionFromPointNormalizeStyles(x, y, nextElement) {
|
||||||
const previousStyles = new Map();
|
const previousStyles = new Map();
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -638,7 +634,7 @@ class DocumentUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_caretRangeFromPointExt(x, y, elements, normalizeCssZoom) {
|
static _caretRangeFromPointExt(x, y, elements, normalizeCssZoom) {
|
||||||
let previousStyles = null;
|
let previousStyles = null;
|
||||||
try {
|
try {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
@ -670,7 +666,7 @@ class DocumentUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_disableTransparentElement(elements, i, previousStyles) {
|
static _disableTransparentElement(elements, i, previousStyles) {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (i >= elements.length) {
|
if (i >= elements.length) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -685,13 +681,13 @@ class DocumentUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_recordPreviousStyle(previousStyles, element) {
|
static _recordPreviousStyle(previousStyles, element) {
|
||||||
if (previousStyles.has(element)) { return; }
|
if (previousStyles.has(element)) { return; }
|
||||||
const style = element.hasAttribute('style') ? element.getAttribute('style') : null;
|
const style = element.hasAttribute('style') ? element.getAttribute('style') : null;
|
||||||
previousStyles.set(element, style);
|
previousStyles.set(element, style);
|
||||||
}
|
}
|
||||||
|
|
||||||
_revertStyles(previousStyles) {
|
static _revertStyles(previousStyles) {
|
||||||
for (const [element, style] of previousStyles.entries()) {
|
for (const [element, style] of previousStyles.entries()) {
|
||||||
if (style === null) {
|
if (style === null) {
|
||||||
element.removeAttribute('style');
|
element.removeAttribute('style');
|
||||||
@ -701,7 +697,7 @@ class DocumentUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_isElementTransparent(element) {
|
static _isElementTransparent(element) {
|
||||||
if (
|
if (
|
||||||
element === document.body ||
|
element === document.body ||
|
||||||
element === document.documentElement
|
element === document.documentElement
|
||||||
@ -716,13 +712,15 @@ class DocumentUtil {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_isColorTransparent(cssColor) {
|
static _isColorTransparent(cssColor) {
|
||||||
return this._transparentColorPattern.test(cssColor);
|
return this._transparentColorPattern.test(cssColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
_isElementUserSelectAll(element) {
|
static _isElementUserSelectAll(element) {
|
||||||
return getComputedStyle(element).userSelect === 'all';
|
return getComputedStyle(element).userSelect === 'all';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line no-underscore-dangle
|
// 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;
|
DocumentUtil._cssZoomSupported = null;
|
||||||
|
@ -22,7 +22,6 @@
|
|||||||
class TextScanner extends EventDispatcher {
|
class TextScanner extends EventDispatcher {
|
||||||
constructor({
|
constructor({
|
||||||
node,
|
node,
|
||||||
documentUtil,
|
|
||||||
getSearchContext,
|
getSearchContext,
|
||||||
ignoreElements=null,
|
ignoreElements=null,
|
||||||
ignorePoint=null,
|
ignorePoint=null,
|
||||||
@ -33,7 +32,6 @@ class TextScanner extends EventDispatcher {
|
|||||||
}) {
|
}) {
|
||||||
super();
|
super();
|
||||||
this._node = node;
|
this._node = node;
|
||||||
this._documentUtil = documentUtil;
|
|
||||||
this._getSearchContext = getSearchContext;
|
this._getSearchContext = getSearchContext;
|
||||||
this._ignoreElements = ignoreElements;
|
this._ignoreElements = ignoreElements;
|
||||||
this._ignorePoint = ignorePoint;
|
this._ignorePoint = ignorePoint;
|
||||||
@ -878,7 +876,7 @@ class TextScanner extends EventDispatcher {
|
|||||||
if (dictionaryEntries.length === 0) { return null; }
|
if (dictionaryEntries.length === 0) { return null; }
|
||||||
|
|
||||||
textSource.setEndOffset(originalTextLength, layoutAwareScan, false);
|
textSource.setEndOffset(originalTextLength, layoutAwareScan, false);
|
||||||
const sentence = this._documentUtil.extractSentence(
|
const sentence = DocumentUtil.extractSentence(
|
||||||
textSource,
|
textSource,
|
||||||
layoutAwareScan,
|
layoutAwareScan,
|
||||||
sentenceScanExtent,
|
sentenceScanExtent,
|
||||||
@ -905,7 +903,7 @@ class TextScanner extends EventDispatcher {
|
|||||||
if (dictionaryEntries.length === 0) { return null; }
|
if (dictionaryEntries.length === 0) { return null; }
|
||||||
|
|
||||||
textSource.setEndOffset(1, layoutAwareScan, false);
|
textSource.setEndOffset(1, layoutAwareScan, false);
|
||||||
const sentence = this._documentUtil.extractSentence(
|
const sentence = DocumentUtil.extractSentence(
|
||||||
textSource,
|
textSource,
|
||||||
layoutAwareScan,
|
layoutAwareScan,
|
||||||
sentenceScanExtent,
|
sentenceScanExtent,
|
||||||
@ -937,7 +935,7 @@ class TextScanner extends EventDispatcher {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const textSource = this._documentUtil.getRangeFromPoint(x, y, {
|
const textSource = DocumentUtil.getRangeFromPoint(x, y, {
|
||||||
deepContentScan: this._deepContentScan,
|
deepContentScan: this._deepContentScan,
|
||||||
normalizeCssZoom: this._normalizeCssZoom
|
normalizeCssZoom: this._normalizeCssZoom
|
||||||
});
|
});
|
||||||
|
@ -166,8 +166,7 @@ async function testDocumentTextScanningFunctions(dom, {DocumentUtil, TextSourceR
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Test docRangeFromPoint
|
// Test docRangeFromPoint
|
||||||
const documentUtil = new DocumentUtil();
|
const source = DocumentUtil.getRangeFromPoint(0, 0, {
|
||||||
const source = documentUtil.getRangeFromPoint(0, 0, {
|
|
||||||
deepContentScan: false,
|
deepContentScan: false,
|
||||||
normalizeCssZoom: true
|
normalizeCssZoom: true
|
||||||
});
|
});
|
||||||
@ -202,7 +201,7 @@ async function testDocumentTextScanningFunctions(dom, {DocumentUtil, TextSourceR
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test docSentenceExtract
|
// Test docSentenceExtract
|
||||||
const sentenceActual = documentUtil.extractSentence(
|
const sentenceActual = DocumentUtil.extractSentence(
|
||||||
source,
|
source,
|
||||||
false,
|
false,
|
||||||
sentenceScanExtent,
|
sentenceScanExtent,
|
||||||
|
Loading…
Reference in New Issue
Block a user