@dvaji/cheerox 0.0.0-stage → 0.1.1

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 (253) hide show
  1. package/CONTRIBUTING.md +74 -0
  2. package/Cargo.lock +1094 -0
  3. package/Cargo.toml +46 -0
  4. package/LICENSE +21 -0
  5. package/README.md +121 -2
  6. package/RELEASING.md +114 -0
  7. package/THIRD_PARTY_NOTICES.md +7579 -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-BS--qPzw.mjs +1648 -0
  18. package/dist/cheerio-compat-BS--qPzw.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-DSwBAJ1s.cjs +1719 -0
  23. package/dist/cheerio-compat-DSwBAJ1s.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 +4119 -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 +26 -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/package.json +167 -4
  167. package/scripts/check-native.mjs +15 -0
  168. package/scripts/check-release.mjs +234 -0
  169. package/scripts/fix-declarations.mjs +39 -0
  170. package/scripts/generate-notices.mjs +188 -0
  171. package/scripts/notices/README.md +13 -0
  172. package/scripts/notices/cheerio-MIT.txt +21 -0
  173. package/scripts/notices/html5ever-AUTHORS.txt +20 -0
  174. package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
  175. package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
  176. package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
  177. package/scripts/notices/htmlparser2-MIT.txt +18 -0
  178. package/scripts/notices/napi-rs-MIT.txt +43 -0
  179. package/scripts/notices/neon-MIT.txt +19 -0
  180. package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
  181. package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
  182. package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
  183. package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
  184. package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
  185. package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
  186. package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
  187. package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
  188. package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
  189. package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
  190. package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
  191. package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
  192. package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
  193. package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
  194. package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
  195. package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
  196. package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
  197. package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
  198. package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
  199. package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
  200. package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
  201. package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
  202. package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
  203. package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
  204. package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
  205. package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
  206. package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
  207. package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
  208. package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
  209. package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
  210. package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
  211. package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
  212. package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
  213. package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
  214. package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
  215. package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
  216. package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
  217. package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
  218. package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
  219. package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
  220. package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
  221. package/scripts/notices/runtime.json +1632 -0
  222. package/scripts/notices/sources.json +561 -0
  223. package/scripts/notices/xray-MIT.txt +21 -0
  224. package/src/attribute-object.ts +71 -0
  225. package/src/attributes.ts +308 -0
  226. package/src/cheerio-compat/css.ts +58 -0
  227. package/src/cheerio-compat/render.ts +106 -0
  228. package/src/cheerio-compat/selectors.ts +51 -0
  229. package/src/cheerio-compat/types.ts +52 -0
  230. package/src/cheerio-compat.ts +2055 -0
  231. package/src/codec.ts +96 -0
  232. package/src/dom.ts +87 -0
  233. package/src/extract-plan.ts +97 -0
  234. package/src/extract.ts +322 -0
  235. package/src/hooks.ts +369 -0
  236. package/src/index.ts +48 -0
  237. package/src/io.ts +491 -0
  238. package/src/match.ts +207 -0
  239. package/src/modern.ts +259 -0
  240. package/src/native.ts +149 -0
  241. package/src/nodes.ts +831 -0
  242. package/src/parse.ts +53 -0
  243. package/src/parser-types.ts +157 -0
  244. package/src/router.ts +1129 -0
  245. package/src/selection.ts +682 -0
  246. package/src/slim.ts +12 -0
  247. package/src/snapshot.ts +291 -0
  248. package/src/transport.ts +113 -0
  249. package/src/types.ts +186 -0
  250. package/src/utils.ts +29 -0
  251. package/src/workers.ts +21 -0
  252. package/tsconfig.json +15 -0
  253. package/tsdown.config.mjs +17 -0
package/src/match.ts ADDED
@@ -0,0 +1,207 @@
1
+ import type { NativeDocument } from './native.js';
2
+ import type { CheerioOptions } from './types.js';
3
+ import { attributeAt, isRouted, recordAt, stateIn, structureSnapshot, type DocumentState } from './router.js';
4
+ import { NodeSnapshot } from './snapshot.js';
5
+
6
+ /**
7
+ * One compound selector: an optional type plus id, class and attribute-presence
8
+ * tests. Lowercased names are kept for documents that lowercase names.
9
+ */
10
+ interface Compound { tag: string | undefined; lowerTag: string | undefined; ids: string[]; classes: string[]; attributes: string[]; lowerAttributes: string[] }
11
+
12
+ const compiled = new Map<string, Compound[] | null>();
13
+ const compiledLimit = 512;
14
+
15
+ /**
16
+ * Matches `ids` against selectors simple enough to evaluate on cached records,
17
+ * with the native engine's semantics; `undefined` means the caller must ask
18
+ * the native engine (any other syntax, foreign documents or parser options).
19
+ */
20
+ export function matchSimpleSelector(document: NativeDocument, ids: readonly number[], selector: string, options: CheerioOptions): number[] | undefined {
21
+ if (!isRouted(document)) return undefined;
22
+ const compounds = compile(selector);
23
+ if (!compounds) return undefined;
24
+ const isXml = isXmlMode(options);
25
+ const isLowerCaseTags = options.lowerCaseTags ?? !isXml;
26
+ const isLowerCaseAttributes = options.lowerCaseAttributeNames ?? !isXml;
27
+ const result: number[] = [];
28
+ for (const id of ids) {
29
+ const state = stateIn(document, id);
30
+ if (!sameMatchOptions(state.options, isXml, isLowerCaseTags, isLowerCaseAttributes)) return undefined;
31
+ const local = id - state.base;
32
+ for (const compound of compounds) {
33
+ if (matchesCompound(state, local, compound, isLowerCaseTags, isLowerCaseAttributes)) { result.push(id); break; }
34
+ }
35
+ }
36
+ return result;
37
+ }
38
+
39
+ /** Nodes a single `find()` walks in JavaScript before leaving larger subtrees to the native engine. */
40
+ const walkBudget = 4096;
41
+ /** Documents small enough that walking all of them in JavaScript beats a native call. */
42
+ const smallDocumentNodes = 512;
43
+ /** Documents small enough that taking a snapshot to walk them still beats a native call. */
44
+ const tinyDocumentNodes = 64;
45
+
46
+ /**
47
+ * Selects the descendants of one element, or of the root of a small
48
+ * document, that match a simple selector, in document order, as the native
49
+ * `select` does; `undefined` defers to it.
50
+ */
51
+ export function selectSimpleWithin(document: NativeDocument, contexts: readonly number[], selector: string, options: CheerioOptions): number[] | undefined {
52
+ if (contexts.length !== 1 || !isRouted(document)) return undefined;
53
+ const compounds = compile(selector);
54
+ if (!compounds) return undefined;
55
+ const isXml = isXmlMode(options);
56
+ const isLowerCaseTags = options.lowerCaseTags ?? !isXml;
57
+ const isLowerCaseAttributes = options.lowerCaseAttributeNames ?? !isXml;
58
+ const state = stateIn(document, contexts[0]);
59
+ if (!sameMatchOptions(state.options, isXml, isLowerCaseTags, isLowerCaseAttributes)) return undefined;
60
+ const context = contexts[0] - state.base;
61
+ const size = Math.max(state.baseline, state.snapshotSize);
62
+ const isDocumentRoot = context === (state.rootLocal ??= state.raw.rootId());
63
+ const contextType = isDocumentRoot ? 'root' : typeAt(state, context);
64
+ // After a write, walking would cost a fresh snapshot; the native engine reads the arena directly.
65
+ // A walk reads every node: worthwhile with a current snapshot, or when a tiny unchanged document needs one.
66
+ const isReadable = state.bulk instanceof NodeSnapshot ? size <= smallDocumentNodes : state.generation === 0 && size <= tinyDocumentNodes;
67
+ const isSmallRoot = contextType === 'root' && isReadable;
68
+ if (!isElementType(contextType) && !isSmallRoot) return undefined;
69
+ const result: number[] = [];
70
+ const stack: number[] = [];
71
+ pushChildren(state, context, stack);
72
+ for (let visited = 0; stack.length; visited++) {
73
+ if (visited === walkBudget) return undefined;
74
+ const local = stack.pop()!;
75
+ const type = typeAt(state, local);
76
+ // Template contents: outside an element's descendants, but selected from the document root.
77
+ if (type === 'root') { if (isSmallRoot) return undefined; continue; }
78
+ if (isElementType(type)) {
79
+ for (const compound of compounds) {
80
+ if (matchesCompound(state, local, compound, isLowerCaseTags, isLowerCaseAttributes)) { result.push(state.base + local); break; }
81
+ }
82
+ }
83
+ pushChildren(state, local, stack);
84
+ }
85
+ return result;
86
+ }
87
+
88
+ const isElementType = (type: string | undefined): boolean => type === 'tag' || type === 'script' || type === 'style';
89
+
90
+ function typeAt(state: DocumentState, local: number): string | undefined {
91
+ const snapshot = structureSnapshot(state, local);
92
+ return snapshot ? snapshot.typeOf(local) : recordAt(state, local)?.type;
93
+ }
94
+
95
+ /** Pushes children last to first, so they pop in document order. */
96
+ function pushChildren(state: DocumentState, local: number, stack: number[]): void {
97
+ const snapshot = structureSnapshot(state, local);
98
+ if (snapshot) {
99
+ for (let index = snapshot.childCount(local) - 1; index >= 0; index--) stack.push(snapshot.childAt(local, index));
100
+ return;
101
+ }
102
+ const children = recordAt(state, local)?.children ?? [];
103
+ for (let index = children.length - 1; index >= 0; index--) stack.push(children[index]);
104
+ }
105
+
106
+ function isXmlMode(options: CheerioOptions): boolean {
107
+ return options.xmlMode ?? (options.xml === true || typeof options.xml === 'object' && options.xml !== null);
108
+ }
109
+
110
+ /** Records were parsed with the document's options; matching must use the same name casing. */
111
+ function sameMatchOptions(documentOptions: CheerioOptions, isXml: boolean, isLowerCaseTags: boolean, isLowerCaseAttributes: boolean): boolean {
112
+ const isDocumentXml = isXmlMode(documentOptions);
113
+ return isDocumentXml === isXml
114
+ && (documentOptions.lowerCaseTags ?? !isDocumentXml) === isLowerCaseTags
115
+ && (documentOptions.lowerCaseAttributeNames ?? !isDocumentXml) === isLowerCaseAttributes;
116
+ }
117
+
118
+ function matchesCompound(state: DocumentState, local: number, compound: Compound, isLowerCaseTags: boolean, isLowerCaseAttributes: boolean): boolean {
119
+ const snapshot = structureSnapshot(state, local);
120
+ const record = snapshot ? undefined : recordAt(state, local);
121
+ if (!snapshot && !record) return false;
122
+ if (!isElementType(snapshot ? snapshot.typeOf(local) : record!.type)) return false;
123
+ if (compound.tag !== undefined && (snapshot ? snapshot.nameOf(local) : record!.name) !== (isLowerCaseTags ? compound.lowerTag : compound.tag)) return false;
124
+ for (const id of compound.ids) if (attributeAt(state, local, 'id') !== id) return false;
125
+ if (compound.classes.length) {
126
+ const classes = attributeAt(state, local, 'class');
127
+ if (classes === undefined) return false;
128
+ for (const name of compound.classes) if (!hasClass(classes, name)) return false;
129
+ }
130
+ for (const name of isLowerCaseAttributes ? compound.lowerAttributes : compound.attributes) if (attributeAt(state, local, name) === undefined) return false;
131
+ return true;
132
+ }
133
+
134
+ const isAsciiWhitespace = (code: number): boolean => code === 32 || code === 9 || code === 10 || code === 12 || code === 13;
135
+
136
+ /** Whether `name` is one of the ASCII-whitespace-separated tokens of `classes`. */
137
+ function hasClass(classes: string, name: string): boolean {
138
+ for (let start = classes.indexOf(name); start !== -1; start = classes.indexOf(name, start + 1)) {
139
+ const end = start + name.length;
140
+ if ((start === 0 || isAsciiWhitespace(classes.charCodeAt(start - 1))) && (end === classes.length || isAsciiWhitespace(classes.charCodeAt(end)))) return true;
141
+ }
142
+ return false;
143
+ }
144
+
145
+ function asciiLowerCase(value: string): string {
146
+ return /[A-Z]/.test(value) ? value.replace(/[A-Z]/g, (letter) => letter.toLowerCase()) : value;
147
+ }
148
+
149
+ function compile(selector: string): Compound[] | null {
150
+ const cached = compiled.get(selector);
151
+ if (cached !== undefined) return cached;
152
+ const result = parse(selector);
153
+ if (compiled.size >= compiledLimit) compiled.clear();
154
+ compiled.set(selector, result);
155
+ return result;
156
+ }
157
+
158
+ const identifierStart = /[A-Za-z_]/;
159
+ const identifierPart = /[A-Za-z0-9_-]/;
160
+
161
+ /** Parses the supported subset; anything else (escapes, combinators, pseudos, values) yields `null`. */
162
+ function parse(selector: string): Compound[] | null {
163
+ const compounds: Compound[] = [];
164
+ let index = 0;
165
+ const skipSpace = (): void => { while (index < selector.length && isAsciiWhitespace(selector.charCodeAt(index))) index++; };
166
+ const identifier = (): string | null => {
167
+ const start = index;
168
+ if (selector[index] === '-') index++;
169
+ if (!identifierStart.test(selector[index] ?? '')) return null;
170
+ while (index < selector.length && identifierPart.test(selector[index])) index++;
171
+ return selector.slice(start, index);
172
+ };
173
+ for (;;) {
174
+ skipSpace();
175
+ const compound: Compound = { tag: undefined, lowerTag: undefined, ids: [], classes: [], attributes: [], lowerAttributes: [] };
176
+ let hasPart = false;
177
+ if (selector[index] === '*') { index++; hasPart = true; }
178
+ else if (/[A-Za-z]/.test(selector[index] ?? '')) { compound.tag = identifier()!; hasPart = true; }
179
+ for (;;) {
180
+ const character = selector[index];
181
+ if (character === '#' || character === '.') {
182
+ index++;
183
+ const name = identifier();
184
+ if (name === null) return null;
185
+ (character === '#' ? compound.ids : compound.classes).push(name);
186
+ } else if (character === '[') {
187
+ index++;
188
+ skipSpace();
189
+ const name = identifier();
190
+ if (name === null) return null;
191
+ skipSpace();
192
+ if (selector[index] !== ']') return null;
193
+ index++;
194
+ compound.attributes.push(name);
195
+ } else break;
196
+ hasPart = true;
197
+ }
198
+ if (!hasPart) return null;
199
+ compound.lowerTag = compound.tag === undefined ? undefined : asciiLowerCase(compound.tag);
200
+ compound.lowerAttributes = compound.attributes.map(asciiLowerCase);
201
+ compounds.push(compound);
202
+ skipSpace();
203
+ if (index === selector.length) return compounds;
204
+ if (selector[index] !== ',') return null;
205
+ index++;
206
+ }
207
+ }
package/src/modern.ts ADDED
@@ -0,0 +1,259 @@
1
+ import * as nativeDOM from './dom.js';
2
+ import { parseNativeDocument, parserOptions, deliverParseErrors, hasParserHooks, cleanParserOptions, createTreeAdapterSession } from './hooks.js';
3
+ import { compileExtractPlan, hydrateExtractResult } from './extract-plan.js';
4
+ import { extractCooperatively, prepareExtractPlans, releaseExtractPlans, rootExtractPlans } from './extract.js';
5
+ import { setImmediate as yieldToEventLoop } from 'node:timers/promises';
6
+ import { availableParallelism } from 'node:os';
7
+ import { requireNative, nativeStatus, type NativeDocument, type NativeQuery } from './native.js';
8
+ import { Cheerio } from './selection.js';
9
+ import { getNode, importNodes, nodeBinding, operation, rootNode, viewIdIn } from './nodes.js';
10
+ import { isHtml } from './utils.js';
11
+ import { DOCUMENT, exposeDocumentTo, resumeCollection, routeDocument, sameNodeSpace, usePreparedSnapshot } from './router.js';
12
+ import type { AnyNode, Document, CheerioAPI, CheerioOptions, LoadContent, ParallelOptions, ExtractMap, ExtractedMap } from './types.js';
13
+
14
+ export type * from './types.js';
15
+ export type { Cheerio } from './selection.js';
16
+ export { nativeStatus };
17
+ export const dom = {
18
+ Node: nativeDOM.Node, DataNode: nativeDOM.DataNode, NodeWithChildren: nativeDOM.NodeWithChildren,
19
+ Document: nativeDOM.Document, Element: nativeDOM.Element, Text: nativeDOM.Text,
20
+ Comment: nativeDOM.Comment, ProcessingInstruction: nativeDOM.ProcessingInstruction, CDATA: nativeDOM.CDATA,
21
+ ElementType: nativeDOM.ElementType, isTag: nativeDOM.isTag, isText: nativeDOM.isText,
22
+ isComment: nativeDOM.isComment, isDirective: nativeDOM.isDirective, isDocument: nativeDOM.isDocument,
23
+ isCDATA: nativeDOM.isCDATA, hasChildren: nativeDOM.hasChildren,
24
+ } as const;
25
+ export { configureWorkers, workerPoolInfo } from './workers.js';
26
+ export type { WorkerOptions, WorkerPoolInfo } from './workers.js';
27
+ export { loadBuffer, stringStream, decodeStream, fromURL } from './io.js';
28
+
29
+ const defaultOptions: CheerioOptions = { _useHtmlParser2: false };
30
+
31
+ /** Cheerio's option merge: without overrides, calls share the base options object. */
32
+ export function flattenOptions(options?: CheerioOptions | null, base?: CheerioOptions): CheerioOptions {
33
+ if (!options) return base ?? defaultOptions;
34
+ const result: CheerioOptions = { _useHtmlParser2: false, ...base, ...options };
35
+ if (options?.xml) { result._useHtmlParser2 = true; result.xmlMode = true; if (options.xml !== true) Object.assign(result, options.xml); }
36
+ else if (options?.xmlMode) result._useHtmlParser2 = true;
37
+ return result;
38
+ }
39
+
40
+ /** A synchronous, constructable Cheerio factory backed by a mutable Rust DOM. */
41
+ export function createAPI(document: NativeDocument, options?: CheerioOptions | null, initialRoot = document.rootId()): CheerioAPI {
42
+ const internal = flattenOptions(options);
43
+ class LoadedCheerio<T = AnyNode> extends Cheerio<T> {}
44
+ let api: CheerioAPI;
45
+ const createRoot = (rootIds: number[], opts: CheerioOptions): Cheerio<Document> => {
46
+ const rootInstance = new LoadedCheerio<Document>(api, document, rootIds, null, opts);
47
+ rootInstance._root = rootInstance;
48
+ return rootInstance;
49
+ };
50
+ const initialize = function<T = AnyNode>(selector?: any, context?: any, root?: any, override?: CheerioOptions): Cheerio<T> {
51
+ resumeCollection(document);
52
+ if (selector?.cheerio) { exposeDocumentTo(document, selector[DOCUMENT]); return selector; }
53
+ const opts = flattenOptions(override, internal);
54
+ // Each call gets its own root selection, as in Cheerio: callers may change the one `end()` returns.
55
+ const rootInstance = createRoot(root === undefined ? [initialRoot]
56
+ : typeof root === 'string' ? [makeRoot(document, operation(document, 'create', { html: root, options: opts }))]
57
+ : root?.cheerio ? importNodes(document, root.toArray()) : importNodes(document, root), opts);
58
+ if (!selector) return new LoadedCheerio<T>(api, document, [], rootInstance, opts);
59
+ const id = viewIdIn(document, selector);
60
+ if (id !== undefined) return new LoadedCheerio<T>(api, document, [id], rootInstance, opts);
61
+ if (typeof selector === 'string' && isHtml(selector)) return new LoadedCheerio<T>(api, document, operation(document, 'create', { html: selector, options: opts }), rootInstance, opts);
62
+ if (typeof selector === 'object') {
63
+ const values = 'length' in selector && !selector.type ? Array.from(selector as ArrayLike<T>) : [selector];
64
+ const allNodes = values.every((value: any) => value && typeof value === 'object' && typeof value.type === 'string');
65
+ return new LoadedCheerio<T>(api, document, allNodes ? importNodes(document, values) : [], rootInstance, opts, allNodes ? undefined : values);
66
+ }
67
+ if (typeof selector !== 'string') throw new TypeError('Unexpected type of selector');
68
+ let search = selector;
69
+ let searchContext: Cheerio<any> = rootInstance;
70
+ if (context) {
71
+ if (typeof context === 'string') {
72
+ if (isHtml(context)) searchContext = new LoadedCheerio<Document>(api, document, [makeRoot(document, operation(document, 'create', { html: context, options: opts }))], rootInstance, opts);
73
+ else search = `${context} ${search}`;
74
+ } else if (context.cheerio) searchContext = context;
75
+ else searchContext = new LoadedCheerio(api, document, importNodes(document, context), rootInstance, opts);
76
+ }
77
+ return searchContext.find(search) as unknown as Cheerio<T>;
78
+ };
79
+ // As in Cheerio, static methods are shared by every loaded API and read the document through `this`.
80
+ api = Object.assign(initialize, staticMethods, {
81
+ load,
82
+ _root: rootNode(document, initialRoot),
83
+ _options: internal,
84
+ fn: LoadedCheerio.prototype,
85
+ prototype: LoadedCheerio.prototype,
86
+ }) as unknown as CheerioAPI;
87
+ return api;
88
+ }
89
+
90
+ const staticMethods = {
91
+ root(this: CheerioAPI): Cheerio<Document> { return this(this._root) as Cheerio<Document>; },
92
+ html(this: CheerioAPI | undefined, dom?: any, renderOptions?: CheerioOptions): string {
93
+ if (!this) return '';
94
+ if (dom && typeof dom === 'object' && !('length' in dom) && !('type' in dom)) { renderOptions = dom; dom = undefined; }
95
+ const nodes = dom === undefined ? [this._root] : this(dom).toArray();
96
+ const binding = nodeBinding(this._root)!;
97
+ const base = this._options;
98
+ const unchanged = !renderOptions && ((!base._useHtmlParser2 && !base.xml && !base.xmlMode)
99
+ || (base.xml === true && base.xmlMode === true && base._useHtmlParser2 === true));
100
+ const options = unchanged ? base : flattenOptions({ ...base, ...flattenOptions(renderOptions) }, base);
101
+ return operation(binding.document, 'render', { ids: importNodes(binding.document, nodes), options });
102
+ },
103
+ xml(this: CheerioAPI, dom?: any): string {
104
+ const binding = nodeBinding(this._root)!;
105
+ return operation(binding.document, 'render', { ids: importNodes(binding.document, dom === undefined ? [this._root] : this(dom).toArray()), options: { ...this._options, xmlMode: true, _useHtmlParser2: true } });
106
+ },
107
+ text(this: CheerioAPI | undefined, elements?: ArrayLike<AnyNode>): string {
108
+ if (!elements && !this) return '';
109
+ const nodes = elements ?? [this!._root];
110
+ const binding = this ? nodeBinding(this._root) : nodeBinding(nodes[0]);
111
+ if (binding) return binding.document.text(importNodes(binding.document, nodes));
112
+ const doc = routeDocument(new (requireNative().NativeDocument)('', false));
113
+ return doc.text(importNodes(doc, nodes));
114
+ },
115
+ parseHTML(this: CheerioAPI, data?: string | null, context?: unknown, keepScripts = typeof context === 'boolean' ? context : false): AnyNode[] | null {
116
+ if (!data || typeof data !== 'string') return null;
117
+ if (typeof context === 'boolean') keepScripts = context;
118
+ const parsed = this.load(data, this._options, false);
119
+ if (!keepScripts) parsed('script').remove();
120
+ return parsed._root.children.slice();
121
+ },
122
+ extract<M extends ExtractMap>(this: CheerioAPI, map: M): ExtractedMap<M> { return this.root().extract(map); },
123
+ contains,
124
+ merge,
125
+ };
126
+
127
+ function makeRoot(document: NativeDocument, children: number[]): number {
128
+ const id = operation<number>(document, 'createNode', { type: 'root' });
129
+ operation(document, 'setNode', { id, field: 'children', value: children });
130
+ return id;
131
+ }
132
+
133
+ export function load(content: LoadContent, options?: CheerioOptions | null, isDocument = true): CheerioAPI {
134
+ if (content == null) throw new Error('cheerio.load() expects a string');
135
+ const opts = flattenOptions(options);
136
+ const binding = requireNative();
137
+ const string = Buffer.isBuffer(content) ? content.toString() : typeof content === 'string' ? content : undefined;
138
+ const document = string === undefined ? routeDocument(new binding.NativeDocument('', false, JSON.stringify(cleanParserOptions(opts))), opts) : parseNativeDocument(string, opts, isDocument);
139
+ if (string !== undefined) return createAPI(document, opts);
140
+ const nodes = importNodes(document, content);
141
+ if (!Array.isArray(content) && (content as AnyNode).type === 'root') return createAPI(document, opts, nodes[0]);
142
+ operation(document, 'setNode', { id: document.rootId(), field: 'children', value: nodes });
143
+ return createAPI(routeDocument(document, opts), opts);
144
+ }
145
+
146
+ export async function loadAsync(content: LoadContent, options?: CheerioOptions | null, isDocument = true): Promise<CheerioAPI> {
147
+ if (content == null) throw new Error('cheerio.load() expects a string');
148
+ if (typeof content !== 'string' && !Buffer.isBuffer(content)) return load(content, options, isDocument);
149
+ const binding = requireNative();
150
+ const opts = flattenOptions(options);
151
+ const html = String(content);
152
+ // User parser hooks execute on JavaScript, exactly where their callbacks live.
153
+ if (!opts._useHtmlParser2 && opts.treeAdapter) {
154
+ const session = createTreeAdapterSession(opts, isDocument);
155
+ try { await session.write(html); return createAPI(await session.end(), opts); }
156
+ catch (error) { session.abort(); throw error; }
157
+ }
158
+ if (opts._useHtmlParser2 && opts.Tokenizer) return createAPI(parseNativeDocument(html, opts, isDocument), opts);
159
+ const document = binding.parseAsync ? await binding.parseAsync(html, isDocument, JSON.stringify(parserOptions(html, opts))) : new binding.NativeDocument(html, isDocument, JSON.stringify(parserOptions(html, opts)));
160
+ deliverParseErrors(document, opts);
161
+ return createAPI(routeDocument(document, opts), opts);
162
+ }
163
+
164
+ export function contains(container: AnyNode, contained: AnyNode): boolean {
165
+ if (!container || !contained || container === contained) return false;
166
+ let parent = nodeBinding(container);
167
+ if (!parent) {
168
+ const document = routeDocument(new (requireNative().NativeDocument)('', false));
169
+ importNodes(document, [container]); parent = nodeBinding(container);
170
+ }
171
+ const child = nodeBinding(contained);
172
+ return !!parent && !!child && sameNodeSpace(child.document, parent.document) && operation(parent.document, 'contains', { container: parent.id, contained: child.id });
173
+ }
174
+
175
+ export function merge<T>(first: ArrayLike<T>, second: ArrayLike<T>): ArrayLike<T> | undefined {
176
+ const arrayLike = (value: any): boolean => Array.isArray(value) || value && typeof value === 'object' && typeof value.length === 'number' && value.length >= 0 && Array.from({ length: value.length }, (_unused, i) => i in value).every(Boolean);
177
+ if (!arrayLike(first) || !arrayLike(second)) return undefined;
178
+ const writable = first as { [key: number]: T; length: number };
179
+ let length = first.length;
180
+ for (let i = 0, count = +second.length; i < count; i++) writable[length++] = second[i];
181
+ writable.length = length;
182
+ return writable;
183
+ }
184
+
185
+ function parallelOptions(options?: ParallelOptions | null): { options: CheerioOptions; threads: number } {
186
+ const { threads = Math.min(availableParallelism(), 256), ...opts } = options ?? {};
187
+ if (!Number.isSafeInteger(threads) || threads < 1 || threads > 256) throw new RangeError('threads must be an integer from 1 to 256');
188
+ return { options: flattenOptions(opts), threads };
189
+ }
190
+
191
+ export async function loadAll(content: readonly string[], options?: ParallelOptions | null, isDocument = true): Promise<CheerioAPI[]> {
192
+ return loadAllWithPlans(content, options, isDocument);
193
+ }
194
+
195
+ /**
196
+ * Loads documents in parallel. With `plans`, the parsing threads also run
197
+ * them on each root and build each document's snapshot, which extraction
198
+ * with callbacks would otherwise do on the calling thread when it needs them.
199
+ */
200
+ async function loadAllWithPlans(content: readonly string[], options: ParallelOptions | null | undefined, isDocument: boolean, plans?: ReturnType<typeof rootExtractPlans>): Promise<CheerioAPI[]> {
201
+ const { options: opts, threads } = parallelOptions(options);
202
+ if (!Array.isArray(content) || content.some((item) => typeof item !== 'string')) throw new TypeError('loadAll expects an array of HTML strings');
203
+ if (!content.length) return [];
204
+ const binding = requireNative();
205
+ if (!binding.parseManyAsync || hasParserHooks(opts)) return Promise.all(content.map((html) => loadAsync(html, opts, isDocument)));
206
+ const documents = await binding.parseManyAsync([...content], isDocument, threads, JSON.stringify(opts), plans && JSON.stringify(plans));
207
+ return documents.map((document) => {
208
+ const routed = routeDocument(document, opts);
209
+ if (plans) {
210
+ usePreparedSnapshot(routed);
211
+ prepareExtractPlans(routed, plans, document.takePreparedPlans?.());
212
+ }
213
+ return createAPI(routed, opts);
214
+ });
215
+ }
216
+
217
+ function nativeQueries(map: ExtractMap, options: CheerioOptions): NativeQuery[] | undefined {
218
+ const queries: NativeQuery[] = [];
219
+ const plainProperties = /^(?:id|class|title|href|src|alt|name|data-.+|aria-.+)$/;
220
+ for (const key in map) {
221
+ const entry = map[key]; const all = Array.isArray(entry); const descriptor = all ? entry[0] : entry;
222
+ const selector = typeof descriptor === 'string' ? descriptor : descriptor.selector;
223
+ const value = typeof descriptor === 'string' ? 'textContent' : descriptor.value ?? 'textContent';
224
+ if (typeof value !== 'string' || value !== 'textContent' && !plainProperties.test(value) || options.baseURI && (value === 'href' || value === 'src') || options.pseudos) return undefined;
225
+ queries.push({ key, selector, all, ...(value === 'textContent' ? {} : { attr: value }) });
226
+ }
227
+ return queries;
228
+ }
229
+
230
+ export async function extractAll<M extends ExtractMap>(content: readonly string[], map: M, options?: ParallelOptions | null): Promise<ExtractedMap<M>[]> {
231
+ const { options: opts, threads } = parallelOptions(options);
232
+ if (!Array.isArray(content) || content.some((item) => typeof item !== 'string')) throw new TypeError('extractAll expects an array of HTML strings');
233
+ if (!content.length) return [];
234
+ const binding = requireNative();
235
+ const plan = hasParserHooks(opts) ? undefined : compileExtractPlan(map, opts);
236
+ if (binding.extractPlanManyAsync && plan) {
237
+ const results: Record<string, unknown>[] = JSON.parse(await binding.extractPlanManyAsync([...content], JSON.stringify(plan), threads, JSON.stringify(opts)));
238
+ for (const result of results) hydrateExtractResult(result, plan.entries);
239
+ return results as ExtractedMap<M>[];
240
+ }
241
+ const queries = binding.extractPlanManyAsync || hasParserHooks(opts) ? undefined : nativeQueries(map, opts);
242
+ if (binding.extractManyAsync && queries) {
243
+ const results: Record<string, unknown>[] = JSON.parse(await binding.extractManyAsync([...content], queries, threads, true, JSON.stringify(opts)));
244
+ for (const result of results) for (const query of queries) {
245
+ if (!Object.hasOwn(result, query.key)) Object.defineProperty(result, query.key, { value: query.all ? [] : undefined, configurable: true, writable: true, enumerable: true });
246
+ if (query.all && Array.isArray(result[query.key])) result[query.key] = (result[query.key] as unknown[]).filter((value) => value != null);
247
+ }
248
+ return results as ExtractedMap<M>[];
249
+ }
250
+ const documents = await loadAllWithPlans(content, { ...opts, threads }, true, hasParserHooks(opts) ? undefined : rootExtractPlans(map, opts));
251
+ const results: ExtractedMap<M>[] = [];
252
+ let yieldedAt = performance.now();
253
+ for (const document of documents) {
254
+ const root = document.root();
255
+ try { results.push((await extractCooperatively(root, map)).value); } finally { releaseExtractPlans(root[DOCUMENT]); }
256
+ if (performance.now() - yieldedAt >= 4) { await yieldToEventLoop(); yieldedAt = performance.now(); }
257
+ }
258
+ return results;
259
+ }
package/src/native.ts ADDED
@@ -0,0 +1,149 @@
1
+ import { createRequire } from 'node:module';
2
+ import { existsSync } from 'node:fs';
3
+ import { dirname, join } from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { wrapNativeBinding } from './transport.js';
6
+ import type { NativeSnapshot } from './snapshot.js';
7
+
8
+ export interface NativeNodeRecord {
9
+ id: number;
10
+ type: 'root' | 'tag' | 'script' | 'style' | 'text' | 'comment' | 'directive' | 'cdata';
11
+ name?: string;
12
+ data?: string;
13
+ attribs?: Record<string, string>;
14
+ children: number[];
15
+ parent?: number;
16
+ namespace?: string;
17
+ prefix?: string;
18
+ attribsNamespace?: Record<string, string>;
19
+ attribsPrefix?: Record<string, string>;
20
+ doctypeName?: string;
21
+ publicId?: string;
22
+ systemId?: string;
23
+ 'x-name'?: string;
24
+ 'x-publicId'?: string;
25
+ 'x-systemId'?: string;
26
+ 'x-mode'?: 'quirks' | 'limited-quirks' | 'no-quirks';
27
+ [key: string]: any;
28
+ }
29
+
30
+ export interface NativeDocument {
31
+ readonly nodeSpace?: object;
32
+ retainNode?(id: number): void;
33
+ retainNodeData?(id: number, data: object): void;
34
+ adopt?(source: NativeDocument, ids: number[], detach?: boolean): string;
35
+ op(command: string, argsJson: string): string;
36
+ create?(html: string, contextId?: number, optionsJson?: string): number[];
37
+ select(selector: string, contexts?: number[]): number[];
38
+ text(ids: number[]): string;
39
+ innerHtml(id: number): string;
40
+ outerHtml(ids: number[]): string;
41
+ attr(id: number, name: string): string | undefined | null;
42
+ tagName(id: number): string | undefined | null;
43
+ exportTree(): string;
44
+ exportTreeWithOffset?(base: number): string;
45
+ forestRoots?(): number[];
46
+ rootId(): number;
47
+ takePreparedPlans?(): (string | null)[] | null;
48
+ takePreparedSnapshot?(isWanted: boolean): NativeSnapshot | null;
49
+ parserDiagnostics?(): string;
50
+ }
51
+
52
+ export interface NativeStream {
53
+ write(chunk: string): Promise<void>;
54
+ finish(): Promise<NativeDocument>;
55
+ abort(): void;
56
+ }
57
+
58
+ export interface NativeWorkerPoolInfo {
59
+ threads: number;
60
+ activeBatches: number;
61
+ activeWorkers: number;
62
+ peakActiveWorkers: number;
63
+ }
64
+
65
+ export interface NativeQuery {
66
+ key: string;
67
+ selector: string;
68
+ attr?: string;
69
+ all?: boolean;
70
+ }
71
+
72
+ export interface NativeBinding {
73
+ NativeDocument: new (html: string, isDocument?: boolean, optionsJson?: string) => NativeDocument;
74
+ NativeStream?: new (optionsJson: string, isDocument?: boolean, errorBridge?: (request: string) => string) => NativeStream;
75
+ configureWorkerPool?: (threads: number) => void;
76
+ workerPoolInfo?: () => NativeWorkerPoolInfo;
77
+ extractPlanManyAsync?: (html: string[], planJson: string, threads: number, optionsJson?: string) => Promise<string>;
78
+ parseAsync?: (html: string, isDocument?: boolean, optionsJson?: string) => Promise<NativeDocument>;
79
+ parseManyAsync?: (
80
+ html: string[],
81
+ isDocument: boolean,
82
+ threads: number,
83
+ optionsJson?: string,
84
+ plansJson?: string,
85
+ ) => Promise<NativeDocument[]>;
86
+ extractManyAsync?: (
87
+ html: string[],
88
+ queries: NativeQuery[],
89
+ threads: number,
90
+ isDocument?: boolean,
91
+ optionsJson?: string,
92
+ ) => Promise<string>;
93
+ }
94
+
95
+ let cached: NativeBinding | undefined;
96
+ let attempted = false;
97
+ let bindingError: unknown;
98
+
99
+ /**
100
+ * The directory of this bundle, shared by the package's CommonJS and ESM
101
+ * entry points and distinct for every installed copy of the package.
102
+ */
103
+ export function packageDirectory(): string {
104
+ return typeof __dirname === 'string' ? __dirname : dirname(fileURLToPath(import.meta.url));
105
+ }
106
+
107
+ /** Resolve the native addon bundled with the npm package. */
108
+ export function nativeBinding(): NativeBinding | undefined {
109
+ if (attempted) return cached;
110
+ attempted = true;
111
+
112
+ // This module is bundled into both CommonJS and ESM entry points. A synthetic
113
+ // absolute filename lets createRequire work in both module formats.
114
+ const moduleDirectory = packageDirectory();
115
+ const require = createRequire(join(moduleDirectory, 'cheerox-loader.cjs'));
116
+ const directories = [moduleDirectory, dirname(moduleDirectory)];
117
+ const libc = process.platform === 'linux'
118
+ ? ((process.report?.getReport() as { header?: { glibcVersionRuntime?: string } })?.header?.glibcVersionRuntime ? 'gnu' : 'musl')
119
+ : undefined;
120
+ const platform = `${process.platform}-${process.arch}${libc ? `-${libc}` : process.platform === 'win32' ? '-msvc' : ''}`;
121
+ const names = [`cheerox.${platform}.node`, 'cheerox.node'];
122
+ const candidates = [...new Set(directories.flatMap((directory) => names.map((name) => join(directory, name))))];
123
+
124
+ for (const candidate of candidates) {
125
+ if (!existsSync(candidate)) continue;
126
+ try {
127
+ const loaded: NativeBinding = require(candidate);
128
+ if (typeof loaded.NativeDocument === 'function') {
129
+ cached = wrapNativeBinding(loaded);
130
+ return cached;
131
+ }
132
+ } catch (error) {
133
+ bindingError ??= error;
134
+ }
135
+ }
136
+ return undefined;
137
+ }
138
+
139
+ export function requireNative(): NativeBinding {
140
+ const binding = nativeBinding();
141
+ if (binding) return binding;
142
+ throw new Error('Cheerox could not load its Rust native binding. Reinstall cheerox for this platform or run "npm run build:native" from a source checkout.', { cause: bindingError });
143
+ }
144
+
145
+ /** Useful for installation diagnostics and benchmarks that require native code. */
146
+ export function nativeStatus(): { available: boolean; error?: string } {
147
+ const available = !!nativeBinding();
148
+ return available ? { available } : { available, error: String(bindingError ?? 'Native binding not found') };
149
+ }