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: Have load export a function #1869

Merged
merged 11 commits into from
May 17, 2021
Prev Previous commit
Next Next commit
Stop CheerioAPI from extending Cheerio statics
fb55 committed May 7, 2021

Verified

This commit was signed with the committer’s verified signature.
sandhose Quentin Gliech
commit e590c546adf727a5229c16453811455ba5aafcd5
25 changes: 20 additions & 5 deletions src/cheerio.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import {
import { isHtml, isCheerio } from './utils';
import type { Node, Document, Element } from 'domhandler';
import * as Static from './static';
import type { load } from './load';
import type * as Load from './load';
import { SelectorType, BasicAcceptedElems } from './types';

import * as Attributes from './api/attributes';
@@ -23,6 +23,9 @@ type ManipulationType = typeof Manipulation;
type CssType = typeof Css;
type FormsType = typeof Forms;

type StaticType = typeof Static;
type LoadType = typeof Load;

/*
* The API
*/
@@ -69,7 +72,7 @@ export class Cheerio<T> implements ArrayLike<T> {
public static root = Static.root;
public static contains = Static.contains;
public static merge = Static.merge;
public static load: typeof load;
public static load: typeof Load.load;

/** Mimic jQuery's prototype alias for plugin authors. */
public static fn = Cheerio.prototype;
@@ -222,19 +225,31 @@ function isNode(obj: any): obj is Node {
);
}

type CheerioClassType = typeof Cheerio;

/**
* Wrapper around the `Cheerio` class, making it possible to create a new
* instance without using `new`.
*/
export interface CheerioAPI extends CheerioClassType {
export interface CheerioAPI extends StaticType, LoadType {
<T extends Node, S extends string>(
selector?: S | BasicAcceptedElems<T>,
context?: BasicAcceptedElems<Node> | null,
root?: BasicAcceptedElems<Document>,
options?: CheerioOptions
): Cheerio<S extends SelectorType ? Element : T>;

/**
* The root the document was originally loaded with. Set in `.load`.
*
* @private
*/
_root: Document | undefined;

/**
* The options the document was originally loaded with. Set in `.load`.
*
* @private
*/
_options: InternalOptions | undefined;
}

// Make it possible to call Cheerio without using `new`.
23 changes: 10 additions & 13 deletions src/static.ts
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ import { render as renderWithHtmlparser2 } from './parsers/htmlparser2';
* @returns The rendered document.
*/
function render(
that: typeof Cheerio | undefined,
that: CheerioAPI | undefined,
dom: ArrayLike<Node> | Node | string | undefined,
options: InternalOptions
): string {
@@ -63,10 +63,7 @@ function isOptions(
* @param options - Options for the renderer.
* @returns The rendered document.
*/
export function html(
this: typeof Cheerio | void,
options?: CheerioOptions
): string;
export function html(this: CheerioAPI | void, options?: CheerioOptions): string;
/**
* Renders the document.
*
@@ -75,12 +72,12 @@ export function html(
* @returns The rendered document.
*/
export function html(
this: typeof Cheerio | void,
this: CheerioAPI | void,
dom?: string | ArrayLike<Node> | Node,
options?: CheerioOptions
): string;
export function html(
this: typeof Cheerio | void,
this: CheerioAPI | void,
dom?: string | ArrayLike<Node> | Node | CheerioOptions,
options?: CheerioOptions
): string {
@@ -119,7 +116,7 @@ export function html(
* @returns THe rendered document.
*/
export function xml(
this: typeof Cheerio,
this: CheerioAPI,
dom?: string | ArrayLike<Node> | Node
): string {
const options = { ...this._options, xmlMode: true };
@@ -134,7 +131,7 @@ export function xml(
* @returns The rendered document.
*/
export function text(
this: typeof Cheerio | void,
this: CheerioAPI | void,
elements?: ArrayLike<Node>
): string {
const elems = elements ? elements : this ? this.root() : [];
@@ -170,14 +167,14 @@ export function text(
* @see {@link https://api.jquery.com/jQuery.parseHTML/}
*/
export function parseHTML(
this: typeof Cheerio,
this: CheerioAPI,
data: string,
context?: unknown | boolean,
keepScripts?: boolean
): Node[];
export function parseHTML(this: typeof Cheerio, data?: '' | null): null;
export function parseHTML(this: CheerioAPI, data?: '' | null): null;
export function parseHTML(
this: typeof Cheerio,
this: CheerioAPI,
data?: string | null,
context?: unknown | boolean,
keepScripts = typeof context === 'boolean' ? context : false
@@ -219,7 +216,7 @@ export function parseHTML(
* @returns Cheerio instance wrapping the root node.
* @alias Cheerio.root
*/
export function root(this: typeof Cheerio): Cheerio<Document> {
export function root(this: CheerioAPI): Cheerio<Document> {
const fn = (this as unknown) as CheerioAPI;
return fn(this._root);
}