From f7df6254d6f71d5331b000dcbd27271bd2c3006f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 6 May 2020 19:33:17 -0400 Subject: [PATCH] Dom text scanner fixes (#505) * Fix test case * Add test-dom-text-scanner * Ensure that DOMTextScanner._node never becomes null * Add remainder --- ext/fg/js/dom-text-scanner.js | 15 ++++++++++++++- package.json | 2 +- test/data/html/test-dom-text-scanner.html | 2 +- test/test-dom-text-scanner.js | 6 ++++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ext/fg/js/dom-text-scanner.js b/ext/fg/js/dom-text-scanner.js index 2de65041..8fa67ede 100644 --- a/ext/fg/js/dom-text-scanner.js +++ b/ext/fg/js/dom-text-scanner.js @@ -59,6 +59,15 @@ class DOMTextScanner { return this._offset; } + /** + * Gets the remaining number of characters that weren't scanned in the last seek() call. + * This value is usually 0 unless the end of the document was reached. + * @returns An integer. + */ + get remainder() { + return this._remainder; + } + /** * Gets the accumulated content string resulting from calls to seek(). * @returns A string. @@ -85,6 +94,7 @@ class DOMTextScanner { const generateLayoutContent = this._generateLayoutContent; let node = this._node; + let lastNode = node; let resetOffset = this._resetOffset; let newlines = 0; while (node !== null) { @@ -92,6 +102,7 @@ class DOMTextScanner { const nodeType = node.nodeType; if (nodeType === TEXT_NODE) { + lastNode = node; if (!( forward ? this._seekTextNodeForward(node, resetOffset) : @@ -101,6 +112,8 @@ class DOMTextScanner { break; } } else if (nodeType === ELEMENT_NODE) { + lastNode = node; + this._offset = 0; [enterable, newlines] = DOMTextScanner.getElementSeekInfo(node); if (newlines > this._newlines && generateLayoutContent) { this._newlines = newlines; @@ -121,7 +134,7 @@ class DOMTextScanner { resetOffset = true; } - this._node = node; + this._node = lastNode; this._resetOffset = resetOffset; return this; diff --git a/package.json b/package.json index 0729cda1..1aa6c856 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "scripts": { "test": "npm run test-lint && npm run test-code", "test-lint": "eslint . && node ./test/lint/global-declarations.js", - "test-code": "node ./test/test-schema.js && node ./test/test-dictionary.js && node ./test/test-database.js && node ./test/test-document.js && node ./test/test-object-property-accessor.js && node ./test/test-japanese.js && node ./test/test-text-source-map.js" + "test-code": "node ./test/test-schema.js && node ./test/test-dictionary.js && node ./test/test-database.js && node ./test/test-document.js && node ./test/test-object-property-accessor.js && node ./test/test-japanese.js && node ./test/test-text-source-map.js && node ./test/test-dom-text-scanner.js" }, "repository": { "type": "git", diff --git a/test/data/html/test-dom-text-scanner.html b/test/data/html/test-dom-text-scanner.html index 6b78570a..dc06eb64 100644 --- a/test/data/html/test-dom-text-scanner.html +++ b/test/data/html/test-dom-text-scanner.html @@ -85,7 +85,7 @@ "expected": { "node": "span:nth-of-type(2)::text", "offset": 6, - "content": "小ぢんまり1\n小ぢんまり2" + "content": "小ぢんまり1 小ぢんまり2" } }' > diff --git a/test/test-dom-text-scanner.js b/test/test-dom-text-scanner.js index 41d6e307..7374ff87 100644 --- a/test/test-dom-text-scanner.js +++ b/test/test-dom-text-scanner.js @@ -103,7 +103,8 @@ async function testDomTextScanner(dom, {DOMTextScanner}) { expected: { node: expectedNode, offset: expectedOffset, - content: expectedContent + content: expectedContent, + remainder: expectedRemainder } } = testDataItem; @@ -115,10 +116,11 @@ async function testDomTextScanner(dom, {DOMTextScanner}) { const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent); scanner.seek(length); - const {node: actualNode1, offset: actualOffset1, content: actualContent1} = scanner; + const {node: actualNode1, offset: actualOffset1, content: actualContent1, remainder: actualRemainder1} = scanner; assert.strictEqual(actualContent1, expectedContent); assert.strictEqual(actualOffset1, expectedOffset); assert.strictEqual(actualNode1, expectedNode); + assert.strictEqual(actualRemainder1, expectedRemainder || 0); } // Substring tests