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

fix(testability): findBindings should escape binding expressions before ... #9600

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"isBoolean": false,
"isPromiseLike": false,
"trim": false,
"escapeForRegexp": true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be false here. To tell JSHint that in general this is readonly constant.
That is why it is overridden in Angular.js below to true where we actually set the value of it.

"isElement": false,
"makeMap": false,
"size": false,
Expand Down
9 changes: 9 additions & 0 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
isBoolean: true,
isPromiseLike: true,
trim: true,
escapeForRegexp: true,
isElement: true,
makeMap: true,
size: true,
Expand Down Expand Up @@ -586,6 +587,14 @@ var trim = function(value) {
return isString(value) ? value.trim() : value;
};

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


/**
* @ngdoc function
Expand Down
9 changes: 0 additions & 9 deletions src/ng/sce.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ var SCE_CONTEXTS = {

// Helper functions follow.

// Copied from:
// http://docs.closure-library.googlecode.com/git/closure_goog_string_string.js.source.html#line962
// Prereq: s is a string.
function escapeForRegexp(s) {
return s.replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').
replace(/\x08/g, '\\x08');
}


function adjustMatcher(matcher) {
if (matcher === 'self') {
return matcher;
Expand Down
2 changes: 1 addition & 1 deletion src/ng/testability.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function $$TestabilityProvider() {
if (dataBinding) {
forEach(dataBinding, function(bindingName) {
if (opt_exactMatch) {
var matcher = new RegExp('(^|\\s)' + expression + '(\\s|\\||$)');
var matcher = new RegExp('(^|\\s)' + escapeForRegexp(expression) + '(\\s|\\||$)');
if (matcher.test(bindingName)) {
matches.push(binding);
}
Expand Down
17 changes: 17 additions & 0 deletions test/ng/testabilitySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ describe('$$testability', function() {
expect(names[0]).toBe(element.find('li')[0]);
});

it('should find bindings with allowed special characters', function() {
element =
'<div>' +
' <span>{{$index}}</span>' +
' <span>{{foo.bar}}</span>' +
' <span>{{foonbar}}</span>' +
'</div>';
element = $compile(element)(scope);
var indexes = $$testability.findBindings(element[0], '$index', true);
expect(indexes.length).toBe(1);
expect(indexes[0]).toBe(element.find('span')[0]);

var foobars = $$testability.findBindings(element[0], 'foo.bar', true);
expect(foobars.length).toBe(1); // it should not match {{foonbar}}
expect(foobars[0]).toBe(element.find('span')[1]);
});

it('should find partial models', function() {
element =
'<div>' +
Expand Down