@@ -28,38 +28,33 @@ const updateSearchState = () => {
2828
2929// Escape meta-characters to prevent DOM->HTML XSS
3030function escapeHtml ( text ) {
31- return text
32- . replace ( / & / g, "&" )
33- . replace ( / < / g, "<" )
34- . replace ( / > / g, ">" )
35- . replace ( / " / g, """ )
36- . replace ( / ' / g, "'" ) ;
31+ return text . replace ( / & / g, '&' ) . replace ( / < / g, '<' ) . replace ( / > / g, '>' ) . replace ( / " / g, '"' ) . replace ( / ' / g, ''' ) ;
3732}
3833
3934const highlightSearchTerm = ( logo , term ) => {
40- const logoText = logo . querySelector ( '.logo__text--link' ) ;
41- const logoTextTitle = logoText . textContent ;
42- if ( ! term ) {
43- logoText . innerHTML = escapeHtml ( logoTextTitle ) ;
44- return ;
45- }
46- // Escape and highlight term
47- // Use a global, case-insensitive regex
48- // We escape fragments, but wrap matches in <mark>
49- const regex = new RegExp ( term , 'gi' ) ;
50- let lastIndex = 0 ;
51- let html = '' ;
52- let match ;
53- while ( ( match = regex . exec ( logoTextTitle ) ) !== null ) {
54- // Escape the text before the match
55- html += escapeHtml ( logoTextTitle . substring ( lastIndex , match . index ) ) ;
56- // Highlight the match (escape just in case)
57- html += `<mark>${ escapeHtml ( match [ 0 ] ) } </mark>` ;
58- lastIndex = regex . lastIndex ;
59- }
60- // Escape the rest after last match
61- html += escapeHtml ( logoTextTitle . substring ( lastIndex ) ) ;
62- logoText . innerHTML = html ;
35+ const logoText = logo . querySelector ( '.logo__text--link' ) ;
36+ const logoTextTitle = logoText . textContent ;
37+ if ( ! term ) {
38+ logoText . innerHTML = escapeHtml ( logoTextTitle ) ;
39+ return ;
40+ }
41+ // Escape and highlight term
42+ // Use a global, case-insensitive regex
43+ // We escape fragments, but wrap matches in <mark>
44+ const regex = new RegExp ( term , 'gi' ) ;
45+ let lastIndex = 0 ;
46+ let html = '' ;
47+ let match ;
48+ while ( ( match = regex . exec ( logoTextTitle ) ) !== null ) {
49+ // Escape the text before the match
50+ html += escapeHtml ( logoTextTitle . substring ( lastIndex , match . index ) ) ;
51+ // Highlight the match (escape just in case)
52+ html += `<mark>${ escapeHtml ( match [ 0 ] ) } </mark>` ;
53+ lastIndex = regex . lastIndex ;
54+ }
55+ // Escape the rest after last match
56+ html += escapeHtml ( logoTextTitle . substring ( lastIndex ) ) ;
57+ logoText . innerHTML = html ;
6358} ;
6459
6560const onSearch = e => {
0 commit comments