From a863b5a509a30cef2b5d02da528ae760262b43ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hubert=20SABLONNI=C3=88RE?= Date: Fri, 25 Oct 2013 21:52:49 +0200 Subject: [PATCH] fix(jqLite): ignore incompatible nodes on find() 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 --- src/jqLite.js | 6 +++++- test/jqLiteSpec.js | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/jqLite.js b/src/jqLite.js index 727218a94244..3923cf566376 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -809,7 +809,11 @@ forEach({ }, find: function(element, selector) { - return element.getElementsByTagName(selector); + if (element.getElementsByTagName) { + return element.getElementsByTagName(selector); + } else { + return []; + } }, clone: jqLiteClone, diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 24920273e42f..6edb71a4f4ca 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -1301,6 +1301,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('
aa
text
bb
'); + var innerSpan = divs.find('span'); + expect(innerSpan.length).toEqual(2); + }); });