Fix important bug causing the web to hang on redundant replacements

This commit is contained in:
Bofh 2022-12-15 18:09:23 +01:00
parent 1fa4f2c56e
commit 05b8d6e6da
1 changed files with 31 additions and 29 deletions

View File

@ -39,7 +39,9 @@ window.view.instance = {
search = search.trim(); search = search.trim();
if (search.startsWith('expr:')) 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(); search = search.substr(5).trim();
var sxs = search var sxs = search
.replaceAll('\n', ' ') .replaceAll('\n', ' ')
@ -48,6 +50,26 @@ window.view.instance = {
.replaceAll(/\s+/g, ' ') .replaceAll(/\s+/g, ' ')
.replaceAll('NOT', '!') .replaceAll('NOT', '!')
.split(/OR|AND/); .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 = '</S>';
const inj_en = '</E>';
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++) for (var i = 0; i < sxs.length; i++)
{ {
const sx = sxs[i].trim(); const sx = sxs[i].trim();
@ -56,46 +78,26 @@ window.view.instance = {
const selector = sx.substr(0, sx.indexOf(' ')).trim(); const selector = sx.substr(0, sx.indexOf(' ')).trim();
var content = sx.substr(sx.indexOf(' ')+1).trim(); var content = sx.substr(sx.indexOf(' ')+1).trim();
content = normalize_for_search(content); content = normalize_for_search(content);
const text_words = content.split(' '); switch (selector)
switch (selector) { {
case 'words': case 'words':
case 'hasall': case 'hasall':
case 'anyword': case 'anyword':
case 'hasany': case 'hasany':
for (var j = 0; j < text_words.length; j++) { const text_words = content.split(' ');
const w = text_words[j]; for (var j = 0; j < text_words.length; j++)
for (var k = 0; k < hwords.length; k++) { regex_process(text_words[j], true);
const h = hwords[k];
if (normalize_for_search(h) === w)
html = html.replace(new RegExp("\\b"+h+"\\b", 'g'),
'<span class="sr">'+h+'</span>');
}
}
break; break;
case 'has': case 'has':
case 'includes': case 'includes':
case 'contains': case 'contains':
case 'regex': case 'regex':
var t = remove_accents(html); regex_process(content);
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 = '<span class="sr">';
const inj_en = '</span>';
html = insert_string(html, inj_st, st);
html = insert_string(html, inj_en, en + inj_st.length);
n = t.indexOf(m) + m.length;
});
break; break;
} }
} }
html = html.replaceAll('</S>', '<span class="sr">');
html = html.replaceAll('</E>', '</span>');
return html; return html;
} }