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