Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 02aa4f4

Browse files
juliemrpetebacondarwin
authored andcommitted
fix(testability): escape regex chars in findBindings if using exactMatch
Move the function to escape regexps to Angular.js, fix the link, and use it in the $$testability service. Closes #9595 Closes #9600
1 parent 69bf2f0 commit 02aa4f4

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

src/.jshintrc

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"isBoolean": false,
5555
"isPromiseLike": false,
5656
"trim": false,
57+
"escapeForRegexp": false,
5758
"isElement": false,
5859
"makeMap": false,
5960
"size": false,

src/Angular.js

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
isBoolean: true,
5050
isPromiseLike: true,
5151
trim: true,
52+
escapeForRegexp: true,
5253
isElement: true,
5354
makeMap: true,
5455
size: true,
@@ -586,6 +587,14 @@ var trim = function(value) {
586587
return isString(value) ? value.trim() : value;
587588
};
588589

590+
// Copied from:
591+
// http://docs.closure-library.googlecode.com/git/local_closure_goog_string_string.js.source.html#line1021
592+
// Prereq: s is a string.
593+
var escapeForRegexp = function(s) {
594+
return s.replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').
595+
replace(/\x08/g, '\\x08');
596+
};
597+
589598

590599
/**
591600
* @ngdoc function

src/ng/sce.js

-9
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ var SCE_CONTEXTS = {
1414

1515
// Helper functions follow.
1616

17-
// Copied from:
18-
// http://docs.closure-library.googlecode.com/git/closure_goog_string_string.js.source.html#line962
19-
// Prereq: s is a string.
20-
function escapeForRegexp(s) {
21-
return s.replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').
22-
replace(/\x08/g, '\\x08');
23-
}
24-
25-
2617
function adjustMatcher(matcher) {
2718
if (matcher === 'self') {
2819
return matcher;

src/ng/testability.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function $$TestabilityProvider() {
3434
if (dataBinding) {
3535
forEach(dataBinding, function(bindingName) {
3636
if (opt_exactMatch) {
37-
var matcher = new RegExp('(^|\\s)' + expression + '(\\s|\\||$)');
37+
var matcher = new RegExp('(^|\\s)' + escapeForRegexp(expression) + '(\\s|\\||$)');
3838
if (matcher.test(bindingName)) {
3939
matches.push(binding);
4040
}

test/ng/testabilitySpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@ describe('$$testability', function() {
8888
expect(names[0]).toBe(element.find('li')[0]);
8989
});
9090

91+
it('should find bindings with allowed special characters', function() {
92+
element =
93+
'<div>' +
94+
' <span>{{$index}}</span>' +
95+
' <span>{{foo.bar}}</span>' +
96+
' <span>{{foonbar}}</span>' +
97+
' <span>{{foo | uppercase}}</span>' +
98+
'</div>';
99+
element = $compile(element)(scope);
100+
var indexes = $$testability.findBindings(element[0], '$index', true);
101+
expect(indexes.length).toBe(1);
102+
expect(indexes[0]).toBe(element.find('span')[0]);
103+
104+
var foobars = $$testability.findBindings(element[0], 'foo.bar', true);
105+
expect(foobars.length).toBe(1); // it should not match {{foonbar}}
106+
expect(foobars[0]).toBe(element.find('span')[1]);
107+
108+
var foo = $$testability.findBindings(element[0], 'foo', true);
109+
expect(foo.length).toBe(1); // it should match {{foo | uppercase}}
110+
var uppercase = $$testability.findBindings(element[0], 'uppercase', true);
111+
expect(uppercase.length).toBe(1); // it should match {{foo | uppercase}}
112+
var filteredFoo = $$testability.findBindings(element[0], 'foo | uppercase', true);
113+
expect(filteredFoo.length).toBe(1); // it should match {{foo | uppercase}}
114+
expect(filteredFoo[0]).toBe(element.find('span')[3]);
115+
});
116+
91117
it('should find partial models', function() {
92118
element =
93119
'<div>' +

0 commit comments

Comments
 (0)