Skip to content

Commit dab9f40

Browse files
fix(typeahead): Changes to typeaheadHighlight
1 parent 25dbd1f commit dab9f40

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

misc/demo/assets/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* global FastClick, smoothScroll */
2-
angular.module('ui.bootstrap.demo', ['ui.bootstrap', 'plunker', 'ngTouch', 'ngAnimate'], function($httpProvider){
2+
angular.module('ui.bootstrap.demo', ['ui.bootstrap', 'plunker', 'ngTouch', 'ngAnimate', 'ngSanitize'], function($httpProvider){
33
FastClick.attach(document.body);
44
delete $httpProvider.defaults.headers.common['X-Requested-With'];
55
}).run(['$location', function($location){
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
describe('Security concerns', function() {
2-
var highlightFilter, $sanitize;
2+
var highlightFilter,
3+
$sanitize,
4+
logSpy;
35

46
beforeEach(module('ui.bootstrap.typeahead', 'ngSanitize'));
57

6-
beforeEach(inject(function (typeaheadHighlightFilter, _$sanitize_) {
8+
beforeEach(inject(function (typeaheadHighlightFilter, _$sanitize_, $log) {
79
highlightFilter = typeaheadHighlightFilter;
810
$sanitize = _$sanitize_;
11+
logSpy = spyOn($log, 'warn');
912
}));
1013

11-
it('should escape the "script" tag when present', function() {
12-
expect(highlightFilter('before <script src=""></script>match after', 'match')).toEqual('before <strong>match</strong> after');
13-
});
14-
15-
it('should escape attributes that execute javascript from html elements', function() {
16-
expect(highlightFilter('before <div onclick="this.textContent = \'Some content\';"></div>match after', 'match')).toEqual('before <div></div><strong>match</strong> after');
14+
it('should not call the $log service when ngSanitize is present', function() {
15+
highlightFilter('before <script src="">match</script> after', 'match');
16+
expect(logSpy).not.toHaveBeenCalled();
1717
});
1818

1919
});

src/typeahead/test/typeahead-highlight.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('typeaheadHighlight', function () {
4545
});
4646

4747
it('should show a warning when this component is being used unsafely', function() {
48+
highlightFilter('<i>before</i> match after', 'match');
4849
expect(logSpy).toHaveBeenCalled();
4950
});
5051

src/typeahead/typeahead.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
411411
$sanitize = $injector.get('$sanitize'); //tries to inject the sanitize service
412412
}
413413
catch(e) {
414-
$log.warn('Unsafe use of typeahead please use ngSanitize'); // Warn the user about the danger
414+
// Catches the exception when the $sanitize service is not available.
415415
}
416416

417417
function escapeRegexp(queryToEscape) {
@@ -420,11 +420,16 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
420420
return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
421421
}
422422

423+
function containsHtml(matchItem) {
424+
return /<.*>/g.test(matchItem);
425+
}
426+
423427
return function(matchItem, query) {
428+
if(!$sanitize && containsHtml(matchItem)) {
429+
$log.warn('Unsafe use of typeahead please use ngSanitize'); // Warn the user about the danger
430+
}
424431
matchItem = query? ('' + matchItem).replace(new RegExp(escapeRegexp(query), 'gi'), '<strong>$&</strong>') : matchItem; // Replaces the capture string with a the same string inside of a "strong" tag
425-
if($sanitize) { // Is the $sanitize service available?
426-
matchItem = $sanitize(matchItem); // If it's present the string gets sanitized and packed in a $sce object.
427-
} else {
432+
if(!$sanitize) {
428433
matchItem = $sce.trustAsHtml(matchItem); // If $sanitize is not present we pack the string in a $sce object for the ng-bind-html directive
429434
}
430435
return matchItem;

0 commit comments

Comments
 (0)