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

fix(jqLite.attr): ignore comment, text and attribute nodes when setting/... #11275

Closed
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions src/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"skipDestroyOnNextJQueryCleanData": true,

"NODE_TYPE_ELEMENT": false,
"NODE_TYPE_ATTRIBUTE": false,
"NODE_TYPE_TEXT": false,
"NODE_TYPE_COMMENT": false,
"NODE_TYPE_COMMENT": false,
Expand Down
2 changes: 2 additions & 0 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
createMap: true,

NODE_TYPE_ELEMENT: true,
NODE_TYPE_ATTRIBUTE: true,
NODE_TYPE_TEXT: true,
NODE_TYPE_COMMENT: true,
NODE_TYPE_DOCUMENT: true,
Expand Down Expand Up @@ -1739,6 +1740,7 @@ function createMap() {
}

var NODE_TYPE_ELEMENT = 1;
var NODE_TYPE_ATTRIBUTE = 2;
var NODE_TYPE_TEXT = 3;
var NODE_TYPE_COMMENT = 8;
var NODE_TYPE_DOCUMENT = 9;
Expand Down
4 changes: 4 additions & 0 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ forEach({
},

attr: function(element, name, value) {
var nodeType = element.nodeType;
if (nodeType === NODE_TYPE_TEXT || nodeType === NODE_TYPE_ATTRIBUTE || nodeType === NODE_TYPE_COMMENT) {
return;
}
var lowercasedName = lowercase(name);
if (BOOLEAN_ATTR[lowercasedName]) {
if (isDefined(value)) {
Expand Down
24 changes: 24 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,30 @@ describe('jqLite', function() {
expect(elm.attr('readOnly')).toBeUndefined();
expect(elm.attr('disabled')).toBeUndefined();
});

it('should do nothing when setting or getting on attribute nodes', function() {
var attrNode = jqLite(document.createAttribute('myattr'));
expect(attrNode).toBeDefined();
expect(attrNode[0].nodeType).toEqual(2);
expect(attrNode.attr('some-attribute','somevalue')).toEqual(attrNode);
expect(attrNode.attr('some-attribute')).toBeUndefined();
});

it('should do nothing when setting or getting on text nodes', function() {
var textNode = jqLite(document.createTextNode('some text'));
expect(textNode).toBeDefined();
expect(textNode[0].nodeType).toEqual(3);
expect(textNode.attr('some-attribute','somevalue')).toEqual(textNode);
expect(textNode.attr('some-attribute')).toBeUndefined();
});

it('should do nothing when setting or getting on comment nodes', function() {
var comment = jqLite(document.createComment('some comment'));
expect(comment).toBeDefined();
expect(comment[0].nodeType).toEqual(8);
expect(comment.attr('some-attribute','somevalue')).toEqual(comment);
expect(comment.attr('some-attribute')).toBeUndefined();
});
});


Expand Down