From 9b58a5e685b3fe543967c4922bddfc23b885eb04 Mon Sep 17 00:00:00 2001 From: Darius Jahandarie Date: Fri, 13 Dec 2024 19:57:23 +0900 Subject: [PATCH] clean up css, make tests pass --- .eslintrc.json | 5 + dev/data/structured-content-overrides.css | 8 - ext/css/structured-content.css | 36 ----- ext/data/structured-content-style.json | 137 +----------------- ext/js/dictionary/dictionary-database.js | 1 + test/data/anki-note-builder-test-results.json | 24 +-- test/data/translator-test-results.json | 6 +- test/database.test.js | 2 + test/dictionary-data.test.js | 4 + test/dictionary-data.write.js | 3 + test/utilities/database.js | 36 +++++ 11 files changed, 72 insertions(+), 190 deletions(-) create mode 100644 test/utilities/database.js diff --git a/.eslintrc.json b/.eslintrc.json index 461d1def97..3c5ab7d96b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -551,10 +551,12 @@ { "files": [ "ext/js/core/api-map.js", + "ext/js/core/event-listener-collection.js", "ext/js/core/extension-error.js", "ext/js/core/json.js", "ext/js/data/anki-note-data-creator.js", "ext/js/dictionary/dictionary-data-util.js", + "ext/js/display/display-content-manager.js", "ext/js/display/pronunciation-generator.js", "ext/js/display/structured-content-generator.js", "ext/js/dom/css-style-applier.js", @@ -575,6 +577,7 @@ }, { "files": [ + "ext/js/core/api-map.js", "ext/js/core/event-dispatcher.js", "ext/js/core/extension-error.js", "ext/js/core/json.js", @@ -582,6 +585,7 @@ "ext/js/core/to-error.js", "ext/js/core/utilities.js", "ext/js/data/database.js", + "ext/js/dictionary/dictionary-database.js", "ext/js/dictionary/dictionary-importer.js", "ext/js/dictionary/dictionary-worker-handler.js", "ext/js/dictionary/dictionary-worker-main.js", @@ -623,6 +627,7 @@ "ext/js/data/json-schema.js", "ext/js/data/options-util.js", "ext/js/data/permissions-util.js", + "ext/js/dictionary/dictionary-database.js", "ext/js/dom/native-simple-dom-parser.js", "ext/js/dom/simple-dom-parser.js", "ext/js/extension/environment.js", diff --git a/dev/data/structured-content-overrides.css b/dev/data/structured-content-overrides.css index dca2b18396..0878eede20 100644 --- a/dev/data/structured-content-overrides.css +++ b/dev/data/structured-content-overrides.css @@ -28,18 +28,10 @@ .gloss-image-link:hover { /* remove-rule */ } -.gloss-image-container-overlay { - font-size: initial; - line-height: initial; - color: initial; -} :root[data-browser=firefox] .gloss-image-link[data-image-rendering=crisp-edges] .gloss-image, :root[data-browser=firefox-mobile] .gloss-image-link[data-image-rendering=crisp-edges] .gloss-image { /* remove-rule */ } -.gloss-image-link-text { - line-height: initial; -} .gloss-sc-thead, .gloss-sc-tfoot, .gloss-sc-th { diff --git a/ext/css/structured-content.css b/ext/css/structured-content.css index db15d4980d..a79d9ddc4e 100644 --- a/ext/css/structured-content.css +++ b/ext/css/structured-content.css @@ -19,14 +19,6 @@ /* Glossary images */ .gloss-image-container { display: inline-block; - white-space: nowrap; - max-width: 100%; - max-height: 100vh; - position: relative; - vertical-align: top; - line-height: 0; - font-size: calc(1em / var(--font-size-no-units)); - overflow: hidden; } .gloss-image-link[data-background=true]>.gloss-image-container { background-color: var(--gloss-image-background-color); @@ -45,28 +37,6 @@ .gloss-image-link[href]:hover { cursor: pointer; } -.gloss-image-container-overlay { - position: absolute; - left: 0; - top: 0; - width: 100%; - height: 100%; - font-size: calc(1em * var(--font-size-no-units)); - line-height: var(--line-height); - display: table; - table-layout: fixed; - white-space: normal; - color: var(--text-color-light3); -} -.gloss-image-link[data-has-image=true][data-image-load-state=load-error] .gloss-image-container-overlay::after { - content: 'Image failed to load'; - display: table-cell; - width: 100%; - height: 100%; - vertical-align: middle; - text-align: center; - padding: 0.25em; -} .gloss-image { display: inline-block; vertical-align: top; @@ -92,12 +62,6 @@ :root[data-browser=firefox-mobile] .gloss-image-link[data-image-rendering=crisp-edges] .gloss-image { image-rendering: auto; } -.gloss-image-link[data-has-aspect-ratio=true] .gloss-image-sizer { - display: inline-block; - width: 0; - vertical-align: top; - font-size: 0; -} .gloss-image-link[data-appearance=monochrome] .gloss-image { filter: grayscale(1); diff --git a/ext/data/structured-content-style.json b/ext/data/structured-content-style.json index c5bb7d38a3..47b1e99513 100644 --- a/ext/data/structured-content-style.json +++ b/ext/data/structured-content-style.json @@ -2,7 +2,8 @@ { "selectors": [".gloss-image-container"], "styles": [ - ["display", "inline-block"] + ["display", "inline-block"], + ["font-size", "1px"] ] }, { @@ -22,56 +23,6 @@ ["cursor", "pointer"] ] }, - { - "selectors": [".gloss-image-container-overlay"], - "styles": [ - ["position", "absolute"], - ["left", "0"], - ["top", "0"], - ["width", "100%"], - ["height", "100%"], - ["display", "table"], - ["table-layout", "fixed"], - ["white-space", "normal"], - ["font-size", "initial"], - ["line-height", "initial"], - ["color", "initial"] - ] - }, - { - "selectors": [".gloss-image-link[data-has-image=true][data-image-load-state=load-error] .gloss-image-container-overlay::after"], - "styles": [ - ["content", "'Image failed to load'"], - ["display", "table-cell"], - ["width", "100%"], - ["height", "100%"], - ["vertical-align", "middle"], - ["text-align", "center"], - ["padding", "0.25em"] - ] - }, - { - "selectors": [".gloss-image-background"], - "styles": [ - ["--image", "none"], - ["position", "absolute"], - ["left", "0"], - ["top", "0"], - ["width", "100%"], - ["height", "100%"], - ["-webkit-mask-repeat", "no-repeat"], - ["-webkit-mask-position", "center center"], - ["-webkit-mask-mode", "alpha"], - ["-webkit-mask-size", "contain"], - ["-webkit-mask-image", "var(--image)"], - ["mask-repeat", "no-repeat"], - ["mask-position", "center center"], - ["mask-mode", "alpha"], - ["mask-size", "contain"], - ["mask-image", "var(--image)"], - ["background-color", "currentColor"] - ] - }, { "selectors": [".gloss-image"], "styles": [ @@ -79,25 +30,12 @@ ["vertical-align", "top"], ["object-fit", "contain"], ["border", "none"], - ["outline", "none"] + ["outline", "none"], + ["width", "100%"] ] }, { - "selectors": [".gloss-image-link[data-has-aspect-ratio=true] .gloss-image"], - "styles": [ - ] - }, - { - "selectors": [".gloss-image:not([src])"], - "styles": [ - ["display", "none"] - ] - }, - { - "selectors": [ - ".gloss-image-link[data-image-rendering=pixelated] .gloss-image", - ".gloss-image-link[data-image-rendering=pixelated] .gloss-image-background" - ], + "selectors": [".gloss-image-link[data-image-rendering=pixelated] .gloss-image"], "styles": [ ["image-rendering", "auto"], ["image-rendering", "-moz-crisp-edges"], @@ -107,10 +45,7 @@ ] }, { - "selectors": [ - ".gloss-image-link[data-image-rendering=crisp-edges] .gloss-image", - ".gloss-image-link[data-image-rendering=crisp-edges] .gloss-image-background" - ], + "selectors": [".gloss-image-link[data-image-rendering=crisp-edges] .gloss-image"], "styles": [ ["image-rendering", "auto"], ["image-rendering", "-moz-crisp-edges"], @@ -118,50 +53,10 @@ ["image-rendering", "crisp-edges"] ] }, - { - "selectors": [".gloss-image-link[data-has-aspect-ratio=true] .gloss-image-sizer"], - "styles": [ - ["display", "inline-block"], - ["width", "0"], - ["vertical-align", "top"], - ["font-size", "0"] - ] - }, - { - "selectors": [".gloss-image-link-text"], - "styles": [ - ["display", "none"], - ["line-height", "initial"] - ] - }, - { - "selectors": [".gloss-image-link-text::before"], - "styles": [ - ["content", "'['"] - ] - }, - { - "selectors": [".gloss-image-link-text::after"], - "styles": [ - ["content", "']'"] - ] - }, - { - "selectors": [".gloss-image-description"], - "styles": [ - ["display", "block"], - ["white-space", "pre-line"] - ] - }, { "selectors": [".gloss-image-link[data-appearance=monochrome] .gloss-image"], "styles": [ - ] - }, - { - "selectors": [".gloss-image-link:not([data-appearance=monochrome]) .gloss-image-background"], - "styles": [ - ["display", "none"] + ["filter", "grayscale(1)"] ] }, { @@ -261,24 +156,6 @@ ["display", "block"] ] }, - { - "selectors": [ - ".gloss-image-link[data-collapsed=true] .gloss-image-link-text", - ":root[data-glossary-layout-mode=compact] .gloss-image-link[data-collapsible=true] .gloss-image-link-text" - ], - "styles": [ - ["display", "inline"] - ] - }, - { - "selectors": [ - ".gloss-image-link[data-collapsed=true]~.gloss-image-description", - ":root[data-glossary-layout-mode=compact] .gloss-image-description" - ], - "styles": [ - ["display", "inline"] - ] - }, { "selectors": [".gloss-link-external-icon"], "styles": [ diff --git a/ext/js/dictionary/dictionary-database.js b/ext/js/dictionary/dictionary-database.js index 82468a4845..00cfcf4bbf 100644 --- a/ext/js/dictionary/dictionary-database.js +++ b/ext/js/dictionary/dictionary-database.js @@ -463,6 +463,7 @@ export class DictionaryDatabase { }); } else { const image = new Blob([m.content], {type: m.mediaType}); + // eslint-disable-next-line no-undef await createImageBitmap(image).then((decodedImage) => { // we need to do a dumb hack where we convert this ImageBitmap to an ImageData by drawing it to a temporary canvas, because Firefox doesn't support transferring ImageBitmaps cross-process const canvas = new OffscreenCanvas(decodedImage.width, decodedImage.height); diff --git a/test/data/anki-note-builder-test-results.json b/test/data/anki-note-builder-test-results.json index b496d0b880..617464d3ee 100644 --- a/test/data/anki-note-builder-test-results.json +++ b/test/data/anki-note-builder-test-results.json @@ -863,12 +863,12 @@ "frequency-average-occurrence": "0", "furigana": "画像がぞう", "furigana-plain": "画像[がぞう]", - "glossary": "
(n, termsDictAlias)
", - "glossary-brief": "
", - "glossary-no-dictionary": "
(n)
", - "glossary-first": "
(n, termsDictAlias)
", - "glossary-first-brief": "
", - "glossary-first-no-dictionary": "
(n)
", + "glossary": "
(n, termsDictAlias)
", + "glossary-brief": "
", + "glossary-no-dictionary": "
(n)
", + "glossary-first": "
(n, termsDictAlias)
", + "glossary-first-brief": "
", + "glossary-first-no-dictionary": "
(n)
", "part-of-speech": "Noun", "pitch-accents": "", "pitch-accent-graphs": "", @@ -1570,12 +1570,12 @@ "frequency-average-occurrence": "0", "furigana": "画像がぞう", "furigana-plain": "画像[がぞう]", - "glossary": "
(n, termsDictAlias)
", - "glossary-brief": "
", - "glossary-no-dictionary": "
(n)
", - "glossary-first": "
(n, termsDictAlias)
", - "glossary-first-brief": "
", - "glossary-first-no-dictionary": "
(n)
", + "glossary": "
(n, termsDictAlias)
", + "glossary-brief": "
", + "glossary-no-dictionary": "
(n)
", + "glossary-first": "
(n, termsDictAlias)
", + "glossary-first-brief": "
", + "glossary-first-no-dictionary": "
(n)
", "part-of-speech": "Noun", "pitch-accents": "", "pitch-accent-graphs": "", diff --git a/test/data/translator-test-results.json b/test/data/translator-test-results.json index 1990469009..5fa0f31845 100644 --- a/test/data/translator-test-results.json +++ b/test/data/translator-test-results.json @@ -10869,7 +10869,6 @@ "dictionary": "Test Dictionary 2", "dictionaryIndex": 0, "dictionaryAlias": "termsDictAlias", - "hasReading": true, "frequency": 10, "displayValue": null, @@ -19650,7 +19649,7 @@ ] }, { - "name": "Find term using primary reading 1", + "name": "Find terms using primary reading 1", "originalTextLength": 2, "dictionaryEntries": [ { @@ -19814,10 +19813,9 @@ ] }, { - "name": "Find term using primary reading 2", + "name": "Find terms using primary reading 2", "originalTextLength": 2, "dictionaryEntries": [ - { "type": "term", "isPrimary": true, diff --git a/test/database.test.js b/test/database.test.js index 8bc95ceb3c..50fc213449 100644 --- a/test/database.test.js +++ b/test/database.test.js @@ -26,9 +26,11 @@ import {parseJson} from '../dev/json.js'; import {DictionaryDatabase} from '../ext/js/dictionary/dictionary-database.js'; import {DictionaryImporter} from '../ext/js/dictionary/dictionary-importer.js'; import {DictionaryImporterMediaLoader} from './mocks/dictionary-importer-media-loader.js'; +import {setupStubs} from './utilities/database.js'; const dirname = pathDirname(fileURLToPath(import.meta.url)); +setupStubs(); vi.stubGlobal('IDBKeyRange', IDBKeyRange); /** diff --git a/test/dictionary-data.test.js b/test/dictionary-data.test.js index cfb951d822..c3e9bb8171 100644 --- a/test/dictionary-data.test.js +++ b/test/dictionary-data.test.js @@ -22,13 +22,17 @@ import {describe} from 'vitest'; import {parseJson} from '../dev/json.js'; import {createTranslatorTest} from './fixtures/translator-test.js'; import {createTestAnkiNoteData, getTemplateRenderResults} from './utilities/anki.js'; +import {setupStubs} from './utilities/database.js'; import {createFindKanjiOptions, createFindTermsOptions} from './utilities/translator.js'; +setupStubs(); + const dirname = path.dirname(fileURLToPath(import.meta.url)); const dictionaryName = 'Test Dictionary 2'; const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName); describe('Dictionary data', () => { + console.log('test'); const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json'); /** @type {import('test/translator').TranslatorTestInputs} */ const {optionsPresets, tests} = parseJson(readFileSync(testInputsFilePath, {encoding: 'utf8'})); diff --git a/test/dictionary-data.write.js b/test/dictionary-data.write.js index 880d23798a..0dc9f5c360 100644 --- a/test/dictionary-data.write.js +++ b/test/dictionary-data.write.js @@ -21,8 +21,11 @@ import path from 'path'; import {parseJson} from '../dev/json.js'; import {createTranslatorTest} from './fixtures/translator-test.js'; import {createTestAnkiNoteData, getTemplateRenderResults} from './utilities/anki.js'; +import {setupStubs} from './utilities/database.js'; import {createFindKanjiOptions, createFindTermsOptions} from './utilities/translator.js'; +setupStubs(); + /** * @param {string} fileName * @param {unknown} content diff --git a/test/utilities/database.js b/test/utilities/database.js new file mode 100644 index 0000000000..4fdf93ad0d --- /dev/null +++ b/test/utilities/database.js @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2023-2024 Yomitan 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 . + */ +import {vi} from 'vitest'; + +/** + * + */ +export function setupStubs() { + vi.stubGlobal('self', { + constructor: { + name: 'Window', + }, + }); + + // eslint-disable-next-line jsdoc/require-jsdoc + function Worker() { + return { + addEventListener: () => {}, + }; + } + vi.stubGlobal('Worker', Worker); +}