[fix] do highlight replacement at once
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 #3057
This commit is contained in:
		
							parent
							
								
									8c73aa772b
								
							
						
					
					
						commit
						e4cf0a7d4f
					
				| @ -290,10 +290,8 @@ def highlight_content(content, query): | |||||||
|         if len(qs) > 0: |         if len(qs) > 0: | ||||||
|             queries.extend(re.findall(regex_highlight_cjk(qs), content, flags=re.I | re.U)) |             queries.extend(re.findall(regex_highlight_cjk(qs), content, flags=re.I | re.U)) | ||||||
|     if len(queries) > 0: |     if len(queries) > 0: | ||||||
|         for q in set(queries): |         regex = re.compile("|".join(map(regex_highlight_cjk, queries))) | ||||||
|             content = re.sub( |         return regex.sub(lambda match: f'<span class="highlight">{match.group(0)}</span>'.replace('\\', r'\\'), content) | ||||||
|                 regex_highlight_cjk(q), f'<span class="highlight">{q}</span>'.replace('\\', r'\\'), content |  | ||||||
|             ) |  | ||||||
|     return content |     return content | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -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: |         for query, content, expected in data: | ||||||
|             self.assertEqual(webutils.highlight_content(content, query), expected) |             self.assertEqual(webutils.highlight_content(content, query), expected) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 allixx
						allixx