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

Commit 2f8c7b0

Browse files
committed
fix($compile): get $$observe listeners array as own property
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.
1 parent 1f18285 commit 2f8c7b0

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
794794
$observe: function(key, fn) {
795795
var attrs = this,
796796
$$observers = (attrs.$$observers || (attrs.$$observers = {})),
797-
listeners = ($$observers[key] || ($$observers[key] = []));
797+
listeners = (($$observers.hasOwnProperty(key) && $$observers[key]) || ($$observers[key] = []));
798798

799799
listeners.push(fn);
800800
$rootScope.$evalAsync(function() {

test/ng/compileSpec.js

+28
Original file line numberDiff line numberDiff line change
@@ -2693,6 +2693,34 @@ describe('$compile', function() {
26932693
}));
26942694

26952695

2696+
it('should be able to bind attribute names which are present in Object.prototype', function() {
2697+
module(function() {
2698+
directive('inProtoAttr', valueFn({
2699+
scope: {
2700+
'constructor': '@',
2701+
// Spidermonkey extension, may be obsolete in the future
2702+
'watch': '=',
2703+
'unwatch': '&'
2704+
}
2705+
}));
2706+
});
2707+
inject(function($rootScope) {
2708+
expect(function() {
2709+
compile('<div in-proto-attr constructor="hello, world" watch="[]" ' +
2710+
'unwatch="value = !value"></div>');
2711+
}).not.toThrow();
2712+
var isolateScope = element.isolateScope();
2713+
2714+
expect(typeof isolateScope.constructor).toBe('string');
2715+
expect(isArray(isolateScope.watch)).toBe(true);
2716+
expect(typeof isolateScope.unwatch).toBe('function');
2717+
expect($rootScope.value).toBeUndefined();
2718+
isolateScope.unwatch();
2719+
expect($rootScope.value).toBe(true);
2720+
});
2721+
});
2722+
2723+
26962724
describe('attribute', function() {
26972725
it('should copy simple attribute', inject(function() {
26982726
compile('<div><span my-component attr="some text">');

0 commit comments

Comments
 (0)