Skip to content

Commit

Permalink
[fix] do highlight replacement at once
Browse files Browse the repository at this point in the history
Highlights all search queries in search result in one go.

Fixes the case where search query contains word from highlight HTML code,
which causes broken HTML to appear in search results.

Closes searxng#3057
  • Loading branch information
allixx authored and return42 committed Jan 29, 2024
1 parent 8c73aa7 commit e4cf0a7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 2 additions & 4 deletions searx/webutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_webutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def test_highlight_content(self):
]
),
),
(
'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)
Expand Down

0 comments on commit e4cf0a7

Please sign in to comment.