Skip to content

Commit 3988630

Browse files
authored
feat: Pass options to cheerio-select (#2511)
1 parent 12128e1 commit 3988630

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

src/api/traversing.ts

+6
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ export function find<T extends AnyNode>(
7070
const options = {
7171
context,
7272
root: this._root?.[0],
73+
74+
// Pass options that are recognized by `cheerio-select`
7375
xmlMode: this.options.xmlMode,
76+
lowerCaseTags: this.options.lowerCaseTags,
77+
lowerCaseAttributeNames: this.options.lowerCaseAttributeNames,
78+
pseudos: this.options.pseudos,
79+
quirksMode: this.options.quirksMode,
7480
};
7581

7682
return this._make(select.select(selectorOrHaystack, elems, options));

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import { filters, pseudos, aliases } from 'cheerio-select';
6868
/**
6969
* Extension points for adding custom pseudo selectors.
7070
*
71+
* @deprecated Use the `options.pseudos` option instead.
7172
* @example <caption>Adds a custom pseudo selector `:classic`, which matches
7273
* some fun HTML elements that are no more.</caption>
7374
*

src/options.ts

+45-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { DomHandlerOptions } from 'domhandler';
22
import type { ParserOptions } from 'htmlparser2';
3+
import type { Options as SelectOptions } from 'cheerio-select';
34

45
/** Options accepted by htmlparser2, the default parser for XML. */
56
export interface HTMLParser2Options extends DomHandlerOptions, ParserOptions {}
@@ -11,14 +12,6 @@ export interface Parse5Options {
1112
sourceCodeLocationInfo?: boolean;
1213
}
1314

14-
/** Internal options for Cheerio. */
15-
export interface InternalOptions extends HTMLParser2Options, Parse5Options {
16-
_useHtmlParser2?: boolean;
17-
18-
/** The base URI for the document. Used for the `href` and `src` props. */
19-
baseURI?: string | URL; // eslint-disable-line node/no-unsupported-features/node-builtins
20-
}
21-
2215
/**
2316
* Options accepted by Cheerio.
2417
*
@@ -31,6 +24,50 @@ export interface CheerioOptions extends HTMLParser2Options, Parse5Options {
3124

3225
/** The base URI for the document. Used for the `href` and `src` props. */
3326
baseURI?: string | URL; // eslint-disable-line node/no-unsupported-features/node-builtins
27+
28+
/**
29+
* Is the document in quirks mode?
30+
*
31+
* This will lead to `.className` and `#id` being case-insensitive.
32+
*
33+
* @default false
34+
*/
35+
quirksMode?: SelectOptions['quirksMode'];
36+
/**
37+
* Extension point for pseudo-classes.
38+
*
39+
* Maps from names to either strings of functions.
40+
*
41+
* - A string value is a selector that the element must match to be selected.
42+
* - A function is called with the element as its first argument, and optional
43+
* parameters second. If it returns true, the element is selected.
44+
*
45+
* @example
46+
*
47+
* ```js
48+
* const $ = cheerio.load(
49+
* '<div class="foo"></div><div data-bar="boo"></div>',
50+
* {
51+
* pseudos: {
52+
* // `:foo` is an alias for `div.foo`
53+
* foo: 'div.foo',
54+
* // `:bar(val)` is equivalent to `[data-bar=val s]`
55+
* bar: (el, val) => el.attribs['data-bar'] === val,
56+
* },
57+
* }
58+
* );
59+
*
60+
* $(':foo').length; // 1
61+
* $('div:bar(boo)').length; // 1
62+
* $('div:bar(baz)').length; // 0
63+
* ```
64+
*/
65+
pseudos?: SelectOptions['pseudos'];
66+
}
67+
68+
/** Internal options for Cheerio. */
69+
export interface InternalOptions extends Omit<CheerioOptions, 'xml'> {
70+
_useHtmlParser2?: boolean;
3471
}
3572

3673
const defaultOpts: CheerioOptions = {

0 commit comments

Comments
 (0)