From b692b00934e033c9f231a985172b27627d38eecc Mon Sep 17 00:00:00 2001 From: Julie Ralph Date: Mon, 13 Oct 2014 12:34:36 -0700 Subject: [PATCH] fix(testability): findBindings should escape binding expressions before performing regexp Move the function to escape regexps to Angular.js, fix the link, and use it in the $$testability service. See #9595 --- src/.jshintrc | 1 + src/Angular.js | 9 +++++++++ src/ng/sce.js | 9 --------- src/ng/testability.js | 2 +- test/ng/testabilitySpec.js | 17 +++++++++++++++++ 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/.jshintrc b/src/.jshintrc index 784f77f45380..9db31b990a6a 100644 --- a/src/.jshintrc +++ b/src/.jshintrc @@ -54,6 +54,7 @@ "isBoolean": false, "isPromiseLike": false, "trim": false, + "escapeForRegexp": true, "isElement": false, "makeMap": false, "size": false, diff --git a/src/Angular.js b/src/Angular.js index 9b421aca53c4..7803b9de0c89 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -49,6 +49,7 @@ isBoolean: true, isPromiseLike: true, trim: true, + escapeForRegexp: true, isElement: true, makeMap: true, size: true, @@ -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(/([-()\[\]{}+?*.$\^|,:#' + + ' {{$index}}' + + ' {{foo.bar}}' + + ' {{foonbar}}' + + ''; + 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 = '
' +