From 62d94612def34b6ca3e7c64de8d962ca884c3c6a Mon Sep 17 00:00:00 2001 From: Khai Truong Date: Wed, 27 Nov 2024 22:33:02 +0700 Subject: [PATCH 1/2] make sentence parser works for spaced languages --- ext/css/visibility-modifiers.css | 3 +++ ext/js/display/query-parser.js | 16 ++++++++++++++-- ext/js/language/text-scanner.js | 4 ++-- ext/settings.html | 2 +- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ext/css/visibility-modifiers.css b/ext/css/visibility-modifiers.css index f461b9c11..f469bd052 100644 --- a/ext/css/visibility-modifiers.css +++ b/ext/css/visibility-modifiers.css @@ -10,6 +10,9 @@ :root:not([data-language=ja]):not([data-language=zh]):not([data-language=yue]) .jpzhyue-only { display: none; } +:root:not([data-language=ja]):not([data-language=zh]):not([data-language=yue]):not([data-language=ko]) .jpzhyueko-only { + display: none; +} :root:is([data-language=ja], [data-language=zh], [data-language=yue], [data-language=ko]) .not-jpzhyueko { display: none; } diff --git a/ext/js/display/query-parser.js b/ext/js/display/query-parser.js index a85ccc4a9..9a5751f91 100644 --- a/ext/js/display/query-parser.js +++ b/ext/js/display/query-parser.js @@ -20,7 +20,7 @@ import {EventDispatcher} from '../core/event-dispatcher.js'; import {log} from '../core/log.js'; import {querySelectorNotNull} from '../dom/query-selector.js'; import {convertHiraganaToKatakana, convertKatakanaToHiragana, isStringEntirelyKana} from '../language/ja/japanese.js'; -import {TextScanner} from '../language/text-scanner.js'; +import {NO_SPACES_LANGUAGES, TextScanner} from '../language/text-scanner.js'; /** * @augments EventDispatcher @@ -43,6 +43,8 @@ export class QueryParser extends EventDispatcher { this._setTextToken = null; /** @type {?string} */ this._selectedParser = null; + /** @type {boolean} */ + this._shouldInsertSpacesBetweenTerms = false; /** @type {import('settings').ParsingReadingMode} */ this._readingMode = 'none'; /** @type {number} */ @@ -100,6 +102,9 @@ export class QueryParser extends EventDispatcher { } if (typeof termSpacing === 'boolean') { this._queryParser.dataset.termSpacing = `${termSpacing}`; + if (!NO_SPACES_LANGUAGES.has(language)) { + this._shouldInsertSpacesBetweenTerms = true; + } } if (typeof readingMode === 'string') { this._setReadingMode(readingMode); @@ -305,6 +310,7 @@ export class QueryParser extends EventDispatcher { termNode.dataset.offset = `${offset}`; for (const {text, reading} of term) { const trimmedText = text.trim(); + if (trimmedText.length === 0) { continue; } if (reading.length === 0) { termNode.appendChild(document.createTextNode(trimmedText)); } else { @@ -312,8 +318,14 @@ export class QueryParser extends EventDispatcher { termNode.appendChild(this._createSegment(trimmedText, reading2, offset)); } offset += trimmedText.length; + if (this._shouldInsertSpacesBetweenTerms) { + termNode.appendChild(document.createTextNode(' ')); + offset += 1; + } + } + if (termNode.childNodes.length > 0) { + fragment.appendChild(termNode); } - fragment.appendChild(termNode); } return fragment; } diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js index c1bccad13..62f9f697b 100644 --- a/ext/js/language/text-scanner.js +++ b/ext/js/language/text-scanner.js @@ -24,7 +24,7 @@ import {clone} from '../core/utilities.js'; import {anyNodeMatchesSelector, everyNodeMatchesSelector, getActiveModifiers, getActiveModifiersAndButtons, isPointInSelection} from '../dom/document-util.js'; import {TextSourceElement} from '../dom/text-source-element.js'; -const SCAN_RESOLUTION_EXCLUDED_LANGUAGES = new Set(['ja', 'zh', 'yue', 'ko']); +export const NO_SPACES_LANGUAGES = new Set(['ja', 'zh', 'yue', 'ko']); /** * @augments EventDispatcher @@ -478,7 +478,7 @@ export class TextScanner extends EventDispatcher { ); if (this._scanResolution === 'word' && !disallowExpandStartOffset && - (this._language === null || !SCAN_RESOLUTION_EXCLUDED_LANGUAGES.has(this._language))) { + (this._language === null || !NO_SPACES_LANGUAGES.has(this._language))) { // Move the start offset to the beginning of the word textSource.setStartOffset(this._scanLength, this._layoutAwareScan, true); } diff --git a/ext/settings.html b/ext/settings.html index 27fefef7f..03bf23220 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -1555,7 +1555,7 @@

Yomitan Settings

-
+
Show space between parsed words
From 855ceb1005b976d949c496eac9e92bbc8efaa285 Mon Sep 17 00:00:00 2001 From: Khai Truong Date: Wed, 27 Nov 2024 23:13:53 +0700 Subject: [PATCH 2/2] Revert "make sentence parser works for spaced languages" This reverts commit 62d94612def34b6ca3e7c64de8d962ca884c3c6a. --- ext/css/visibility-modifiers.css | 3 --- ext/js/display/query-parser.js | 16 ++-------------- ext/js/language/text-scanner.js | 4 ++-- ext/settings.html | 2 +- 4 files changed, 5 insertions(+), 20 deletions(-) diff --git a/ext/css/visibility-modifiers.css b/ext/css/visibility-modifiers.css index f469bd052..f461b9c11 100644 --- a/ext/css/visibility-modifiers.css +++ b/ext/css/visibility-modifiers.css @@ -10,9 +10,6 @@ :root:not([data-language=ja]):not([data-language=zh]):not([data-language=yue]) .jpzhyue-only { display: none; } -:root:not([data-language=ja]):not([data-language=zh]):not([data-language=yue]):not([data-language=ko]) .jpzhyueko-only { - display: none; -} :root:is([data-language=ja], [data-language=zh], [data-language=yue], [data-language=ko]) .not-jpzhyueko { display: none; } diff --git a/ext/js/display/query-parser.js b/ext/js/display/query-parser.js index 9a5751f91..a85ccc4a9 100644 --- a/ext/js/display/query-parser.js +++ b/ext/js/display/query-parser.js @@ -20,7 +20,7 @@ import {EventDispatcher} from '../core/event-dispatcher.js'; import {log} from '../core/log.js'; import {querySelectorNotNull} from '../dom/query-selector.js'; import {convertHiraganaToKatakana, convertKatakanaToHiragana, isStringEntirelyKana} from '../language/ja/japanese.js'; -import {NO_SPACES_LANGUAGES, TextScanner} from '../language/text-scanner.js'; +import {TextScanner} from '../language/text-scanner.js'; /** * @augments EventDispatcher @@ -43,8 +43,6 @@ export class QueryParser extends EventDispatcher { this._setTextToken = null; /** @type {?string} */ this._selectedParser = null; - /** @type {boolean} */ - this._shouldInsertSpacesBetweenTerms = false; /** @type {import('settings').ParsingReadingMode} */ this._readingMode = 'none'; /** @type {number} */ @@ -102,9 +100,6 @@ export class QueryParser extends EventDispatcher { } if (typeof termSpacing === 'boolean') { this._queryParser.dataset.termSpacing = `${termSpacing}`; - if (!NO_SPACES_LANGUAGES.has(language)) { - this._shouldInsertSpacesBetweenTerms = true; - } } if (typeof readingMode === 'string') { this._setReadingMode(readingMode); @@ -310,7 +305,6 @@ export class QueryParser extends EventDispatcher { termNode.dataset.offset = `${offset}`; for (const {text, reading} of term) { const trimmedText = text.trim(); - if (trimmedText.length === 0) { continue; } if (reading.length === 0) { termNode.appendChild(document.createTextNode(trimmedText)); } else { @@ -318,14 +312,8 @@ export class QueryParser extends EventDispatcher { termNode.appendChild(this._createSegment(trimmedText, reading2, offset)); } offset += trimmedText.length; - if (this._shouldInsertSpacesBetweenTerms) { - termNode.appendChild(document.createTextNode(' ')); - offset += 1; - } - } - if (termNode.childNodes.length > 0) { - fragment.appendChild(termNode); } + fragment.appendChild(termNode); } return fragment; } diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js index 62f9f697b..c1bccad13 100644 --- a/ext/js/language/text-scanner.js +++ b/ext/js/language/text-scanner.js @@ -24,7 +24,7 @@ import {clone} from '../core/utilities.js'; import {anyNodeMatchesSelector, everyNodeMatchesSelector, getActiveModifiers, getActiveModifiersAndButtons, isPointInSelection} from '../dom/document-util.js'; import {TextSourceElement} from '../dom/text-source-element.js'; -export const NO_SPACES_LANGUAGES = new Set(['ja', 'zh', 'yue', 'ko']); +const SCAN_RESOLUTION_EXCLUDED_LANGUAGES = new Set(['ja', 'zh', 'yue', 'ko']); /** * @augments EventDispatcher @@ -478,7 +478,7 @@ export class TextScanner extends EventDispatcher { ); if (this._scanResolution === 'word' && !disallowExpandStartOffset && - (this._language === null || !NO_SPACES_LANGUAGES.has(this._language))) { + (this._language === null || !SCAN_RESOLUTION_EXCLUDED_LANGUAGES.has(this._language))) { // Move the start offset to the beginning of the word textSource.setStartOffset(this._scanLength, this._layoutAwareScan, true); } diff --git a/ext/settings.html b/ext/settings.html index 03bf23220..27fefef7f 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -1555,7 +1555,7 @@

Yomitan Settings

-
+
Show space between parsed words