diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-03-06 13:41:38 -0500 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-03-07 15:54:06 -0500 |
commit | 439e6308bb01425a999b89b677611ae3ea426e1a (patch) | |
tree | 6d9dbf8900df8b92fc93e1dc1f4addb13848b2f5 /ext/js | |
parent | c073c4a70630ac58b4e97ad152f14485481fb3a5 (diff) |
Fix multiline copying (#1493)
* Change order
* Update multiline text assignment
Diffstat (limited to 'ext/js')
-rw-r--r-- | ext/js/display/display-generator.js | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js index bd0c83a2..93e84bdd 100644 --- a/ext/js/display/display-generator.js +++ b/ext/js/display/display-generator.js @@ -302,7 +302,7 @@ class DisplayGenerator { _createTermGlossaryItemText(glossary) { const node = this._templates.instantiate('glossary-item'); const container = node.querySelector('.glossary'); - this._setTextContent(container, glossary); + this._setMultilineTextContent(container, glossary); return node; } @@ -350,7 +350,7 @@ class DisplayGenerator { if (typeof description === 'string') { const container = node.querySelector('.glossary-image-description'); - this._setTextContent(container, description); + this._setMultilineTextContent(container, description); } return node; @@ -385,7 +385,7 @@ class DisplayGenerator { _createKanjiGlossaryItem(glossary) { const node = this._templates.instantiate('kanji-glossary-item'); const container = node.querySelector('.kanji-glossary'); - this._setTextContent(container, glossary); + this._setMultilineTextContent(container, glossary); return node; } @@ -721,12 +721,36 @@ class DisplayGenerator { } _setTextContent(node, value, language) { + if (typeof language === 'string') { + node.lang = language; + } else if (this._japaneseUtil.isStringPartiallyJapanese(value)) { + node.lang = 'ja'; + } + node.textContent = value; + } + + _setMultilineTextContent(node, value, language) { + // This can't just call _setTextContent because the lack of <br> elements will + // cause the text to not copy correctly. if (typeof language === 'string') { node.lang = language; } else if (this._japaneseUtil.isStringPartiallyJapanese(value)) { node.lang = 'ja'; } + + let start = 0; + while (true) { + const end = value.indexOf('\n', start); + if (end < 0) { break; } + node.appendChild(document.createTextNode(value.substring(start, end))); + node.appendChild(document.createElement('br')); + start = end + 1; + } + + if (start < value.length) { + node.appendChild(document.createTextNode(start === 0 ? value : value.substring(start))); + } } _getPitchAccentCategories(pitches) { |