@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,857 @@
1
+ /**
2
+ * Methods for traversing the DOM structure.
3
+ *
4
+ * @module cheerio/traversing
5
+ */
6
+ import { isTag, hasChildren, isDocument, } from 'domhandler';
7
+ import * as select from 'cheerio-select';
8
+ import { domEach, isCheerio } from '../utils.js';
9
+ import { contains } from '../static.js';
10
+ import { getChildren, getSiblings, nextElementSibling, prevElementSibling, uniqueSort, } from 'domutils';
11
+ const reContextSelector = /^\s*(?:[+~]|:scope\b)/;
12
+ /**
13
+ * Get the descendants of each element in the current set of matched elements,
14
+ * filtered by a selector, jQuery object, or element.
15
+ *
16
+ * @category Traversing
17
+ * @example
18
+ *
19
+ * ```js
20
+ * $('#fruits').find('li').length;
21
+ * //=> 3
22
+ * $('#fruits').find($('.apple')).length;
23
+ * //=> 1
24
+ * ```
25
+ *
26
+ * @param selectorOrHaystack - Element to look for.
27
+ * @returns The found elements.
28
+ * @see {@link https://api.jquery.com/find/}
29
+ */
30
+ export function find(selectorOrHaystack) {
31
+ if (!selectorOrHaystack) {
32
+ return this._make([]);
33
+ }
34
+ if (typeof selectorOrHaystack !== 'string') {
35
+ const haystack = isCheerio(selectorOrHaystack)
36
+ ? selectorOrHaystack.toArray()
37
+ : [selectorOrHaystack];
38
+ const context = this.toArray();
39
+ return this._make(haystack.filter((elem) => context.some((node) => contains(node, elem))));
40
+ }
41
+ return this._findBySelector(selectorOrHaystack, Number.POSITIVE_INFINITY);
42
+ }
43
+ /**
44
+ * Find elements by a specific selector.
45
+ *
46
+ * @private
47
+ * @category Traversing
48
+ * @param selector - Selector to filter by.
49
+ * @param limit - Maximum number of elements to match.
50
+ * @returns The found elements.
51
+ */
52
+ export function _findBySelector(selector, limit) {
53
+ var _a;
54
+ const context = this.toArray();
55
+ const elems = reContextSelector.test(selector)
56
+ ? context
57
+ : this.children().toArray();
58
+ const options = {
59
+ context,
60
+ root: (_a = this._root) === null || _a === void 0 ? void 0 : _a[0],
61
+ // Pass options that are recognized by `cheerio-select`
62
+ xmlMode: this.options.xmlMode,
63
+ lowerCaseTags: this.options.lowerCaseTags,
64
+ lowerCaseAttributeNames: this.options.lowerCaseAttributeNames,
65
+ pseudos: this.options.pseudos,
66
+ quirksMode: this.options.quirksMode,
67
+ };
68
+ return this._make(select.select(selector, elems, options, limit));
69
+ }
70
+ /**
71
+ * Creates a matcher, using a particular mapping function. Matchers provide a
72
+ * function that finds elements using a generating function, supporting
73
+ * filtering.
74
+ *
75
+ * @private
76
+ * @param matchMap - Mapping function.
77
+ * @returns - Function for wrapping generating functions.
78
+ */
79
+ function _getMatcher(matchMap) {
80
+ return function (fn, ...postFns) {
81
+ return function (selector) {
82
+ var _a;
83
+ let matched = matchMap(fn, this);
84
+ if (selector) {
85
+ matched = filterArray(matched, selector, this.options.xmlMode, (_a = this._root) === null || _a === void 0 ? void 0 : _a[0]);
86
+ }
87
+ return this._make(
88
+ // Post processing is only necessary if there is more than one element.
89
+ this.length > 1 && matched.length > 1
90
+ ? postFns.reduce((elems, fn) => fn(elems), matched)
91
+ : matched);
92
+ };
93
+ };
94
+ }
95
+ /** Matcher that adds multiple elements for each entry in the input. */
96
+ const _matcher = _getMatcher((fn, elems) => {
97
+ let ret = [];
98
+ for (let i = 0; i < elems.length; i++) {
99
+ const value = fn(elems[i]);
100
+ if (value.length > 0)
101
+ ret = ret.concat(value);
102
+ }
103
+ return ret;
104
+ });
105
+ /** Matcher that adds at most one element for each entry in the input. */
106
+ const _singleMatcher = _getMatcher((fn, elems) => {
107
+ const ret = [];
108
+ for (let i = 0; i < elems.length; i++) {
109
+ const value = fn(elems[i]);
110
+ if (value !== null) {
111
+ ret.push(value);
112
+ }
113
+ }
114
+ return ret;
115
+ });
116
+ /**
117
+ * Matcher that supports traversing until a condition is met.
118
+ *
119
+ * @param nextElem - Function that returns the next element.
120
+ * @param postFns - Post processing functions.
121
+ * @returns A function usable for `*Until` methods.
122
+ */
123
+ function _matchUntil(nextElem, ...postFns) {
124
+ // We use a variable here that is used from within the matcher.
125
+ let matches = null;
126
+ const innerMatcher = _getMatcher((nextElem, elems) => {
127
+ const matched = [];
128
+ domEach(elems, (elem) => {
129
+ for (let next; (next = nextElem(elem)); elem = next) {
130
+ // FIXME: `matched` might contain duplicates here and the index is too large.
131
+ if (matches === null || matches === void 0 ? void 0 : matches(next, matched.length))
132
+ break;
133
+ matched.push(next);
134
+ }
135
+ });
136
+ return matched;
137
+ })(nextElem, ...postFns);
138
+ return function (selector, filterSelector) {
139
+ // Override `matches` variable with the new target.
140
+ matches =
141
+ typeof selector === 'string'
142
+ ? (elem) => select.is(elem, selector, this.options)
143
+ : selector
144
+ ? getFilterFn(selector)
145
+ : null;
146
+ const ret = innerMatcher.call(this, filterSelector);
147
+ // Set `matches` to `null`, so we don't waste memory.
148
+ matches = null;
149
+ return ret;
150
+ };
151
+ }
152
+ function _removeDuplicates(elems) {
153
+ return elems.length > 1 ? Array.from(new Set(elems)) : elems;
154
+ }
155
+ /**
156
+ * Get the parent of each element in the current set of matched elements,
157
+ * optionally filtered by a selector.
158
+ *
159
+ * @category Traversing
160
+ * @example
161
+ *
162
+ * ```js
163
+ * $('.pear').parent().attr('id');
164
+ * //=> fruits
165
+ * ```
166
+ *
167
+ * @param selector - If specified filter for parent.
168
+ * @returns The parents.
169
+ * @see {@link https://api.jquery.com/parent/}
170
+ */
171
+ export const parent = _singleMatcher(({ parent }) => (parent && !isDocument(parent) ? parent : null), _removeDuplicates);
172
+ /**
173
+ * Get a set of parents filtered by `selector` of each element in the current
174
+ * set of match elements.
175
+ *
176
+ * @category Traversing
177
+ * @example
178
+ *
179
+ * ```js
180
+ * $('.orange').parents().length;
181
+ * //=> 2
182
+ * $('.orange').parents('#fruits').length;
183
+ * //=> 1
184
+ * ```
185
+ *
186
+ * @param selector - If specified filter for parents.
187
+ * @returns The parents.
188
+ * @see {@link https://api.jquery.com/parents/}
189
+ */
190
+ export const parents = _matcher((elem) => {
191
+ const matched = [];
192
+ while (elem.parent && !isDocument(elem.parent)) {
193
+ matched.push(elem.parent);
194
+ elem = elem.parent;
195
+ }
196
+ return matched;
197
+ }, uniqueSort,
198
+ // eslint-disable-next-line unicorn/no-array-reverse
199
+ (elems) => elems.reverse());
200
+ /**
201
+ * Get the ancestors of each element in the current set of matched elements, up
202
+ * to but not including the element matched by the selector, DOM node, or
203
+ * cheerio object.
204
+ *
205
+ * @category Traversing
206
+ * @example
207
+ *
208
+ * ```js
209
+ * $('.orange').parentsUntil('#food').length;
210
+ * //=> 1
211
+ * ```
212
+ *
213
+ * @param selector - Selector for element to stop at.
214
+ * @param filterSelector - Optional filter for parents.
215
+ * @returns The parents.
216
+ * @see {@link https://api.jquery.com/parentsUntil/}
217
+ */
218
+ export const parentsUntil = _matchUntil(({ parent }) => (parent && !isDocument(parent) ? parent : null), uniqueSort,
219
+ // eslint-disable-next-line unicorn/no-array-reverse
220
+ (elems) => elems.reverse());
221
+ /**
222
+ * For each element in the set, get the first element that matches the selector
223
+ * by testing the element itself and traversing up through its ancestors in the
224
+ * DOM tree.
225
+ *
226
+ * @category Traversing
227
+ * @example
228
+ *
229
+ * ```js
230
+ * $('.orange').closest();
231
+ * //=> []
232
+ *
233
+ * $('.orange').closest('.apple');
234
+ * // => []
235
+ *
236
+ * $('.orange').closest('li');
237
+ * //=> [<li class="orange">Orange</li>]
238
+ *
239
+ * $('.orange').closest('#fruits');
240
+ * //=> [<ul id="fruits"> ... </ul>]
241
+ * ```
242
+ *
243
+ * @param selector - Selector for the element to find.
244
+ * @returns The closest nodes.
245
+ * @see {@link https://api.jquery.com/closest/}
246
+ */
247
+ export function closest(selector) {
248
+ var _a;
249
+ const set = [];
250
+ if (!selector) {
251
+ return this._make(set);
252
+ }
253
+ const selectOpts = {
254
+ xmlMode: this.options.xmlMode,
255
+ root: (_a = this._root) === null || _a === void 0 ? void 0 : _a[0],
256
+ };
257
+ const selectFn = typeof selector === 'string'
258
+ ? (elem) => select.is(elem, selector, selectOpts)
259
+ : getFilterFn(selector);
260
+ domEach(this, (elem) => {
261
+ if (elem && !isDocument(elem) && !isTag(elem)) {
262
+ elem = elem.parent;
263
+ }
264
+ while (elem && isTag(elem)) {
265
+ if (selectFn(elem, 0)) {
266
+ // Do not add duplicate elements to the set
267
+ if (!set.includes(elem)) {
268
+ set.push(elem);
269
+ }
270
+ break;
271
+ }
272
+ elem = elem.parent;
273
+ }
274
+ });
275
+ return this._make(set);
276
+ }
277
+ /**
278
+ * Gets the next sibling of each selected element, optionally filtered by a
279
+ * selector.
280
+ *
281
+ * @category Traversing
282
+ * @example
283
+ *
284
+ * ```js
285
+ * $('.apple').next().hasClass('orange');
286
+ * //=> true
287
+ * ```
288
+ *
289
+ * @param selector - If specified filter for sibling.
290
+ * @returns The next nodes.
291
+ * @see {@link https://api.jquery.com/next/}
292
+ */
293
+ export const next = _singleMatcher((elem) => nextElementSibling(elem));
294
+ /**
295
+ * Gets all the following siblings of the each selected element, optionally
296
+ * filtered by a selector.
297
+ *
298
+ * @category Traversing
299
+ * @example
300
+ *
301
+ * ```js
302
+ * $('.apple').nextAll();
303
+ * //=> [<li class="orange">Orange</li>, <li class="pear">Pear</li>]
304
+ * $('.apple').nextAll('.orange');
305
+ * //=> [<li class="orange">Orange</li>]
306
+ * ```
307
+ *
308
+ * @param selector - If specified filter for siblings.
309
+ * @returns The next nodes.
310
+ * @see {@link https://api.jquery.com/nextAll/}
311
+ */
312
+ export const nextAll = _matcher((elem) => {
313
+ const matched = [];
314
+ while (elem.next) {
315
+ elem = elem.next;
316
+ if (isTag(elem))
317
+ matched.push(elem);
318
+ }
319
+ return matched;
320
+ }, _removeDuplicates);
321
+ /**
322
+ * Gets all the following siblings up to but not including the element matched
323
+ * by the selector, optionally filtered by another selector.
324
+ *
325
+ * @category Traversing
326
+ * @example
327
+ *
328
+ * ```js
329
+ * $('.apple').nextUntil('.pear');
330
+ * //=> [<li class="orange">Orange</li>]
331
+ * ```
332
+ *
333
+ * @param selector - Selector for element to stop at.
334
+ * @param filterSelector - If specified filter for siblings.
335
+ * @returns The next nodes.
336
+ * @see {@link https://api.jquery.com/nextUntil/}
337
+ */
338
+ export const nextUntil = _matchUntil((el) => nextElementSibling(el), _removeDuplicates);
339
+ /**
340
+ * Gets the previous sibling of each selected element optionally filtered by a
341
+ * selector.
342
+ *
343
+ * @category Traversing
344
+ * @example
345
+ *
346
+ * ```js
347
+ * $('.orange').prev().hasClass('apple');
348
+ * //=> true
349
+ * ```
350
+ *
351
+ * @param selector - If specified filter for siblings.
352
+ * @returns The previous nodes.
353
+ * @see {@link https://api.jquery.com/prev/}
354
+ */
355
+ export const prev = _singleMatcher((elem) => prevElementSibling(elem));
356
+ /**
357
+ * Gets all the preceding siblings of each selected element, optionally filtered
358
+ * by a selector.
359
+ *
360
+ * @category Traversing
361
+ * @example
362
+ *
363
+ * ```js
364
+ * $('.pear').prevAll();
365
+ * //=> [<li class="orange">Orange</li>, <li class="apple">Apple</li>]
366
+ *
367
+ * $('.pear').prevAll('.orange');
368
+ * //=> [<li class="orange">Orange</li>]
369
+ * ```
370
+ *
371
+ * @param selector - If specified filter for siblings.
372
+ * @returns The previous nodes.
373
+ * @see {@link https://api.jquery.com/prevAll/}
374
+ */
375
+ export const prevAll = _matcher((elem) => {
376
+ const matched = [];
377
+ while (elem.prev) {
378
+ elem = elem.prev;
379
+ if (isTag(elem))
380
+ matched.push(elem);
381
+ }
382
+ return matched;
383
+ }, _removeDuplicates);
384
+ /**
385
+ * Gets all the preceding siblings up to but not including the element matched
386
+ * by the selector, optionally filtered by another selector.
387
+ *
388
+ * @category Traversing
389
+ * @example
390
+ *
391
+ * ```js
392
+ * $('.pear').prevUntil('.apple');
393
+ * //=> [<li class="orange">Orange</li>]
394
+ * ```
395
+ *
396
+ * @param selector - Selector for element to stop at.
397
+ * @param filterSelector - If specified filter for siblings.
398
+ * @returns The previous nodes.
399
+ * @see {@link https://api.jquery.com/prevUntil/}
400
+ */
401
+ export const prevUntil = _matchUntil((el) => prevElementSibling(el), _removeDuplicates);
402
+ /**
403
+ * Get the siblings of each element (excluding the element) in the set of
404
+ * matched elements, optionally filtered by a selector.
405
+ *
406
+ * @category Traversing
407
+ * @example
408
+ *
409
+ * ```js
410
+ * $('.pear').siblings().length;
411
+ * //=> 2
412
+ *
413
+ * $('.pear').siblings('.orange').length;
414
+ * //=> 1
415
+ * ```
416
+ *
417
+ * @param selector - If specified filter for siblings.
418
+ * @returns The siblings.
419
+ * @see {@link https://api.jquery.com/siblings/}
420
+ */
421
+ export const siblings = _matcher((elem) => getSiblings(elem).filter((el) => isTag(el) && el !== elem), uniqueSort);
422
+ /**
423
+ * Gets the element children of each element in the set of matched elements.
424
+ *
425
+ * @category Traversing
426
+ * @example
427
+ *
428
+ * ```js
429
+ * $('#fruits').children().length;
430
+ * //=> 3
431
+ *
432
+ * $('#fruits').children('.pear').text();
433
+ * //=> Pear
434
+ * ```
435
+ *
436
+ * @param selector - If specified filter for children.
437
+ * @returns The children.
438
+ * @see {@link https://api.jquery.com/children/}
439
+ */
440
+ export const children = _matcher((elem) => getChildren(elem).filter(isTag), _removeDuplicates);
441
+ /**
442
+ * Gets the children of each element in the set of matched elements, including
443
+ * text and comment nodes.
444
+ *
445
+ * @category Traversing
446
+ * @example
447
+ *
448
+ * ```js
449
+ * $('#fruits').contents().length;
450
+ * //=> 3
451
+ * ```
452
+ *
453
+ * @returns The children.
454
+ * @see {@link https://api.jquery.com/contents/}
455
+ */
456
+ export function contents() {
457
+ const elems = this.toArray().reduce((newElems, elem) => hasChildren(elem) ? newElems.concat(elem.children) : newElems, []);
458
+ return this._make(elems);
459
+ }
460
+ /**
461
+ * Iterates over a cheerio object, executing a function for each matched
462
+ * element. When the callback is fired, the function is fired in the context of
463
+ * the DOM element, so `this` refers to the current element, which is equivalent
464
+ * to the function parameter `element`. To break out of the `each` loop early,
465
+ * return with `false`.
466
+ *
467
+ * @category Traversing
468
+ * @example
469
+ *
470
+ * ```js
471
+ * const fruits = [];
472
+ *
473
+ * $('li').each(function (i, elem) {
474
+ * fruits[i] = $(this).text();
475
+ * });
476
+ *
477
+ * fruits.join(', ');
478
+ * //=> Apple, Orange, Pear
479
+ * ```
480
+ *
481
+ * @param fn - Function to execute.
482
+ * @returns The instance itself, useful for chaining.
483
+ * @see {@link https://api.jquery.com/each/}
484
+ */
485
+ export function each(fn) {
486
+ let i = 0;
487
+ const len = this.length;
488
+ while (i < len && fn.call(this[i], i, this[i]) !== false)
489
+ ++i;
490
+ return this;
491
+ }
492
+ /**
493
+ * Pass each element in the current matched set through a function, producing a
494
+ * new Cheerio object containing the return values. The function can return an
495
+ * individual data item or an array of data items to be inserted into the
496
+ * resulting set. If an array is returned, the elements inside the array are
497
+ * inserted into the set. If the function returns null or undefined, no element
498
+ * will be inserted.
499
+ *
500
+ * @category Traversing
501
+ * @example
502
+ *
503
+ * ```js
504
+ * $('li')
505
+ * .map(function (i, el) {
506
+ * // this === el
507
+ * return $(this).text();
508
+ * })
509
+ * .toArray()
510
+ * .join(' ');
511
+ * //=> "apple orange pear"
512
+ * ```
513
+ *
514
+ * @param fn - Function to execute.
515
+ * @returns The mapped elements, wrapped in a Cheerio collection.
516
+ * @see {@link https://api.jquery.com/map/}
517
+ */
518
+ export function map(fn) {
519
+ let elems = [];
520
+ for (let i = 0; i < this.length; i++) {
521
+ const el = this[i];
522
+ const val = fn.call(el, i, el);
523
+ if (val != null) {
524
+ elems = elems.concat(val);
525
+ }
526
+ }
527
+ return this._make(elems);
528
+ }
529
+ /**
530
+ * Creates a function to test if a filter is matched.
531
+ *
532
+ * @param match - A filter.
533
+ * @returns A function that determines if a filter has been matched.
534
+ */
535
+ function getFilterFn(match) {
536
+ if (typeof match === 'function') {
537
+ return (el, i) => match.call(el, i, el);
538
+ }
539
+ if (isCheerio(match)) {
540
+ return (el) => Array.prototype.includes.call(match, el);
541
+ }
542
+ return function (el) {
543
+ return match === el;
544
+ };
545
+ }
546
+ export function filter(match) {
547
+ var _a;
548
+ return this._make(filterArray(this.toArray(), match, this.options.xmlMode, (_a = this._root) === null || _a === void 0 ? void 0 : _a[0]));
549
+ }
550
+ export function filterArray(nodes, match, xmlMode, root) {
551
+ return typeof match === 'string'
552
+ ? select.filter(match, nodes, { xmlMode, root })
553
+ : nodes.filter(getFilterFn(match));
554
+ }
555
+ /**
556
+ * Checks the current list of elements and returns `true` if _any_ of the
557
+ * elements match the selector. If using an element or Cheerio selection,
558
+ * returns `true` if _any_ of the elements match. If using a predicate function,
559
+ * the function is executed in the context of the selected element, so `this`
560
+ * refers to the current element.
561
+ *
562
+ * @category Traversing
563
+ * @param selector - Selector for the selection.
564
+ * @returns Whether or not the selector matches an element of the instance.
565
+ * @see {@link https://api.jquery.com/is/}
566
+ */
567
+ export function is(selector) {
568
+ const nodes = this.toArray();
569
+ return typeof selector === 'string'
570
+ ? select.some(nodes.filter(isTag), selector, this.options)
571
+ : selector
572
+ ? nodes.some(getFilterFn(selector))
573
+ : false;
574
+ }
575
+ /**
576
+ * Remove elements from the set of matched elements. Given a Cheerio object that
577
+ * represents a set of DOM elements, the `.not()` method constructs a new
578
+ * Cheerio object from a subset of the matching elements. The supplied selector
579
+ * is tested against each element; the elements that don't match the selector
580
+ * will be included in the result.
581
+ *
582
+ * The `.not()` method can take a function as its argument in the same way that
583
+ * `.filter()` does. Elements for which the function returns `true` are excluded
584
+ * from the filtered set; all other elements are included.
585
+ *
586
+ * @category Traversing
587
+ * @example <caption>Selector</caption>
588
+ *
589
+ * ```js
590
+ * $('li').not('.apple').length;
591
+ * //=> 2
592
+ * ```
593
+ *
594
+ * @example <caption>Function</caption>
595
+ *
596
+ * ```js
597
+ * $('li').not(function (i, el) {
598
+ * // this === el
599
+ * return $(this).attr('class') === 'orange';
600
+ * }).length; //=> 2
601
+ * ```
602
+ *
603
+ * @param match - Value to look for, following the rules above.
604
+ * @returns The filtered collection.
605
+ * @see {@link https://api.jquery.com/not/}
606
+ */
607
+ export function not(match) {
608
+ let nodes = this.toArray();
609
+ if (typeof match === 'string') {
610
+ const matches = new Set(select.filter(match, nodes, this.options));
611
+ nodes = nodes.filter((el) => !matches.has(el));
612
+ }
613
+ else {
614
+ const filterFn = getFilterFn(match);
615
+ nodes = nodes.filter((el, i) => !filterFn(el, i));
616
+ }
617
+ return this._make(nodes);
618
+ }
619
+ /**
620
+ * Filters the set of matched elements to only those which have the given DOM
621
+ * element as a descendant or which have a descendant that matches the given
622
+ * selector. Equivalent to `.filter(':has(selector)')`.
623
+ *
624
+ * @category Traversing
625
+ * @example <caption>Selector</caption>
626
+ *
627
+ * ```js
628
+ * $('ul').has('.pear').attr('id');
629
+ * //=> fruits
630
+ * ```
631
+ *
632
+ * @example <caption>Element</caption>
633
+ *
634
+ * ```js
635
+ * $('ul').has($('.pear')[0]).attr('id');
636
+ * //=> fruits
637
+ * ```
638
+ *
639
+ * @param selectorOrHaystack - Element to look for.
640
+ * @returns The filtered collection.
641
+ * @see {@link https://api.jquery.com/has/}
642
+ */
643
+ export function has(selectorOrHaystack) {
644
+ return this.filter(typeof selectorOrHaystack === 'string'
645
+ ? // Using the `:has` selector here short-circuits searches.
646
+ `:has(${selectorOrHaystack})`
647
+ : (_, el) => this._make(el).find(selectorOrHaystack).length > 0);
648
+ }
649
+ /**
650
+ * Will select the first element of a cheerio object.
651
+ *
652
+ * @category Traversing
653
+ * @example
654
+ *
655
+ * ```js
656
+ * $('#fruits').children().first().text();
657
+ * //=> Apple
658
+ * ```
659
+ *
660
+ * @returns The first element.
661
+ * @see {@link https://api.jquery.com/first/}
662
+ */
663
+ export function first() {
664
+ return this.length > 1 ? this._make(this[0]) : this;
665
+ }
666
+ /**
667
+ * Will select the last element of a cheerio object.
668
+ *
669
+ * @category Traversing
670
+ * @example
671
+ *
672
+ * ```js
673
+ * $('#fruits').children().last().text();
674
+ * //=> Pear
675
+ * ```
676
+ *
677
+ * @returns The last element.
678
+ * @see {@link https://api.jquery.com/last/}
679
+ */
680
+ export function last() {
681
+ return this.length > 0 ? this._make(this[this.length - 1]) : this;
682
+ }
683
+ /**
684
+ * Reduce the set of matched elements to the one at the specified index. Use
685
+ * `.eq(-i)` to count backwards from the last selected element.
686
+ *
687
+ * @category Traversing
688
+ * @example
689
+ *
690
+ * ```js
691
+ * $('li').eq(0).text();
692
+ * //=> Apple
693
+ *
694
+ * $('li').eq(-1).text();
695
+ * //=> Pear
696
+ * ```
697
+ *
698
+ * @param i - Index of the element to select.
699
+ * @returns The element at the `i`th position.
700
+ * @see {@link https://api.jquery.com/eq/}
701
+ */
702
+ export function eq(i) {
703
+ var _a;
704
+ i = +i;
705
+ // Use the first identity optimization if possible
706
+ if (i === 0 && this.length <= 1)
707
+ return this;
708
+ if (i < 0)
709
+ i = this.length + i;
710
+ return this._make((_a = this[i]) !== null && _a !== void 0 ? _a : []);
711
+ }
712
+ export function get(i) {
713
+ if (i == null) {
714
+ return this.toArray();
715
+ }
716
+ return this[i < 0 ? this.length + i : i];
717
+ }
718
+ /**
719
+ * Retrieve all the DOM elements contained in the jQuery set as an array.
720
+ *
721
+ * @example
722
+ *
723
+ * ```js
724
+ * $('li').toArray();
725
+ * //=> [ {...}, {...}, {...} ]
726
+ * ```
727
+ *
728
+ * @returns The contained items.
729
+ */
730
+ export function toArray() {
731
+ return Array.prototype.slice.call(this);
732
+ }
733
+ /**
734
+ * Search for a given element from among the matched elements.
735
+ *
736
+ * @category Traversing
737
+ * @example
738
+ *
739
+ * ```js
740
+ * $('.pear').index();
741
+ * //=> 2 $('.orange').index('li');
742
+ * //=> 1
743
+ * $('.apple').index($('#fruit, li'));
744
+ * //=> 1
745
+ * ```
746
+ *
747
+ * @param selectorOrNeedle - Element to look for.
748
+ * @returns The index of the element.
749
+ * @see {@link https://api.jquery.com/index/}
750
+ */
751
+ export function index(selectorOrNeedle) {
752
+ let $haystack;
753
+ let needle;
754
+ if (selectorOrNeedle == null) {
755
+ $haystack = this.parent().children();
756
+ needle = this[0];
757
+ }
758
+ else if (typeof selectorOrNeedle === 'string') {
759
+ $haystack = this._make(selectorOrNeedle);
760
+ needle = this[0];
761
+ }
762
+ else {
763
+ // eslint-disable-next-line @typescript-eslint/no-this-alias, unicorn/no-this-assignment
764
+ $haystack = this;
765
+ needle = isCheerio(selectorOrNeedle)
766
+ ? selectorOrNeedle[0]
767
+ : selectorOrNeedle;
768
+ }
769
+ return Array.prototype.indexOf.call($haystack, needle);
770
+ }
771
+ /**
772
+ * Gets the elements matching the specified range (0-based position).
773
+ *
774
+ * @category Traversing
775
+ * @example
776
+ *
777
+ * ```js
778
+ * $('li').slice(1).eq(0).text();
779
+ * //=> 'Orange'
780
+ *
781
+ * $('li').slice(1, 2).length;
782
+ * //=> 1
783
+ * ```
784
+ *
785
+ * @param start - A position at which the elements begin to be selected. If
786
+ * negative, it indicates an offset from the end of the set.
787
+ * @param end - A position at which the elements stop being selected. If
788
+ * negative, it indicates an offset from the end of the set. If omitted, the
789
+ * range continues until the end of the set.
790
+ * @returns The elements matching the specified range.
791
+ * @see {@link https://api.jquery.com/slice/}
792
+ */
793
+ export function slice(start, end) {
794
+ return this._make(Array.prototype.slice.call(this, start, end));
795
+ }
796
+ /**
797
+ * End the most recent filtering operation in the current chain and return the
798
+ * set of matched elements to its previous state.
799
+ *
800
+ * @category Traversing
801
+ * @example
802
+ *
803
+ * ```js
804
+ * $('li').eq(0).end().length;
805
+ * //=> 3
806
+ * ```
807
+ *
808
+ * @returns The previous state of the set of matched elements.
809
+ * @see {@link https://api.jquery.com/end/}
810
+ */
811
+ export function end() {
812
+ var _a;
813
+ return (_a = this.prevObject) !== null && _a !== void 0 ? _a : this._make([]);
814
+ }
815
+ /**
816
+ * Add elements to the set of matched elements.
817
+ *
818
+ * @category Traversing
819
+ * @example
820
+ *
821
+ * ```js
822
+ * $('.apple').add('.orange').length;
823
+ * //=> 2
824
+ * ```
825
+ *
826
+ * @param other - Elements to add.
827
+ * @param context - Optionally the context of the new selection.
828
+ * @returns The combined set.
829
+ * @see {@link https://api.jquery.com/add/}
830
+ */
831
+ export function add(other, context) {
832
+ const selection = this._make(other, context);
833
+ const contents = uniqueSort([...this.get(), ...selection.get()]);
834
+ return this._make(contents);
835
+ }
836
+ /**
837
+ * Add the previous set of elements on the stack to the current set, optionally
838
+ * filtered by a selector.
839
+ *
840
+ * @category Traversing
841
+ * @example
842
+ *
843
+ * ```js
844
+ * $('li').eq(0).addBack('.orange').length;
845
+ * //=> 2
846
+ * ```
847
+ *
848
+ * @param selector - Selector for the elements to add.
849
+ * @returns The combined set.
850
+ * @see {@link https://api.jquery.com/addBack/}
851
+ */
852
+ export function addBack(selector) {
853
+ return this.prevObject
854
+ ? this.add(selector ? this.prevObject.filter(selector) : this.prevObject)
855
+ : this;
856
+ }
857
+ //# sourceMappingURL=traversing.js.map