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

fix($compile): do not initialize optional '&' binding if attribute not specified #9216

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
7 changes: 7 additions & 0 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2576,7 +2576,14 @@ 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
if (parentGet === noop && optional) break;

destination[scopeName] = function(locals) {
return parentGet(scope, locals);
};
Expand Down
2 changes: 1 addition & 1 deletion src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ function $ParseProvider() {
return addInterceptor(exp, interceptorFn);

default:
return addInterceptor(noop, interceptorFn);
return noop;
}
};

Expand Down
36 changes: 35 additions & 1 deletion test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3084,7 +3084,9 @@ describe('$compile', function() {
colref: '=*',
colrefAlias: '=* colref',
expr: '&',
exprAlias: '&expr'
optExpr: '&?',
exprAlias: '&expr',
constructor: '&?'
},
link: function(scope) {
componentScope = scope;
Expand Down Expand Up @@ -3224,6 +3226,38 @@ describe('$compile', function() {
});


it('should not initialize scope value if optional expression binding is not passed', inject(function($compile) {
compile('<div my-component></div>');
var isolateScope = element.isolateScope();
expect(isolateScope.optExpr).toBeUndefined();
}));


it('should not initialize scope value if optional expression binding with Object.prototype name is not passed', inject(function($compile) {
compile('<div my-component></div>');
var isolateScope = element.isolateScope();
expect(isolateScope.constructor).toBe($rootScope.constructor);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Object.prototype stuff is optional --- I mean, it's basically going to do the wrong thing no matter what, so it's kind of weird. One option is to just always assign to undefined instead of just breaking, I guess?

}));


it('should initialize scope value if optional expression binding is passed', inject(function($compile) {
compile('<div my-component opt-expr="value = \'did!\'"></div>');
var isolateScope = element.isolateScope();
expect(typeof isolateScope.optExpr).toBe('function');
expect(isolateScope.optExpr()).toBe('did!');
expect($rootScope.value).toBe('did!');
}));


it('should initialize scope value if optional expression binding with Object.prototype name is passed', inject(function($compile) {
compile('<div my-component constructor="value = \'did!\'"></div>');
var isolateScope = element.isolateScope();
expect(typeof isolateScope.constructor).toBe('function');
expect(isolateScope.constructor()).toBe('did!');
expect($rootScope.value).toBe('did!');
}));


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

function countWatches(scope) {
Expand Down