Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
Merge PR #665
Browse files Browse the repository at this point in the history
  • Loading branch information
mbenford committed Jul 22, 2016
2 parents e7792e1 + bbc1f58 commit a4c03ca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ tagsInput.factory('tiUtil', function($timeout, $q) {
};

self.safeHighlight = function(str, value) {
str = self.encodeHTML(str);
value = self.encodeHTML(value);

if (!value) {
return str;
}
Expand All @@ -63,9 +66,6 @@ tagsInput.factory('tiUtil', function($timeout, $q) {
return str.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
}

str = self.encodeHTML(str);
value = self.encodeHTML(value);

var expression = new RegExp('&[^;]+;|' + escapeRegexChars(value), 'gi');
return str.replace(expression, function(match) {
return match.toLowerCase() === value.toLowerCase() ? '<em>' + match + '</em>' : match;
Expand Down Expand Up @@ -127,4 +127,4 @@ tagsInput.factory('tiUtil', function($timeout, $q) {
};

return self;
});
});
6 changes: 6 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,18 @@ describe('tiUtil factory', function() {
expect(tiUtil.safeHighlight('abc', 'b')).toBe('a<em>b</em>c');
expect(tiUtil.safeHighlight('aBc', 'b')).toBe('a<em>B</em>c');
expect(tiUtil.safeHighlight('abc', 'B')).toBe('a<em>b</em>c');
expect(tiUtil.safeHighlight('abcB', 'B')).toBe('a<em>b</em>c<em>B</em>');
expect(tiUtil.safeHighlight('abc', '')).toBe('abc');
});

it('highlights HTML entities', function() {
expect(tiUtil.safeHighlight('a&a', '&')).toBe('a<em>&amp;</em>a');
expect(tiUtil.safeHighlight('a>a', '>')).toBe('a<em>&gt;</em>a');
expect(tiUtil.safeHighlight('a<a', '<')).toBe('a<em>&lt;</em>a');
expect(tiUtil.safeHighlight('<script>alert("XSS")</script>', '<'))
.toBe('<em>&lt;</em>script&gt;alert("XSS")<em>&lt;</em>/script&gt;');
expect(tiUtil.safeHighlight('<script>alert("XSS")</script>', ''))
.toBe('&lt;script&gt;alert("XSS")&lt;/script&gt;');
});
});

Expand Down

0 comments on commit a4c03ca

Please sign in to comment.