From f0c5ef69ca1758d5ef9201bc8a4375e5933888be Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 29 Nov 2023 21:53:22 -0500 Subject: Update types --- test/dom-text-scanner.test.js | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'test/dom-text-scanner.test.js') 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} */ (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(); } -- cgit v1.2.3 From 338d210b66006189a0b775e8bd524c1cc11c2d9a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 30 Nov 2023 20:00:46 -0500 Subject: Update paths --- dev/dictionary-validate.js | 5 ++++- dev/generate-css-json.js | 15 +++++++++------ test/anki-note-builder.test.js | 6 +++--- test/database.test.js | 5 ++++- test/deinflector.test.js | 5 ++++- test/dom-text-scanner.test.js | 4 +++- 6 files changed, 27 insertions(+), 13 deletions(-) (limited to 'test/dom-text-scanner.test.js') diff --git a/dev/dictionary-validate.js b/dev/dictionary-validate.js index b3654e75..a6948bfe 100644 --- a/dev/dictionary-validate.js +++ b/dev/dictionary-validate.js @@ -20,14 +20,17 @@ import fs from 'fs'; import JSZip from 'jszip'; import path from 'path'; import {performance} from 'perf_hooks'; +import {fileURLToPath} from 'url'; import {createJsonSchema} from './schema-validate.js'; +const dirname = path.dirname(fileURLToPath(import.meta.url)); + /** * @param {string} relativeFileName * @returns {import('dev/dictionary-validate').Schema} */ function readSchema(relativeFileName) { - const fileName = path.join(__dirname, relativeFileName); + const fileName = path.join(dirname, relativeFileName); const source = fs.readFileSync(fileName, {encoding: 'utf8'}); return JSON.parse(source); } diff --git a/dev/generate-css-json.js b/dev/generate-css-json.js index 02e54530..e5d4d7f0 100644 --- a/dev/generate-css-json.js +++ b/dev/generate-css-json.js @@ -19,6 +19,9 @@ import css from 'css'; import fs from 'fs'; import path from 'path'; +import {fileURLToPath} from 'url'; + +const dirname = path.dirname(fileURLToPath(import.meta.url)); /** * @returns {{cssFile: string, overridesCssFile: string, outputPath: string}[]} @@ -26,14 +29,14 @@ import path from 'path'; export function getTargets() { return [ { - cssFile: path.join(__dirname, '..', 'ext/css/structured-content.css'), - overridesCssFile: path.join(__dirname, 'data/structured-content-overrides.css'), - outputPath: path.join(__dirname, '..', 'ext/data/structured-content-style.json') + cssFile: path.join(dirname, '..', 'ext/css/structured-content.css'), + overridesCssFile: path.join(dirname, 'data/structured-content-overrides.css'), + outputPath: path.join(dirname, '..', 'ext/data/structured-content-style.json') }, { - cssFile: path.join(__dirname, '..', 'ext/css/display-pronunciation.css'), - overridesCssFile: path.join(__dirname, 'data/display-pronunciation-overrides.css'), - outputPath: path.join(__dirname, '..', 'ext/data/pronunciation-style.json') + cssFile: path.join(dirname, '..', 'ext/css/display-pronunciation.css'), + overridesCssFile: path.join(dirname, 'data/display-pronunciation-overrides.css'), + outputPath: path.join(dirname, '..', 'ext/data/pronunciation-style.json') } ]; } diff --git a/test/anki-note-builder.test.js b/test/anki-note-builder.test.js index e4af7943..96dacab6 100644 --- a/test/anki-note-builder.test.js +++ b/test/anki-note-builder.test.js @@ -26,12 +26,14 @@ import {TranslatorVM} from '../dev/translator-vm.js'; import {AnkiNoteBuilder} from '../ext/js/data/anki-note-builder.js'; import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js'; +const dirname = path.dirname(fileURLToPath(import.meta.url)); + /** * @param {string} url2 * @returns {Promise} */ async function fetch(url2) { - const extDir = path.join(__dirname, '..', 'ext'); + const extDir = path.join(dirname, '..', 'ext'); let filePath; try { filePath = url.fileURLToPath(url2); @@ -51,8 +53,6 @@ async function fetch(url2) { vi.stubGlobal('fetch', fetch); vi.mock('../ext/js/templates/template-renderer-proxy.js'); -const dirname = path.dirname(fileURLToPath(import.meta.url)); - /** * @returns {Promise} */ diff --git a/test/database.test.js b/test/database.test.js index ee818467..30854d55 100644 --- a/test/database.test.js +++ b/test/database.test.js @@ -18,12 +18,15 @@ import {IDBFactory, IDBKeyRange} from 'fake-indexeddb'; import path from 'path'; +import {fileURLToPath} from 'node:url'; import {beforeEach, describe, expect, test, vi} from 'vitest'; import {createDictionaryArchive} from '../dev/util.js'; import {DictionaryDatabase} from '../ext/js/language/dictionary-database.js'; import {DictionaryImporterMediaLoader} from '../ext/js/language/dictionary-importer-media-loader.js'; import {DictionaryImporter} from '../ext/js/language/dictionary-importer.js'; +const dirname = path.dirname(fileURLToPath(import.meta.url)); + vi.stubGlobal('IDBKeyRange', IDBKeyRange); vi.mock('../ext/js/language/dictionary-importer-media-loader.js'); @@ -34,7 +37,7 @@ vi.mock('../ext/js/language/dictionary-importer-media-loader.js'); * @returns {import('jszip')} */ function createTestDictionaryArchive(dictionary, dictionaryName) { - const dictionaryDirectory = path.join(__dirname, 'data', 'dictionaries', dictionary); + const dictionaryDirectory = path.join(dirname, 'data', 'dictionaries', dictionary); return createDictionaryArchive(dictionaryDirectory, dictionaryName); } diff --git a/test/deinflector.test.js b/test/deinflector.test.js index 77184799..a69f8e56 100644 --- a/test/deinflector.test.js +++ b/test/deinflector.test.js @@ -20,6 +20,9 @@ import fs from 'fs'; import path from 'path'; import {describe, expect, test} from 'vitest'; import {Deinflector} from '../ext/js/language/deinflector.js'; +import {fileURLToPath} from 'node:url'; + +const dirname = path.dirname(fileURLToPath(import.meta.url)); /** * @param {Deinflector} deinflector @@ -924,7 +927,7 @@ function testDeinflections() { } ]; - const deinflectionReasons = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'ext', 'data/deinflect.json'), {encoding: 'utf8'})); + const deinflectionReasons = JSON.parse(fs.readFileSync(path.join(dirname, '..', 'ext', 'data/deinflect.json'), {encoding: 'utf8'})); const deinflector = new Deinflector(deinflectionReasons); describe('deinflections', () => { diff --git a/test/dom-text-scanner.test.js b/test/dom-text-scanner.test.js index 6d0c047b..30aec33e 100644 --- a/test/dom-text-scanner.test.js +++ b/test/dom-text-scanner.test.js @@ -19,9 +19,11 @@ import fs from 'fs'; import {JSDOM} from 'jsdom'; import path from 'path'; +import {fileURLToPath} from 'node:url'; import {expect, test} from 'vitest'; import {DOMTextScanner} from '../ext/js/dom/dom-text-scanner.js'; +const dirname = path.dirname(fileURLToPath(import.meta.url)); /** * @param {string} fileName @@ -188,7 +190,7 @@ async function testDomTextScanner(dom) { /** */ async function testDocument1() { - const dom = createJSDOM(path.join(__dirname, 'data', 'html', 'test-dom-text-scanner.html')); + const dom = createJSDOM(path.join(dirname, 'data', 'html', 'test-dom-text-scanner.html')); const window = dom.window; try { window.getComputedStyle = createAbsoluteGetComputedStyle(window); -- cgit v1.2.3 From 083b4749139213c6fefe80166d73f54604a85267 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 3 Dec 2023 10:45:08 -0500 Subject: Fix some import orderings --- dev/bin/build.js | 4 ++-- ext/js/background/offscreen-proxy.js | 2 +- ext/js/language/dictionary-importer.js | 2 +- test/database.test.js | 2 +- test/deinflector.test.js | 2 +- test/dom-text-scanner.test.js | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) (limited to 'test/dom-text-scanner.test.js') diff --git a/dev/bin/build.js b/dev/bin/build.js index 47c08f3c..deb82618 100644 --- a/dev/bin/build.js +++ b/dev/bin/build.js @@ -19,10 +19,10 @@ import assert from 'assert'; import childProcess from 'child_process'; import fs from 'fs'; -import path from 'path'; -import readline from 'readline'; import JSZip from 'jszip'; import {fileURLToPath} from 'node:url'; +import path from 'path'; +import readline from 'readline'; import {buildLibs} from '../build-libs.js'; import {ManifestUtil} from '../manifest-util.js'; import {getAllFiles, getArgs, testMain} from '../util.js'; diff --git a/ext/js/background/offscreen-proxy.js b/ext/js/background/offscreen-proxy.js index 757d78d5..7b504855 100644 --- a/ext/js/background/offscreen-proxy.js +++ b/ext/js/background/offscreen-proxy.js @@ -17,8 +17,8 @@ */ import {isObject} from '../core.js'; -import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js'; import {ExtensionError} from '../core/extension-error.js'; +import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js'; export class OffscreenProxy { constructor() { diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js index 115e0726..08fcf86b 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -25,8 +25,8 @@ import { configure } from '../../lib/zip.js'; import {stringReverse} from '../core.js'; -import {MediaUtil} from '../media/media-util.js'; import {ExtensionError} from '../core/extension-error.js'; +import {MediaUtil} from '../media/media-util.js'; const ajvSchemas = /** @type {import('dictionary-importer').CompiledSchemaValidators} */ (/** @type {unknown} */ (ajvSchemas0)); const BlobWriter = /** @type {typeof import('@zip.js/zip.js').BlobWriter} */ (/** @type {unknown} */ (BlobWriter0)); diff --git a/test/database.test.js b/test/database.test.js index 30854d55..80871f95 100644 --- a/test/database.test.js +++ b/test/database.test.js @@ -17,8 +17,8 @@ */ import {IDBFactory, IDBKeyRange} from 'fake-indexeddb'; -import path from 'path'; import {fileURLToPath} from 'node:url'; +import path from 'path'; import {beforeEach, describe, expect, test, vi} from 'vitest'; import {createDictionaryArchive} from '../dev/util.js'; import {DictionaryDatabase} from '../ext/js/language/dictionary-database.js'; diff --git a/test/deinflector.test.js b/test/deinflector.test.js index a69f8e56..bd538428 100644 --- a/test/deinflector.test.js +++ b/test/deinflector.test.js @@ -17,10 +17,10 @@ */ import fs from 'fs'; +import {fileURLToPath} from 'node:url'; import path from 'path'; import {describe, expect, test} from 'vitest'; import {Deinflector} from '../ext/js/language/deinflector.js'; -import {fileURLToPath} from 'node:url'; const dirname = path.dirname(fileURLToPath(import.meta.url)); diff --git a/test/dom-text-scanner.test.js b/test/dom-text-scanner.test.js index 30aec33e..f6a7410a 100644 --- a/test/dom-text-scanner.test.js +++ b/test/dom-text-scanner.test.js @@ -18,8 +18,8 @@ import fs from 'fs'; import {JSDOM} from 'jsdom'; -import path from 'path'; import {fileURLToPath} from 'node:url'; +import path from 'path'; import {expect, test} from 'vitest'; import {DOMTextScanner} from '../ext/js/dom/dom-text-scanner.js'; -- cgit v1.2.3