@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,4120 @@
1
+ //! Immutable HTML5 documents, exposed to Node.js through Node-API.
2
+ //!
3
+ //! Parsing and extraction tasks own their input and output. In particular, no
4
+ //! JavaScript value or borrowed DOM crosses into the libuv or Rayon workers.
5
+
6
+ use std::borrow::Cow;
7
+ use std::cell::{Cell, RefCell};
8
+ use std::collections::{HashMap, HashSet};
9
+
10
+ use ego_tree::{NodeId, NodeRef};
11
+ use html5ever::tendril::TendrilSink;
12
+ use napi::bindgen_prelude::*;
13
+ use napi_derive::napi;
14
+ use scraper::{Html, Node, Selector};
15
+ use serde::Serialize;
16
+ use serde_json::{Map, Value};
17
+
18
+ use codec::JsText;
19
+
20
+ #[cfg(target_os = "linux")]
21
+ #[global_allocator]
22
+ static ALLOCATOR: mimalloc::MiMalloc = mimalloc::MiMalloc;
23
+
24
+ mod arena;
25
+ mod attributes;
26
+ mod codec;
27
+ mod diagnostics;
28
+ mod document_order;
29
+ mod extraction;
30
+ mod gc;
31
+ mod hook_sink;
32
+ mod hook_stream;
33
+ mod id_hash;
34
+ mod immutable;
35
+ mod location;
36
+ mod markup_tokenizer;
37
+ mod runtime;
38
+ mod selector;
39
+ mod sink;
40
+ mod snapshot;
41
+ mod stream;
42
+ mod tokenizer;
43
+ mod tracker;
44
+
45
+ pub use hook_stream::NativeHookStream;
46
+ pub use runtime::{configure_worker_pool, worker_pool_info, WorkerPoolInfo};
47
+ pub use stream::NativeStream;
48
+
49
+ const HTML_NS: &str = "http://www.w3.org/1999/xhtml";
50
+ const XML_NS: &str = "http://www.w3.org/XML/1998/namespace";
51
+ const XMLNS_NS: &str = "http://www.w3.org/2000/xmlns/";
52
+ const XLINK_NS: &str = "http://www.w3.org/1999/xlink";
53
+
54
+ #[derive(Serialize)]
55
+ struct OffsetNode<'a> {
56
+ id: u64,
57
+ #[serde(rename = "type")]
58
+ kind: &'a Cow<'static, str>,
59
+ #[serde(skip_serializing_if = "Option::is_none")]
60
+ name: Option<&'a String>,
61
+ #[serde(skip_serializing_if = "Option::is_none")]
62
+ data: Option<&'a String>,
63
+ attribs: &'a arena::Attributes,
64
+ children: OffsetChildren<'a>,
65
+ parent: Option<u64>,
66
+ prev: Option<u64>,
67
+ next: Option<u64>,
68
+ #[serde(skip_serializing_if = "Option::is_none")]
69
+ namespace: Option<&'a Cow<'static, str>>,
70
+ #[serde(rename = "attribsNamespace", skip_serializing_if = "Option::is_none")]
71
+ attribs_namespace: Option<&'a Map<String, Value>>,
72
+ #[serde(rename = "attribsPrefix", skip_serializing_if = "Option::is_none")]
73
+ attribs_prefix: Option<&'a Map<String, Value>>,
74
+ #[serde(flatten)]
75
+ extras: &'a arena::FieldMap,
76
+ }
77
+
78
+ struct OffsetChildren<'a> {
79
+ ids: &'a [u32],
80
+ base: u64,
81
+ }
82
+
83
+ impl Serialize for OffsetChildren<'_> {
84
+ fn serialize<S: serde::Serializer>(
85
+ &self,
86
+ serializer: S,
87
+ ) -> std::result::Result<S::Ok, S::Error> {
88
+ use serde::ser::SerializeSeq;
89
+ let mut seq = serializer.serialize_seq(Some(self.ids.len()))?;
90
+ for id in self.ids {
91
+ seq.serialize_element(&(self.base + u64::from(*id)))?;
92
+ }
93
+ seq.end()
94
+ }
95
+ }
96
+
97
+ struct OffsetRecords<'a> {
98
+ nodes: Vec<&'a arena::DomNode>,
99
+ base: u64,
100
+ }
101
+
102
+ impl Serialize for OffsetRecords<'_> {
103
+ fn serialize<S: serde::Serializer>(
104
+ &self,
105
+ serializer: S,
106
+ ) -> std::result::Result<S::Ok, S::Error> {
107
+ use serde::ser::SerializeSeq;
108
+ let mut seq = serializer.serialize_seq(Some(self.nodes.len()))?;
109
+ for node in &self.nodes {
110
+ seq.serialize_element(&offset_node(node, self.base))?;
111
+ }
112
+ seq.end()
113
+ }
114
+ }
115
+
116
+ fn offset_node(node: &arena::DomNode, base: u64) -> OffsetNode<'_> {
117
+ let link = |id: Option<u32>| id.map(|id| base + u64::from(id));
118
+ OffsetNode {
119
+ id: base + u64::from(node.id),
120
+ kind: &node.kind,
121
+ name: node.name.as_ref(),
122
+ data: node.data.as_ref(),
123
+ attribs: &node.attribs,
124
+ children: OffsetChildren {
125
+ ids: &node.children,
126
+ base,
127
+ },
128
+ parent: link(node.parent),
129
+ prev: link(node.prev),
130
+ next: link(node.next),
131
+ namespace: node.namespace.as_ref(),
132
+ attribs_namespace: node.attribs_namespace.as_deref(),
133
+ attribs_prefix: node.attribs_prefix.as_deref(),
134
+ extras: &node.extras,
135
+ }
136
+ }
137
+
138
+ /// Writes an attribute's serialized name. Like parse5, the namespace rather
139
+ /// than the stored prefix selects the qualifier: HTML5 gives foreign `xmlns`
140
+ /// attributes an empty prefix, which must not become `:xmlns`. Other namespaces
141
+ /// keep a non-empty prefix; an empty or missing one writes the bare name.
142
+ pub(crate) fn push_attribute_name(
143
+ output: &mut String,
144
+ local: &str,
145
+ namespace: Option<&str>,
146
+ prefix: Option<&str>,
147
+ ) {
148
+ match namespace.filter(|namespace| !namespace.is_empty()) {
149
+ None => {}
150
+ Some(XML_NS) => output.push_str("xml:"),
151
+ Some(XMLNS_NS) => {
152
+ if local != "xmlns" {
153
+ output.push_str("xmlns:");
154
+ }
155
+ }
156
+ Some(XLINK_NS) => output.push_str("xlink:"),
157
+ Some(_) => {
158
+ if let Some(prefix) = prefix.filter(|prefix| !prefix.is_empty()) {
159
+ output.push_str(prefix);
160
+ output.push(':');
161
+ }
162
+ }
163
+ }
164
+ output.push_str(local);
165
+ }
166
+
167
+ /// Orders attributes the way a JavaScript object enumerates its keys: integer
168
+ /// keys first, ascending, then insertion order. Cheerio serializes attributes
169
+ /// from such an object.
170
+ pub(crate) fn js_property_order<'a, T, I>(entries: I) -> JsPropertyOrder<I>
171
+ where
172
+ I: Iterator<Item = (&'a str, T)> + Clone,
173
+ {
174
+ if !entries.clone().any(|(name, _)| array_index(name).is_some()) {
175
+ return JsPropertyOrder::Unchanged(entries);
176
+ }
177
+ let mut keyed: Vec<_> = entries
178
+ .map(|entry| {
179
+ (
180
+ array_index(entry.0).map_or((1, 0), |index| (0, index)),
181
+ entry,
182
+ )
183
+ })
184
+ .collect();
185
+ keyed.sort_by_key(|(key, _)| *key);
186
+ let sorted: Vec<_> = keyed.into_iter().map(|(_, entry)| entry).collect();
187
+ JsPropertyOrder::Sorted(sorted.into_iter())
188
+ }
189
+
190
+ pub(crate) enum JsPropertyOrder<I: Iterator> {
191
+ Unchanged(I),
192
+ Sorted(std::vec::IntoIter<I::Item>),
193
+ }
194
+
195
+ impl<I: Iterator> Iterator for JsPropertyOrder<I> {
196
+ type Item = I::Item;
197
+ fn next(&mut self) -> Option<Self::Item> {
198
+ match self {
199
+ Self::Unchanged(entries) => entries.next(),
200
+ Self::Sorted(entries) => entries.next(),
201
+ }
202
+ }
203
+ }
204
+
205
+ fn array_index(name: &str) -> Option<u32> {
206
+ let is_canonical = !name.is_empty()
207
+ && name.len() <= 10
208
+ && name.bytes().all(|byte| byte.is_ascii_digit())
209
+ && (name == "0" || !name.starts_with('0'));
210
+ is_canonical
211
+ .then(|| name.parse::<u64>().ok())
212
+ .flatten()
213
+ .filter(|value| *value < u64::from(u32::MAX))
214
+ .map(|value| value as u32)
215
+ }
216
+
217
+ #[napi(custom_finalize)]
218
+ pub struct NativeDocument {
219
+ html: RefCell<Option<Html>>,
220
+ // Stable public IDs are preorder positions, rather than parser allocation
221
+ // positions: HTML5 foster parenting can change the latter's ordering.
222
+ ids: RefCell<Vec<NodeId>>,
223
+ reverse: RefCell<id_hash::IdMap<NodeId, u32>>,
224
+ /// Parent/child links of the parse tree, built on the first query that needs them.
225
+ tree_index: RefCell<Option<immutable::TreeIndex>>,
226
+ pub(crate) arena: RefCell<Option<arena::DomArena>>,
227
+ options: Value,
228
+ source: RefCell<Option<String>>,
229
+ retained: RefCell<HashMap<u32, usize>>,
230
+ needs_collect: Cell<bool>,
231
+ managed: Cell<bool>,
232
+ /// Slots managed collections keep regardless of pins.
233
+ kept_slots: Cell<u32>,
234
+ /// The document index JavaScript routes this document's node ids by.
235
+ routing_index: Cell<Option<u32>>,
236
+ /// Nodes were removed from the parse tree, so traversal order no longer
237
+ /// matches local ids and parsed nodes may be orphans.
238
+ tree_detached: Cell<bool>,
239
+ /// Parents whose children a parse-tree removal changed since JavaScript last asked.
240
+ tree_touched: RefCell<Vec<u32>>,
241
+ _live: LiveDocument,
242
+ diagnostics: RefCell<Vec<Value>>,
243
+ locations: RefCell<HashMap<u32, Value>>,
244
+ // V8 cannot see native allocations. Reporting them lets its GC collect
245
+ // unreachable documents under native memory pressure.
246
+ content_bytes: Cell<usize>,
247
+ reported_heap_bytes: Cell<i64>,
248
+ /// Results of extraction plans run on the parsing thread, in request
249
+ /// order, until JavaScript takes them.
250
+ prepared_plans: RefCell<Option<Vec<Option<String>>>>,
251
+ /// A snapshot built on the parsing thread, until JavaScript takes it.
252
+ prepared_snapshot: RefCell<Option<snapshot::NodeSnapshot>>,
253
+ }
254
+
255
+ /// Node-API calls the finalizers of a module that declares the experimental
256
+ /// version directly from garbage collection, rather than on a later turn of
257
+ /// the event loop, so a synchronous loop over documents frees each native tree
258
+ /// as soon as V8 collects its document. Finalizers must therefore not call
259
+ /// into V8: see `NativeDocument::finalize`.
260
+ #[no_mangle]
261
+ pub extern "C" fn node_api_module_get_api_version_v1() -> i32 {
262
+ NAPI_VERSION_EXPERIMENTAL
263
+ }
264
+
265
+ const NAPI_VERSION_EXPERIMENTAL: i32 = i32::MAX;
266
+
267
+ thread_local! {
268
+ /// External memory of finalized documents not yet returned to V8.
269
+ static RELEASED_HEAP_BYTES: Cell<i64> = const { Cell::new(0) };
270
+ /// Routing indexes of finalized documents, free for new documents.
271
+ static FREE_ROUTING_INDEXES: RefCell<Vec<u32>> = const { RefCell::new(Vec::new()) };
272
+ static NEXT_ROUTING_INDEX: Cell<u32> = const { Cell::new(0) };
273
+ }
274
+
275
+ static LIVE_DOCUMENTS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
276
+
277
+ /// Counts documents whose native storage has not been dropped yet.
278
+ struct LiveDocument;
279
+
280
+ impl LiveDocument {
281
+ fn new() -> Self {
282
+ LIVE_DOCUMENTS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
283
+ Self
284
+ }
285
+ }
286
+
287
+ impl Drop for LiveDocument {
288
+ fn drop(&mut self) {
289
+ LIVE_DOCUMENTS.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
290
+ }
291
+ }
292
+
293
+ /// Trees with more nodes than this are freed on the shared pool, so a
294
+ /// collection pause does not wait for them.
295
+ const POOL_RELEASE_NODES: usize = 4096;
296
+
297
+ impl ObjectFinalize for NativeDocument {
298
+ fn finalize(self, _env: Env) -> Result<()> {
299
+ // Adjusting external memory during garbage collection is not allowed;
300
+ // the next report on this thread returns it.
301
+ let reported = self.reported_heap_bytes.get();
302
+ RELEASED_HEAP_BYTES.with(|released| released.set(released.get() + reported));
303
+ if let Some(index) = self.routing_index.get() {
304
+ FREE_ROUTING_INDEXES.with(|free| free.borrow_mut().push(index));
305
+ }
306
+ let nodes = self.ids.borrow().len()
307
+ + self
308
+ .arena
309
+ .borrow()
310
+ .as_ref()
311
+ .map_or(0, |arena| arena.nodes.len());
312
+ if nodes > POOL_RELEASE_NODES {
313
+ runtime::release_later((
314
+ self.html.into_inner(),
315
+ self.arena.into_inner(),
316
+ self.ids.into_inner(),
317
+ self.reverse.into_inner(),
318
+ self.tree_index.into_inner(),
319
+ ));
320
+ }
321
+ Ok(())
322
+ }
323
+ }
324
+
325
+ fn html_node_content_bytes(node: &Node) -> usize {
326
+ match node {
327
+ Node::Text(text) => text.len(),
328
+ Node::Comment(comment) => comment.len(),
329
+ Node::Element(element) => element.attrs.iter().map(|(_, value)| value.len()).sum(),
330
+ _ => 0,
331
+ }
332
+ }
333
+
334
+ /// Approximate retained bytes per node in each representation, excluding the
335
+ /// text and attribute content counted separately. V8 treats the total only as
336
+ /// allocation pressure, so these err on the high side.
337
+ const IMMUTABLE_NODE_BYTES: usize = 256;
338
+ const ARENA_NODE_BYTES: usize = 512;
339
+ /// Smaller changes to an already reported size are skipped to keep operations cheap.
340
+ const HEAP_REPORT_STEP: i64 = 64 * 1024;
341
+
342
+ impl NativeDocument {
343
+ pub(crate) fn from_arena(arena: arena::DomArena, options: Value) -> Self {
344
+ let content_bytes = arena.nodes.iter().map(arena::DomNode::content_bytes).sum();
345
+ Self::from_arena_sized(arena, options, content_bytes)
346
+ }
347
+
348
+ /// A document over `arena` whose text and attribute content takes about
349
+ /// `content_bytes`, such as the length of the source it was parsed from.
350
+ fn from_arena_sized(arena: arena::DomArena, options: Value, content_bytes: usize) -> Self {
351
+ if arena.nodes.len() > POOL_RELEASE_NODES {
352
+ runtime::start_release_thread();
353
+ }
354
+ runtime::return_parse_pages(arena.nodes.len());
355
+ Self {
356
+ html: RefCell::new(None),
357
+ ids: RefCell::new(Vec::new()),
358
+ reverse: RefCell::new(id_hash::IdMap::default()),
359
+ tree_index: RefCell::new(None),
360
+ arena: RefCell::new(Some(arena)),
361
+ options,
362
+ source: RefCell::new(None),
363
+ retained: RefCell::new(HashMap::new()),
364
+ needs_collect: Cell::new(false),
365
+ managed: Cell::new(false),
366
+ kept_slots: Cell::new(0),
367
+ routing_index: Cell::new(None),
368
+ tree_detached: Cell::new(false),
369
+ tree_touched: RefCell::new(Vec::new()),
370
+ _live: LiveDocument::new(),
371
+ diagnostics: RefCell::new(Vec::new()),
372
+ locations: RefCell::new(HashMap::new()),
373
+ content_bytes: Cell::new(content_bytes),
374
+ reported_heap_bytes: Cell::new(0),
375
+ prepared_plans: RefCell::new(None),
376
+ prepared_snapshot: RefCell::new(None),
377
+ }
378
+ }
379
+ pub(crate) fn set_diagnostic_records(&self, records: Vec<Value>) {
380
+ *self.diagnostics.borrow_mut() = records;
381
+ }
382
+ fn maybe_collect(&self) {
383
+ if self.needs_collect.get() {
384
+ self.collect_garbage();
385
+ }
386
+ }
387
+ #[cfg(test)]
388
+ fn parse(source: &str, is_document: bool) -> Self {
389
+ Self::parse_options(source, is_document, serde_json::json!({}), None)
390
+ }
391
+
392
+ fn estimated_heap_bytes(&self) -> i64 {
393
+ let live_arena_nodes = self.arena.borrow().as_ref().map_or(0, |arena| {
394
+ arena.nodes.len().saturating_sub(arena.free.len())
395
+ });
396
+ let immutable_nodes = self.ids.borrow().len();
397
+ let retained_source = self.source.borrow().as_ref().map_or(0, String::capacity);
398
+ let content = if live_arena_nodes + immutable_nodes == 0 {
399
+ 0
400
+ } else {
401
+ self.content_bytes.get()
402
+ };
403
+ let bytes = content
404
+ + retained_source
405
+ + immutable_nodes * IMMUTABLE_NODE_BYTES
406
+ + live_arena_nodes * ARENA_NODE_BYTES;
407
+ i64::try_from(bytes).unwrap_or(i64::MAX)
408
+ }
409
+
410
+ fn report_heap_bytes(&self, mut env: Env) {
411
+ let released = RELEASED_HEAP_BYTES.with(|released| released.replace(0));
412
+ if released != 0 && env.adjust_external_memory(-released).is_err() {
413
+ RELEASED_HEAP_BYTES.with(|pending| pending.set(pending.get() + released));
414
+ }
415
+ let reported = self.reported_heap_bytes.get();
416
+ let estimated = self.estimated_heap_bytes();
417
+ let change = estimated - reported;
418
+ let is_small_update = reported != 0 && estimated != 0 && change.abs() < HEAP_REPORT_STEP;
419
+ if change != 0 && !is_small_update && env.adjust_external_memory(change).is_ok() {
420
+ self.reported_heap_bytes.set(estimated);
421
+ }
422
+ }
423
+
424
+ fn reported<T>(&self, env: Env, value: T) -> T {
425
+ self.report_heap_bytes(env);
426
+ value
427
+ }
428
+
429
+ pub(crate) fn parse_options(
430
+ source: &str,
431
+ is_document: bool,
432
+ parser_options: Value,
433
+ context_name: Option<&str>,
434
+ ) -> Self {
435
+ let mut parser_options = arena::normalized_options(parser_options);
436
+ if codec::has_encoding(source) {
437
+ parser_options["__losslessUnicode"] = Value::Bool(true);
438
+ }
439
+ if let Some(events) = parser_options
440
+ .as_object_mut()
441
+ .and_then(|options| options.remove("__tokenEvents"))
442
+ {
443
+ return Self::from_arena_sized(
444
+ tokenizer::parse(source, &events, parser_options.clone()),
445
+ parser_options,
446
+ source.len(),
447
+ );
448
+ }
449
+ if arena::xml_mode(&parser_options)
450
+ || parser_options.get("_useHtmlParser2") == Some(&Value::Bool(true))
451
+ {
452
+ return Self::from_arena_sized(
453
+ arena::parse_xml(source, parser_options.clone()),
454
+ parser_options,
455
+ source.len(),
456
+ );
457
+ }
458
+
459
+ let capture = parser_options
460
+ .get("sourceCodeLocationInfo")
461
+ .is_some_and(|value| !matches!(value, Value::Null | Value::Bool(false)))
462
+ || parser_options.get("__collectParseErrors") == Some(&Value::Bool(true));
463
+ // HTML5 input preprocessing turns CRLF and CR into LF; html5ever does
464
+ // it one character at a time, which dominates pages with CRLF endings.
465
+ // Captured source locations must index the original input instead.
466
+ let normalized;
467
+ let source = if !capture && source.contains('\r') {
468
+ normalized = normalize_newlines(source);
469
+ normalized.as_str()
470
+ } else {
471
+ source
472
+ };
473
+ if !is_document && context_name.is_none() && !capture && is_plain_text(source) {
474
+ return Self::from_html(plain_text_fragment(source), false, parser_options, None);
475
+ }
476
+
477
+ let options = html5ever::driver::ParseOpts {
478
+ tokenizer: html5ever::tokenizer::TokenizerOpts {
479
+ discard_bom: false,
480
+ cheerox_codec: parser_options.get("__utf16Codec") == Some(&Value::Bool(true)),
481
+ ..Default::default()
482
+ },
483
+ tree_builder: html5ever::tree_builder::TreeBuilderOpts {
484
+ scripting_enabled: parser_options.get("scriptingEnabled")
485
+ != Some(&Value::Bool(false)),
486
+ ..Default::default()
487
+ },
488
+ };
489
+ let html = if is_document {
490
+ html5ever::driver::parse_document(
491
+ sink::CompatibleSink::new(sized_html(source, true)),
492
+ options,
493
+ )
494
+ .one(source)
495
+ } else {
496
+ // parse5's default fragment context is a template. A body context
497
+ // discards valid standalone table fragments such as <td> and <tr>.
498
+ html5ever::driver::parse_fragment(
499
+ sink::CompatibleSink::new(sized_html(source, false)),
500
+ options,
501
+ html5ever::QualName::new(
502
+ None,
503
+ html5ever::Namespace::from(
504
+ parser_options
505
+ .get("__contextNamespace")
506
+ .and_then(Value::as_str)
507
+ .unwrap_or(HTML_NS),
508
+ ),
509
+ html5ever::LocalName::from(context_name.unwrap_or("template")),
510
+ ),
511
+ Vec::new(),
512
+ false,
513
+ )
514
+ .one(source)
515
+ };
516
+
517
+ Self::from_html(
518
+ html,
519
+ is_document,
520
+ parser_options,
521
+ capture.then(|| source.to_string()),
522
+ )
523
+ }
524
+
525
+ pub(crate) fn from_html(
526
+ mut html: Html,
527
+ is_document: bool,
528
+ parser_options: Value,
529
+ source: Option<String>,
530
+ ) -> Self {
531
+ if !is_document {
532
+ unwrap_fragment_root(&mut html);
533
+ }
534
+
535
+ let mut locations = HashMap::new();
536
+ if parser_options.get("sourceCodeLocationInfo") == Some(&Value::Bool(true)) {
537
+ if let Some(source) = source.as_ref() {
538
+ let context = std::rc::Rc::new(diagnostics::DiagnosticContext::new(source));
539
+ let tracker = std::rc::Rc::new(tracker::Locations::new(context.clone()));
540
+ let mut tracked = diagnostics::parse_with_sink(
541
+ source,
542
+ is_document,
543
+ &parser_options,
544
+ sink::CompatibleSink::with_locations(
545
+ if is_document {
546
+ Html::new_document()
547
+ } else {
548
+ Html::new_fragment()
549
+ },
550
+ tracker.clone(),
551
+ ),
552
+ context,
553
+ |_, _| {},
554
+ );
555
+ if !is_document {
556
+ unwrap_fragment_root(&mut tracked);
557
+ }
558
+ let order: HashMap<_, _> = tracked
559
+ .tree
560
+ .root()
561
+ .descendants()
562
+ .enumerate()
563
+ .map(|(index, node)| (node.id(), index as u32))
564
+ .collect();
565
+ locations = tracker
566
+ .take()
567
+ .into_iter()
568
+ .filter_map(|(id, value)| order.get(&id).map(|id| (*id, value)))
569
+ .collect();
570
+ }
571
+ }
572
+
573
+ let node_count = html.tree.values().len();
574
+ let mut ids = Vec::with_capacity(node_count);
575
+ let mut reverse = id_hash::IdMap::with_capacity_and_hasher(node_count, Default::default());
576
+ let mut content_bytes = 0;
577
+ for node in html.tree.root().descendants() {
578
+ let id = node.id();
579
+ reverse.insert(id, ids.len() as u32);
580
+ content_bytes += html_node_content_bytes(node.value());
581
+ ids.push(id);
582
+ }
583
+ let diagnostics = if parser_options.get("__collectParseErrors") == Some(&Value::Bool(true))
584
+ {
585
+ source
586
+ .as_ref()
587
+ .map(|source| diagnostics::collect(source, is_document, &parser_options))
588
+ .unwrap_or_default()
589
+ } else {
590
+ Vec::new()
591
+ };
592
+ let source = parser_options
593
+ .get("sourceCodeLocationInfo")
594
+ .filter(|value| {
595
+ !matches!(value, Value::Null | Value::Bool(false))
596
+ && value != &&serde_json::json!(0)
597
+ && value != &&serde_json::json!("")
598
+ })
599
+ .and(source);
600
+ if ids.len() > POOL_RELEASE_NODES {
601
+ runtime::start_release_thread();
602
+ }
603
+ runtime::return_parse_pages(ids.len());
604
+ Self {
605
+ html: RefCell::new(Some(html)),
606
+ ids: RefCell::new(ids),
607
+ reverse: RefCell::new(reverse),
608
+ tree_index: RefCell::new(None),
609
+ arena: RefCell::new(None),
610
+ options: parser_options,
611
+ source: RefCell::new(source),
612
+ retained: RefCell::new(HashMap::new()),
613
+ needs_collect: Cell::new(false),
614
+ managed: Cell::new(false),
615
+ kept_slots: Cell::new(0),
616
+ routing_index: Cell::new(None),
617
+ tree_detached: Cell::new(false),
618
+ tree_touched: RefCell::new(Vec::new()),
619
+ _live: LiveDocument::new(),
620
+ diagnostics: RefCell::new(diagnostics),
621
+ locations: RefCell::new(locations),
622
+ content_bytes: Cell::new(content_bytes),
623
+ reported_heap_bytes: Cell::new(0),
624
+ prepared_plans: RefCell::new(None),
625
+ prepared_snapshot: RefCell::new(None),
626
+ }
627
+ }
628
+
629
+ /// The arena borrowed for writing. It drops the name index first: a write
630
+ /// may add, remove or reorder elements.
631
+ pub(crate) fn arena_for_write(&self) -> std::cell::RefMut<'_, Option<arena::DomArena>> {
632
+ let mut arena = self.arena.borrow_mut();
633
+ if let Some(arena) = arena.as_mut() {
634
+ arena.name_index = None;
635
+ }
636
+ arena
637
+ }
638
+
639
+ /// The arena borrowed for a write that changes only attributes or data,
640
+ /// which keeps the name index valid.
641
+ pub(crate) fn arena_for_attribute_write(
642
+ &self,
643
+ ) -> std::cell::RefMut<'_, Option<arena::DomArena>> {
644
+ self.arena.borrow_mut()
645
+ }
646
+
647
+ pub(crate) fn ensure_arena(&self) -> Result<()> {
648
+ let missing = self.arena.borrow().is_none();
649
+ if missing {
650
+ let records = {
651
+ let html = self.html.borrow();
652
+ let ids = self.ids.borrow();
653
+ let reverse = self.reverse.borrow();
654
+ let tree = SharedTree {
655
+ html: html
656
+ .as_ref()
657
+ .ok_or_else(|| Error::new(Status::GenericFailure, "Missing parse tree"))?,
658
+ ids: &ids,
659
+ reverse: &reverse,
660
+ };
661
+ runtime::chunked_map(ids.len(), CONVERSION_CHUNK, |range| tree.convert(range))
662
+ };
663
+ let mut arena = arena::DomArena::from_linked_records(records, self.options.clone());
664
+ if let Some(source) = self.source.borrow().as_ref() {
665
+ location::annotate(
666
+ source,
667
+ &mut arena.nodes,
668
+ self.options.get("scriptingEnabled") != Some(&Value::Bool(false)),
669
+ self.options.get("__utf16Codec") == Some(&Value::Bool(true)),
670
+ );
671
+ }
672
+ for (id, patch) in std::mem::take(&mut *self.locations.borrow_mut()) {
673
+ if let Some(node) = arena.nodes.get_mut(id as usize) {
674
+ if let Value::Object(patch) = patch {
675
+ let existing = node
676
+ .extras
677
+ .entry("sourceCodeLocation")
678
+ .or_insert_with(|| serde_json::json!({}));
679
+ if !existing.is_object() {
680
+ *existing = serde_json::json!({});
681
+ }
682
+ existing.as_object_mut().unwrap().extend(patch);
683
+ } else {
684
+ node.extras.insert("sourceCodeLocation".into(), patch);
685
+ }
686
+ }
687
+ }
688
+ *self.arena.borrow_mut() = Some(arena);
689
+ let html = self.html.borrow_mut().take();
690
+ let reverse = std::mem::take(&mut *self.reverse.borrow_mut());
691
+ let ids = std::mem::take(&mut *self.ids.borrow_mut());
692
+ // Freeing a large parse tree takes as long as copying it; the
693
+ // JavaScript thread need not wait for it. Waking a worker costs
694
+ // more than freeing a small one.
695
+ if ids.len() > POOL_RELEASE_NODES {
696
+ runtime::release_later((html, reverse, ids));
697
+ }
698
+ self.source.borrow_mut().take();
699
+ *self.tree_index.borrow_mut() = None;
700
+ }
701
+ Ok(())
702
+ }
703
+
704
+ fn with_node<T>(
705
+ &self,
706
+ id: u32,
707
+ operation: impl FnOnce(NodeRef<'_, Node>, &Html) -> T,
708
+ ) -> Option<T> {
709
+ let html = self.html.borrow();
710
+ let html = html.as_ref()?;
711
+ let node = html.tree.get(*self.ids.borrow().get(id as usize)?)?;
712
+ Some(operation(node, html))
713
+ }
714
+
715
+ /// Runs `operation` over the parse tree, or returns `None` once the
716
+ /// document has moved to the mutable arena.
717
+ fn with_tree_dom<T>(
718
+ &self,
719
+ options: &Value,
720
+ operation: impl FnOnce(&immutable::TreeDom<'_>) -> T,
721
+ ) -> Option<T> {
722
+ if self.arena.borrow().is_some() {
723
+ return None;
724
+ }
725
+ let html = self.html.borrow();
726
+ let html = html.as_ref()?;
727
+ let ids = self.ids.borrow();
728
+ if self.tree_index.borrow().is_none() {
729
+ let index = if self.tree_detached.get() {
730
+ immutable::TreeIndex::build_mapped(html, &ids, &self.reverse.borrow())
731
+ } else {
732
+ immutable::TreeIndex::build(html, &ids)
733
+ };
734
+ *self.tree_index.borrow_mut() = Some(index);
735
+ }
736
+ let index = self.tree_index.borrow();
737
+ let dom = immutable::TreeDom {
738
+ html,
739
+ ids: &ids,
740
+ index: index.as_ref()?,
741
+ options,
742
+ };
743
+ Some(operation(&dom))
744
+ }
745
+
746
+ fn immutable_dom_node(&self, id: u32) -> Option<arena::DomNode> {
747
+ let html = self.html.borrow();
748
+ let ids = self.ids.borrow();
749
+ let reverse = self.reverse.borrow();
750
+ convert_node(html.as_ref()?, &ids, &reverse, id)
751
+ }
752
+
753
+ fn immutable_record(&self, id: u32) -> Option<Value> {
754
+ serde_json::to_value(self.immutable_dom_node(id)?).ok()
755
+ }
756
+
757
+ /// Read commands answered from the parse tree; `None` when the command
758
+ /// needs the mutable arena.
759
+ fn tree_read(
760
+ &self,
761
+ command: &str,
762
+ args: &Value,
763
+ ids: &[u32],
764
+ options: &Value,
765
+ ) -> Option<Result<Value>> {
766
+ match command {
767
+ "select" | "matches" => {
768
+ let mut options = options.clone();
769
+ if let Some(matches) = args.get("pseudoMatches") {
770
+ options["__pseudoMatches"] = matches.clone();
771
+ }
772
+ let selector = args["selector"].as_str().unwrap_or("");
773
+ let contexts = args.get("contexts").map(arena::value_ids);
774
+ self.with_tree_dom(&options, |dom| {
775
+ if command == "select" {
776
+ selector::select(dom, selector, contexts.as_deref())
777
+ } else {
778
+ selector::filter(dom, selector, ids)
779
+ }
780
+ .map(|ids| serde_json::json!(ids))
781
+ })
782
+ }
783
+ "uniqueSort" => {
784
+ // Removed nodes have no place in the parse tree's order, so
785
+ // the arena orders them.
786
+ if self.tree_detached.get() {
787
+ return None;
788
+ }
789
+ self.with_tree_dom(options, |dom| {
790
+ if !document_order::is_document_ordered(dom, ids) {
791
+ return Ok(serde_json::json!(document_order::unique_sort(dom, ids)));
792
+ }
793
+ // Local ids of a parse tree are preorder positions: document order.
794
+ let mut unique = ids.to_vec();
795
+ unique.sort_unstable();
796
+ unique.dedup();
797
+ Ok(serde_json::json!(unique))
798
+ })
799
+ }
800
+ "hasClass" => {
801
+ let needle = args["name"].as_str().unwrap_or("");
802
+ self.with_tree_dom(options, |dom| {
803
+ Ok(Value::Bool(ids.iter().any(|id| {
804
+ dom.is_element(*id)
805
+ && dom
806
+ .element_attr(*id, "class")
807
+ .is_some_and(|classes| immutable::has_class_token(classes, needle))
808
+ })))
809
+ })
810
+ }
811
+ "contains" => {
812
+ let container = args
813
+ .get("container")
814
+ .or_else(|| args.get("parent"))
815
+ .and_then(Value::as_u64)
816
+ .unwrap_or(0) as u32;
817
+ let contained = args
818
+ .get("contained")
819
+ .or_else(|| args.get("child"))
820
+ .and_then(Value::as_u64)? as u32;
821
+ self.with_tree_dom(options, |dom| {
822
+ Ok(Value::Bool(dom.index.contains(container, contained)))
823
+ })
824
+ }
825
+ "extractPlan" => Some(extraction::execute_in(self, &args["plan"], ids)),
826
+ _ => None,
827
+ }
828
+ }
829
+
830
+ /// A node with only the kind, name and attributes the attribute commands read.
831
+ fn attribute_node(&self, id: u32) -> Option<arena::DomNode> {
832
+ self.with_node(id, |node, _| {
833
+ let mut record = arena::DomNode::new("text");
834
+ if let Node::Element(element) = node.value() {
835
+ record.kind = match element.name() {
836
+ "script" => "script",
837
+ "style" => "style",
838
+ _ => "tag",
839
+ }
840
+ .into();
841
+ record.name = Some(element.name().into());
842
+ if !element.attrs.is_empty() {
843
+ record.attribs = arena::Attributes::with_capacity(element.attrs.len());
844
+ }
845
+ for (name, value) in &element.attrs {
846
+ record
847
+ .attribs
848
+ .insert(name.local.to_string(), Value::String(value.to_string()));
849
+ }
850
+ }
851
+ record
852
+ })
853
+ }
854
+
855
+ /// Applies a write that only changes attributes to the parse tree, so the
856
+ /// document is not copied into the mutable arena. The arena's own attribute
857
+ /// logic runs on a temporary arena holding just the target elements.
858
+ fn tree_attribute_write(
859
+ &self,
860
+ command: &str,
861
+ args: &Value,
862
+ ids: &[u32],
863
+ options: &Value,
864
+ ) -> Option<Result<Value>> {
865
+ let is_write = match command {
866
+ "attr" | "css" => args.get("value").is_some(),
867
+ "removeAttr" | "addClass" | "removeClass" | "toggleClass" => true,
868
+ _ => false,
869
+ };
870
+ if !is_write || self.source.borrow().is_some() {
871
+ return None;
872
+ }
873
+ let mut targets: Vec<u32> = Vec::new();
874
+ for id in ids {
875
+ if !targets.contains(id) {
876
+ targets.push(*id);
877
+ }
878
+ }
879
+ let nodes: Vec<arena::DomNode> = targets
880
+ .iter()
881
+ .map(|id| self.attribute_node(*id))
882
+ .collect::<Option<_>>()?;
883
+ let before: Vec<arena::Attributes> =
884
+ nodes.iter().map(|node| node.attribs.clone()).collect();
885
+ let mut scratch = arena::DomArena::from_records(nodes, options.clone());
886
+ if self.options.get("__utf16Codec") == Some(&Value::Bool(true)) {
887
+ scratch.options["__utf16Codec"] = Value::Bool(true);
888
+ }
889
+ let mut local_args = args.clone();
890
+ local_args["ids"] = serde_json::json!(ids
891
+ .iter()
892
+ .map(|id| targets.iter().position(|target| target == id).unwrap_or(0))
893
+ .collect::<Vec<_>>());
894
+ let result = match attributes::execute(&mut scratch, command, &local_args) {
895
+ Ok(Some(result)) => result,
896
+ Ok(None) => return None,
897
+ Err(error) => return Some(Err(error)),
898
+ };
899
+ let mut html = self.html.borrow_mut();
900
+ let html = html.as_mut()?;
901
+ let node_ids = self.ids.borrow();
902
+ for (index, id) in targets.iter().enumerate() {
903
+ let after = &scratch.nodes[index].attribs;
904
+ if *after == before[index] {
905
+ continue;
906
+ }
907
+ let mut node = html.tree.get_mut(*node_ids.get(*id as usize)?)?;
908
+ let Node::Element(element) = node.value() else {
909
+ continue;
910
+ };
911
+ let attributes = after
912
+ .iter()
913
+ .map(|(name, value)| {
914
+ let qualified = element
915
+ .attrs
916
+ .iter()
917
+ .map(|(key, _)| key)
918
+ .rev()
919
+ .find(|key| key.local.as_ref() == name)
920
+ .cloned()
921
+ .unwrap_or_else(|| {
922
+ html5ever::QualName::new(
923
+ None,
924
+ html5ever::ns!(),
925
+ html5ever::LocalName::from(name.as_str()),
926
+ )
927
+ });
928
+ let value = value
929
+ .as_str()
930
+ .map_or_else(|| value.to_string(), str::to_owned);
931
+ html5ever::Attribute {
932
+ name: qualified,
933
+ value: value.into(),
934
+ }
935
+ })
936
+ .collect();
937
+ *element = scraper::node::Element::new(element.name.clone(), attributes);
938
+ }
939
+ Some(Ok(result))
940
+ }
941
+
942
+ /// Cheerio-compatible selection without copying an unchanged document.
943
+ fn select_native(&self, source: &str, contexts: Option<&[u32]>) -> Result<Vec<u32>> {
944
+ if let Some(result) =
945
+ self.with_tree_dom(&self.options, |dom| selector::select(dom, source, contexts))
946
+ {
947
+ return result;
948
+ }
949
+ self.ensure_arena()?;
950
+ selector::select(self.arena.borrow().as_ref().unwrap(), source, contexts)
951
+ }
952
+
953
+ fn select_ids(&self, source: &str, contexts: Option<&[u32]>) -> Result<Vec<u32>> {
954
+ self.maybe_collect();
955
+ let contexts = contexts.filter(|contexts| *contexts != [0]);
956
+ let simple_tag = !source.is_empty()
957
+ && source.as_bytes()[0].is_ascii_lowercase()
958
+ && source
959
+ .bytes()
960
+ .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-');
961
+ if self.arena.borrow().is_some()
962
+ || self.tree_detached.get()
963
+ || contexts.is_some()
964
+ || self.options.get("__losslessUnicode") == Some(&Value::Bool(true))
965
+ || codec::has_encoding(source)
966
+ || source.contains(':')
967
+ || source.contains('\\')
968
+ || source.chars().any(char::is_uppercase)
969
+ || source
970
+ .trim_start()
971
+ .chars()
972
+ .next()
973
+ .is_some_and(|ch| matches!(ch, '>' | '+' | '~'))
974
+ || source.contains("!=")
975
+ || compares_case_insensitive_attribute(source)
976
+ {
977
+ return self.select_native(source, contexts);
978
+ }
979
+ let Some(selector) = parsed_selector(source) else {
980
+ return self.select_native(source, contexts);
981
+ };
982
+ let mut matched = Vec::new();
983
+
984
+ if let Some(html) = self.html.borrow().as_ref() {
985
+ for element in html.select(&selector) {
986
+ if let Some(id) = self.reverse.borrow().get(&element.id()) {
987
+ matched.push(*id);
988
+ }
989
+ }
990
+ }
991
+ if !simple_tag {
992
+ matched.sort_unstable();
993
+ matched.dedup();
994
+ }
995
+ Ok(matched)
996
+ }
997
+
998
+ /// Return the first match without collecting all matches for an unchanged,
999
+ /// document-root query with an unambiguous lowercase tag selector.
1000
+ fn select_first_id(&self, source: &str, contexts: Option<&[u32]>) -> Result<Option<u32>> {
1001
+ self.maybe_collect();
1002
+ let simple = !source.is_empty()
1003
+ && source.as_bytes()[0].is_ascii_lowercase()
1004
+ && source
1005
+ .bytes()
1006
+ .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-');
1007
+ if simple
1008
+ && contexts.is_none_or(|ids| ids == [0])
1009
+ && self.arena.borrow().is_none()
1010
+ && !self.tree_detached.get()
1011
+ && self.options.get("__losslessUnicode") != Some(&Value::Bool(true))
1012
+ {
1013
+ if let Some(selector) = parsed_selector(source) {
1014
+ let html = self.html.borrow();
1015
+ if let Some(html) = html.as_ref() {
1016
+ let reverse = self.reverse.borrow();
1017
+ return Ok(html
1018
+ .select(&selector)
1019
+ .find_map(|element| reverse.get(&element.id()).copied()));
1020
+ }
1021
+ }
1022
+ }
1023
+ Ok(self.select_ids(source, contexts)?.first().copied())
1024
+ }
1025
+
1026
+ fn text_ids(&self, ids: &[u32]) -> String {
1027
+ if let Some(arena) = self.arena.borrow().as_ref() {
1028
+ return arena.text(ids);
1029
+ }
1030
+ let mut text = String::new();
1031
+ for id in ids {
1032
+ self.with_node(*id, |node, _| {
1033
+ for descendant in node.descendants() {
1034
+ if let Node::Text(data) = descendant.value() {
1035
+ text.push_str(data);
1036
+ }
1037
+ }
1038
+ });
1039
+ }
1040
+ text
1041
+ }
1042
+
1043
+ fn attribute(&self, id: u32, name: &str) -> Option<String> {
1044
+ if let Some(arena) = self.arena.borrow().as_ref() {
1045
+ return arena.attr(id, name).map(str::to_owned);
1046
+ }
1047
+ self.with_node(id, |node, _| {
1048
+ node.value().as_element().and_then(|element| {
1049
+ element.attrs.iter().find_map(|(key, value)| {
1050
+ (key.local.as_ref() == name).then(|| value.to_string())
1051
+ })
1052
+ })
1053
+ })
1054
+ .flatten()
1055
+ }
1056
+
1057
+ fn serialize_nodes(&self, ids: &[u32], children_only: bool) -> String {
1058
+ if self.arena.borrow().is_some() {
1059
+ let mut arena = self.arena.borrow_mut();
1060
+ let arena = arena.as_mut().unwrap();
1061
+ return if children_only {
1062
+ ids.iter().map(|id| arena.inner_html_mut(*id)).collect()
1063
+ } else {
1064
+ arena.serialize_mut(ids, &arena.options.clone())
1065
+ };
1066
+ }
1067
+ let mut output = String::new();
1068
+ for id in ids {
1069
+ self.with_node(*id, |node, _| {
1070
+ if children_only {
1071
+ for child in node.children() {
1072
+ serialize_node(child, &mut output);
1073
+ }
1074
+ } else {
1075
+ serialize_node(node, &mut output);
1076
+ }
1077
+ });
1078
+ }
1079
+ output
1080
+ }
1081
+ }
1082
+
1083
+ #[napi]
1084
+ impl NativeDocument {
1085
+ #[napi(constructor)]
1086
+ pub fn new(html: String, is_document: bool, options_json: Option<String>) -> Self {
1087
+ let options = options_json
1088
+ .and_then(|value| serde_json::from_str(&value).ok())
1089
+ .unwrap_or_else(|| serde_json::json!({}));
1090
+ Self::parse_options(&html, is_document, options, None)
1091
+ }
1092
+
1093
+ #[napi]
1094
+ pub fn retain(&self, ids: Vec<u32>) {
1095
+ self.managed.set(true);
1096
+ let mut pins = self.retained.borrow_mut();
1097
+ for id in ids {
1098
+ if self
1099
+ .arena
1100
+ .borrow()
1101
+ .as_ref()
1102
+ .map_or((id as usize) < self.ids.borrow().len(), |arena| {
1103
+ arena.node(id).is_some()
1104
+ })
1105
+ {
1106
+ *pins.entry(id).or_insert(0) += 1;
1107
+ }
1108
+ }
1109
+ }
1110
+ #[napi]
1111
+ pub fn release(&self, ids: Vec<u32>) {
1112
+ let mut pins = self.retained.borrow_mut();
1113
+ for id in ids {
1114
+ if let Some(count) = pins.get_mut(&id) {
1115
+ if *count > 1 {
1116
+ *count -= 1;
1117
+ } else {
1118
+ pins.remove(&id);
1119
+ self.needs_collect.set(true);
1120
+ }
1121
+ }
1122
+ }
1123
+ }
1124
+ /// Every live node that `root` does not reach; a parse tree has none.
1125
+ #[napi(js_name = "detachedSlots")]
1126
+ pub fn detached_slots(&self, root: u32) -> Uint32Array {
1127
+ if let Some(arena) = self.arena.borrow().as_ref() {
1128
+ return Uint32Array::from(arena.detached_slots(root));
1129
+ }
1130
+ Uint32Array::from(self.tree_detached_slots(root))
1131
+ }
1132
+
1133
+ /// Nodes removed from the parse tree itself, which keeps them in place
1134
+ /// but unreachable from `root`.
1135
+ fn tree_detached_slots(&self, root: u32) -> Vec<u32> {
1136
+ if !self.tree_detached.get() {
1137
+ return Vec::new();
1138
+ }
1139
+ let html = self.html.borrow();
1140
+ let ids = self.ids.borrow();
1141
+ let reverse = self.reverse.borrow();
1142
+ let Some(root) = html
1143
+ .as_ref()
1144
+ .zip(ids.get(root as usize))
1145
+ .and_then(|(html, id)| html.tree.get(*id))
1146
+ else {
1147
+ return Vec::new();
1148
+ };
1149
+ let mut attached = vec![false; ids.len()];
1150
+ for node in root.descendants() {
1151
+ if let Some(local) = reverse.get(&node.id()) {
1152
+ attached[*local as usize] = true;
1153
+ }
1154
+ }
1155
+ (0..ids.len() as u32)
1156
+ .filter(|local| !attached[*local as usize])
1157
+ .collect()
1158
+ }
1159
+
1160
+ /// Sets an attribute on elements, or removes it when `value` is `null`,
1161
+ /// exactly as the `attr` command does, without copying an unchanged
1162
+ /// document into the mutable arena.
1163
+ #[napi(js_name = "setAttribute")]
1164
+ pub fn set_attribute(&self, ids: Vec<u32>, name: String, value: Option<String>) -> Result<()> {
1165
+ if self.arena.borrow().is_none() && self.source.borrow().is_none() {
1166
+ if let Some(html) = self.html.borrow_mut().as_mut() {
1167
+ let node_ids = self.ids.borrow();
1168
+ for id in &ids {
1169
+ let Some(mut node) = node_ids
1170
+ .get(*id as usize)
1171
+ .and_then(|node_id| html.tree.get_mut(*node_id))
1172
+ else {
1173
+ continue;
1174
+ };
1175
+ if let Node::Element(element) = node.value() {
1176
+ let attributes = with_attribute(element, &name, value.as_deref());
1177
+ *element = scraper::node::Element::new(element.name.clone(), attributes);
1178
+ }
1179
+ }
1180
+ return Ok(());
1181
+ }
1182
+ }
1183
+ self.ensure_arena()?;
1184
+ if let Some(arena) = self.arena_for_attribute_write().as_mut() {
1185
+ arena.set_attr(&ids, &name, value.as_deref());
1186
+ }
1187
+ Ok(())
1188
+ }
1189
+
1190
+ #[napi(js_name = "addClass")]
1191
+ pub fn add_class_for_js(&self, ids: Vec<u32>, name: String) -> Result<()> {
1192
+ self.ensure_arena()?;
1193
+ if let Some(arena) = self.arena_for_attribute_write().as_mut() {
1194
+ let args = serde_json::json!({ "ids": ids, "name": name });
1195
+ let _ = attributes::execute(arena, "addClass", &args)?;
1196
+ }
1197
+ Ok(())
1198
+ }
1199
+
1200
+ /// This document's routing index, assigned on first call. An index is
1201
+ /// reused only once its document has been finalized, when nothing in
1202
+ /// JavaScript can hold the document's node ids any more.
1203
+ #[napi(js_name = "routingIndex")]
1204
+ pub fn routing_index(&self) -> u32 {
1205
+ if let Some(index) = self.routing_index.get() {
1206
+ return index;
1207
+ }
1208
+ let index = FREE_ROUTING_INDEXES
1209
+ .with(|free| free.borrow_mut().pop())
1210
+ .unwrap_or_else(|| {
1211
+ NEXT_ROUTING_INDEX.with(|next| {
1212
+ let index = next.get();
1213
+ next.set(index.saturating_add(1));
1214
+ index
1215
+ })
1216
+ });
1217
+ self.routing_index.set(Some(index));
1218
+ index
1219
+ }
1220
+
1221
+ /// Documents in this process whose native storage has not been dropped,
1222
+ /// for tests of memory release; resident memory is a noisy signal.
1223
+ #[napi(js_name = "liveDocumentCount")]
1224
+ pub fn live_document_count(&self) -> u32 {
1225
+ LIVE_DOCUMENTS.load(std::sync::atomic::Ordering::Relaxed) as u32
1226
+ }
1227
+
1228
+ /// Whether removed created nodes and arena growth make a collection worthwhile.
1229
+ #[napi(js_name = "isCollectionDue")]
1230
+ pub fn is_collection_due(&self) -> bool {
1231
+ self.arena
1232
+ .borrow()
1233
+ .as_ref()
1234
+ .is_some_and(arena::DomArena::is_collection_due)
1235
+ }
1236
+
1237
+ /// The number of node slots, including released ones.
1238
+ #[napi(js_name = "slotCount")]
1239
+ pub fn slot_count(&self) -> u32 {
1240
+ match self.arena.borrow().as_ref() {
1241
+ Some(arena) => arena.nodes.len() as u32,
1242
+ None => self.ids.borrow().len() as u32,
1243
+ }
1244
+ }
1245
+
1246
+ /// Parents whose child lists changed since the last call; `null` when
1247
+ /// too many changed to list and every parent must be treated as changed.
1248
+ #[napi(js_name = "takeTouched")]
1249
+ pub fn take_touched(&self) -> Option<Uint32Array> {
1250
+ let removed = std::mem::take(&mut *self.tree_touched.borrow_mut());
1251
+ match self.arena_for_write().as_mut() {
1252
+ Some(arena) => arena.take_touched().map(|mut touched| {
1253
+ touched.extend(removed);
1254
+ Uint32Array::from(touched)
1255
+ }),
1256
+ None => Some(Uint32Array::from(removed)),
1257
+ }
1258
+ }
1259
+
1260
+ /// Removes nodes from the parse tree itself: the common clean-up before
1261
+ /// reading text or markup then needs no copy into the mutable arena.
1262
+ fn remove_from_tree(&self, ids: &[u32]) {
1263
+ let mut html = self.html.borrow_mut();
1264
+ let Some(html) = html.as_mut() else {
1265
+ return;
1266
+ };
1267
+ let node_ids = self.ids.borrow();
1268
+ let reverse = self.reverse.borrow();
1269
+ let mut touched = self.tree_touched.borrow_mut();
1270
+ for id in ids {
1271
+ let Some(node_id) = node_ids.get(*id as usize) else {
1272
+ continue;
1273
+ };
1274
+ let Some(mut node) = html.tree.get_mut(*node_id) else {
1275
+ continue;
1276
+ };
1277
+ let Some(parent) = node.parent().map(|parent| parent.id()) else {
1278
+ continue;
1279
+ };
1280
+ node.detach();
1281
+ touched.extend(reverse.get(&parent).copied());
1282
+ }
1283
+ self.tree_detached.set(true);
1284
+ *self.tree_index.borrow_mut() = None;
1285
+ }
1286
+
1287
+ /// Replaces every pin with `pins`, the nodes JavaScript still references,
1288
+ /// then reclaims detached nodes that are neither pinned, below
1289
+ /// `keep_below`, nor connected to such a node. Computing pins at
1290
+ /// collection time lets JavaScript track references without a native call
1291
+ /// per node.
1292
+ #[napi(js_name = "collectPinned")]
1293
+ pub fn collect_pinned(
1294
+ &self,
1295
+ env: Env,
1296
+ pins: Uint32Array,
1297
+ keep_below: Option<u32>,
1298
+ ) -> PinnedCollection {
1299
+ self.kept_slots.set(keep_below.unwrap_or(0));
1300
+ let mut retained = HashMap::new();
1301
+ {
1302
+ let arena = self.arena.borrow();
1303
+ let immutable_count = self.ids.borrow().len();
1304
+ for id in pins.iter() {
1305
+ let exists = arena
1306
+ .as_ref()
1307
+ .map_or((*id as usize) < immutable_count, |arena| {
1308
+ arena.node(*id).is_some()
1309
+ });
1310
+ if exists {
1311
+ *retained.entry(*id).or_insert(0) += 1;
1312
+ }
1313
+ }
1314
+ }
1315
+ *self.retained.borrow_mut() = retained;
1316
+ self.managed.set(true);
1317
+ let stats = self.collect_garbage_and_report(env);
1318
+ let free = self
1319
+ .arena
1320
+ .borrow()
1321
+ .as_ref()
1322
+ .map(|arena| arena.free.clone())
1323
+ .unwrap_or_default();
1324
+ PinnedCollection {
1325
+ live: stats.live,
1326
+ slots: stats.slots,
1327
+ retained: stats.retained,
1328
+ reclaimed: stats.reclaimed,
1329
+ immutable_nodes: stats.immutable_nodes,
1330
+ external_bytes: stats.external_bytes,
1331
+ free: free.into(),
1332
+ }
1333
+ }
1334
+
1335
+ #[napi(js_name = "collectGarbage")]
1336
+ pub fn collect_garbage_and_report(&self, env: Env) -> MemoryStats {
1337
+ let stats = self.collect_garbage();
1338
+ self.report_heap_bytes(env);
1339
+ MemoryStats {
1340
+ external_bytes: self.reported_heap_bytes.get(),
1341
+ ..stats
1342
+ }
1343
+ }
1344
+
1345
+ pub fn collect_garbage(&self) -> MemoryStats {
1346
+ self.needs_collect.set(false);
1347
+ if self.arena.borrow().is_none()
1348
+ && self.managed.get()
1349
+ && self.kept_slots.get() == 0
1350
+ && self.retained.borrow().is_empty()
1351
+ {
1352
+ self.html.borrow_mut().take();
1353
+ self.source.borrow_mut().take();
1354
+ let reclaimed = self.ids.borrow().len() as u32;
1355
+ *self.ids.borrow_mut() = Vec::new();
1356
+ *self.reverse.borrow_mut() = id_hash::IdMap::default();
1357
+ *self.tree_index.borrow_mut() = None;
1358
+ return MemoryStats {
1359
+ live: 0,
1360
+ slots: 0,
1361
+ retained: 0,
1362
+ reclaimed,
1363
+ immutable_nodes: 0,
1364
+ external_bytes: self.reported_heap_bytes.get(),
1365
+ };
1366
+ }
1367
+ if let Some(arena) = self.arena_for_write().as_mut() {
1368
+ let stats = gc::collect_managed(
1369
+ arena,
1370
+ &self.retained.borrow(),
1371
+ !self.managed.get(),
1372
+ self.kept_slots.get(),
1373
+ );
1374
+ arena.free = stats.free;
1375
+ MemoryStats {
1376
+ live: stats.live,
1377
+ slots: stats.slots,
1378
+ retained: stats.retained,
1379
+ reclaimed: stats.reclaimed,
1380
+ immutable_nodes: 0,
1381
+ external_bytes: self.reported_heap_bytes.get(),
1382
+ }
1383
+ } else {
1384
+ let count = self.ids.borrow().len() as u32;
1385
+ MemoryStats {
1386
+ live: count,
1387
+ slots: count,
1388
+ retained: self.retained.borrow().len() as u32,
1389
+ reclaimed: 0,
1390
+ immutable_nodes: count,
1391
+ external_bytes: self.reported_heap_bytes.get(),
1392
+ }
1393
+ }
1394
+ }
1395
+
1396
+ #[napi]
1397
+ pub fn parser_diagnostics(&self) -> Result<String> {
1398
+ serde_json::to_string(&*self.diagnostics.borrow()).map_err(json_error)
1399
+ }
1400
+
1401
+ #[napi]
1402
+ pub fn root_id(&self, env: Env) -> u32 {
1403
+ self.report_heap_bytes(env);
1404
+ 0
1405
+ }
1406
+
1407
+ #[napi(js_name = "select")]
1408
+ pub fn select_and_report(
1409
+ &self,
1410
+ env: Env,
1411
+ selector: String,
1412
+ contexts: Option<Vec<u32>>,
1413
+ ) -> Result<Vec<u32>> {
1414
+ self.reported(env, self.select(selector, contexts))
1415
+ }
1416
+
1417
+ pub fn select(&self, selector: String, contexts: Option<Vec<u32>>) -> Result<Vec<u32>> {
1418
+ self.select_ids(&selector, contexts.as_deref())
1419
+ }
1420
+
1421
+ #[napi]
1422
+ pub fn matches(&self, env: Env, selector: String, ids: Vec<u32>) -> Result<Vec<u32>> {
1423
+ self.ensure_arena()?;
1424
+ let matched = selector::filter(self.arena.borrow().as_ref().unwrap(), &selector, &ids);
1425
+ self.reported(env, matched)
1426
+ }
1427
+
1428
+ #[napi]
1429
+ pub fn element_ids(&self) -> Vec<u32> {
1430
+ if let Some(arena) = self.arena.borrow().as_ref() {
1431
+ return arena
1432
+ .nodes
1433
+ .iter()
1434
+ .filter(|node| node.is_element())
1435
+ .map(|node| node.id)
1436
+ .collect();
1437
+ }
1438
+ self.ids
1439
+ .borrow()
1440
+ .iter()
1441
+ .enumerate()
1442
+ .filter_map(|(id, _)| {
1443
+ self.with_node(id as u32, |node, _| node.value().is_element())
1444
+ .unwrap_or(false)
1445
+ .then_some(id as u32)
1446
+ })
1447
+ .collect()
1448
+ }
1449
+
1450
+ /// The results of the extraction plans `parseManyAsync` ran on the
1451
+ /// document's root while parsing it, each `null` if it failed; `null`
1452
+ /// after the first call.
1453
+ #[napi(
1454
+ js_name = "takePreparedPlans",
1455
+ ts_return_type = "Array<string | null> | null"
1456
+ )]
1457
+ pub fn take_prepared_plans(&self) -> Option<Vec<Option<JsText>>> {
1458
+ self.prepared_plans.borrow_mut().take().map(|results| {
1459
+ results
1460
+ .into_iter()
1461
+ .map(|result| result.map(JsText))
1462
+ .collect()
1463
+ })
1464
+ }
1465
+
1466
+ /// The snapshot `parseManyAsync` built while parsing the document, if it
1467
+ /// did; `null` after the first call, which discards it unless `is_wanted`.
1468
+ /// It describes the parsed tree, so JavaScript discards it on its first write.
1469
+ #[napi(js_name = "takePreparedSnapshot")]
1470
+ pub fn take_prepared_snapshot(&self, env: Env, is_wanted: bool) -> Result<Option<Object>> {
1471
+ let prepared = self.prepared_snapshot.borrow_mut().take();
1472
+ match prepared {
1473
+ Some(snapshot) if is_wanted => snapshot.into_js(&env).map(Some),
1474
+ _ => Ok(None),
1475
+ }
1476
+ }
1477
+
1478
+ #[napi(js_name = "text", ts_return_type = "string")]
1479
+ pub fn text_for_js(&self, ids: Vec<u32>) -> JsText {
1480
+ JsText(self.text(ids))
1481
+ }
1482
+
1483
+ pub fn text(&self, ids: Vec<u32>) -> String {
1484
+ self.text_ids(&ids)
1485
+ }
1486
+
1487
+ #[napi(js_name = "innerHtml", ts_return_type = "string")]
1488
+ pub fn inner_html_and_report(&self, env: Env, id: u32) -> JsText {
1489
+ JsText(self.reported(env, self.inner_html(id)))
1490
+ }
1491
+
1492
+ pub fn inner_html(&self, id: u32) -> String {
1493
+ if (self.tag_name(id).as_deref() == Some("template") || self.arena.borrow().is_some())
1494
+ && self.ensure_arena().is_ok()
1495
+ {
1496
+ return self.arena_for_write().as_mut().unwrap().inner_html_mut(id);
1497
+ }
1498
+ self.serialize_nodes(&[id], true)
1499
+ }
1500
+
1501
+ #[napi(js_name = "outerHtml", ts_return_type = "string")]
1502
+ pub fn outer_html_for_js(&self, ids: Vec<u32>) -> JsText {
1503
+ JsText(self.outer_html(ids))
1504
+ }
1505
+
1506
+ pub fn outer_html(&self, ids: Vec<u32>) -> String {
1507
+ self.serialize_nodes(&ids, false)
1508
+ }
1509
+
1510
+ /// Serializes nodes as the `render` command does, returning the markup
1511
+ /// itself rather than markup encoded as JSON.
1512
+ #[napi(js_name = "render", ts_return_type = "string")]
1513
+ pub fn render_and_report(
1514
+ &self,
1515
+ env: Env,
1516
+ ids: Vec<u32>,
1517
+ options_json: Option<String>,
1518
+ ) -> Result<JsText> {
1519
+ self.maybe_collect();
1520
+ let overrides = match options_json {
1521
+ Some(json) => Some(serde_json::from_str::<Value>(&json).map_err(json_error)?),
1522
+ None => None,
1523
+ }
1524
+ .filter(Value::is_object);
1525
+ let rendered = self.render_nodes(&ids, overrides)?;
1526
+ Ok(JsText(self.reported(env, rendered)))
1527
+ }
1528
+
1529
+ fn render_nodes(&self, ids: &[u32], overrides: Option<Value>) -> Result<String> {
1530
+ let legacy = overrides
1531
+ .as_ref()
1532
+ .and_then(|value| value.get("legacySerializer"))
1533
+ .and_then(Value::as_bool)
1534
+ .unwrap_or(false);
1535
+ if self.arena.borrow().is_none() {
1536
+ if legacy {
1537
+ // The record tree converts to the arena on demand; the legacy
1538
+ // serializer rules live in the arena walker.
1539
+ self.ensure_arena()?;
1540
+ } else {
1541
+ let mut options = self.options.clone();
1542
+ if let Some(Value::Object(overrides)) = &overrides {
1543
+ for (key, value) in overrides {
1544
+ options[key] = value.clone();
1545
+ }
1546
+ }
1547
+ let options = arena::normalized_options(options);
1548
+ if !arena::xml_mode(&options)
1549
+ && options.get("_useHtmlParser2") != Some(&Value::Bool(true))
1550
+ {
1551
+ return Ok(self.serialize_nodes(ids, false));
1552
+ }
1553
+ }
1554
+ }
1555
+ self.ensure_arena()?;
1556
+ let mut arena = self.arena.borrow_mut();
1557
+ let arena = arena.as_mut().unwrap();
1558
+ let mut options = match overrides {
1559
+ Some(overrides) => arena::normalized_options(overrides),
1560
+ None => arena.options.clone(),
1561
+ };
1562
+ if self.options.get("__utf16Codec") == Some(&Value::Bool(true)) {
1563
+ options["__utf16Codec"] = Value::Bool(true);
1564
+ }
1565
+ Ok(arena.serialize_mut(ids, &arena::normalized_options(options)))
1566
+ }
1567
+
1568
+ #[napi(js_name = "attr", ts_return_type = "string | null")]
1569
+ pub fn attr_for_js(&self, id: u32, name: String) -> Option<JsText> {
1570
+ self.attr(id, name).map(JsText)
1571
+ }
1572
+
1573
+ pub fn attr(&self, id: u32, name: String) -> Option<String> {
1574
+ self.attribute(id, &name)
1575
+ }
1576
+
1577
+ #[napi]
1578
+ pub fn tag_name(&self, id: u32) -> Option<String> {
1579
+ if let Some(arena) = self.arena.borrow().as_ref() {
1580
+ return arena.node(id).and_then(|node| node.name.clone());
1581
+ }
1582
+ self.with_node(id, |node, _| {
1583
+ node.value()
1584
+ .as_element()
1585
+ .map(|element| element.name().to_string())
1586
+ })
1587
+ .flatten()
1588
+ }
1589
+
1590
+ #[napi(js_name = "exportTree", ts_return_type = "string")]
1591
+ pub fn export_tree_and_report(&self, env: Env) -> Result<JsText> {
1592
+ self.reported(env, self.export_tree()).map(JsText)
1593
+ }
1594
+
1595
+ pub fn export_tree(&self) -> Result<String> {
1596
+ if self.source.borrow().is_some() {
1597
+ self.ensure_arena()?;
1598
+ }
1599
+ if let Some(arena) = self.arena.borrow().as_ref() {
1600
+ return serde_json::to_string(&arena.nodes).map_err(json_error);
1601
+ }
1602
+ let records: Vec<_> = (0..self.ids.borrow().len())
1603
+ .filter_map(|id| self.immutable_dom_node(id as u32))
1604
+ .collect();
1605
+ serde_json::to_string(&records).map_err(json_error)
1606
+ }
1607
+
1608
+ #[napi(js_name = "exportTreeWithOffset", ts_return_type = "string")]
1609
+ pub fn export_tree_with_offset(&self, env: Env, base: f64) -> Result<JsText> {
1610
+ if !base.is_finite() || base < 0.0 || base.fract() != 0.0 || base > 9_007_199_254_740_991.0
1611
+ {
1612
+ return Err(Error::new(
1613
+ Status::InvalidArg,
1614
+ "base must be a non-negative safe integer",
1615
+ ));
1616
+ }
1617
+ if self.source.borrow().is_some() {
1618
+ self.ensure_arena()?;
1619
+ }
1620
+ let base = base as u64;
1621
+ let arena = self.arena.borrow();
1622
+ if let Some(arena) = arena.as_ref() {
1623
+ let nodes = arena.nodes.iter().collect();
1624
+ let serialized =
1625
+ serde_json::to_string(&OffsetRecords { nodes, base }).map_err(json_error)?;
1626
+ return Ok(JsText(self.reported(env, serialized)));
1627
+ }
1628
+ drop(arena);
1629
+ {
1630
+ // Keep the immutable tree's existing ID mapping and lifetime while
1631
+ // serializing; this path is only used before arena materialization.
1632
+ let records: Vec<_> = (0..self.ids.borrow().len())
1633
+ .filter_map(|id| self.immutable_dom_node(id as u32))
1634
+ .collect();
1635
+ let serialized = serde_json::to_string(
1636
+ &records
1637
+ .iter()
1638
+ .map(|node| offset_node(node, base))
1639
+ .collect::<Vec<_>>(),
1640
+ )
1641
+ .map_err(json_error)?;
1642
+ Ok(JsText(self.reported(env, serialized)))
1643
+ }
1644
+ }
1645
+
1646
+ /// Root IDs in the same order and with the same parent rules as exportTree.
1647
+ #[napi]
1648
+ pub fn forest_roots(&self, env: Env) -> Result<Vec<u32>> {
1649
+ let roots = self.forest_root_ids()?;
1650
+ self.report_heap_bytes(env);
1651
+ Ok(roots)
1652
+ }
1653
+
1654
+ fn forest_root_ids(&self) -> Result<Vec<u32>> {
1655
+ if self.source.borrow().is_some() {
1656
+ self.ensure_arena()?;
1657
+ }
1658
+ let roots = if let Some(arena) = self.arena.borrow().as_ref() {
1659
+ arena
1660
+ .nodes
1661
+ .iter()
1662
+ .filter(|node| node.parent.is_none())
1663
+ .map(|node| node.id)
1664
+ .collect()
1665
+ } else {
1666
+ let html = self.html.borrow();
1667
+ let ids = self.ids.borrow();
1668
+ let reverse = self.reverse.borrow();
1669
+ match html.as_ref() {
1670
+ Some(html) => ids
1671
+ .iter()
1672
+ .enumerate()
1673
+ .filter_map(|(id, node_id)| {
1674
+ let node = html.tree.get(*node_id)?;
1675
+ let parent = node.parent().and_then(|parent| reverse.get(&parent.id()));
1676
+ parent.is_none().then_some(id as u32)
1677
+ })
1678
+ .collect(),
1679
+ None => Vec::new(),
1680
+ }
1681
+ };
1682
+ Ok(roots)
1683
+ }
1684
+
1685
+ /// Every node record at once, in the compact form `snapshot.rs` describes.
1686
+ #[napi]
1687
+ pub fn snapshot(&self, env: Env) -> Result<Object> {
1688
+ self.maybe_collect();
1689
+ if self.source.borrow().is_some() {
1690
+ self.ensure_arena()?;
1691
+ }
1692
+ let built = snapshot::build(self);
1693
+ self.report_heap_bytes(env);
1694
+ built.into_js(&env)
1695
+ }
1696
+
1697
+ #[napi(js_name = "node", ts_return_type = "string")]
1698
+ pub fn node_and_report(&self, env: Env, id: u32) -> Result<JsText> {
1699
+ self.reported(env, self.node(id)).map(JsText)
1700
+ }
1701
+
1702
+ pub fn node(&self, id: u32) -> Result<String> {
1703
+ if self.source.borrow().is_some() {
1704
+ self.ensure_arena()?;
1705
+ }
1706
+ if self.arena.borrow().is_none() {
1707
+ return serde_json::to_string(&self.immutable_record(id)).map_err(json_error);
1708
+ }
1709
+ serde_json::to_string(&self.arena.borrow().as_ref().unwrap().node(id)).map_err(json_error)
1710
+ }
1711
+
1712
+ #[napi(js_name = "create")]
1713
+ pub fn create_and_report(
1714
+ &self,
1715
+ env: Env,
1716
+ html: String,
1717
+ context_id: Option<u32>,
1718
+ options_json: Option<String>,
1719
+ ) -> Result<Vec<u32>> {
1720
+ let created = self.create(html, context_id, options_json);
1721
+ self.reported(env, created)
1722
+ }
1723
+
1724
+ pub fn create(
1725
+ &self,
1726
+ html: String,
1727
+ context_id: Option<u32>,
1728
+ _options_json: Option<String>,
1729
+ ) -> Result<Vec<u32>> {
1730
+ self.content_bytes
1731
+ .set(self.content_bytes.get().saturating_add(html.len()));
1732
+ self.ensure_arena()?;
1733
+ self.arena
1734
+ .borrow_mut()
1735
+ .as_mut()
1736
+ .unwrap()
1737
+ .create_fragment(&html, context_id)
1738
+ }
1739
+
1740
+ #[napi(js_name = "removeNodes")]
1741
+ pub fn remove_nodes_for_js(&self, env: Env, ids: Vec<u32>) -> Result<()> {
1742
+ self.ensure_arena()?;
1743
+ self.arena_for_write().as_mut().unwrap().detach_many(&ids);
1744
+ self.report_heap_bytes(env);
1745
+ Ok(())
1746
+ }
1747
+
1748
+ #[napi(js_name = "insertHtml")]
1749
+ pub fn insert_html_for_js(
1750
+ &self,
1751
+ env: Env,
1752
+ target_ids: Vec<u32>,
1753
+ html: String,
1754
+ position: String,
1755
+ ) -> Result<Vec<u32>> {
1756
+ let created = self.create(html, None, None)?;
1757
+ let inserted =
1758
+ self.arena_for_write()
1759
+ .as_mut()
1760
+ .unwrap()
1761
+ .insert(&target_ids, &created, &position);
1762
+ Ok(self.reported(env, inserted))
1763
+ }
1764
+
1765
+ /// Move a branch between native documents. JavaScript only redirects its
1766
+ /// existing stable handles using the returned complete descendant mapping.
1767
+ #[napi(js_name = "adopt")]
1768
+ pub fn adopt_and_report(
1769
+ &self,
1770
+ env: Env,
1771
+ source: &NativeDocument,
1772
+ ids: Vec<u32>,
1773
+ detach: Option<bool>,
1774
+ ) -> Result<String> {
1775
+ let adopted = self.adopt(source, ids, detach);
1776
+ source.report_heap_bytes(env);
1777
+ self.reported(env, adopted)
1778
+ }
1779
+
1780
+ pub fn adopt(
1781
+ &self,
1782
+ source: &NativeDocument,
1783
+ ids: Vec<u32>,
1784
+ detach: Option<bool>,
1785
+ ) -> Result<String> {
1786
+ if std::ptr::eq(self, source) {
1787
+ return serde_json::to_string(&serde_json::json!({"roots":ids,"mapping":ids.iter().map(|id|[*id,*id]).collect::<Vec<_>>()})).map_err(json_error);
1788
+ }
1789
+ self.ensure_arena()?;
1790
+ source.ensure_arena()?;
1791
+ let mut source_arena = source.arena_for_write();
1792
+ let source_arena = source_arena.as_mut().unwrap();
1793
+ // Detach all requested roots first: overlapping roots become siblings
1794
+ // and each requested descendant is imported exactly once.
1795
+ if detach.unwrap_or(true) {
1796
+ source_arena.detach_many(&ids);
1797
+ }
1798
+ let mut seen = HashSet::new();
1799
+ let mut moved = Vec::new();
1800
+ let mut stack: Vec<_> = ids.iter().rev().copied().collect();
1801
+ while let Some(id) = stack.pop() {
1802
+ if !seen.insert(id) {
1803
+ continue;
1804
+ }
1805
+ if let Some(node) = source_arena.node(id) {
1806
+ moved.push(id);
1807
+ stack.extend(node.children.iter().rev().copied());
1808
+ }
1809
+ }
1810
+ let mut target_arena = self.arena_for_write();
1811
+ let target_arena = target_arena.as_mut().unwrap();
1812
+ target_arena.nodes.reserve(moved.len());
1813
+ let (roots, mapping) = if detach.unwrap_or(true) {
1814
+ let mapping: Vec<_> = moved
1815
+ .iter()
1816
+ .map(|id| [*id, target_arena.push(arena::DomNode::new("released"))])
1817
+ .collect();
1818
+ let remap: HashMap<_, _> = mapping.iter().map(|pair| (pair[0], pair[1])).collect();
1819
+ for id in &moved {
1820
+ let mut tombstone = arena::DomNode::new("released");
1821
+ tombstone.id = *id;
1822
+ let mut node = std::mem::replace(&mut source_arena.nodes[*id as usize], tombstone);
1823
+ source_arena.free.push(*id);
1824
+ node.id = remap[id];
1825
+ node.children.retain_mut(|child| {
1826
+ if let Some(mapped) = remap.get(child) {
1827
+ *child = *mapped;
1828
+ true
1829
+ } else {
1830
+ false
1831
+ }
1832
+ });
1833
+ node.parent = node.parent.and_then(|id| remap.get(&id).copied());
1834
+ node.prev = node.prev.and_then(|id| remap.get(&id).copied());
1835
+ node.next = node.next.and_then(|id| remap.get(&id).copied());
1836
+ let new_id = node.id;
1837
+ target_arena.nodes[new_id as usize] = node;
1838
+ if let Some(count) = source.retained.borrow_mut().remove(id) {
1839
+ *self.retained.borrow_mut().entry(new_id).or_insert(0) += count;
1840
+ }
1841
+ }
1842
+ for pair in &mapping {
1843
+ target_arena.relink(pair[1]);
1844
+ }
1845
+ (
1846
+ ids.iter()
1847
+ .filter_map(|id| remap.get(id).copied())
1848
+ .collect::<Vec<_>>(),
1849
+ mapping,
1850
+ )
1851
+ } else {
1852
+ // Imported roots use the original preorder allocation. Preserve
1853
+ // a complete mapping even when this arena has recyclable slots.
1854
+ let free = std::mem::take(&mut target_arena.free);
1855
+ let first = target_arena.nodes.len() as u32;
1856
+ let records: Vec<_> = moved
1857
+ .iter()
1858
+ .filter_map(|id| source_arena.node(*id).cloned())
1859
+ .collect();
1860
+ let roots = target_arena.import(&records, &ids);
1861
+ let mapping = moved
1862
+ .iter()
1863
+ .enumerate()
1864
+ .map(|(index, id)| [*id, first + index as u32])
1865
+ .collect::<Vec<_>>();
1866
+ target_arena.free = free;
1867
+ (roots, mapping)
1868
+ };
1869
+ serde_json::to_string(&serde_json::json!({"roots":roots,"mapping":mapping}))
1870
+ .map_err(json_error)
1871
+ }
1872
+
1873
+ #[napi(js_name = "op", ts_return_type = "string")]
1874
+ pub fn op_and_report(&self, env: Env, command: String, args_json: String) -> Result<JsText> {
1875
+ self.reported(env, self.op(command, args_json)).map(JsText)
1876
+ }
1877
+
1878
+ pub fn op(&self, command: String, args_json: String) -> Result<String> {
1879
+ self.maybe_collect();
1880
+ let args: Value = serde_json::from_str(&args_json).map_err(json_error)?;
1881
+ if command == "node" {
1882
+ return self.node(args["id"].as_u64().unwrap_or(0) as u32);
1883
+ }
1884
+ if command == "remove"
1885
+ && args["keepChildren"].as_bool() != Some(true)
1886
+ && self.arena.borrow().is_none()
1887
+ && self.source.borrow().is_none()
1888
+ {
1889
+ self.remove_from_tree(&arena::value_ids(&args["ids"]));
1890
+ return Ok("null".into());
1891
+ }
1892
+ if self.arena.borrow().is_none() {
1893
+ let ids = arena::value_ids(&args["ids"]);
1894
+ let effective_options = arena::normalized_options(
1895
+ args.get("options")
1896
+ .filter(|options| options.is_object())
1897
+ .cloned()
1898
+ .unwrap_or_else(|| self.options.clone()),
1899
+ );
1900
+ if command == "attr" && args.get("value").is_none() {
1901
+ let name = args["name"].as_str().unwrap_or("");
1902
+ let result = ids
1903
+ .first()
1904
+ .and_then(|id| {
1905
+ let element_name = self.tag_name(*id)?;
1906
+ if name.is_empty() {
1907
+ return self
1908
+ .immutable_record(*id)
1909
+ .and_then(|value| value.get("attribs").cloned());
1910
+ }
1911
+ if let Some(value) = self.attribute(*id, name) {
1912
+ let boolean = matches!(
1913
+ name.to_ascii_lowercase().as_str(),
1914
+ "autofocus"
1915
+ | "autoplay"
1916
+ | "async"
1917
+ | "checked"
1918
+ | "controls"
1919
+ | "defer"
1920
+ | "disabled"
1921
+ | "hidden"
1922
+ | "loop"
1923
+ | "multiple"
1924
+ | "open"
1925
+ | "readonly"
1926
+ | "required"
1927
+ | "scoped"
1928
+ | "selected"
1929
+ );
1930
+ return Some(Value::String(
1931
+ if boolean && !arena::xml_mode(&effective_options) {
1932
+ name.into()
1933
+ } else {
1934
+ value
1935
+ },
1936
+ ));
1937
+ }
1938
+ if name == "value" {
1939
+ if element_name == "option" {
1940
+ return Some(Value::String(self.text_ids(&[*id])));
1941
+ }
1942
+ if element_name == "input"
1943
+ && matches!(
1944
+ self.attribute(*id, "type").as_deref(),
1945
+ Some("radio" | "checkbox")
1946
+ )
1947
+ {
1948
+ return Some(serde_json::json!("on"));
1949
+ }
1950
+ }
1951
+ None
1952
+ })
1953
+ .unwrap_or(Value::Null);
1954
+ return serde_json::to_string(&result).map_err(json_error);
1955
+ }
1956
+ if let Some(result) = self.tree_read(&command, &args, &ids, &effective_options) {
1957
+ return serde_json::to_string(&result?).map_err(json_error);
1958
+ }
1959
+ if let Some(result) =
1960
+ self.tree_attribute_write(&command, &args, &ids, &effective_options)
1961
+ {
1962
+ return serde_json::to_string(&result?).map_err(json_error);
1963
+ }
1964
+ if command == "render" || command == "outerHtml" {
1965
+ let mut options = self.options.clone();
1966
+ if let Some(overrides) = args.get("options").and_then(Value::as_object) {
1967
+ for (key, value) in overrides {
1968
+ options[key] = value.clone();
1969
+ }
1970
+ }
1971
+ let options = arena::normalized_options(options);
1972
+ if !arena::xml_mode(&options)
1973
+ && options.get("_useHtmlParser2") != Some(&Value::Bool(true))
1974
+ {
1975
+ return serde_json::to_string(&self.serialize_nodes(&ids, false))
1976
+ .map_err(json_error);
1977
+ }
1978
+ }
1979
+ }
1980
+ self.ensure_arena()?;
1981
+ let mut arena = if attributes::keeps_element_names(&command) {
1982
+ self.arena_for_attribute_write()
1983
+ } else {
1984
+ self.arena_for_write()
1985
+ };
1986
+ let arena = arena.as_mut().unwrap();
1987
+ let previous_options = arena.options.clone();
1988
+ if let Some(options) = args.get("options").filter(|options| options.is_object()) {
1989
+ arena.options = arena::normalized_options(options.clone());
1990
+ if self.options.get("__utf16Codec") == Some(&Value::Bool(true)) {
1991
+ arena.options["__utf16Codec"] = Value::Bool(true);
1992
+ }
1993
+ }
1994
+ let result = (|| -> Result<Value> {
1995
+ if command == "extractPlan" {
1996
+ return extraction::execute_arena(
1997
+ arena,
1998
+ &args["plan"],
1999
+ &arena::value_ids(&args["ids"]),
2000
+ );
2001
+ }
2002
+ if let Some(result) = attributes::execute(arena, &command, &args)? {
2003
+ Ok(result)
2004
+ } else {
2005
+ arena::primitive(arena, &command, &args)
2006
+ }
2007
+ })();
2008
+ arena.options = previous_options;
2009
+ let result = result?;
2010
+ serde_json::to_string(&result).map_err(json_error)
2011
+ }
2012
+ }
2013
+
2014
+ #[napi(object)]
2015
+ pub struct PinnedCollection {
2016
+ pub live: u32,
2017
+ pub slots: u32,
2018
+ pub retained: u32,
2019
+ pub reclaimed: u32,
2020
+ pub immutable_nodes: u32,
2021
+ pub external_bytes: i64,
2022
+ /// Slots that hold no node after collection.
2023
+ pub free: Uint32Array,
2024
+ }
2025
+
2026
+ #[napi(object)]
2027
+ pub struct MemoryStats {
2028
+ pub live: u32,
2029
+ pub slots: u32,
2030
+ pub retained: u32,
2031
+ pub reclaimed: u32,
2032
+ pub immutable_nodes: u32,
2033
+ /// Native bytes currently reported to V8 for this document.
2034
+ pub external_bytes: i64,
2035
+ }
2036
+
2037
+ /// The Cheerio DOM adapter exposes foreign attributes by local name, whereas
2038
+ /// CSS normally limits unprefixed attribute selectors to the empty namespace.
2039
+ /// A wildcard prefix restores the adapter's matching rules. Preserve explicit
2040
+ /// namespaces, quoted values, and escapes while scanning selector syntax.
2041
+ fn normalize_attribute_namespaces(source: &str) -> Cow<'_, str> {
2042
+ let bytes = source.as_bytes();
2043
+ let mut insertions = Vec::new();
2044
+ let mut quote = None;
2045
+ let mut index = 0;
2046
+ while index < bytes.len() {
2047
+ let current = bytes[index];
2048
+ if current == b'\\' {
2049
+ index += 2;
2050
+ continue;
2051
+ }
2052
+ if let Some(quoted) = quote {
2053
+ if current == quoted {
2054
+ quote = None;
2055
+ }
2056
+ } else if current == b'\'' || current == b'"' {
2057
+ quote = Some(current);
2058
+ } else if current == b'[' {
2059
+ let mut start = index + 1;
2060
+ while start < bytes.len() && bytes[start].is_ascii_whitespace() {
2061
+ start += 1;
2062
+ }
2063
+ let mut cursor = start;
2064
+ let mut explicit_namespace = false;
2065
+ while cursor < bytes.len() {
2066
+ let ch = bytes[cursor];
2067
+ if ch == b'\\' {
2068
+ cursor += 2;
2069
+ continue;
2070
+ }
2071
+ if ch == b'|' && bytes.get(cursor + 1) != Some(&b'=') {
2072
+ explicit_namespace = true;
2073
+ break;
2074
+ }
2075
+ if ch == b']'
2076
+ || ch == b'='
2077
+ || ch.is_ascii_whitespace()
2078
+ || (matches!(ch, b'~' | b'^' | b'$' | b'*' | b'!' | b'|')
2079
+ && bytes.get(cursor + 1) == Some(&b'='))
2080
+ {
2081
+ break;
2082
+ }
2083
+ cursor += 1;
2084
+ }
2085
+ if !explicit_namespace && start < bytes.len() {
2086
+ insertions.push(start);
2087
+ }
2088
+ }
2089
+ index += 1;
2090
+ }
2091
+ if insertions.is_empty() {
2092
+ return Cow::Borrowed(source);
2093
+ }
2094
+ let mut normalized = String::with_capacity(source.len() + insertions.len() * 2);
2095
+ let mut previous = 0;
2096
+ for insertion in insertions {
2097
+ normalized.push_str(&source[previous..insertion]);
2098
+ normalized.push_str("*|");
2099
+ previous = insertion;
2100
+ }
2101
+ normalized.push_str(&source[previous..]);
2102
+ Cow::Owned(normalized)
2103
+ }
2104
+
2105
+ /// An element's attributes after setting or removing `name`. Attributes are
2106
+ /// keyed by local name as in the arena: a repeated local name keeps its first
2107
+ /// position, with the name and value of its last occurrence.
2108
+ fn with_attribute(
2109
+ element: &scraper::node::Element,
2110
+ name: &str,
2111
+ value: Option<&str>,
2112
+ ) -> Vec<html5ever::Attribute> {
2113
+ let mut attributes: Vec<html5ever::Attribute> = Vec::with_capacity(element.attrs.len() + 1);
2114
+ for (key, current) in &element.attrs {
2115
+ let attribute = html5ever::Attribute {
2116
+ name: key.clone(),
2117
+ value: current.as_ref().into(),
2118
+ };
2119
+ match attributes
2120
+ .iter_mut()
2121
+ .find(|existing| existing.name.local == key.local)
2122
+ {
2123
+ Some(existing) => *existing = attribute,
2124
+ None => attributes.push(attribute),
2125
+ }
2126
+ }
2127
+ match value {
2128
+ Some(value) => match attributes
2129
+ .iter_mut()
2130
+ .find(|attribute| attribute.name.local.as_ref() == name)
2131
+ {
2132
+ Some(attribute) => attribute.value = value.into(),
2133
+ None => attributes.push(html5ever::Attribute {
2134
+ name: html5ever::QualName::new(
2135
+ None,
2136
+ html5ever::ns!(),
2137
+ html5ever::LocalName::from(name),
2138
+ ),
2139
+ value: value.into(),
2140
+ }),
2141
+ },
2142
+ None => attributes.retain(|attribute| attribute.name.local.as_ref() != name),
2143
+ }
2144
+ attributes
2145
+ }
2146
+
2147
+ thread_local! {
2148
+ /// Parse-tree selectors by source, or `None` for sources they cannot express.
2149
+ /// A two-generation cache avoids the cliff of clearing all 512 entries at once.
2150
+ static SELECTORS_CURRENT: RefCell<HashMap<String, Option<std::rc::Rc<Selector>>>> =
2151
+ RefCell::new(HashMap::new());
2152
+ static SELECTORS_PREVIOUS: RefCell<HashMap<String, Option<std::rc::Rc<Selector>>>> =
2153
+ RefCell::new(HashMap::new());
2154
+ }
2155
+
2156
+ /// A selector for the parse tree, parsed once per source per thread.
2157
+ fn parsed_selector(source: &str) -> Option<std::rc::Rc<Selector>> {
2158
+ if let Some(parsed) = SELECTORS_CURRENT.with(|cache| cache.borrow().get(source).cloned()) {
2159
+ return parsed;
2160
+ }
2161
+ if let Some(parsed) = SELECTORS_PREVIOUS.with(|cache| cache.borrow().get(source).cloned()) {
2162
+ return parsed;
2163
+ }
2164
+ let parsed = Selector::parse(&normalize_attribute_namespaces(source))
2165
+ .ok()
2166
+ .map(std::rc::Rc::new);
2167
+ SELECTORS_CURRENT.with(|cache| {
2168
+ let mut cache = cache.borrow_mut();
2169
+ if cache.len() >= 512 {
2170
+ let previous = std::mem::take(&mut *cache);
2171
+ SELECTORS_PREVIOUS.with(|p| *p.borrow_mut() = previous);
2172
+ }
2173
+ cache.insert(source.to_owned(), parsed.clone());
2174
+ });
2175
+ parsed
2176
+ }
2177
+
2178
+ /// Whether a selector compares the value of an attribute whose values HTML
2179
+ /// matches without regard to case, which the parse-tree selector engine does not.
2180
+ fn compares_case_insensitive_attribute(source: &str) -> bool {
2181
+ source.contains('=')
2182
+ && source.match_indices('[').any(|(index, _)| {
2183
+ let name: String = source[index + 1..]
2184
+ .trim_start()
2185
+ .chars()
2186
+ .take_while(|character| character.is_ascii_alphanumeric() || *character == '-')
2187
+ .collect();
2188
+ selector::CASE_INSENSITIVE_ATTRIBUTES
2189
+ .iter()
2190
+ .any(|candidate| name.eq_ignore_ascii_case(candidate))
2191
+ })
2192
+ }
2193
+
2194
+ /// `source` with CRLF and CR replaced by LF, as HTML5 input preprocessing does.
2195
+ fn normalize_newlines(source: &str) -> String {
2196
+ let mut output = String::with_capacity(source.len());
2197
+ let mut rest = source;
2198
+ while let Some(index) = rest.find('\r') {
2199
+ output.push_str(&rest[..index]);
2200
+ output.push('\n');
2201
+ rest = &rest[index + 1..];
2202
+ if let Some(stripped) = rest.strip_prefix('\n') {
2203
+ rest = stripped;
2204
+ }
2205
+ }
2206
+ output.push_str(rest);
2207
+ output
2208
+ }
2209
+
2210
+ /// Whether HTML5 parses `source` as a fragment to a single text node holding
2211
+ /// it unchanged: no markup, character references, carriage returns to
2212
+ /// normalize or NUL characters to replace.
2213
+ fn is_plain_text(source: &str) -> bool {
2214
+ !source
2215
+ .bytes()
2216
+ .any(|byte| matches!(byte, b'<' | b'&' | b'\r' | b'\0'))
2217
+ }
2218
+
2219
+ /// The fragment HTML5 builds from plain text, without running the parser,
2220
+ /// whose fixed setup costs more than parsing a short string.
2221
+ fn plain_text_fragment(source: &str) -> Html {
2222
+ let mut html = Html::new_fragment();
2223
+ if !source.is_empty() {
2224
+ html.tree.root_mut().append(Node::Text(scraper::node::Text {
2225
+ text: source.into(),
2226
+ }));
2227
+ }
2228
+ html
2229
+ }
2230
+
2231
+ /// A parse target with node storage sized for `source`: grown one
2232
+ /// reallocation at a time, it copies every node again, which dominates the
2233
+ /// parse of small inputs. Large documents grow from the cap.
2234
+ fn sized_html(source: &str, is_document: bool) -> Html {
2235
+ const MAXIMUM_INITIAL_NODES: usize = 4096;
2236
+ let capacity = (source.len() / 16 + 8).min(MAXIMUM_INITIAL_NODES);
2237
+ let mut html = if is_document {
2238
+ Html::new_document()
2239
+ } else {
2240
+ Html::new_fragment()
2241
+ };
2242
+ let root = if is_document {
2243
+ Node::Document
2244
+ } else {
2245
+ Node::Fragment
2246
+ };
2247
+ html.tree = ego_tree::Tree::with_capacity(root, capacity);
2248
+ html
2249
+ }
2250
+
2251
+ /// html5ever's fragment sink creates a synthetic html element; Cheerio
2252
+ /// exposes the fragment's actual children at its root. Children move one at a
2253
+ /// time because `reparent_from_id_append` only re-points the first and last
2254
+ /// moved child at their new parent.
2255
+ fn unwrap_fragment_root(html: &mut Html) {
2256
+ let wrapper = html.tree.root().children().find_map(|node| {
2257
+ node.value()
2258
+ .as_element()
2259
+ .filter(|element| element.name() == "html")
2260
+ .map(|_| node.id())
2261
+ });
2262
+ let Some(wrapper) = wrapper else { return };
2263
+ let children: Vec<NodeId> = html
2264
+ .tree
2265
+ .get(wrapper)
2266
+ .map(|node| node.children().map(|child| child.id()).collect())
2267
+ .unwrap_or_default();
2268
+ let mut root = html.tree.root_mut();
2269
+ for child in children {
2270
+ root.append_id(child);
2271
+ }
2272
+ html.tree.get_mut(wrapper).unwrap().detach();
2273
+ }
2274
+
2275
+ /// Nodes converted per worker task when copying a parse tree into the arena.
2276
+ const CONVERSION_CHUNK: usize = 2048;
2277
+
2278
+ /// A parse tree read by several workers at once while copying it into the
2279
+ /// mutable arena.
2280
+ struct SharedTree<'a> {
2281
+ html: &'a Html,
2282
+ ids: &'a [NodeId],
2283
+ reverse: &'a id_hash::IdMap<NodeId, u32>,
2284
+ }
2285
+
2286
+ // SAFETY: `Html` is not `Sync` only because elements cache their id and
2287
+ // classes in `OnceCell`s and tendrils keep their header in `Cell`s. Conversion
2288
+ // reads names, attribute lists and text through shared references and never
2289
+ // touches those caches, and the owning thread waits for the workers, so no
2290
+ // write can race these reads.
2291
+ unsafe impl Sync for SharedTree<'_> {}
2292
+
2293
+ impl SharedTree<'_> {
2294
+ fn convert(&self, range: std::ops::Range<usize>) -> Vec<arena::DomNode> {
2295
+ let mut nodes = Vec::with_capacity(range.len());
2296
+ nodes.extend(
2297
+ range.filter_map(|id| convert_node(self.html, self.ids, self.reverse, id as u32)),
2298
+ );
2299
+ nodes
2300
+ }
2301
+ }
2302
+
2303
+ /// A parse-tree node as a mutable-arena record. It reads only names,
2304
+ /// attributes and text, never the tree's lazily filled caches.
2305
+ fn convert_node(
2306
+ html: &Html,
2307
+ ids: &[NodeId],
2308
+ reverse: &id_hash::IdMap<NodeId, u32>,
2309
+ id: u32,
2310
+ ) -> Option<arena::DomNode> {
2311
+ let node = html.tree.get(*ids.get(id as usize)?)?;
2312
+ let pointer =
2313
+ |node: Option<NodeRef<'_, Node>>| node.and_then(|node| reverse.get(&node.id()).copied());
2314
+ let mut record = arena::DomNode::new("root");
2315
+ record.id = id;
2316
+ record.children = node
2317
+ .children()
2318
+ .filter_map(|child| reverse.get(&child.id()).copied())
2319
+ .collect();
2320
+ record.parent = pointer(node.parent());
2321
+ record.prev = pointer(node.prev_sibling());
2322
+ record.next = pointer(node.next_sibling());
2323
+ match node.value() {
2324
+ Node::Document => {
2325
+ record.extras.insert(
2326
+ "x-mode".into(),
2327
+ Value::String(
2328
+ match html.quirks_mode {
2329
+ html5ever::tree_builder::QuirksMode::Quirks => "quirks",
2330
+ html5ever::tree_builder::QuirksMode::LimitedQuirks => "limited-quirks",
2331
+ html5ever::tree_builder::QuirksMode::NoQuirks => "no-quirks",
2332
+ }
2333
+ .into(),
2334
+ ),
2335
+ );
2336
+ }
2337
+ Node::Fragment => {}
2338
+ Node::Text(data) => {
2339
+ record.kind = "text".into();
2340
+ record.data = Some(String::from(&**data));
2341
+ }
2342
+ Node::Comment(data) => {
2343
+ record.kind = "comment".into();
2344
+ record.data = Some(String::from(&**data));
2345
+ }
2346
+ Node::Doctype(data) => {
2347
+ record.kind = "directive".into();
2348
+ record.name = Some("!doctype".into());
2349
+ record
2350
+ .extras
2351
+ .insert("x-name".into(), Value::String(data.name().into()));
2352
+ record
2353
+ .extras
2354
+ .insert("x-publicId".into(), Value::String(data.public_id().into()));
2355
+ record
2356
+ .extras
2357
+ .insert("x-systemId".into(), Value::String(data.system_id().into()));
2358
+ record.data = Some(doctype_content(data));
2359
+ }
2360
+ Node::ProcessingInstruction(data) => {
2361
+ record.kind = "directive".into();
2362
+ record.name = Some(String::from(&*data.target));
2363
+ record.data = Some(String::from(&*data.data));
2364
+ }
2365
+ Node::Element(element) => {
2366
+ record.kind = match element.name() {
2367
+ "script" => "script",
2368
+ "style" => "style",
2369
+ _ => "tag",
2370
+ }
2371
+ .into();
2372
+ record.name = Some(element.name().into());
2373
+ record.namespace = Some(arena::namespace_name(&element.name.ns));
2374
+ if let Some(prefix) = &element.name.prefix {
2375
+ record
2376
+ .extras
2377
+ .insert("prefix".into(), Value::String(String::from(&**prefix)));
2378
+ }
2379
+ let (mut namespaces, mut prefixes) = (Map::new(), Map::new());
2380
+ if !element.attrs.is_empty() {
2381
+ record.attribs = arena::Attributes::with_capacity(element.attrs.len());
2382
+ }
2383
+ for (name, value) in &element.attrs {
2384
+ let key = String::from(&*name.local);
2385
+ if !name.ns.is_empty() {
2386
+ namespaces.insert(key.clone(), Value::String(String::from(&*name.ns)));
2387
+ }
2388
+ if let Some(prefix) = &name.prefix {
2389
+ prefixes.insert(key.clone(), Value::String(String::from(&**prefix)));
2390
+ }
2391
+ record
2392
+ .attribs
2393
+ .insert(key, Value::String(String::from(&**value)));
2394
+ }
2395
+ record.attribs_namespace = Some(Box::new(namespaces));
2396
+ record.attribs_prefix = Some(Box::new(prefixes));
2397
+ }
2398
+ }
2399
+ Some(record)
2400
+ }
2401
+
2402
+ /// A doctype's `data`, as htmlparser2 reports the declaration.
2403
+ pub(crate) fn doctype_content(data: &scraper::node::Doctype) -> String {
2404
+ let mut content = format!("!DOCTYPE {}", data.name());
2405
+ if !data.public_id().is_empty() {
2406
+ content.push_str(" PUBLIC ");
2407
+ content.push_str(&quote_doctype(data.public_id()));
2408
+ } else if !data.system_id().is_empty() {
2409
+ content.push_str(" SYSTEM");
2410
+ }
2411
+ if !data.system_id().is_empty() {
2412
+ content.push(' ');
2413
+ content.push_str(&quote_doctype(data.system_id()));
2414
+ }
2415
+ content
2416
+ }
2417
+
2418
+ fn quote_doctype(value: &str) -> String {
2419
+ let quote = if value.contains('"') { '\'' } else { '"' };
2420
+ format!("{quote}{value}{quote}")
2421
+ }
2422
+
2423
+ fn json_error(error: serde_json::Error) -> Error {
2424
+ Error::new(Status::GenericFailure, error.to_string())
2425
+ }
2426
+
2427
+ fn escape_html(value: &str, attribute: bool, output: &mut String) {
2428
+ let mut start = 0;
2429
+ for (offset, ch) in value.char_indices() {
2430
+ let replacement = match ch {
2431
+ '&' => "&amp;",
2432
+ '\u{a0}' => "&nbsp;",
2433
+ '"' if attribute => "&quot;",
2434
+ '<' if !attribute => "&lt;",
2435
+ '>' if !attribute => "&gt;",
2436
+ _ => continue,
2437
+ };
2438
+ output.push_str(&value[start..offset]);
2439
+ output.push_str(replacement);
2440
+ start = offset + ch.len_utf8();
2441
+ }
2442
+ output.push_str(&value[start..]);
2443
+ }
2444
+
2445
+ fn is_void(element: &scraper::node::Element) -> bool {
2446
+ element.name.ns.as_ref() == HTML_NS && void_name(element.name())
2447
+ }
2448
+
2449
+ pub(crate) fn void_name(name: &str) -> bool {
2450
+ matches!(
2451
+ name,
2452
+ "area"
2453
+ | "base"
2454
+ | "basefont"
2455
+ | "bgsound"
2456
+ | "br"
2457
+ | "col"
2458
+ | "embed"
2459
+ | "frame"
2460
+ | "hr"
2461
+ | "img"
2462
+ | "input"
2463
+ | "keygen"
2464
+ | "link"
2465
+ | "meta"
2466
+ | "param"
2467
+ | "source"
2468
+ | "track"
2469
+ | "wbr"
2470
+ )
2471
+ }
2472
+
2473
+ fn serialize_node(node: NodeRef<'_, Node>, output: &mut String) {
2474
+ // Use the tree's iterative traversal so deeply nested untrusted HTML does
2475
+ // not consume the native stack during serialization.
2476
+ for edge in node.traverse() {
2477
+ let node = match edge {
2478
+ ego_tree::iter::Edge::Open(node) => node,
2479
+ ego_tree::iter::Edge::Close(node) => {
2480
+ if let Some(element) = node
2481
+ .value()
2482
+ .as_element()
2483
+ .filter(|element| !is_void(element))
2484
+ {
2485
+ output.push_str("</");
2486
+ output.push_str(element.name());
2487
+ output.push('>');
2488
+ }
2489
+ continue;
2490
+ }
2491
+ };
2492
+ match node.value() {
2493
+ Node::Document | Node::Fragment => {}
2494
+ Node::Text(text) => {
2495
+ let raw = node
2496
+ .parent()
2497
+ .and_then(|parent| parent.value().as_element())
2498
+ .is_some_and(|element| {
2499
+ element.name.ns.as_ref() == HTML_NS
2500
+ && matches!(
2501
+ element.name(),
2502
+ "script"
2503
+ | "style"
2504
+ | "xmp"
2505
+ | "iframe"
2506
+ | "noembed"
2507
+ | "noframes"
2508
+ | "plaintext"
2509
+ | "noscript"
2510
+ )
2511
+ });
2512
+ if raw {
2513
+ output.push_str(text);
2514
+ } else {
2515
+ escape_html(text, false, output);
2516
+ }
2517
+ }
2518
+ Node::Comment(comment) => {
2519
+ output.push_str("<!--");
2520
+ output.push_str(comment);
2521
+ output.push_str("-->");
2522
+ }
2523
+ Node::Doctype(doctype) => {
2524
+ output.push_str("<!DOCTYPE ");
2525
+ output.push_str(doctype.name());
2526
+ output.push('>');
2527
+ }
2528
+ Node::ProcessingInstruction(instruction) => {
2529
+ output.push_str("<?");
2530
+ output.push_str(&instruction.target);
2531
+ output.push(' ');
2532
+ output.push_str(&instruction.data);
2533
+ output.push_str("?>");
2534
+ }
2535
+ Node::Element(element) => {
2536
+ output.push('<');
2537
+ output.push_str(element.name());
2538
+ let attributes = js_property_order(
2539
+ element
2540
+ .attrs
2541
+ .iter()
2542
+ .map(|(name, value)| (name.local.as_ref(), (name, value))),
2543
+ );
2544
+ for (_, (name, value)) in attributes {
2545
+ output.push(' ');
2546
+ push_attribute_name(
2547
+ output,
2548
+ &name.local,
2549
+ Some(name.ns.as_ref()),
2550
+ name.prefix.as_deref(),
2551
+ );
2552
+ output.push_str("=\"");
2553
+ escape_html(value, true, output);
2554
+ output.push('"');
2555
+ }
2556
+ output.push('>');
2557
+ }
2558
+ }
2559
+ }
2560
+ }
2561
+
2562
+ pub struct ParseTask {
2563
+ html: String,
2564
+ is_document: bool,
2565
+ options: Value,
2566
+ }
2567
+
2568
+ #[napi]
2569
+ impl Task for ParseTask {
2570
+ type Output = NativeDocument;
2571
+ type JsValue = NativeDocument;
2572
+
2573
+ fn compute(&mut self) -> Result<Self::Output> {
2574
+ let _batch = runtime::batch();
2575
+ let _permit = runtime::permit()?;
2576
+ Ok(NativeDocument::parse_options(
2577
+ &self.html,
2578
+ self.is_document,
2579
+ self.options.clone(),
2580
+ None,
2581
+ ))
2582
+ }
2583
+
2584
+ fn resolve(&mut self, _: Env, output: Self::Output) -> Result<Self::JsValue> {
2585
+ Ok(output)
2586
+ }
2587
+ }
2588
+
2589
+ #[napi]
2590
+ pub fn parse_async(
2591
+ html: String,
2592
+ is_document: bool,
2593
+ options_json: Option<String>,
2594
+ ) -> AsyncTask<ParseTask> {
2595
+ let options = options_json
2596
+ .and_then(|value| serde_json::from_str(&value).ok())
2597
+ .unwrap_or_else(|| serde_json::json!({}));
2598
+ AsyncTask::new(ParseTask {
2599
+ html,
2600
+ is_document,
2601
+ options,
2602
+ })
2603
+ }
2604
+
2605
+ fn thread_count(requested: Option<u32>, jobs: usize) -> Result<usize> {
2606
+ if matches!(requested, Some(0)) || requested.is_some_and(|threads| threads > 1024) {
2607
+ return Err(Error::new(
2608
+ Status::InvalidArg,
2609
+ "threads must be an integer between 1 and 1024",
2610
+ ));
2611
+ }
2612
+ let requested = requested
2613
+ .map(|n| n as usize)
2614
+ .unwrap_or_else(|| std::thread::available_parallelism().map_or(1, usize::from));
2615
+ Ok(requested.min(jobs.max(1)))
2616
+ }
2617
+
2618
+ pub struct ParseManyTask {
2619
+ htmls: Vec<String>,
2620
+ is_document: bool,
2621
+ threads: usize,
2622
+ options: Value,
2623
+ /// Extraction plans to run on each root after parsing; with them, each
2624
+ /// document also gets its snapshot built on the parsing thread.
2625
+ plans: Option<Vec<Value>>,
2626
+ }
2627
+
2628
+ #[napi]
2629
+ impl Task for ParseManyTask {
2630
+ type Output = Vec<NativeDocument>;
2631
+ type JsValue = Vec<NativeDocument>;
2632
+
2633
+ fn compute(&mut self) -> Result<Self::Output> {
2634
+ runtime::parallel_map(&self.htmls, self.threads, |html| {
2635
+ let document =
2636
+ NativeDocument::parse_options(html, self.is_document, self.options.clone(), None);
2637
+ if let Some(plans) = &self.plans {
2638
+ let results = plans
2639
+ .iter()
2640
+ .map(|plan| {
2641
+ let args =
2642
+ serde_json::json!({"ids": [0], "plan": plan, "options": self.options});
2643
+ document.op("extractPlan".into(), args.to_string()).ok()
2644
+ })
2645
+ .collect();
2646
+ *document.prepared_plans.borrow_mut() = Some(results);
2647
+ // With source locations the snapshot is built from the mutable tree instead.
2648
+ if document.source.borrow().is_none() {
2649
+ *document.prepared_snapshot.borrow_mut() = Some(snapshot::build(&document));
2650
+ }
2651
+ }
2652
+ Ok(document)
2653
+ })
2654
+ }
2655
+
2656
+ fn resolve(&mut self, _: Env, output: Self::Output) -> Result<Self::JsValue> {
2657
+ Ok(output)
2658
+ }
2659
+ }
2660
+
2661
+ #[napi]
2662
+ pub fn parse_many_async(
2663
+ htmls: Vec<String>,
2664
+ is_document: bool,
2665
+ threads: Option<u32>,
2666
+ options_json: Option<String>,
2667
+ plans_json: Option<String>,
2668
+ ) -> Result<AsyncTask<ParseManyTask>> {
2669
+ let threads = thread_count(threads, htmls.len())?;
2670
+ let plans = plans_json
2671
+ .map(|json| serde_json::from_str(&json).map_err(json_error))
2672
+ .transpose()?;
2673
+ Ok(AsyncTask::new(ParseManyTask {
2674
+ htmls,
2675
+ is_document,
2676
+ threads,
2677
+ options: options_json
2678
+ .and_then(|value| serde_json::from_str(&value).ok())
2679
+ .unwrap_or_else(|| serde_json::json!({})),
2680
+ plans,
2681
+ }))
2682
+ }
2683
+
2684
+ #[napi(object)]
2685
+ #[derive(Clone)]
2686
+ pub struct ExtractQuery {
2687
+ pub key: String,
2688
+ pub selector: String,
2689
+ pub attr: Option<String>,
2690
+ pub all: Option<bool>,
2691
+ }
2692
+
2693
+ pub struct ExtractManyTask {
2694
+ htmls: Vec<String>,
2695
+ queries: Vec<ExtractQuery>,
2696
+ threads: usize,
2697
+ is_document: bool,
2698
+ options: Value,
2699
+ }
2700
+
2701
+ #[napi]
2702
+ impl Task for ExtractManyTask {
2703
+ type Output = String;
2704
+ type JsValue = String;
2705
+
2706
+ fn compute(&mut self) -> Result<Self::Output> {
2707
+ // Validate before scheduling any documents. Each worker compiles its
2708
+ // selectors independently, because selector internals need not be Sync.
2709
+ let mut keys = HashSet::new();
2710
+ for query in &self.queries {
2711
+ // Full selector validation happens through the native selector
2712
+ // engine for each document, including jQuery-only pseudo classes.
2713
+ if !keys.insert(&query.key) {
2714
+ return Err(Error::new(
2715
+ Status::InvalidArg,
2716
+ format!("Duplicate extraction key {:?}", query.key),
2717
+ ));
2718
+ }
2719
+ }
2720
+ let records: Result<Vec<_>> = runtime::parallel_map(&self.htmls, self.threads, |html| {
2721
+ let document =
2722
+ NativeDocument::parse_options(html, self.is_document, self.options.clone(), None);
2723
+ let mut record = Map::new();
2724
+ for query in &self.queries {
2725
+ let ids = document.select_ids(&query.selector, None)?;
2726
+ let extract = |id| -> Option<Value> {
2727
+ match &query.attr {
2728
+ Some(attr) => document.attribute(id, attr).map(Value::String),
2729
+ None => Some(Value::String(document.text_ids(&[id]))),
2730
+ }
2731
+ };
2732
+ if query.all.unwrap_or(false) {
2733
+ // Cheerio's map().get() drops absent attribute values.
2734
+ record.insert(
2735
+ query.key.clone(),
2736
+ Value::Array(ids.into_iter().filter_map(extract).collect()),
2737
+ );
2738
+ } else if let Some(value) = ids.first().and_then(|id| extract(*id)) {
2739
+ record.insert(query.key.clone(), value);
2740
+ }
2741
+ }
2742
+ Ok(Value::Object(record))
2743
+ });
2744
+ serde_json::to_string(&records?).map_err(json_error)
2745
+ }
2746
+
2747
+ fn resolve(&mut self, _: Env, output: Self::Output) -> Result<Self::JsValue> {
2748
+ Ok(output)
2749
+ }
2750
+ }
2751
+
2752
+ #[napi]
2753
+ pub fn extract_many_async(
2754
+ htmls: Vec<String>,
2755
+ queries: Vec<ExtractQuery>,
2756
+ threads: Option<u32>,
2757
+ is_document: Option<bool>,
2758
+ options_json: Option<String>,
2759
+ ) -> Result<AsyncTask<ExtractManyTask>> {
2760
+ let threads = thread_count(threads, htmls.len())?;
2761
+ Ok(AsyncTask::new(ExtractManyTask {
2762
+ htmls,
2763
+ queries,
2764
+ threads,
2765
+ is_document: is_document.unwrap_or(true),
2766
+ options: options_json
2767
+ .and_then(|value| serde_json::from_str(&value).ok())
2768
+ .unwrap_or_else(|| serde_json::json!({})),
2769
+ }))
2770
+ }
2771
+
2772
+ #[napi]
2773
+ pub fn parse_with_tree_adapter(
2774
+ env: Env,
2775
+ source: String,
2776
+ is_document: bool,
2777
+ options_json: String,
2778
+ bridge: napi::JsFunction,
2779
+ ) -> Result<NativeDocument> {
2780
+ hook_sink::parse(env, source, is_document, options_json, bridge)
2781
+ }
2782
+
2783
+ pub struct ExtractPlanTask {
2784
+ htmls: Vec<String>,
2785
+ plan: Value,
2786
+ threads: usize,
2787
+ options: Value,
2788
+ }
2789
+ #[napi]
2790
+ impl Task for ExtractPlanTask {
2791
+ type Output = String;
2792
+ type JsValue = String;
2793
+ fn compute(&mut self) -> Result<String> {
2794
+ let rows = runtime::parallel_map(&self.htmls, self.threads, |html| {
2795
+ let document = NativeDocument::parse_options(html, true, self.options.clone(), None);
2796
+ extraction::execute(&document, &self.plan)
2797
+ })?;
2798
+ serde_json::to_string(&rows).map_err(json_error)
2799
+ }
2800
+ fn resolve(&mut self, _: Env, output: String) -> Result<String> {
2801
+ Ok(output)
2802
+ }
2803
+ }
2804
+ #[napi]
2805
+ pub fn extract_plan_many_async(
2806
+ htmls: Vec<String>,
2807
+ plan_json: String,
2808
+ threads: Option<u32>,
2809
+ options_json: Option<String>,
2810
+ ) -> Result<AsyncTask<ExtractPlanTask>> {
2811
+ let threads = thread_count(threads, htmls.len())?;
2812
+ let plan = serde_json::from_str(&plan_json).map_err(json_error)?;
2813
+ let options = options_json
2814
+ .map(|value| serde_json::from_str(&value))
2815
+ .transpose()
2816
+ .map_err(json_error)?
2817
+ .unwrap_or_else(|| serde_json::json!({}));
2818
+ Ok(AsyncTask::new(ExtractPlanTask {
2819
+ htmls,
2820
+ plan,
2821
+ threads,
2822
+ options,
2823
+ }))
2824
+ }
2825
+
2826
+ #[cfg(test)]
2827
+ mod tests {
2828
+ use super::*;
2829
+
2830
+ #[test]
2831
+ #[ignore = "diagnostic timing; run explicitly in release mode"]
2832
+ fn profile_document_load_phases() {
2833
+ let mut source = String::from("<main>");
2834
+ for i in 0..2000 {
2835
+ source.push_str(&format!("<article class='card' id='n{i}'><h2>Title {i}</h2><a href='/{i}'>link</a><img src='/{i}.png'><p>text <b>bold</b></p></article>"));
2836
+ }
2837
+ source.push_str("</main>");
2838
+ let mut parse_samples = Vec::new();
2839
+ let mut prepare_samples = Vec::new();
2840
+ for iteration in 0..35 {
2841
+ let start = std::time::Instant::now();
2842
+ let html = html5ever::driver::parse_document(
2843
+ sink::CompatibleSink::new(sized_html(&source, true)),
2844
+ html5ever::driver::ParseOpts::default(),
2845
+ )
2846
+ .one(source.as_str());
2847
+ let parsed = start.elapsed();
2848
+ let start = std::time::Instant::now();
2849
+ let document = NativeDocument::from_html(html, true, serde_json::json!({}), None);
2850
+ let prepared = start.elapsed();
2851
+ std::hint::black_box(&document);
2852
+ if iteration >= 5 {
2853
+ parse_samples.push(parsed);
2854
+ prepare_samples.push(prepared);
2855
+ }
2856
+ }
2857
+ parse_samples.sort_unstable();
2858
+ prepare_samples.sort_unstable();
2859
+ println!(
2860
+ "HTML parser median: {:?}; document preparation median: {:?}",
2861
+ parse_samples[15], prepare_samples[15]
2862
+ );
2863
+ }
2864
+
2865
+ #[test]
2866
+ #[ignore = "diagnostic timing; run explicitly in release mode"]
2867
+ fn profile_load_fixture_phases() {
2868
+ // The exact operations.mjs generated-2000 fixture.
2869
+ let card = |i: usize| {
2870
+ format!(
2871
+ "<div class=\"card c{}\" id=\"d{}\" data-index=\"{}\"><header><a href=\"/p/{}\" title=\"Item {}\"><img src=\"/i/{}.png\" alt=\"x{}\" width=\"10\"></a></header><p>Lorem ipsum {} <b>bold</b> &amp; <i>more</i></p><ul><li>a</li><li class=\"x\">b</li></ul></div>",
2872
+ i % 7, i, i, i, i, i, i, i
2873
+ )
2874
+ };
2875
+ let mut source = String::from("<!doctype html><html><head><title>Page</title><meta name=\"description\" content=\"d\"><script>var x = 1 < 2;</script></head><body><nav><a href=\"/\">Home</a></nav><main>");
2876
+ for i in 0..2000 {
2877
+ source.push_str(&card(i));
2878
+ }
2879
+ source.push_str("</main><footer>f</footer></body></html>");
2880
+ println!("fixture bytes: {}", source.len());
2881
+
2882
+ let mut sized_samples = Vec::new();
2883
+ let mut parse_samples = Vec::new();
2884
+ let mut prep_samples = Vec::new();
2885
+ let mut full_samples = Vec::new();
2886
+ let mut arena_samples = Vec::new();
2887
+ for iteration in 0..40 {
2888
+ let start = std::time::Instant::now();
2889
+ let sized = sized_html(&source, true);
2890
+ let sized_t = start.elapsed();
2891
+
2892
+ let start = std::time::Instant::now();
2893
+ let options = html5ever::driver::ParseOpts {
2894
+ tokenizer: html5ever::tokenizer::TokenizerOpts {
2895
+ discard_bom: false,
2896
+ cheerox_codec: false,
2897
+ ..Default::default()
2898
+ },
2899
+ tree_builder: html5ever::tree_builder::TreeBuilderOpts {
2900
+ scripting_enabled: true,
2901
+ ..Default::default()
2902
+ },
2903
+ };
2904
+ let html = html5ever::driver::parse_document(sink::CompatibleSink::new(sized), options)
2905
+ .one(source.as_str());
2906
+ let parse_t = start.elapsed();
2907
+
2908
+ let start = std::time::Instant::now();
2909
+ let document = NativeDocument::from_html(html, true, serde_json::json!({}), None);
2910
+ let prep_t = start.elapsed();
2911
+
2912
+ let start = std::time::Instant::now();
2913
+ document.ensure_arena().unwrap();
2914
+ let arena_t = start.elapsed();
2915
+ std::hint::black_box(&document);
2916
+
2917
+ if iteration >= 5 {
2918
+ sized_samples.push(sized_t);
2919
+ parse_samples.push(parse_t);
2920
+ prep_samples.push(prep_t);
2921
+ full_samples.push(sized_t + parse_t + prep_t);
2922
+ arena_samples.push(arena_t);
2923
+ }
2924
+ }
2925
+ let median = |mut v: Vec<std::time::Duration>| {
2926
+ v.sort_unstable();
2927
+ v[v.len() / 2]
2928
+ };
2929
+ println!(
2930
+ "sized: {:?}; parse: {:?}; prep: {:?}; full: {:?}; ensure_arena: {:?}",
2931
+ median(sized_samples),
2932
+ median(parse_samples),
2933
+ median(prep_samples),
2934
+ median(full_samples),
2935
+ median(arena_samples),
2936
+ );
2937
+ }
2938
+
2939
+ #[test]
2940
+ #[ignore = "diagnostic timing; run explicitly in release mode"]
2941
+ fn profile_load_fixture_null_sink() {
2942
+ use html5ever::tendril::StrTendril;
2943
+ use html5ever::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
2944
+ use html5ever::{Attribute, QualName};
2945
+ struct NullSink {
2946
+ next: std::cell::Cell<u32>,
2947
+ names: RefCell<Vec<html5ever::QualName>>,
2948
+ }
2949
+ impl NullSink {
2950
+ fn new() -> Self {
2951
+ Self {
2952
+ next: std::cell::Cell::new(0),
2953
+ names: RefCell::new(Vec::new()),
2954
+ }
2955
+ }
2956
+ }
2957
+ impl TreeSink for NullSink {
2958
+ type Handle = u32;
2959
+ type Output = ();
2960
+ type ElemName<'a> = std::cell::Ref<'a, html5ever::QualName>;
2961
+
2962
+ fn finish(self) -> Self::Output {}
2963
+ fn parse_error(&self, _msg: Cow<'static, str>) {}
2964
+ fn get_document(&self) -> Self::Handle {
2965
+ 0
2966
+ }
2967
+ fn same_node(&self, x: &Self::Handle, y: &Self::Handle) -> bool {
2968
+ x == y
2969
+ }
2970
+ fn elem_name<'a>(&'a self, target: &'a Self::Handle) -> Self::ElemName<'a> {
2971
+ std::cell::Ref::map(self.names.borrow(), |names| &names[*target as usize])
2972
+ }
2973
+ fn create_element(
2974
+ &self,
2975
+ name: QualName,
2976
+ _attrs: Vec<Attribute>,
2977
+ _flags: ElementFlags,
2978
+ ) -> Self::Handle {
2979
+ let id = self.next.get();
2980
+ self.next.set(id + 1);
2981
+ self.names.borrow_mut().push(name);
2982
+ id
2983
+ }
2984
+ fn create_comment(&self, _text: StrTendril) -> Self::Handle {
2985
+ u32::MAX - 1
2986
+ }
2987
+ fn append_doctype_to_document(&self, _: StrTendril, _: StrTendril, _: StrTendril) {}
2988
+ fn append(&self, _parent: &Self::Handle, _child: NodeOrText<Self::Handle>) {}
2989
+ fn append_before_sibling(&self, _: &Self::Handle, _: NodeOrText<Self::Handle>) {}
2990
+ fn append_based_on_parent_node(
2991
+ &self,
2992
+ _: &Self::Handle,
2993
+ _: &Self::Handle,
2994
+ _: NodeOrText<Self::Handle>,
2995
+ ) {
2996
+ }
2997
+ fn remove_from_parent(&self, _: &Self::Handle) {}
2998
+ fn reparent_children(&self, _: &Self::Handle, _: &Self::Handle) {}
2999
+ fn add_attrs_if_missing(&self, _: &Self::Handle, _: Vec<Attribute>) {}
3000
+ fn get_template_contents(&self, target: &Self::Handle) -> Self::Handle {
3001
+ *target
3002
+ }
3003
+ fn mark_script_already_started(&self, _: &Self::Handle) {}
3004
+ fn pop(&self, _: &Self::Handle) {}
3005
+ fn set_current_line(&self, _: u64) {}
3006
+ fn set_quirks_mode(&self, _: QuirksMode) {}
3007
+ fn create_pi(&self, _: StrTendril, _: StrTendril) -> Self::Handle {
3008
+ u32::MAX - 2
3009
+ }
3010
+ }
3011
+
3012
+ let card = |i: usize| {
3013
+ format!(
3014
+ "<div class=\"card c{}\" id=\"d{}\" data-index=\"{}\"><header><a href=\"/p/{}\" title=\"Item {}\"><img src=\"/i/{}.png\" alt=\"x{}\" width=\"10\"></a></header><p>Lorem ipsum {} <b>bold</b> &amp; <i>more</i></p><ul><li>a</li><li class=\"x\">b</li></ul></div>",
3015
+ i % 7, i, i, i, i, i, i, i
3016
+ )
3017
+ };
3018
+ let mut source = String::from("<!doctype html><html><head><title>Page</title><meta name=\"description\" content=\"d\"><script>var x = 1 < 2;</script></head><body><nav><a href=\"/\">Home</a></nav><main>");
3019
+ for i in 0..2000 {
3020
+ source.push_str(&card(i));
3021
+ }
3022
+ source.push_str("</main><footer>f</footer></body></html>");
3023
+
3024
+ let mut null_samples = Vec::new();
3025
+ let mut real_samples = Vec::new();
3026
+ for iteration in 0..40 {
3027
+ let start = std::time::Instant::now();
3028
+ let sink = std::rc::Rc::new(NullSink::new());
3029
+ // The tree builder owns the sink; a clone shares the same storage.
3030
+ #[derive(Clone)]
3031
+ struct Shared(std::rc::Rc<NullSink>);
3032
+ impl TreeSink for Shared {
3033
+ type Handle = u32;
3034
+ type Output = ();
3035
+ type ElemName<'a> = std::cell::Ref<'a, html5ever::QualName>;
3036
+ fn finish(self) -> Self::Output {}
3037
+ fn parse_error(&self, msg: Cow<'static, str>) {
3038
+ self.0.parse_error(msg)
3039
+ }
3040
+ fn get_document(&self) -> Self::Handle {
3041
+ self.0.get_document()
3042
+ }
3043
+ fn same_node(&self, x: &Self::Handle, y: &Self::Handle) -> bool {
3044
+ self.0.same_node(x, y)
3045
+ }
3046
+ fn elem_name<'a>(&'a self, target: &'a Self::Handle) -> Self::ElemName<'a> {
3047
+ self.0.elem_name(target)
3048
+ }
3049
+ fn create_element(
3050
+ &self,
3051
+ name: QualName,
3052
+ attrs: Vec<Attribute>,
3053
+ flags: ElementFlags,
3054
+ ) -> Self::Handle {
3055
+ self.0.create_element(name, attrs, flags)
3056
+ }
3057
+ fn create_comment(&self, text: StrTendril) -> Self::Handle {
3058
+ self.0.create_comment(text)
3059
+ }
3060
+ fn append_doctype_to_document(&self, a: StrTendril, b: StrTendril, c: StrTendril) {
3061
+ self.0.append_doctype_to_document(a, b, c)
3062
+ }
3063
+ fn append(&self, parent: &Self::Handle, child: NodeOrText<Self::Handle>) {
3064
+ self.0.append(parent, child)
3065
+ }
3066
+ fn append_before_sibling(&self, s: &Self::Handle, n: NodeOrText<Self::Handle>) {
3067
+ self.0.append_before_sibling(s, n)
3068
+ }
3069
+ fn append_based_on_parent_node(
3070
+ &self,
3071
+ e: &Self::Handle,
3072
+ p: &Self::Handle,
3073
+ c: NodeOrText<Self::Handle>,
3074
+ ) {
3075
+ self.0.append_based_on_parent_node(e, p, c)
3076
+ }
3077
+ fn remove_from_parent(&self, t: &Self::Handle) {
3078
+ self.0.remove_from_parent(t)
3079
+ }
3080
+ fn reparent_children(&self, n: &Self::Handle, np: &Self::Handle) {
3081
+ self.0.reparent_children(n, np)
3082
+ }
3083
+ fn add_attrs_if_missing(&self, t: &Self::Handle, a: Vec<Attribute>) {
3084
+ self.0.add_attrs_if_missing(t, a)
3085
+ }
3086
+ fn get_template_contents(&self, t: &Self::Handle) -> Self::Handle {
3087
+ self.0.get_template_contents(t)
3088
+ }
3089
+ fn mark_script_already_started(&self, t: &Self::Handle) {
3090
+ self.0.mark_script_already_started(t)
3091
+ }
3092
+ fn pop(&self, t: &Self::Handle) {
3093
+ self.0.pop(t)
3094
+ }
3095
+ fn set_current_line(&self, n: u64) {
3096
+ self.0.set_current_line(n)
3097
+ }
3098
+ fn set_quirks_mode(&self, m: QuirksMode) {
3099
+ self.0.set_quirks_mode(m)
3100
+ }
3101
+ fn create_pi(&self, a: StrTendril, b: StrTendril) -> Self::Handle {
3102
+ self.0.create_pi(a, b)
3103
+ }
3104
+ }
3105
+ html5ever::driver::parse_document(Shared(sink.clone()), Default::default())
3106
+ .one(source.as_str());
3107
+ let null_t = start.elapsed();
3108
+
3109
+ let start = std::time::Instant::now();
3110
+ let html = html5ever::driver::parse_document(
3111
+ sink::CompatibleSink::new(sized_html(&source, true)),
3112
+ Default::default(),
3113
+ )
3114
+ .one(source.as_str());
3115
+ let real_t = start.elapsed();
3116
+ std::hint::black_box(&html);
3117
+
3118
+ if iteration >= 5 {
3119
+ null_samples.push(null_t);
3120
+ real_samples.push(real_t);
3121
+ }
3122
+ }
3123
+ let median = |mut v: Vec<std::time::Duration>| {
3124
+ v.sort_unstable();
3125
+ v[v.len() / 2]
3126
+ };
3127
+ println!(
3128
+ "null sink parse: {:?}; scraper sink parse: {:?}",
3129
+ median(null_samples),
3130
+ median(real_samples)
3131
+ );
3132
+
3133
+ // Tokenizer only, with a token sink that drops everything.
3134
+ use html5ever::tokenizer::{
3135
+ BufferQueue, Token, TokenSink, TokenSinkResult, Tokenizer, TokenizerOpts,
3136
+ };
3137
+ struct DropSink;
3138
+ impl TokenSink for DropSink {
3139
+ type Handle = ();
3140
+ fn process_token(&self, _token: Token, _source: u64) -> TokenSinkResult<()> {
3141
+ TokenSinkResult::Continue
3142
+ }
3143
+ }
3144
+ let mut token_samples = Vec::new();
3145
+ let mut last_stats = (0u32, 0u32);
3146
+ for iteration in 0..40 {
3147
+ let start = std::time::Instant::now();
3148
+ let tokenizer = Tokenizer::new(DropSink, TokenizerOpts::default());
3149
+ let buffers = BufferQueue::default();
3150
+ buffers.push_back(html5ever::tendril::StrTendril::from_slice(source.as_str()));
3151
+ let _ = tokenizer.feed(&buffers);
3152
+ tokenizer.end();
3153
+ if iteration >= 5 {
3154
+ token_samples.push(start.elapsed());
3155
+ }
3156
+ last_stats = tokenizer.fast_scan_stats();
3157
+ }
3158
+ println!(
3159
+ "tokenizer only: {:?}; fast hits={}, bails={}",
3160
+ median(token_samples),
3161
+ last_stats.0,
3162
+ last_stats.1
3163
+ );
3164
+ }
3165
+
3166
+ #[test]
3167
+ #[ignore = "diagnostic timing; run explicitly in release mode"]
3168
+ #[allow(irrefutable_let_patterns)]
3169
+ fn profile_load_fixture_mid_sink() {
3170
+ use html5ever::tendril::StrTendril;
3171
+ use html5ever::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
3172
+ use html5ever::{Attribute, QualName};
3173
+
3174
+ // A flat-Vec sink: no RefCell, no ego-tree during parse, no IndexMap.
3175
+ // Children and parents are u32 indexes; finish() returns the count.
3176
+ #[allow(dead_code)]
3177
+ enum FlatNode {
3178
+ Element {
3179
+ name: QualName,
3180
+ attrs: Vec<(QualName, StrTendril)>,
3181
+ parent: u32,
3182
+ children: Vec<u32>,
3183
+ },
3184
+ }
3185
+ // TreeSink needs &'a QualName from &'a self; use RefCell-free storage
3186
+ // by returning through a raw pointer wrapper is unsafe; instead keep
3187
+ // names in a RefCell<Vec<QualName>> just for elem_name.
3188
+ struct FlatSinkWrap(std::rc::Rc<FlatCore>);
3189
+ struct FlatCore {
3190
+ nodes: RefCell<Vec<FlatNode>>,
3191
+ names: RefCell<Vec<QualName>>,
3192
+ }
3193
+ impl TreeSink for FlatSinkWrap {
3194
+ type Handle = u32;
3195
+ type Output = usize;
3196
+ type ElemName<'a> = std::cell::Ref<'a, QualName>;
3197
+ fn finish(self) -> usize {
3198
+ self.0.nodes.borrow().len()
3199
+ }
3200
+ fn parse_error(&self, _: Cow<'static, str>) {}
3201
+ fn get_document(&self) -> u32 {
3202
+ 0
3203
+ }
3204
+ fn same_node(&self, x: &u32, y: &u32) -> bool {
3205
+ x == y
3206
+ }
3207
+ fn elem_name<'a>(&'a self, target: &'a u32) -> std::cell::Ref<'a, QualName> {
3208
+ std::cell::Ref::map(self.0.names.borrow(), |names| &names[*target as usize])
3209
+ }
3210
+ fn create_element(
3211
+ &self,
3212
+ name: QualName,
3213
+ attrs: Vec<Attribute>,
3214
+ _f: ElementFlags,
3215
+ ) -> u32 {
3216
+ let mut nodes = self.0.nodes.borrow_mut();
3217
+ let id = nodes.len() as u32;
3218
+ let converted: Vec<(QualName, StrTendril)> =
3219
+ attrs.into_iter().map(|a| (a.name, a.value)).collect();
3220
+ nodes.push(FlatNode::Element {
3221
+ name: name.clone(),
3222
+ attrs: converted,
3223
+ parent: u32::MAX,
3224
+ children: Vec::new(),
3225
+ });
3226
+ self.0.names.borrow_mut().push(name);
3227
+ id
3228
+ }
3229
+ fn create_comment(&self, _: StrTendril) -> u32 {
3230
+ u32::MAX - 1
3231
+ }
3232
+ fn append_doctype_to_document(&self, _: StrTendril, _: StrTendril, _: StrTendril) {}
3233
+ fn append(&self, parent: &u32, child: NodeOrText<u32>) {
3234
+ let mut nodes = self.0.nodes.borrow_mut();
3235
+ match child {
3236
+ NodeOrText::AppendNode(id) => {
3237
+ if let FlatNode::Element { children, .. } = &mut nodes[*parent as usize] {
3238
+ children.push(id);
3239
+ }
3240
+ if let FlatNode::Element { parent: p, .. } = &mut nodes[id as usize] {
3241
+ *p = *parent;
3242
+ }
3243
+ }
3244
+ NodeOrText::AppendText(_) => {
3245
+ if let FlatNode::Element { children, .. } = &mut nodes[*parent as usize] {
3246
+ children.push(u32::MAX - 1);
3247
+ }
3248
+ }
3249
+ }
3250
+ }
3251
+ fn append_before_sibling(&self, _: &u32, _: NodeOrText<u32>) {}
3252
+ fn append_based_on_parent_node(&self, _: &u32, _: &u32, _: NodeOrText<u32>) {}
3253
+ fn remove_from_parent(&self, _: &u32) {}
3254
+ fn reparent_children(&self, _: &u32, _: &u32) {}
3255
+ fn add_attrs_if_missing(&self, target: &u32, attrs: Vec<Attribute>) {
3256
+ let mut nodes = self.0.nodes.borrow_mut();
3257
+ if let FlatNode::Element { attrs: stored, .. } = &mut nodes[*target as usize] {
3258
+ for attr in attrs {
3259
+ if !stored.iter().any(|(name, _)| *name == attr.name) {
3260
+ stored.push((attr.name, attr.value));
3261
+ }
3262
+ }
3263
+ }
3264
+ }
3265
+ fn get_template_contents(&self, t: &u32) -> u32 {
3266
+ *t
3267
+ }
3268
+ fn mark_script_already_started(&self, _: &u32) {}
3269
+ fn pop(&self, _: &u32) {}
3270
+ fn set_current_line(&self, _: u64) {}
3271
+ fn set_quirks_mode(&self, _: QuirksMode) {}
3272
+ fn create_pi(&self, _: StrTendril, _: StrTendril) -> u32 {
3273
+ u32::MAX - 2
3274
+ }
3275
+ }
3276
+
3277
+ let card = |i: usize| {
3278
+ format!(
3279
+ "<div class=\"card c{}\" id=\"d{}\" data-index=\"{}\"><header><a href=\"/p/{}\" title=\"Item {}\"><img src=\"/i/{}.png\" alt=\"x{}\" width=\"10\"></a></header><p>Lorem ipsum {} <b>bold</b> &amp; <i>more</i></p><ul><li>a</li><li class=\"x\">b</li></ul></div>",
3280
+ i % 7, i, i, i, i, i, i, i
3281
+ )
3282
+ };
3283
+ let mut source = String::from("<!doctype html><html><head><title>Page</title><meta name=\"description\" content=\"d\"><script>var x = 1 < 2;</script></head><body><nav><a href=\"/\">Home</a></nav><main>");
3284
+ for i in 0..2000 {
3285
+ source.push_str(&card(i));
3286
+ }
3287
+ source.push_str("</main><footer>f</footer></body></html>");
3288
+
3289
+ let mut flat_samples = Vec::new();
3290
+ let mut real_samples = Vec::new();
3291
+ for iteration in 0..40 {
3292
+ let start = std::time::Instant::now();
3293
+ let core = std::rc::Rc::new(FlatCore {
3294
+ nodes: RefCell::new(Vec::new()),
3295
+ names: RefCell::new(Vec::new()),
3296
+ });
3297
+ let out = html5ever::driver::parse_document(FlatSinkWrap(core), Default::default())
3298
+ .one(source.as_str());
3299
+ let flat_t = start.elapsed();
3300
+ std::hint::black_box(&out);
3301
+
3302
+ let start = std::time::Instant::now();
3303
+ let html = html5ever::driver::parse_document(
3304
+ sink::CompatibleSink::new(sized_html(&source, true)),
3305
+ Default::default(),
3306
+ )
3307
+ .one(source.as_str());
3308
+ let real_t = start.elapsed();
3309
+ std::hint::black_box(&html);
3310
+
3311
+ if iteration >= 5 {
3312
+ flat_samples.push(flat_t);
3313
+ real_samples.push(real_t);
3314
+ }
3315
+ }
3316
+ let median = |mut v: Vec<std::time::Duration>| {
3317
+ v.sort_unstable();
3318
+ v[v.len() / 2]
3319
+ };
3320
+ println!(
3321
+ "flat sink parse: {:?}; scraper sink parse: {:?}",
3322
+ median(flat_samples),
3323
+ median(real_samples)
3324
+ );
3325
+ }
3326
+
3327
+ #[test]
3328
+ #[ignore = "diagnostic timing; run explicitly in release mode"]
3329
+ fn profile_load_fixture_gum_path() {
3330
+ // The html5gum tokenizer driving html5ever's tree builder — the
3331
+ // bridge already used for locations — versus the html5ever tokenizer.
3332
+ let card = |i: usize| {
3333
+ format!(
3334
+ "<div class=\"card c{}\" id=\"d{}\" data-index=\"{}\"><header><a href=\"/p/{}\" title=\"Item {}\"><img src=\"/i/{}.png\" alt=\"x{}\" width=\"10\"></a></header><p>Lorem ipsum {} <b>bold</b> &amp; <i>more</i></p><ul><li>a</li><li class=\"x\">b</li></ul></div>",
3335
+ i % 7, i, i, i, i, i, i, i
3336
+ )
3337
+ };
3338
+ let mut source = String::from("<!doctype html><html><head><title>Page</title><meta name=\"description\" content=\"d\"><script>var x = 1 < 2;</script></head><body><nav><a href=\"/\">Home</a></nav><main>");
3339
+ for i in 0..2000 {
3340
+ source.push_str(&card(i));
3341
+ }
3342
+ source.push_str("</main><footer>f</footer></body></html>");
3343
+
3344
+ let options = serde_json::json!({});
3345
+ let mut gum_samples = Vec::new();
3346
+ let mut ever_samples = Vec::new();
3347
+ for iteration in 0..40 {
3348
+ let start = std::time::Instant::now();
3349
+ let context = std::rc::Rc::new(diagnostics::DiagnosticContext::new(&source));
3350
+ let html = diagnostics::parse_with_sink(
3351
+ &source,
3352
+ true,
3353
+ &options,
3354
+ sink::CompatibleSink::new(sized_html(&source, true)),
3355
+ context,
3356
+ |_, _| {},
3357
+ );
3358
+ let gum_t = start.elapsed();
3359
+ std::hint::black_box(&html);
3360
+
3361
+ let start = std::time::Instant::now();
3362
+ let html = html5ever::driver::parse_document(
3363
+ sink::CompatibleSink::new(sized_html(&source, true)),
3364
+ Default::default(),
3365
+ )
3366
+ .one(source.as_str());
3367
+ let ever_t = start.elapsed();
3368
+ std::hint::black_box(&html);
3369
+
3370
+ if iteration >= 5 {
3371
+ gum_samples.push(gum_t);
3372
+ ever_samples.push(ever_t);
3373
+ }
3374
+ }
3375
+ let median = |mut v: Vec<std::time::Duration>| {
3376
+ v.sort_unstable();
3377
+ v[v.len() / 2]
3378
+ };
3379
+ println!(
3380
+ "gum+bridge parse: {:?}; html5ever parse: {:?}",
3381
+ median(gum_samples),
3382
+ median(ever_samples)
3383
+ );
3384
+ }
3385
+
3386
+ #[test]
3387
+ fn forest_roots_match_export_before_and_after_detach() {
3388
+ for is_document in [false, true] {
3389
+ let document = NativeDocument::parse(
3390
+ "<main><p>A</p><template><b>B</b></template></main>",
3391
+ is_document,
3392
+ );
3393
+ let check = || {
3394
+ let records: Vec<Value> =
3395
+ serde_json::from_str(&document.export_tree().unwrap()).unwrap();
3396
+ let expected: Vec<u32> = records
3397
+ .iter()
3398
+ .filter(|node| node["parent"].is_null())
3399
+ .map(|node| node["id"].as_u64().unwrap() as u32)
3400
+ .collect();
3401
+ assert_eq!(document.forest_root_ids().unwrap(), expected);
3402
+ };
3403
+ check();
3404
+ let id = document.select_ids("p", None).unwrap()[0];
3405
+ document
3406
+ .op("remove".into(), serde_json::json!({"ids":[id]}).to_string())
3407
+ .unwrap();
3408
+ check();
3409
+ document.ensure_arena().unwrap();
3410
+ check();
3411
+ }
3412
+ }
3413
+
3414
+ #[test]
3415
+ fn attribute_writes_keep_the_parse_tree() {
3416
+ let document = NativeDocument::parse(
3417
+ "<main><p id='a' class='x'>one</p><svg><use href='#h' xlink:href='#x'/></svg></main>",
3418
+ false,
3419
+ );
3420
+ let ids = document.select_ids("p, use", None).unwrap();
3421
+ for (command, args) in [
3422
+ (
3423
+ "attr",
3424
+ serde_json::json!({"ids":ids,"name":"data-k","value":"v"}),
3425
+ ),
3426
+ ("addClass", serde_json::json!({"ids":ids,"name":"y z"})),
3427
+ ("removeClass", serde_json::json!({"ids":ids,"name":"x"})),
3428
+ ("removeAttr", serde_json::json!({"ids":ids,"name":"id"})),
3429
+ ] {
3430
+ document.op(command.into(), args.to_string()).unwrap();
3431
+ }
3432
+ assert!(document.arena.borrow().is_none());
3433
+ assert_eq!(
3434
+ document.serialize_nodes(&ids, false),
3435
+ "<p class=\"y z\" data-k=\"v\">one</p><use xlink:href=\"#x\" data-k=\"v\" class=\"y z\"></use>"
3436
+ );
3437
+ assert_eq!(document.select_ids(".z", None).unwrap(), ids);
3438
+ }
3439
+
3440
+ #[test]
3441
+ fn managed_detached_payloads_are_reclaimed_and_slots_are_reused() {
3442
+ let document = NativeDocument::parse("<main></main>", false);
3443
+ document.retain(vec![0]);
3444
+ let main = document.select_ids("main", None).unwrap()[0];
3445
+ document
3446
+ .op(
3447
+ "clone".into(),
3448
+ serde_json::json!({"ids":[main],"deep":false}).to_string(),
3449
+ )
3450
+ .unwrap();
3451
+ assert_eq!(document.collect_garbage().immutable_nodes, 0);
3452
+ assert!(document.ids.borrow().is_empty());
3453
+ assert!(document.reverse.borrow().is_empty());
3454
+ for _ in 0..1000 {
3455
+ let id = document
3456
+ .create("<p>temporary payload</p>".into(), None, None)
3457
+ .unwrap()[0];
3458
+ document.retain(vec![id]);
3459
+ document.release(vec![id]);
3460
+ let stats = document.collect_garbage();
3461
+ assert_eq!(stats.live, 2);
3462
+ assert!(stats.slots <= 2);
3463
+ }
3464
+ let held = document
3465
+ .create(
3466
+ "<section><b>held</b><i>sibling</i></section>".into(),
3467
+ None,
3468
+ None,
3469
+ )
3470
+ .unwrap()[0];
3471
+ let child = document.select_ids("b", Some(&[held])).unwrap()[0];
3472
+ document.retain(vec![child]);
3473
+ document.release(vec![0]);
3474
+ document.collect_garbage();
3475
+ assert_eq!(document.text_ids(&[held]), "heldsibling");
3476
+ assert!(document.arena.borrow().as_ref().unwrap().node(0).is_none());
3477
+ document.release(vec![child]);
3478
+ assert_eq!(document.collect_garbage().live, 0);
3479
+ }
3480
+
3481
+ #[test]
3482
+ fn adopting_pinned_nodes_transfers_ownership_without_retaining_source_payloads() {
3483
+ let source = NativeDocument::parse("<p><b>move</b></p><aside>stay</aside>", false);
3484
+ let target = NativeDocument::parse("<main></main>", false);
3485
+ source.retain(vec![0]);
3486
+ target.retain(vec![0]);
3487
+ let p = source.select_ids("p", None).unwrap()[0];
3488
+ let b = source.select_ids("b", None).unwrap()[0];
3489
+ source.retain(vec![b]);
3490
+ let result: Value =
3491
+ serde_json::from_str(&target.adopt(&source, vec![p], None).unwrap()).unwrap();
3492
+ let mapping: Vec<[u32; 2]> = serde_json::from_value(result["mapping"].clone()).unwrap();
3493
+ let moved_b = mapping.iter().find(|pair| pair[0] == b).unwrap()[1];
3494
+ source.collect_garbage();
3495
+ target.collect_garbage();
3496
+ assert_eq!(target.text_ids(&[moved_b]), "move");
3497
+ assert!(!source.retained.borrow().contains_key(&b));
3498
+ assert_eq!(target.retained.borrow().get(&moved_b), Some(&1));
3499
+ target.release(vec![moved_b]);
3500
+ target.collect_garbage();
3501
+ assert_eq!(target.collect_garbage().live, 2);
3502
+ }
3503
+
3504
+ #[test]
3505
+ fn text_comment_doctype_locations_include_entities_and_foster_parenting() {
3506
+ let source = "<!doctype html><p>A&amp;B<!--c-->😀</p><table>A<tr><td>B</td></tr>C</table>";
3507
+ let document = NativeDocument::parse_options(
3508
+ source,
3509
+ true,
3510
+ serde_json::json!({"sourceCodeLocationInfo":true}),
3511
+ None,
3512
+ );
3513
+ document.ensure_arena().unwrap();
3514
+ let arena = document.arena.borrow();
3515
+ let nodes = &arena.as_ref().unwrap().nodes;
3516
+ let location = |data: &str| {
3517
+ nodes
3518
+ .iter()
3519
+ .find(|node| node.data.as_deref() == Some(data))
3520
+ .unwrap()
3521
+ .extras
3522
+ .get("sourceCodeLocation")
3523
+ .cloned()
3524
+ .unwrap()
3525
+ };
3526
+ assert_eq!(location("A&B")["startOffset"], 18);
3527
+ assert_eq!(location("A&B")["endOffset"], 25);
3528
+ assert_eq!(location("c")["startOffset"], 25);
3529
+ assert_eq!(location("c")["endOffset"], 33);
3530
+ assert_eq!(location("😀")["startOffset"], 33);
3531
+ assert_eq!(location("😀")["endOffset"], 35);
3532
+ let foster = location("AC");
3533
+ assert_eq!(foster["startOffset"], 46);
3534
+ assert_eq!(foster["endOffset"], 67);
3535
+ let html = nodes
3536
+ .iter()
3537
+ .find(|node| node.name.as_deref() == Some("html"))
3538
+ .unwrap();
3539
+ assert_eq!(html.extras.get("sourceCodeLocation"), Some(&Value::Null));
3540
+ }
3541
+
3542
+ #[test]
3543
+ fn htmlparser2_cdata_and_xml_element_types_match_parser_options() {
3544
+ let xml = NativeDocument::parse_options(
3545
+ "<script>x</script><style>y</style>",
3546
+ true,
3547
+ serde_json::json!({"xmlMode":true}),
3548
+ None,
3549
+ );
3550
+ xml.ensure_arena().unwrap();
3551
+ assert!(xml
3552
+ .arena
3553
+ .borrow()
3554
+ .as_ref()
3555
+ .unwrap()
3556
+ .nodes
3557
+ .iter()
3558
+ .filter(|node| node.name.is_some())
3559
+ .all(|node| node.kind == "tag"));
3560
+ for (options, expected) in [
3561
+ (
3562
+ serde_json::json!({"_useHtmlParser2":true}),
3563
+ "<root><!--[CDATA[x<y]]--></root>",
3564
+ ),
3565
+ (
3566
+ serde_json::json!({"_useHtmlParser2":true,"recognizeCDATA":true}),
3567
+ "<root><![CDATA[x<y]]></root>",
3568
+ ),
3569
+ (
3570
+ serde_json::json!({"xmlMode":true}),
3571
+ "<root><![CDATA[x<y]]></root>",
3572
+ ),
3573
+ ] {
3574
+ assert_eq!(
3575
+ NativeDocument::parse_options("<root><![CDATA[x<y]]></root>", true, options, None)
3576
+ .outer_html(vec![0]),
3577
+ expected
3578
+ );
3579
+ }
3580
+ }
3581
+
3582
+ #[test]
3583
+ fn live_token_locations_do_not_reuse_ignored_identical_whitespace() {
3584
+ let source =
3585
+ "\n<html>\n<head></head>\n<body>\n<p a=one b=\"two\" c>Text</p>\n</body></html>";
3586
+ let document = NativeDocument::parse_options(
3587
+ source,
3588
+ true,
3589
+ serde_json::json!({"sourceCodeLocationInfo":true}),
3590
+ None,
3591
+ );
3592
+ document.ensure_arena().unwrap();
3593
+ let arena = document.arena.borrow();
3594
+ let arena = arena.as_ref().unwrap();
3595
+ let body = arena
3596
+ .nodes
3597
+ .iter()
3598
+ .find(|node| node.name.as_deref() == Some("body"))
3599
+ .unwrap();
3600
+ let first = arena.node(body.children[0]).unwrap();
3601
+ assert_eq!(first.data.as_deref(), Some("\n"));
3602
+ assert_eq!(first.extras["sourceCodeLocation"]["startOffset"], 28);
3603
+ let p = arena
3604
+ .nodes
3605
+ .iter()
3606
+ .find(|node| node.name.as_deref() == Some("p"))
3607
+ .unwrap();
3608
+ assert_eq!(
3609
+ p.extras["sourceCodeLocation"]["attrs"]["a"]["startOffset"],
3610
+ 32
3611
+ );
3612
+ assert_eq!(
3613
+ p.extras["sourceCodeLocation"]["attrs"]["a"]["endOffset"],
3614
+ 37
3615
+ );
3616
+ }
3617
+
3618
+ #[test]
3619
+ fn reconstructed_formatting_and_template_fragments_keep_native_source_locations() {
3620
+ let document = NativeDocument::parse_options(
3621
+ "<p><b>A</p>B",
3622
+ true,
3623
+ serde_json::json!({"sourceCodeLocationInfo":true}),
3624
+ None,
3625
+ );
3626
+ document.ensure_arena().unwrap();
3627
+ let records = document.arena.borrow();
3628
+ let locations: Vec<_> = records
3629
+ .as_ref()
3630
+ .unwrap()
3631
+ .nodes
3632
+ .iter()
3633
+ .filter(|node| node.name.as_deref() == Some("b"))
3634
+ .map(|node| node.extras["sourceCodeLocation"].clone())
3635
+ .collect();
3636
+ assert_eq!(locations.len(), 2);
3637
+ assert_eq!(locations[0]["startOffset"], 3);
3638
+ assert_eq!(locations[0]["endOffset"], 7);
3639
+ assert_eq!(locations[1]["startOffset"], 3);
3640
+ assert_eq!(locations[1]["endOffset"], 12);
3641
+ let template = NativeDocument::parse_options(
3642
+ "<template><p>T</template>",
3643
+ true,
3644
+ serde_json::json!({"sourceCodeLocationInfo":true}),
3645
+ None,
3646
+ );
3647
+ template.ensure_arena().unwrap();
3648
+ let records = template.arena.borrow();
3649
+ let fragment = records
3650
+ .as_ref()
3651
+ .unwrap()
3652
+ .nodes
3653
+ .iter()
3654
+ .find(|node| node.kind == "root" && node.parent.is_some())
3655
+ .unwrap();
3656
+ assert_eq!(
3657
+ fragment.extras.get("sourceCodeLocation"),
3658
+ Some(&Value::Null)
3659
+ );
3660
+ }
3661
+
3662
+ #[test]
3663
+ fn fragment_root_and_html_serialization() {
3664
+ let document = NativeDocument::parse("hello &amp; <b z='x' a='y'>world</b>", false);
3665
+ assert_eq!(document.element_ids().len(), 1);
3666
+ assert_eq!(
3667
+ document.outer_html(vec![0]),
3668
+ "hello &amp; <b z=\"x\" a=\"y\">world</b>"
3669
+ );
3670
+ assert_eq!(document.text(vec![0]), "hello & world");
3671
+ let table_fragment = NativeDocument::parse("<td>A</td><td>B</td>", false);
3672
+ assert_eq!(table_fragment.outer_html(vec![0]), "<td>A</td><td>B</td>");
3673
+ }
3674
+
3675
+ #[test]
3676
+ fn raw_text_and_html5_recovery() {
3677
+ let document = NativeDocument::parse(
3678
+ "<title>A &amp; B</title><script>1 < 2 && 3 > 0</script><p>one<p>two",
3679
+ true,
3680
+ );
3681
+ assert_eq!(document.select_ids("p", None).unwrap().len(), 2);
3682
+ let script = document.select_ids("script", None).unwrap();
3683
+ assert_eq!(document.text_ids(&script), "1 < 2 && 3 > 0");
3684
+ assert_eq!(document.inner_html(script[0]), "1 < 2 && 3 > 0");
3685
+ }
3686
+
3687
+ #[test]
3688
+ fn selector_contexts_and_document_order() {
3689
+ let document = NativeDocument::parse(
3690
+ "<table>outside<tr><td>A</table><div><p class='x'>B</p><p>C</p></div>",
3691
+ false,
3692
+ );
3693
+ let all_paragraphs = document.select_ids("p", None).unwrap();
3694
+ assert_eq!(all_paragraphs.len(), 2);
3695
+ assert!(all_paragraphs.windows(2).all(|pair| pair[0] < pair[1]));
3696
+ let div = document.select_ids("div", None).unwrap();
3697
+ let paragraphs = document.select_ids("p", Some(&div)).unwrap();
3698
+ assert_eq!(paragraphs.len(), 2);
3699
+ assert!(paragraphs.windows(2).all(|pair| pair[0] < pair[1]));
3700
+ assert_eq!(document.select_ids("div", Some(&div)).unwrap().len(), 0);
3701
+ let nodes = document.select_ids("*", None).unwrap();
3702
+ assert!(nodes.windows(2).all(|pair| pair[0] < pair[1]));
3703
+ }
3704
+
3705
+ #[test]
3706
+ fn export_includes_template_fragments_and_doctype() {
3707
+ let document = NativeDocument::parse("<!DOCTYPE html><template><p>x</p></template>", true);
3708
+ let records: Value = serde_json::from_str(&document.export_tree().unwrap()).unwrap();
3709
+ assert_eq!(records[1]["x-name"], "html");
3710
+ let template = document.select_ids("template", None).unwrap()[0];
3711
+ let fragment = records[template as usize]["children"][0].as_u64().unwrap() as usize;
3712
+ assert_eq!(records[fragment]["type"], "root");
3713
+ assert_eq!(document.select_ids("p", None).unwrap().len(), 1);
3714
+ assert_eq!(
3715
+ document.select_ids("p", Some(&[template])).unwrap().len(),
3716
+ 0
3717
+ );
3718
+ }
3719
+
3720
+ #[test]
3721
+ fn native_documents_can_move_to_worker_threads() {
3722
+ fn assert_send<T: Send>() {}
3723
+ assert_send::<NativeDocument>();
3724
+ }
3725
+
3726
+ #[test]
3727
+ fn unicode_bom_and_mathml_integration_points() {
3728
+ for is_document in [true, false] {
3729
+ let document = NativeDocument::parse("\u{feff}<h1>UTF8</h1>", is_document);
3730
+ assert_eq!(document.text(vec![0]), "\u{feff}UTF8");
3731
+ let document = NativeDocument::parse("<math><annotation-xml encoding='text/html'><div>HTML</div></annotation-xml></math>", is_document);
3732
+ assert_eq!(
3733
+ document
3734
+ .select_ids("annotation-xml > div", None)
3735
+ .unwrap()
3736
+ .len(),
3737
+ 1
3738
+ );
3739
+ assert!(document.outer_html(vec![0]).contains(
3740
+ "<annotation-xml encoding=\"text/html\"><div>HTML</div></annotation-xml>"
3741
+ ));
3742
+ }
3743
+ }
3744
+
3745
+ #[test]
3746
+ fn foreign_attributes_match_the_cheerio_dom_adapter() {
3747
+ for (attrs, expected_html, expected_href) in [
3748
+ (
3749
+ "href='plain' xlink:href='linked'",
3750
+ "xlink:href=\"linked\"",
3751
+ "linked",
3752
+ ),
3753
+ (
3754
+ "xlink:href='linked' href='plain'",
3755
+ "href=\"plain\"",
3756
+ "plain",
3757
+ ),
3758
+ ] {
3759
+ let document = NativeDocument::parse(&format!("<svg><a {attrs}>x</a></svg>"), false);
3760
+ let ids = document.select_ids("[href]", None).unwrap();
3761
+ assert_eq!(ids.len(), 1);
3762
+ assert_eq!(
3763
+ document.attribute(ids[0], "href").as_deref(),
3764
+ Some(expected_href)
3765
+ );
3766
+ assert_eq!(
3767
+ document.outer_html(vec![0]),
3768
+ format!("<svg><a {expected_html}>x</a></svg>")
3769
+ );
3770
+ let mut task = ExtractManyTask {
3771
+ htmls: vec![format!("<svg><a {attrs}>x</a></svg>")],
3772
+ queries: vec![ExtractQuery {
3773
+ key: "href".into(),
3774
+ selector: "[href]".into(),
3775
+ attr: Some("href".into()),
3776
+ all: None,
3777
+ }],
3778
+ threads: 1,
3779
+ is_document: true,
3780
+ options: serde_json::json!({}),
3781
+ };
3782
+ let result: Value = serde_json::from_str(&task.compute().unwrap()).unwrap();
3783
+ assert_eq!(result[0]["href"], expected_href);
3784
+ }
3785
+ }
3786
+
3787
+ #[test]
3788
+ fn attribute_namespace_scanner_respects_css_syntax() {
3789
+ for (source, expected) in [
3790
+ ("a[href][lang|=en]", "a[*|href][*|lang|=en]"),
3791
+ (
3792
+ "a[*|href][xlink|href][|href]",
3793
+ "a[*|href][xlink|href][|href]",
3794
+ ),
3795
+ (
3796
+ "[title='[quoted]'][data-x=\"x\\\"[y]\"]",
3797
+ "[*|title='[quoted]'][*|data-x=\"x\\\"[y]\"]",
3798
+ ),
3799
+ ("#a\\[b[data\\|name]", "#a\\[b[*|data\\|name]"),
3800
+ ("[ href = 'x' ]", "[ *|href = 'x' ]"),
3801
+ ] {
3802
+ assert_eq!(normalize_attribute_namespaces(source), expected);
3803
+ }
3804
+ }
3805
+
3806
+ #[test]
3807
+ fn export_document_mode_omits_fragment_metadata() {
3808
+ for (source, mode) in [
3809
+ ("<p>x", "quirks"),
3810
+ ("<!doctype html><p>x", "no-quirks"),
3811
+ ("<!doctype html public '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><p>x", "limited-quirks"),
3812
+ ] {
3813
+ let document: Value = serde_json::from_str(&NativeDocument::parse(source, true).export_tree().unwrap()).unwrap();
3814
+ assert_eq!(document[0]["x-mode"], mode);
3815
+ let fragment: Value = serde_json::from_str(&NativeDocument::parse(source, false).export_tree().unwrap()).unwrap();
3816
+ assert!(fragment[0].get("x-mode").is_none());
3817
+ }
3818
+ }
3819
+
3820
+ #[test]
3821
+ fn native_mutations_preserve_ids_and_query_live_tree() {
3822
+ let document = NativeDocument::parse("<ul><li>A</li><li>B</li></ul>", false);
3823
+ let ul = document.select_ids("ul", None).unwrap()[0];
3824
+ let first = document.select_ids("li", None).unwrap()[0];
3825
+ document
3826
+ .op(
3827
+ "text".into(),
3828
+ serde_json::json!({"ids":[first],"value":"changed"}).to_string(),
3829
+ )
3830
+ .unwrap();
3831
+ assert_eq!(document.text_ids(&[first]), "changed");
3832
+ let added = document
3833
+ .create("<li>C</li>".into(), Some(ul), None)
3834
+ .unwrap();
3835
+ document
3836
+ .op(
3837
+ "insert".into(),
3838
+ serde_json::json!({"targetIds":[ul],"nodeIds":added,"position":"append"})
3839
+ .to_string(),
3840
+ )
3841
+ .unwrap();
3842
+ assert_eq!(document.select_ids("li", None).unwrap().len(), 3);
3843
+ assert_eq!(document.select_ids("li:first", None).unwrap(), vec![first]);
3844
+ assert_eq!(
3845
+ document.outer_html(vec![ul]),
3846
+ "<ul><li>changed</li><li>B</li><li>C</li></ul>"
3847
+ );
3848
+ document
3849
+ .op(
3850
+ "insert".into(),
3851
+ serde_json::json!({"targetIds":[first],"nodeIds":[first],"position":"replace"})
3852
+ .to_string(),
3853
+ )
3854
+ .unwrap();
3855
+ assert_eq!(document.select_ids("li", None).unwrap().len(), 3);
3856
+ let detached = document
3857
+ .create("<div>detached</div>".into(), None, None)
3858
+ .unwrap()[0];
3859
+ let sorted = document
3860
+ .op(
3861
+ "uniqueSort".into(),
3862
+ serde_json::json!({"ids":[detached,ul,detached]}).to_string(),
3863
+ )
3864
+ .unwrap();
3865
+ // As domutils does: the last of repeated ids is kept, and nodes of
3866
+ // separate trees keep their order.
3867
+ assert_eq!(
3868
+ serde_json::from_str::<Vec<u32>>(&sorted).unwrap(),
3869
+ vec![ul, detached]
3870
+ );
3871
+ }
3872
+
3873
+ #[test]
3874
+ fn batched_moves_preserve_sibling_order_and_last_duplicate_position() {
3875
+ let document = NativeDocument::parse(
3876
+ "<div><i id='a'></i><i id='b'></i><i id='c'></i></div>",
3877
+ false,
3878
+ );
3879
+ let parent = document.select_ids("div", None).unwrap()[0];
3880
+ let ids = document.select_ids("i", None).unwrap();
3881
+ let (a, b, c) = (ids[0], ids[1], ids[2]);
3882
+ document
3883
+ .op(
3884
+ "insert".into(),
3885
+ serde_json::json!({"targetIds":[b],"nodeIds":[c,a],"position":"before"})
3886
+ .to_string(),
3887
+ )
3888
+ .unwrap();
3889
+ assert_eq!(document.select_ids("i", None).unwrap(), vec![c, a, b]);
3890
+ document
3891
+ .op(
3892
+ "insert".into(),
3893
+ serde_json::json!({"targetIds":[parent],"nodeIds":[c,b,c],"position":"append"})
3894
+ .to_string(),
3895
+ )
3896
+ .unwrap();
3897
+ assert_eq!(document.select_ids("i", None).unwrap(), vec![a, b, c]);
3898
+ document
3899
+ .op(
3900
+ "remove".into(),
3901
+ serde_json::json!({"ids":[a,c]}).to_string(),
3902
+ )
3903
+ .unwrap();
3904
+ let node: Value = serde_json::from_str(&document.node(b).unwrap()).unwrap();
3905
+ assert!(node["prev"].is_null());
3906
+ assert!(node["next"].is_null());
3907
+ assert_eq!(document.select_ids("i", None).unwrap(), vec![b]);
3908
+ }
3909
+
3910
+ #[test]
3911
+ fn jquery_numeric_ids_recover_from_browser_css_parse_restrictions() {
3912
+ let document = NativeDocument::parse("<a id='1'>First</a><a id='2'>Second</a>", false);
3913
+ let selected = document.select_ids("#1", None).unwrap();
3914
+ assert_eq!(selected.len(), 1);
3915
+ assert_eq!(document.text_ids(&selected), "First");
3916
+ }
3917
+
3918
+ #[test]
3919
+ fn cloning_nodes_drops_api_data_caches() {
3920
+ let document = NativeDocument::parse("<p data-from-attribute='value'>x</p>", false);
3921
+ let id = document.select_ids("p", None).unwrap()[0];
3922
+ document
3923
+ .op(
3924
+ "data".into(),
3925
+ serde_json::json!({"ids":[id],"name":"cacheOnly","value":"cached"}).to_string(),
3926
+ )
3927
+ .unwrap();
3928
+ let clone: Vec<u32> = serde_json::from_str(
3929
+ &document
3930
+ .op("clone".into(), serde_json::json!({"ids":[id]}).to_string())
3931
+ .unwrap(),
3932
+ )
3933
+ .unwrap();
3934
+ let data: Value = serde_json::from_str(
3935
+ &document
3936
+ .op(
3937
+ "data".into(),
3938
+ serde_json::json!({"ids":clone,"name":"cacheOnly"}).to_string(),
3939
+ )
3940
+ .unwrap(),
3941
+ )
3942
+ .unwrap();
3943
+ assert_eq!(data["found"], false);
3944
+ }
3945
+
3946
+ #[test]
3947
+ fn adopting_branches_between_native_documents_detaches_the_source() {
3948
+ let target = NativeDocument::parse("<div></div>", false);
3949
+ let source = NativeDocument::parse("<p><b>moved</b></p><span>kept</span>", false);
3950
+ let id = source.select_ids("p", None).unwrap()[0];
3951
+ source
3952
+ .op(
3953
+ "data".into(),
3954
+ serde_json::json!({"ids":[id],"name":"cache","value":{"preserved":true}})
3955
+ .to_string(),
3956
+ )
3957
+ .unwrap();
3958
+ let adopted: Value =
3959
+ serde_json::from_str(&target.adopt(&source, vec![id], None).unwrap()).unwrap();
3960
+ let roots = arena::value_ids(&adopted["roots"]);
3961
+ let div = target.select_ids("div", None).unwrap()[0];
3962
+ target
3963
+ .op(
3964
+ "insert".into(),
3965
+ serde_json::json!({"targetIds":[div],"nodeIds":roots,"position":"append"})
3966
+ .to_string(),
3967
+ )
3968
+ .unwrap();
3969
+ assert_eq!(source.outer_html(vec![0]), "<span>kept</span>");
3970
+ assert_eq!(target.outer_html(vec![0]), "<div><p><b>moved</b></p></div>");
3971
+ assert_eq!(adopted["mapping"].as_array().unwrap().len(), 3);
3972
+ let data: Value = serde_json::from_str(
3973
+ &target
3974
+ .op(
3975
+ "data".into(),
3976
+ serde_json::json!({"ids":roots,"name":"cache"}).to_string(),
3977
+ )
3978
+ .unwrap(),
3979
+ )
3980
+ .unwrap();
3981
+ assert_eq!(data["value"]["preserved"], true);
3982
+ let source = NativeDocument::parse("<p><b>nested</b></p>", false);
3983
+ let ids = source.select_ids("p,b", None).unwrap();
3984
+ let adopted: Value =
3985
+ serde_json::from_str(&target.adopt(&source, ids, None).unwrap()).unwrap();
3986
+ let roots = arena::value_ids(&adopted["roots"]);
3987
+ assert_eq!(target.outer_html(roots), "<p></p><b>nested</b>");
3988
+ assert_eq!(adopted["mapping"].as_array().unwrap().len(), 3);
3989
+ let source = NativeDocument::parse("<em>copied</em>", false);
3990
+ let id = source.select_ids("em", None).unwrap()[0];
3991
+ target.adopt(&source, vec![id], Some(false)).unwrap();
3992
+ assert_eq!(source.outer_html(vec![0]), "<em>copied</em>");
3993
+ let xml = NativeDocument::parse_options(
3994
+ "<Item ID='case'>XML</Item>",
3995
+ true,
3996
+ serde_json::json!({"xml":true}),
3997
+ None,
3998
+ );
3999
+ let ids = xml.select_ids("Item", None).unwrap();
4000
+ let adopted: Value = serde_json::from_str(&target.adopt(&xml, ids, None).unwrap()).unwrap();
4001
+ let roots = arena::value_ids(&adopted["roots"]);
4002
+ let matched: Vec<u32> = serde_json::from_str(
4003
+ &target
4004
+ .op(
4005
+ "matches".into(),
4006
+ serde_json::json!({"ids":roots,"selector":"Item","options":{"xmlMode":true}})
4007
+ .to_string(),
4008
+ )
4009
+ .unwrap(),
4010
+ )
4011
+ .unwrap();
4012
+ assert_eq!(matched.len(), 1);
4013
+ }
4014
+
4015
+ #[test]
4016
+ fn per_operation_selection_options_restore_after_success_and_errors() {
4017
+ let document = NativeDocument::parse("<input checked><a href='/path'>link</a>", false);
4018
+ let input = document.select_ids("input", None).unwrap()[0];
4019
+ let xml = document
4020
+ .op(
4021
+ "attr".into(),
4022
+ serde_json::json!({"ids":[input],"name":"checked","options":{"xmlMode":true}})
4023
+ .to_string(),
4024
+ )
4025
+ .unwrap();
4026
+ assert_eq!(xml, "\"\"");
4027
+ assert!(document
4028
+ .op(
4029
+ "matches".into(),
4030
+ serde_json::json!({"ids":[input],"selector":":unknown","options":{"xmlMode":true}})
4031
+ .to_string()
4032
+ )
4033
+ .is_err());
4034
+ let html = document
4035
+ .op(
4036
+ "attr".into(),
4037
+ serde_json::json!({"ids":[input],"name":"checked"}).to_string(),
4038
+ )
4039
+ .unwrap();
4040
+ assert_eq!(html, "\"checked\"");
4041
+ }
4042
+
4043
+ #[test]
4044
+ fn native_xml_preserves_case_cdata_and_decoding_options() {
4045
+ let document = NativeDocument::parse_options(
4046
+ "<?xml version='1.0'?><Root><Item ID='x'>&amp;<![CDATA[a<b]]></Item><Empty/></Root>",
4047
+ true,
4048
+ serde_json::json!({"xml":true}),
4049
+ None,
4050
+ );
4051
+ let item = document.select_ids("Item", None).unwrap();
4052
+ assert_eq!(item.len(), 1);
4053
+ assert_eq!(document.attribute(item[0], "ID").as_deref(), Some("x"));
4054
+ assert_eq!(document.text_ids(&item), "&a<b");
4055
+ assert_eq!(document.select_ids("item", None).unwrap().len(), 0);
4056
+ assert!(document
4057
+ .outer_html(vec![0])
4058
+ .contains("<Item ID=\"x\">&amp;<![CDATA[a<b]]></Item><Empty/>"));
4059
+ let raw = NativeDocument::parse_options(
4060
+ "<tag>&amp;</tag>",
4061
+ true,
4062
+ serde_json::json!({"xml":{"decodeEntities":false}}),
4063
+ None,
4064
+ );
4065
+ assert_eq!(raw.text_ids(&[0]), "&amp;");
4066
+ assert_eq!(raw.outer_html(vec![0]), "<tag>&amp;</tag>");
4067
+ }
4068
+
4069
+ #[test]
4070
+ fn htmlparser_mode_preserves_raw_text_and_implied_closing_rules() {
4071
+ for (source, expected) in [
4072
+ (
4073
+ "<script>if(a < b) value = \"&amp;\";</script>",
4074
+ "<script>if(a < b) value = \"&amp;\";</script>",
4075
+ ),
4076
+ ("<p>A<p>B", "<p>A</p><p>B</p>"),
4077
+ (
4078
+ "<textarea>&lt;b&gt; &amp;</textarea>",
4079
+ "<textarea>&amp;lt;b&amp;gt; &amp;amp;</textarea>",
4080
+ ),
4081
+ ("<div/>A", "<div>A</div>"),
4082
+ (
4083
+ "<title>&lt;b&gt; &amp;</title>",
4084
+ "<title>&lt;b&gt; &amp;</title>",
4085
+ ),
4086
+ ("a < 2", "a &lt; 2"),
4087
+ ] {
4088
+ let document = NativeDocument::parse_options(
4089
+ source,
4090
+ true,
4091
+ serde_json::json!({"xml":{"xmlMode":false}}),
4092
+ None,
4093
+ );
4094
+ assert_eq!(document.outer_html(vec![0]), expected, "{source}");
4095
+ }
4096
+ let document = NativeDocument::parse_options(
4097
+ "<div/>A",
4098
+ true,
4099
+ serde_json::json!({"xml":{"xmlMode":false,"recognizeSelfClosing":true}}),
4100
+ None,
4101
+ );
4102
+ assert_eq!(document.outer_html(vec![0]), "<div></div>A");
4103
+ }
4104
+
4105
+ #[test]
4106
+ fn native_html_source_locations_use_utf16_offsets() {
4107
+ let source = "<div id='x'>😀<span>x</span></div>";
4108
+ let document = NativeDocument::parse_options(
4109
+ source,
4110
+ false,
4111
+ serde_json::json!({"sourceCodeLocationInfo":true}),
4112
+ None,
4113
+ );
4114
+ let span = document.select_ids("span", None).unwrap()[0];
4115
+ let node: Value = serde_json::from_str(&document.node(span).unwrap()).unwrap();
4116
+ assert_eq!(node["sourceCodeLocation"]["startOffset"], 14);
4117
+ assert_eq!(node["sourceCodeLocation"]["endOffset"], 28);
4118
+ assert_eq!(node["sourceCodeLocation"]["startTag"]["endOffset"], 20);
4119
+ }
4120
+ }