Skip to content

Commit 966f6d8

Browse files
committed
fix($parse): Initialize elements in an array from left to right
When constructing an array, never lazy initialize the elements and build the array strictly from left to right. When evaluating the expressions in a function call, never do so lazy. When evaluating expressions inside object literals, never do so lazy. Closes: angular#10968
1 parent 28114fa commit 966f6d8

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/ng/parse.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ ASTCompiler.prototype = {
996996
self.if(self.notNull(right), function() {
997997
self.addEnsureSafeFunction(right);
998998
forEach(ast.arguments, function(expr) {
999-
self.recurse(expr, undefined, undefined, function(argument) {
999+
self.recurse(expr, self.nextId(), undefined, function(argument) {
10001000
args.push(self.ensureSafeObject(argument));
10011001
});
10021002
});
@@ -1027,14 +1027,14 @@ ASTCompiler.prototype = {
10271027
self.addEnsureSafeObject(self.member(left.context, left.name, left.computed));
10281028
expression = self.member(left.context, left.name, left.computed) + ast.operator + right;
10291029
self.assign(intoId, expression);
1030-
recursionFn(expression);
1030+
recursionFn(intoId || expression);
10311031
});
10321032
}, 1);
10331033
break;
10341034
case AST.ArrayExpression:
10351035
args = [];
10361036
forEach(ast.elements, function(expr) {
1037-
self.recurse(expr, undefined, undefined, function(argument) {
1037+
self.recurse(expr, self.nextId(), undefined, function(argument) {
10381038
args.push(argument);
10391039
});
10401040
});
@@ -1045,7 +1045,7 @@ ASTCompiler.prototype = {
10451045
case AST.ObjectExpression:
10461046
args = [];
10471047
forEach(ast.properties, function(property) {
1048-
self.recurse(property.value, undefined, undefined, function(expr) {
1048+
self.recurse(property.value, self.nextId(), undefined, function(expr) {
10491049
args.push(self.escape(
10501050
property.key.type === AST.Identifier ? property.key.name :
10511051
('' + property.key.value)) +

test/ng/parseSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,24 @@ describe('parser', function() {
22042204
expect(scope.$eval('a + \n b.c + \r "\td" + \t \r\n\r "\r\n\n"')).toEqual("abc\td\r\n\n");
22052205
});
22062206

2207+
2208+
// https://github.com/angular/angular.js/issues/10968
2209+
it('should evaluate arrays literals initializers left-to-right', inject(function($parse) {
2210+
var s = {c:function() {return {b: 1}; }};
2211+
expect($parse("e=1;[a=c(),d=a.b+1]")(s)).toEqual([{b: 1}, 2]);
2212+
}));
2213+
2214+
it('should evaluate function arguments left-to-right', inject(function($parse) {
2215+
var s = {c:function() {return {b: 1}; }, i: function(x, y) { return [x, y];}};
2216+
expect($parse("e=1;i(a=c(),d=a.b+1)")(s)).toEqual([{b: 1}, 2]);
2217+
}));
2218+
2219+
it('should evaluate object properties expressions left-to-right', inject(function($parse) {
2220+
var s = {c:function() {return {b: 1}; }};
2221+
expect($parse("e=1;{x: a=c(), y: d=a.b+1}")(s)).toEqual({x: {b: 1}, y: 2});
2222+
}));
2223+
2224+
22072225
describe('sandboxing', function() {
22082226
describe('Function constructor', function() {
22092227
it('should not tranverse the Function constructor in the getter', function() {

0 commit comments

Comments
 (0)