(string)
| 157 | |
| 158 | // Wraps each occurence of the query terms in the given string in a <span>. |
| 159 | highlightQueryTerms(string) { |
| 160 | if (!this.highlightTerms) return string; |
| 161 | let ranges = []; |
| 162 | const escapedTerms = this.queryTerms.map((term) => Utils.escapeHtml(term)); |
| 163 | for (const term of escapedTerms) { |
| 164 | this.pushMatchingRanges(string, term, ranges); |
| 165 | } |
| 166 | |
| 167 | if (ranges.length === 0) { |
| 168 | return string; |
| 169 | } |
| 170 | |
| 171 | ranges = this.mergeRanges(ranges.sort((a, b) => a[0] - b[0])); |
| 172 | // Replace portions of the string from right to left. |
| 173 | ranges = ranges.sort((a, b) => b[0] - a[0]); |
| 174 | for (const [start, end] of ranges) { |
| 175 | string = string.substring(0, start) + |
| 176 | `<span class='match'>${string.substring(start, end)}</span>` + |
| 177 | string.substring(end); |
| 178 | } |
| 179 | return string; |
| 180 | } |
| 181 | |
| 182 | // Merges the given list of ranges such that any overlapping regions are combined. E.g. |
| 183 | // mergeRanges([0, 4], [3, 6]) => [0, 6]. A range is [startIndex, endIndex]. |
no test coverage detected