From 05b8d6e6dae69e983e16e7d8964ec5bb763387cf Mon Sep 17 00:00:00 2001 From: Bofh Date: Thu, 15 Dec 2022 18:09:23 +0100 Subject: [PATCH] Fix important bug causing the web to hang on redundant replacements --- views/instance.php | 60 ++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/views/instance.php b/views/instance.php index 146ea60..e22914a 100644 --- a/views/instance.php +++ b/views/instance.php @@ -39,7 +39,9 @@ window.view.instance = { search = search.trim(); if (search.startsWith('expr:')) { - const hwords = html.split(/\b/); + var t = remove_accents(html); + t = t.replace(/[^a-zA-Z0-9]/g, ' '); + t = normalize_word_sound(t.toLowerCase()); search = search.substr(5).trim(); var sxs = search .replaceAll('\n', ' ') @@ -48,6 +50,26 @@ window.view.instance = { .replaceAll(/\s+/g, ' ') .replaceAll('NOT', '!') .split(/OR|AND/); + + const regex_process = function(cnt, whole_word) { + if (cnt.trim() === '') return false; + whole_word = whole_word === undefined ? false : whole_word; + cnt = cnt.replace(/\s+/g, ' ').trim().replaceAll(' ', '.') + .replace(/\w/g, function(m) { return m+'+?\\s?'}); + cnt = cnt.substr(0, cnt.length-3); + if (whole_word) cnt = '\\b'+cnt+'\\b'; + + t = t.replace(new RegExp(cnt, 'g'), function(m, st) { + var en = st + m.length; + const inj_st = ''; + const inj_en = ''; + html = insert_string(html, inj_st, st); + html = insert_string(html, inj_en, en + inj_st.length); + return inj_st + m + inj_en; + }); + return true; + }; + for (var i = 0; i < sxs.length; i++) { const sx = sxs[i].trim(); @@ -56,46 +78,26 @@ window.view.instance = { const selector = sx.substr(0, sx.indexOf(' ')).trim(); var content = sx.substr(sx.indexOf(' ')+1).trim(); content = normalize_for_search(content); - const text_words = content.split(' '); - switch (selector) { + switch (selector) + { case 'words': case 'hasall': case 'anyword': case 'hasany': - for (var j = 0; j < text_words.length; j++) { - const w = text_words[j]; - for (var k = 0; k < hwords.length; k++) { - const h = hwords[k]; - if (normalize_for_search(h) === w) - html = html.replace(new RegExp("\\b"+h+"\\b", 'g'), - ''+h+''); - } - } + const text_words = content.split(' '); + for (var j = 0; j < text_words.length; j++) + regex_process(text_words[j], true); break; case 'has': case 'includes': case 'contains': case 'regex': - var t = remove_accents(html); - t = t.replace(/[^a-zA-Z0-9]/g, ' '); - t = normalize_word_sound(t.toLowerCase()); - content = content.replace(/\s+/g, ' ') - .trim().replaceAll(' ', '.') - .replace(/\w/g, function(m) { return m+'+?\\s?'}); - content = content.substr(0, content.length-3); - var n = 0; - t.replace(new RegExp(content), function(m) { - var st = t.indexOf(m, n); - var en = st + m.length; - const inj_st = ''; - const inj_en = ''; - html = insert_string(html, inj_st, st); - html = insert_string(html, inj_en, en + inj_st.length); - n = t.indexOf(m) + m.length; - }); + regex_process(content); break; } } + html = html.replaceAll('', ''); + html = html.replaceAll('', ''); return html; }