diff --git a/app/static/js/fillword.js b/app/static/js/fillword.js index 18461f0..8172622 100644 --- a/app/static/js/fillword.js +++ b/app/static/js/fillword.js @@ -1,6 +1,5 @@ -// initialize from localStorage -let isRead = localStorage.getItem('readChecked') !== 'false'; // default to true -let isChoose = localStorage.getItem('chooseChecked') !== 'false'; +let isRead = true; +let isChoose = true; function getWord() { return window.getSelection ? window.getSelection() : document.selection.createRange().text; @@ -11,8 +10,13 @@ function fillInWord() { if (isRead) Reader.read(word, inputSlider.value); if (!isChoose) return; const element = document.getElementById("selected-words"); + let element1 = document.getElementById("selected-words"); + let index = (String)(element1.value).indexOf(word); + localStorage.setItem("nowWord",element1.value); + if(index === -1){ + element1.value = element1.value + " " + word; + } element.value = element.value + " " + word; - localStorage.setItem('selectedWords', element.value); } document.getElementById("text-content").addEventListener("click", fillInWord, false); @@ -26,15 +30,8 @@ inputSlider.oninput = () => { function onReadClick() { isRead = !isRead; - localStorage.setItem('readChecked', isRead); } function onChooseClick() { isChoose = !isChoose; - localStorage.setItem('chooseChecked', isChoose); -} - -// 如果网页刷新,停止播放声音 -if (performance.getEntriesByType("navigation")[0].type == "reload") { - Reader.stopRead(); } diff --git a/app/static/js/highlight.js b/app/static/js/highlight.js index e8a3cf8..0cea31a 100644 --- a/app/static/js/highlight.js +++ b/app/static/js/highlight.js @@ -1,4 +1,4 @@ -let isHighlight = localStorage.getItem('highlightChecked') !== 'false'; // default to true +let isHighlight = true; function cancelBtnHandler() { cancelHighlighting(); @@ -22,38 +22,62 @@ function getWord() { function highLight() { if (!isHighlight) return; - let articleContent = document.getElementById("article").innerHTML; // innerHTML保留HTML标签来保持部分格式,且适配不同的浏览器 + let articleContent = document.getElementById("article").innerText; //将原来的.innerText改为.innerHtml,使用innerText会把原文章中所包含的
标签去除,导致处理后的文章内容失去了原来的格式 let pickedWords = document.getElementById("selected-words"); // words picked to the text area let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list - let allWords = dictionaryWords === null ? pickedWords.value + " " : pickedWords.value + " " + dictionaryWords.value; - const list = allWords.split(" "); // 将所有的生词放入一个list中 - let totalSet = new Set(); - for (let i = 0; i < list.length; ++i) { - list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符 - if (list[i] != "" && !totalSet.has(list[i])) { - // 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。 - let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi"))); - if (matches.has("mark")) { - // 优先处理单词为 "mark" 的情况 - totalSet = new Set(["mark", ...totalSet]); - } - totalSet = new Set([...totalSet, ...matches]); - } + let allWords = ""; //初始化allWords的值,避免进入判断后编译器认为allWords未初始化的问题 + if(dictionaryWords != null){//增加一个判断,检查生词本里面是否为空,如果为空,allWords只添加选中的单词 + allWords = pickedWords.value + " " + dictionaryWords.value; } - // 删除所有的mark标签,防止标签发生嵌套 - articleContent = articleContent.replace(/<(mark)[^>]*>/gi, ""); - articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, ""); - // 将文章中所有出现该单词word的地方改为:"" + word + ""。 - for (let word of totalSet) { - articleContent = articleContent.replace(new RegExp("\\b" + word + "\\b", "g"), "" + word + ""); + else{ + allWords = pickedWords.value + " "; + } + const list = allWords.split(" ");//将所有的生词放入一个list中,用于后续处理 + for (let i = 0; i < list.length; ++i) { + list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符 + list[i] = list[i].replace('|', ""); + list[i] = list[i].replace('?', ""); + if (list[i] !== "" && "".indexOf(list[i]) === -1 && "".indexOf(list[i]) === -1) { + //将文章中所有出现该单词word的地方改为:"" + word + ""。 正则表达式RegExp()中,"\\b"代表单词边界匹配。 + + //修改代码 + let articleContent_fb = articleContent; //文章副本 + while(articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase()) !== -1 && list[i]!=""){ + //找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。 + const index = articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase()); + list[i] = articleContent_fb.substring(index, index + list[i].length); + + articleContent_fb = articleContent_fb.substring(index + list[i].length); // 使用副本中list[i]之后的子串替换掉副本 + articleContent = articleContent.replace(new RegExp("\\b"+list[i]+"\\b","g"),"" + list[i] + ""); + } + } } document.getElementById("article").innerHTML = articleContent; } function cancelHighlighting() { - let articleContent = document.getElementById("article").innerHTML; - articleContent = articleContent.replace(/<(mark)[^>]*>/gi, ""); - articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, ""); + let articleContent = document.getElementById("article").innerText;//将原来的.innerText改为.innerHtml,原因同上 + let pickedWords = document.getElementById("selected-words"); + const dictionaryWords = document.getElementById("selected-words2"); + const list = pickedWords.value.split(" "); + if (pickedWords != null) { + for (let i = 0; i < list.length; ++i) { + list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); + if (list[i] !== "") { //原来判断的代码中,替换的内容为“list[i]”这个字符串,这明显是错误的,我们需要替换的是list[i]里的内容 + articleContent = articleContent.replace(new RegExp(""+list[i]+"", "g"), list[i]); + } + } + } + if (dictionaryWords != null) { + let list2 = pickedWords.value.split(" "); + for (let i = 0; i < list2.length; ++i) { + list2 = dictionaryWords.value.split(" "); + list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, ""); + if (list2[i] !== "") { //原来代码中,替换的内容为“list[i]”这个字符串,这明显是错误的,我们需要替换的是list[i]里的内容 + articleContent = articleContent.replace(new RegExp(""+list2[i]+"", "g"), list2[i]); + } + } + } document.getElementById("article").innerHTML = articleContent; } @@ -73,7 +97,6 @@ function toggleHighlighting() { isHighlight = true; highLight(); } - localStorage.setItem('highlightChecked', isHighlight); } -showBtnHandler(); \ No newline at end of file +showBtnHandler(); diff --git a/app/test/test_bug545_huanghuiling.py b/app/test/test_bug545_huanghuiling.py new file mode 100644 index 0000000..5d7f88b --- /dev/null +++ b/app/test/test_bug545_huanghuiling.py @@ -0,0 +1,87 @@ +# 用于模拟read方法 +class Reader: + @staticmethod + def read(word, value): + print(f"Reading word '{word}' with slider value {value}") + + +is_read = True +is_choose = True + + +# 模拟获取用户选中的单词 +def get_word(): + return "selected word" + + +# 模拟fillInWord函数的行为 +def fill_in_word(): + word = get_word() + if is_read: + Reader.read(word, 5) + if not is_choose: + return + # 模拟selected-words元素 + selected_words = "previous word " + index = selected_words.find(word) + print(f"Current selected words: {selected_words}") + # 假设的localStorage实现 + local_storage = {} + + if index == -1: + selected_words += word + " " + local_storage["nowWord"] = selected_words + print(f"Added word to selected words: {selected_words}") + else: + print(f"Word '{word}' is already in selected words.") + + # 打印模拟的localStorage内容 + print(f"Local storage: {local_storage}") + + +# 假设的slider和rangeValue元素 +slider_value = "5×" +input_slider_value = 5 + + +# 模拟slider的oninput事件 +def on_slider_input(value): + global slider_value + slider_value = str(value) + '×' + print(f"Slider value changed to: {slider_value}") + + +# 模拟按钮点击事件来切换is_read和is_choose的值 +def on_read_click(): + global is_read + is_read = not is_read + print(f"Reading is now {'enabled' if is_read else 'disabled'}") + + +def on_choose_click(): + global is_choose + is_choose = not is_choose + print(f"Choosing is now {'enabled' if is_choose else 'disabled'}") + + +# 假设的功能测试 +def run_functional_test(): + print("\nRunning functional test...") + + # 模拟用户点击操作,调用fill_in_word函数 + fill_in_word() + + # 模拟用户移动滑块,调用on_slider_input函数 + on_slider_input(7) + + # 模拟用户点击“Read”按钮,切换is_read状态 + on_read_click() + fill_in_word() # 再次调用fill_in_word来测试is_read的变化 + + # 模拟用户点击“Choose”按钮,切换is_choose状态 + on_choose_click() + fill_in_word() # 再次调用fill_in_word来测试is_choose的变化 + + +# 运行功能测试 +run_functional_test()