Dom text scanner fixes (#505)

* Fix test case

* Add test-dom-text-scanner

* Ensure that DOMTextScanner._node never becomes null

* Add remainder
This commit is contained in:
toasted-nutbread 2020-05-06 19:33:17 -04:00 committed by GitHub
parent bb2d9501af
commit f7df6254d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 5 deletions

View File

@ -59,6 +59,15 @@ class DOMTextScanner {
return this._offset; 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(). * Gets the accumulated content string resulting from calls to seek().
* @returns A string. * @returns A string.
@ -85,6 +94,7 @@ class DOMTextScanner {
const generateLayoutContent = this._generateLayoutContent; const generateLayoutContent = this._generateLayoutContent;
let node = this._node; let node = this._node;
let lastNode = node;
let resetOffset = this._resetOffset; let resetOffset = this._resetOffset;
let newlines = 0; let newlines = 0;
while (node !== null) { while (node !== null) {
@ -92,6 +102,7 @@ class DOMTextScanner {
const nodeType = node.nodeType; const nodeType = node.nodeType;
if (nodeType === TEXT_NODE) { if (nodeType === TEXT_NODE) {
lastNode = node;
if (!( if (!(
forward ? forward ?
this._seekTextNodeForward(node, resetOffset) : this._seekTextNodeForward(node, resetOffset) :
@ -101,6 +112,8 @@ class DOMTextScanner {
break; break;
} }
} else if (nodeType === ELEMENT_NODE) { } else if (nodeType === ELEMENT_NODE) {
lastNode = node;
this._offset = 0;
[enterable, newlines] = DOMTextScanner.getElementSeekInfo(node); [enterable, newlines] = DOMTextScanner.getElementSeekInfo(node);
if (newlines > this._newlines && generateLayoutContent) { if (newlines > this._newlines && generateLayoutContent) {
this._newlines = newlines; this._newlines = newlines;
@ -121,7 +134,7 @@ class DOMTextScanner {
resetOffset = true; resetOffset = true;
} }
this._node = node; this._node = lastNode;
this._resetOffset = resetOffset; this._resetOffset = resetOffset;
return this; return this;

View File

@ -8,7 +8,7 @@
"scripts": { "scripts": {
"test": "npm run test-lint && npm run test-code", "test": "npm run test-lint && npm run test-code",
"test-lint": "eslint . && node ./test/lint/global-declarations.js", "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": { "repository": {
"type": "git", "type": "git",

View File

@ -85,7 +85,7 @@
"expected": { "expected": {
"node": "span:nth-of-type(2)::text", "node": "span:nth-of-type(2)::text",
"offset": 6, "offset": 6,
"content": "小ぢんまり1\n小ぢんまり2" "content": "小ぢんまり1 小ぢんまり2"
} }
}' }'
> >

View File

@ -103,7 +103,8 @@ async function testDomTextScanner(dom, {DOMTextScanner}) {
expected: { expected: {
node: expectedNode, node: expectedNode,
offset: expectedOffset, offset: expectedOffset,
content: expectedContent content: expectedContent,
remainder: expectedRemainder
} }
} = testDataItem; } = testDataItem;
@ -115,10 +116,11 @@ async function testDomTextScanner(dom, {DOMTextScanner}) {
const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent); const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent);
scanner.seek(length); 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(actualContent1, expectedContent);
assert.strictEqual(actualOffset1, expectedOffset); assert.strictEqual(actualOffset1, expectedOffset);
assert.strictEqual(actualNode1, expectedNode); assert.strictEqual(actualNode1, expectedNode);
assert.strictEqual(actualRemainder1, expectedRemainder || 0);
} }
// Substring tests // Substring tests