@dvaji/cheerox 0.0.0-stage → 0.1.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 (268) hide show
  1. package/CONTRIBUTING.md +74 -0
  2. package/Cargo.lock +1091 -0
  3. package/Cargo.toml +47 -0
  4. package/LICENSE +21 -0
  5. package/README.md +121 -2
  6. package/RELEASING.md +114 -0
  7. package/THIRD_PARTY_NOTICES.md +7581 -0
  8. package/build.rs +3 -0
  9. package/cheerox.darwin-arm64.node +0 -0
  10. package/cheerox.darwin-x64.node +0 -0
  11. package/cheerox.linux-arm64-gnu.node +0 -0
  12. package/cheerox.linux-arm64-musl.node +0 -0
  13. package/cheerox.linux-x64-gnu.node +0 -0
  14. package/cheerox.linux-x64-musl.node +0 -0
  15. package/cheerox.win32-arm64-msvc.node +0 -0
  16. package/cheerox.win32-x64-msvc.node +0 -0
  17. package/dist/cheerio-compat-BKkfJEAr.mjs +1648 -0
  18. package/dist/cheerio-compat-BKkfJEAr.mjs.map +1 -0
  19. package/dist/cheerio-compat-DRLPzBdX.d.mts +88 -0
  20. package/dist/cheerio-compat-DRsINTwV.d.cts +88 -0
  21. package/dist/cheerio-compat-DRsINTwV.d.ts +88 -0
  22. package/dist/cheerio-compat-DcnYK9OB.cjs +1719 -0
  23. package/dist/cheerio-compat-DcnYK9OB.cjs.map +1 -0
  24. package/dist/cheerio-compat.cjs +18 -0
  25. package/dist/cheerio-compat.d.cts +2 -0
  26. package/dist/cheerio-compat.d.mts +2 -0
  27. package/dist/cheerio-compat.d.ts +2 -0
  28. package/dist/cheerio-compat.mjs +1 -0
  29. package/dist/index.cjs +13 -0
  30. package/dist/index.cjs.map +1 -0
  31. package/dist/index.d.cts +28 -0
  32. package/dist/index.d.mts +6 -0
  33. package/dist/index.d.ts +28 -0
  34. package/dist/index.mjs +53 -0
  35. package/dist/index.mjs.map +1 -0
  36. package/dist/legacy-parse.cjs +5 -0
  37. package/dist/legacy-parse.d.mts +8 -0
  38. package/dist/legacy-parse.d.ts +7 -0
  39. package/dist/legacy-parse.mjs +1 -0
  40. package/dist/modern-B7hyW4cI.d.cts +102 -0
  41. package/dist/modern-B7hyW4cI.d.ts +102 -0
  42. package/dist/modern-CHQGP0Yt.cjs +2858 -0
  43. package/dist/modern-CHQGP0Yt.cjs.map +1 -0
  44. package/dist/modern-CJvSD_y5.d.mts +102 -0
  45. package/dist/modern-CvXcTn82.mjs +2769 -0
  46. package/dist/modern-CvXcTn82.mjs.map +1 -0
  47. package/dist/modern.cjs +19 -0
  48. package/dist/modern.d.cts +3 -0
  49. package/dist/modern.d.mts +3 -0
  50. package/dist/modern.d.ts +3 -0
  51. package/dist/modern.mjs +3 -0
  52. package/dist/nodes-jbWWhssj.cjs +4210 -0
  53. package/dist/nodes-jbWWhssj.cjs.map +1 -0
  54. package/dist/nodes-sYxb5qyZ.mjs +3803 -0
  55. package/dist/nodes-sYxb5qyZ.mjs.map +1 -0
  56. package/dist/slim.cjs +16 -0
  57. package/dist/slim.cjs.map +1 -0
  58. package/dist/slim.d.cts +6 -0
  59. package/dist/slim.d.mts +6 -0
  60. package/dist/slim.d.ts +6 -0
  61. package/dist/slim.mjs +12 -0
  62. package/dist/slim.mjs.map +1 -0
  63. package/dist/types-CA01Zg3r.d.cts +526 -0
  64. package/dist/types-CA01Zg3r.d.mts +526 -0
  65. package/dist/types-CA01Zg3r.d.ts +526 -0
  66. package/dist/utils.cjs +31 -0
  67. package/dist/utils.cjs.map +1 -0
  68. package/dist/utils.d.cts +8 -0
  69. package/dist/utils.d.mts +8 -0
  70. package/dist/utils.d.ts +8 -0
  71. package/dist/utils.mjs +25 -0
  72. package/dist/utils.mjs.map +1 -0
  73. package/native/src/arena.rs +2109 -0
  74. package/native/src/attributes.rs +946 -0
  75. package/native/src/codec.rs +298 -0
  76. package/native/src/diagnostics.rs +1249 -0
  77. package/native/src/document_order.rs +711 -0
  78. package/native/src/extraction.rs +321 -0
  79. package/native/src/gc.rs +175 -0
  80. package/native/src/hook_sink.rs +801 -0
  81. package/native/src/hook_stream.rs +513 -0
  82. package/native/src/id_hash.rs +42 -0
  83. package/native/src/immutable.rs +333 -0
  84. package/native/src/lib.rs +4120 -0
  85. package/native/src/location.rs +392 -0
  86. package/native/src/markup_tokenizer.rs +668 -0
  87. package/native/src/runtime.rs +368 -0
  88. package/native/src/selector.rs +1596 -0
  89. package/native/src/sink.rs +274 -0
  90. package/native/src/snapshot.rs +672 -0
  91. package/native/src/stream.rs +334 -0
  92. package/native/src/tokenizer.rs +581 -0
  93. package/native/src/tracker.rs +232 -0
  94. package/native/vendor/README.md +35 -0
  95. package/native/vendor/html5ever-0.35.0/.cargo_vcs_info.json +6 -0
  96. package/native/vendor/html5ever-0.35.0/Cargo.lock +828 -0
  97. package/native/vendor/html5ever-0.35.0/Cargo.toml +85 -0
  98. package/native/vendor/html5ever-0.35.0/LICENSE-APACHE +201 -0
  99. package/native/vendor/html5ever-0.35.0/LICENSE-MIT +25 -0
  100. package/native/vendor/html5ever-0.35.0/README.md +60 -0
  101. package/native/vendor/html5ever-0.35.0/benches/html5ever.rs +86 -0
  102. package/native/vendor/html5ever-0.35.0/data/bench/lipsum-zh.html +19 -0
  103. package/native/vendor/html5ever-0.35.0/data/bench/lipsum.html +40 -0
  104. package/native/vendor/html5ever-0.35.0/data/bench/medium-fragment.html +24 -0
  105. package/native/vendor/html5ever-0.35.0/data/bench/small-fragment.html +7 -0
  106. package/native/vendor/html5ever-0.35.0/data/bench/strong.html +1 -0
  107. package/native/vendor/html5ever-0.35.0/data/bench/tiny-fragment.html +1 -0
  108. package/native/vendor/html5ever-0.35.0/examples/arena.rs +356 -0
  109. package/native/vendor/html5ever-0.35.0/examples/capi/tokenize.c +74 -0
  110. package/native/vendor/html5ever-0.35.0/examples/noop-tokenize.rs +47 -0
  111. package/native/vendor/html5ever-0.35.0/examples/noop-tree-builder.rs +132 -0
  112. package/native/vendor/html5ever-0.35.0/examples/print-tree-actions.rs +185 -0
  113. package/native/vendor/html5ever-0.35.0/examples/tokenize.rs +111 -0
  114. package/native/vendor/html5ever-0.35.0/src/driver.rs +148 -0
  115. package/native/vendor/html5ever-0.35.0/src/lib.rs +30 -0
  116. package/native/vendor/html5ever-0.35.0/src/macros.rs +34 -0
  117. package/native/vendor/html5ever-0.35.0/src/serialize/mod.rs +255 -0
  118. package/native/vendor/html5ever-0.35.0/src/tokenizer/char_ref/mod.rs +530 -0
  119. package/native/vendor/html5ever-0.35.0/src/tokenizer/interface.rs +99 -0
  120. package/native/vendor/html5ever-0.35.0/src/tokenizer/mod.rs +2628 -0
  121. package/native/vendor/html5ever-0.35.0/src/tokenizer/states.rs +97 -0
  122. package/native/vendor/html5ever-0.35.0/src/tree_builder/data.rs +170 -0
  123. package/native/vendor/html5ever-0.35.0/src/tree_builder/mod.rs +1848 -0
  124. package/native/vendor/html5ever-0.35.0/src/tree_builder/rules.rs +1494 -0
  125. package/native/vendor/html5ever-0.35.0/src/tree_builder/tag_sets.rs +114 -0
  126. package/native/vendor/html5ever-0.35.0/src/tree_builder/types.rs +91 -0
  127. package/native/vendor/html5ever-0.35.0/src/util/str.rs +52 -0
  128. package/native/vendor/html5gum-0.8.4/Cargo.lock +1006 -0
  129. package/native/vendor/html5gum-0.8.4/Cargo.toml +109 -0
  130. package/native/vendor/html5gum-0.8.4/LICENSE +19 -0
  131. package/native/vendor/html5gum-0.8.4/README.md +110 -0
  132. package/native/vendor/html5gum-0.8.4/benches/files/0050.html +2131 -0
  133. package/native/vendor/html5gum-0.8.4/benches/files/6D30.html +1004 -0
  134. package/native/vendor/html5gum-0.8.4/benches/files.rs +32 -0
  135. package/native/vendor/html5gum-0.8.4/benches/patterns.rs +31 -0
  136. package/native/vendor/html5gum-0.8.4/src/arrayvec.rs +29 -0
  137. package/native/vendor/html5gum-0.8.4/src/char_validator.rs +134 -0
  138. package/native/vendor/html5gum-0.8.4/src/emitters/callback.rs +676 -0
  139. package/native/vendor/html5gum-0.8.4/src/emitters/default.rs +207 -0
  140. package/native/vendor/html5gum-0.8.4/src/emitters/emitter.rs +344 -0
  141. package/native/vendor/html5gum-0.8.4/src/emitters/html5ever.rs +306 -0
  142. package/native/vendor/html5gum-0.8.4/src/emitters/mod.rs +25 -0
  143. package/native/vendor/html5gum-0.8.4/src/entities.rs +3342 -0
  144. package/native/vendor/html5gum-0.8.4/src/error.rs +96 -0
  145. package/native/vendor/html5gum-0.8.4/src/htmlstring.rs +130 -0
  146. package/native/vendor/html5gum-0.8.4/src/lib.rs +66 -0
  147. package/native/vendor/html5gum-0.8.4/src/machine.rs +2136 -0
  148. package/native/vendor/html5gum-0.8.4/src/machine_helper.rs +277 -0
  149. package/native/vendor/html5gum-0.8.4/src/read_helper.rs +244 -0
  150. package/native/vendor/html5gum-0.8.4/src/reader.rs +440 -0
  151. package/native/vendor/html5gum-0.8.4/src/span.rs +98 -0
  152. package/native/vendor/html5gum-0.8.4/src/state.rs +16 -0
  153. package/native/vendor/html5gum-0.8.4/src/testutils.rs +71 -0
  154. package/native/vendor/html5gum-0.8.4/src/tokenizer.rs +189 -0
  155. package/native/vendor/html5gum-0.8.4/src/utils.rs +88 -0
  156. package/native/vendor/markup5ever-0.35.0/.cargo_vcs_info.json +6 -0
  157. package/native/vendor/markup5ever-0.35.0/Cargo.toml +45 -0
  158. package/native/vendor/markup5ever-0.35.0/LICENSE-APACHE +201 -0
  159. package/native/vendor/markup5ever-0.35.0/LICENSE-MIT +25 -0
  160. package/native/vendor/markup5ever-0.35.0/interface/mod.rs +422 -0
  161. package/native/vendor/markup5ever-0.35.0/interface/tree_builder.rs +280 -0
  162. package/native/vendor/markup5ever-0.35.0/lib.rs +59 -0
  163. package/native/vendor/markup5ever-0.35.0/serialize.rs +77 -0
  164. package/native/vendor/markup5ever-0.35.0/util/buffer_queue.rs +333 -0
  165. package/native/vendor/markup5ever-0.35.0/util/smallcharset.rs +88 -0
  166. package/native/vendor/scraper-0.24.0/Cargo.toml +52 -0
  167. package/native/vendor/scraper-0.24.0/LICENSE +15 -0
  168. package/native/vendor/scraper-0.24.0/src/element_ref/element.rs +212 -0
  169. package/native/vendor/scraper-0.24.0/src/element_ref/mod.rs +236 -0
  170. package/native/vendor/scraper-0.24.0/src/element_ref/serializable.rs +15 -0
  171. package/native/vendor/scraper-0.24.0/src/error/utils.rs +91 -0
  172. package/native/vendor/scraper-0.24.0/src/error.rs +118 -0
  173. package/native/vendor/scraper-0.24.0/src/html/mod.rs +270 -0
  174. package/native/vendor/scraper-0.24.0/src/html/serializable.rs +27 -0
  175. package/native/vendor/scraper-0.24.0/src/html/tree_sink.rs +285 -0
  176. package/native/vendor/scraper-0.24.0/src/lib.rs +180 -0
  177. package/native/vendor/scraper-0.24.0/src/node/serializable.rs +52 -0
  178. package/native/vendor/scraper-0.24.0/src/node.rs +465 -0
  179. package/native/vendor/scraper-0.24.0/src/selectable.rs +70 -0
  180. package/native/vendor/scraper-0.24.0/src/selector.rs +290 -0
  181. package/package.json +167 -4
  182. package/scripts/check-native.mjs +15 -0
  183. package/scripts/check-release.mjs +234 -0
  184. package/scripts/fix-declarations.mjs +39 -0
  185. package/scripts/generate-notices.mjs +194 -0
  186. package/scripts/notices/README.md +13 -0
  187. package/scripts/notices/cheerio-MIT.txt +21 -0
  188. package/scripts/notices/html5ever-AUTHORS.txt +20 -0
  189. package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
  190. package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
  191. package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
  192. package/scripts/notices/htmlparser2-MIT.txt +18 -0
  193. package/scripts/notices/napi-rs-MIT.txt +43 -0
  194. package/scripts/notices/neon-MIT.txt +19 -0
  195. package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
  196. package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
  197. package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
  198. package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
  199. package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
  200. package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
  201. package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
  202. package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
  203. package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
  204. package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
  205. package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
  206. package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
  207. package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
  208. package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
  209. package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
  210. package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
  211. package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
  212. package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
  213. package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
  214. package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
  215. package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
  216. package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
  217. package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
  218. package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
  219. package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
  220. package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
  221. package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
  222. package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
  223. package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
  224. package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
  225. package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
  226. package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
  227. package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
  228. package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
  229. package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
  230. package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
  231. package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
  232. package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
  233. package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
  234. package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
  235. package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
  236. package/scripts/notices/runtime.json +1632 -0
  237. package/scripts/notices/sources.json +561 -0
  238. package/scripts/notices/xray-MIT.txt +21 -0
  239. package/src/attribute-object.ts +71 -0
  240. package/src/attributes.ts +308 -0
  241. package/src/cheerio-compat/css.ts +58 -0
  242. package/src/cheerio-compat/render.ts +106 -0
  243. package/src/cheerio-compat/selectors.ts +51 -0
  244. package/src/cheerio-compat/types.ts +52 -0
  245. package/src/cheerio-compat.ts +2055 -0
  246. package/src/codec.ts +96 -0
  247. package/src/dom.ts +87 -0
  248. package/src/extract-plan.ts +97 -0
  249. package/src/extract.ts +322 -0
  250. package/src/hooks.ts +369 -0
  251. package/src/index.ts +48 -0
  252. package/src/io.ts +491 -0
  253. package/src/match.ts +207 -0
  254. package/src/modern.ts +259 -0
  255. package/src/native.ts +149 -0
  256. package/src/nodes.ts +831 -0
  257. package/src/parse.ts +53 -0
  258. package/src/parser-types.ts +157 -0
  259. package/src/router.ts +1129 -0
  260. package/src/selection.ts +682 -0
  261. package/src/slim.ts +12 -0
  262. package/src/snapshot.ts +291 -0
  263. package/src/transport.ts +113 -0
  264. package/src/types.ts +186 -0
  265. package/src/utils.ts +29 -0
  266. package/src/workers.ts +21 -0
  267. package/tsconfig.json +15 -0
  268. package/tsdown.config.mjs +17 -0
@@ -0,0 +1,682 @@
1
+ import type { AnyNode, Document, Element, ParentNode, CheerioAPI, CheerioOptions, AcceptedFilters, BasicAcceptedElems, AcceptedElems, ExtractMap, ExtractedMap } from './types.js';
2
+ import { requireNative, type NativeDocument } from './native.js';
3
+ import { getNode, importNodes, nodeBinding, nodeId, nodeRecord, operation, isTag, traverseRecords, viewId } from './nodes.js';
4
+ import { installAttributes, type AttributeMethods } from './attributes.js';
5
+ import { extract } from './extract.js';
6
+ import { API, DOCUMENT, exposeDocumentTo, IDS, IS_PLAIN, LEASE, leaseNodes, prepareBulkViews, routeDocument, sameNodeSpace, updateLease, VALUES, type Lease } from './router.js';
7
+ import { parse } from './parse.js';
8
+ import { matchSimpleSelector, selectSimpleWithin } from './match.js';
9
+ const numeric = /^(?:0|[1-9]\d*)$/;
10
+ const isMarkup = (value: string) => /<[a-zA-Z!/?]/.test(value);
11
+ function* customPseudoArguments(selector: string, name: string): Generator<string | undefined> {
12
+ let attributeDepth = 0;
13
+ let quote = '';
14
+ for (let index = 0; index < selector.length; index++) {
15
+ const character = selector[index];
16
+ if (character === '\\') {
17
+ index++;
18
+ continue;
19
+ }
20
+ if (quote) {
21
+ if (character === quote)
22
+ quote = '';
23
+ continue;
24
+ }
25
+ if (character === '"' || character === "'") {
26
+ quote = character;
27
+ continue;
28
+ }
29
+ if (character === '[') {
30
+ attributeDepth++;
31
+ continue;
32
+ }
33
+ if (character === ']') {
34
+ attributeDepth--;
35
+ continue;
36
+ }
37
+ if (attributeDepth || character !== ':' || selector[index - 1] === ':')
38
+ continue;
39
+ const start = index + 1;
40
+ let end = start;
41
+ while (/[\w-]/.test(selector[end] ?? '') && end < selector.length)
42
+ end++;
43
+ if (selector.slice(start, end).toLowerCase() !== name.toLowerCase())
44
+ continue;
45
+ index = end - 1;
46
+ if (selector[end] !== '(') {
47
+ yield undefined;
48
+ continue;
49
+ }
50
+ const argumentStart = end + 1;
51
+ let depth = 1;
52
+ let argumentQuote = '';
53
+ for (end++; end < selector.length; end++) {
54
+ const next = selector[end];
55
+ if (next === '\\') {
56
+ end++;
57
+ continue;
58
+ }
59
+ if (argumentQuote) {
60
+ if (next === argumentQuote)
61
+ argumentQuote = '';
62
+ continue;
63
+ }
64
+ if (next === '"' || next === "'") {
65
+ argumentQuote = next;
66
+ continue;
67
+ }
68
+ if (next === '(')
69
+ depth++;
70
+ else if (next === ')' && --depth === 0)
71
+ break;
72
+ }
73
+ if (depth)
74
+ continue; // The native selector parser reports malformed syntax.
75
+ yield selector.slice(argumentStart, end);
76
+ index = end;
77
+ }
78
+ }
79
+ const isIndex = (property: string | symbol): boolean => {
80
+ if (typeof property !== 'string') return false;
81
+ const first = property.charCodeAt(0);
82
+ return first >= 48 && first <= 57 && numeric.test(property);
83
+ };
84
+ /** Results up to this size are plain objects holding their nodes, like Cheerio's own selections. */
85
+ const plainSelectionLimit = 5;
86
+ const lengthOf = (target: Cheerio<any>): number => target[VALUES]?.length ?? target[IDS].length;
87
+ function nodeAt(target: Cheerio<any>, index: number): any {
88
+ if (target[VALUES]) return target[VALUES][index];
89
+ const id = target[IDS][index];
90
+ return id === undefined ? undefined : getNode(target[DOCUMENT], id);
91
+ }
92
+ function releaseIds(target: Cheerio<any>): void {
93
+ if (target[LEASE]) updateLease(target[DOCUMENT], target[LEASE], target[VALUES] ? [] : target[IDS]);
94
+ }
95
+ function writeIndex(target: Cheerio<any>, index: number, value: any): void {
96
+ if (target[VALUES]) target[VALUES][index] = value;
97
+ else {
98
+ const id = importNodes(target[DOCUMENT], [value])[0];
99
+ if (id === undefined) {
100
+ target[VALUES] = target[IDS].map((existing) => getNode(target[DOCUMENT], existing));
101
+ target[VALUES][index] = value;
102
+ }
103
+ else target[IDS][index] = id;
104
+ }
105
+ releaseIds(target);
106
+ }
107
+ /** Large selections resolve nodes only when an index is read. */
108
+ const selectionHandler: ProxyHandler<Cheerio<any>> = {
109
+ get: (target, property) => isIndex(property) ? nodeAt(target, Number(property)) : property === 'length' ? lengthOf(target) : Reflect.get(target, property),
110
+ set: (target, property, value) => {
111
+ if (isIndex(property)) { writeIndex(target, Number(property), value); return true; }
112
+ if (property === 'length') {
113
+ if (target[VALUES]) target[VALUES].length = value;
114
+ else target[IDS].length = value;
115
+ releaseIds(target);
116
+ return true;
117
+ }
118
+ return Reflect.set(target, property, value);
119
+ },
120
+ has: (target, property) => isIndex(property) ? Number(property) < lengthOf(target) : property === 'length' || Reflect.has(target, property),
121
+ ownKeys: (target) => [...Array.from({ length: lengthOf(target) }, (_, i) => String(i)), 'length', ...Reflect.ownKeys(target)],
122
+ getOwnPropertyDescriptor: (target, property) => {
123
+ if (isIndex(property) && Number(property) < lengthOf(target))
124
+ return { value: nodeAt(target, Number(property)), writable: true, configurable: true, enumerable: true };
125
+ if (property === 'length') return { value: lengthOf(target), writable: true, configurable: true, enumerable: true };
126
+ return Reflect.getOwnPropertyDescriptor(target, property);
127
+ },
128
+ deleteProperty: (target, property) => {
129
+ if (isIndex(property)) {
130
+ if (target[VALUES])
131
+ delete target[VALUES][Number(property)];
132
+ else
133
+ delete target[IDS][Number(property)];
134
+ releaseIds(target);
135
+ return true;
136
+ }
137
+ return Reflect.deleteProperty(target, property);
138
+ },
139
+ };
140
+ /** An array-like selection of native nodes. */
141
+ export class Cheerio<T = AnyNode> implements ArrayLike<T>, Iterable<T> {
142
+ [index: number]: T;
143
+ /** @internal */ declare [DOCUMENT]: NativeDocument;
144
+ /** @internal */ declare [API]: CheerioAPI;
145
+ /** @internal */ declare [IDS]: number[];
146
+ /** @internal */ declare [VALUES]: T[] | undefined;
147
+ /** @internal */ declare [LEASE]: Lease | undefined;
148
+ /** @internal Whether the selection holds its items as own index properties, as Cheerio's selections do. */ declare [IS_PLAIN]: boolean;
149
+ /** @internal */ get _document(): NativeDocument { return this[DOCUMENT]; }
150
+ /** @internal */ get _api(): CheerioAPI { return this[API]; }
151
+ /** @internal */ get _ids(): number[] { return this[IDS]; }
152
+ declare length: number;
153
+ declare options: CheerioOptions;
154
+ declare _root: Cheerio<Document> | null;
155
+ declare prevObject: Cheerio<any> | undefined;
156
+ declare cheerio: '[cheerio object]';
157
+ constructor(api: CheerioAPI, document: NativeDocument, ids: number[] = [], root: Cheerio<Document> | null = null, options: CheerioOptions = {}, values?: T[]) {
158
+ this[DOCUMENT] = document;
159
+ this[API] = api;
160
+ this[IDS] = ids;
161
+ this[VALUES] = values;
162
+ this[LEASE] = undefined;
163
+ const count = values ? values.length : ids.length;
164
+ const isPlain = count <= plainSelectionLimit;
165
+ this[IS_PLAIN] = isPlain;
166
+ // Own keys enumerate in Cheerio's order: length, options, _root.
167
+ if (isPlain) this.length = count;
168
+ this.options = options;
169
+ this._root = root;
170
+ if (isPlain) {
171
+ // Each node object keeps its own node alive, so a plain selection needs no lease.
172
+ if (values) for (let index = 0; index < count; index++) this[index] = values[index];
173
+ else for (let index = 0; index < count; index++) this[index] = getNode(document, ids[index]) as T;
174
+ return;
175
+ }
176
+ if (!values) this[LEASE] = leaseNodes(document, this, ids);
177
+ return new Proxy(this, selectionHandler);
178
+ }
179
+ /** @internal Re-reads a plain selection after direct index writes or after its nodes moved documents. */
180
+ _syncIndices(): void {
181
+ const length = this.length;
182
+ const values = this[VALUES];
183
+ const ids = this[IDS];
184
+ let isChanged = (values ?? ids).length !== length;
185
+ for (let index = 0; !isChanged && index < length; index++) isChanged = values ? this[index] !== values[index] : viewId(this[index]) !== ids[index];
186
+ if (!isChanged) return;
187
+ const items: T[] = Array.from({ length }, (_unused, index) => this[index]);
188
+ const isAllNodes = items.every((value: any) => value && typeof value === 'object' && typeof value.type === 'string');
189
+ this[VALUES] = isAllNodes ? undefined : items;
190
+ this[IDS] = isAllNodes ? importNodes(this[DOCUMENT], items) : [];
191
+ }
192
+ /** @internal */ get ids(): number[] {
193
+ if (this[IS_PLAIN]) this._syncIndices();
194
+ return this[VALUES] ? importNodes(this[DOCUMENT], this[VALUES]) : this[IDS];
195
+ }
196
+ /** @internal */ op<R = any>(command: string, args: unknown = {}): R { const values = args as Record<string, unknown>; return operation<R>(this[DOCUMENT], command, { ...values, options: values.options ?? this.options }); }
197
+ /** @internal */ _query(command: 'select' | 'matches', args: {
198
+ selector: string;
199
+ ids?: number[];
200
+ contexts?: number[];
201
+ options?: CheerioOptions;
202
+ }): number[] {
203
+ const pseudos = this.options.pseudos;
204
+ const callbacks = pseudos ? Object.entries(pseudos).filter((entry): entry is [
205
+ string,
206
+ (element: Element, argument?: string | null) => boolean
207
+ ] => typeof entry[1] === 'function') : [];
208
+ if (!callbacks.length) {
209
+ if (command === 'matches' && args.ids) {
210
+ const matched = matchSimpleSelector(this[DOCUMENT], args.ids, args.selector, args.options ?? this.options);
211
+ if (matched) return matched;
212
+ }
213
+ if (command === 'select' && args.contexts) {
214
+ const found = selectSimpleWithin(this[DOCUMENT], args.contexts, args.selector, args.options ?? this.options);
215
+ if (found) return found;
216
+ }
217
+ return command === 'select' && !args.options ? this[DOCUMENT].select(args.selector, args.contexts) : this.op(command, args);
218
+ }
219
+ const pseudoMatches: Record<string, number[]> = {};
220
+ const candidates = [...new Set([...this[DOCUMENT].select('*'), ...(args.ids ?? []), ...(args.contexts ? this[DOCUMENT].select('*', args.contexts) : [])])];
221
+ for (const [name, callback] of callbacks) {
222
+ for (const argument of customPseudoArguments(args.selector, name)) {
223
+ if (argument !== undefined && callback.length < 2)
224
+ throw new Error(`Pseudo-class :${name} doesn't have any arguments`);
225
+ if (argument === undefined && callback.length >= 2)
226
+ throw new Error(`Pseudo-class :${name} requires an argument`);
227
+ pseudoMatches[`${name}(${argument ?? ''})`] = candidates.filter((id) => callback(getNode(this[DOCUMENT], id) as Element, argument ?? null));
228
+ }
229
+ }
230
+ return this.op(command, { ...args, pseudoMatches });
231
+ }
232
+ /** @internal */ _fromIds<R = AnyNode>(ids: number[]): Cheerio<R> {
233
+ const result = new (this[API].fn.constructor as typeof Cheerio)<R>(this[API], this[DOCUMENT], ids, this._root, this.options);
234
+ result.prevObject = this;
235
+ return result;
236
+ }
237
+ /** @internal */ _fromValues<R>(values: R[]): Cheerio<R> {
238
+ const allNodes = values.every((value: any) => value && typeof value === 'object' && typeof value.type === 'string');
239
+ const result = new (this[API].fn.constructor as typeof Cheerio)<R>(this[API], this[DOCUMENT], allNodes ? importNodes(this[DOCUMENT], values) : [], this._root, this.options, allNodes ? undefined : values);
240
+ result.prevObject = this;
241
+ return result;
242
+ }
243
+ _make<R>(value: ArrayLike<R> | R | string, context?: BasicAcceptedElems<AnyNode>): Cheerio<R>;
244
+ _make<R>(value: any, context?: any): Cheerio<R> {
245
+ const result = typeof value === 'string' || value?.cheerio ? this[API](value, context) : this._fromValues<R>(value == null ? [] : typeof value === 'object' && 'length' in value && !value.type ? Array.from(value) : [value]);
246
+ result.prevObject = this;
247
+ return result as Cheerio<R>;
248
+ }
249
+ _parse(content: any, options: CheerioOptions, isDocument: boolean, context: ParentNode | null): Document {
250
+ return parse(content, options, isDocument, context);
251
+ }
252
+ _render(content: any): string { return this[DOCUMENT].outerHtml(importNodes(this[DOCUMENT], content)); }
253
+ /** @internal */ _domIds(content: any, contextId?: number): number[] {
254
+ if (content == null || content === false)
255
+ return [];
256
+ if (typeof content === 'string')
257
+ return this.op('create', { html: content, contextId, options: this.options });
258
+ if (content?.cheerio) {
259
+ if (!content[DOCUMENT] || !sameNodeSpace(this[DOCUMENT], content[DOCUMENT]))
260
+ return importNodes(this[DOCUMENT], content.toArray());
261
+ exposeDocumentTo(this[DOCUMENT], content[DOCUMENT]);
262
+ return content.ids;
263
+ }
264
+ if (typeof content === 'object' && 'length' in content && !content.type)
265
+ return Array.from(content as ArrayLike<any>).flatMap((item) => this._domIds(item, contextId));
266
+ return importNodes(this[DOCUMENT], [content]);
267
+ }
268
+ _makeDomArray<T extends AnyNode>(this: Cheerio<T>, elem?: BasicAcceptedElems<AnyNode> | BasicAcceptedElems<AnyNode>[], clone?: boolean): AnyNode[];
269
+ _makeDomArray(this: Cheerio<any>, content: any, clone: any = false): any {
270
+ const ids = this._domIds(content);
271
+ return (clone ? this.op<number[]>('clone', { ids, deep: true }) : ids).map((id) => getNode(this[DOCUMENT], id));
272
+ }
273
+ find<T extends AnyNode>(this: Cheerio<T>, selectorOrHaystack?: string | Cheerio<Element> | Element): Cheerio<Element>;
274
+ find(this: Cheerio<any>, selector?: any): any {
275
+ if (!selector)
276
+ return this._fromIds([]);
277
+ if (typeof selector === 'string')
278
+ return this._findBySelector(selector, Infinity);
279
+ const ids = importNodes(this[DOCUMENT], selector);
280
+ return this._fromIds(ids.filter((id) => this.ids.some((container) => this.op<boolean>('contains', { container, contained: id }))));
281
+ }
282
+ _findBySelector<T extends AnyNode>(this: Cheerio<T>, selector: string, limit: number): Cheerio<Element>;
283
+ _findBySelector(this: Cheerio<any>, selector: any, limit: any): any {
284
+ const ids = this._query('select', { selector, contexts: this.ids });
285
+ return this._fromIds(Number.isFinite(limit) ? ids.slice(0, limit) : ids);
286
+ }
287
+ /** @internal */ _filterIds(ids: number[], selector?: any): number[] {
288
+ if (!selector)
289
+ return ids;
290
+ if (typeof selector === 'string')
291
+ return this._query('matches', { ids, selector, options: this.options });
292
+ return ids.filter((id, index) => this._predicate(selector, getNode(this[DOCUMENT], id), index));
293
+ }
294
+ /** @internal */ _predicate(selector: any, node: any, index: number): boolean {
295
+ if (typeof selector === 'function')
296
+ return !!selector.call(node, index, node);
297
+ if (typeof selector === 'string')
298
+ return this._query('matches', { ids: importNodes(this[DOCUMENT], [node]), selector, options: this.options }).length > 0;
299
+ if (selector?.cheerio)
300
+ return selector.toArray().includes(node);
301
+ return node === selector;
302
+ }
303
+ /** @internal */ _traverse(kind: string, selector?: any): Cheerio<Element> {
304
+ let ids = traverseRecords(this[DOCUMENT], this.ids, kind) ?? this.op<number[]>('traverse', { ids: this.ids, kind });
305
+ ids = this._filterIds(ids, selector);
306
+ if (this.length > 1 && ids.length > 1) {
307
+ if (kind === 'parents' || kind === 'siblings')
308
+ ids = this.op('uniqueSort', { ids });
309
+ else if (!['next', 'prev', 'contents'].includes(kind))
310
+ ids = [...new Set(ids)];
311
+ if (kind === 'parents')
312
+ ids.reverse();
313
+ }
314
+ return this._fromIds(ids);
315
+ }
316
+ parent<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element>): Cheerio<Element>;
317
+ parent(this: Cheerio<any>, selector?: any): any { return this._traverse('parent', selector); }
318
+ parents<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element>): Cheerio<Element>;
319
+ parents(this: Cheerio<any>, selector?: any): any { return this._traverse('parents', selector); }
320
+ next<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element>): Cheerio<Element>;
321
+ next(this: Cheerio<any>, selector?: any): any { return this._traverse('next', selector); }
322
+ prev<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element>): Cheerio<Element>;
323
+ prev(this: Cheerio<any>, selector?: any): any { return this._traverse('prev', selector); }
324
+ nextAll<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element>): Cheerio<Element>;
325
+ nextAll(this: Cheerio<any>, selector?: any): any { return this._traverse('nextAll', selector); }
326
+ prevAll<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element>): Cheerio<Element>;
327
+ prevAll(this: Cheerio<any>, selector?: any): any { return this._traverse('prevAll', selector); }
328
+ siblings<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element>): Cheerio<Element>;
329
+ siblings(this: Cheerio<any>, selector?: any): any { return this._traverse('siblings', selector); }
330
+ children<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element>): Cheerio<Element>;
331
+ children(this: Cheerio<any>, selector?: any): any { return this._traverse('children', selector); }
332
+ contents<T extends AnyNode>(this: Cheerio<T>): Cheerio<AnyNode>;
333
+ contents(this: Cheerio<any>): any { return this._traverse('contents') as any; }
334
+ /** @internal */ _until(kind: string, until?: any, filter?: any): Cheerio<Element> {
335
+ let ids: number[] = [];
336
+ for (const id of this.ids)
337
+ for (const candidate of traverseRecords(this[DOCUMENT], [id], kind) ?? this.op<number[]>('traverse', { ids: [id], kind })) {
338
+ if (until && this._predicate(until, getNode(this[DOCUMENT], candidate), ids.length))
339
+ break;
340
+ ids.push(candidate);
341
+ }
342
+ ids = this._filterIds(ids, filter);
343
+ if (this.length > 1 && ids.length > 1)
344
+ ids = kind === 'parents' ? this.op<number[]>('uniqueSort', { ids }).reverse() : [...new Set(ids)];
345
+ return this._fromIds(ids);
346
+ }
347
+ parentsUntil<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | null, filterSelector?: AcceptedFilters<Element>): Cheerio<Element>;
348
+ parentsUntil(this: Cheerio<any>, until?: any, filter?: any): any { return this._until('parents', until, filter); }
349
+ nextUntil<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | null, filterSelector?: AcceptedFilters<Element>): Cheerio<Element>;
350
+ nextUntil(this: Cheerio<any>, until?: any, filter?: any): any { return this._until('nextAll', until, filter); }
351
+ prevUntil<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element> | null, filterSelector?: AcceptedFilters<Element>): Cheerio<Element>;
352
+ prevUntil(this: Cheerio<any>, until?: any, filter?: any): any { return this._until('prevAll', until, filter); }
353
+ closest<T extends AnyNode>(this: Cheerio<T>, selector?: AcceptedFilters<Element>): Cheerio<AnyNode>;
354
+ closest(this: Cheerio<any>, selector?: any): any {
355
+ if (!selector)
356
+ return this._fromIds([]);
357
+ const result: number[] = [];
358
+ for (const id of this.ids) {
359
+ const record = nodeRecord(this[DOCUMENT], id);
360
+ const candidates = [...(['tag', 'script', 'style'].includes(record.type) ? [id] : []), ...(traverseRecords(this[DOCUMENT], [id], 'parents') ?? this.op<number[]>('traverse', { ids: [id], kind: 'parents' }))];
361
+ // One native match for all candidates replaces a round trip per ancestor.
362
+ const matched = typeof selector === 'string' ? new Set(this._filterIds(candidates, selector)) : undefined;
363
+ const match = matched ? candidates.find((candidate) => matched.has(candidate)) : candidates.find((candidate) => this._predicate(selector, getNode(this[DOCUMENT], candidate), 0));
364
+ if (match !== undefined && !result.includes(match))
365
+ result.push(match);
366
+ }
367
+ return this._fromIds(result);
368
+ }
369
+ each<T>(this: Cheerio<T>, fn: (this: T, i: number, el: T) => void | boolean): Cheerio<T>;
370
+ each(this: Cheerio<any>, callback: any): any {
371
+ if (this[IS_PLAIN]) {
372
+ const length = this.length;
373
+ for (let i = 0; i < length; i++)
374
+ if (callback.call(this[i], i, this[i]) === false)
375
+ break;
376
+ return this;
377
+ }
378
+ // Reading ids directly avoids a string-keyed proxy trap per index.
379
+ const values = this[VALUES], ids = this[IDS], document = this[DOCUMENT];
380
+ const length = values ? values.length : ids.length;
381
+ for (let i = 0; i < length; i++) {
382
+ const node = values ? values[i] : getNode(document, ids[i]);
383
+ if (callback.call(node, i, node) === false)
384
+ break;
385
+ }
386
+ return this;
387
+ }
388
+ map<T, M>(this: Cheerio<T>, fn: (this: T, i: number, el: T) => M[] | M | null | undefined): Cheerio<M>;
389
+ map<R>(this: Cheerio<any>, callback: any): any {
390
+ const result: R[] = [];
391
+ const items = this._items();
392
+ for (let i = 0; i < items.length; i++) {
393
+ const value = callback.call(items[i], i, items[i]);
394
+ if (value != null)
395
+ result.push(...(Array.isArray(value) ? value : [value]));
396
+ }
397
+ return this._fromValues(result);
398
+ }
399
+ filter<T, S extends T>(this: Cheerio<T>, match: (this: T, index: number, value: T) => value is S): Cheerio<S>;
400
+ filter<T, S extends AcceptedFilters<T>>(this: Cheerio<T>, match: S): Cheerio<S extends string ? Element : T>;
401
+ filter<S extends AcceptedFilters<T>>(this: Cheerio<any>, selector: any): any {
402
+ return (typeof selector === 'string' ? this._fromIds(this._query('matches', { ids: this.ids, selector, options: this.options })) : this._fromValues(this.toArray().filter((node, i) => this._predicate(selector, node, i)))) as any;
403
+ }
404
+ filterArray<T>(nodes: T[], match: AcceptedFilters<T>, xmlMode?: boolean, root?: Document): Element[] | T[];
405
+ filterArray<R>(this: Cheerio<any>, nodes: any, match: any, xmlMode?: any, root?: any): any {
406
+ const matcher: any = match;
407
+ if (typeof match !== 'string')
408
+ return nodes.filter((node: any, index: number) => typeof matcher === 'function' ? matcher.call(node, index, node) : matcher?.cheerio ? matcher.toArray().includes(node) : matcher === node);
409
+ const document = this?.[DOCUMENT] ?? nodeBinding(root)?.document ?? nodeBinding(nodes[0])?.document ?? routeDocument(new (requireNative().NativeDocument)('', false));
410
+ const ids = importNodes(document, nodes);
411
+ const selected = new Set(operation<number[]>(document, 'matches', { ids, selector: match, options: { xmlMode } }));
412
+ return nodes.filter((node: any) => { const id = nodeId(node, document); return id !== undefined && selected.has(id); });
413
+ }
414
+ not<T extends AnyNode>(this: Cheerio<T>, match: AcceptedFilters<T>): Cheerio<T>;
415
+ not(this: Cheerio<any>, selector: any): any {
416
+ if (typeof selector === 'string') {
417
+ const matched = new Set(this._query('matches', { ids: this.ids, selector, options: this.options }));
418
+ return this._fromIds(this.ids.filter((id) => !matched.has(id)));
419
+ }
420
+ return this._fromValues(this.toArray().filter((node, i) => !this._predicate(selector, node, i)));
421
+ }
422
+ is<T>(this: Cheerio<T>, selector?: AcceptedFilters<T>): boolean;
423
+ is(this: Cheerio<any>, selector?: any): any {
424
+ return typeof selector === 'string' ? this._query('matches', { ids: this.ids, selector, options: this.options }).length > 0 : !!selector && this.toArray().some((node, i) => this._predicate(selector, node, i));
425
+ }
426
+ has(this: Cheerio<AnyNode | Element>, selectorOrHaystack: string | Cheerio<Element> | Element): Cheerio<AnyNode | Element>;
427
+ has(this: Cheerio<any>, selector: any): any { return this.filter(((_i: number, node: any) => this._make(node).find(selector).length > 0) as any) as Cheerio<T>; }
428
+ first<T extends AnyNode>(this: Cheerio<T>): Cheerio<T>;
429
+ first(this: Cheerio<any>): any { return this.length > 1 ? this.slice(0, 1) : this; }
430
+ last<T>(this: Cheerio<T>): Cheerio<T>;
431
+ last(this: Cheerio<any>): any { return this.length > 0 ? this.slice(-1) : this; }
432
+ eq<T>(this: Cheerio<T>, i: number): Cheerio<T>;
433
+ eq(this: Cheerio<any>, index: any): any {
434
+ index = +index;
435
+ if (index === 0 && this.length <= 1)
436
+ return this;
437
+ if (index < 0)
438
+ index += this.length;
439
+ const item = this._itemAt(index);
440
+ return this._fromValues(item === undefined ? [] : [item]);
441
+ }
442
+ get<T>(this: Cheerio<T>, i: number): T | undefined;
443
+ get<T>(this: Cheerio<T>): T[];
444
+ get(this: Cheerio<any>, index?: any): any { return index == null ? this.toArray() : this._itemAt(index < 0 ? this.length + index : index); }
445
+ toArray<T>(this: Cheerio<T>): T[];
446
+ toArray(this: Cheerio<any>): any { return this._items(); }
447
+ /** @internal Every item, read without a proxy trap per index. */
448
+ _items(): T[] {
449
+ if (this[IS_PLAIN]) {
450
+ const length = this.length;
451
+ const items: T[] = new Array(length);
452
+ for (let i = 0; i < length; i++) items[i] = this[i];
453
+ return items;
454
+ }
455
+ const values = this[VALUES], ids = this[IDS], document = this[DOCUMENT];
456
+ if (!values) prepareBulkViews(document, ids);
457
+ return values ? values.slice() : ids.map((id) => getNode(document, id) as T);
458
+ }
459
+ /** @internal */ _itemAt(index: number): T | undefined {
460
+ if (this[IS_PLAIN]) return this[index];
461
+ const values = this[VALUES];
462
+ if (values) return values[index];
463
+ const id = this[IDS][index];
464
+ return id === undefined ? undefined : getNode(this[DOCUMENT], id) as T;
465
+ }
466
+ slice<T>(this: Cheerio<T>, start?: number, end?: number): Cheerio<T>;
467
+ slice(this: Cheerio<any>, start?: any, end?: any): any { const ids = this.ids; return this[VALUES] ? this._fromValues(this[VALUES].slice(start, end)) : this._fromIds(ids.slice(start, end)); }
468
+ end<T>(this: Cheerio<T>): Cheerio<AnyNode>;
469
+ end(this: Cheerio<any>): any { return this.prevObject ?? this._make([]); }
470
+ add<S extends AnyNode, T extends AnyNode>(this: Cheerio<T>, other: string | Cheerio<S> | S | S[], context?: Cheerio<S> | string): Cheerio<S | T>;
471
+ add<R extends AnyNode>(this: Cheerio<any>, other: any, context?: any): any {
472
+ const added = this[API](other, context);
473
+ // Ids of another copy of the package belong to its own id space.
474
+ const addedIds = added[DOCUMENT] && sameNodeSpace(this[DOCUMENT], added[DOCUMENT]) ? added.ids : importNodes(this[DOCUMENT], added.toArray());
475
+ return this._fromIds(this.op('uniqueSort', { ids: [...this.ids, ...addedIds] }));
476
+ }
477
+ addBack<T extends AnyNode>(this: Cheerio<T>, selector?: string): Cheerio<AnyNode>;
478
+ addBack(this: Cheerio<any>, selector?: any): any { return this.prevObject ? this.add(selector ? this.prevObject.filter(selector) : this.prevObject) as any : this as any; }
479
+ index<T extends AnyNode>(this: Cheerio<T>, selectorOrNeedle?: string | Cheerio<AnyNode> | AnyNode): number;
480
+ index(this: Cheerio<any>, selector?: any): any { const haystack = selector == null ? this.parent().children() : typeof selector === 'string' ? this[API](selector) : this; const needle = selector == null || typeof selector === 'string' ? this[0] : selector?.cheerio ? selector[0] : selector; return haystack.toArray().indexOf(needle); }
481
+ splice(start: number, deleteCount?: number): any[];
482
+ splice(start: number, deleteCount: number, ...items: any[]): any[];
483
+ splice(...args: any[]): any[] { return Reflect.apply(Array.prototype.splice, this, args); }
484
+ [Symbol.iterator](): Iterator<T>;
485
+ [Symbol.iterator](): IterableIterator<T> { return this._items()[Symbol.iterator](); }
486
+ /** @internal */ _insert(position: string, content: any[]): this {
487
+ if (typeof content[0] !== 'function') {
488
+ // The native insert skips targets that cannot hold children, and
489
+ // only the last listed target receives the nodes themselves.
490
+ const ids = this.ids;
491
+ if (ids.length) this.op('insert', { targetIds: ids, nodeIds: this._domIds(content), position });
492
+ return this;
493
+ }
494
+ const targets = this.ids.filter((id) => ['root', 'tag', 'script', 'style', 'cdata'].includes(nodeRecord(this[DOCUMENT], id).type));
495
+ if (!targets.length)
496
+ return this;
497
+ return (this as Cheerio<any>).each((index, node: any) => {
498
+ if (!targets.includes(this.ids[index]))
499
+ return;
500
+ if ((position === 'before' || position === 'after') && nodeRecord(this[DOCUMENT], this.ids[index]).parent == null)
501
+ return;
502
+ const value = content[0].call(node, index, this[DOCUMENT].innerHtml(this.ids[index]));
503
+ let nodes = this._domIds(value);
504
+ if (index < this.length - 1)
505
+ nodes = this.op('clone', { ids: nodes, deep: true });
506
+ this.op('insert', { targetIds: [this.ids[index]], nodeIds: nodes, position });
507
+ }) as this;
508
+ }
509
+ append<T extends AnyNode>(this: Cheerio<T>, ...elems: [
510
+ (this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>
511
+ ] | BasicAcceptedElems<AnyNode>[]): Cheerio<T>;
512
+ append(this: Cheerio<any>, ...content: any[]): any { return this._insert('append', content); }
513
+ prepend<T extends AnyNode>(this: Cheerio<T>, ...elems: [
514
+ (this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>
515
+ ] | BasicAcceptedElems<AnyNode>[]): Cheerio<T>;
516
+ prepend(this: Cheerio<any>, ...content: any[]): any { return this._insert('prepend', content); }
517
+ before<T extends AnyNode>(this: Cheerio<T>, ...elems: [
518
+ (this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>
519
+ ] | BasicAcceptedElems<AnyNode>[]): Cheerio<T>;
520
+ before(this: Cheerio<any>, ...content: any[]): any { return this._insert('before', content); }
521
+ after<T extends AnyNode>(this: Cheerio<T>, ...elems: [
522
+ (this: AnyNode, i: number, html: string) => BasicAcceptedElems<AnyNode>
523
+ ] | BasicAcceptedElems<AnyNode>[]): Cheerio<T>;
524
+ after(this: Cheerio<any>, ...content: any[]): any { return this._insert('after', content); }
525
+ appendTo<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
526
+ appendTo(this: Cheerio<any>, target: any): any { this[API](target).append(this); return this; }
527
+ prependTo<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
528
+ prependTo(this: Cheerio<any>, target: any): any { this[API](target).prepend(this); return this; }
529
+ /** @internal */ _insertRelative(target: any, position: string): Cheerio<T> {
530
+ const targets = this[API](target).ids;
531
+ (this as Cheerio<any>).remove();
532
+ const inserted: number[] = [];
533
+ for (const id of targets) {
534
+ if (nodeRecord(this[DOCUMENT], id).parent == null)
535
+ continue;
536
+ const clones = this.op<number[]>('clone', { ids: this.ids, deep: true });
537
+ this.op('insert', { targetIds: [id], nodeIds: clones, position });
538
+ inserted.push(...clones);
539
+ }
540
+ return this._fromIds(inserted);
541
+ }
542
+ insertBefore<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
543
+ insertBefore(this: Cheerio<any>, target: any): any { return this._insertRelative(target, 'before'); }
544
+ insertAfter<T extends AnyNode>(this: Cheerio<T>, target: BasicAcceptedElems<AnyNode>): Cheerio<T>;
545
+ insertAfter(this: Cheerio<any>, target: any): any { return this._insertRelative(target, 'after'); }
546
+ remove<T extends AnyNode>(this: Cheerio<T>, selector?: string): Cheerio<T>;
547
+ remove(this: Cheerio<any>, selector?: any): any { this.op('remove', { ids: selector ? this.filter(selector).ids : this.ids }); return this; }
548
+ replaceWith<T extends AnyNode>(this: Cheerio<T>, content: AcceptedElems<AnyNode>): Cheerio<T>;
549
+ replaceWith(this: Cheerio<any>, content: any): any {
550
+ return this.each((index, node: any) => {
551
+ if (nodeRecord(this[DOCUMENT], this.ids[index]).parent == null)
552
+ return;
553
+ const value = typeof content === 'function' ? content.call(node, index, node) : content;
554
+ this.op('insert', { targetIds: [this.ids[index]], nodeIds: this._domIds(value), position: 'replace' });
555
+ });
556
+ }
557
+ empty<T extends AnyNode>(this: Cheerio<T>): Cheerio<T>;
558
+ empty(this: Cheerio<any>): any {
559
+ for (const id of this.ids)
560
+ this.op('setNode', { id, field: 'children', value: [] });
561
+ return this;
562
+ }
563
+ html<T extends AnyNode>(this: Cheerio<T>): string | null;
564
+ html<T extends AnyNode>(this: Cheerio<T>, str: string | Cheerio<T>): Cheerio<T>;
565
+ html(this: Cheerio<any>, content?: any): any {
566
+ if (content === undefined)
567
+ return this.length && ['root', 'tag', 'script', 'style', 'cdata'].includes(nodeRecord(this[DOCUMENT], this.ids[0]).type) ? this[DOCUMENT].innerHtml(this.ids[0]) : null;
568
+ if (!content?.cheerio) {
569
+ this.op('html', { ids: this.ids, value: String(content) });
570
+ return this;
571
+ }
572
+ for (const id of this.ids) {
573
+ const nodes = content?.cheerio ? importNodes(this[DOCUMENT], content.toArray()) : this._domIds(String(content), id);
574
+ this.op('setNode', { id, field: 'children', value: nodes });
575
+ }
576
+ return this;
577
+ }
578
+ text<T extends AnyNode>(this: Cheerio<T>): string;
579
+ text<T extends AnyNode>(this: Cheerio<T>, str: string | ((this: AnyNode, i: number, text: string) => string)): Cheerio<T>;
580
+ text(this: Cheerio<any>, content?: any): any {
581
+ if (content === undefined)
582
+ return this[DOCUMENT].text(this.ids);
583
+ if (typeof content !== 'function') {
584
+ this.op('text', { ids: this.ids, value: String(content) });
585
+ return this;
586
+ }
587
+ for (let i = 0; i < this.ids.length; i++) {
588
+ const id = this.ids[i];
589
+ const value = typeof content === 'function' ? content.call(getNode(this[DOCUMENT], id), i, this[DOCUMENT].text([id])) : content;
590
+ this.op('text', { ids: [id], value: String(value) });
591
+ }
592
+ return this;
593
+ }
594
+ toString<T extends AnyNode>(this: Cheerio<T>): string;
595
+ toString(this: Cheerio<any>): any { return this[DOCUMENT].outerHtml(this.ids); }
596
+ clone<T extends AnyNode>(this: Cheerio<T>): Cheerio<T>;
597
+ clone(this: Cheerio<any>): any { return this._fromIds(this.op('clone', { ids: this.ids, deep: true })); }
598
+ /** @internal */ _deepest(id: number, tagsOnly = false): number {
599
+ for (;;) {
600
+ const children = this.op<number[]>('traverse', { ids: [id], kind: 'children' });
601
+ const child = tagsOnly ? children.find((candidate) => nodeRecord(this[DOCUMENT], candidate).type === 'tag') : children[0];
602
+ if (child === undefined)
603
+ return id;
604
+ id = child;
605
+ }
606
+ }
607
+ /** @internal */ _wrap(content: any, inner: boolean): this {
608
+ const lastParent = (this as Cheerio<any>).parents().last();
609
+ for (let i = 0; i < this.length; i++) {
610
+ const id = this.ids[i];
611
+ const node = getNode(this[DOCUMENT], id);
612
+ const value = typeof content === 'function' ? content.call(node, i, node) : typeof content === 'string' && !isMarkup(content) ? lastParent.find(content).clone() : content;
613
+ let wrappers = this._domIds(value);
614
+ if (i < this.length - 1)
615
+ wrappers = this.op('clone', { ids: wrappers, deep: true });
616
+ const wrapper = wrappers[0];
617
+ if (wrapper === undefined || !['root', 'tag', 'script', 'style'].includes(nodeRecord(this[DOCUMENT], wrapper).type))
618
+ continue;
619
+ if (inner) {
620
+ if (!['root', 'tag', 'script', 'style', 'cdata'].includes(nodeRecord(this[DOCUMENT], id).type))
621
+ continue;
622
+ const contents = this.op<number[]>('traverse', { ids: [id], kind: 'contents' });
623
+ this.op('setNode', { id: this._deepest(wrapper), field: 'children', value: contents });
624
+ this.op('setNode', { id, field: 'children', value: [wrapper] });
625
+ }
626
+ else if (nodeRecord(this[DOCUMENT], id).parent != null) {
627
+ // A wrapper from another document gets new ids when the insertion moves it here.
628
+ const [inserted = wrapper] = this.op<number[]>('insert', { targetIds: [id], nodeIds: [wrapper], position: 'before' });
629
+ this.op('setNode', { id: this._deepest(inserted), field: 'children', value: [id] });
630
+ }
631
+ }
632
+ return this;
633
+ }
634
+ wrap<T extends AnyNode>(this: Cheerio<T>, wrapper: AcceptedElems<AnyNode>): Cheerio<T>;
635
+ wrap(this: Cheerio<any>, content: any): any { return this._wrap(content, false); }
636
+ wrapInner<T extends AnyNode>(this: Cheerio<T>, wrapper: AcceptedElems<AnyNode>): Cheerio<T>;
637
+ wrapInner(this: Cheerio<any>, content: any): any { return this._wrap(content, true); }
638
+ unwrap<T extends AnyNode>(this: Cheerio<T>, selector?: string): Cheerio<T>;
639
+ unwrap(this: Cheerio<any>, selector?: any): any { this.op('remove', { ids: this.parent(selector).not('body').ids, keepChildren: true }); return this; }
640
+ wrapAll<T extends AnyNode>(this: Cheerio<T>, wrapper: AcceptedElems<T>): Cheerio<T>;
641
+ wrapAll(this: Cheerio<any>, content: any): any {
642
+ if (!this.length)
643
+ return this;
644
+ const first = this[0];
645
+ const wrappers = this._make(typeof content === 'function' ? content.call(first, 0, first) : content).insertBefore(first);
646
+ const root = [...wrappers.ids].reverse().find((id) => nodeRecord(this[DOCUMENT], id).type === 'tag');
647
+ if (root !== undefined)
648
+ this.op('insert', { targetIds: [this._deepest(root, true)], nodeIds: this.ids, position: 'append' });
649
+ return this;
650
+ }
651
+ extract<M extends ExtractMap, T extends AnyNode>(this: Cheerio<T>, map: M): ExtractedMap<M>;
652
+ // A structurally shared object signature breaks recursive enum comparisons
653
+ // across independent DOM declarations; calls retain the precise overload.
654
+ extract<M extends ExtractMap, T extends AnyNode>(this: Cheerio<T>, map: M): Record<string, unknown>;
655
+ extract<M extends ExtractMap>(this: Cheerio<any>, map: any): any { return extract.call(this as any, map) as ExtractedMap<M>; }
656
+ }
657
+ Object.defineProperty(Cheerio.prototype, 'cheerio', { value: '[cheerio object]', writable: true, configurable: true });
658
+ const inspectCustom = Symbol.for('nodejs.util.inspect.custom');
659
+ // Inspection shows the selection's own string keys, as Cheerio's selections
660
+ // have, without the internals stored under symbols.
661
+ Object.defineProperty(Cheerio.prototype, inspectCustom, {
662
+ configurable: true, writable: true,
663
+ value(this: Cheerio<any>, depth: number, options: { stylize(text: string, style: string): string }, inspect: (value: unknown, options: object) => string): string {
664
+ const prototype = Object.getPrototypeOf(this);
665
+ if (depth < 0) return options.stylize(`[${prototype.constructor.name}]`, 'special');
666
+ const shown: Record<string | symbol, unknown> = Object.create(prototype);
667
+ Object.defineProperty(shown, inspectCustom, { value: undefined });
668
+ for (const key of Object.keys(this)) shown[key] = (this as any)[key];
669
+ return inspect(shown, { ...options, depth });
670
+ },
671
+ });
672
+ export interface Cheerio<T = AnyNode> extends AttributeMethods<T> {
673
+ }
674
+ installAttributes(Cheerio);
675
+
676
+ /** @internal */
677
+ export const nativeExtractionMethods = Object.freeze({
678
+ make: Cheerio.prototype._make,
679
+ find: Cheerio.prototype._findBySelector,
680
+ prop: Cheerio.prototype.prop,
681
+ extract: Cheerio.prototype.extract,
682
+ });