From 9f2470ff53a911e2ab53fb514f4d302429d2c141 Mon Sep 17 00:00:00 2001 From: kuuuube Date: Sat, 26 Oct 2024 23:08:54 -0400 Subject: [PATCH 1/3] Fix UTF16 and UTF32 characters breaking cloze data due to the use of substring --- ext/js/data/anki-note-data-creator.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/js/data/anki-note-data-creator.js b/ext/js/data/anki-note-data-creator.js index 0a401a0c4e..29ff9bc1e3 100644 --- a/ext/js/data/anki-note-data-creator.js +++ b/ext/js/data/anki-note-data-creator.js @@ -970,16 +970,16 @@ function getCloze(dictionaryEntry, context) { if (typeof offset !== 'number') { offset = 0; } const textSegments = []; - for (const {text: text2, reading: reading2} of distributeFuriganaInflected(term, reading, text.substring(offset, offset + originalText.length))) { + for (const {text: text2, reading: reading2} of distributeFuriganaInflected(term, reading, [...text].slice(offset, offset + originalText.length).join(''))) { textSegments.push(reading2.length > 0 ? reading2 : text2); } return { sentence: text, - prefix: text.substring(0, offset), - body: text.substring(offset, offset + originalText.length), + prefix: [...text].slice(0, offset).join(''), + body: [...text].slice(offset, offset + originalText.length).join(''), bodyKana: textSegments.join(''), - suffix: text.substring(offset + originalText.length), + suffix: [...text].slice(offset + originalText.length).join(''), }; } From 83b64b8a67eb670526b92932d4fbcb743db3ad43 Mon Sep 17 00:00:00 2001 From: kuuuube Date: Sat, 26 Oct 2024 23:21:00 -0400 Subject: [PATCH 2/3] Reduce potential for future footguns --- ext/js/data/anki-note-data-creator.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ext/js/data/anki-note-data-creator.js b/ext/js/data/anki-note-data-creator.js index 29ff9bc1e3..6df19e9ac5 100644 --- a/ext/js/data/anki-note-data-creator.js +++ b/ext/js/data/anki-note-data-creator.js @@ -968,18 +968,19 @@ function getCloze(dictionaryEntry, context) { } if (typeof text !== 'string') { text = ''; } if (typeof offset !== 'number') { offset = 0; } + text = [...text]; const textSegments = []; - for (const {text: text2, reading: reading2} of distributeFuriganaInflected(term, reading, [...text].slice(offset, offset + originalText.length).join(''))) { + for (const {text: text2, reading: reading2} of distributeFuriganaInflected(term, reading, text.slice(offset, offset + originalText.length).join(''))) { textSegments.push(reading2.length > 0 ? reading2 : text2); } return { - sentence: text, - prefix: [...text].slice(0, offset).join(''), - body: [...text].slice(offset, offset + originalText.length).join(''), + sentence: text.join(''), + prefix: text.slice(0, offset).join(''), + body: text.slice(offset, offset + originalText.length).join(''), bodyKana: textSegments.join(''), - suffix: [...text].slice(offset + originalText.length).join(''), + suffix: text.slice(offset + originalText.length).join(''), }; } From 88b56271a1474829b9c2fe776c69fd735d7204bf Mon Sep 17 00:00:00 2001 From: kuuuube Date: Sat, 26 Oct 2024 23:24:23 -0400 Subject: [PATCH 3/3] Redefine textChars instead of changing type of text --- ext/js/data/anki-note-data-creator.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/js/data/anki-note-data-creator.js b/ext/js/data/anki-note-data-creator.js index 6df19e9ac5..43d1252e3a 100644 --- a/ext/js/data/anki-note-data-creator.js +++ b/ext/js/data/anki-note-data-creator.js @@ -968,19 +968,19 @@ function getCloze(dictionaryEntry, context) { } if (typeof text !== 'string') { text = ''; } if (typeof offset !== 'number') { offset = 0; } - text = [...text]; + const textChars = [...text]; const textSegments = []; - for (const {text: text2, reading: reading2} of distributeFuriganaInflected(term, reading, text.slice(offset, offset + originalText.length).join(''))) { + for (const {text: text2, reading: reading2} of distributeFuriganaInflected(term, reading, textChars.slice(offset, offset + originalText.length).join(''))) { textSegments.push(reading2.length > 0 ? reading2 : text2); } return { - sentence: text.join(''), - prefix: text.slice(0, offset).join(''), - body: text.slice(offset, offset + originalText.length).join(''), + sentence: textChars.join(''), + prefix: textChars.slice(0, offset).join(''), + body: textChars.slice(offset, offset + originalText.length).join(''), bodyKana: textSegments.join(''), - suffix: text.slice(offset + originalText.length).join(''), + suffix: textChars.slice(offset + originalText.length).join(''), }; }