summaryrefslogtreecommitdiff
path: root/test/dom-text-scanner.test.js
diff options
context:
space:
mode:
authorJames Maa <jmaa@berkeley.edu>2024-06-16 18:39:44 -0700
committerGitHub <noreply@github.com>2024-06-17 01:39:44 +0000
commit1d2a91523604cce7dd608408da65bea5645fc7d4 (patch)
tree7c2b915dc29fffc707a5376002682256bce2c6de /test/dom-text-scanner.test.js
parent0283b298af2d04c7bc013fad7db5966d651f0142 (diff)
Fix DOMTextScanner entering initial div on backwards seeks (#1071)
* Fix dom-text-scanner entering initial div on reverse seeks * Add new case * Add launch.json * Cleanup launch.json
Diffstat (limited to 'test/dom-text-scanner.test.js')
-rw-r--r--test/dom-text-scanner.test.js111
1 files changed, 55 insertions, 56 deletions
diff --git a/test/dom-text-scanner.test.js b/test/dom-text-scanner.test.js
index e9d3405c..e204f2a3 100644
--- a/test/dom-text-scanner.test.js
+++ b/test/dom-text-scanner.test.js
@@ -112,56 +112,57 @@ function createAbsoluteGetComputedStyle(window) {
const domTestEnv = await setupDomTest(path.join(dirname, 'data/html/dom-text-scanner.html'));
-describe('DOMTextScanner', () => {
+describe('DOMTextScanner seek tests', () => {
const {window, teardown} = domTestEnv;
afterAll(() => teardown(global));
- test('Seek tests', () => {
- const {document} = window;
- window.getComputedStyle = createAbsoluteGetComputedStyle(window);
+ const {document} = window;
+ window.getComputedStyle = createAbsoluteGetComputedStyle(window);
- for (const testElement of /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll('test-case'))) {
- /** @type {import('test/dom-text-scanner').TestData|import('test/dom-text-scanner').TestData[]} */
- let testData = parseJson(/** @type {string} */ (testElement.dataset.testData));
- if (!Array.isArray(testData)) {
- testData = [testData];
- }
- for (const testDataItem of testData) {
- const {
- node: nodeSelector,
- offset,
- length,
- forcePreserveWhitespace,
- generateLayoutContent,
- reversible,
- expected: {
- node: expectedNodeSelector,
- offset: expectedOffset,
- content: expectedContent,
- remainder: expectedRemainder,
- },
- } = testDataItem;
-
- const node = querySelectorTextNode(testElement, nodeSelector);
- const expectedNode = querySelectorTextNode(testElement, expectedNodeSelector);
-
- expect(node).not.toEqual(null);
- expect(expectedNode).not.toEqual(null);
- if (node === null || expectedNode === null) { continue; }
-
- // Standard test
- {
- const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent);
- scanner.seek(length);
+ for (const testElement of /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll('test-case'))) {
+ const testDescription = testElement.querySelector('test-description')?.textContent || 'Test description not found.';
- const {node: actualNode1, offset: actualOffset1, content: actualContent1, remainder: actualRemainder1} = scanner;
- expect(actualContent1).toStrictEqual(expectedContent);
- expect(actualOffset1).toStrictEqual(expectedOffset);
- expect(actualNode1).toStrictEqual(expectedNode);
- expect(actualRemainder1).toStrictEqual(expectedRemainder || 0);
- }
+ /** @type {import('test/dom-text-scanner').TestData|import('test/dom-text-scanner').TestData[]} */
+ let testData = parseJson(/** @type {string} */ (testElement.dataset.testData));
+ if (!Array.isArray(testData)) {
+ testData = [testData];
+ }
- // Substring tests
+ for (const testDataItem of testData) {
+ const {
+ node: nodeSelector,
+ offset,
+ length,
+ forcePreserveWhitespace,
+ generateLayoutContent,
+ reversible,
+ expected: {
+ node: expectedNodeSelector,
+ offset: expectedOffset,
+ content: expectedContent,
+ remainder: expectedRemainder,
+ },
+ } = testDataItem;
+
+ const node = querySelectorTextNode(testElement, nodeSelector);
+ const expectedNode = querySelectorTextNode(testElement, expectedNodeSelector);
+
+ expect(node).not.toEqual(null);
+ expect(expectedNode).not.toEqual(null);
+ if (node === null || expectedNode === null) { continue; }
+
+ test(`${testDescription} standard test`, () => {
+ const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent);
+ scanner.seek(length);
+
+ const {node: actualNode1, offset: actualOffset1, content: actualContent1, remainder: actualRemainder1} = scanner;
+ expect(actualContent1).toStrictEqual(expectedContent);
+ expect(actualOffset1).toStrictEqual(expectedOffset);
+ expect(actualNode1).toStrictEqual(expectedNode);
+ expect(actualRemainder1).toStrictEqual(expectedRemainder || 0);
+ });
+
+ test(`${testDescription} substring test`, () => {
for (let i = 1; i <= length; ++i) {
const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent);
scanner.seek(length - i);
@@ -169,19 +170,17 @@ describe('DOMTextScanner', () => {
const {content: actualContent} = scanner;
expect(actualContent).toStrictEqual(expectedContent.substring(0, expectedContent.length - i));
}
+ });
- if (reversible === false) { continue; }
-
- // Reversed test
- {
- const scanner = new DOMTextScanner(expectedNode, expectedOffset, forcePreserveWhitespace, generateLayoutContent);
- scanner.seek(-length);
+ test.runIf(reversible)(`${testDescription} reversed test`, () => {
+ const scanner = new DOMTextScanner(expectedNode, expectedOffset, forcePreserveWhitespace, generateLayoutContent);
+ scanner.seek(-length);
- const {content: actualContent} = scanner;
- expect(actualContent).toStrictEqual(expectedContent);
- }
+ const {content: actualContent} = scanner;
+ expect(actualContent).toStrictEqual(expectedContent);
+ });
- // Reversed substring tests
+ test.runIf(reversible)(`${testDescription} reversed substring test`, () => {
for (let i = 1; i <= length; ++i) {
const scanner = new DOMTextScanner(expectedNode, expectedOffset, forcePreserveWhitespace, generateLayoutContent);
scanner.seek(-(length - i));
@@ -189,7 +188,7 @@ describe('DOMTextScanner', () => {
const {content: actualContent} = scanner;
expect(actualContent).toStrictEqual(expectedContent.substring(i));
}
- }
+ });
}
- });
+ }
});