Options
All
  • Public
  • Public/Protected
  • All
Menu

cheerio

Index

Type aliases

AcceptedElems

AcceptedElems<T>: BasicAcceptedElems<T> | ((this: T, i: number, el: T) => BasicAcceptedElems<T>)

Elements that can be passed to manipulation methods, including functions.

Type parameters

AcceptedFilters

AcceptedFilters<T>: string | FilterFunction<T> | T | Cheerio<T>

Supported filter types, for traversal methods.

Type parameters

  • T

BasicAcceptedElems

BasicAcceptedElems<T>: Cheerio<T> | T[] | T | string

Elements that can be passed to manipulation methods.

Type parameters

FilterFunction

FilterFunction<T>: (this: T, i: number, el: T) => boolean

Type parameters

  • T

Type declaration

    • (this: T, i: number, el: T): boolean
    • Function signature, for traversal methods.

      Parameters

      • this: T
      • i: number
      • el: T

      Returns boolean

SelectorType

SelectorType: `${SelectorSpecial}${AlphaNumeric}${string}` | `${AlphaNumeric}${string}`

Type for identifying selectors. Allows us to "upgrade" queries using selectors to return Elements.

Properties

default

default: CheerioAPI

Variables

select

select: { aliases: Record<string, string>; filters: Record<string, Filter>; pseudos: Record<string, Pseudo> } = ...

Extension points for adding custom pseudo selectors.

example

Adds a custom pseudo selector :classic, which matches some fun HTML elements that are no more.

import { load, select } from 'cheerio';

// Aliases are short hands for longer HTML selectors
select.aliases.classic = 'marquee,blink';

const $ = load(doc);
$(':classic').html();

Type declaration

  • aliases: Record<string, string>
  • filters: Record<string, Filter>
  • pseudos: Record<string, Pseudo>

Functions

contains

  • contains(container: Node, contained: Node): boolean
  • Checks to see if the contained DOM element is a descendant of the container DOM element.

    alias

    Cheerio.contains

    see

    https://api.jquery.com/jQuery.contains/

    Parameters

    • container: Node

      Potential parent node.

    • contained: Node

      Potential child node.

    Returns boolean

    Indicates if the nodes contain one another.

Const load

  • Create a querying function, bound to a document created from the provided markup.

    Note that similar to web browser contexts, this operation may introduce <html>, <head>, and <body> elements; set isDocument to false to switch to fragment mode and disable this.

    see

    https://cheerio.js.org#loading for additional usage information.

    Parameters

    • content: string | Node | Node[] | Buffer

      Markup to be loaded.

    • Optional options: null | CheerioOptions

      Options for the created instance.

    • isDocument: boolean = true

      Allows parser to be switched to fragment mode.

    Returns CheerioAPI

    The loaded document.

merge

  • merge<T>(arr1: WritableArrayLike<T>, arr2: ArrayLike<T>): ArrayLike<T> | undefined
  • $.merge().

    alias

    Cheerio.merge

    see

    https://api.jquery.com/jQuery.merge/

    Type parameters

    • T

    Parameters

    • arr1: WritableArrayLike<T>

      First array.

    • arr2: ArrayLike<T>

      Second array.

    Returns ArrayLike<T> | undefined

    arr1, with elements of arr2 inserted.

parseHTML

  • parseHTML(data: string, context?: unknown, keepScripts?: boolean): Node[]
  • parseHTML(data?: null | ""): null
  • Parses a string into an array of DOM nodes. The context argument has no meaning for Cheerio, but it is maintained for API compatibility with jQuery.

    alias

    Cheerio.parseHTML

    see

    https://api.jquery.com/jQuery.parseHTML/

    Parameters

    • data: string

      Markup that will be parsed.

    • Optional context: unknown

      Will be ignored. If it is a boolean it will be used as the value of keepScripts.

    • Optional keepScripts: boolean

      If false all scripts will be removed.

    Returns Node[]

    The parsed DOM.

  • Parameters

    • Optional data: null | ""

    Returns null

root

  • Sometimes you need to work with the top-level root element. To query it, you can use $.root().

    example
    $.root().append('<ul id="vegetables"></ul>').html();
    //=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
    alias

    Cheerio.root

    Returns Cheerio<Document>

    Cheerio instance wrapping the root node.