From 1fed0cc4a47b4f11dc9665445a8208c7c0883247 Mon Sep 17 00:00:00 2001 From: "1994836463@qq.com" Date: Thu, 11 Apr 2024 19:09:26 +0800 Subject: [PATCH 1/3] fix bug 545 --- app/static/js/fillword.js | 8 ++++++-- app/static/js/highlight.js | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/static/js/fillword.js b/app/static/js/fillword.js index b967633..bd5d5a2 100644 --- a/app/static/js/fillword.js +++ b/app/static/js/fillword.js @@ -9,8 +9,12 @@ function fillInWord() { let word = getWord(); if (isRead) Reader.read(word, inputSlider.value); if (!isChoose) return; - const element = document.getElementById("selected-words"); - element.value = element.value + " " + word; + let element = document.getElementById("selected-words"); + let index = (String)(element.value).indexOf(word); + localStorage.setItem("nowWord",element.value); + if(index === -1){ + element.value = element.value + " " + word; + } } document.getElementById("text-content").addEventListener("click", fillInWord, false); diff --git a/app/static/js/highlight.js b/app/static/js/highlight.js index 0cea31a..d7d72c2 100644 --- a/app/static/js/highlight.js +++ b/app/static/js/highlight.js @@ -33,6 +33,22 @@ function highLight() { allWords = pickedWords.value + " "; } const list = allWords.split(" ");//将所有的生词放入一个list中,用于后续处理 + //判断获取的单词不能为空 + if(word !== null && word !== "" && word !== " "){ + let articleContent_fb2 = articleContent; + if(localStorage.getItem("nowWord").indexOf(word) !== -1){ + while(articleContent_fb2.toLowerCase().indexOf(word.toLowerCase()) !== -1){ + // 找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。 + const index = articleContent_fb2.toLowerCase().indexOf(word.toLowerCase()); + word = articleContent_fb2.substring(index, index + word.length); + articleContent_fb2 = articleContent_fb2.substring(index + word.length); // 使用副本中list[i]之后的子串替换掉副本 + articleContent = articleContent.replace(new RegExp(""+word+"", "g"), word) + } + pickedWords.value = localStorage.getItem("nowWord").replace(word,""); + document.getElementById("article").innerHTML = articleContent; + return; + } + } for (let i = 0; i < list.length; ++i) { list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符 list[i] = list[i].replace('|', ""); -- 2.17.1 From 5ad6ba07cf889508cbc215ab48b28372d18a6f7d Mon Sep 17 00:00:00 2001 From: "1994836463@qq.com" Date: Thu, 11 Apr 2024 20:57:59 +0800 Subject: [PATCH 2/3] test bug 545 --- app/test/test_bug545_huanghuiling.py | 87 ++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 app/test/test_bug545_huanghuiling.py 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() -- 2.17.1 From 0b5dacb4728938d1051a99e45fa23aad86e7a118 Mon Sep 17 00:00:00 2001 From: "1994836463@qq.com" Date: Tue, 16 Apr 2024 19:08:42 +0800 Subject: [PATCH 3/3] fix bug 545 --- app/static/js/highlight.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/static/js/highlight.js b/app/static/js/highlight.js index d7d72c2..3070d43 100644 --- a/app/static/js/highlight.js +++ b/app/static/js/highlight.js @@ -34,10 +34,10 @@ function highLight() { } const list = allWords.split(" ");//将所有的生词放入一个list中,用于后续处理 //判断获取的单词不能为空 - if(word !== null && word !== "" && word !== " "){ + if(word != null && word != "" && word != " "){ let articleContent_fb2 = articleContent; - if(localStorage.getItem("nowWord").indexOf(word) !== -1){ - while(articleContent_fb2.toLowerCase().indexOf(word.toLowerCase()) !== -1){ + if(localStorage.getItem("nowWord").indexOf(word) != -1){ + while(articleContent_fb2.toLowerCase().indexOf(word.toLowerCase()) != -1){ // 找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。 const index = articleContent_fb2.toLowerCase().indexOf(word.toLowerCase()); word = articleContent_fb2.substring(index, index + word.length); -- 2.17.1