Skip to content

SearXNG is a free internet metasearch engine which aggregates results from various search services and databases. Users are neither tracked nor profiled.

[fix] do highlight replacement at once

allixxe4cf0a7d4f04
diff --git a/searx/webutils.py b/searx/webutils.py
index bfc6b22f7..8cdcab84b 100644
--- a/searx/webutils.py
+++ b/searx/webutils.py
@@ -290,10 +290,8 @@ def highlight_content(content, query):
if len(qs) > 0:
queries.extend(re.findall(regex_highlight_cjk(qs), content, flags=re.I | re.U))
if len(queries) > 0:
- for q in set(queries):
- content = re.sub(
- regex_highlight_cjk(q), f'<span class="highlight">{q}</span>'.replace('\\', r'\\'), content
- )
+ regex = re.compile("|".join(map(regex_highlight_cjk, queries)))
+ return regex.sub(lambda match: f'<span class="highlight">{match.group(0)}</span>'.replace('\\', r'\\'), content)
return content
diff --git a/tests/unit/test_webutils.py b/tests/unit/test_webutils.py
index 244d2b180..b4395539b 100644
--- a/tests/unit/test_webutils.py
+++ b/tests/unit/test_webutils.py
@@ -57,6 +57,11 @@ class TestWebUtils(SearxTestCase):
]
),
),
+ (
+ 'a class',
+ 'a string with class.',
+ '<span class="highlight">a</span> string with <span class="highlight">class</span>.',
+ ),
)
for query, content, expected in data:
self.assertEqual(webutils.highlight_content(content, query), expected)