@depup/cheerio 1.2.0-depup.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (230) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +36 -0
  3. package/Readme.md +229 -0
  4. package/changes.json +30 -0
  5. package/dist/browser/api/attributes.d.ts +385 -0
  6. package/dist/browser/api/attributes.d.ts.map +1 -0
  7. package/dist/browser/api/attributes.js +636 -0
  8. package/dist/browser/api/attributes.js.map +1 -0
  9. package/dist/browser/api/css.d.ts +42 -0
  10. package/dist/browser/api/css.d.ts.map +1 -0
  11. package/dist/browser/api/css.js +116 -0
  12. package/dist/browser/api/css.js.map +1 -0
  13. package/dist/browser/api/extract.d.ts +27 -0
  14. package/dist/browser/api/extract.d.ts.map +1 -0
  15. package/dist/browser/api/extract.js +42 -0
  16. package/dist/browser/api/extract.js.map +1 -0
  17. package/dist/browser/api/forms.d.ts +36 -0
  18. package/dist/browser/api/forms.d.ts.map +1 -0
  19. package/dist/browser/api/forms.js +81 -0
  20. package/dist/browser/api/forms.js.map +1 -0
  21. package/dist/browser/api/manipulation.d.ts +528 -0
  22. package/dist/browser/api/manipulation.d.ts.map +1 -0
  23. package/dist/browser/api/manipulation.js +831 -0
  24. package/dist/browser/api/manipulation.js.map +1 -0
  25. package/dist/browser/api/traversing.d.ts +657 -0
  26. package/dist/browser/api/traversing.d.ts.map +1 -0
  27. package/dist/browser/api/traversing.js +857 -0
  28. package/dist/browser/api/traversing.js.map +1 -0
  29. package/dist/browser/cheerio.d.ts +85 -0
  30. package/dist/browser/cheerio.d.ts.map +1 -0
  31. package/dist/browser/cheerio.js +58 -0
  32. package/dist/browser/cheerio.js.map +1 -0
  33. package/dist/browser/index-browser.d.mts.map +1 -0
  34. package/dist/browser/index-browser.mjs.map +1 -0
  35. package/dist/browser/index.d.ts +5 -0
  36. package/dist/browser/index.js +3 -0
  37. package/dist/browser/load-parse.d.ts +20 -0
  38. package/dist/browser/load-parse.d.ts.map +1 -0
  39. package/dist/browser/load-parse.js +28 -0
  40. package/dist/browser/load-parse.js.map +1 -0
  41. package/dist/browser/load.d.ts +91 -0
  42. package/dist/browser/load.d.ts.map +1 -0
  43. package/dist/browser/load.js +129 -0
  44. package/dist/browser/load.js.map +1 -0
  45. package/dist/browser/options.d.ts +98 -0
  46. package/dist/browser/options.d.ts.map +1 -0
  47. package/dist/browser/options.js +34 -0
  48. package/dist/browser/options.js.map +1 -0
  49. package/dist/browser/package.json +3 -0
  50. package/dist/browser/parse.d.ts +18 -0
  51. package/dist/browser/parse.d.ts.map +1 -0
  52. package/dist/browser/parse.js +73 -0
  53. package/dist/browser/parse.js.map +1 -0
  54. package/dist/browser/parsers/parse5-adapter.d.ts +20 -0
  55. package/dist/browser/parsers/parse5-adapter.d.ts.map +1 -0
  56. package/dist/browser/parsers/parse5-adapter.js +50 -0
  57. package/dist/browser/parsers/parse5-adapter.js.map +1 -0
  58. package/dist/browser/slim.d.ts +25 -0
  59. package/dist/browser/slim.d.ts.map +1 -0
  60. package/dist/browser/slim.js +22 -0
  61. package/dist/browser/slim.js.map +1 -0
  62. package/dist/browser/static.d.ts +112 -0
  63. package/dist/browser/static.d.ts.map +1 -0
  64. package/dist/browser/static.js +204 -0
  65. package/dist/browser/static.js.map +1 -0
  66. package/dist/browser/types.d.ts +21 -0
  67. package/dist/browser/types.d.ts.map +1 -0
  68. package/dist/browser/types.js +3 -0
  69. package/dist/browser/types.js.map +1 -0
  70. package/dist/browser/utils.d.ts +55 -0
  71. package/dist/browser/utils.d.ts.map +1 -0
  72. package/dist/browser/utils.js +84 -0
  73. package/dist/browser/utils.js.map +1 -0
  74. package/dist/commonjs/api/attributes.d.ts +385 -0
  75. package/dist/commonjs/api/attributes.d.ts.map +1 -0
  76. package/dist/commonjs/api/attributes.js +647 -0
  77. package/dist/commonjs/api/attributes.js.map +1 -0
  78. package/dist/commonjs/api/css.d.ts +42 -0
  79. package/dist/commonjs/api/css.d.ts.map +1 -0
  80. package/dist/commonjs/api/css.js +119 -0
  81. package/dist/commonjs/api/css.js.map +1 -0
  82. package/dist/commonjs/api/extract.d.ts +27 -0
  83. package/dist/commonjs/api/extract.d.ts.map +1 -0
  84. package/dist/commonjs/api/extract.js +45 -0
  85. package/dist/commonjs/api/extract.js.map +1 -0
  86. package/dist/commonjs/api/forms.d.ts +36 -0
  87. package/dist/commonjs/api/forms.d.ts.map +1 -0
  88. package/dist/commonjs/api/forms.js +85 -0
  89. package/dist/commonjs/api/forms.js.map +1 -0
  90. package/dist/commonjs/api/manipulation.d.ts +528 -0
  91. package/dist/commonjs/api/manipulation.d.ts.map +1 -0
  92. package/dist/commonjs/api/manipulation.js +850 -0
  93. package/dist/commonjs/api/manipulation.js.map +1 -0
  94. package/dist/commonjs/api/traversing.d.ts +657 -0
  95. package/dist/commonjs/api/traversing.d.ts.map +1 -0
  96. package/dist/commonjs/api/traversing.js +914 -0
  97. package/dist/commonjs/api/traversing.js.map +1 -0
  98. package/dist/commonjs/cheerio.d.ts +85 -0
  99. package/dist/commonjs/cheerio.d.ts.map +1 -0
  100. package/dist/commonjs/cheerio.js +95 -0
  101. package/dist/commonjs/cheerio.js.map +1 -0
  102. package/dist/commonjs/index.d.ts +104 -0
  103. package/dist/commonjs/index.d.ts.map +1 -0
  104. package/dist/commonjs/index.js +250 -0
  105. package/dist/commonjs/index.js.map +1 -0
  106. package/dist/commonjs/load-parse.d.ts +20 -0
  107. package/dist/commonjs/load-parse.d.ts.map +1 -0
  108. package/dist/commonjs/load-parse.js +34 -0
  109. package/dist/commonjs/load-parse.js.map +1 -0
  110. package/dist/commonjs/load.d.ts +91 -0
  111. package/dist/commonjs/load.d.ts.map +1 -0
  112. package/dist/commonjs/load.js +165 -0
  113. package/dist/commonjs/load.js.map +1 -0
  114. package/dist/commonjs/options.d.ts +98 -0
  115. package/dist/commonjs/options.d.ts.map +1 -0
  116. package/dist/commonjs/options.js +37 -0
  117. package/dist/commonjs/options.js.map +1 -0
  118. package/dist/commonjs/package.json +3 -0
  119. package/dist/commonjs/parse.d.ts +18 -0
  120. package/dist/commonjs/parse.d.ts.map +1 -0
  121. package/dist/commonjs/parse.js +77 -0
  122. package/dist/commonjs/parse.js.map +1 -0
  123. package/dist/commonjs/parsers/parse5-adapter.d.ts +20 -0
  124. package/dist/commonjs/parsers/parse5-adapter.d.ts.map +1 -0
  125. package/dist/commonjs/parsers/parse5-adapter.js +54 -0
  126. package/dist/commonjs/parsers/parse5-adapter.js.map +1 -0
  127. package/dist/commonjs/slim.d.ts +25 -0
  128. package/dist/commonjs/slim.d.ts.map +1 -0
  129. package/dist/commonjs/slim.js +30 -0
  130. package/dist/commonjs/slim.js.map +1 -0
  131. package/dist/commonjs/static.d.ts +112 -0
  132. package/dist/commonjs/static.d.ts.map +1 -0
  133. package/dist/commonjs/static.js +214 -0
  134. package/dist/commonjs/static.js.map +1 -0
  135. package/dist/commonjs/types.d.ts +21 -0
  136. package/dist/commonjs/types.d.ts.map +1 -0
  137. package/dist/commonjs/types.js +4 -0
  138. package/dist/commonjs/types.js.map +1 -0
  139. package/dist/commonjs/utils.d.ts +55 -0
  140. package/dist/commonjs/utils.d.ts.map +1 -0
  141. package/dist/commonjs/utils.js +91 -0
  142. package/dist/commonjs/utils.js.map +1 -0
  143. package/dist/esm/api/attributes.d.ts +385 -0
  144. package/dist/esm/api/attributes.d.ts.map +1 -0
  145. package/dist/esm/api/attributes.js +636 -0
  146. package/dist/esm/api/attributes.js.map +1 -0
  147. package/dist/esm/api/css.d.ts +42 -0
  148. package/dist/esm/api/css.d.ts.map +1 -0
  149. package/dist/esm/api/css.js +116 -0
  150. package/dist/esm/api/css.js.map +1 -0
  151. package/dist/esm/api/extract.d.ts +27 -0
  152. package/dist/esm/api/extract.d.ts.map +1 -0
  153. package/dist/esm/api/extract.js +42 -0
  154. package/dist/esm/api/extract.js.map +1 -0
  155. package/dist/esm/api/forms.d.ts +36 -0
  156. package/dist/esm/api/forms.d.ts.map +1 -0
  157. package/dist/esm/api/forms.js +81 -0
  158. package/dist/esm/api/forms.js.map +1 -0
  159. package/dist/esm/api/manipulation.d.ts +528 -0
  160. package/dist/esm/api/manipulation.d.ts.map +1 -0
  161. package/dist/esm/api/manipulation.js +831 -0
  162. package/dist/esm/api/manipulation.js.map +1 -0
  163. package/dist/esm/api/traversing.d.ts +657 -0
  164. package/dist/esm/api/traversing.d.ts.map +1 -0
  165. package/dist/esm/api/traversing.js +857 -0
  166. package/dist/esm/api/traversing.js.map +1 -0
  167. package/dist/esm/cheerio.d.ts +85 -0
  168. package/dist/esm/cheerio.d.ts.map +1 -0
  169. package/dist/esm/cheerio.js +58 -0
  170. package/dist/esm/cheerio.js.map +1 -0
  171. package/dist/esm/index.d.ts +104 -0
  172. package/dist/esm/index.d.ts.map +1 -0
  173. package/dist/esm/index.js +202 -0
  174. package/dist/esm/index.js.map +1 -0
  175. package/dist/esm/load-parse.d.ts +20 -0
  176. package/dist/esm/load-parse.d.ts.map +1 -0
  177. package/dist/esm/load-parse.js +28 -0
  178. package/dist/esm/load-parse.js.map +1 -0
  179. package/dist/esm/load.d.ts +91 -0
  180. package/dist/esm/load.d.ts.map +1 -0
  181. package/dist/esm/load.js +129 -0
  182. package/dist/esm/load.js.map +1 -0
  183. package/dist/esm/options.d.ts +98 -0
  184. package/dist/esm/options.d.ts.map +1 -0
  185. package/dist/esm/options.js +34 -0
  186. package/dist/esm/options.js.map +1 -0
  187. package/dist/esm/package.json +3 -0
  188. package/dist/esm/parse.d.ts +18 -0
  189. package/dist/esm/parse.d.ts.map +1 -0
  190. package/dist/esm/parse.js +73 -0
  191. package/dist/esm/parse.js.map +1 -0
  192. package/dist/esm/parsers/parse5-adapter.d.ts +20 -0
  193. package/dist/esm/parsers/parse5-adapter.d.ts.map +1 -0
  194. package/dist/esm/parsers/parse5-adapter.js +50 -0
  195. package/dist/esm/parsers/parse5-adapter.js.map +1 -0
  196. package/dist/esm/slim.d.ts +25 -0
  197. package/dist/esm/slim.d.ts.map +1 -0
  198. package/dist/esm/slim.js +22 -0
  199. package/dist/esm/slim.js.map +1 -0
  200. package/dist/esm/static.d.ts +112 -0
  201. package/dist/esm/static.d.ts.map +1 -0
  202. package/dist/esm/static.js +204 -0
  203. package/dist/esm/static.js.map +1 -0
  204. package/dist/esm/types.d.ts +21 -0
  205. package/dist/esm/types.d.ts.map +1 -0
  206. package/dist/esm/types.js +3 -0
  207. package/dist/esm/types.js.map +1 -0
  208. package/dist/esm/utils.d.ts +55 -0
  209. package/dist/esm/utils.d.ts.map +1 -0
  210. package/dist/esm/utils.js +84 -0
  211. package/dist/esm/utils.js.map +1 -0
  212. package/package.json +219 -0
  213. package/src/api/attributes.ts +1145 -0
  214. package/src/api/css.ts +224 -0
  215. package/src/api/extract.ts +92 -0
  216. package/src/api/forms.ts +103 -0
  217. package/src/api/manipulation.ts +1115 -0
  218. package/src/api/traversing.ts +1175 -0
  219. package/src/cheerio.ts +143 -0
  220. package/src/index-browser.mts +10 -0
  221. package/src/index.ts +294 -0
  222. package/src/load-parse.ts +39 -0
  223. package/src/load.ts +282 -0
  224. package/src/options.ts +136 -0
  225. package/src/parse.ts +105 -0
  226. package/src/parsers/parse5-adapter.ts +66 -0
  227. package/src/slim.ts +33 -0
  228. package/src/static.ts +312 -0
  229. package/src/types.ts +58 -0
  230. package/src/utils.ts +99 -0
@@ -0,0 +1,1145 @@
1
+ /**
2
+ * Methods for getting and modifying attributes.
3
+ *
4
+ * @module cheerio/attributes
5
+ */
6
+
7
+ import { text } from '../static.js';
8
+ import { domEach, camelCase, cssCase } from '../utils.js';
9
+ import { isTag, type AnyNode, type Element } from 'domhandler';
10
+ import type { Cheerio } from '../cheerio.js';
11
+ import { innerText, textContent } from 'domutils';
12
+ import { ElementType } from 'htmlparser2';
13
+ const hasOwn =
14
+ // @ts-expect-error `hasOwn` is a standard object method
15
+ (Object.hasOwn as (object: unknown, prop: string) => boolean) ??
16
+ ((object: unknown, prop: string) =>
17
+ Object.prototype.hasOwnProperty.call(object, prop));
18
+ const rspace = /\s+/;
19
+ const dataAttrPrefix = 'data-';
20
+
21
+ // Attributes that are booleans
22
+ const rboolean =
23
+ /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i;
24
+ // Matches strings that look like JSON objects or arrays
25
+ const rbrace = /^{[^]*}$|^\[[^]*]$/;
26
+
27
+ /**
28
+ * Gets a node's attribute. For boolean attributes, it will return the value's
29
+ * name should it be set.
30
+ *
31
+ * Also supports getting the `value` of several form elements.
32
+ *
33
+ * @private
34
+ * @category Attributes
35
+ * @param elem - Element to get the attribute of.
36
+ * @param name - Name of the attribute.
37
+ * @param xmlMode - Disable handling of special HTML attributes.
38
+ * @returns The attribute's value.
39
+ */
40
+ function getAttr(
41
+ elem: AnyNode,
42
+ name: undefined,
43
+ xmlMode?: boolean,
44
+ ): Record<string, string> | undefined;
45
+ function getAttr(
46
+ elem: AnyNode,
47
+ name: string,
48
+ xmlMode?: boolean,
49
+ ): string | undefined;
50
+ function getAttr(
51
+ elem: AnyNode,
52
+ name: string | undefined,
53
+ xmlMode?: boolean,
54
+ ): Record<string, string> | string | undefined {
55
+ if (!elem || !isTag(elem)) return undefined;
56
+
57
+ elem.attribs ??= {};
58
+
59
+ // Return the entire attribs object if no attribute specified
60
+ if (!name) {
61
+ return elem.attribs;
62
+ }
63
+
64
+ if (hasOwn(elem.attribs, name)) {
65
+ // Get the (decoded) attribute
66
+ return !xmlMode && rboolean.test(name) ? name : elem.attribs[name];
67
+ }
68
+
69
+ // Mimic the DOM and return text content as value for `option's`
70
+ if (elem.name === 'option' && name === 'value') {
71
+ return text(elem.children);
72
+ }
73
+
74
+ // Mimic DOM with default value for radios/checkboxes
75
+ if (
76
+ elem.name === 'input' &&
77
+ (elem.attribs['type'] === 'radio' || elem.attribs['type'] === 'checkbox') &&
78
+ name === 'value'
79
+ ) {
80
+ return 'on';
81
+ }
82
+
83
+ return undefined;
84
+ }
85
+
86
+ /**
87
+ * Sets the value of an attribute. The attribute will be deleted if the value is
88
+ * `null`.
89
+ *
90
+ * @private
91
+ * @param el - The element to set the attribute on.
92
+ * @param name - The attribute's name.
93
+ * @param value - The attribute's value.
94
+ */
95
+ function setAttr(el: Element, name: string, value: string | null) {
96
+ if (value === null) {
97
+ removeAttribute(el, name);
98
+ } else {
99
+ el.attribs[name] = `${value}`;
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Method for getting attributes. Gets the attribute value for only the first
105
+ * element in the matched set.
106
+ *
107
+ * @category Attributes
108
+ * @example
109
+ *
110
+ * ```js
111
+ * $('ul').attr('id');
112
+ * //=> fruits
113
+ * ```
114
+ *
115
+ * @param name - Name of the attribute.
116
+ * @returns The attribute's value.
117
+ * @see {@link https://api.jquery.com/attr/}
118
+ */
119
+ export function attr<T extends AnyNode>(
120
+ this: Cheerio<T>,
121
+ name: string,
122
+ ): string | undefined;
123
+ /**
124
+ * Method for getting all attributes and their values of the first element in
125
+ * the matched set.
126
+ *
127
+ * @category Attributes
128
+ * @example
129
+ *
130
+ * ```js
131
+ * $('ul').attr();
132
+ * //=> { id: 'fruits' }
133
+ * ```
134
+ *
135
+ * @returns The attribute's values.
136
+ * @see {@link https://api.jquery.com/attr/}
137
+ */
138
+ export function attr<T extends AnyNode>(
139
+ this: Cheerio<T>,
140
+ ): Record<string, string> | undefined;
141
+ /**
142
+ * Method for setting attributes. Sets the attribute value for all elements in
143
+ * the matched set. If you set an attribute's value to `null`, you remove that
144
+ * attribute. You may also pass a `map` and `function`.
145
+ *
146
+ * @category Attributes
147
+ * @example
148
+ *
149
+ * ```js
150
+ * $('.apple').attr('id', 'favorite').prop('outerHTML');
151
+ * //=> <li class="apple" id="favorite">Apple</li>
152
+ * ```
153
+ *
154
+ * @param name - Name of the attribute.
155
+ * @param value - The new value of the attribute.
156
+ * @returns The instance itself.
157
+ * @see {@link https://api.jquery.com/attr/}
158
+ */
159
+ export function attr<T extends AnyNode>(
160
+ this: Cheerio<T>,
161
+ name: string,
162
+ value?:
163
+ | string
164
+ | null
165
+ | ((this: Element, i: number, attrib: string) => string | null),
166
+ ): Cheerio<T>;
167
+ /**
168
+ * Method for setting multiple attributes at once. Sets the attribute value for
169
+ * all elements in the matched set. If you set an attribute's value to `null`,
170
+ * you remove that attribute.
171
+ *
172
+ * @category Attributes
173
+ * @example
174
+ *
175
+ * ```js
176
+ * $('.apple').attr({ id: 'favorite' }).prop('outerHTML');
177
+ * //=> <li class="apple" id="favorite">Apple</li>
178
+ * ```
179
+ *
180
+ * @param values - Map of attribute names and values.
181
+ * @returns The instance itself.
182
+ * @see {@link https://api.jquery.com/attr/}
183
+ */
184
+ export function attr<T extends AnyNode>(
185
+ this: Cheerio<T>,
186
+ values: Record<string, string | null>,
187
+ ): Cheerio<T>;
188
+ export function attr<T extends AnyNode>(
189
+ this: Cheerio<T>,
190
+ name?: string | Record<string, string | null>,
191
+ value?:
192
+ | string
193
+ | null
194
+ | ((this: Element, i: number, attrib: string) => string | null),
195
+ ): string | Cheerio<T> | undefined | Record<string, string> {
196
+ // Set the value (with attr map support)
197
+ if (typeof name === 'object' || value !== undefined) {
198
+ if (typeof value === 'function') {
199
+ if (typeof name !== 'string') {
200
+ {
201
+ throw new Error('Bad combination of arguments.');
202
+ }
203
+ }
204
+ return domEach(this, (el, i) => {
205
+ if (isTag(el)) setAttr(el, name, value.call(el, i, el.attribs[name]));
206
+ });
207
+ }
208
+ return domEach(this, (el) => {
209
+ if (!isTag(el)) return;
210
+
211
+ if (typeof name === 'object') {
212
+ for (const objName of Object.keys(name)) {
213
+ const objValue = name[objName];
214
+ setAttr(el, objName, objValue);
215
+ }
216
+ } else {
217
+ setAttr(el, name!, value!);
218
+ }
219
+ });
220
+ }
221
+
222
+ return arguments.length > 1
223
+ ? this
224
+ : getAttr(this[0], name!, this.options.xmlMode);
225
+ }
226
+
227
+ /**
228
+ * Gets a node's prop.
229
+ *
230
+ * @private
231
+ * @category Attributes
232
+ * @param el - Element to get the prop of.
233
+ * @param name - Name of the prop.
234
+ * @param xmlMode - Disable handling of special HTML attributes.
235
+ * @returns The prop's value.
236
+ */
237
+ function getProp(
238
+ el: Element,
239
+ name: string,
240
+ xmlMode?: boolean,
241
+ ): string | undefined | boolean | Element[keyof Element] {
242
+ return name in el
243
+ ? // @ts-expect-error TS doesn't like us accessing the value directly here.
244
+ (el[name] as string | undefined)
245
+ : !xmlMode && rboolean.test(name)
246
+ ? getAttr(el, name, false) !== undefined
247
+ : getAttr(el, name, xmlMode);
248
+ }
249
+
250
+ /**
251
+ * Sets the value of a prop.
252
+ *
253
+ * @private
254
+ * @param el - The element to set the prop on.
255
+ * @param name - The prop's name.
256
+ * @param value - The prop's value.
257
+ * @param xmlMode - Disable handling of special HTML attributes.
258
+ */
259
+ function setProp(el: Element, name: string, value: unknown, xmlMode?: boolean) {
260
+ if (name in el) {
261
+ // @ts-expect-error Overriding value
262
+ el[name] = value;
263
+ } else {
264
+ setAttr(
265
+ el,
266
+ name,
267
+ !xmlMode && rboolean.test(name)
268
+ ? value
269
+ ? ''
270
+ : null
271
+ : `${value as string}`,
272
+ );
273
+ }
274
+ }
275
+
276
+ interface StyleProp {
277
+ length: number;
278
+ [key: string]: string | number;
279
+ [index: number]: string;
280
+ }
281
+
282
+ /**
283
+ * Method for getting and setting properties. Gets the property value for only
284
+ * the first element in the matched set.
285
+ *
286
+ * @category Attributes
287
+ * @example
288
+ *
289
+ * ```js
290
+ * $('input[type="checkbox"]').prop('checked');
291
+ * //=> false
292
+ *
293
+ * $('input[type="checkbox"]').prop('checked', true).val();
294
+ * //=> ok
295
+ * ```
296
+ *
297
+ * @param name - Name of the property.
298
+ * @returns If `value` is specified the instance itself, otherwise the prop's
299
+ * value.
300
+ * @see {@link https://api.jquery.com/prop/}
301
+ */
302
+ export function prop<T extends AnyNode>(
303
+ this: Cheerio<T>,
304
+ name: 'tagName' | 'nodeName',
305
+ ): string | undefined;
306
+ export function prop<T extends AnyNode>(
307
+ this: Cheerio<T>,
308
+ name: 'innerHTML' | 'outerHTML' | 'innerText' | 'textContent',
309
+ ): string | null;
310
+ /**
311
+ * Get a parsed CSS style object.
312
+ *
313
+ * @param name - Name of the property.
314
+ * @returns The style object, or `undefined` if the element has no `style`
315
+ * attribute.
316
+ */
317
+ export function prop<T extends AnyNode>(
318
+ this: Cheerio<T>,
319
+ name: 'style',
320
+ ): StyleProp | undefined;
321
+ /**
322
+ * Resolve `href` or `src` of supported elements. Requires the `baseURI` option
323
+ * to be set, and a global `URL` object to be part of the environment.
324
+ *
325
+ * @example With `baseURI` set to `'https://example.com'`:
326
+ *
327
+ * ```js
328
+ * $('<img src="image.png">').prop('src');
329
+ * //=> 'https://example.com/image.png'
330
+ * ```
331
+ *
332
+ * @param name - Name of the property.
333
+ * @returns The resolved URL, or `undefined` if the element is not supported.
334
+ */
335
+ export function prop<T extends AnyNode>(
336
+ this: Cheerio<T>,
337
+ name: 'href' | 'src',
338
+ ): string | undefined;
339
+ /**
340
+ * Get a property of an element.
341
+ *
342
+ * @param name - Name of the property.
343
+ * @returns The property's value.
344
+ */
345
+ export function prop<T extends AnyNode, K extends keyof Element>(
346
+ this: Cheerio<T>,
347
+ name: K,
348
+ ): Element[K];
349
+ /**
350
+ * Set a property of an element.
351
+ *
352
+ * @param name - Name of the property.
353
+ * @param value - Value to set the property to.
354
+ * @returns The instance itself.
355
+ */
356
+ export function prop<T extends AnyNode, K extends keyof Element>(
357
+ this: Cheerio<T>,
358
+ name: K,
359
+ value:
360
+ | Element[K]
361
+ | ((this: Element, i: number, prop: K) => Element[keyof Element]),
362
+ ): Cheerio<T>;
363
+ /**
364
+ * Set multiple properties of an element.
365
+ *
366
+ * @example
367
+ *
368
+ * ```js
369
+ * $('input[type="checkbox"]').prop({
370
+ * checked: true,
371
+ * disabled: false,
372
+ * });
373
+ * ```
374
+ *
375
+ * @param map - Object of properties to set.
376
+ * @returns The instance itself.
377
+ */
378
+ export function prop<T extends AnyNode>(
379
+ this: Cheerio<T>,
380
+ map: Record<string, string | Element[keyof Element] | boolean>,
381
+ ): Cheerio<T>;
382
+ /**
383
+ * Set a property of an element.
384
+ *
385
+ * @param name - Name of the property.
386
+ * @param value - Value to set the property to.
387
+ * @returns The instance itself.
388
+ */
389
+ export function prop<T extends AnyNode>(
390
+ this: Cheerio<T>,
391
+ name: string,
392
+ value:
393
+ | string
394
+ | boolean
395
+ | null
396
+ | ((this: Element, i: number, prop: string) => string | boolean),
397
+ ): Cheerio<T>;
398
+ /**
399
+ * Get a property of an element.
400
+ *
401
+ * @param name - The property's name.
402
+ * @returns The property's value.
403
+ */
404
+ export function prop<T extends AnyNode>(this: Cheerio<T>, name: string): string;
405
+ export function prop<T extends AnyNode>(
406
+ this: Cheerio<T>,
407
+ name: string | Record<string, string | Element[keyof Element] | boolean>,
408
+ value?: unknown,
409
+ ):
410
+ | Cheerio<T>
411
+ | string
412
+ | boolean
413
+ | undefined
414
+ | null
415
+ | Element[keyof Element]
416
+ | StyleProp {
417
+ if (typeof name === 'string' && value === undefined) {
418
+ const el = this[0];
419
+
420
+ if (!el) return undefined;
421
+
422
+ switch (name) {
423
+ case 'style': {
424
+ const property = this.css() as StyleProp;
425
+ const keys = Object.keys(property);
426
+ for (let i = 0; i < keys.length; i++) {
427
+ property[i] = keys[i];
428
+ }
429
+
430
+ property.length = keys.length;
431
+
432
+ return property;
433
+ }
434
+ case 'tagName':
435
+ case 'nodeName': {
436
+ if (!isTag(el)) return undefined;
437
+ return el.name.toUpperCase();
438
+ }
439
+
440
+ case 'href':
441
+ case 'src': {
442
+ if (!isTag(el)) return undefined;
443
+ const prop = el.attribs?.[name];
444
+
445
+ if (
446
+ typeof URL !== 'undefined' &&
447
+ ((name === 'href' && (el.tagName === 'a' || el.tagName === 'link')) ||
448
+ (name === 'src' &&
449
+ (el.tagName === 'img' ||
450
+ el.tagName === 'iframe' ||
451
+ el.tagName === 'audio' ||
452
+ el.tagName === 'video' ||
453
+ el.tagName === 'source'))) &&
454
+ prop !== undefined &&
455
+ this.options.baseURI
456
+ ) {
457
+ return new URL(prop, this.options.baseURI).href;
458
+ }
459
+
460
+ return prop;
461
+ }
462
+
463
+ case 'innerText': {
464
+ return innerText(el);
465
+ }
466
+
467
+ case 'textContent': {
468
+ return textContent(el);
469
+ }
470
+
471
+ case 'outerHTML': {
472
+ if (el.type === ElementType.Root) return this.html();
473
+ return this.clone().wrap('<container />').parent().html();
474
+ }
475
+
476
+ case 'innerHTML': {
477
+ return this.html();
478
+ }
479
+
480
+ default: {
481
+ if (!isTag(el)) return undefined;
482
+ return getProp(el, name, this.options.xmlMode);
483
+ }
484
+ }
485
+ }
486
+
487
+ if (typeof name === 'object' || value !== undefined) {
488
+ if (typeof value === 'function') {
489
+ if (typeof name === 'object') {
490
+ throw new TypeError('Bad combination of arguments.');
491
+ }
492
+ return domEach(this, (el, i) => {
493
+ if (isTag(el)) {
494
+ setProp(
495
+ el,
496
+ name,
497
+ value.call(el, i, getProp(el, name, this.options.xmlMode)),
498
+ this.options.xmlMode,
499
+ );
500
+ }
501
+ });
502
+ }
503
+
504
+ return domEach(this, (el) => {
505
+ if (!isTag(el)) return;
506
+
507
+ if (typeof name === 'object') {
508
+ for (const key of Object.keys(name)) {
509
+ const val = name[key];
510
+ setProp(el, key, val, this.options.xmlMode);
511
+ }
512
+ } else {
513
+ setProp(el, name, value, this.options.xmlMode);
514
+ }
515
+ });
516
+ }
517
+
518
+ return undefined;
519
+ }
520
+
521
+ /**
522
+ * An element with a data attribute.
523
+ *
524
+ * @private
525
+ */
526
+ interface DataElement extends Element {
527
+ /** The data attribute. */
528
+ data?: Record<string, unknown>;
529
+ }
530
+
531
+ /**
532
+ * Sets the value of a data attribute.
533
+ *
534
+ * @private
535
+ * @param elem - The element to set the data attribute on.
536
+ * @param name - The data attribute's name.
537
+ * @param value - The data attribute's value.
538
+ */
539
+ function setData(
540
+ elem: DataElement,
541
+ name: string | Record<string, unknown>,
542
+ value?: unknown,
543
+ ) {
544
+ elem.data ??= {};
545
+
546
+ if (typeof name === 'object') Object.assign(elem.data, name);
547
+ else if (typeof name === 'string' && value !== undefined) {
548
+ elem.data[name] = value;
549
+ }
550
+ }
551
+
552
+ /**
553
+ * Read _all_ HTML5 `data-*` attributes from the equivalent HTML5 `data-*`
554
+ * attribute, and cache the value in the node's internal data store.
555
+ *
556
+ * @private
557
+ * @category Attributes
558
+ * @param el - Element to get the data attribute of.
559
+ * @returns A map with all of the data attributes.
560
+ */
561
+ function readAllData(el: DataElement): unknown {
562
+ for (const domName of Object.keys(el.attribs)) {
563
+ if (!domName.startsWith(dataAttrPrefix)) {
564
+ continue;
565
+ }
566
+
567
+ const jsName = camelCase(domName.slice(dataAttrPrefix.length));
568
+
569
+ if (!hasOwn(el.data, jsName)) {
570
+ el.data![jsName] = parseDataValue(el.attribs[domName]);
571
+ }
572
+ }
573
+
574
+ return el.data;
575
+ }
576
+
577
+ /**
578
+ * Read the specified attribute from the equivalent HTML5 `data-*` attribute,
579
+ * and (if present) cache the value in the node's internal data store.
580
+ *
581
+ * @private
582
+ * @category Attributes
583
+ * @param el - Element to get the data attribute of.
584
+ * @param name - Name of the data attribute.
585
+ * @returns The data attribute's value.
586
+ */
587
+ function readData(el: DataElement, name: string): unknown {
588
+ const domName = dataAttrPrefix + cssCase(name);
589
+ const data = el.data!;
590
+
591
+ if (hasOwn(data, name)) {
592
+ return data[name];
593
+ }
594
+
595
+ if (hasOwn(el.attribs, domName)) {
596
+ return (data[name] = parseDataValue(el.attribs[domName]));
597
+ }
598
+
599
+ return undefined;
600
+ }
601
+
602
+ /**
603
+ * Coerce string data-* attributes to their corresponding JavaScript primitives.
604
+ *
605
+ * @private
606
+ * @category Attributes
607
+ * @param value - The value to parse.
608
+ * @returns The parsed value.
609
+ */
610
+ function parseDataValue(value: string): unknown {
611
+ if (value === 'null') return null;
612
+ if (value === 'true') return true;
613
+ if (value === 'false') return false;
614
+ const num = Number(value);
615
+ if (value === String(num)) return num;
616
+ if (rbrace.test(value)) {
617
+ try {
618
+ return JSON.parse(value);
619
+ } catch {
620
+ /* Ignore */
621
+ }
622
+ }
623
+ return value;
624
+ }
625
+
626
+ /**
627
+ * Method for getting data attributes, for only the first element in the matched
628
+ * set.
629
+ *
630
+ * @category Attributes
631
+ * @example
632
+ *
633
+ * ```js
634
+ * $('<div data-apple-color="red"></div>').data('apple-color');
635
+ * //=> 'red'
636
+ * ```
637
+ *
638
+ * @param name - Name of the data attribute.
639
+ * @returns The data attribute's value, or `undefined` if the attribute does not
640
+ * exist.
641
+ * @see {@link https://api.jquery.com/data/}
642
+ */
643
+ export function data<T extends AnyNode>(
644
+ this: Cheerio<T>,
645
+ name: string,
646
+ ): unknown;
647
+ /**
648
+ * Method for getting all of an element's data attributes, for only the first
649
+ * element in the matched set.
650
+ *
651
+ * @category Attributes
652
+ * @example
653
+ *
654
+ * ```js
655
+ * $('<div data-apple-color="red"></div>').data();
656
+ * //=> { appleColor: 'red' }
657
+ * ```
658
+ *
659
+ * @returns A map with all of the data attributes.
660
+ * @see {@link https://api.jquery.com/data/}
661
+ */
662
+ export function data<T extends AnyNode>(
663
+ this: Cheerio<T>,
664
+ ): Record<string, unknown>;
665
+ /**
666
+ * Method for setting data attributes, for only the first element in the matched
667
+ * set.
668
+ *
669
+ * @category Attributes
670
+ * @example
671
+ *
672
+ * ```js
673
+ * const apple = $('.apple').data('kind', 'mac');
674
+ *
675
+ * apple.data('kind');
676
+ * //=> 'mac'
677
+ * ```
678
+ *
679
+ * @param name - Name of the data attribute.
680
+ * @param value - The new value.
681
+ * @returns The instance itself.
682
+ * @see {@link https://api.jquery.com/data/}
683
+ */
684
+ export function data<T extends AnyNode>(
685
+ this: Cheerio<T>,
686
+ name: string,
687
+ value: unknown,
688
+ ): Cheerio<T>;
689
+ /**
690
+ * Method for setting multiple data attributes at once, for only the first
691
+ * element in the matched set.
692
+ *
693
+ * @category Attributes
694
+ * @example
695
+ *
696
+ * ```js
697
+ * const apple = $('.apple').data({ kind: 'mac' });
698
+ *
699
+ * apple.data('kind');
700
+ * //=> 'mac'
701
+ * ```
702
+ *
703
+ * @param values - Map of names to values.
704
+ * @returns The instance itself.
705
+ * @see {@link https://api.jquery.com/data/}
706
+ */
707
+ export function data<T extends AnyNode>(
708
+ this: Cheerio<T>,
709
+ values: Record<string, unknown>,
710
+ ): Cheerio<T>;
711
+ export function data<T extends AnyNode>(
712
+ this: Cheerio<T>,
713
+ name?: string | Record<string, unknown>,
714
+ value?: unknown,
715
+ ): unknown {
716
+ const elem = this[0];
717
+
718
+ if (!elem || !isTag(elem)) return;
719
+
720
+ const dataEl: DataElement = elem;
721
+ dataEl.data ??= {};
722
+
723
+ // Return the entire data object if no data specified
724
+ if (name == null) {
725
+ return readAllData(dataEl);
726
+ }
727
+
728
+ // Set the value (with attr map support)
729
+ if (typeof name === 'object' || value !== undefined) {
730
+ domEach(this, (el) => {
731
+ if (isTag(el)) {
732
+ if (typeof name === 'object') setData(el, name);
733
+ else setData(el, name, value);
734
+ }
735
+ });
736
+ return this;
737
+ }
738
+
739
+ return readData(dataEl, name);
740
+ }
741
+
742
+ /**
743
+ * Method for getting the value of input, select, and textarea. Note: Support
744
+ * for `map`, and `function` has not been added yet.
745
+ *
746
+ * @category Attributes
747
+ * @example
748
+ *
749
+ * ```js
750
+ * $('input[type="text"]').val();
751
+ * //=> input_text
752
+ * ```
753
+ *
754
+ * @returns The value.
755
+ * @see {@link https://api.jquery.com/val/}
756
+ */
757
+ export function val<T extends AnyNode>(
758
+ this: Cheerio<T>,
759
+ ): string | undefined | string[];
760
+ /**
761
+ * Method for setting the value of input, select, and textarea. Note: Support
762
+ * for `map`, and `function` has not been added yet.
763
+ *
764
+ * @category Attributes
765
+ * @example
766
+ *
767
+ * ```js
768
+ * $('input[type="text"]').val('test').prop('outerHTML');
769
+ * //=> <input type="text" value="test"/>
770
+ * ```
771
+ *
772
+ * @param value - The new value.
773
+ * @returns The instance itself.
774
+ * @see {@link https://api.jquery.com/val/}
775
+ */
776
+ export function val<T extends AnyNode>(
777
+ this: Cheerio<T>,
778
+ value: string | string[],
779
+ ): Cheerio<T>;
780
+ export function val<T extends AnyNode>(
781
+ this: Cheerio<T>,
782
+ value?: string | string[],
783
+ ): string | string[] | Cheerio<T> | undefined {
784
+ const querying = arguments.length === 0;
785
+ const element = this[0];
786
+
787
+ if (!element || !isTag(element)) return querying ? undefined : this;
788
+
789
+ switch (element.name) {
790
+ case 'textarea': {
791
+ return this.text(value as string);
792
+ }
793
+ case 'select': {
794
+ const option = this.find('option:selected');
795
+ if (!querying) {
796
+ if (this.attr('multiple') == null && typeof value === 'object') {
797
+ return this;
798
+ }
799
+
800
+ this.find('option').removeAttr('selected');
801
+
802
+ const values = typeof value === 'object' ? value : [value];
803
+ for (const val of values) {
804
+ this.find(`option[value="${val}"]`).attr('selected', '');
805
+ }
806
+
807
+ return this;
808
+ }
809
+
810
+ return this.attr('multiple')
811
+ ? option.toArray().map((el) => text(el.children))
812
+ : option.attr('value');
813
+ }
814
+ case 'button':
815
+ case 'input':
816
+ case 'option': {
817
+ return querying
818
+ ? this.attr('value')
819
+ : this.attr('value', value as string);
820
+ }
821
+ }
822
+
823
+ return undefined;
824
+ }
825
+
826
+ /**
827
+ * Remove an attribute.
828
+ *
829
+ * @private
830
+ * @param elem - Node to remove attribute from.
831
+ * @param name - Name of the attribute to remove.
832
+ */
833
+ function removeAttribute(elem: Element, name: string) {
834
+ if (!elem.attribs || !hasOwn(elem.attribs, name)) return;
835
+
836
+ delete elem.attribs[name];
837
+ }
838
+
839
+ /**
840
+ * Splits a space-separated list of names to individual names.
841
+ *
842
+ * @category Attributes
843
+ * @param names - Names to split.
844
+ * @returns - Split names.
845
+ */
846
+ function splitNames(names?: string): string[] {
847
+ return names ? names.trim().split(rspace) : [];
848
+ }
849
+
850
+ /**
851
+ * Method for removing attributes by `name`.
852
+ *
853
+ * @category Attributes
854
+ * @example
855
+ *
856
+ * ```js
857
+ * $('.pear').removeAttr('class').prop('outerHTML');
858
+ * //=> <li>Pear</li>
859
+ *
860
+ * $('.apple').attr('id', 'favorite');
861
+ * $('.apple').removeAttr('id class').prop('outerHTML');
862
+ * //=> <li>Apple</li>
863
+ * ```
864
+ *
865
+ * @param name - Name of the attribute.
866
+ * @returns The instance itself.
867
+ * @see {@link https://api.jquery.com/removeAttr/}
868
+ */
869
+ export function removeAttr<T extends AnyNode>(
870
+ this: Cheerio<T>,
871
+ name: string,
872
+ ): Cheerio<T> {
873
+ const attrNames = splitNames(name);
874
+
875
+ for (const attrName of attrNames) {
876
+ domEach(this, (elem) => {
877
+ if (isTag(elem)) removeAttribute(elem, attrName);
878
+ });
879
+ }
880
+
881
+ return this;
882
+ }
883
+
884
+ /**
885
+ * Check to see if _any_ of the matched elements have the given `className`.
886
+ *
887
+ * @category Attributes
888
+ * @example
889
+ *
890
+ * ```js
891
+ * $('.pear').hasClass('pear');
892
+ * //=> true
893
+ *
894
+ * $('apple').hasClass('fruit');
895
+ * //=> false
896
+ *
897
+ * $('li').hasClass('pear');
898
+ * //=> true
899
+ * ```
900
+ *
901
+ * @param className - Name of the class.
902
+ * @returns Indicates if an element has the given `className`.
903
+ * @see {@link https://api.jquery.com/hasClass/}
904
+ */
905
+ export function hasClass<T extends AnyNode>(
906
+ this: Cheerio<T>,
907
+ className: string,
908
+ ): boolean {
909
+ return this.toArray().some((elem) => {
910
+ const clazz = isTag(elem) && elem.attribs['class'];
911
+ let idx = -1;
912
+
913
+ if (clazz && className.length > 0) {
914
+ while ((idx = clazz.indexOf(className, idx + 1)) > -1) {
915
+ const end = idx + className.length;
916
+
917
+ if (
918
+ (idx === 0 || rspace.test(clazz[idx - 1])) &&
919
+ (end === clazz.length || rspace.test(clazz[end]))
920
+ ) {
921
+ return true;
922
+ }
923
+ }
924
+ }
925
+
926
+ return false;
927
+ });
928
+ }
929
+
930
+ /**
931
+ * Adds class(es) to all of the matched elements. Also accepts a `function`.
932
+ *
933
+ * @category Attributes
934
+ * @example
935
+ *
936
+ * ```js
937
+ * $('.pear').addClass('fruit').prop('outerHTML');
938
+ * //=> <li class="pear fruit">Pear</li>
939
+ *
940
+ * $('.apple').addClass('fruit red').prop('outerHTML');
941
+ * //=> <li class="apple fruit red">Apple</li>
942
+ * ```
943
+ *
944
+ * @param value - Name of new class.
945
+ * @returns The instance itself.
946
+ * @see {@link https://api.jquery.com/addClass/}
947
+ */
948
+ export function addClass<T extends AnyNode, R extends ArrayLike<T>>(
949
+ this: R,
950
+ value?:
951
+ | string
952
+ | ((this: Element, i: number, className: string) => string | undefined),
953
+ ): R {
954
+ // Support functions
955
+ if (typeof value === 'function') {
956
+ return domEach(this, (el, i) => {
957
+ if (isTag(el)) {
958
+ const className = el.attribs['class'] || '';
959
+ addClass.call([el], value.call(el, i, className));
960
+ }
961
+ });
962
+ }
963
+
964
+ // Return if no value or not a string or function
965
+ if (!value || typeof value !== 'string') return this;
966
+
967
+ const classNames = value.split(rspace);
968
+ const numElements = this.length;
969
+
970
+ for (let i = 0; i < numElements; i++) {
971
+ const el = this[i];
972
+ // If selected element isn't a tag, move on
973
+ if (!isTag(el)) continue;
974
+
975
+ // If we don't already have classes — always set xmlMode to false here, as it doesn't matter for classes
976
+ const className = getAttr(el, 'class', false);
977
+
978
+ if (className) {
979
+ let setClass = ` ${className} `;
980
+
981
+ // Check if class already exists
982
+ for (const cn of classNames) {
983
+ const appendClass = `${cn} `;
984
+ if (!setClass.includes(` ${appendClass}`)) setClass += appendClass;
985
+ }
986
+
987
+ setAttr(el, 'class', setClass.trim());
988
+ } else {
989
+ setAttr(el, 'class', classNames.join(' ').trim());
990
+ }
991
+ }
992
+
993
+ return this;
994
+ }
995
+
996
+ /**
997
+ * Removes one or more space-separated classes from the selected elements. If no
998
+ * `className` is defined, all classes will be removed. Also accepts a
999
+ * `function`.
1000
+ *
1001
+ * @category Attributes
1002
+ * @example
1003
+ *
1004
+ * ```js
1005
+ * $('.pear').removeClass('pear').prop('outerHTML');
1006
+ * //=> <li class="">Pear</li>
1007
+ *
1008
+ * $('.apple').addClass('red').removeClass().prop('outerHTML');
1009
+ * //=> <li class="">Apple</li>
1010
+ * ```
1011
+ *
1012
+ * @param name - Name of the class. If not specified, removes all elements.
1013
+ * @returns The instance itself.
1014
+ * @see {@link https://api.jquery.com/removeClass/}
1015
+ */
1016
+ export function removeClass<T extends AnyNode, R extends ArrayLike<T>>(
1017
+ this: R,
1018
+ name?:
1019
+ | string
1020
+ | ((this: Element, i: number, className: string) => string | undefined),
1021
+ ): R {
1022
+ // Handle if value is a function
1023
+ if (typeof name === 'function') {
1024
+ return domEach(this, (el, i) => {
1025
+ if (isTag(el)) {
1026
+ removeClass.call([el], name.call(el, i, el.attribs['class'] || ''));
1027
+ }
1028
+ });
1029
+ }
1030
+
1031
+ const classes = splitNames(name);
1032
+ const numClasses = classes.length;
1033
+ const removeAll = arguments.length === 0;
1034
+
1035
+ return domEach(this, (el) => {
1036
+ if (!isTag(el)) return;
1037
+
1038
+ if (removeAll) {
1039
+ // Short circuit the remove all case as this is the nice one
1040
+ el.attribs['class'] = '';
1041
+ } else {
1042
+ const elClasses = splitNames(el.attribs['class']);
1043
+ let changed = false;
1044
+
1045
+ for (let j = 0; j < numClasses; j++) {
1046
+ const index = elClasses.indexOf(classes[j]);
1047
+
1048
+ if (index !== -1) {
1049
+ elClasses.splice(index, 1);
1050
+ changed = true;
1051
+
1052
+ /*
1053
+ * We have to do another pass to ensure that there are not duplicate
1054
+ * classes listed
1055
+ */
1056
+ j--;
1057
+ }
1058
+ }
1059
+ if (changed) {
1060
+ el.attribs['class'] = elClasses.join(' ');
1061
+ }
1062
+ }
1063
+ });
1064
+ }
1065
+
1066
+ /**
1067
+ * Add or remove class(es) from the matched elements, depending on either the
1068
+ * class's presence or the value of the switch argument. Also accepts a
1069
+ * `function`.
1070
+ *
1071
+ * @category Attributes
1072
+ * @example
1073
+ *
1074
+ * ```js
1075
+ * $('.apple.green').toggleClass('fruit green red').prop('outerHTML');
1076
+ * //=> <li class="apple fruit red">Apple</li>
1077
+ *
1078
+ * $('.apple.green').toggleClass('fruit green red', true).prop('outerHTML');
1079
+ * //=> <li class="apple green fruit red">Apple</li>
1080
+ * ```
1081
+ *
1082
+ * @param value - Name of the class. Can also be a function.
1083
+ * @param stateVal - If specified the state of the class.
1084
+ * @returns The instance itself.
1085
+ * @see {@link https://api.jquery.com/toggleClass/}
1086
+ */
1087
+ export function toggleClass<T extends AnyNode, R extends ArrayLike<T>>(
1088
+ this: R,
1089
+ value?:
1090
+ | string
1091
+ | ((
1092
+ this: Element,
1093
+ i: number,
1094
+ className: string,
1095
+ stateVal?: boolean,
1096
+ ) => string),
1097
+ stateVal?: boolean,
1098
+ ): R {
1099
+ // Support functions
1100
+ if (typeof value === 'function') {
1101
+ return domEach(this, (el, i) => {
1102
+ if (isTag(el)) {
1103
+ toggleClass.call(
1104
+ [el],
1105
+ value.call(el, i, el.attribs['class'] || '', stateVal),
1106
+ stateVal,
1107
+ );
1108
+ }
1109
+ });
1110
+ }
1111
+
1112
+ // Return if no value or not a string or function
1113
+ if (!value || typeof value !== 'string') return this;
1114
+
1115
+ const classNames = value.split(rspace);
1116
+ const numClasses = classNames.length;
1117
+ const state = typeof stateVal === 'boolean' ? (stateVal ? 1 : -1) : 0;
1118
+ const numElements = this.length;
1119
+
1120
+ for (let i = 0; i < numElements; i++) {
1121
+ const el = this[i];
1122
+ // If selected element isn't a tag, move on
1123
+ if (!isTag(el)) continue;
1124
+
1125
+ const elementClasses = splitNames(el.attribs['class']);
1126
+
1127
+ // Check if class already exists
1128
+ for (let j = 0; j < numClasses; j++) {
1129
+ // Check if the class name is currently defined
1130
+ const index = elementClasses.indexOf(classNames[j]);
1131
+
1132
+ // Add if stateValue === true or we are toggling and there is no value
1133
+ if (state >= 0 && index === -1) {
1134
+ elementClasses.push(classNames[j]);
1135
+ } else if (state <= 0 && index !== -1) {
1136
+ // Otherwise remove but only if the item exists
1137
+ elementClasses.splice(index, 1);
1138
+ }
1139
+ }
1140
+
1141
+ el.attribs['class'] = elementClasses.join(' ');
1142
+ }
1143
+
1144
+ return this;
1145
+ }