@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,850 @@
1
+ "use strict";
2
+ /**
3
+ * Methods for modifying the DOM structure.
4
+ *
5
+ * @module cheerio/manipulation
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.wrapInner = exports.wrap = exports.prepend = exports.append = void 0;
9
+ exports._makeDomArray = _makeDomArray;
10
+ exports.appendTo = appendTo;
11
+ exports.prependTo = prependTo;
12
+ exports.unwrap = unwrap;
13
+ exports.wrapAll = wrapAll;
14
+ exports.after = after;
15
+ exports.insertAfter = insertAfter;
16
+ exports.before = before;
17
+ exports.insertBefore = insertBefore;
18
+ exports.remove = remove;
19
+ exports.replaceWith = replaceWith;
20
+ exports.empty = empty;
21
+ exports.html = html;
22
+ exports.toString = toString;
23
+ exports.text = text;
24
+ exports.clone = clone;
25
+ const domhandler_1 = require("domhandler");
26
+ const parse_js_1 = require("../parse.js");
27
+ const static_js_1 = require("../static.js");
28
+ const utils_js_1 = require("../utils.js");
29
+ const domutils_1 = require("domutils");
30
+ const htmlparser2_1 = require("htmlparser2");
31
+ /**
32
+ * Create an array of nodes, recursing into arrays and parsing strings if
33
+ * necessary.
34
+ *
35
+ * @private
36
+ * @category Manipulation
37
+ * @param elem - Elements to make an array of.
38
+ * @param clone - Optionally clone nodes.
39
+ * @returns The array of nodes.
40
+ */
41
+ function _makeDomArray(elem, clone) {
42
+ if (elem == null) {
43
+ return [];
44
+ }
45
+ if (typeof elem === 'string') {
46
+ return this._parse(elem, this.options, false, null).children.slice(0);
47
+ }
48
+ if ('length' in elem) {
49
+ if (elem.length === 1) {
50
+ return this._makeDomArray(elem[0], clone);
51
+ }
52
+ const result = [];
53
+ for (let i = 0; i < elem.length; i++) {
54
+ const el = elem[i];
55
+ if (typeof el === 'object') {
56
+ if (el == null) {
57
+ continue;
58
+ }
59
+ if (!('length' in el)) {
60
+ result.push(clone ? (0, domhandler_1.cloneNode)(el, true) : el);
61
+ continue;
62
+ }
63
+ }
64
+ result.push(...this._makeDomArray(el, clone));
65
+ }
66
+ return result;
67
+ }
68
+ return [clone ? (0, domhandler_1.cloneNode)(elem, true) : elem];
69
+ }
70
+ function _insert(concatenator) {
71
+ return function (...elems) {
72
+ const lastIdx = this.length - 1;
73
+ return (0, utils_js_1.domEach)(this, (el, i) => {
74
+ if (!(0, domhandler_1.hasChildren)(el))
75
+ return;
76
+ const domSrc = typeof elems[0] === 'function'
77
+ ? elems[0].call(el, i, this._render(el.children))
78
+ : elems;
79
+ const dom = this._makeDomArray(domSrc, i < lastIdx);
80
+ concatenator(dom, el.children, el);
81
+ });
82
+ };
83
+ }
84
+ /**
85
+ * Modify an array in-place, removing some number of elements and adding new
86
+ * elements directly following them.
87
+ *
88
+ * @private
89
+ * @category Manipulation
90
+ * @param array - Target array to splice.
91
+ * @param spliceIdx - Index at which to begin changing the array.
92
+ * @param spliceCount - Number of elements to remove from the array.
93
+ * @param newElems - Elements to insert into the array.
94
+ * @param parent - The parent of the node.
95
+ * @returns The spliced array.
96
+ */
97
+ function uniqueSplice(array, spliceIdx, spliceCount, newElems, parent) {
98
+ var _a, _b;
99
+ const spliceArgs = [
100
+ spliceIdx,
101
+ spliceCount,
102
+ ...newElems,
103
+ ];
104
+ const prev = spliceIdx === 0 ? null : array[spliceIdx - 1];
105
+ const next = spliceIdx + spliceCount >= array.length
106
+ ? null
107
+ : array[spliceIdx + spliceCount];
108
+ /*
109
+ * Before splicing in new elements, ensure they do not already appear in the
110
+ * current array.
111
+ */
112
+ for (let idx = 0; idx < newElems.length; ++idx) {
113
+ const node = newElems[idx];
114
+ const oldParent = node.parent;
115
+ if (oldParent) {
116
+ const oldSiblings = oldParent.children;
117
+ const prevIdx = oldSiblings.indexOf(node);
118
+ if (prevIdx !== -1) {
119
+ oldParent.children.splice(prevIdx, 1);
120
+ if (parent === oldParent && spliceIdx > prevIdx) {
121
+ spliceArgs[0]--;
122
+ }
123
+ }
124
+ }
125
+ node.parent = parent;
126
+ if (node.prev) {
127
+ node.prev.next = (_a = node.next) !== null && _a !== void 0 ? _a : null;
128
+ }
129
+ if (node.next) {
130
+ node.next.prev = (_b = node.prev) !== null && _b !== void 0 ? _b : null;
131
+ }
132
+ node.prev = idx === 0 ? prev : newElems[idx - 1];
133
+ node.next = idx === newElems.length - 1 ? next : newElems[idx + 1];
134
+ }
135
+ if (prev) {
136
+ prev.next = newElems[0];
137
+ }
138
+ if (next) {
139
+ next.prev = newElems[newElems.length - 1];
140
+ }
141
+ return array.splice(...spliceArgs);
142
+ }
143
+ /**
144
+ * Insert every element in the set of matched elements to the end of the target.
145
+ *
146
+ * @category Manipulation
147
+ * @example
148
+ *
149
+ * ```js
150
+ * $('<li class="plum">Plum</li>').appendTo('#fruits');
151
+ * $.html();
152
+ * //=> <ul id="fruits">
153
+ * // <li class="apple">Apple</li>
154
+ * // <li class="orange">Orange</li>
155
+ * // <li class="pear">Pear</li>
156
+ * // <li class="plum">Plum</li>
157
+ * // </ul>
158
+ * ```
159
+ *
160
+ * @param target - Element to append elements to.
161
+ * @returns The instance itself.
162
+ * @see {@link https://api.jquery.com/appendTo/}
163
+ */
164
+ function appendTo(target) {
165
+ const appendTarget = (0, utils_js_1.isCheerio)(target) ? target : this._make(target);
166
+ appendTarget.append(this);
167
+ return this;
168
+ }
169
+ /**
170
+ * Insert every element in the set of matched elements to the beginning of the
171
+ * target.
172
+ *
173
+ * @category Manipulation
174
+ * @example
175
+ *
176
+ * ```js
177
+ * $('<li class="plum">Plum</li>').prependTo('#fruits');
178
+ * $.html();
179
+ * //=> <ul id="fruits">
180
+ * // <li class="plum">Plum</li>
181
+ * // <li class="apple">Apple</li>
182
+ * // <li class="orange">Orange</li>
183
+ * // <li class="pear">Pear</li>
184
+ * // </ul>
185
+ * ```
186
+ *
187
+ * @param target - Element to prepend elements to.
188
+ * @returns The instance itself.
189
+ * @see {@link https://api.jquery.com/prependTo/}
190
+ */
191
+ function prependTo(target) {
192
+ const prependTarget = (0, utils_js_1.isCheerio)(target) ? target : this._make(target);
193
+ prependTarget.prepend(this);
194
+ return this;
195
+ }
196
+ /**
197
+ * Inserts content as the _last_ child of each of the selected elements.
198
+ *
199
+ * @category Manipulation
200
+ * @example
201
+ *
202
+ * ```js
203
+ * $('ul').append('<li class="plum">Plum</li>');
204
+ * $.html();
205
+ * //=> <ul id="fruits">
206
+ * // <li class="apple">Apple</li>
207
+ * // <li class="orange">Orange</li>
208
+ * // <li class="pear">Pear</li>
209
+ * // <li class="plum">Plum</li>
210
+ * // </ul>
211
+ * ```
212
+ *
213
+ * @see {@link https://api.jquery.com/append/}
214
+ */
215
+ exports.append = _insert((dom, children, parent) => {
216
+ uniqueSplice(children, children.length, 0, dom, parent);
217
+ });
218
+ /**
219
+ * Inserts content as the _first_ child of each of the selected elements.
220
+ *
221
+ * @category Manipulation
222
+ * @example
223
+ *
224
+ * ```js
225
+ * $('ul').prepend('<li class="plum">Plum</li>');
226
+ * $.html();
227
+ * //=> <ul id="fruits">
228
+ * // <li class="plum">Plum</li>
229
+ * // <li class="apple">Apple</li>
230
+ * // <li class="orange">Orange</li>
231
+ * // <li class="pear">Pear</li>
232
+ * // </ul>
233
+ * ```
234
+ *
235
+ * @see {@link https://api.jquery.com/prepend/}
236
+ */
237
+ exports.prepend = _insert((dom, children, parent) => {
238
+ uniqueSplice(children, 0, 0, dom, parent);
239
+ });
240
+ function _wrap(insert) {
241
+ return function (wrapper) {
242
+ const lastIdx = this.length - 1;
243
+ const lastParent = this.parents().last();
244
+ for (let i = 0; i < this.length; i++) {
245
+ const el = this[i];
246
+ const wrap = typeof wrapper === 'function'
247
+ ? wrapper.call(el, i, el)
248
+ : typeof wrapper === 'string' && !(0, utils_js_1.isHtml)(wrapper)
249
+ ? lastParent.find(wrapper).clone()
250
+ : wrapper;
251
+ const [wrapperDom] = this._makeDomArray(wrap, i < lastIdx);
252
+ if (!wrapperDom || !(0, domhandler_1.hasChildren)(wrapperDom))
253
+ continue;
254
+ let elInsertLocation = wrapperDom;
255
+ /*
256
+ * Find the deepest child. Only consider the first tag child of each node
257
+ * (ignore text); stop if no children are found.
258
+ */
259
+ let j = 0;
260
+ while (j < elInsertLocation.children.length) {
261
+ const child = elInsertLocation.children[j];
262
+ if ((0, domhandler_1.isTag)(child)) {
263
+ elInsertLocation = child;
264
+ j = 0;
265
+ }
266
+ else {
267
+ j++;
268
+ }
269
+ }
270
+ insert(el, elInsertLocation, [wrapperDom]);
271
+ }
272
+ return this;
273
+ };
274
+ }
275
+ /**
276
+ * The .wrap() function can take any string or object that could be passed to
277
+ * the $() factory function to specify a DOM structure. This structure may be
278
+ * nested several levels deep, but should contain only one inmost element. A
279
+ * copy of this structure will be wrapped around each of the elements in the set
280
+ * of matched elements. This method returns the original set of elements for
281
+ * chaining purposes.
282
+ *
283
+ * @category Manipulation
284
+ * @example
285
+ *
286
+ * ```js
287
+ * const redFruit = $('<div class="red-fruit"></div>');
288
+ * $('.apple').wrap(redFruit);
289
+ *
290
+ * //=> <ul id="fruits">
291
+ * // <div class="red-fruit">
292
+ * // <li class="apple">Apple</li>
293
+ * // </div>
294
+ * // <li class="orange">Orange</li>
295
+ * // <li class="plum">Plum</li>
296
+ * // </ul>
297
+ *
298
+ * const healthy = $('<div class="healthy"></div>');
299
+ * $('li').wrap(healthy);
300
+ *
301
+ * //=> <ul id="fruits">
302
+ * // <div class="healthy">
303
+ * // <li class="apple">Apple</li>
304
+ * // </div>
305
+ * // <div class="healthy">
306
+ * // <li class="orange">Orange</li>
307
+ * // </div>
308
+ * // <div class="healthy">
309
+ * // <li class="plum">Plum</li>
310
+ * // </div>
311
+ * // </ul>
312
+ * ```
313
+ *
314
+ * @param wrapper - The DOM structure to wrap around each element in the
315
+ * selection.
316
+ * @see {@link https://api.jquery.com/wrap/}
317
+ */
318
+ exports.wrap = _wrap((el, elInsertLocation, wrapperDom) => {
319
+ const { parent } = el;
320
+ if (!parent)
321
+ return;
322
+ const siblings = parent.children;
323
+ const index = siblings.indexOf(el);
324
+ (0, parse_js_1.update)([el], elInsertLocation);
325
+ /*
326
+ * The previous operation removed the current element from the `siblings`
327
+ * array, so the `dom` array can be inserted without removing any
328
+ * additional elements.
329
+ */
330
+ uniqueSplice(siblings, index, 0, wrapperDom, parent);
331
+ });
332
+ /**
333
+ * The .wrapInner() function can take any string or object that could be passed
334
+ * to the $() factory function to specify a DOM structure. This structure may be
335
+ * nested several levels deep, but should contain only one inmost element. The
336
+ * structure will be wrapped around the content of each of the elements in the
337
+ * set of matched elements.
338
+ *
339
+ * @category Manipulation
340
+ * @example
341
+ *
342
+ * ```js
343
+ * const redFruit = $('<div class="red-fruit"></div>');
344
+ * $('.apple').wrapInner(redFruit);
345
+ *
346
+ * //=> <ul id="fruits">
347
+ * // <li class="apple">
348
+ * // <div class="red-fruit">Apple</div>
349
+ * // </li>
350
+ * // <li class="orange">Orange</li>
351
+ * // <li class="pear">Pear</li>
352
+ * // </ul>
353
+ *
354
+ * const healthy = $('<div class="healthy"></div>');
355
+ * $('li').wrapInner(healthy);
356
+ *
357
+ * //=> <ul id="fruits">
358
+ * // <li class="apple">
359
+ * // <div class="healthy">Apple</div>
360
+ * // </li>
361
+ * // <li class="orange">
362
+ * // <div class="healthy">Orange</div>
363
+ * // </li>
364
+ * // <li class="pear">
365
+ * // <div class="healthy">Pear</div>
366
+ * // </li>
367
+ * // </ul>
368
+ * ```
369
+ *
370
+ * @param wrapper - The DOM structure to wrap around the content of each element
371
+ * in the selection.
372
+ * @returns The instance itself, for chaining.
373
+ * @see {@link https://api.jquery.com/wrapInner/}
374
+ */
375
+ exports.wrapInner = _wrap((el, elInsertLocation, wrapperDom) => {
376
+ if (!(0, domhandler_1.hasChildren)(el))
377
+ return;
378
+ (0, parse_js_1.update)(el.children, elInsertLocation);
379
+ (0, parse_js_1.update)(wrapperDom, el);
380
+ });
381
+ /**
382
+ * The .unwrap() function, removes the parents of the set of matched elements
383
+ * from the DOM, leaving the matched elements in their place.
384
+ *
385
+ * @category Manipulation
386
+ * @example <caption>without selector</caption>
387
+ *
388
+ * ```js
389
+ * const $ = cheerio.load(
390
+ * '<div id=test>\n <div><p>Hello</p></div>\n <div><p>World</p></div>\n</div>',
391
+ * );
392
+ * $('#test p').unwrap();
393
+ *
394
+ * //=> <div id=test>
395
+ * // <p>Hello</p>
396
+ * // <p>World</p>
397
+ * // </div>
398
+ * ```
399
+ *
400
+ * @example <caption>with selector</caption>
401
+ *
402
+ * ```js
403
+ * const $ = cheerio.load(
404
+ * '<div id=test>\n <p>Hello</p>\n <b><p>World</p></b>\n</div>',
405
+ * );
406
+ * $('#test p').unwrap('b');
407
+ *
408
+ * //=> <div id=test>
409
+ * // <p>Hello</p>
410
+ * // <p>World</p>
411
+ * // </div>
412
+ * ```
413
+ *
414
+ * @param selector - A selector to check the parent element against. If an
415
+ * element's parent does not match the selector, the element won't be
416
+ * unwrapped.
417
+ * @returns The instance itself, for chaining.
418
+ * @see {@link https://api.jquery.com/unwrap/}
419
+ */
420
+ function unwrap(selector) {
421
+ this.parent(selector)
422
+ .not('body')
423
+ .each((_, el) => {
424
+ this._make(el).replaceWith(el.children);
425
+ });
426
+ return this;
427
+ }
428
+ /**
429
+ * The .wrapAll() function can take any string or object that could be passed to
430
+ * the $() function to specify a DOM structure. This structure may be nested
431
+ * several levels deep, but should contain only one inmost element. The
432
+ * structure will be wrapped around all of the elements in the set of matched
433
+ * elements, as a single group.
434
+ *
435
+ * @category Manipulation
436
+ * @example <caption>With markup passed to `wrapAll`</caption>
437
+ *
438
+ * ```js
439
+ * const $ = cheerio.load(
440
+ * '<div class="container"><div class="inner">First</div><div class="inner">Second</div></div>',
441
+ * );
442
+ * $('.inner').wrapAll("<div class='new'></div>");
443
+ *
444
+ * //=> <div class="container">
445
+ * // <div class='new'>
446
+ * // <div class="inner">First</div>
447
+ * // <div class="inner">Second</div>
448
+ * // </div>
449
+ * // </div>
450
+ * ```
451
+ *
452
+ * @example <caption>With an existing cheerio instance</caption>
453
+ *
454
+ * ```js
455
+ * const $ = cheerio.load(
456
+ * '<span>Span 1</span><strong>Strong</strong><span>Span 2</span>',
457
+ * );
458
+ * const wrap = $('<div><p><em><b></b></em></p></div>');
459
+ * $('span').wrapAll(wrap);
460
+ *
461
+ * //=> <div>
462
+ * // <p>
463
+ * // <em>
464
+ * // <b>
465
+ * // <span>Span 1</span>
466
+ * // <span>Span 2</span>
467
+ * // </b>
468
+ * // </em>
469
+ * // </p>
470
+ * // </div>
471
+ * // <strong>Strong</strong>
472
+ * ```
473
+ *
474
+ * @param wrapper - The DOM structure to wrap around all matched elements in the
475
+ * selection.
476
+ * @returns The instance itself.
477
+ * @see {@link https://api.jquery.com/wrapAll/}
478
+ */
479
+ function wrapAll(wrapper) {
480
+ const el = this[0];
481
+ if (el) {
482
+ const wrap = this._make(typeof wrapper === 'function' ? wrapper.call(el, 0, el) : wrapper).insertBefore(el);
483
+ // If html is given as wrapper, wrap may contain text elements
484
+ let elInsertLocation;
485
+ for (let i = 0; i < wrap.length; i++) {
486
+ if (wrap[i].type === htmlparser2_1.ElementType.Tag) {
487
+ elInsertLocation = wrap[i];
488
+ }
489
+ }
490
+ let j = 0;
491
+ /*
492
+ * Find the deepest child. Only consider the first tag child of each node
493
+ * (ignore text); stop if no children are found.
494
+ */
495
+ while (elInsertLocation && j < elInsertLocation.children.length) {
496
+ const child = elInsertLocation.children[j];
497
+ if (child.type === htmlparser2_1.ElementType.Tag) {
498
+ elInsertLocation = child;
499
+ j = 0;
500
+ }
501
+ else {
502
+ j++;
503
+ }
504
+ }
505
+ if (elInsertLocation)
506
+ this._make(elInsertLocation).append(this);
507
+ }
508
+ return this;
509
+ }
510
+ /**
511
+ * Insert content next to each element in the set of matched elements.
512
+ *
513
+ * @category Manipulation
514
+ * @example
515
+ *
516
+ * ```js
517
+ * $('.apple').after('<li class="plum">Plum</li>');
518
+ * $.html();
519
+ * //=> <ul id="fruits">
520
+ * // <li class="apple">Apple</li>
521
+ * // <li class="plum">Plum</li>
522
+ * // <li class="orange">Orange</li>
523
+ * // <li class="pear">Pear</li>
524
+ * // </ul>
525
+ * ```
526
+ *
527
+ * @param elems - HTML string, DOM element, array of DOM elements or Cheerio to
528
+ * insert after each element in the set of matched elements.
529
+ * @returns The instance itself.
530
+ * @see {@link https://api.jquery.com/after/}
531
+ */
532
+ function after(...elems) {
533
+ const lastIdx = this.length - 1;
534
+ return (0, utils_js_1.domEach)(this, (el, i) => {
535
+ if (!(0, domhandler_1.hasChildren)(el) || !el.parent) {
536
+ return;
537
+ }
538
+ const siblings = el.parent.children;
539
+ const index = siblings.indexOf(el);
540
+ // If not found, move on
541
+ /* istanbul ignore next */
542
+ if (index === -1)
543
+ return;
544
+ const domSrc = typeof elems[0] === 'function'
545
+ ? elems[0].call(el, i, this._render(el.children))
546
+ : elems;
547
+ const dom = this._makeDomArray(domSrc, i < lastIdx);
548
+ // Add element after `this` element
549
+ uniqueSplice(siblings, index + 1, 0, dom, el.parent);
550
+ });
551
+ }
552
+ /**
553
+ * Insert every element in the set of matched elements after the target.
554
+ *
555
+ * @category Manipulation
556
+ * @example
557
+ *
558
+ * ```js
559
+ * $('<li class="plum">Plum</li>').insertAfter('.apple');
560
+ * $.html();
561
+ * //=> <ul id="fruits">
562
+ * // <li class="apple">Apple</li>
563
+ * // <li class="plum">Plum</li>
564
+ * // <li class="orange">Orange</li>
565
+ * // <li class="pear">Pear</li>
566
+ * // </ul>
567
+ * ```
568
+ *
569
+ * @param target - Element to insert elements after.
570
+ * @returns The set of newly inserted elements.
571
+ * @see {@link https://api.jquery.com/insertAfter/}
572
+ */
573
+ function insertAfter(target) {
574
+ if (typeof target === 'string') {
575
+ target = this._make(target);
576
+ }
577
+ this.remove();
578
+ const clones = [];
579
+ for (const el of this._makeDomArray(target)) {
580
+ const clonedSelf = this.clone().toArray();
581
+ const { parent } = el;
582
+ if (!parent) {
583
+ continue;
584
+ }
585
+ const siblings = parent.children;
586
+ const index = siblings.indexOf(el);
587
+ // If not found, move on
588
+ /* istanbul ignore next */
589
+ if (index === -1)
590
+ continue;
591
+ // Add cloned `this` element(s) after target element
592
+ uniqueSplice(siblings, index + 1, 0, clonedSelf, parent);
593
+ clones.push(...clonedSelf);
594
+ }
595
+ return this._make(clones);
596
+ }
597
+ /**
598
+ * Insert content previous to each element in the set of matched elements.
599
+ *
600
+ * @category Manipulation
601
+ * @example
602
+ *
603
+ * ```js
604
+ * $('.apple').before('<li class="plum">Plum</li>');
605
+ * $.html();
606
+ * //=> <ul id="fruits">
607
+ * // <li class="plum">Plum</li>
608
+ * // <li class="apple">Apple</li>
609
+ * // <li class="orange">Orange</li>
610
+ * // <li class="pear">Pear</li>
611
+ * // </ul>
612
+ * ```
613
+ *
614
+ * @param elems - HTML string, DOM element, array of DOM elements or Cheerio to
615
+ * insert before each element in the set of matched elements.
616
+ * @returns The instance itself.
617
+ * @see {@link https://api.jquery.com/before/}
618
+ */
619
+ function before(...elems) {
620
+ const lastIdx = this.length - 1;
621
+ return (0, utils_js_1.domEach)(this, (el, i) => {
622
+ if (!(0, domhandler_1.hasChildren)(el) || !el.parent) {
623
+ return;
624
+ }
625
+ const siblings = el.parent.children;
626
+ const index = siblings.indexOf(el);
627
+ // If not found, move on
628
+ /* istanbul ignore next */
629
+ if (index === -1)
630
+ return;
631
+ const domSrc = typeof elems[0] === 'function'
632
+ ? elems[0].call(el, i, this._render(el.children))
633
+ : elems;
634
+ const dom = this._makeDomArray(domSrc, i < lastIdx);
635
+ // Add element before `el` element
636
+ uniqueSplice(siblings, index, 0, dom, el.parent);
637
+ });
638
+ }
639
+ /**
640
+ * Insert every element in the set of matched elements before the target.
641
+ *
642
+ * @category Manipulation
643
+ * @example
644
+ *
645
+ * ```js
646
+ * $('<li class="plum">Plum</li>').insertBefore('.apple');
647
+ * $.html();
648
+ * //=> <ul id="fruits">
649
+ * // <li class="plum">Plum</li>
650
+ * // <li class="apple">Apple</li>
651
+ * // <li class="orange">Orange</li>
652
+ * // <li class="pear">Pear</li>
653
+ * // </ul>
654
+ * ```
655
+ *
656
+ * @param target - Element to insert elements before.
657
+ * @returns The set of newly inserted elements.
658
+ * @see {@link https://api.jquery.com/insertBefore/}
659
+ */
660
+ function insertBefore(target) {
661
+ const targetArr = this._make(target);
662
+ this.remove();
663
+ const clones = [];
664
+ (0, utils_js_1.domEach)(targetArr, (el) => {
665
+ const clonedSelf = this.clone().toArray();
666
+ const { parent } = el;
667
+ if (!parent) {
668
+ return;
669
+ }
670
+ const siblings = parent.children;
671
+ const index = siblings.indexOf(el);
672
+ // If not found, move on
673
+ /* istanbul ignore next */
674
+ if (index === -1)
675
+ return;
676
+ // Add cloned `this` element(s) after target element
677
+ uniqueSplice(siblings, index, 0, clonedSelf, parent);
678
+ clones.push(...clonedSelf);
679
+ });
680
+ return this._make(clones);
681
+ }
682
+ /**
683
+ * Removes the set of matched elements from the DOM and all their children.
684
+ * `selector` filters the set of matched elements to be removed.
685
+ *
686
+ * @category Manipulation
687
+ * @example
688
+ *
689
+ * ```js
690
+ * $('.pear').remove();
691
+ * $.html();
692
+ * //=> <ul id="fruits">
693
+ * // <li class="apple">Apple</li>
694
+ * // <li class="orange">Orange</li>
695
+ * // </ul>
696
+ * ```
697
+ *
698
+ * @param selector - Optional selector for elements to remove.
699
+ * @returns The instance itself.
700
+ * @see {@link https://api.jquery.com/remove/}
701
+ */
702
+ function remove(selector) {
703
+ // Filter if we have selector
704
+ const elems = selector ? this.filter(selector) : this;
705
+ (0, utils_js_1.domEach)(elems, (el) => {
706
+ (0, domutils_1.removeElement)(el);
707
+ el.prev = el.next = el.parent = null;
708
+ });
709
+ return this;
710
+ }
711
+ /**
712
+ * Replaces matched elements with `content`.
713
+ *
714
+ * @category Manipulation
715
+ * @example
716
+ *
717
+ * ```js
718
+ * const plum = $('<li class="plum">Plum</li>');
719
+ * $('.pear').replaceWith(plum);
720
+ * $.html();
721
+ * //=> <ul id="fruits">
722
+ * // <li class="apple">Apple</li>
723
+ * // <li class="orange">Orange</li>
724
+ * // <li class="plum">Plum</li>
725
+ * // </ul>
726
+ * ```
727
+ *
728
+ * @param content - Replacement for matched elements.
729
+ * @returns The instance itself.
730
+ * @see {@link https://api.jquery.com/replaceWith/}
731
+ */
732
+ function replaceWith(content) {
733
+ return (0, utils_js_1.domEach)(this, (el, i) => {
734
+ const { parent } = el;
735
+ if (!parent) {
736
+ return;
737
+ }
738
+ const siblings = parent.children;
739
+ const cont = typeof content === 'function' ? content.call(el, i, el) : content;
740
+ const dom = this._makeDomArray(cont);
741
+ /*
742
+ * In the case that `dom` contains nodes that already exist in other
743
+ * structures, ensure those nodes are properly removed.
744
+ */
745
+ (0, parse_js_1.update)(dom, null);
746
+ const index = siblings.indexOf(el);
747
+ // Completely remove old element
748
+ uniqueSplice(siblings, index, 1, dom, parent);
749
+ if (!dom.includes(el)) {
750
+ el.parent = el.prev = el.next = null;
751
+ }
752
+ });
753
+ }
754
+ /**
755
+ * Removes all children from each item in the selection. Text nodes and comment
756
+ * nodes are left as is.
757
+ *
758
+ * @category Manipulation
759
+ * @example
760
+ *
761
+ * ```js
762
+ * $('ul').empty();
763
+ * $.html();
764
+ * //=> <ul id="fruits"></ul>
765
+ * ```
766
+ *
767
+ * @returns The instance itself.
768
+ * @see {@link https://api.jquery.com/empty/}
769
+ */
770
+ function empty() {
771
+ return (0, utils_js_1.domEach)(this, (el) => {
772
+ if (!(0, domhandler_1.hasChildren)(el))
773
+ return;
774
+ for (const child of el.children) {
775
+ child.next = child.prev = child.parent = null;
776
+ }
777
+ el.children.length = 0;
778
+ });
779
+ }
780
+ function html(str) {
781
+ if (str === undefined) {
782
+ const el = this[0];
783
+ if (!el || !(0, domhandler_1.hasChildren)(el))
784
+ return null;
785
+ return this._render(el.children);
786
+ }
787
+ return (0, utils_js_1.domEach)(this, (el) => {
788
+ if (!(0, domhandler_1.hasChildren)(el))
789
+ return;
790
+ for (const child of el.children) {
791
+ child.next = child.prev = child.parent = null;
792
+ }
793
+ const content = (0, utils_js_1.isCheerio)(str)
794
+ ? str.toArray()
795
+ : this._parse(`${str}`, this.options, false, el).children;
796
+ (0, parse_js_1.update)(content, el);
797
+ });
798
+ }
799
+ /**
800
+ * Turns the collection to a string. Alias for `.html()`.
801
+ *
802
+ * @category Manipulation
803
+ * @returns The rendered document.
804
+ */
805
+ function toString() {
806
+ return this._render(this);
807
+ }
808
+ function text(str) {
809
+ // If `str` is undefined, act as a "getter"
810
+ if (str === undefined) {
811
+ return (0, static_js_1.text)(this);
812
+ }
813
+ if (typeof str === 'function') {
814
+ // Function support
815
+ return (0, utils_js_1.domEach)(this, (el, i) => this._make(el).text(str.call(el, i, (0, static_js_1.text)([el]))));
816
+ }
817
+ // Append text node to each selected elements
818
+ return (0, utils_js_1.domEach)(this, (el) => {
819
+ if (!(0, domhandler_1.hasChildren)(el))
820
+ return;
821
+ for (const child of el.children) {
822
+ child.next = child.prev = child.parent = null;
823
+ }
824
+ const textNode = new domhandler_1.Text(`${str}`);
825
+ (0, parse_js_1.update)(textNode, el);
826
+ });
827
+ }
828
+ /**
829
+ * Clone the cheerio object.
830
+ *
831
+ * @category Manipulation
832
+ * @example
833
+ *
834
+ * ```js
835
+ * const moreFruit = $('#fruits').clone();
836
+ * ```
837
+ *
838
+ * @returns The cloned object.
839
+ * @see {@link https://api.jquery.com/clone/}
840
+ */
841
+ function clone() {
842
+ const clone = Array.prototype.map.call(this.get(), (el) => (0, domhandler_1.cloneNode)(el, true));
843
+ // Add a root node around the cloned nodes
844
+ const root = new domhandler_1.Document(clone);
845
+ for (const node of clone) {
846
+ node.parent = root;
847
+ }
848
+ return this._make(clone);
849
+ }
850
+ //# sourceMappingURL=manipulation.js.map