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

fix($compile): exception when using "watch" as isolated scope binding variable in Firefox #11628

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
14 changes: 11 additions & 3 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2545,9 +2545,19 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
lastValue,
parentGet, parentSet, compare;

if (!hasOwnProperty.call(attrs, attrName)) {
// In the case of user defined a binding with the same name as a method in Object.prototype but didn't set
// the corresponding attribute. We need to make sure subsequent code won't access to the prototype function
attrs[attrName] = undefined;
}

switch (mode) {

case '@':
if (!attrs[attrName] && !optional) {
destination[scopeName] = undefined;
}

attrs.$observe(attrName, function(value) {
destination[scopeName] = value;
});
Expand All @@ -2564,6 +2574,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
return;
}
parentGet = $parse(attrs[attrName]);

if (parentGet.literal) {
compare = equals;
} else {
Expand Down Expand Up @@ -2602,9 +2613,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
break;

case '&':
// Don't assign Object.prototype method to scope
if (!attrs.hasOwnProperty(attrName) && optional) break;

parentGet = $parse(attrs[attrName]);

// Don't assign noop to destination if expression is not valid
Expand Down
151 changes: 151 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2231,6 +2231,45 @@ describe('$compile', function() {
}}
};
});
directive('prototypeMethodNameAsScopeVarA', function() {
return {
scope: {
'constructor': '=?',
'valueOf': '='
},
restrict: 'AE',
template: '<span></span>'
};
});
directive('prototypeMethodNameAsScopeVarB', function() {
return {
scope: {
'constructor': '@?',
'valueOf': '@'
},
restrict: 'AE',
template: '<span></span>'
};
});
directive('prototypeMethodNameAsScopeVarC', function() {
return {
scope: {
'constructor': '&?',
'valueOf': '&'
},
restrict: 'AE',
template: '<span></span>'
};
});
directive('watchAsScopeVar', function() {
return {
scope: {
'watch': '='
},
restrict: 'AE',
template: '<span></span>'
};
});
}));


Expand Down Expand Up @@ -2472,6 +2511,118 @@ describe('$compile', function() {
expect(element.isolateScope()).not.toBe($rootScope);
})
);

it('should handle "=" bindings with same method names in Object.prototype correctly when not present', inject(
function($rootScope, $compile) {
var func = function() {
element = $compile(
'<div prototype-method-name-as-scope-var-a></div>'
)($rootScope);
};

expect(func).not.toThrow();
expect(element.find('span').scope()).toBe(element.isolateScope());
expect(element.isolateScope()).not.toBe($rootScope);
expect(element.isolateScope()['constructor']).toBe($rootScope.constructor);
expect(element.isolateScope()['valueOf']).toBeUndefined();
})
);

it('should handle "=" bindings with same method names in Object.prototype correctly when present', inject(
function($rootScope, $compile) {
$rootScope.constructor = 'constructor';
$rootScope.valueOf = 'valueOf';
var func = function() {
element = $compile(
'<div prototype-method-name-as-scope-var-a constructor="constructor" value-of="valueOf"></div>'
)($rootScope);
};

expect(func).not.toThrow();
expect(element.find('span').scope()).toBe(element.isolateScope());
expect(element.isolateScope()).not.toBe($rootScope);
expect(element.isolateScope()['constructor']).toBe('constructor');
expect(element.isolateScope()['valueOf']).toBe('valueOf');
})
);

it('should handle "@" bindings with same method names in Object.prototype correctly when not present', inject(
function($rootScope, $compile) {
var func = function() {
element = $compile('<div prototype-method-name-as-scope-var-b></div>')($rootScope);
};

expect(func).not.toThrow();
expect(element.find('span').scope()).toBe(element.isolateScope());
expect(element.isolateScope()).not.toBe($rootScope);
expect(element.isolateScope()['constructor']).toBe($rootScope.constructor);
expect(element.isolateScope()['valueOf']).toBeUndefined();
})
);

it('should handle "@" bindings with same method names in Object.prototype correctly when present', inject(
function($rootScope, $compile) {
var func = function() {
element = $compile(
'<div prototype-method-name-as-scope-var-b constructor="constructor" value-of="valueOf"></div>'
)($rootScope);
};

expect(func).not.toThrow();
expect(element.find('span').scope()).toBe(element.isolateScope());
expect(element.isolateScope()).not.toBe($rootScope);
expect(element.isolateScope()['constructor']).toBe('constructor');
expect(element.isolateScope()['valueOf']).toBe('valueOf');
})
);

it('should handle "&" bindings with same method names in Object.prototype correctly when not present', inject(
function($rootScope, $compile) {
var func = function() {
element = $compile('<div prototype-method-name-as-scope-var-c></div>')($rootScope);
};

expect(func).not.toThrow();
expect(element.find('span').scope()).toBe(element.isolateScope());
expect(element.isolateScope()).not.toBe($rootScope);
expect(element.isolateScope()['constructor']).toBe($rootScope.constructor);
expect(element.isolateScope()['valueOf']()).toBeUndefined();
})
);

it('should handle "&" bindings with same method names in Object.prototype correctly when present', inject(
function($rootScope, $compile) {
$rootScope.constructor = function() { return 'constructor'; };
$rootScope.valueOf = function() { return 'valueOf'; };
var func = function() {
element = $compile(
'<div prototype-method-name-as-scope-var-c constructor="constructor()" value-of="valueOf()"></div>'
)($rootScope);
};

expect(func).not.toThrow();
expect(element.find('span').scope()).toBe(element.isolateScope());
expect(element.isolateScope()).not.toBe($rootScope);
expect(element.isolateScope()['constructor']()).toBe('constructor');
expect(element.isolateScope()['valueOf']()).toBe('valueOf');
})
);

it('should not throw exception when using "watch" as binding in Firefox', inject(
function($rootScope, $compile) {
$rootScope.watch = 'watch';
var func = function() {
element = $compile(
'<div watch-as-scope-var watch="watch"></div>'
)($rootScope);
};

expect(func).not.toThrow();
expect(element.find('span').scope()).toBe(element.isolateScope());
expect(element.isolateScope()).not.toBe($rootScope);
expect(element.isolateScope()['watch']).toBe('watch');
})
);
});


Expand Down