traversing.d.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /**
  2. * Methods for traversing the DOM structure.
  3. *
  4. * @module cheerio/traversing
  5. */
  6. import { Node, Element, Document } from 'domhandler';
  7. import type { Cheerio } from '../cheerio';
  8. import type { AcceptedFilters } from '../types';
  9. /**
  10. * Get the descendants of each element in the current set of matched elements,
  11. * filtered by a selector, jQuery object, or element.
  12. *
  13. * @category Traversing
  14. * @example
  15. *
  16. * ```js
  17. * $('#fruits').find('li').length;
  18. * //=> 3
  19. * $('#fruits').find($('.apple')).length;
  20. * //=> 1
  21. * ```
  22. *
  23. * @param selectorOrHaystack - Element to look for.
  24. * @returns The found elements.
  25. * @see {@link https://api.jquery.com/find/}
  26. */
  27. export declare function find<T extends Node>(this: Cheerio<T>, selectorOrHaystack?: string | Cheerio<Element> | Element): Cheerio<Element>;
  28. /**
  29. * Get the parent of each element in the current set of matched elements,
  30. * optionally filtered by a selector.
  31. *
  32. * @category Traversing
  33. * @example
  34. *
  35. * ```js
  36. * $('.pear').parent().attr('id');
  37. * //=> fruits
  38. * ```
  39. *
  40. * @param selector - If specified filter for parent.
  41. * @returns The parents.
  42. * @see {@link https://api.jquery.com/parent/}
  43. */
  44. export declare const parent: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  45. /**
  46. * Get a set of parents filtered by `selector` of each element in the current
  47. * set of match elements.
  48. *
  49. * @category Traversing
  50. * @example
  51. *
  52. * ```js
  53. * $('.orange').parents().length;
  54. * //=> 2
  55. * $('.orange').parents('#fruits').length;
  56. * //=> 1
  57. * ```
  58. *
  59. * @param selector - If specified filter for parents.
  60. * @returns The parents.
  61. * @see {@link https://api.jquery.com/parents/}
  62. */
  63. export declare const parents: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  64. /**
  65. * Get the ancestors of each element in the current set of matched elements, up
  66. * to but not including the element matched by the selector, DOM node, or cheerio object.
  67. *
  68. * @category Traversing
  69. * @example
  70. *
  71. * ```js
  72. * $('.orange').parentsUntil('#food').length;
  73. * //=> 1
  74. * ```
  75. *
  76. * @param selector - Selector for element to stop at.
  77. * @param filterSelector - Optional filter for parents.
  78. * @returns The parents.
  79. * @see {@link https://api.jquery.com/parentsUntil/}
  80. */
  81. export declare const parentsUntil: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | null | undefined, filterSelector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  82. /**
  83. * For each element in the set, get the first element that matches the selector
  84. * by testing the element itself and traversing up through its ancestors in the DOM tree.
  85. *
  86. * @category Traversing
  87. * @example
  88. *
  89. * ```js
  90. * $('.orange').closest();
  91. * //=> []
  92. *
  93. * $('.orange').closest('.apple');
  94. * // => []
  95. *
  96. * $('.orange').closest('li');
  97. * //=> [<li class="orange">Orange</li>]
  98. *
  99. * $('.orange').closest('#fruits');
  100. * //=> [<ul id="fruits"> ... </ul>]
  101. * ```
  102. *
  103. * @param selector - Selector for the element to find.
  104. * @returns The closest nodes.
  105. * @see {@link https://api.jquery.com/closest/}
  106. */
  107. export declare function closest<T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Node>): Cheerio<Node>;
  108. /**
  109. * Gets the next sibling of the first selected element, optionally filtered by a selector.
  110. *
  111. * @category Traversing
  112. * @example
  113. *
  114. * ```js
  115. * $('.apple').next().hasClass('orange');
  116. * //=> true
  117. * ```
  118. *
  119. * @param selector - If specified filter for sibling.
  120. * @returns The next nodes.
  121. * @see {@link https://api.jquery.com/next/}
  122. */
  123. export declare const next: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  124. /**
  125. * Gets all the following siblings of the first selected element, optionally
  126. * filtered by a selector.
  127. *
  128. * @category Traversing
  129. * @example
  130. *
  131. * ```js
  132. * $('.apple').nextAll();
  133. * //=> [<li class="orange">Orange</li>, <li class="pear">Pear</li>]
  134. * $('.apple').nextAll('.orange');
  135. * //=> [<li class="orange">Orange</li>]
  136. * ```
  137. *
  138. * @param selector - If specified filter for siblings.
  139. * @returns The next nodes.
  140. * @see {@link https://api.jquery.com/nextAll/}
  141. */
  142. export declare const nextAll: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  143. /**
  144. * Gets all the following siblings up to but not including the element matched
  145. * by the selector, optionally filtered by another selector.
  146. *
  147. * @category Traversing
  148. * @example
  149. *
  150. * ```js
  151. * $('.apple').nextUntil('.pear');
  152. * //=> [<li class="orange">Orange</li>]
  153. * ```
  154. *
  155. * @param selector - Selector for element to stop at.
  156. * @param filterSelector - If specified filter for siblings.
  157. * @returns The next nodes.
  158. * @see {@link https://api.jquery.com/nextUntil/}
  159. */
  160. export declare const nextUntil: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | null | undefined, filterSelector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  161. /**
  162. * Gets the previous sibling of the first selected element optionally filtered
  163. * by a selector.
  164. *
  165. * @category Traversing
  166. * @example
  167. *
  168. * ```js
  169. * $('.orange').prev().hasClass('apple');
  170. * //=> true
  171. * ```
  172. *
  173. * @param selector - If specified filter for siblings.
  174. * @returns The previous nodes.
  175. * @see {@link https://api.jquery.com/prev/}
  176. */
  177. export declare const prev: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  178. /**
  179. * Gets all the preceding siblings of the first selected element, optionally
  180. * filtered by a selector.
  181. *
  182. * @category Traversing
  183. * @example
  184. *
  185. * ```js
  186. * $('.pear').prevAll();
  187. * //=> [<li class="orange">Orange</li>, <li class="apple">Apple</li>]
  188. *
  189. * $('.pear').prevAll('.orange');
  190. * //=> [<li class="orange">Orange</li>]
  191. * ```
  192. *
  193. * @param selector - If specified filter for siblings.
  194. * @returns The previous nodes.
  195. * @see {@link https://api.jquery.com/prevAll/}
  196. */
  197. export declare const prevAll: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  198. /**
  199. * Gets all the preceding siblings up to but not including the element matched
  200. * by the selector, optionally filtered by another selector.
  201. *
  202. * @category Traversing
  203. * @example
  204. *
  205. * ```js
  206. * $('.pear').prevUntil('.apple');
  207. * //=> [<li class="orange">Orange</li>]
  208. * ```
  209. *
  210. * @param selector - Selector for element to stop at.
  211. * @param filterSelector - If specified filter for siblings.
  212. * @returns The previous nodes.
  213. * @see {@link https://api.jquery.com/prevUntil/}
  214. */
  215. export declare const prevUntil: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | null | undefined, filterSelector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  216. /**
  217. * Get the siblings of each element (excluding the element) in the set of
  218. * matched elements, optionally filtered by a selector.
  219. *
  220. * @category Traversing
  221. * @example
  222. *
  223. * ```js
  224. * $('.pear').siblings().length;
  225. * //=> 2
  226. *
  227. * $('.pear').siblings('.orange').length;
  228. * //=> 1
  229. * ```
  230. *
  231. * @param selector - If specified filter for siblings.
  232. * @returns The siblings.
  233. * @see {@link https://api.jquery.com/siblings/}
  234. */
  235. export declare const siblings: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  236. /**
  237. * Gets the children of the first selected element.
  238. *
  239. * @category Traversing
  240. * @example
  241. *
  242. * ```js
  243. * $('#fruits').children().length;
  244. * //=> 3
  245. *
  246. * $('#fruits').children('.pear').text();
  247. * //=> Pear
  248. * ```
  249. *
  250. * @param selector - If specified filter for children.
  251. * @returns The children.
  252. * @see {@link https://api.jquery.com/children/}
  253. */
  254. export declare const children: <T extends Node>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | undefined) => Cheerio<Element>;
  255. /**
  256. * Gets the children of each element in the set of matched elements, including
  257. * text and comment nodes.
  258. *
  259. * @category Traversing
  260. * @example
  261. *
  262. * ```js
  263. * $('#fruits').contents().length;
  264. * //=> 3
  265. * ```
  266. *
  267. * @returns The children.
  268. * @see {@link https://api.jquery.com/contents/}
  269. */
  270. export declare function contents<T extends Node>(this: Cheerio<T>): Cheerio<Node>;
  271. /**
  272. * Iterates over a cheerio object, executing a function for each matched
  273. * element. When the callback is fired, the function is fired in the context of
  274. * the DOM element, so `this` refers to the current element, which is equivalent
  275. * to the function parameter `element`. To break out of the `each` loop early,
  276. * return with `false`.
  277. *
  278. * @category Traversing
  279. * @example
  280. *
  281. * ```js
  282. * const fruits = [];
  283. *
  284. * $('li').each(function (i, elem) {
  285. * fruits[i] = $(this).text();
  286. * });
  287. *
  288. * fruits.join(', ');
  289. * //=> Apple, Orange, Pear
  290. * ```
  291. *
  292. * @param fn - Function to execute.
  293. * @returns The instance itself, useful for chaining.
  294. * @see {@link https://api.jquery.com/each/}
  295. */
  296. export declare function each<T>(this: Cheerio<T>, fn: (this: T, i: number, el: T) => void | boolean): Cheerio<T>;
  297. /**
  298. * Pass each element in the current matched set through a function, producing a
  299. * new Cheerio object containing the return values. The function can return an
  300. * individual data item or an array of data items to be inserted into the
  301. * resulting set. If an array is returned, the elements inside the array are
  302. * inserted into the set. If the function returns null or undefined, no element
  303. * will be inserted.
  304. *
  305. * @category Traversing
  306. * @example
  307. *
  308. * ```js
  309. * $('li')
  310. * .map(function (i, el) {
  311. * // this === el
  312. * return $(this).text();
  313. * })
  314. * .toArray()
  315. * .join(' ');
  316. * //=> "apple orange pear"
  317. * ```
  318. *
  319. * @param fn - Function to execute.
  320. * @returns The mapped elements, wrapped in a Cheerio collection.
  321. * @see {@link https://api.jquery.com/map/}
  322. */
  323. export declare function map<T, M>(this: Cheerio<T>, fn: (this: T, i: number, el: T) => M[] | M | null | undefined): Cheerio<M>;
  324. /**
  325. * Iterates over a cheerio object, reducing the set of selector elements to
  326. * those that match the selector or pass the function's test.
  327. *
  328. * This is the definition for using type guards; have a look below for other
  329. * ways to invoke this method. The function is executed in the context of the
  330. * selected element, so `this` refers to the current element.
  331. *
  332. * @category Traversing
  333. * @example <caption>Function</caption>
  334. *
  335. * ```js
  336. * $('li')
  337. * .filter(function (i, el) {
  338. * // this === el
  339. * return $(this).attr('class') === 'orange';
  340. * })
  341. * .attr('class'); //=> orange
  342. * ```
  343. *
  344. * @param match - Value to look for, following the rules above.
  345. * @returns The filtered collection.
  346. * @see {@link https://api.jquery.com/filter/}
  347. */
  348. export declare function filter<T, S extends T>(this: Cheerio<T>, match: (this: T, index: number, value: T) => value is S): Cheerio<S>;
  349. /**
  350. * Iterates over a cheerio object, reducing the set of selector elements to
  351. * those that match the selector or pass the function's test.
  352. *
  353. * - When a Cheerio selection is specified, return only the elements contained in
  354. * that selection.
  355. * - When an element is specified, return only that element (if it is contained in
  356. * the original selection).
  357. * - If using the function method, the function is executed in the context of the
  358. * selected element, so `this` refers to the current element.
  359. *
  360. * @category Traversing
  361. * @example <caption>Selector</caption>
  362. *
  363. * ```js
  364. * $('li').filter('.orange').attr('class');
  365. * //=> orange
  366. * ```
  367. *
  368. * @example <caption>Function</caption>
  369. *
  370. * ```js
  371. * $('li')
  372. * .filter(function (i, el) {
  373. * // this === el
  374. * return $(this).attr('class') === 'orange';
  375. * })
  376. * .attr('class'); //=> orange
  377. * ```
  378. *
  379. * @param match - Value to look for, following the rules above. See
  380. * {@link AcceptedFilters}.
  381. * @returns The filtered collection.
  382. * @see {@link https://api.jquery.com/filter/}
  383. */
  384. export declare function filter<T, S extends AcceptedFilters<T>>(this: Cheerio<T>, match: S): Cheerio<S extends string ? Element : T>;
  385. export declare function filterArray<T>(nodes: T[], match: AcceptedFilters<T>, xmlMode?: boolean, root?: Document): Element[] | T[];
  386. /**
  387. * Checks the current list of elements and returns `true` if *any* of the
  388. * elements match the selector. If using an element or Cheerio selection,
  389. * returns `true` if *any* of the elements match. If using a predicate function,
  390. * the function is executed in the context of the selected element, so `this`
  391. * refers to the current element.
  392. *
  393. * @category Attributes
  394. * @param selector - Selector for the selection.
  395. * @returns Whether or not the selector matches an element of the instance.
  396. * @see {@link https://api.jquery.com/is/}
  397. */
  398. export declare function is<T>(this: Cheerio<T>, selector?: AcceptedFilters<T>): boolean;
  399. /**
  400. * Remove elements from the set of matched elements. Given a Cheerio object that
  401. * represents a set of DOM elements, the `.not()` method constructs a new
  402. * Cheerio object from a subset of the matching elements. The supplied selector
  403. * is tested against each element; the elements that don't match the selector
  404. * will be included in the result.
  405. *
  406. * The `.not()` method can take a function as its argument in the same way that
  407. * `.filter()` does. Elements for which the function returns `true` are excluded
  408. * from the filtered set; all other elements are included.
  409. *
  410. * @category Traversing
  411. * @example <caption>Selector</caption>
  412. *
  413. * ```js
  414. * $('li').not('.apple').length;
  415. * //=> 2
  416. * ```
  417. *
  418. * @example <caption>Function</caption>
  419. *
  420. * ```js
  421. * $('li').not(function (i, el) {
  422. * // this === el
  423. * return $(this).attr('class') === 'orange';
  424. * }).length; //=> 2
  425. * ```
  426. *
  427. * @param match - Value to look for, following the rules above.
  428. * @param container - Optional node to filter instead.
  429. * @returns The filtered collection.
  430. * @see {@link https://api.jquery.com/not/}
  431. */
  432. export declare function not<T extends Node>(this: Cheerio<T>, match: AcceptedFilters<T>): Cheerio<T>;
  433. /**
  434. * Filters the set of matched elements to only those which have the given DOM
  435. * element as a descendant or which have a descendant that matches the given
  436. * selector. Equivalent to `.filter(':has(selector)')`.
  437. *
  438. * @category Traversing
  439. * @example <caption>Selector</caption>
  440. *
  441. * ```js
  442. * $('ul').has('.pear').attr('id');
  443. * //=> fruits
  444. * ```
  445. *
  446. * @example <caption>Element</caption>
  447. *
  448. * ```js
  449. * $('ul').has($('.pear')[0]).attr('id');
  450. * //=> fruits
  451. * ```
  452. *
  453. * @param selectorOrHaystack - Element to look for.
  454. * @returns The filtered collection.
  455. * @see {@link https://api.jquery.com/has/}
  456. */
  457. export declare function has(this: Cheerio<Node | Element>, selectorOrHaystack: string | Cheerio<Element> | Element): Cheerio<Node | Element>;
  458. /**
  459. * Will select the first element of a cheerio object.
  460. *
  461. * @category Traversing
  462. * @example
  463. *
  464. * ```js
  465. * $('#fruits').children().first().text();
  466. * //=> Apple
  467. * ```
  468. *
  469. * @returns The first element.
  470. * @see {@link https://api.jquery.com/first/}
  471. */
  472. export declare function first<T extends Node>(this: Cheerio<T>): Cheerio<T>;
  473. /**
  474. * Will select the last element of a cheerio object.
  475. *
  476. * @category Traversing
  477. * @example
  478. *
  479. * ```js
  480. * $('#fruits').children().last().text();
  481. * //=> Pear
  482. * ```
  483. *
  484. * @returns The last element.
  485. * @see {@link https://api.jquery.com/last/}
  486. */
  487. export declare function last<T>(this: Cheerio<T>): Cheerio<T>;
  488. /**
  489. * Reduce the set of matched elements to the one at the specified index. Use
  490. * `.eq(-i)` to count backwards from the last selected element.
  491. *
  492. * @category Traversing
  493. * @example
  494. *
  495. * ```js
  496. * $('li').eq(0).text();
  497. * //=> Apple
  498. *
  499. * $('li').eq(-1).text();
  500. * //=> Pear
  501. * ```
  502. *
  503. * @param i - Index of the element to select.
  504. * @returns The element at the `i`th position.
  505. * @see {@link https://api.jquery.com/eq/}
  506. */
  507. export declare function eq<T>(this: Cheerio<T>, i: number): Cheerio<T>;
  508. /**
  509. * Retrieve one of the elements matched by the Cheerio object, at the `i`th position.
  510. *
  511. * @category Traversing
  512. * @example
  513. *
  514. * ```js
  515. * $('li').get(0).tagName;
  516. * //=> li
  517. * ```
  518. *
  519. * @param i - Element to retrieve.
  520. * @returns The element at the `i`th position.
  521. * @see {@link https://api.jquery.com/get/}
  522. */
  523. export declare function get<T>(this: Cheerio<T>, i: number): T;
  524. /**
  525. * Retrieve all elements matched by the Cheerio object, as an array.
  526. *
  527. * @category Traversing
  528. * @example
  529. *
  530. * ```js
  531. * $('li').get().length;
  532. * //=> 3
  533. * ```
  534. *
  535. * @returns All elements matched by the Cheerio object.
  536. * @see {@link https://api.jquery.com/get/}
  537. */
  538. export declare function get<T>(this: Cheerio<T>): T[];
  539. /**
  540. * Retrieve all the DOM elements contained in the jQuery set as an array.
  541. *
  542. * @example
  543. *
  544. * ```js
  545. * $('li').toArray();
  546. * //=> [ {...}, {...}, {...} ]
  547. * ```
  548. *
  549. * @returns The contained items.
  550. */
  551. export declare function toArray<T>(this: Cheerio<T>): T[];
  552. /**
  553. * Search for a given element from among the matched elements.
  554. *
  555. * @category Traversing
  556. * @example
  557. *
  558. * ```js
  559. * $('.pear').index();
  560. * //=> 2 $('.orange').index('li');
  561. * //=> 1
  562. * $('.apple').index($('#fruit, li'));
  563. * //=> 1
  564. * ```
  565. *
  566. * @param selectorOrNeedle - Element to look for.
  567. * @returns The index of the element.
  568. * @see {@link https://api.jquery.com/index/}
  569. */
  570. export declare function index<T extends Node>(this: Cheerio<T>, selectorOrNeedle?: string | Cheerio<Node> | Node): number;
  571. /**
  572. * Gets the elements matching the specified range (0-based position).
  573. *
  574. * @category Traversing
  575. * @example
  576. *
  577. * ```js
  578. * $('li').slice(1).eq(0).text();
  579. * //=> 'Orange'
  580. *
  581. * $('li').slice(1, 2).length;
  582. * //=> 1
  583. * ```
  584. *
  585. * @param start - An position at which the elements begin to be selected. If
  586. * negative, it indicates an offset from the end of the set.
  587. * @param end - An position at which the elements stop being selected. If
  588. * negative, it indicates an offset from the end of the set. If omitted, the
  589. * range continues until the end of the set.
  590. * @returns The elements matching the specified range.
  591. * @see {@link https://api.jquery.com/slice/}
  592. */
  593. export declare function slice<T>(this: Cheerio<T>, start?: number, end?: number): Cheerio<T>;
  594. /**
  595. * End the most recent filtering operation in the current chain and return the
  596. * set of matched elements to its previous state.
  597. *
  598. * @category Traversing
  599. * @example
  600. *
  601. * ```js
  602. * $('li').eq(0).end().length;
  603. * //=> 3
  604. * ```
  605. *
  606. * @returns The previous state of the set of matched elements.
  607. * @see {@link https://api.jquery.com/end/}
  608. */
  609. export declare function end<T>(this: Cheerio<T>): Cheerio<Node>;
  610. /**
  611. * Add elements to the set of matched elements.
  612. *
  613. * @category Traversing
  614. * @example
  615. *
  616. * ```js
  617. * $('.apple').add('.orange').length;
  618. * //=> 2
  619. * ```
  620. *
  621. * @param other - Elements to add.
  622. * @param context - Optionally the context of the new selection.
  623. * @returns The combined set.
  624. * @see {@link https://api.jquery.com/add/}
  625. */
  626. export declare function add<S extends Node, T extends Node>(this: Cheerio<T>, other: string | Cheerio<S> | S | S[], context?: Cheerio<S> | string): Cheerio<S | T>;
  627. /**
  628. * Add the previous set of elements on the stack to the current set, optionally
  629. * filtered by a selector.
  630. *
  631. * @category Traversing
  632. * @example
  633. *
  634. * ```js
  635. * $('li').eq(0).addBack('.orange').length;
  636. * //=> 2
  637. * ```
  638. *
  639. * @param selector - Selector for the elements to add.
  640. * @returns The combined set.
  641. * @see {@link https://api.jquery.com/addBack/}
  642. */
  643. export declare function addBack<T extends Node>(this: Cheerio<T>, selector?: string): Cheerio<Node>;
  644. //# sourceMappingURL=traversing.d.ts.map