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

fix($parse): reduce false-positives in isElement tests #5675

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ var trim = (function() {
function isElement(node) {
return !!(node &&
(node.nodeName // we are a direct element
|| (node.on && node.find))); // we have an on and find method part of jQuery API
|| (node.prop && node.attr && node.find))); // we have an on and find method part of jQuery API
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't really in sync with the stuff in $parse, and wasn't to begin with (not looking at the children property). Adding it actually breaks a lot of tests (on travis, seems to pass everything locally). Hmm.

}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function ensureSafeObject(obj, fullExpression) {
'Referencing the Window in Angular expressions is disallowed! Expression: {0}',
fullExpression);
} else if (// isElement(obj)
obj.children && (obj.nodeName || (obj.on && obj.find))) {
obj.children && (obj.nodeName || (obj.prop && obj.attr && obj.find))) {
throw $parseMinErr('isecdom',
'Referencing DOM nodes in Angular expressions is disallowed! Expression: {0}',
fullExpression);
Expand Down
20 changes: 20 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1106,5 +1106,25 @@ describe('angular', function() {
expect(result).toEqual(expected[idx]);
});
}));

// Issue #4805
it('should return false for objects resembling a Backbone Collection', function() {
// Backbone stuff is sort of hard to mock, if you have a better way of doing this,
// please fix this.
var fakeBackboneCollection = {
children: [{}, {}, {}],
find: function() {},
on: function() {},
off: function() {},
bind: function() {}
};
expect(isElement(fakeBackboneCollection)).toBe(false);
});

it('should return false for arrays with node-like properties', function() {
var array = [1,2,3];
array.on = true;
expect(isElement(array)).toBe(false);
});
});
});
22 changes: 22 additions & 0 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,28 @@ describe('parser', function() {
'$parse', 'isecdom', 'Referencing DOM nodes in Angular expressions is ' +
'disallowed! Expression: a.b.doc.on("click")');
}));

// Issue #4805
it('should NOT throw isecdom when referencing a Backbone Collection', function() {
// Backbone stuff is sort of hard to mock, if you have a better way of doing this,
// please fix this.
var fakeBackboneCollection = {
children: [{}, {}, {}],
find: function() {},
on: function() {},
off: function() {},
bind: function() {}
};
scope.backbone = fakeBackboneCollection;
expect(function() { scope.$eval('backbone'); }).not.toThrow();
});

it('should NOT throw isecdom when referencing an array with node properties', function() {
var array = [1,2,3];
array.on = array.attr = array.prop = array.bind = true;
scope.array = array;
expect(function() { scope.$eval('array'); }).not.toThrow();
});
});
});

Expand Down