Skip to content

Commit

Permalink
Fix UTF16 and UTF32 characters breaking cloze data due to the use of …
Browse files Browse the repository at this point in the history
…substring (#1533)

* Fix UTF16 and UTF32 characters breaking cloze data due to the use of substring

* Reduce potential for future footguns

* Redefine textChars instead of changing type of text
  • Loading branch information
Kuuuube authored Oct 27, 2024
1 parent 019c458 commit b523711
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions ext/js/data/anki-note-data-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,18 +968,19 @@ function getCloze(dictionaryEntry, context) {
}
if (typeof text !== 'string') { text = ''; }
if (typeof offset !== 'number') { offset = 0; }
const textChars = [...text];

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, textChars.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),
sentence: textChars.join(''),
prefix: textChars.slice(0, offset).join(''),
body: textChars.slice(offset, offset + originalText.length).join(''),
bodyKana: textSegments.join(''),
suffix: text.substring(offset + originalText.length),
suffix: textChars.slice(offset + originalText.length).join(''),
};
}

Expand Down

0 comments on commit b523711

Please sign in to comment.