From 1ba458ea821b9a39a2cf5eb388f81e4db4763207 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 8 Apr 2017 12:25:18 -0700 Subject: fixing cloze bug --- ext/bg/js/options.js | 3 --- ext/fg/js/util.js | 4 ++-- ext/mixed/js/display.js | 2 +- ext/mixed/js/util.js | 15 ++++++++++----- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index ad8d83d8..51f242c2 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -368,9 +368,6 @@ function ankiFieldsPopulate(element, options) { ], 'kanji': [ 'character', - 'cloze-body', - 'cloze-prefix', - 'cloze-suffix', 'dictionary', 'glossary', 'kunyomi', diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index 445efa5b..e5705ffd 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -182,7 +182,7 @@ function docSentenceExtract(source, extent) { quoteStack = []; - let endPos = content.length - 1; + let endPos = content.length; for (let i = position; i <= endPos; ++i) { const c = content[i]; @@ -205,7 +205,7 @@ function docSentenceExtract(source, extent) { } const text = content.substring(startPos, endPos); - const padding = text.length - text.replace(/^\s+/, ''); + const padding = text.length - text.replace(/^\s+/, '').length; return { text: text.trim(), diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 973b7749..64d462ae 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -108,7 +108,7 @@ class Display { if (context) { for (const definition of definitions) { - definition.cloze = clozeBuild(context.sentence, definition.source); + definition.cloze = clozeBuild(context.sentence); definition.url = context.url; } } diff --git a/ext/mixed/js/util.js b/ext/mixed/js/util.js index eae54f49..62838674 100644 --- a/ext/mixed/js/util.js +++ b/ext/mixed/js/util.js @@ -22,12 +22,17 @@ */ function clozeBuild(sentence, source) { - return { - sentence: sentence.text.trim(), - prefix: sentence.text.substring(0, sentence.offset).trim(), - body: source.trim(), - suffix: sentence.text.substring(sentence.offset + source.length).trim() + const result = { + sentence: sentence.text.trim() }; + + if (source) { + result.prefix = sentence.text.substring(0, sentence.offset).trim(); + result.body = source.trim(); + result.suffix = sentence.text.substring(sentence.offset + source.length).trim(); + } + + return result; } -- cgit v1.2.3