manipulation.d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /**
  2. * Methods for modifying the DOM structure.
  3. *
  4. * @module cheerio/manipulation
  5. */
  6. import { type AnyNode } from 'domhandler';
  7. import type { Cheerio } from '../cheerio.js';
  8. import type { BasicAcceptedElems, AcceptedElems } from '../types.js';
  9. /**
  10. * Create an array of nodes, recursing into arrays and parsing strings if
  11. * necessary.
  12. *
  13. * @private
  14. * @category Manipulation
  15. * @param elem - Elements to make an array of.
  16. * @param clone - Optionally clone nodes.
  17. * @returns The array of nodes.
  18. */
  19. export declare function _makeDomArray<T extends AnyNode>(this: Cheerio<T>, elem?: BasicAcceptedElems<AnyNode> | BasicAcceptedElems<AnyNode>[], clone?: boolean): AnyNode[];
  20. /**
  21. * Insert every element in the set of matched elements to the end of the target.
  22. *
  23. * @category Manipulation
  24. * @example
  25. *
  26. * ```js
  27. * $('<li class="plum">Plum</li>').appendTo('#fruits');
  28. * $.html();
  29. * //=> <ul id="fruits">
  30. * // <li class="apple">Apple</li>
  31. * // <li class="orange">Orange</li>
  32. * // <li class="pear">Pear</li>
  33. * // <li class="plum">Plum</li>
  34. * // </ul>
  35. * ```
  36. *
  37. * @param target - Element to append elements to.
  38. * @returns The instance itself.
  39. * @see {@link https://api.jquery.com/appendTo/}
  40. */
  41. export declare function appendTo<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
  42. /**
  43. * Insert every element in the set of matched elements to the beginning of the
  44. * target.
  45. *
  46. * @category Manipulation
  47. * @example
  48. *
  49. * ```js
  50. * $('<li class="plum">Plum</li>').prependTo('#fruits');
  51. * $.html();
  52. * //=> <ul id="fruits">
  53. * // <li class="plum">Plum</li>
  54. * // <li class="apple">Apple</li>
  55. * // <li class="orange">Orange</li>
  56. * // <li class="pear">Pear</li>
  57. * // </ul>
  58. * ```
  59. *
  60. * @param target - Element to prepend elements to.
  61. * @returns The instance itself.
  62. * @see {@link https://api.jquery.com/prependTo/}
  63. */
  64. export declare function prependTo<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
  65. /**
  66. * Inserts content as the _last_ child of each of the selected elements.
  67. *
  68. * @category Manipulation
  69. * @example
  70. *
  71. * ```js
  72. * $('ul').append('<li class="plum">Plum</li>');
  73. * $.html();
  74. * //=> <ul id="fruits">
  75. * // <li class="apple">Apple</li>
  76. * // <li class="orange">Orange</li>
  77. * // <li class="pear">Pear</li>
  78. * // <li class="plum">Plum</li>
  79. * // </ul>
  80. * ```
  81. *
  82. * @see {@link https://api.jquery.com/append/}
  83. */
  84. export declare const append: <T extends AnyNode>(this: Cheerio<T>, ...elems: [(this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>] | BasicAcceptedElems<AnyNode>[]) => Cheerio<T>;
  85. /**
  86. * Inserts content as the _first_ child of each of the selected elements.
  87. *
  88. * @category Manipulation
  89. * @example
  90. *
  91. * ```js
  92. * $('ul').prepend('<li class="plum">Plum</li>');
  93. * $.html();
  94. * //=> <ul id="fruits">
  95. * // <li class="plum">Plum</li>
  96. * // <li class="apple">Apple</li>
  97. * // <li class="orange">Orange</li>
  98. * // <li class="pear">Pear</li>
  99. * // </ul>
  100. * ```
  101. *
  102. * @see {@link https://api.jquery.com/prepend/}
  103. */
  104. export declare const prepend: <T extends AnyNode>(this: Cheerio<T>, ...elems: [(this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>] | BasicAcceptedElems<AnyNode>[]) => Cheerio<T>;
  105. /**
  106. * The .wrap() function can take any string or object that could be passed to
  107. * the $() factory function to specify a DOM structure. This structure may be
  108. * nested several levels deep, but should contain only one inmost element. A
  109. * copy of this structure will be wrapped around each of the elements in the set
  110. * of matched elements. This method returns the original set of elements for
  111. * chaining purposes.
  112. *
  113. * @category Manipulation
  114. * @example
  115. *
  116. * ```js
  117. * const redFruit = $('<div class="red-fruit"></div>');
  118. * $('.apple').wrap(redFruit);
  119. *
  120. * //=> <ul id="fruits">
  121. * // <div class="red-fruit">
  122. * // <li class="apple">Apple</li>
  123. * // </div>
  124. * // <li class="orange">Orange</li>
  125. * // <li class="plum">Plum</li>
  126. * // </ul>
  127. *
  128. * const healthy = $('<div class="healthy"></div>');
  129. * $('li').wrap(healthy);
  130. *
  131. * //=> <ul id="fruits">
  132. * // <div class="healthy">
  133. * // <li class="apple">Apple</li>
  134. * // </div>
  135. * // <div class="healthy">
  136. * // <li class="orange">Orange</li>
  137. * // </div>
  138. * // <div class="healthy">
  139. * // <li class="plum">Plum</li>
  140. * // </div>
  141. * // </ul>
  142. * ```
  143. *
  144. * @param wrapper - The DOM structure to wrap around each element in the
  145. * selection.
  146. * @see {@link https://api.jquery.com/wrap/}
  147. */
  148. export declare const wrap: <T extends AnyNode>(this: Cheerio<T>, wrapper: AcceptedElems<AnyNode>) => Cheerio<T>;
  149. /**
  150. * The .wrapInner() function can take any string or object that could be passed
  151. * to the $() factory function to specify a DOM structure. This structure may be
  152. * nested several levels deep, but should contain only one inmost element. The
  153. * structure will be wrapped around the content of each of the elements in the
  154. * set of matched elements.
  155. *
  156. * @category Manipulation
  157. * @example
  158. *
  159. * ```js
  160. * const redFruit = $('<div class="red-fruit"></div>');
  161. * $('.apple').wrapInner(redFruit);
  162. *
  163. * //=> <ul id="fruits">
  164. * // <li class="apple">
  165. * // <div class="red-fruit">Apple</div>
  166. * // </li>
  167. * // <li class="orange">Orange</li>
  168. * // <li class="pear">Pear</li>
  169. * // </ul>
  170. *
  171. * const healthy = $('<div class="healthy"></div>');
  172. * $('li').wrapInner(healthy);
  173. *
  174. * //=> <ul id="fruits">
  175. * // <li class="apple">
  176. * // <div class="healthy">Apple</div>
  177. * // </li>
  178. * // <li class="orange">
  179. * // <div class="healthy">Orange</div>
  180. * // </li>
  181. * // <li class="pear">
  182. * // <div class="healthy">Pear</div>
  183. * // </li>
  184. * // </ul>
  185. * ```
  186. *
  187. * @param wrapper - The DOM structure to wrap around the content of each element
  188. * in the selection.
  189. * @returns The instance itself, for chaining.
  190. * @see {@link https://api.jquery.com/wrapInner/}
  191. */
  192. export declare const wrapInner: <T extends AnyNode>(this: Cheerio<T>, wrapper: AcceptedElems<AnyNode>) => Cheerio<T>;
  193. /**
  194. * The .unwrap() function, removes the parents of the set of matched elements
  195. * from the DOM, leaving the matched elements in their place.
  196. *
  197. * @category Manipulation
  198. * @example <caption>without selector</caption>
  199. *
  200. * ```js
  201. * const $ = cheerio.load(
  202. * '<div id=test>\n <div><p>Hello</p></div>\n <div><p>World</p></div>\n</div>',
  203. * );
  204. * $('#test p').unwrap();
  205. *
  206. * //=> <div id=test>
  207. * // <p>Hello</p>
  208. * // <p>World</p>
  209. * // </div>
  210. * ```
  211. *
  212. * @example <caption>with selector</caption>
  213. *
  214. * ```js
  215. * const $ = cheerio.load(
  216. * '<div id=test>\n <p>Hello</p>\n <b><p>World</p></b>\n</div>',
  217. * );
  218. * $('#test p').unwrap('b');
  219. *
  220. * //=> <div id=test>
  221. * // <p>Hello</p>
  222. * // <p>World</p>
  223. * // </div>
  224. * ```
  225. *
  226. * @param selector - A selector to check the parent element against. If an
  227. * element's parent does not match the selector, the element won't be
  228. * unwrapped.
  229. * @returns The instance itself, for chaining.
  230. * @see {@link https://api.jquery.com/unwrap/}
  231. */
  232. export declare function unwrap<T extends AnyNode>(this: Cheerio<T>, selector?: string): Cheerio<T>;
  233. /**
  234. * The .wrapAll() function can take any string or object that could be passed to
  235. * the $() function to specify a DOM structure. This structure may be nested
  236. * several levels deep, but should contain only one inmost element. The
  237. * structure will be wrapped around all of the elements in the set of matched
  238. * elements, as a single group.
  239. *
  240. * @category Manipulation
  241. * @example <caption>With markup passed to `wrapAll`</caption>
  242. *
  243. * ```js
  244. * const $ = cheerio.load(
  245. * '<div class="container"><div class="inner">First</div><div class="inner">Second</div></div>',
  246. * );
  247. * $('.inner').wrapAll("<div class='new'></div>");
  248. *
  249. * //=> <div class="container">
  250. * // <div class='new'>
  251. * // <div class="inner">First</div>
  252. * // <div class="inner">Second</div>
  253. * // </div>
  254. * // </div>
  255. * ```
  256. *
  257. * @example <caption>With an existing cheerio instance</caption>
  258. *
  259. * ```js
  260. * const $ = cheerio.load(
  261. * '<span>Span 1</span><strong>Strong</strong><span>Span 2</span>',
  262. * );
  263. * const wrap = $('<div><p><em><b></b></em></p></div>');
  264. * $('span').wrapAll(wrap);
  265. *
  266. * //=> <div>
  267. * // <p>
  268. * // <em>
  269. * // <b>
  270. * // <span>Span 1</span>
  271. * // <span>Span 2</span>
  272. * // </b>
  273. * // </em>
  274. * // </p>
  275. * // </div>
  276. * // <strong>Strong</strong>
  277. * ```
  278. *
  279. * @param wrapper - The DOM structure to wrap around all matched elements in the
  280. * selection.
  281. * @returns The instance itself.
  282. * @see {@link https://api.jquery.com/wrapAll/}
  283. */
  284. export declare function wrapAll<T extends AnyNode>(this: Cheerio<T>, wrapper: AcceptedElems<T>): Cheerio<T>;
  285. /**
  286. * Insert content next to each element in the set of matched elements.
  287. *
  288. * @category Manipulation
  289. * @example
  290. *
  291. * ```js
  292. * $('.apple').after('<li class="plum">Plum</li>');
  293. * $.html();
  294. * //=> <ul id="fruits">
  295. * // <li class="apple">Apple</li>
  296. * // <li class="plum">Plum</li>
  297. * // <li class="orange">Orange</li>
  298. * // <li class="pear">Pear</li>
  299. * // </ul>
  300. * ```
  301. *
  302. * @param elems - HTML string, DOM element, array of DOM elements or Cheerio to
  303. * insert after each element in the set of matched elements.
  304. * @returns The instance itself.
  305. * @see {@link https://api.jquery.com/after/}
  306. */
  307. export declare function after<T extends AnyNode>(this: Cheerio<T>, ...elems: [(this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>] | BasicAcceptedElems<AnyNode>[]): Cheerio<T>;
  308. /**
  309. * Insert every element in the set of matched elements after the target.
  310. *
  311. * @category Manipulation
  312. * @example
  313. *
  314. * ```js
  315. * $('<li class="plum">Plum</li>').insertAfter('.apple');
  316. * $.html();
  317. * //=> <ul id="fruits">
  318. * // <li class="apple">Apple</li>
  319. * // <li class="plum">Plum</li>
  320. * // <li class="orange">Orange</li>
  321. * // <li class="pear">Pear</li>
  322. * // </ul>
  323. * ```
  324. *
  325. * @param target - Element to insert elements after.
  326. * @returns The set of newly inserted elements.
  327. * @see {@link https://api.jquery.com/insertAfter/}
  328. */
  329. export declare function insertAfter<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
  330. /**
  331. * Insert content previous to each element in the set of matched elements.
  332. *
  333. * @category Manipulation
  334. * @example
  335. *
  336. * ```js
  337. * $('.apple').before('<li class="plum">Plum</li>');
  338. * $.html();
  339. * //=> <ul id="fruits">
  340. * // <li class="plum">Plum</li>
  341. * // <li class="apple">Apple</li>
  342. * // <li class="orange">Orange</li>
  343. * // <li class="pear">Pear</li>
  344. * // </ul>
  345. * ```
  346. *
  347. * @param elems - HTML string, DOM element, array of DOM elements or Cheerio to
  348. * insert before each element in the set of matched elements.
  349. * @returns The instance itself.
  350. * @see {@link https://api.jquery.com/before/}
  351. */
  352. export declare function before<T extends AnyNode>(this: Cheerio<T>, ...elems: [(this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>] | BasicAcceptedElems<AnyNode>[]): Cheerio<T>;
  353. /**
  354. * Insert every element in the set of matched elements before the target.
  355. *
  356. * @category Manipulation
  357. * @example
  358. *
  359. * ```js
  360. * $('<li class="plum">Plum</li>').insertBefore('.apple');
  361. * $.html();
  362. * //=> <ul id="fruits">
  363. * // <li class="plum">Plum</li>
  364. * // <li class="apple">Apple</li>
  365. * // <li class="orange">Orange</li>
  366. * // <li class="pear">Pear</li>
  367. * // </ul>
  368. * ```
  369. *
  370. * @param target - Element to insert elements before.
  371. * @returns The set of newly inserted elements.
  372. * @see {@link https://api.jquery.com/insertBefore/}
  373. */
  374. export declare function insertBefore<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
  375. /**
  376. * Removes the set of matched elements from the DOM and all their children.
  377. * `selector` filters the set of matched elements to be removed.
  378. *
  379. * @category Manipulation
  380. * @example
  381. *
  382. * ```js
  383. * $('.pear').remove();
  384. * $.html();
  385. * //=> <ul id="fruits">
  386. * // <li class="apple">Apple</li>
  387. * // <li class="orange">Orange</li>
  388. * // </ul>
  389. * ```
  390. *
  391. * @param selector - Optional selector for elements to remove.
  392. * @returns The instance itself.
  393. * @see {@link https://api.jquery.com/remove/}
  394. */
  395. export declare function remove<T extends AnyNode>(this: Cheerio<T>, selector?: string): Cheerio<T>;
  396. /**
  397. * Replaces matched elements with `content`.
  398. *
  399. * @category Manipulation
  400. * @example
  401. *
  402. * ```js
  403. * const plum = $('<li class="plum">Plum</li>');
  404. * $('.pear').replaceWith(plum);
  405. * $.html();
  406. * //=> <ul id="fruits">
  407. * // <li class="apple">Apple</li>
  408. * // <li class="orange">Orange</li>
  409. * // <li class="plum">Plum</li>
  410. * // </ul>
  411. * ```
  412. *
  413. * @param content - Replacement for matched elements.
  414. * @returns The instance itself.
  415. * @see {@link https://api.jquery.com/replaceWith/}
  416. */
  417. export declare function replaceWith<T extends AnyNode>(this: Cheerio<T>, content: AcceptedElems<AnyNode>): Cheerio<T>;
  418. /**
  419. * Removes all children from each item in the selection. Text nodes and comment
  420. * nodes are left as is.
  421. *
  422. * @category Manipulation
  423. * @example
  424. *
  425. * ```js
  426. * $('ul').empty();
  427. * $.html();
  428. * //=> <ul id="fruits"></ul>
  429. * ```
  430. *
  431. * @returns The instance itself.
  432. * @see {@link https://api.jquery.com/empty/}
  433. */
  434. export declare function empty<T extends AnyNode>(this: Cheerio<T>): Cheerio<T>;
  435. /**
  436. * Gets an HTML content string from the first selected element.
  437. *
  438. * @category Manipulation
  439. * @example
  440. *
  441. * ```js
  442. * $('.orange').html();
  443. * //=> Orange
  444. *
  445. * $('#fruits').html('<li class="mango">Mango</li>').html();
  446. * //=> <li class="mango">Mango</li>
  447. * ```
  448. *
  449. * @returns The HTML content string.
  450. * @see {@link https://api.jquery.com/html/}
  451. */
  452. export declare function html<T extends AnyNode>(this: Cheerio<T>): string | null;
  453. /**
  454. * Replaces each selected element's content with the specified content.
  455. *
  456. * @category Manipulation
  457. * @example
  458. *
  459. * ```js
  460. * $('.orange').html('<li class="mango">Mango</li>').html();
  461. * //=> <li class="mango">Mango</li>
  462. * ```
  463. *
  464. * @param str - The content to replace selection's contents with.
  465. * @returns The instance itself.
  466. * @see {@link https://api.jquery.com/html/}
  467. */
  468. export declare function html<T extends AnyNode>(this: Cheerio<T>, str: string | Cheerio<T>): Cheerio<T>;
  469. /**
  470. * Turns the collection to a string. Alias for `.html()`.
  471. *
  472. * @category Manipulation
  473. * @returns The rendered document.
  474. */
  475. export declare function toString<T extends AnyNode>(this: Cheerio<T>): string;
  476. /**
  477. * Get the combined text contents of each element in the set of matched
  478. * elements, including their descendants.
  479. *
  480. * @category Manipulation
  481. * @example
  482. *
  483. * ```js
  484. * $('.orange').text();
  485. * //=> Orange
  486. *
  487. * $('ul').text();
  488. * //=> Apple
  489. * // Orange
  490. * // Pear
  491. * ```
  492. *
  493. * @returns The text contents of the collection.
  494. * @see {@link https://api.jquery.com/text/}
  495. */
  496. export declare function text<T extends AnyNode>(this: Cheerio<T>): string;
  497. /**
  498. * Set the content of each element in the set of matched elements to the
  499. * specified text.
  500. *
  501. * @category Manipulation
  502. * @example
  503. *
  504. * ```js
  505. * $('.orange').text('Orange');
  506. * //=> <div class="orange">Orange</div>
  507. * ```
  508. *
  509. * @param str - The text to set as the content of each matched element.
  510. * @returns The instance itself.
  511. * @see {@link https://api.jquery.com/text/}
  512. */
  513. export declare function text<T extends AnyNode>(this: Cheerio<T>, str: string | ((this: AnyNode, i: number, text: string) => string)): Cheerio<T>;
  514. /**
  515. * Clone the cheerio object.
  516. *
  517. * @category Manipulation
  518. * @example
  519. *
  520. * ```js
  521. * const moreFruit = $('#fruits').clone();
  522. * ```
  523. *
  524. * @returns The cloned object.
  525. * @see {@link https://api.jquery.com/clone/}
  526. */
  527. export declare function clone<T extends AnyNode>(this: Cheerio<T>): Cheerio<T>;
  528. //# sourceMappingURL=manipulation.d.ts.map