attributes.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * Methods for getting and modifying attributes.
  3. *
  4. * @module cheerio/attributes
  5. */
  6. import type { AnyNode, 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. * @param value - If specified set the property to this.
  100. * @returns If `value` is specified the instance itself, otherwise the prop's value.
  101. * @see {@link https://api.jquery.com/prop/}
  102. */
  103. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: 'tagName' | 'nodeName'): T extends Element ? string : undefined;
  104. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: 'innerHTML' | 'outerHTML' | 'innerText' | 'textContent'): string | null;
  105. /** Get a parsed CSS style object. */
  106. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: 'style'): StyleProp | undefined;
  107. /**
  108. * Resolve `href` or `src` of supported elements. Requires the `baseURI` option
  109. * to be set, and a global `URL` object to be part of the environment.
  110. *
  111. * @example With `baseURI` set to `'https://example.com'`:
  112. *
  113. * ```js
  114. * $('<img src="image.png">').prop('src');
  115. * //=> 'https://example.com/image.png'
  116. * ```
  117. */
  118. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: 'href' | 'src'): string | undefined;
  119. /** Get a property of an element. */
  120. export declare function prop<T extends AnyNode, K extends keyof Element>(this: Cheerio<T>, name: K): Element[K];
  121. /** Set a property of an element. */
  122. 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>;
  123. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: Record<string, string | Element[keyof Element] | boolean>): Cheerio<T>;
  124. 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>;
  125. export declare function prop<T extends AnyNode>(this: Cheerio<T>, name: string): string;
  126. /**
  127. * Method for getting data attributes, for only the first element in the matched set.
  128. *
  129. * @category Attributes
  130. * @example
  131. *
  132. * ```js
  133. * $('<div data-apple-color="red"></div>').data('apple-color');
  134. * //=> 'red'
  135. * ```
  136. *
  137. * @param name - Name of the data attribute.
  138. * @returns The data attribute's value, or `undefined` if the attribute does not exist.
  139. * @see {@link https://api.jquery.com/data/}
  140. */
  141. export declare function data<T extends AnyNode>(this: Cheerio<T>, name: string): unknown | undefined;
  142. /**
  143. * Method for getting all of an element's data attributes, for only the first
  144. * element in the matched set.
  145. *
  146. * @category Attributes
  147. * @example
  148. *
  149. * ```js
  150. * $('<div data-apple-color="red"></div>').data();
  151. * //=> { appleColor: 'red' }
  152. * ```
  153. *
  154. * @returns A map with all of the data attributes.
  155. * @see {@link https://api.jquery.com/data/}
  156. */
  157. export declare function data<T extends AnyNode>(this: Cheerio<T>): Record<string, unknown>;
  158. /**
  159. * Method for setting data attributes, for only the first element in the matched set.
  160. *
  161. * @category Attributes
  162. * @example
  163. *
  164. * ```js
  165. * const apple = $('.apple').data('kind', 'mac');
  166. *
  167. * apple.data('kind');
  168. * //=> 'mac'
  169. * ```
  170. *
  171. * @param name - Name of the data attribute.
  172. * @param value - The new value.
  173. * @returns The instance itself.
  174. * @see {@link https://api.jquery.com/data/}
  175. */
  176. export declare function data<T extends AnyNode>(this: Cheerio<T>, name: string, value: unknown): Cheerio<T>;
  177. /**
  178. * Method for setting multiple data attributes at once, for only the first
  179. * element in the matched set.
  180. *
  181. * @category Attributes
  182. * @example
  183. *
  184. * ```js
  185. * const apple = $('.apple').data({ kind: 'mac' });
  186. *
  187. * apple.data('kind');
  188. * //=> 'mac'
  189. * ```
  190. *
  191. * @param values - Map of names to values.
  192. * @returns The instance itself.
  193. * @see {@link https://api.jquery.com/data/}
  194. */
  195. export declare function data<T extends AnyNode>(this: Cheerio<T>, values: Record<string, unknown>): Cheerio<T>;
  196. /**
  197. * Method for getting the value of input, select, and textarea. Note: Support
  198. * for `map`, and `function` has not been added yet.
  199. *
  200. * @category Attributes
  201. * @example
  202. *
  203. * ```js
  204. * $('input[type="text"]').val();
  205. * //=> input_text
  206. * ```
  207. *
  208. * @returns The value.
  209. * @see {@link https://api.jquery.com/val/}
  210. */
  211. export declare function val<T extends AnyNode>(this: Cheerio<T>): string | undefined | string[];
  212. /**
  213. * Method for setting the value of input, select, and textarea. Note: Support
  214. * for `map`, and `function` has not been added yet.
  215. *
  216. * @category Attributes
  217. * @example
  218. *
  219. * ```js
  220. * $('input[type="text"]').val('test').html();
  221. * //=> <input type="text" value="test"/>
  222. * ```
  223. *
  224. * @param value - The new value.
  225. * @returns The instance itself.
  226. * @see {@link https://api.jquery.com/val/}
  227. */
  228. export declare function val<T extends AnyNode>(this: Cheerio<T>, value: string | string[]): Cheerio<T>;
  229. /**
  230. * Method for removing attributes by `name`.
  231. *
  232. * @category Attributes
  233. * @example
  234. *
  235. * ```js
  236. * $('.pear').removeAttr('class').html();
  237. * //=> <li>Pear</li>
  238. *
  239. * $('.apple').attr('id', 'favorite');
  240. * $('.apple').removeAttr('id class').html();
  241. * //=> <li>Apple</li>
  242. * ```
  243. *
  244. * @param name - Name of the attribute.
  245. * @returns The instance itself.
  246. * @see {@link https://api.jquery.com/removeAttr/}
  247. */
  248. export declare function removeAttr<T extends AnyNode>(this: Cheerio<T>, name: string): Cheerio<T>;
  249. /**
  250. * Check to see if _any_ of the matched elements have the given `className`.
  251. *
  252. * @category Attributes
  253. * @example
  254. *
  255. * ```js
  256. * $('.pear').hasClass('pear');
  257. * //=> true
  258. *
  259. * $('apple').hasClass('fruit');
  260. * //=> false
  261. *
  262. * $('li').hasClass('pear');
  263. * //=> true
  264. * ```
  265. *
  266. * @param className - Name of the class.
  267. * @returns Indicates if an element has the given `className`.
  268. * @see {@link https://api.jquery.com/hasClass/}
  269. */
  270. export declare function hasClass<T extends AnyNode>(this: Cheerio<T>, className: string): boolean;
  271. /**
  272. * Adds class(es) to all of the matched elements. Also accepts a `function`.
  273. *
  274. * @category Attributes
  275. * @example
  276. *
  277. * ```js
  278. * $('.pear').addClass('fruit').html();
  279. * //=> <li class="pear fruit">Pear</li>
  280. *
  281. * $('.apple').addClass('fruit red').html();
  282. * //=> <li class="apple fruit red">Apple</li>
  283. * ```
  284. *
  285. * @param value - Name of new class.
  286. * @returns The instance itself.
  287. * @see {@link https://api.jquery.com/addClass/}
  288. */
  289. export declare function addClass<T extends AnyNode, R extends ArrayLike<T>>(this: R, value?: string | ((this: Element, i: number, className: string) => string | undefined)): R;
  290. /**
  291. * Removes one or more space-separated classes from the selected elements. If no
  292. * `className` is defined, all classes will be removed. Also accepts a `function`.
  293. *
  294. * @category Attributes
  295. * @example
  296. *
  297. * ```js
  298. * $('.pear').removeClass('pear').html();
  299. * //=> <li class="">Pear</li>
  300. *
  301. * $('.apple').addClass('red').removeClass().html();
  302. * //=> <li class="">Apple</li>
  303. * ```
  304. *
  305. * @param name - Name of the class. If not specified, removes all elements.
  306. * @returns The instance itself.
  307. * @see {@link https://api.jquery.com/removeClass/}
  308. */
  309. export declare function removeClass<T extends AnyNode, R extends ArrayLike<T>>(this: R, name?: string | ((this: Element, i: number, className: string) => string | undefined)): R;
  310. /**
  311. * Add or remove class(es) from the matched elements, depending on either the
  312. * class's presence or the value of the switch argument. Also accepts a `function`.
  313. *
  314. * @category Attributes
  315. * @example
  316. *
  317. * ```js
  318. * $('.apple.green').toggleClass('fruit green red').html();
  319. * //=> <li class="apple fruit red">Apple</li>
  320. *
  321. * $('.apple.green').toggleClass('fruit green red', true).html();
  322. * //=> <li class="apple green fruit red">Apple</li>
  323. * ```
  324. *
  325. * @param value - Name of the class. Can also be a function.
  326. * @param stateVal - If specified the state of the class.
  327. * @returns The instance itself.
  328. * @see {@link https://api.jquery.com/toggleClass/}
  329. */
  330. 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;
  331. export {};
  332. //# sourceMappingURL=attributes.d.ts.map