Skip to content

Commit afe7518

Browse files
authored
Merge pull request #158 from bindable-ui/ddavis/fix-search-highlights
Fix Highlight Phrases function
2 parents 8b6a8b8 + b278d11 commit afe7518

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

dev-app/app.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<c-nav-horizontal-item
5353
position="right"
5454
href="https://github.com/bindable-ui/bindable"
55-
title="v1.12.0"
55+
title="v1.12.1"
5656
></c-nav-horizontal-item>
5757
</c-nav-horizontal>
5858
</l-box>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@bindable-ui/bindable",
33
"description": "An Aurelia component library",
4-
"version": "1.12.0",
4+
"version": "1.12.1",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/bindable-ui/bindable"

src/helpers/highlight-phrases.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,26 @@ describe('Highlight Search Phrases Helper', () => {
1313
const empty = highlightSearchPhrases(searchPhrases);
1414
expect(empty).toEqual('');
1515
});
16+
17+
it('should should not highlight span tags', () => {
18+
const searchPhrases = ['pan', 'span', 'color'];
19+
const desc = 'Peter Pan';
20+
const highlighted = highlightSearchPhrases(searchPhrases, desc);
21+
expect(highlighted).toEqual('Peter <span style="background-color: #226684;">Pan</span>');
22+
const empty = highlightSearchPhrases(searchPhrases);
23+
expect(empty).toEqual('');
24+
});
25+
26+
it('should escape html characters', () => {
27+
const searchPhrases = ['dumb', 'and'];
28+
const desc = 'Dumb & Dumber <HD>';
29+
const highlighted = highlightSearchPhrases(searchPhrases, desc);
30+
expect(highlighted).toEqual(
31+
'<span style="background-color: #226684;">Dumb</span> ' +
32+
'&amp; ' +
33+
'<span style="background-color: #226684;">Dumb</span>er &lt;HD&gt;',
34+
);
35+
const empty = highlightSearchPhrases(searchPhrases);
36+
expect(empty).toEqual('');
37+
});
1638
});

src/helpers/highlight-phrases.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
export const highlightSearchPhrases = (searchPhrases: string[], matchAgainst?: string): string => {
2-
let title = matchAgainst || '';
3-
title = title
4-
.replace('&', '&amp;')
5-
.replace('<', '&lt;')
6-
.replace('>', '&gt;');
7-
if (searchPhrases && searchPhrases.length > 0) {
8-
searchPhrases.forEach(sp => {
9-
const regEx = new RegExp(sp, 'gi');
10-
title = title.replace(regEx, '<span style="background-color: #226684;">$&</span>');
11-
});
1+
export function highlightSearchPhrases(searchPhrases: string[], desc: string = ''): string {
2+
if (desc === '') {
3+
return '';
124
}
13-
return title;
14-
};
5+
6+
const openTag = '<span style="background-color: #226684;">';
7+
const closeTag = '</span>';
8+
9+
let highlightedDesc = _.escape(desc);
10+
11+
searchPhrases.forEach(phrase => {
12+
const regex = new RegExp(`(${phrase})(?![^<]*>|[^<>]*<\/)`, 'gi');
13+
highlightedDesc = highlightedDesc.replace(regex, `${openTag}$1${closeTag}`);
14+
});
15+
16+
return highlightedDesc;
17+
}

0 commit comments

Comments
 (0)