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

feat: Pass options to cheerio-select #2511

Merged
merged 1 commit into from
May 1, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/api/traversing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ export function find<T extends AnyNode>(
const options = {
context,
root: this._root?.[0],

// Pass options that are recognized by `cheerio-select`
xmlMode: this.options.xmlMode,
lowerCaseTags: this.options.lowerCaseTags,
lowerCaseAttributeNames: this.options.lowerCaseAttributeNames,
pseudos: this.options.pseudos,
quirksMode: this.options.quirksMode,
};

return this._make(select.select(selectorOrHaystack, elems, options));
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import { filters, pseudos, aliases } from 'cheerio-select';
/**
* Extension points for adding custom pseudo selectors.
*
* @deprecated Use the `options.pseudos` option instead.
* @example <caption>Adds a custom pseudo selector `:classic`, which matches
* some fun HTML elements that are no more.</caption>
*
Expand Down
53 changes: 45 additions & 8 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { DomHandlerOptions } from 'domhandler';
import type { ParserOptions } from 'htmlparser2';
import type { Options as SelectOptions } from 'cheerio-select';

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

/** Internal options for Cheerio. */
export interface InternalOptions extends HTMLParser2Options, Parse5Options {
_useHtmlParser2?: boolean;

/** The base URI for the document. Used for the `href` and `src` props. */
baseURI?: string | URL; // eslint-disable-line node/no-unsupported-features/node-builtins
}

/**
* Options accepted by Cheerio.
*
Expand All @@ -31,6 +24,50 @@ export interface CheerioOptions extends HTMLParser2Options, Parse5Options {

/** The base URI for the document. Used for the `href` and `src` props. */
baseURI?: string | URL; // eslint-disable-line node/no-unsupported-features/node-builtins

/**
* Is the document in quirks mode?
*
* This will lead to `.className` and `#id` being case-insensitive.
*
* @default false
*/
quirksMode?: SelectOptions['quirksMode'];
/**
* Extension point for pseudo-classes.
*
* Maps from names to either strings of functions.
*
* - A string value is a selector that the element must match to be selected.
* - A function is called with the element as its first argument, and optional
* parameters second. If it returns true, the element is selected.
*
* @example
*
* ```js
* const $ = cheerio.load(
* '<div class="foo"></div><div data-bar="boo"></div>',
* {
* pseudos: {
* // `:foo` is an alias for `div.foo`
* foo: 'div.foo',
* // `:bar(val)` is equivalent to `[data-bar=val s]`
* bar: (el, val) => el.attribs['data-bar'] === val,
* },
* }
* );
*
* $(':foo').length; // 1
* $('div:bar(boo)').length; // 1
* $('div:bar(baz)').length; // 0
* ```
*/
pseudos?: SelectOptions['pseudos'];
}

/** Internal options for Cheerio. */
export interface InternalOptions extends Omit<CheerioOptions, 'xml'> {
_useHtmlParser2?: boolean;
}

const defaultOpts: CheerioOptions = {
Expand Down