Navigation Menu

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

Commit

Permalink
fix(jqLite): attr should ignore comment, text and attribute nodes
Browse files Browse the repository at this point in the history
Follow jQuery handling of the `attr` function

Close #11038
  • Loading branch information
RobiFerentz authored and petebacondarwin committed Mar 17, 2015
1 parent 8743924 commit 181e5eb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/.jshintrc
Expand Up @@ -94,6 +94,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
Expand Up @@ -85,6 +85,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 @@ -1611,6 +1612,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
Expand Up @@ -598,6 +598,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
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

0 comments on commit 181e5eb

Please sign in to comment.