Extract textSource text inside TextScanner (#500)

* extract text inside TextScanner

* clone textSource before text extraction
This commit is contained in:
siikamiika 2020-05-05 00:13:43 +03:00 committed by GitHub
parent 77b744e675
commit 7d7ff165ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 21 deletions

View File

@ -51,8 +51,7 @@ class QueryParser extends TextScanner {
async onSearchSource(textSource, cause) { async onSearchSource(textSource, cause) {
if (textSource === null) { return null; } if (textSource === null) { return null; }
this.setTextSourceScanLength(textSource, this.options.scanning.length); const searchText = this.getTextSourceContent(textSource, this.options.scanning.length);
const searchText = textSource.text();
if (searchText.length === 0) { return; } if (searchText.length === 0) { return; }
const {definitions, length} = await apiTermsFind(searchText, {}, this.getOptionsContext()); const {definitions, length} = await apiTermsFind(searchText, {}, this.getOptionsContext());

View File

@ -239,9 +239,7 @@ class Frontend {
} }
async findTerms(textSource, optionsContext) { async findTerms(textSource, optionsContext) {
this._textScanner.setTextSourceScanLength(textSource, this._options.scanning.length); const searchText = this._textScanner.getTextSourceContent(textSource, this._options.scanning.length);
const searchText = textSource.text();
if (searchText.length === 0) { return null; } if (searchText.length === 0) { return null; }
const {definitions, length} = await apiTermsFind(searchText, {}, optionsContext); const {definitions, length} = await apiTermsFind(searchText, {}, optionsContext);
@ -253,9 +251,7 @@ class Frontend {
} }
async findKanji(textSource, optionsContext) { async findKanji(textSource, optionsContext) {
this._textScanner.setTextSourceScanLength(textSource, 1); const searchText = this._textScanner.getTextSourceContent(textSource, 1);
const searchText = textSource.text();
if (searchText.length === 0) { return null; } if (searchText.length === 0) { return null; }
const definitions = await apiKanjiFind(searchText, optionsContext); const definitions = await apiKanjiFind(searchText, optionsContext);

View File

@ -318,23 +318,26 @@ class TextScanner extends EventDispatcher {
} }
} }
setTextSourceScanLength(textSource, length) { getTextSourceContent(textSource, length) {
textSource.setEndOffset(length); const clonedTextSource = textSource.clone();
if (this.ignoreNodes === null || !textSource.range) {
return;
}
length = textSource.text().length; clonedTextSource.setEndOffset(length);
while (textSource.range && length > 0) {
const nodes = TextSourceRange.getNodesInRange(textSource.range); 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)) { if (!TextSourceRange.anyNodeMatchesSelector(nodes, this.ignoreNodes)) {
break; break;
} }
--length; --length;
textSource.setEndOffset(length); clonedTextSource.setEndOffset(length);
} }
} }
return clonedTextSource.text();
}
clearSelection(passive) { clearSelection(passive) {
if (!this._canClearSelection) { return; } if (!this._canClearSelection) { return; }
if (this.textSourceCurrent !== null) { if (this.textSourceCurrent !== null) {