diff options
-rw-r--r-- | ext/js/background/backend.js | 2 | ||||
-rw-r--r-- | ext/js/display/query-parser.js | 26 | ||||
-rw-r--r-- | ext/js/language/japanese-util.js | 29 | ||||
-rw-r--r-- | test/test-japanese-util.js (renamed from test/test-japanese.js) | 43 |
4 files changed, 34 insertions, 66 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index c797bbf5..af847610 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -1066,7 +1066,7 @@ class Backend { if ( dictionaryEntries.length > 0 && originalTextLength > 0 && - (originalTextLength !== character.length || this._japaneseUtil.isCodePointJapanese(codePoint)) + (originalTextLength !== character.length || jp.isCodePointJapanese(codePoint)) ) { previousUngroupedSegment = null; const {headwords: [{term, reading}]} = dictionaryEntries[0]; diff --git a/ext/js/display/query-parser.js b/ext/js/display/query-parser.js index 0acf6ec8..cbcf7cff 100644 --- a/ext/js/display/query-parser.js +++ b/ext/js/display/query-parser.js @@ -208,8 +208,6 @@ class QueryParser extends EventDispatcher { } _createParseResult(data) { - const jp = this._japaneseUtil; - const readingMode = this._readingMode; const fragment = document.createDocumentFragment(); for (const term of data) { const termNode = document.createElement('span'); @@ -218,7 +216,7 @@ class QueryParser extends EventDispatcher { if (reading.length === 0) { termNode.appendChild(document.createTextNode(text)); } else { - const reading2 = jp.convertReading(text, reading, readingMode); + const reading2 = this._convertReading(text, reading); termNode.appendChild(this._createSegment(text, reading2)); } } @@ -245,4 +243,26 @@ class QueryParser extends EventDispatcher { return segmentNode; } + + _convertReading(term, reading) { + switch (this._readingMode) { + case 'hiragana': + return this._japaneseUtil.convertKatakanaToHiragana(reading); + case 'katakana': + return this._japaneseUtil.convertHiraganaToKatakana(reading); + case 'romaji': + if (this._japaneseUtil.convertToRomajiSupported()) { + if (reading.length > 0) { + return this._japaneseUtil.convertToRomaji(reading); + } else if (this._japaneseUtil.isStringEntirelyKana(term)) { + return this._japaneseUtil.convertToRomaji(term); + } + } + return reading; + case 'none': + return ''; + default: + return reading; + } + } } diff --git a/ext/js/language/japanese-util.js b/ext/js/language/japanese-util.js index 7ee726b2..9d7ad0d8 100644 --- a/ext/js/language/japanese-util.js +++ b/ext/js/language/japanese-util.js @@ -277,6 +277,10 @@ const JapaneseUtil = (() => { return this._getWanakana().toKana(text); } + convertToKanaSupported() { + return this._wanakana !== null; + } + convertKatakanaToHiragana(text) { let result = ''; const offset = (HIRAGANA_CONVERSION_RANGE[0] - KATAKANA_CONVERSION_RANGE[0]); @@ -315,25 +319,8 @@ const JapaneseUtil = (() => { return wanakana.toRomaji(text); } - convertReading(term, reading, readingMode) { - switch (readingMode) { - case 'hiragana': - return this.convertKatakanaToHiragana(reading); - case 'katakana': - return this.convertHiraganaToKatakana(reading); - case 'romaji': - if (reading.length > 0) { - return this.convertToRomaji(reading); - } else if (this.isStringEntirelyKana(term)) { - return this.convertToRomaji(term); - } else { - return reading; - } - case 'none': - return ''; - default: - return reading; - } + convertToRomajiSupported() { + return this._wanakana !== null; } convertNumericToFullWidth(text) { @@ -426,6 +413,10 @@ const JapaneseUtil = (() => { return result; } + convertAlphabeticToKanaSupported() { + return this._wanakana !== null; + } + // Furigana distribution distributeFurigana(term, reading) { diff --git a/test/test-japanese.js b/test/test-japanese-util.js index 9452a1d1..82abf50b 100644 --- a/test/test-japanese.js +++ b/test/test-japanese-util.js @@ -171,48 +171,6 @@ function testConvertToRomaji() { } } -function testConvertReading() { - const data = [ - [['アリガトウ', 'アリガトウ', 'hiragana'], 'ありがとう'], - [['アリガトウ', 'アリガトウ', 'katakana'], 'アリガトウ'], - [['アリガトウ', 'アリガトウ', 'romaji'], 'arigatou'], - [['アリガトウ', 'アリガトウ', 'none'], ''], - [['アリガトウ', 'アリガトウ', 'default'], 'アリガトウ'], - - [['ありがとう', 'ありがとう', 'hiragana'], 'ありがとう'], - [['ありがとう', 'ありがとう', 'katakana'], 'アリガトウ'], - [['ありがとう', 'ありがとう', 'romaji'], 'arigatou'], - [['ありがとう', 'ありがとう', 'none'], ''], - [['ありがとう', 'ありがとう', 'default'], 'ありがとう'], - - [['有り難う', 'ありがとう', 'hiragana'], 'ありがとう'], - [['有り難う', 'ありがとう', 'katakana'], 'アリガトウ'], - [['有り難う', 'ありがとう', 'romaji'], 'arigatou'], - [['有り難う', 'ありがとう', 'none'], ''], - [['有り難う', 'ありがとう', 'default'], 'ありがとう'], - - // Cases with falsy readings - - [['ありがとう', '', 'hiragana'], ''], - [['ありがとう', '', 'katakana'], ''], - [['ありがとう', '', 'romaji'], 'arigatou'], - [['ありがとう', '', 'none'], ''], - [['ありがとう', '', 'default'], ''], - - // Cases with falsy readings and kanji terms - - [['有り難う', '', 'hiragana'], ''], - [['有り難う', '', 'katakana'], ''], - [['有り難う', '', 'romaji'], ''], - [['有り難う', '', 'none'], ''], - [['有り難う', '', 'default'], ''] - ]; - - for (const [[term, reading, readingMode], expected] of data) { - assert.strictEqual(jp.convertReading(term, reading, readingMode), expected); - } -} - function testConvertNumericToFullWidth() { const data = [ ['0123456789', '0123456789'], @@ -888,7 +846,6 @@ function main() { testConvertKatakanaToHiragana(); testConvertHiraganaToKatakana(); testConvertToRomaji(); - testConvertReading(); testConvertNumericToFullWidth(); testConvertHalfWidthKanaToFullWidth(); testConvertAlphabeticToKana(); |