diff --git a/.gitignore b/.gitignore index 3941db60cc..459af2b711 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,5 @@ dictionaries/ ext/manifest.json ext/lib/* -!ext/lib/__mocks__/ ext/legal-npm.html diff --git a/dev/translator-vm.js b/dev/translator-vm.js index 7fdda879eb..60777da05c 100644 --- a/dev/translator-vm.js +++ b/dev/translator-vm.js @@ -28,7 +28,7 @@ import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js'; import {Translator} from '../ext/js/language/translator.js'; import {createDictionaryArchive} from './util.js'; -vi.mock('../ext/js/language/dictionary-importer-media-loader.js'); +vi.mock('../ext/js/language/dictionary-importer-media-loader.js', async () => await import('../test/mocks/dictionary-importer-media-loader.js')); const dirname = path.dirname(fileURLToPath(import.meta.url)); diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index 749c81a6b6..20c7a189da 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -2635,7 +2635,10 @@ export class Backend { */ _getTranslatorFindKanjiOptions(options) { const enabledDictionaryMap = this._getTranslatorEnabledDictionaryMap(options); - return {enabledDictionaryMap}; + return { + enabledDictionaryMap, + removeNonJapaneseCharacters: !options.scanning.alphanumeric + }; } /** diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index aa1b71dd57..e33ea4d451 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -125,6 +125,9 @@ export class Translator { * @returns {Promise} An array of definitions. See the _createKanjiDefinition() function for structure details. */ async findKanji(text, options) { + if (options.removeNonJapaneseCharacters) { + text = this._getJapaneseOnlyText(text); + } const {enabledDictionaryMap} = options; const kanjiUnique = new Set(); for (const c of text) { diff --git a/test/anki-note-builder.test.js b/test/anki-note-builder.test.js index 6901dde9b8..42ee22909c 100644 --- a/test/anki-note-builder.test.js +++ b/test/anki-note-builder.test.js @@ -53,7 +53,7 @@ async function fetch(url2) { }; } vi.stubGlobal('fetch', fetch); -vi.mock('../ext/js/templates/template-renderer-proxy.js'); +vi.mock('../ext/js/templates/template-renderer-proxy.js', async () => await import('../test/mocks/template-renderer-proxy.js')); /** * @returns {Promise} diff --git a/test/data/translator-test-inputs.json b/test/data/translator-test-inputs.json index f6b5ea675d..cf4b8f6a6e 100644 --- a/test/data/translator-test-inputs.json +++ b/test/data/translator-test-inputs.json @@ -9,7 +9,8 @@ "priority": 0 } ] - ] + ], + "removeNonJapaneseCharacters": false }, "default": { "matchType": "exact", diff --git a/test/database.test.js b/test/database.test.js index e7774d95b2..2fdea99c31 100644 --- a/test/database.test.js +++ b/test/database.test.js @@ -22,15 +22,13 @@ 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'; -import {DictionaryImporterMediaLoader} from '../ext/js/language/dictionary-importer-media-loader.js'; import {DictionaryImporter} from '../ext/js/language/dictionary-importer.js'; +import {DictionaryImporterMediaLoader} from './mocks/dictionary-importer-media-loader.js'; const dirname = path.dirname(fileURLToPath(import.meta.url)); vi.stubGlobal('IDBKeyRange', IDBKeyRange); -vi.mock('../ext/js/language/dictionary-importer-media-loader.js'); - /** * @param {string} dictionary * @param {string} [dictionaryName] diff --git a/ext/js/language/__mocks__/dictionary-importer-media-loader.js b/test/mocks/dictionary-importer-media-loader.js similarity index 100% rename from ext/js/language/__mocks__/dictionary-importer-media-loader.js rename to test/mocks/dictionary-importer-media-loader.js diff --git a/ext/js/templates/__mocks__/template-renderer-proxy.js b/test/mocks/template-renderer-proxy.js similarity index 95% rename from ext/js/templates/__mocks__/template-renderer-proxy.js rename to test/mocks/template-renderer-proxy.js index 8823e8f3c2..106cdb35ec 100644 --- a/ext/js/templates/__mocks__/template-renderer-proxy.js +++ b/test/mocks/template-renderer-proxy.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {AnkiTemplateRenderer} from '../sandbox/anki-template-renderer.js'; +import {AnkiTemplateRenderer} from '../../ext/js/templates/sandbox/anki-template-renderer.js'; export class TemplateRendererProxy { constructor() { diff --git a/types/ext/translation.d.ts b/types/ext/translation.d.ts index 3c41c9f367..3adfc6731f 100644 --- a/types/ext/translation.d.ts +++ b/types/ext/translation.d.ts @@ -29,6 +29,10 @@ export type FindKanjiOptions = { * The key is the dictionary name. */ enabledDictionaryMap: Map; + /** + * Whether or not non-Japanese characters should be searched. + */ + removeNonJapaneseCharacters: boolean; }; /**