Skip to content

Commit

Permalink
fix(traversing): Fix parentsUntil filtering (#1708)
Browse files Browse the repository at this point in the history
  • Loading branch information
5saviahv committed Feb 11, 2021
1 parent 6f35a39 commit bf899d5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ exports.parentsUntil = function (selector, filter) {
var untilNodes;

if (typeof selector === 'string') {
untilNode = select.select(
selector,
this.parents().toArray(),
this.options
)[0];
untilNodes = this.parents(selector).toArray();
} else if (selector && selector.cheerio) {
untilNodes = selector.toArray();
} else if (selector) {
Expand Down Expand Up @@ -186,7 +182,7 @@ exports.parentsUntil = function (selector, filter) {
}, this);

return this._make(
filter ? select.select(filter, parentNodes, this.options) : parentNodes
filter ? select.filter(filter, parentNodes, this.options) : parentNodes
);
};

Expand Down
13 changes: 13 additions & 0 deletions test/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,11 @@ describe('$(...)', function () {
expect(result).toHaveLength(0);
});

it('(selector) : Less simple parentsUntil check with selector', function () {
var result = $('#fruits').parentsUntil('html, body');
expect(result.eq(0).attr('id')).toBe('food');
});

it('(selector not parent) : should return all parents', function () {
var result = $('.orange').parentsUntil('.apple');
expect(result).toHaveLength(4);
Expand All @@ -568,6 +573,14 @@ describe('$(...)', function () {
expect(result[0].attribs.id).toBe('vegetables');
});

it('(selector, filter) : Multiple-filtered parentsUntil check', function () {
var result = $('.orange').parentsUntil('html', 'ul,body');
expect(result).toHaveLength(3);
expect(result.eq(0).prop('tagName')).toBe('BODY');
expect(result.eq(1).attr('id')).toBe('food');
expect(result.eq(2).attr('id')).toBe('fruits');
});

it('() : should return empty object when called on an empty object', function () {
var result = $('.saladbar').parentsUntil();
expect(result).toHaveLength(0);
Expand Down

0 comments on commit bf899d5

Please sign in to comment.