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

Commit

Permalink
fix($compile): get $$observe listeners array as own property
Browse files Browse the repository at this point in the history
Prevent accidentally treating a builtin function from Object.prototype as the binding object, and thus
preventing the compiler from throwing when using attribute binding names which match a property of the
Object prototype.

Closes #9343
Closes #9345
  • Loading branch information
caitp committed Sep 30, 2014
1 parent a1648a7 commit a27d827
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ng/compile.js
Expand Up @@ -929,7 +929,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
*/
$observe: function(key, fn) {
var attrs = this,
$$observers = (attrs.$$observers || (attrs.$$observers = {})),
$$observers = (attrs.$$observers || (attrs.$$observers = Object.create(null))),

This comment has been minimized.

Copy link
@IgorMinar

IgorMinar Oct 9, 2014

Contributor

we have createMap() shortcut for this

This comment has been minimized.

Copy link
@caitp

caitp Oct 9, 2014

Author Contributor

I learned that yesterday and submitted a pr b to use it instead. I'm on a train right now so I can't land it for a bit

listeners = ($$observers[key] || ($$observers[key] = []));

listeners.push(fn);
Expand Down
29 changes: 29 additions & 0 deletions test/ng/compileSpec.js
Expand Up @@ -3124,6 +3124,35 @@ describe('$compile', function() {
}));


it('should be able to bind attribute names which are present in Object.prototype', function() {
module(function() {
directive('inProtoAttr', valueFn({
scope: {
'constructor': '@',
'toString': '&',

// Spidermonkey extension, may be obsolete in the future
'watch': '=',
}
}));
});
inject(function($rootScope) {
expect(function() {
compile('<div in-proto-attr constructor="hello, world" watch="[]" ' +
'to-string="value = !value"></div>');
}).not.toThrow();
var isolateScope = element.isolateScope();

expect(typeof isolateScope.constructor).toBe('string');
expect(isArray(isolateScope.watch)).toBe(true);
expect(typeof isolateScope.toString).toBe('function');
expect($rootScope.value).toBeUndefined();
isolateScope.toString();
expect($rootScope.value).toBe(true);
});
});


describe('bind-once', function () {

function countWatches(scope) {
Expand Down

0 comments on commit a27d827

Please sign in to comment.