You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
522 lines
16 KiB
522 lines
16 KiB
2 years ago
|
/**
|
||
|
* Methods for modifying the DOM structure.
|
||
|
*
|
||
|
* @module cheerio/manipulation
|
||
|
*/
|
||
|
import { AnyNode } from 'domhandler';
|
||
|
import type { Cheerio } from '../cheerio.js';
|
||
|
import type { BasicAcceptedElems, AcceptedElems } from '../types.js';
|
||
|
/**
|
||
|
* Create an array of nodes, recursing into arrays and parsing strings if necessary.
|
||
|
*
|
||
|
* @private
|
||
|
* @category Manipulation
|
||
|
* @param elem - Elements to make an array of.
|
||
|
* @param clone - Optionally clone nodes.
|
||
|
* @returns The array of nodes.
|
||
|
*/
|
||
|
export declare function _makeDomArray<T extends AnyNode>(this: Cheerio<T>, elem?: BasicAcceptedElems<AnyNode>, clone?: boolean): AnyNode[];
|
||
|
/**
|
||
|
* Insert every element in the set of matched elements to the end of the target.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('<li class="plum">Plum</li>').appendTo('#fruits');
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="pear">Pear</li>
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @param target - Element to append elements to.
|
||
|
* @returns The instance itself.
|
||
|
* @see {@link https://api.jquery.com/appendTo/}
|
||
|
*/
|
||
|
export declare function appendTo<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
|
||
|
/**
|
||
|
* Insert every element in the set of matched elements to the beginning of the target.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('<li class="plum">Plum</li>').prependTo('#fruits');
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="pear">Pear</li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @param target - Element to prepend elements to.
|
||
|
* @returns The instance itself.
|
||
|
* @see {@link https://api.jquery.com/prependTo/}
|
||
|
*/
|
||
|
export declare function prependTo<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
|
||
|
/**
|
||
|
* Inserts content as the _last_ child of each of the selected elements.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('ul').append('<li class="plum">Plum</li>');
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="pear">Pear</li>
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @see {@link https://api.jquery.com/append/}
|
||
|
*/
|
||
|
export declare const append: <T extends AnyNode>(this: Cheerio<T>, ...elems: [(this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>] | BasicAcceptedElems<AnyNode>[]) => Cheerio<T>;
|
||
|
/**
|
||
|
* Inserts content as the _first_ child of each of the selected elements.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('ul').prepend('<li class="plum">Plum</li>');
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="pear">Pear</li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @see {@link https://api.jquery.com/prepend/}
|
||
|
*/
|
||
|
export declare const prepend: <T extends AnyNode>(this: Cheerio<T>, ...elems: [(this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>] | BasicAcceptedElems<AnyNode>[]) => Cheerio<T>;
|
||
|
/**
|
||
|
* The .wrap() function can take any string or object that could be passed to
|
||
|
* the $() factory function to specify a DOM structure. This structure may be
|
||
|
* nested several levels deep, but should contain only one inmost element. A
|
||
|
* copy of this structure will be wrapped around each of the elements in the set
|
||
|
* of matched elements. This method returns the original set of elements for
|
||
|
* chaining purposes.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* const redFruit = $('<div class="red-fruit"></div>');
|
||
|
* $('.apple').wrap(redFruit);
|
||
|
*
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <div class="red-fruit">
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // </div>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // </ul>
|
||
|
*
|
||
|
* const healthy = $('<div class="healthy"></div>');
|
||
|
* $('li').wrap(healthy);
|
||
|
*
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <div class="healthy">
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // </div>
|
||
|
* // <div class="healthy">
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // </div>
|
||
|
* // <div class="healthy">
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // </div>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @param wrapper - The DOM structure to wrap around each element in the selection.
|
||
|
* @see {@link https://api.jquery.com/wrap/}
|
||
|
*/
|
||
|
export declare const wrap: <T extends AnyNode>(this: Cheerio<T>, wrapper: AcceptedElems<AnyNode>) => Cheerio<T>;
|
||
|
/**
|
||
|
* The .wrapInner() function can take any string or object that could be passed
|
||
|
* to the $() factory function to specify a DOM structure. This structure may be
|
||
|
* nested several levels deep, but should contain only one inmost element. The
|
||
|
* structure will be wrapped around the content of each of the elements in the
|
||
|
* set of matched elements.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* const redFruit = $('<div class="red-fruit"></div>');
|
||
|
* $('.apple').wrapInner(redFruit);
|
||
|
*
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="apple">
|
||
|
* // <div class="red-fruit">Apple</div>
|
||
|
* // </li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="pear">Pear</li>
|
||
|
* // </ul>
|
||
|
*
|
||
|
* const healthy = $('<div class="healthy"></div>');
|
||
|
* $('li').wrapInner(healthy);
|
||
|
*
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="apple">
|
||
|
* // <div class="healthy">Apple</div>
|
||
|
* // </li>
|
||
|
* // <li class="orange">
|
||
|
* // <div class="healthy">Orange</div>
|
||
|
* // </li>
|
||
|
* // <li class="pear">
|
||
|
* // <div class="healthy">Pear</div>
|
||
|
* // </li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @param wrapper - The DOM structure to wrap around the content of each element
|
||
|
* in the selection.
|
||
|
* @returns The instance itself, for chaining.
|
||
|
* @see {@link https://api.jquery.com/wrapInner/}
|
||
|
*/
|
||
|
export declare const wrapInner: <T extends AnyNode>(this: Cheerio<T>, wrapper: AcceptedElems<AnyNode>) => Cheerio<T>;
|
||
|
/**
|
||
|
* The .unwrap() function, removes the parents of the set of matched elements
|
||
|
* from the DOM, leaving the matched elements in their place.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example <caption>without selector</caption>
|
||
|
*
|
||
|
* ```js
|
||
|
* const $ = cheerio.load(
|
||
|
* '<div id=test>\n <div><p>Hello</p></div>\n <div><p>World</p></div>\n</div>'
|
||
|
* );
|
||
|
* $('#test p').unwrap();
|
||
|
*
|
||
|
* //=> <div id=test>
|
||
|
* // <p>Hello</p>
|
||
|
* // <p>World</p>
|
||
|
* // </div>
|
||
|
* ```
|
||
|
*
|
||
|
* @example <caption>with selector</caption>
|
||
|
*
|
||
|
* ```js
|
||
|
* const $ = cheerio.load(
|
||
|
* '<div id=test>\n <p>Hello</p>\n <b><p>World</p></b>\n</div>'
|
||
|
* );
|
||
|
* $('#test p').unwrap('b');
|
||
|
*
|
||
|
* //=> <div id=test>
|
||
|
* // <p>Hello</p>
|
||
|
* // <p>World</p>
|
||
|
* // </div>
|
||
|
* ```
|
||
|
*
|
||
|
* @param selector - A selector to check the parent element against. If an
|
||
|
* element's parent does not match the selector, the element won't be unwrapped.
|
||
|
* @returns The instance itself, for chaining.
|
||
|
* @see {@link https://api.jquery.com/unwrap/}
|
||
|
*/
|
||
|
export declare function unwrap<T extends AnyNode>(this: Cheerio<T>, selector?: string): Cheerio<T>;
|
||
|
/**
|
||
|
* The .wrapAll() function can take any string or object that could be passed to
|
||
|
* the $() function to specify a DOM structure. This structure may be nested
|
||
|
* several levels deep, but should contain only one inmost element. The
|
||
|
* structure will be wrapped around all of the elements in the set of matched
|
||
|
* elements, as a single group.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example <caption>With markup passed to `wrapAll`</caption>
|
||
|
*
|
||
|
* ```js
|
||
|
* const $ = cheerio.load(
|
||
|
* '<div class="container"><div class="inner">First</div><div class="inner">Second</div></div>'
|
||
|
* );
|
||
|
* $('.inner').wrapAll("<div class='new'></div>");
|
||
|
*
|
||
|
* //=> <div class="container">
|
||
|
* // <div class='new'>
|
||
|
* // <div class="inner">First</div>
|
||
|
* // <div class="inner">Second</div>
|
||
|
* // </div>
|
||
|
* // </div>
|
||
|
* ```
|
||
|
*
|
||
|
* @example <caption>With an existing cheerio instance</caption>
|
||
|
*
|
||
|
* ```js
|
||
|
* const $ = cheerio.load(
|
||
|
* '<span>Span 1</span><strong>Strong</strong><span>Span 2</span>'
|
||
|
* );
|
||
|
* const wrap = $('<div><p><em><b></b></em></p></div>');
|
||
|
* $('span').wrapAll(wrap);
|
||
|
*
|
||
|
* //=> <div>
|
||
|
* // <p>
|
||
|
* // <em>
|
||
|
* // <b>
|
||
|
* // <span>Span 1</span>
|
||
|
* // <span>Span 2</span>
|
||
|
* // </b>
|
||
|
* // </em>
|
||
|
* // </p>
|
||
|
* // </div>
|
||
|
* // <strong>Strong</strong>
|
||
|
* ```
|
||
|
*
|
||
|
* @param wrapper - The DOM structure to wrap around all matched elements in the
|
||
|
* selection.
|
||
|
* @returns The instance itself.
|
||
|
* @see {@link https://api.jquery.com/wrapAll/}
|
||
|
*/
|
||
|
export declare function wrapAll<T extends AnyNode>(this: Cheerio<T>, wrapper: AcceptedElems<T>): Cheerio<T>;
|
||
|
/**
|
||
|
* Insert content next to each element in the set of matched elements.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('.apple').after('<li class="plum">Plum</li>');
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="pear">Pear</li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @param content - HTML string, DOM element, array of DOM elements or Cheerio
|
||
|
* to insert after each element in the set of matched elements.
|
||
|
* @returns The instance itself.
|
||
|
* @see {@link https://api.jquery.com/after/}
|
||
|
*/
|
||
|
export declare function after<T extends AnyNode>(this: Cheerio<T>, ...elems: [(this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>] | BasicAcceptedElems<AnyNode>[]): Cheerio<T>;
|
||
|
/**
|
||
|
* Insert every element in the set of matched elements after the target.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('<li class="plum">Plum</li>').insertAfter('.apple');
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="pear">Pear</li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @param target - Element to insert elements after.
|
||
|
* @returns The set of newly inserted elements.
|
||
|
* @see {@link https://api.jquery.com/insertAfter/}
|
||
|
*/
|
||
|
export declare function insertAfter<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
|
||
|
/**
|
||
|
* Insert content previous to each element in the set of matched elements.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('.apple').before('<li class="plum">Plum</li>');
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="pear">Pear</li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @param content - HTML string, DOM element, array of DOM elements or Cheerio
|
||
|
* to insert before each element in the set of matched elements.
|
||
|
* @returns The instance itself.
|
||
|
* @see {@link https://api.jquery.com/before/}
|
||
|
*/
|
||
|
export declare function before<T extends AnyNode>(this: Cheerio<T>, ...elems: [(this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>] | BasicAcceptedElems<AnyNode>[]): Cheerio<T>;
|
||
|
/**
|
||
|
* Insert every element in the set of matched elements before the target.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('<li class="plum">Plum</li>').insertBefore('.apple');
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="pear">Pear</li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @param target - Element to insert elements before.
|
||
|
* @returns The set of newly inserted elements.
|
||
|
* @see {@link https://api.jquery.com/insertBefore/}
|
||
|
*/
|
||
|
export declare function insertBefore<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
|
||
|
/**
|
||
|
* Removes the set of matched elements from the DOM and all their children.
|
||
|
* `selector` filters the set of matched elements to be removed.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('.pear').remove();
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @param selector - Optional selector for elements to remove.
|
||
|
* @returns The instance itself.
|
||
|
* @see {@link https://api.jquery.com/remove/}
|
||
|
*/
|
||
|
export declare function remove<T extends AnyNode>(this: Cheerio<T>, selector?: string): Cheerio<T>;
|
||
|
/**
|
||
|
* Replaces matched elements with `content`.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* const plum = $('<li class="plum">Plum</li>');
|
||
|
* $('.pear').replaceWith(plum);
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits">
|
||
|
* // <li class="apple">Apple</li>
|
||
|
* // <li class="orange">Orange</li>
|
||
|
* // <li class="plum">Plum</li>
|
||
|
* // </ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @param content - Replacement for matched elements.
|
||
|
* @returns The instance itself.
|
||
|
* @see {@link https://api.jquery.com/replaceWith/}
|
||
|
*/
|
||
|
export declare function replaceWith<T extends AnyNode>(this: Cheerio<T>, content: AcceptedElems<AnyNode>): Cheerio<T>;
|
||
|
/**
|
||
|
* Empties an element, removing all its children.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('ul').empty();
|
||
|
* $.html();
|
||
|
* //=> <ul id="fruits"></ul>
|
||
|
* ```
|
||
|
*
|
||
|
* @returns The instance itself.
|
||
|
* @see {@link https://api.jquery.com/empty/}
|
||
|
*/
|
||
|
export declare function empty<T extends AnyNode>(this: Cheerio<T>): Cheerio<T>;
|
||
|
/**
|
||
|
* Gets an HTML content string from the first selected element.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('.orange').html();
|
||
|
* //=> Orange
|
||
|
*
|
||
|
* $('#fruits').html('<li class="mango">Mango</li>').html();
|
||
|
* //=> <li class="mango">Mango</li>
|
||
|
* ```
|
||
|
*
|
||
|
* @returns The HTML content string.
|
||
|
* @see {@link https://api.jquery.com/html/}
|
||
|
*/
|
||
|
export declare function html<T extends AnyNode>(this: Cheerio<T>): string | null;
|
||
|
/**
|
||
|
* Replaces each selected element's content with the specified content.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('.orange').html('<li class="mango">Mango</li>').html();
|
||
|
* //=> <li class="mango">Mango</li>
|
||
|
* ```
|
||
|
*
|
||
|
* @param str - The content to replace selection's contents with.
|
||
|
* @returns The instance itself.
|
||
|
* @see {@link https://api.jquery.com/html/}
|
||
|
*/
|
||
|
export declare function html<T extends AnyNode>(this: Cheerio<T>, str: string | Cheerio<T>): Cheerio<T>;
|
||
|
/**
|
||
|
* Turns the collection to a string. Alias for `.html()`.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @returns The rendered document.
|
||
|
*/
|
||
|
export declare function toString<T extends AnyNode>(this: Cheerio<T>): string;
|
||
|
/**
|
||
|
* Get the combined text contents of each element in the set of matched
|
||
|
* elements, including their descendants.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('.orange').text();
|
||
|
* //=> Orange
|
||
|
*
|
||
|
* $('ul').text();
|
||
|
* //=> Apple
|
||
|
* // Orange
|
||
|
* // Pear
|
||
|
* ```
|
||
|
*
|
||
|
* @returns The text contents of the collection.
|
||
|
* @see {@link https://api.jquery.com/text/}
|
||
|
*/
|
||
|
export declare function text<T extends AnyNode>(this: Cheerio<T>): string;
|
||
|
/**
|
||
|
* Set the content of each element in the set of matched elements to the specified text.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* $('.orange').text('Orange');
|
||
|
* //=> <div class="orange">Orange</div>
|
||
|
* ```
|
||
|
*
|
||
|
* @param str - The text to set as the content of each matched element.
|
||
|
* @returns The instance itself.
|
||
|
* @see {@link https://api.jquery.com/text/}
|
||
|
*/
|
||
|
export declare function text<T extends AnyNode>(this: Cheerio<T>, str: string | ((this: AnyNode, i: number, text: string) => string)): Cheerio<T>;
|
||
|
/**
|
||
|
* Clone the cheerio object.
|
||
|
*
|
||
|
* @category Manipulation
|
||
|
* @example
|
||
|
*
|
||
|
* ```js
|
||
|
* const moreFruit = $('#fruits').clone();
|
||
|
* ```
|
||
|
*
|
||
|
* @returns The cloned object.
|
||
|
* @see {@link https://api.jquery.com/clone/}
|
||
|
*/
|
||
|
export declare function clone<T extends AnyNode>(this: Cheerio<T>): Cheerio<T>;
|
||
|
//# sourceMappingURL=manipulation.d.ts.map
|