Merge pull request #181 from siikamiika/fix-zwnj

ignore zero-width non-joiner
This commit is contained in:
Alex Yatskov 2019-08-17 09:03:26 -07:00 committed by GitHub
commit 6a96555d4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
// \u200c (Zero-width non-joiner) appears on Google Docs from Chrome 76 onwards
const IGNORE_TEXT_PATTERN = /\u200c/;
/* /*
* TextSourceRange * TextSourceRange
@ -124,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) {
@ -161,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) {
@ -211,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;
} }