From 0601fcdf4a1becf00d3496f9196bfbc6a6964eaf Mon Sep 17 00:00:00 2001 From: rodyhaddad Date: Tue, 8 Jul 2014 14:54:44 -0700 Subject: [PATCH] fix($parse): correctly assign expressions who's path is undefined and that use brackets notation Closes #8039 --- src/ng/parse.js | 11 +++++++---- test/ng/parseSpec.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/ng/parse.js b/src/ng/parse.js index 78582bde210e..de073f95e469 100644 --- a/src/ng/parse.js +++ b/src/ng/parse.js @@ -679,7 +679,9 @@ Parser.prototype = { return getter(self || object(scope, locals)); }, { assign: function(scope, value, locals) { - return setter(object(scope, locals), field, value, parser.text); + var o = object(scope, locals); + if (!o) object.assign(scope, o = {}); + return setter(o, field, value, parser.text); } }); }, @@ -701,10 +703,11 @@ Parser.prototype = { return v; }, { assign: function(self, value, locals) { - var key = indexFn(self, locals); + var key = ensureSafeMemberName(indexFn(self, locals), parser.text); // prevent overwriting of Function.constructor which would break ensureSafeObject check - var safe = ensureSafeObject(obj(self, locals), parser.text); - return safe[key] = value; + var o = ensureSafeObject(obj(self, locals), parser.text); + if (!o) obj.assign(self, o = {}); + return o[key] = value; } }); }, diff --git a/test/ng/parseSpec.js b/test/ng/parseSpec.js index 327aee71a14d..dd2aa555a1a1 100644 --- a/test/ng/parseSpec.js +++ b/test/ng/parseSpec.js @@ -1078,6 +1078,22 @@ describe('parser', function() { fn.assign(scope, 123); expect(scope).toEqual({a:123}); })); + + it('should expose working assignment function for expressions ending with brackets', inject(function($parse) { + var fn = $parse('a.b["c"]'); + expect(fn.assign).toBeTruthy(); + var scope = {}; + fn.assign(scope, 123); + expect(scope.a.b.c).toEqual(123); + })); + + it('should expose working assignment function for expressions with brackets in the middle', inject(function($parse) { + var fn = $parse('a["b"].c'); + expect(fn.assign).toBeTruthy(); + var scope = {}; + fn.assign(scope, 123); + expect(scope.a.b.c).toEqual(123); + })); }); describe('one-time binding', function() {