attributes.d.ts 9.9 KB

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