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

Commit

Permalink
fix(jqLite): ignore incompatible nodes on find()
Browse files Browse the repository at this point in the history
When a jqLite collection contains text nodes, find() does not work :-(

This fix ignores all nodes than can't do getElementsByTagName()

It seems a little bit faster than testing nodeType : http://jsperf.com/nodetype-vs-duck-typing

Closes #4120
  • Loading branch information
hsablonniere authored and tbosch committed Dec 3, 2013
1 parent 81b8185 commit 1169b54
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,11 @@ forEach({
},

find: function(element, selector) {
return element.getElementsByTagName(selector);
if (element.getElementsByTagName) {
return element.getElementsByTagName(selector);
} else {
return [];
}
},

clone: jqLiteClone,
Expand Down
6 changes: 6 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,12 @@ describe('jqLite', function() {
expect(innerDiv.length).toEqual(1);
expect(innerDiv.html()).toEqual('text');
});

it('should find child by name and not care about text nodes', function() {
var divs = jqLite('<div><span>aa</span></div>text<div><span>bb</span></div>');
var innerSpan = divs.find('span');
expect(innerSpan.length).toEqual(2);
});
});


Expand Down

0 comments on commit 1169b54

Please sign in to comment.