Skip to content

Commit

Permalink
fix(traversing): Filter text nodes in find function (#1680)
Browse files Browse the repository at this point in the history
  • Loading branch information
5saviahv authored Jan 19, 2021
1 parent bc2e636 commit 9b28b49
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ exports.find = function (selectorOrHaystack) {
var elems = reSiblingSelector.test(selectorOrHaystack)
? context
: context.reduce(function (newElems, elem) {
return newElems.concat(elem.children.filter(isTag));
return Array.isArray(elem.children)
? newElems.concat(elem.children.filter(isTag))
: newElems;
}, []);

var options = Object.assign({ context: context }, this.options);
Expand Down
6 changes: 6 additions & 0 deletions test/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ describe('$(...)', function () {
expect($('#fruits').find('.apple')[0].attribs['class']).toBe('apple');
});

// #1679 - text tags not filtered
it('(single) : should filter out text nodes', function () {
var $root = $('<html>\n' + fruits.replace(/></g, '>\n<') + '\n</html>');
expect($root.find('.apple')[0].attribs['class']).toBe('apple');
});

it('(many) : should find all matching descendant', function () {
expect($('#fruits').find('li')).toHaveLength(3);
});
Expand Down

0 comments on commit 9b28b49

Please # to comment.