diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2023-11-29 21:53:22 -0500 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2023-11-29 21:53:22 -0500 |
commit | f0c5ef69ca1758d5ef9201bc8a4375e5933888be (patch) | |
tree | dfbc206fccaac88d59dac7ed90eca87f5fc8044d /test/dom-text-scanner.test.js | |
parent | bfcc9d3fba4a3ff85be167eb771a4c0f26f9cc49 (diff) |
Update types
Diffstat (limited to 'test/dom-text-scanner.test.js')
-rw-r--r-- | test/dom-text-scanner.test.js | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/test/dom-text-scanner.test.js b/test/dom-text-scanner.test.js index d1b31276..6d0c047b 100644 --- a/test/dom-text-scanner.test.js +++ b/test/dom-text-scanner.test.js @@ -22,11 +22,21 @@ import path from 'path'; import {expect, test} from 'vitest'; import {DOMTextScanner} from '../ext/js/dom/dom-text-scanner.js'; + +/** + * @param {string} fileName + * @returns {JSDOM} + */ function createJSDOM(fileName) { const domSource = fs.readFileSync(fileName, {encoding: 'utf8'}); return new JSDOM(domSource); } +/** + * @param {Element} element + * @param {string} selector + * @returns {?Node} + */ function querySelectorTextNode(element, selector) { let textIndex = -1; const match = /::text$|::nth-text\((\d+)\)$/.exec(selector); @@ -35,13 +45,16 @@ function querySelectorTextNode(element, selector) { 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 === n.constructor.TEXT_NODE) { + if (n.nodeType === /** @type {typeof Node} */ (n.constructor).TEXT_NODE) { if (textIndex === 0) { - return n; + return /** @type {Text} */ (n); } --textIndex; } @@ -50,10 +63,16 @@ function querySelectorTextNode(element, selector) { } +/** + * @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(element).fontSize; + const fontSize = getComputedStyle(/** @type {Element} */ (element)).fontSize; if (fontSize.endsWith('px')) { const value = parseFloat(fontSize.substring(0, fontSize.length - 2)); return value; @@ -64,14 +83,19 @@ function getComputedFontSizeInPixels(window, getComputedStyle, element) { return defaultFontSize; } +/** + * @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 = 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); @@ -85,12 +109,15 @@ function createAbsoluteGetComputedStyle(window) { } +/** + * @param {JSDOM} dom + */ async function testDomTextScanner(dom) { const document = dom.window.document; test('DomTextScanner', () => { - for (const testElement of document.querySelectorAll('y-test')) { - let testData = JSON.parse(testElement.dataset.testData); + for (const testElement of /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll('y-test'))) { + let testData = JSON.parse(/** @type {string} */ (testElement.dataset.testData)); if (!Array.isArray(testData)) { testData = [testData]; } @@ -159,19 +186,21 @@ async function testDomTextScanner(dom) { } +/** */ async function testDocument1() { const dom = createJSDOM(path.join(__dirname, 'data', 'html', 'test-dom-text-scanner.html')); const window = dom.window; try { window.getComputedStyle = createAbsoluteGetComputedStyle(window); - await testDomTextScanner(dom, {DOMTextScanner}); + await testDomTextScanner(dom); } finally { window.close(); } } +/** */ async function main() { await testDocument1(); } |