manipulation.d.ts 15 KB

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