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

Commit

Permalink
fix($rootScope): Set no context when calling helper functions for $watch
Browse files Browse the repository at this point in the history
When calling a $watch getter or listener, do not expose the inner workings with `this`.

Closes: #13909
  • Loading branch information
lgalfaso authored and gkalpak committed Feb 1, 2016
1 parent 8969050 commit ab5c769
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/ng/rootScope.js
Expand Up @@ -744,7 +744,7 @@ function $RootScopeProvider() {
*
*/
$digest: function() {
var watch, value, last,
var watch, value, last, fn, get,
watchers,
length,
dirty, ttl = TTL,
Expand Down Expand Up @@ -790,15 +790,17 @@ function $RootScopeProvider() {
// Most common watches are on primitives, in which case we can short
// circuit it with === operator, only when === fails do we use .equals
if (watch) {
if ((value = watch.get(current)) !== (last = watch.last) &&
get = watch.get;
if ((value = get(current)) !== (last = watch.last) &&
!(watch.eq
? equals(value, last)
: (typeof value === 'number' && typeof last === 'number'
&& isNaN(value) && isNaN(last)))) {
dirty = true;
lastDirtyWatch = watch;
watch.last = watch.eq ? copy(value, null) : value;
watch.fn(value, ((last === initWatchVal) ? value : last), current);
fn = watch.fn;
fn(value, ((last === initWatchVal) ? value : last), current);
if (ttl < 5) {
logIdx = 4 - ttl;
if (!watchLog[logIdx]) watchLog[logIdx] = [];
Expand Down
14 changes: 14 additions & 0 deletions test/ng/rootScopeSpec.js
Expand Up @@ -117,6 +117,20 @@ describe('Scope', function() {
}));


it('should not expose the `inner working of watch', inject(function($rootScope) {
function Getter() {
expect(this).toBeUndefined();
return 'foo';
}
function Listener() {
expect(this).toBeUndefined();
}
if (msie < 10) return;
$rootScope.$watch(Getter, Listener);
$rootScope.$digest();
}));


it('should watch and fire on expression change', inject(function($rootScope) {
var spy = jasmine.createSpy();
$rootScope.$watch('name.first', spy);
Expand Down

0 comments on commit ab5c769

Please sign in to comment.