Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(traversing): Bump cheerio-select, adopt changes #1922

Merged
merged 1 commit into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 31 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"node": ">= 6"
},
"dependencies": {
"cheerio-select": "^1.4.0",
"cheerio-select": "^1.5.0",
"dom-serializer": "^1.3.2",
"domhandler": "^4.2.0",
"htmlparser2": "^6.1.0",
Expand Down
42 changes: 25 additions & 17 deletions src/api/traversing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* @module cheerio/traversing
*/

import { Node, Element, hasChildren, isDocument } from 'domhandler';
import { Node, Element, hasChildren, isDocument, Document } from 'domhandler';
import type { Cheerio } from '../cheerio';
import * as select from 'cheerio-select';
import { domEach, isTag, isCheerio } from '../utils';
import { contains } from '../static';
import { InternalOptions } from '../options';
import { DomUtils } from 'htmlparser2';
import type { FilterFunction, AcceptedFilters } from '../types';
const { uniqueSort } = DomUtils;
Expand Down Expand Up @@ -55,15 +54,13 @@ export function find<T extends Node>(

const elems = reSiblingSelector.test(selectorOrHaystack)
? context
: context.reduce<Node[]>(
(newElems, elem) =>
hasChildren(elem)
? newElems.concat(elem.children.filter(isTag))
: newElems,
[]
);
: this.children().toArray();

const options = { context, xmlMode: this.options.xmlMode };
const options = {
context,
root: this._root?.[0],
xmlMode: this.options.xmlMode,
};

return this._make(select.select(selectorOrHaystack, elems, options));
}
Expand All @@ -90,7 +87,12 @@ function _getMatcher<P>(
let matched: Element[] = matchMap(fn, this);

if (selector) {
matched = filterArray(matched, selector, this.options);
matched = filterArray(
matched,
selector,
this.options.xmlMode,
this._root?.[0]
);
}

return this._make(
Expand Down Expand Up @@ -296,7 +298,11 @@ export function closest<T extends Node>(

domEach(this, (elem: Node | null) => {
while (elem && elem.type !== 'root') {
if (!selector || filterArray([elem], selector, this.options).length) {
if (
!selector ||
filterArray([elem], selector, this.options.xmlMode, this._root?.[0])
.length
) {
// Do not add duplicate elements to the set
if (elem && !set.includes(elem)) {
set.push(elem);
Expand Down Expand Up @@ -685,16 +691,19 @@ export function filter<T>(
this: Cheerio<T>,
match: AcceptedFilters<T>
): Cheerio<unknown> {
return this._make<unknown>(filterArray(this.toArray(), match, this.options));
return this._make<unknown>(
filterArray(this.toArray(), match, this.options.xmlMode, this._root?.[0])
);
}

export function filterArray<T>(
nodes: T[],
match: AcceptedFilters<T>,
options: InternalOptions
xmlMode?: boolean,
root?: Document
): Element[] | T[] {
return typeof match === 'string'
? select.filter(match, (nodes as unknown as Node[]).filter(isTag), options)
? select.filter(match, nodes as unknown as Node[], { xmlMode, root })
: nodes.filter(getFilterFn<T>(match));
}

Expand Down Expand Up @@ -766,8 +775,7 @@ export function not<T extends Node>(
let nodes = this.toArray();

if (typeof match === 'string') {
const elements = (nodes as Node[]).filter(isTag);
const matches = new Set<Node>(select.filter(match, elements, this.options));
const matches = new Set<Node>(select.filter(match, nodes, this.options));
nodes = nodes.filter((el) => !matches.has(el));
} else {
const filterFn = getFilterFn(match);
Expand Down
3 changes: 1 addition & 2 deletions src/cheerio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export class Cheerio<T> implements ArrayLike<T> {

options: InternalOptions;
/**
* The root of the document. Can be overwritten by using the `root` argument
* of the constructor.
* The root of the document. Can be set by using the `root` argument of the constructor.
*
* @private
*/
Expand Down