From 7d7ff165ced305bc269b24aa0ce8664272d86fa6 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 5 May 2020 00:13:43 +0300 Subject: Extract textSource text inside TextScanner (#500) * extract text inside TextScanner * clone textSource before text extraction --- ext/bg/js/search-query-parser.js | 3 +-- ext/fg/js/frontend.js | 8 ++------ ext/mixed/js/text-scanner.js | 27 +++++++++++++++------------ 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 137234e8..935f01f2 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -51,8 +51,7 @@ class QueryParser extends TextScanner { async onSearchSource(textSource, cause) { if (textSource === null) { return null; } - this.setTextSourceScanLength(textSource, this.options.scanning.length); - const searchText = textSource.text(); + const searchText = this.getTextSourceContent(textSource, this.options.scanning.length); if (searchText.length === 0) { return; } const {definitions, length} = await apiTermsFind(searchText, {}, this.getOptionsContext()); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index d979246d..b2cb0cd8 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -239,9 +239,7 @@ class Frontend { } async findTerms(textSource, optionsContext) { - this._textScanner.setTextSourceScanLength(textSource, this._options.scanning.length); - - const searchText = textSource.text(); + const searchText = this._textScanner.getTextSourceContent(textSource, this._options.scanning.length); if (searchText.length === 0) { return null; } const {definitions, length} = await apiTermsFind(searchText, {}, optionsContext); @@ -253,9 +251,7 @@ class Frontend { } async findKanji(textSource, optionsContext) { - this._textScanner.setTextSourceScanLength(textSource, 1); - - const searchText = textSource.text(); + const searchText = this._textScanner.getTextSourceContent(textSource, 1); if (searchText.length === 0) { return null; } const definitions = await apiKanjiFind(searchText, optionsContext); diff --git a/ext/mixed/js/text-scanner.js b/ext/mixed/js/text-scanner.js index d74a04f8..9dcf6009 100644 --- a/ext/mixed/js/text-scanner.js +++ b/ext/mixed/js/text-scanner.js @@ -318,21 +318,24 @@ class TextScanner extends EventDispatcher { } } - setTextSourceScanLength(textSource, length) { - textSource.setEndOffset(length); - if (this.ignoreNodes === null || !textSource.range) { - return; - } + getTextSourceContent(textSource, length) { + const clonedTextSource = textSource.clone(); + + clonedTextSource.setEndOffset(length); - length = textSource.text().length; - while (textSource.range && length > 0) { - const nodes = TextSourceRange.getNodesInRange(textSource.range); - if (!TextSourceRange.anyNodeMatchesSelector(nodes, this.ignoreNodes)) { - break; + if (this.ignoreNodes !== null && clonedTextSource.range) { + length = clonedTextSource.text().length; + while (clonedTextSource.range && length > 0) { + const nodes = TextSourceRange.getNodesInRange(clonedTextSource.range); + if (!TextSourceRange.anyNodeMatchesSelector(nodes, this.ignoreNodes)) { + break; + } + --length; + clonedTextSource.setEndOffset(length); } - --length; - textSource.setEndOffset(length); } + + return clonedTextSource.text(); } clearSelection(passive) { -- cgit v1.2.3