@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
@@ -0,0 +1,321 @@
1
+ //! Evaluate callback-free extraction maps on native worker threads.
2
+
3
+ use napi::bindgen_prelude::*;
4
+ use serde_json::{json, Map, Value};
5
+
6
+ use crate::NativeDocument;
7
+
8
+ fn invalid(message: &str) -> Error {
9
+ Error::new(Status::InvalidArg, message.to_owned())
10
+ }
11
+
12
+ fn entries(value: &Value) -> Result<&[Value]> {
13
+ value
14
+ .get("entries")
15
+ .and_then(Value::as_array)
16
+ .map(Vec::as_slice)
17
+ .ok_or_else(|| invalid("Extraction plan requires entries"))
18
+ }
19
+
20
+ fn extract(
21
+ document: &NativeDocument,
22
+ plan: &[Value],
23
+ contexts: Option<&[u32]>,
24
+ depth: usize,
25
+ ) -> Result<Value> {
26
+ if depth > 64 {
27
+ return Err(invalid("Extraction plan nesting exceeds 64 levels"));
28
+ }
29
+ let mut result = Map::new();
30
+ let mut previous_selection: Option<(&str, Vec<u32>, bool)> = None;
31
+ for entry in plan {
32
+ let key = entry
33
+ .get("key")
34
+ .and_then(Value::as_str)
35
+ .ok_or_else(|| invalid("Extraction entry requires a key"))?;
36
+ let selector = entry
37
+ .get("selector")
38
+ .and_then(Value::as_str)
39
+ .ok_or_else(|| invalid("Extraction entry requires a selector"))?;
40
+ let all = entry.get("all").and_then(Value::as_bool).unwrap_or(false);
41
+ let value = entry
42
+ .get("value")
43
+ .ok_or_else(|| invalid("Extraction entry requires a value"))?;
44
+ let reusable = value.get("kind").and_then(Value::as_str) == Some("property")
45
+ && value
46
+ .get("name")
47
+ .and_then(Value::as_str)
48
+ .is_some_and(|name| {
49
+ matches!(
50
+ name,
51
+ "textContent"
52
+ | "tagName"
53
+ | "nodeName"
54
+ | "name"
55
+ | "type"
56
+ | "nodeType"
57
+ | "id"
58
+ | "class"
59
+ | "title"
60
+ | "alt"
61
+ ) || name.starts_with("data-")
62
+ || name.starts_with("aria-")
63
+ || matches!(name, "href" | "src")
64
+ && document.options.get("baseURI").is_none()
65
+ });
66
+ if previous_selection
67
+ .as_ref()
68
+ .is_none_or(|(previous, _, complete)| *previous != selector || all && !complete)
69
+ {
70
+ let ids = if all {
71
+ document.select_ids(selector, contexts)?
72
+ } else {
73
+ document
74
+ .select_first_id(selector, contexts)?
75
+ .into_iter()
76
+ .collect()
77
+ };
78
+ previous_selection = Some((selector, ids, all));
79
+ }
80
+ let ids = &previous_selection.as_ref().unwrap().1;
81
+ let read = |id: u32| -> Result<Value> {
82
+ match value.get("kind").and_then(Value::as_str) {
83
+ Some("map") => extract(document, entries(value)?, Some(&[id]), depth + 1),
84
+ Some("property") => {
85
+ let name = value
86
+ .get("name")
87
+ .and_then(Value::as_str)
88
+ .ok_or_else(|| invalid("Extraction property requires a name"))?;
89
+ if name == "textContent" {
90
+ return Ok(Value::String(document.text_ids(&[id])));
91
+ }
92
+ if matches!(name, "tagName" | "nodeName" | "name" | "type" | "nodeType")
93
+ && document.arena.borrow().is_none()
94
+ {
95
+ return Ok(document
96
+ .with_node(id, |node, _| {
97
+ node.value().as_element().map(|element| match name {
98
+ "tagName" | "nodeName" => {
99
+ Value::String(element.name().to_uppercase())
100
+ }
101
+ "name" => Value::String(element.name().to_owned()),
102
+ "type" => Value::String(
103
+ match element.name() {
104
+ "script" => "script",
105
+ "style" => "style",
106
+ _ => "tag",
107
+ }
108
+ .to_owned(),
109
+ ),
110
+ _ => json!(1),
111
+ })
112
+ })
113
+ .flatten()
114
+ .unwrap_or(Value::Null));
115
+ }
116
+ if matches!(name, "id" | "class" | "title" | "alt")
117
+ || name.starts_with("data-")
118
+ || name.starts_with("aria-")
119
+ || matches!(name, "href" | "src")
120
+ && document.options.get("baseURI").is_none()
121
+ {
122
+ return Ok(document
123
+ .attribute(id, name)
124
+ .map(Value::String)
125
+ .unwrap_or(Value::Null));
126
+ }
127
+ document.ensure_arena()?;
128
+ let mut arena = document.arena_for_write();
129
+ let arena = arena
130
+ .as_mut()
131
+ .ok_or_else(|| invalid("Missing native document arena"))?;
132
+ if matches!(name, "name" | "type" | "nodeType") {
133
+ return Ok(match arena.node(id) {
134
+ Some(node) if name == "name" => {
135
+ node.name.clone().map(Value::String).unwrap_or(Value::Null)
136
+ }
137
+ Some(node) if name == "type" => Value::String(node.kind.to_string()),
138
+ Some(_) => json!(1),
139
+ None => Value::Null,
140
+ });
141
+ }
142
+ crate::attributes::execute(
143
+ arena,
144
+ "prop",
145
+ &json!({ "ids": [id], "name": name }),
146
+ )?
147
+ .ok_or_else(|| invalid("Unsupported extraction property"))
148
+ }
149
+ _ => Err(invalid("Unknown extraction value kind")),
150
+ }
151
+ };
152
+ if all {
153
+ let mut values = Vec::new();
154
+ for &id in ids {
155
+ match read(id)? {
156
+ Value::Null => {}
157
+ Value::Array(items) => values.extend(items),
158
+ item => values.push(item),
159
+ }
160
+ }
161
+ result.insert(key.into(), Value::Array(values));
162
+ } else if let Some(id) = ids.first() {
163
+ let item = read(*id)?;
164
+ if !item.is_null() {
165
+ result.insert(key.into(), item);
166
+ }
167
+ }
168
+ if !reusable {
169
+ previous_selection = None;
170
+ }
171
+ }
172
+ Ok(Value::Object(result))
173
+ }
174
+
175
+ pub(crate) fn execute(document: &NativeDocument, plan: &Value) -> Result<Value> {
176
+ if plan.get("version").and_then(Value::as_u64) != Some(1) {
177
+ return Err(invalid("Unsupported extraction plan version"));
178
+ }
179
+ extract(document, entries(plan)?, None, 0)
180
+ }
181
+
182
+ /// Extracts relative to `contexts`, reading the parse tree while it is unchanged.
183
+ pub(crate) fn execute_in(
184
+ document: &NativeDocument,
185
+ plan: &Value,
186
+ contexts: &[u32],
187
+ ) -> Result<Value> {
188
+ if plan.get("version").and_then(Value::as_u64) != Some(1) {
189
+ return Err(invalid("Unsupported extraction plan version"));
190
+ }
191
+ extract(document, entries(plan)?, Some(contexts), 0)
192
+ }
193
+
194
+ fn extract_arena(
195
+ arena: &mut crate::arena::DomArena,
196
+ plan: &[Value],
197
+ contexts: &[u32],
198
+ depth: usize,
199
+ ) -> Result<Value> {
200
+ if depth > 64 {
201
+ return Err(invalid("Extraction plan nesting exceeds 64 levels"));
202
+ }
203
+ let mut result = Map::new();
204
+ for entry in plan {
205
+ let key = entry
206
+ .get("key")
207
+ .and_then(Value::as_str)
208
+ .ok_or_else(|| invalid("Extraction entry requires a key"))?;
209
+ let selector = entry
210
+ .get("selector")
211
+ .and_then(Value::as_str)
212
+ .ok_or_else(|| invalid("Extraction entry requires a selector"))?;
213
+ let all = entry.get("all").and_then(Value::as_bool).unwrap_or(false);
214
+ let value = entry
215
+ .get("value")
216
+ .ok_or_else(|| invalid("Extraction entry requires a value"))?;
217
+ let ids = crate::selector::select(arena, selector, Some(contexts))?;
218
+ let mut read = |id| -> Result<Value> {
219
+ match value.get("kind").and_then(Value::as_str) {
220
+ Some("map") => extract_arena(arena, entries(value)?, &[id], depth + 1),
221
+ Some("property") => {
222
+ let name = value
223
+ .get("name")
224
+ .and_then(Value::as_str)
225
+ .ok_or_else(|| invalid("Extraction property requires a name"))?;
226
+ if matches!(name, "name" | "type" | "nodeType") {
227
+ return Ok(match arena.node(id) {
228
+ Some(node) if name == "name" => {
229
+ node.name.clone().map(Value::String).unwrap_or(Value::Null)
230
+ }
231
+ Some(node) if name == "type" => Value::String(node.kind.to_string()),
232
+ Some(_) => json!(1),
233
+ None => Value::Null,
234
+ });
235
+ }
236
+ crate::attributes::execute(arena, "prop", &json!({"ids":[id],"name":name}))?
237
+ .ok_or_else(|| invalid("Unsupported extraction property"))
238
+ }
239
+ _ => Err(invalid("Unknown extraction value kind")),
240
+ }
241
+ };
242
+ if all {
243
+ let mut values = Vec::new();
244
+ for id in ids {
245
+ match read(id)? {
246
+ Value::Null => {}
247
+ Value::Array(items) => values.extend(items),
248
+ value => values.push(value),
249
+ }
250
+ }
251
+ result.insert(key.into(), Value::Array(values));
252
+ } else if let Some(id) = ids.first() {
253
+ let value = read(*id)?;
254
+ if !value.is_null() {
255
+ result.insert(key.into(), value);
256
+ }
257
+ }
258
+ }
259
+ Ok(Value::Object(result))
260
+ }
261
+
262
+ /** Use the caller's active options and contexts for pure prefixes around callbacks. */
263
+ pub(crate) fn execute_arena(
264
+ arena: &mut crate::arena::DomArena,
265
+ plan: &Value,
266
+ contexts: &[u32],
267
+ ) -> Result<Value> {
268
+ if plan.get("version").and_then(Value::as_u64) != Some(1) {
269
+ return Err(invalid("Unsupported extraction plan version"));
270
+ }
271
+ extract_arena(arena, entries(plan)?, contexts, 0)
272
+ }
273
+
274
+ #[cfg(test)]
275
+ mod tests {
276
+ use super::*;
277
+
278
+ #[test]
279
+ fn tag_names_do_not_materialize_the_arena() {
280
+ let document = NativeDocument::parse_options(
281
+ "<main><img src='/x'><svg><linearGradient id='g'/></svg></main>",
282
+ true,
283
+ json!({}),
284
+ None,
285
+ );
286
+ let plan = json!({"version":1,"entries":[
287
+ {"key":"images","selector":"img","all":true,"value":{"kind":"property","name":"tagName"}},
288
+ {"key":"gradients","selector":"svg","all":true,"value":{"kind":"property","name":"nodeName"}}
289
+ ]});
290
+ assert_eq!(
291
+ execute(&document, &plan).unwrap(),
292
+ json!({"images":["IMG"],"gradients":["SVG"]})
293
+ );
294
+ assert!(document.arena.borrow().is_none());
295
+ }
296
+
297
+ #[test]
298
+ fn nested_maps_preserve_native_property_and_missing_value_semantics() {
299
+ let document = NativeDocument::parse_options(
300
+ "<section><a href='/page'>A<b>B</b></a><input checked></section><section><p>C</p></section>",
301
+ true,
302
+ json!({ "baseURI": "https://example.test/base/" }),
303
+ None,
304
+ );
305
+ let plan = json!({"version":1,"entries":[
306
+ {"key":"rows","selector":"section","all":true,"value":{"kind":"map","entries":[
307
+ {"key":"link","selector":"a","all":false,"value":{"kind":"property","name":"href"}},
308
+ {"key":"content","selector":"a","all":false,"value":{"kind":"property","name":"innerHTML"}},
309
+ {"key":"checked","selector":"input","all":true,"value":{"kind":"property","name":"checked"}},
310
+ {"key":"missing","selector":".absent","all":false,"value":{"kind":"property","name":"textContent"}}
311
+ ]}}
312
+ ]});
313
+ assert_eq!(
314
+ execute(&document, &plan).unwrap(),
315
+ json!({"rows":[
316
+ {"link":"https://example.test/page","content":"A<b>B</b>","checked":[true]},
317
+ {"checked":[]}
318
+ ]})
319
+ );
320
+ }
321
+ }
@@ -0,0 +1,175 @@
1
+ //! Reclaim detached native components that have no live JavaScript handles.
2
+ use std::collections::HashMap;
3
+
4
+ use crate::arena::DomArena;
5
+
6
+ #[derive(Debug)]
7
+ pub(crate) struct GcStats {
8
+ pub live: u32,
9
+ pub slots: u32,
10
+ pub retained: u32,
11
+ pub reclaimed: u32,
12
+ pub free: Vec<u32>,
13
+ }
14
+
15
+ #[cfg(test)]
16
+ pub(crate) fn collect(arena: &mut DomArena, pins: &HashMap<u32, usize>) -> GcStats {
17
+ collect_managed(arena, pins, true, 0)
18
+ }
19
+
20
+ /// Reclaims nodes connected to neither a pin, the root (with `keep_root`)
21
+ /// nor a slot below `keep_below`.
22
+ pub(crate) fn collect_managed(
23
+ arena: &mut DomArena,
24
+ pins: &HashMap<u32, usize>,
25
+ keep_root: bool,
26
+ keep_below: u32,
27
+ ) -> GcStats {
28
+ let mut marked = vec![false; arena.nodes.len()];
29
+ let mut stack: Vec<u32> = if keep_root { vec![0] } else { Vec::new() };
30
+ stack.extend(0..keep_below.min(arena.nodes.len() as u32));
31
+ stack.extend(
32
+ pins.iter()
33
+ .filter_map(|(id, count)| (*count > 0).then_some(*id)),
34
+ );
35
+ // Following both directions marks the entire parent/child component. A
36
+ // held descendant keeps its detached parent and siblings navigable. The
37
+ // visited bitmap also bounds work for cyclic or inconsistent user graphs.
38
+ while let Some(id) = stack.pop() {
39
+ let Some(node) = arena.nodes.get(id as usize) else {
40
+ continue;
41
+ };
42
+ if marked[id as usize] || node.kind == "released" {
43
+ continue;
44
+ }
45
+ marked[id as usize] = true;
46
+ stack.extend(node.parent);
47
+ stack.extend(node.children.iter().copied());
48
+ }
49
+
50
+ let retained = pins
51
+ .iter()
52
+ .filter(|(id, count)| **count > 0 && marked.get(**id as usize) == Some(&true))
53
+ .count() as u32;
54
+ let mut reclaimed = 0;
55
+ for (index, node) in arena.nodes.iter_mut().enumerate() {
56
+ if marked[index] || node.kind == "released" {
57
+ continue;
58
+ }
59
+ reclaimed += 1;
60
+ node.kind = "released".into();
61
+ node.name = None;
62
+ node.data = None;
63
+ node.attribs = Default::default();
64
+ node.children = smallvec::SmallVec::new();
65
+ node.extras = Default::default();
66
+ node.namespace = None;
67
+ node.attribs_namespace = None;
68
+ node.attribs_prefix = None;
69
+ node.parent = None;
70
+ node.prev = None;
71
+ node.next = None;
72
+ }
73
+ while arena
74
+ .nodes
75
+ .last()
76
+ .is_some_and(|node| node.kind == "released")
77
+ {
78
+ arena.nodes.pop();
79
+ }
80
+ if arena.nodes.capacity() > arena.nodes.len().saturating_mul(2).saturating_add(64) {
81
+ arena.nodes.shrink_to_fit();
82
+ }
83
+ let free = arena
84
+ .nodes
85
+ .iter()
86
+ .filter(|node| node.kind == "released")
87
+ .map(|node| node.id)
88
+ .collect::<Vec<_>>();
89
+ arena.detached_since_collection = 0;
90
+ arena.collected_live = arena.nodes.len() as u32 - free.len() as u32;
91
+ GcStats {
92
+ live: arena.nodes.len() as u32 - free.len() as u32,
93
+ slots: arena.nodes.len() as u32,
94
+ retained,
95
+ reclaimed,
96
+ free,
97
+ }
98
+ }
99
+
100
+ #[cfg(test)]
101
+ mod tests {
102
+ use super::*;
103
+ use crate::arena::DomNode;
104
+ use serde_json::json;
105
+
106
+ fn fixture() -> DomArena {
107
+ let mut nodes = vec![DomNode::new("root")];
108
+ nodes.extend((0..5).map(|_| DomNode::new("tag")));
109
+ nodes[0].children = smallvec::smallvec![1, 5];
110
+ nodes[2].children = smallvec::smallvec![3, 4];
111
+ nodes[2].name = Some("detached".into());
112
+ nodes[3].data = Some("payload".repeat(100));
113
+ nodes[3].attribs.insert("key".into(), json!("value"));
114
+ nodes[3].extras.insert("data".into(), json!({"held":true}));
115
+ DomArena::from_records(nodes, json!({}))
116
+ }
117
+
118
+ #[test]
119
+ fn held_detached_child_preserves_its_parent_and_sibling() {
120
+ let mut arena = fixture();
121
+ let stats = collect(&mut arena, &HashMap::from([(3, 2)]));
122
+ assert_eq!(
123
+ (stats.live, stats.slots, stats.retained, stats.reclaimed),
124
+ (6, 6, 1, 0)
125
+ );
126
+ assert!(stats.free.is_empty());
127
+ assert_eq!(arena.nodes[3].parent, Some(2));
128
+ assert_eq!(arena.nodes[3].next, Some(4));
129
+ assert_eq!(arena.nodes[2].children.as_slice(), &[3, 4]);
130
+ }
131
+
132
+ #[test]
133
+ fn unpinned_payloads_are_cleared_and_trailing_slots_trimmed() {
134
+ let mut arena = fixture();
135
+ let stats = collect(&mut arena, &HashMap::new());
136
+ assert_eq!((stats.live, stats.slots, stats.reclaimed), (3, 6, 3));
137
+ assert_eq!(stats.free, vec![2, 3, 4]);
138
+ let node = &arena.nodes[3];
139
+ assert_eq!(node.kind, "released");
140
+ assert!(node.data.is_none() && node.name.is_none());
141
+ assert!(node.attribs.len() == 0 && node.children.is_empty() && node.extras.is_empty());
142
+ assert!(node.parent.is_none() && node.prev.is_none() && node.next.is_none());
143
+ arena.detach(5);
144
+ let stats = collect(&mut arena, &HashMap::from([(5, 0)]));
145
+ assert_eq!(
146
+ (stats.live, stats.slots, stats.retained, stats.reclaimed),
147
+ (2, 2, 0, 1)
148
+ );
149
+ assert!(stats.free.is_empty());
150
+ assert_eq!(collect(&mut arena, &HashMap::new()).reclaimed, 0);
151
+ }
152
+
153
+ #[test]
154
+ fn cyclic_components_are_marked_once() {
155
+ let mut arena = fixture();
156
+ arena.nodes[2].parent = Some(3);
157
+ arena.nodes[3].children.push(2);
158
+ let stats = collect(&mut arena, &HashMap::from([(3, 1)]));
159
+ assert_eq!((stats.live, stats.retained, stats.reclaimed), (6, 1, 0));
160
+ let stats = collect(&mut arena, &HashMap::new());
161
+ assert_eq!(stats.reclaimed, 3);
162
+ }
163
+
164
+ #[test]
165
+ fn managed_detached_handle_does_not_retain_the_original_document() {
166
+ let mut arena = fixture();
167
+ let stats = collect_managed(&mut arena, &HashMap::from([(3, 1)]), false, 0);
168
+ assert_eq!((stats.live, stats.retained, stats.reclaimed), (3, 1, 3));
169
+ assert_eq!(arena.nodes[0].kind, "released");
170
+ assert_eq!(arena.nodes[1].kind, "released");
171
+ assert_eq!(arena.nodes[2].name.as_deref(), Some("detached"));
172
+ assert_eq!(arena.nodes[3].parent, Some(2));
173
+ assert_eq!(arena.nodes[3].next, Some(4));
174
+ }
175
+ }