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

Commit 60366c8

Browse files
committed
fix($parse): correctly assign expressions who's path is undefined and that use brackets notation
Closes #8039
1 parent 494c8aa commit 60366c8

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/ng/parse.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,9 @@ Parser.prototype = {
696696
return getter(self || object(scope, locals));
697697
}, {
698698
assign: function(scope, value, locals) {
699-
return setter(object(scope, locals), field, value, parser.text, parser.options);
699+
var o = object(scope, locals);
700+
if (!o) object.assign(scope, o = {});
701+
return setter(o, field, value, parser.text, parser.options);
700702
}
701703
});
702704
},
@@ -726,10 +728,11 @@ Parser.prototype = {
726728
return v;
727729
}, {
728730
assign: function(self, value, locals) {
729-
var key = indexFn(self, locals);
731+
var key = ensureSafeMemberName(indexFn(self, locals), parser.text);
730732
// prevent overwriting of Function.constructor which would break ensureSafeObject check
731-
var safe = ensureSafeObject(obj(self, locals), parser.text);
732-
return safe[key] = value;
733+
var o = ensureSafeObject(obj(self, locals), parser.text);
734+
if (!o) obj.assign(self, o = {});
735+
return o[key] = value;
733736
}
734737
});
735738
},

test/ng/parseSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,22 @@ describe('parser', function() {
10891089
fn.assign(scope, 123);
10901090
expect(scope).toEqual({a:123});
10911091
}));
1092+
1093+
it('should expose working assignment function for expressions ending with brackets', inject(function($parse) {
1094+
var fn = $parse('a.b["c"]');
1095+
expect(fn.assign).toBeTruthy();
1096+
var scope = {};
1097+
fn.assign(scope, 123);
1098+
expect(scope.a.b.c).toEqual(123);
1099+
}));
1100+
1101+
it('should expose working assignment function for expressions with brackets in the middle', inject(function($parse) {
1102+
var fn = $parse('a["b"].c');
1103+
expect(fn.assign).toBeTruthy();
1104+
var scope = {};
1105+
fn.assign(scope, 123);
1106+
expect(scope.a.b.c).toEqual(123);
1107+
}));
10921108
});
10931109

10941110

0 commit comments

Comments
 (0)