attributes.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. * Methods for getting and modifying attributes.
  3. *
  4. * @module cheerio/attributes
  5. */
  6. import { type AnyNode, type Element } from 'domhandler';
  7. import type { Cheerio } from '../cheerio.js';
  8. /**
  9. * Method for getting attributes. Gets the attribute value for only the first
  10. * element in the matched set.
  11. *
  12. * @category Attributes
  13. * @example
  14. *
  15. * ```js
  16. * $('ul').attr('id');
  17. * //=> fruits
  18. * ```
  19. *
  20. * @param name - Name of the attribute.
  21. * @returns The attribute's value.
  22. * @see {@link https://api.jquery.com/attr/}
  23. */
  24. export declare function attr<T extends AnyNode>(this: Cheerio<T>, name: string): string | undefined;
  25. /**
  26. * Method for getting all attributes and their values of the first element in
  27. * the matched set.
  28. *
  29. * @category Attributes
  30. * @example
  31. *
  32. * ```js
  33. * $('ul').attr();
  34. * //=> { id: 'fruits' }
  35. * ```
  36. *
  37. * @returns The attribute's values.
  38. * @see {@link https://api.jquery.com/attr/}
  39. */
  40. export declare function attr<T extends AnyNode>(this: Cheerio<T>): Record<string, string> | undefined;
  41. /**
  42. * Method for setting attributes. Sets the attribute value for only the first
  43. * element in the matched set. If you set an attribute's value to `null`, you
  44. * remove that attribute. You may also pass a `map` and `function`.
  45. *
  46. * @category Attributes
  47. * @example
  48. *
  49. * ```js
  50. * $('.apple').attr('id', 'favorite').html();
  51. * //=> <li class="apple" id="favorite">Apple</li>
  52. * ```
  53. *
  54. * @param name - Name of the attribute.
  55. * @param value - The new value of the attribute.
  56. * @returns The instance itself.
  57. * @see {@link https://api.jquery.com/attr/}
  58. */
  59. export declare function attr<T extends AnyNode>(this: Cheerio<T>, name: string, value?: string | null | ((this: Element, i: number, attrib: string) => string | null)): Cheerio<T>;
  60. /**
  61. * Method for setting multiple attributes at once. Sets the attribute value for
  62. * only the first element in the matched set. If you set an attribute's value to
  63. * `null`, you remove that attribute.
  64. *
  65. * @category Attributes
  66. * @example
  67. *
  68. * ```js
  69. * $('.apple').attr({ id: 'favorite' }).html();
  70. * //=> <li class="apple" id="favorite">Apple</li>
  71. * ```
  72. *
  73. * @param values - Map of attribute names and values.
  74. * @returns The instance itself.
  75. * @see {@link https://api.jquery.com/attr/}
  76. */
  77. export declare function attr<T extends AnyNode>(this: Cheerio<T>, values: Record<string, string | null>): Cheerio<T>;
  78. interface StyleProp {
  79. length: number;
  80. [key: string]: string | number;
  81. [index: number]: string;
  82. }
  83. /**
  84. * Method for getting and setting properties. Gets the property value for only
  85. * the first element in the matched set.
  86. *
  87. * @category Attributes
  88. * @example
  89. *
  90. * ```js
  91. * $('input[type="checkbox"]').prop('checked');
  92. * //=> false
  93. *
  94. * $('input[type="checkbox"]').prop('checked', true).val();
  95. * //=> ok
  96. * ```
  97. *
  98. * @param name - Name of the property.
  99. * @returns If `value` is specified the instance itself, otherwise the prop's
  100. * value.
  101. * @see {@link https://api.jquery.com/prop/}
  102. */
  103. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: 'tagName' | 'nodeName'): string | undefined;
  104. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: 'innerHTML' | 'outerHTML' | 'innerText' | 'textContent'): string | null;
  105. /**
  106. * Get a parsed CSS style object.
  107. *
  108. * @param name - Name of the property.
  109. * @returns The style object, or `undefined` if the element has no `style`
  110. * attribute.
  111. */
  112. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: 'style'): StyleProp | undefined;
  113. /**
  114. * Resolve `href` or `src` of supported elements. Requires the `baseURI` option
  115. * to be set, and a global `URL` object to be part of the environment.
  116. *
  117. * @example With `baseURI` set to `'https://example.com'`:
  118. *
  119. * ```js
  120. * $('<img src="image.png">').prop('src');
  121. * //=> 'https://example.com/image.png'
  122. * ```
  123. *
  124. * @param name - Name of the property.
  125. * @returns The resolved URL, or `undefined` if the element is not supported.
  126. */
  127. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: 'href' | 'src'): string | undefined;
  128. /**
  129. * Get a property of an element.
  130. *
  131. * @param name - Name of the property.
  132. * @returns The property's value.
  133. */
  134. export declare function prop<T extends AnyNode, K extends keyof Element>(this: Cheerio<T>, name: K): Element[K];
  135. /**
  136. * Set a property of an element.
  137. *
  138. * @param name - Name of the property.
  139. * @param value - Value to set the property to.
  140. * @returns The instance itself.
  141. */
  142. export declare function prop<T extends AnyNode, K extends keyof Element>(this: Cheerio<T>, name: K, value: Element[K] | ((this: Element, i: number, prop: K) => Element[keyof Element])): Cheerio<T>;
  143. /**
  144. * Set multiple properties of an element.
  145. *
  146. * @example
  147. *
  148. * ```js
  149. * $('input[type="checkbox"]').prop({
  150. * checked: true,
  151. * disabled: false,
  152. * });
  153. * ```
  154. *
  155. * @param map - Object of properties to set.
  156. * @returns The instance itself.
  157. */
  158. export declare function prop<T extends AnyNode>(this: Cheerio<T>, map: Record<string, string | Element[keyof Element] | boolean>): Cheerio<T>;
  159. /**
  160. * Set a property of an element.
  161. *
  162. * @param name - Name of the property.
  163. * @param value - Value to set the property to.
  164. * @returns The instance itself.
  165. */
  166. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: string, value: string | boolean | null | ((this: Element, i: number, prop: string) => string | boolean)): Cheerio<T>;
  167. /**
  168. * Get a property of an element.
  169. *
  170. * @param name - The property's name.
  171. * @returns The property's value.
  172. */
  173. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: string): string;
  174. /**
  175. * Method for getting data attributes, for only the first element in the matched
  176. * set.
  177. *
  178. * @category Attributes
  179. * @example
  180. *
  181. * ```js
  182. * $('<div data-apple-color="red"></div>').data('apple-color');
  183. * //=> 'red'
  184. * ```
  185. *
  186. * @param name - Name of the data attribute.
  187. * @returns The data attribute's value, or `undefined` if the attribute does not
  188. * exist.
  189. * @see {@link https://api.jquery.com/data/}
  190. */
  191. export declare function data<T extends AnyNode>(this: Cheerio<T>, name: string): unknown | undefined;
  192. /**
  193. * Method for getting all of an element's data attributes, for only the first
  194. * element in the matched set.
  195. *
  196. * @category Attributes
  197. * @example
  198. *
  199. * ```js
  200. * $('<div data-apple-color="red"></div>').data();
  201. * //=> { appleColor: 'red' }
  202. * ```
  203. *
  204. * @returns A map with all of the data attributes.
  205. * @see {@link https://api.jquery.com/data/}
  206. */
  207. export declare function data<T extends AnyNode>(this: Cheerio<T>): Record<string, unknown>;
  208. /**
  209. * Method for setting data attributes, for only the first element in the matched
  210. * set.
  211. *
  212. * @category Attributes
  213. * @example
  214. *
  215. * ```js
  216. * const apple = $('.apple').data('kind', 'mac');
  217. *
  218. * apple.data('kind');
  219. * //=> 'mac'
  220. * ```
  221. *
  222. * @param name - Name of the data attribute.
  223. * @param value - The new value.
  224. * @returns The instance itself.
  225. * @see {@link https://api.jquery.com/data/}
  226. */
  227. export declare function data<T extends AnyNode>(this: Cheerio<T>, name: string, value: unknown): Cheerio<T>;
  228. /**
  229. * Method for setting multiple data attributes at once, for only the first
  230. * element in the matched set.
  231. *
  232. * @category Attributes
  233. * @example
  234. *
  235. * ```js
  236. * const apple = $('.apple').data({ kind: 'mac' });
  237. *
  238. * apple.data('kind');
  239. * //=> 'mac'
  240. * ```
  241. *
  242. * @param values - Map of names to values.
  243. * @returns The instance itself.
  244. * @see {@link https://api.jquery.com/data/}
  245. */
  246. export declare function data<T extends AnyNode>(this: Cheerio<T>, values: Record<string, unknown>): Cheerio<T>;
  247. /**
  248. * Method for getting the value of input, select, and textarea. Note: Support
  249. * for `map`, and `function` has not been added yet.
  250. *
  251. * @category Attributes
  252. * @example
  253. *
  254. * ```js
  255. * $('input[type="text"]').val();
  256. * //=> input_text
  257. * ```
  258. *
  259. * @returns The value.
  260. * @see {@link https://api.jquery.com/val/}
  261. */
  262. export declare function val<T extends AnyNode>(this: Cheerio<T>): string | undefined | string[];
  263. /**
  264. * Method for setting the value of input, select, and textarea. Note: Support
  265. * for `map`, and `function` has not been added yet.
  266. *
  267. * @category Attributes
  268. * @example
  269. *
  270. * ```js
  271. * $('input[type="text"]').val('test').html();
  272. * //=> <input type="text" value="test"/>
  273. * ```
  274. *
  275. * @param value - The new value.
  276. * @returns The instance itself.
  277. * @see {@link https://api.jquery.com/val/}
  278. */
  279. export declare function val<T extends AnyNode>(this: Cheerio<T>, value: string | string[]): Cheerio<T>;
  280. /**
  281. * Method for removing attributes by `name`.
  282. *
  283. * @category Attributes
  284. * @example
  285. *
  286. * ```js
  287. * $('.pear').removeAttr('class').html();
  288. * //=> <li>Pear</li>
  289. *
  290. * $('.apple').attr('id', 'favorite');
  291. * $('.apple').removeAttr('id class').html();
  292. * //=> <li>Apple</li>
  293. * ```
  294. *
  295. * @param name - Name of the attribute.
  296. * @returns The instance itself.
  297. * @see {@link https://api.jquery.com/removeAttr/}
  298. */
  299. export declare function removeAttr<T extends AnyNode>(this: Cheerio<T>, name: string): Cheerio<T>;
  300. /**
  301. * Check to see if _any_ of the matched elements have the given `className`.
  302. *
  303. * @category Attributes
  304. * @example
  305. *
  306. * ```js
  307. * $('.pear').hasClass('pear');
  308. * //=> true
  309. *
  310. * $('apple').hasClass('fruit');
  311. * //=> false
  312. *
  313. * $('li').hasClass('pear');
  314. * //=> true
  315. * ```
  316. *
  317. * @param className - Name of the class.
  318. * @returns Indicates if an element has the given `className`.
  319. * @see {@link https://api.jquery.com/hasClass/}
  320. */
  321. export declare function hasClass<T extends AnyNode>(this: Cheerio<T>, className: string): boolean;
  322. /**
  323. * Adds class(es) to all of the matched elements. Also accepts a `function`.
  324. *
  325. * @category Attributes
  326. * @example
  327. *
  328. * ```js
  329. * $('.pear').addClass('fruit').html();
  330. * //=> <li class="pear fruit">Pear</li>
  331. *
  332. * $('.apple').addClass('fruit red').html();
  333. * //=> <li class="apple fruit red">Apple</li>
  334. * ```
  335. *
  336. * @param value - Name of new class.
  337. * @returns The instance itself.
  338. * @see {@link https://api.jquery.com/addClass/}
  339. */
  340. export declare function addClass<T extends AnyNode, R extends ArrayLike<T>>(this: R, value?: string | ((this: Element, i: number, className: string) => string | undefined)): R;
  341. /**
  342. * Removes one or more space-separated classes from the selected elements. If no
  343. * `className` is defined, all classes will be removed. Also accepts a
  344. * `function`.
  345. *
  346. * @category Attributes
  347. * @example
  348. *
  349. * ```js
  350. * $('.pear').removeClass('pear').html();
  351. * //=> <li class="">Pear</li>
  352. *
  353. * $('.apple').addClass('red').removeClass().html();
  354. * //=> <li class="">Apple</li>
  355. * ```
  356. *
  357. * @param name - Name of the class. If not specified, removes all elements.
  358. * @returns The instance itself.
  359. * @see {@link https://api.jquery.com/removeClass/}
  360. */
  361. export declare function removeClass<T extends AnyNode, R extends ArrayLike<T>>(this: R, name?: string | ((this: Element, i: number, className: string) => string | undefined)): R;
  362. /**
  363. * Add or remove class(es) from the matched elements, depending on either the
  364. * class's presence or the value of the switch argument. Also accepts a
  365. * `function`.
  366. *
  367. * @category Attributes
  368. * @example
  369. *
  370. * ```js
  371. * $('.apple.green').toggleClass('fruit green red').html();
  372. * //=> <li class="apple fruit red">Apple</li>
  373. *
  374. * $('.apple.green').toggleClass('fruit green red', true).html();
  375. * //=> <li class="apple green fruit red">Apple</li>
  376. * ```
  377. *
  378. * @param value - Name of the class. Can also be a function.
  379. * @param stateVal - If specified the state of the class.
  380. * @returns The instance itself.
  381. * @see {@link https://api.jquery.com/toggleClass/}
  382. */
  383. export declare function toggleClass<T extends AnyNode, R extends ArrayLike<T>>(this: R, value?: string | ((this: Element, i: number, className: string, stateVal?: boolean) => string), stateVal?: boolean): R;
  384. export {};
  385. //# sourceMappingURL=attributes.d.ts.map