This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix($parse): allow assignment on objects in locals #10322
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -488,6 +488,62 @@ describe('parser', function() { | |
expect(scope.b).toEqual(234); | ||
}); | ||
|
||
it('should allow use of locals in the left side of an assignment', inject(function($rootScope) { | ||
$rootScope.a = {}; | ||
$rootScope.key = "value"; | ||
var localA = {}; | ||
|
||
//getterFn | ||
$rootScope.$eval('a.value = 1', {a: localA}); | ||
expect(localA.value).toBe(1); | ||
|
||
$rootScope.$eval('w.a.value = 2', {w: {a: localA}}); | ||
expect(localA.value).toBe(2); | ||
|
||
//field access | ||
$rootScope.$eval('(a).value = 3', {a: localA}); | ||
expect(localA.value).toBe(3); | ||
|
||
$rootScope.$eval('{c: {b: a}}.c.b.value = 4', {a: localA}); | ||
expect(localA.value).toBe(4); | ||
|
||
//object index | ||
$rootScope.$eval('a[key] = 5', {a: localA}); | ||
expect(localA.value).toBe(5); | ||
|
||
$rootScope.$eval('w.a[key] = 6', {w: {a: localA}}); | ||
expect(localA.value).toBe(6); | ||
|
||
$rootScope.$eval('{c: {b: a}}.c.b[key] = 7', {a: localA}); | ||
expect(localA.value).toBe(7); | ||
|
||
//Nothing should have touched the $rootScope.a | ||
expect($rootScope.a.value).toBeUndefined(); | ||
})); | ||
|
||
it('should allow use of locals in sub expressions of the left side of an assignment', inject(function($rootScope, $parse) { | ||
delete $rootScope.x; | ||
$rootScope.$eval('x[a][b] = true', {a: 'foo', b: 'bar'}); | ||
expect($rootScope.x.foo.bar).toBe(true); | ||
|
||
delete $rootScope.x; | ||
$rootScope.$eval('x.foo[b] = true', {b: 'bar'}); | ||
expect($rootScope.x.foo.bar).toBe(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this block of test repeated? Did you mean? $rootScope.$eval('x.foo[b] = true', {a: 'foo', b: 'bar'}); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. That's from #10322 (comment) but I changed them to all use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or actually I'll change it to what you suggested instead of deleting it... |
||
|
||
delete $rootScope.x; | ||
$rootScope.$eval('x[a].bar = true', {a: 'foo'}); | ||
expect($rootScope.x.foo.bar).toBe(true); | ||
})); | ||
|
||
it('should ignore locals beyond the root object of an assignment expression', inject(function($rootScope) { | ||
var a = {}; | ||
var locals = {a: a}; | ||
$rootScope.b = {a: {value: 123}}; | ||
$rootScope.$eval('b.a.value = 1', locals); | ||
expect(a.value).toBeUndefined(); | ||
expect($rootScope.b.a.value).toBe(1); | ||
})); | ||
|
||
it('should evaluate assignments in ternary operator', function() { | ||
scope.$eval('a = 1 ? 2 : 3'); | ||
expect(scope.a).toBe(2); | ||
|
@@ -799,6 +855,12 @@ describe('parser', function() { | |
}).toThrowMinErr( | ||
'$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' + | ||
'Expression: a.toString.constructor'); | ||
|
||
expect(function() { | ||
scope.$eval("c.a = 1", {c: Function.prototype.constructor}); | ||
}).toThrowMinErr( | ||
'$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' + | ||
'Expression: c.a'); | ||
}); | ||
|
||
it('should disallow traversing the Function object in a setter: E02', function() { | ||
|
@@ -933,6 +995,14 @@ describe('parser', function() { | |
'$parse', 'isecobj', 'Referencing Object in Angular expressions is disallowed! ' + | ||
'Expression: foo["bar"]["keys"](foo)'); | ||
}); | ||
|
||
it('should NOT allow access to Object constructor in assignment locals', function() { | ||
expect(function() { | ||
scope.$eval("O.constructor.a = 1", {O: Object}); | ||
}).toThrowMinErr( | ||
'$parse', 'isecobj', 'Referencing Object in Angular expressions is disallowed! ' + | ||
'Expression: O.constructor.a'); | ||
}); | ||
}); | ||
|
||
describe('Window and $element/node', function() { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is what I'm referring to, @jbedard
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just 1 of 3
assign
implementations though. The others already had the third param so it is nothing new, it was just missing from this implementation...You want to document it to make it public though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no reason not to document it --- nobody has a good reason to use it anyways, but we aren't using it as a private API anywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used within this file, just this version (unlike the other 2/3) did not support it...