From 6e00b5d765dbcb580f93d1cdf6326169710cf36a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 6 Mar 2021 13:41:38 -0500 Subject: [PATCH] Fix multiline copying (#1493) * Change order * Update multiline text assignment --- ext/js/display/display-generator.js | 32 +++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 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) { - node.textContent = value; 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
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) {