reimplement ignored chars inside source.js only

This commit is contained in:
siikamiika 2019-08-15 15:28:30 +03:00
parent 658e5ddff1
commit 0a9b673e27
4 changed files with 51 additions and 39 deletions

View File

@ -116,7 +116,7 @@ function docSentenceExtract(source, extent) {
const sourceLocal = source.clone(); const sourceLocal = source.clone();
const position = sourceLocal.setStartOffset(extent); const position = sourceLocal.setStartOffset(extent);
sourceLocal.setEndOffset(position + extent); sourceLocal.setEndOffset(position + extent);
const {text: content} = sourceLocal.text(); const content = sourceLocal.text();
let quoteStack = []; let quoteStack = [];

View File

@ -311,18 +311,10 @@ class Frontend {
async searchTerms(textSource) { async searchTerms(textSource) {
textSource.setEndOffset(this.options.scanning.length); textSource.setEndOffset(this.options.scanning.length);
const {text, strippedIndices} = textSource.text(); const {definitions, length} = await apiTermsFind(textSource.text());
let {definitions, length} = await apiTermsFind(text);
if (definitions.length === 0) { if (definitions.length === 0) {
return false; return false;
} }
for (let index of strippedIndices) {
if (index < length) {
length++;
} else {
break;
}
}
textSource.setEndOffset(length); textSource.setEndOffset(length);
@ -346,8 +338,7 @@ class Frontend {
async searchKanji(textSource) { async searchKanji(textSource) {
textSource.setEndOffset(1); textSource.setEndOffset(1);
const {text} = textSource.text(); const definitions = await apiKanjiFind(textSource.text());
const definitions = await apiKanjiFind(text);
if (definitions.length === 0) { if (definitions.length === 0) {
return false; return false;
} }

View File

@ -17,7 +17,7 @@
*/ */
// \u200c (Zero-width non-joiner) appears on Google Docs from Chrome 76 onwards // \u200c (Zero-width non-joiner) appears on Google Docs from Chrome 76 onwards
const IGNORE_TEXT_PATTERN = /\u200c/g; const IGNORE_TEXT_PATTERN = /\u200c/;
/* /*
@ -35,13 +35,7 @@ class TextSourceRange {
} }
text() { text() {
let strippedIndices = []; return this.content;
const text = this.content.replace(IGNORE_TEXT_PATTERN, (match, offset) => {
strippedIndices.push(offset);
return '';
});
return {text, strippedIndices};
} }
setEndOffset(length) { setEndOffset(length) {
@ -133,11 +127,23 @@ class TextSourceRange {
static seekForwardHelper(node, state) { static seekForwardHelper(node, state) {
if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) { if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) {
const offset = state.node === node ? state.offset : 0; const offset = state.node === node ? state.offset : 0;
const remaining = node.length - offset;
const consumed = Math.min(remaining, state.remainder); let consumed = 0;
state.content = state.content + node.nodeValue.substring(offset, offset + consumed); let stripped = 0;
while (state.remainder - consumed > 0) {
const currentChar = node.nodeValue[offset + consumed + stripped];
if (!currentChar) {
break;
} else if (currentChar.match(IGNORE_TEXT_PATTERN)) {
stripped++;
} else {
consumed++;
state.content += currentChar;
}
}
state.node = node; state.node = node;
state.offset = offset + consumed; state.offset = offset + consumed + stripped;
state.remainder -= consumed; state.remainder -= consumed;
} else if (TextSourceRange.shouldEnter(node)) { } else if (TextSourceRange.shouldEnter(node)) {
for (let i = 0; i < node.childNodes.length; ++i) { for (let i = 0; i < node.childNodes.length; ++i) {
@ -170,11 +176,23 @@ class TextSourceRange {
static seekBackwardHelper(node, state) { static seekBackwardHelper(node, state) {
if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) { if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) {
const offset = state.node === node ? state.offset : node.length; const offset = state.node === node ? state.offset : node.length;
const remaining = offset;
const consumed = Math.min(remaining, state.remainder); let consumed = 0;
state.content = node.nodeValue.substring(offset - consumed, offset) + state.content; let stripped = 0;
while (state.remainder - consumed > 0) {
const currentChar = node.nodeValue[offset - consumed - stripped]; // negative indices are undefined in JS
if (!currentChar) {
break;
} else if (currentChar.match(IGNORE_TEXT_PATTERN)) {
stripped++;
} else {
consumed++;
state.content = currentChar + state.content;
}
}
state.node = node; state.node = node;
state.offset = offset - consumed; state.offset = offset - consumed - stripped;
state.remainder -= consumed; state.remainder -= consumed;
} else if (TextSourceRange.shouldEnter(node)) { } else if (TextSourceRange.shouldEnter(node)) {
for (let i = node.childNodes.length - 1; i >= 0; --i) { for (let i = node.childNodes.length - 1; i >= 0; --i) {
@ -204,13 +222,7 @@ class TextSourceElement {
} }
text() { text() {
let strippedIndices = []; return this.content;
const text = this.content.replace(IGNORE_TEXT_PATTERN, (match, offset) => {
strippedIndices.push(offset);
return '';
});
return {text, strippedIndices};
} }
setEndOffset(length) { setEndOffset(length) {
@ -226,8 +238,18 @@ class TextSourceElement {
break; break;
} }
this.content = this.content || ''; let consumed = 0;
this.content = this.content.substring(0, length); let content = '';
for (let currentChar of this.content) {
if (consumed >= length) {
break;
} else if (!currentChar.match(IGNORE_TEXT_PATTERN)) {
consumed++;
content += currentChar;
}
}
this.content = content;
return this.content.length; return this.content.length;
} }

View File

@ -83,8 +83,7 @@ class Display {
const textSource = docRangeFromPoint({x: e.clientX, y: e.clientY}); const textSource = docRangeFromPoint({x: e.clientX, y: e.clientY});
textSource.setEndOffset(this.options.scanning.length); textSource.setEndOffset(this.options.scanning.length);
const {text} = textSource.text(); const {definitions, length} = await apiTermsFind(textSource.text());
const {definitions, length} = await apiTermsFind(text);
if (definitions.length === 0) { if (definitions.length === 0) {
return false; return false;
} }