summaryrefslogtreecommitdiff
path: root/test/dom-text-scanner.test.js
blob: 1ec7cab71090a3bfce5c84fecd38d2e7619c4bce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2023-2024  Yomitan Authors
 * Copyright (C) 2020-2022  Yomichan Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import {fileURLToPath} from 'node:url';
import path from 'path';
import {afterAll, describe, expect, test} from 'vitest';
import {parseJson} from '../dev/json.js';
import {DOMTextScanner} from '../ext/js/dom/dom-text-scanner.js';
import {setupDomTest} from './fixtures/dom-test.js';

const dirname = path.dirname(fileURLToPath(import.meta.url));

/**
 * @param {Element} element
 * @param {string} selector
 * @returns {?Node}
 */
function querySelectorTextNode(element, selector) {
    let textIndex = -1;
    const match = /::text$|::nth-text\((\d+)\)$/.exec(selector);
    if (match !== null) {
        textIndex = (match[1] ? Number.parseInt(match[1], 10) - 1 : 0);
        selector = selector.substring(0, selector.length - match[0].length);
    }
    const result = element.querySelector(selector);
    if (result === null) {
        return null;
    }
    if (textIndex < 0) {
        return result;
    }
    for (let n = result.firstChild; n !== null; n = n.nextSibling) {
        if (n.nodeType === /** @type {typeof Node} */ (n.constructor).TEXT_NODE) {
            if (textIndex === 0) {
                return /** @type {Text} */ (n);
            }
            --textIndex;
        }
    }
    return null;
}


/**
 * @param {import('jsdom').DOMWindow} window
 * @param {(element: Element) => CSSStyleDeclaration} getComputedStyle
 * @param {?Node} element
 * @returns {number}
 */
function getComputedFontSizeInPixels(window, getComputedStyle, element) {
    for (; element !== null; element = element.parentNode) {
        if (element.nodeType === window.Node.ELEMENT_NODE) {
            const fontSize = getComputedStyle(/** @type {Element} */ (element)).fontSize;
            if (fontSize.endsWith('px')) {
                return Number.parseFloat(fontSize.substring(0, fontSize.length - 2));
            }
        }
    }
    return 14; // Default font size
}

/**
 * @param {import('jsdom').DOMWindow} window
 * @returns {(element: Element, pseudoElement?: ?string) => CSSStyleDeclaration}
 */
function createAbsoluteGetComputedStyle(window) {
    // Wrapper to convert em units to px units
    const getComputedStyleOld = window.getComputedStyle.bind(window);
    /** @type {(element: Element, pseudoElement?: ?string) => CSSStyleDeclaration} */
    return (element, ...args) => {
        const style = getComputedStyleOld(element, ...args);
        return new Proxy(style, {
            get: (target, property) => {
                let result = /** @type {import('core').SafeAny} */ (target)[property];
                if (typeof result === 'string') {
                    result = result.replace(/([-+]?\d(?:\.\d)?(?:[eE][-+]?\d+)?)em/g, (g0, g1) => {
                        const fontSize = getComputedFontSizeInPixels(window, getComputedStyleOld, element);
                        return `${Number.parseFloat(g1) * fontSize}px`;
                    });
                }
                return result;
            }
        });
    };
}

const domTestEnv = await setupDomTest(path.join(dirname, 'data/html/dom-text-scanner.html'));

describe('DOMTextScanner', () => {
    const {window, teardown} = domTestEnv;
    afterAll(() => teardown(global));

    test('Seek tests', () => {
        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);

                    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);
                }

                // Substring tests
                for (let i = 1; i <= length; ++i) {
                    const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent);
                    scanner.seek(length - i);

                    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);

                    const {content: actualContent} = scanner;
                    expect(actualContent).toStrictEqual(expectedContent);
                }

                // Reversed substring tests
                for (let i = 1; i <= length; ++i) {
                    const scanner = new DOMTextScanner(expectedNode, expectedOffset, forcePreserveWhitespace, generateLayoutContent);
                    scanner.seek(-(length - i));

                    const {content: actualContent} = scanner;
                    expect(actualContent).toStrictEqual(expectedContent.substring(i));
                }
            }
        }
    });
});