Skip to content

Commit

Permalink
fix(atomic): escape searchQuery in regex (#4116)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar authored Jun 25, 2024
1 parent 5f60704 commit dbd33a1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ describe('#highlightSearchResult', () => {
'&lt;script&gt;<span part="search-highlight" class="font-bold">con</span>sole.log(&quot;hmm&quot;)&lt;script&gt;'
);
});

it('should not highlight in the middle of an escaped character', () => {
expect(highlightSearchResult('foo&bar', 'foo&')).toBe(
'<span part="search-highlight" class="font-bold">foo&amp;</span>bar'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function highlightSearchResult(resultValue: string, searchQuery = '') {
return sanitizedResult;
}

const regex = new RegExp(`(${regexEncode(searchQuery)})`, 'i');
const regex = new RegExp(`(${regexEncode(escape(searchQuery))})`, 'i');
return escape(resultValue).replace(
regex,
'<span part="search-highlight" class="font-bold">$1</span>'
Expand Down

0 comments on commit dbd33a1

Please sign in to comment.