@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,672 @@
1
+ //! A compact copy of every node record that JavaScript decodes lazily. One call
2
+ //! replaces a native round trip per node when a document is read node by node.
3
+ //!
4
+ //! Each node occupies `STRIDE` entries of `nodes`:
5
+ //! 0 type, 1 parent + 1, 2 previous sibling + 1, 3 next sibling + 1,
6
+ //! 4 first entry in `children`, 5 child count, 6 name + 1, 7 data + 1,
7
+ //! 8 first pair in `attributes`, 9 attribute count, 10 flags, 11 start index,
8
+ //! 12 end index, 13 namespace, 14 remaining fields as JSON + 1.
9
+ //! String references index pairs of UTF-16 offsets into `text`.
10
+ //!
11
+ //! JavaScript receives one `Uint32Array`: the lengths of `nodes`, `children`,
12
+ //! `attributes` and `strings`, whether any string holds codec escapes, then
13
+ //! the four lists. String offsets count UTF-16 units of the encoded text, so
14
+ //! JavaScript decodes only the strings it reads. Its memory belongs to V8,
15
+ //! which frees it as soon as the snapshot is collected; an external buffer
16
+ //! would add finalizer bookkeeping and wait for a later event-loop turn.
17
+ use html5ever::{LocalName, Namespace};
18
+ use napi::{check_status, sys, Env, JsObject, JsTypedArray, NapiValue, Result};
19
+ use scraper::Node;
20
+ use serde_json::{Map, Value};
21
+
22
+ use crate::arena::DomNode;
23
+ use crate::id_hash::IdMap;
24
+ use crate::NativeDocument;
25
+
26
+ pub(crate) const STRIDE: usize = 15;
27
+ /// Text or attribute values longer than this stay out of snapshots: such
28
+ /// nodes, mostly inline scripts, styles, SVG paths and embedded data, are read
29
+ /// individually if JavaScript asks for them.
30
+ const LARGE_TEXT_BYTES: usize = 4096;
31
+ const ABSENT: u32 = u32::MAX;
32
+ const HAS_START_INDEX: u32 = 1;
33
+ const HAS_END_INDEX: u32 = 2;
34
+ const HAS_NAMESPACE: u32 = 4;
35
+ const EMPTY_ATTRIBUTE_MAPS: u32 = 8;
36
+ /// The record cannot be represented here; JavaScript reads it individually.
37
+ const OPAQUE: u32 = 16;
38
+ const RELEASED: u32 = 32;
39
+ const KINDS: [&str; 8] = [
40
+ "tag",
41
+ "text",
42
+ "comment",
43
+ "root",
44
+ "script",
45
+ "style",
46
+ "directive",
47
+ "cdata",
48
+ ];
49
+ const SHORT_NAME_SLOTS: usize = 256;
50
+
51
+ pub(crate) struct NodeSnapshot {
52
+ /// Consecutive ranges of nodes, encoded separately (in parallel for
53
+ /// large trees) with references local to each chunk.
54
+ chunks: Vec<Chunk>,
55
+ }
56
+
57
+ struct Chunk {
58
+ is_encoded: bool,
59
+ nodes: Vec<u32>,
60
+ children: Vec<u32>,
61
+ attributes: Vec<u32>,
62
+ strings: Vec<u32>,
63
+ text: String,
64
+ units: u32,
65
+ }
66
+
67
+ impl NodeSnapshot {
68
+ pub(crate) fn into_js(self, env: &Env) -> Result<JsObject> {
69
+ let total = |part: fn(&Chunk) -> usize| self.chunks.iter().map(part).sum::<usize>();
70
+ let lengths = [
71
+ total(|chunk| chunk.nodes.len()),
72
+ total(|chunk| chunk.children.len()),
73
+ total(|chunk| chunk.attributes.len()),
74
+ total(|chunk| chunk.strings.len()),
75
+ ];
76
+ let header = [
77
+ lengths[0] as u32,
78
+ lengths[1] as u32,
79
+ lengths[2] as u32,
80
+ lengths[3] as u32,
81
+ u32::from(self.chunks.iter().any(|chunk| chunk.is_encoded)),
82
+ ];
83
+ let length = header.len() + lengths.iter().sum::<usize>();
84
+ let data = uninitialized_words(env, length, |words| {
85
+ words[..header.len()].copy_from_slice(&header);
86
+ let (nodes, rest) = words[header.len()..].split_at_mut(lengths[0]);
87
+ let (children, rest) = rest.split_at_mut(lengths[1]);
88
+ let (attributes, strings) = rest.split_at_mut(lengths[2]);
89
+ let mut bases = Bases::default();
90
+ for chunk in &self.chunks {
91
+ bases.copy(chunk, nodes, children, attributes, strings);
92
+ }
93
+ })?;
94
+ let text = if self.chunks.len() == 1 {
95
+ crate::codec::js_string(env, &self.chunks[0].text)?
96
+ } else {
97
+ let mut text =
98
+ String::with_capacity(self.chunks.iter().map(|chunk| chunk.text.len()).sum());
99
+ for chunk in &self.chunks {
100
+ text.push_str(&chunk.text);
101
+ }
102
+ crate::codec::js_string(env, &text)?
103
+ };
104
+ let mut object = env.create_object()?;
105
+ object.set_named_property("data", data)?;
106
+ object.set_named_property("text", text)?;
107
+ Ok(object)
108
+ }
109
+ }
110
+
111
+ /// Where the next chunk's entries start in each combined list.
112
+ #[derive(Default)]
113
+ struct Bases {
114
+ nodes: usize,
115
+ children: usize,
116
+ attributes: usize,
117
+ strings: usize,
118
+ units: u32,
119
+ }
120
+
121
+ impl Bases {
122
+ /// Copies `chunk` after the chunks before it, shifting its references to
123
+ /// children, attribute pairs, strings and text by theirs.
124
+ fn copy(
125
+ &mut self,
126
+ chunk: &Chunk,
127
+ nodes: &mut [u32],
128
+ children: &mut [u32],
129
+ attributes: &mut [u32],
130
+ strings: &mut [u32],
131
+ ) {
132
+ let string_base = (self.strings / 2) as u32;
133
+ let child_base = self.children as u32;
134
+ let pair_base = (self.attributes / 2) as u32;
135
+ let target = &mut nodes[self.nodes..self.nodes + chunk.nodes.len()];
136
+ target.copy_from_slice(&chunk.nodes);
137
+ if self.nodes != 0 {
138
+ for fields in target.as_chunks_mut::<STRIDE>().0 {
139
+ let flags = fields[10];
140
+ if flags & RELEASED != 0 {
141
+ continue;
142
+ }
143
+ fields[0] += string_base;
144
+ fields[4] += child_base;
145
+ fields[8] += pair_base;
146
+ for field in [6, 7, 14] {
147
+ if fields[field] != 0 {
148
+ fields[field] += string_base;
149
+ }
150
+ }
151
+ if flags & HAS_NAMESPACE != 0 {
152
+ fields[13] += string_base;
153
+ }
154
+ }
155
+ }
156
+ children[self.children..self.children + chunk.children.len()]
157
+ .copy_from_slice(&chunk.children);
158
+ let target = &mut attributes[self.attributes..self.attributes + chunk.attributes.len()];
159
+ for (slot, reference) in target.iter_mut().zip(&chunk.attributes) {
160
+ *slot = reference + string_base;
161
+ }
162
+ let target = &mut strings[self.strings..self.strings + chunk.strings.len()];
163
+ for (slot, offset) in target.iter_mut().zip(&chunk.strings) {
164
+ *slot = offset + self.units;
165
+ }
166
+ self.nodes += chunk.nodes.len();
167
+ self.children += chunk.children.len();
168
+ self.attributes += chunk.attributes.len();
169
+ self.strings += chunk.strings.len();
170
+ self.units += chunk.units;
171
+ }
172
+ }
173
+
174
+ /// A `Uint32Array` of `length` words that `fill` writes. A Node buffer is
175
+ /// used for its storage because, unlike an `ArrayBuffer`, it is not
176
+ /// zero-filled first; `fill` writes every word before JavaScript can read it.
177
+ fn uninitialized_words(
178
+ env: &Env,
179
+ length: usize,
180
+ fill: impl FnOnce(&mut [u32]),
181
+ ) -> Result<JsTypedArray> {
182
+ let raw_env = env.raw();
183
+ let mut buffer = std::ptr::null_mut();
184
+ let mut memory = std::ptr::null_mut();
185
+ // SAFETY: Node-API calls on the current environment with valid out
186
+ // pointers. `memory` is the buffer's own allocation of `length` words,
187
+ // checked to be aligned, and the buffer stays alive in this handle scope.
188
+ unsafe {
189
+ check_status!(sys::napi_create_buffer(
190
+ raw_env,
191
+ length * 4,
192
+ &mut memory,
193
+ &mut buffer
194
+ ))?;
195
+ let mut kind = 0;
196
+ let mut bytes = 0;
197
+ let mut view_memory = std::ptr::null_mut();
198
+ let mut arraybuffer = std::ptr::null_mut();
199
+ let mut byte_offset = 0;
200
+ check_status!(sys::napi_get_typedarray_info(
201
+ raw_env,
202
+ buffer,
203
+ &mut kind,
204
+ &mut bytes,
205
+ &mut view_memory,
206
+ &mut arraybuffer,
207
+ &mut byte_offset,
208
+ ))?;
209
+ if !byte_offset.is_multiple_of(4)
210
+ || !(memory as usize).is_multiple_of(4)
211
+ || bytes < length * 4
212
+ {
213
+ return Err(napi::Error::from_reason("Unaligned snapshot buffer"));
214
+ }
215
+ let mut words = std::ptr::null_mut();
216
+ check_status!(sys::napi_create_typedarray(
217
+ raw_env,
218
+ sys::TypedarrayType::uint32_array,
219
+ length,
220
+ arraybuffer,
221
+ byte_offset,
222
+ &mut words,
223
+ ))?;
224
+ fill(std::slice::from_raw_parts_mut(memory.cast::<u32>(), length));
225
+ Ok(JsTypedArray::from_raw_unchecked(raw_env, words))
226
+ }
227
+ }
228
+
229
+ /// The UTF-16 length of `text`: one unit per character, two for characters
230
+ /// beyond the basic plane, counted from UTF-8 lead bytes.
231
+ fn utf16_length(text: &str) -> usize {
232
+ text.bytes()
233
+ .map(|byte| usize::from(byte & 0xC0 != 0x80) + usize::from(byte >= 0xF0))
234
+ .sum()
235
+ }
236
+
237
+ struct Builder {
238
+ nodes: Vec<u32>,
239
+ children: Vec<u32>,
240
+ attributes: Vec<u32>,
241
+ strings: Vec<u32>,
242
+ text: String,
243
+ units: u32,
244
+ interned: IdMap<String, u32>,
245
+ /// References of the common node types, resolved without hashing.
246
+ kinds: [Option<u32>; KINDS.len()],
247
+ /// Recently interned names of up to eight bytes, keyed by their bytes:
248
+ /// element and attribute names repeat across nodes.
249
+ short_names: Box<[(u64, u8, u32); SHORT_NAME_SLOTS]>,
250
+ /// html5ever names are atoms that hash and compare by identity.
251
+ local_names: IdMap<LocalName, u32>,
252
+ namespaces: IdMap<Namespace, u32>,
253
+ codec: bool,
254
+ is_encoded: bool,
255
+ }
256
+
257
+ /// Nodes per chunk worth encoding on another thread.
258
+ const PARALLEL_CHUNK_NODES: usize = 8192;
259
+
260
+ pub(crate) fn build(document: &NativeDocument) -> NodeSnapshot {
261
+ let codec = document.options.get("__utf16Codec") == Some(&Value::Bool(true))
262
+ || document.options.get("__losslessUnicode") == Some(&Value::Bool(true));
263
+ if let Some(arena) = document.arena.borrow().as_ref() {
264
+ let chunks =
265
+ crate::runtime::chunked_map(arena.nodes.len(), PARALLEL_CHUNK_NODES, |range| {
266
+ let mut builder = Builder::new(range.len(), 0, codec);
267
+ for id in range {
268
+ match arena.node(id as u32) {
269
+ Some(node) => builder.encode(node),
270
+ None => builder.released(),
271
+ }
272
+ }
273
+ vec![builder.finish()]
274
+ });
275
+ return NodeSnapshot { chunks };
276
+ }
277
+ let mut builder = Builder::new(
278
+ document.slot_count() as usize,
279
+ document.content_bytes.get(),
280
+ codec,
281
+ );
282
+ builder.encode_immutable(document);
283
+ NodeSnapshot {
284
+ chunks: vec![builder.finish()],
285
+ }
286
+ }
287
+
288
+ /// The JSON fields a document root's record carries, as `convert_node` sets them.
289
+ fn quirks_mode_json(mode: html5ever::tree_builder::QuirksMode) -> &'static str {
290
+ match mode {
291
+ html5ever::tree_builder::QuirksMode::Quirks => r#"{"x-mode":"quirks"}"#,
292
+ html5ever::tree_builder::QuirksMode::LimitedQuirks => r#"{"x-mode":"limited-quirks"}"#,
293
+ html5ever::tree_builder::QuirksMode::NoQuirks => r#"{"x-mode":"no-quirks"}"#,
294
+ }
295
+ }
296
+
297
+ /// The JSON fields a doctype's record carries, as `convert_node` sets them.
298
+ fn doctype_fields_json(doctype: &scraper::node::Doctype) -> String {
299
+ let quoted = |value: &str| serde_json::to_string(value).unwrap_or_else(|_| "\"\"".into());
300
+ format!(
301
+ r#"{{"x-name":{},"x-publicId":{},"x-systemId":{}}}"#,
302
+ quoted(doctype.name()),
303
+ quoted(doctype.public_id()),
304
+ quoted(doctype.system_id())
305
+ )
306
+ }
307
+
308
+ fn pointer(value: Option<u32>) -> u32 {
309
+ value.map_or(0, |id| id + 1)
310
+ }
311
+
312
+ impl Builder {
313
+ fn new(count: usize, text_bytes: usize, codec: bool) -> Self {
314
+ Self {
315
+ nodes: Vec::with_capacity(count * STRIDE),
316
+ children: Vec::with_capacity(count),
317
+ attributes: Vec::new(),
318
+ strings: Vec::with_capacity(count * 2),
319
+ text: String::with_capacity(text_bytes),
320
+ units: 0,
321
+ interned: IdMap::with_capacity_and_hasher(32, Default::default()),
322
+ kinds: [None; KINDS.len()],
323
+ short_names: Box::new([(0, 0, 0); SHORT_NAME_SLOTS]),
324
+ local_names: IdMap::with_capacity_and_hasher(32, Default::default()),
325
+ namespaces: IdMap::default(),
326
+ codec,
327
+ is_encoded: false,
328
+ }
329
+ }
330
+
331
+ fn finish(self) -> Chunk {
332
+ Chunk {
333
+ is_encoded: self.is_encoded,
334
+ nodes: self.nodes,
335
+ children: self.children,
336
+ attributes: self.attributes,
337
+ strings: self.strings,
338
+ text: self.text,
339
+ units: self.units,
340
+ }
341
+ }
342
+
343
+ fn string(&mut self, value: &str) -> u32 {
344
+ let start = self.units;
345
+ self.text.push_str(value);
346
+ let units = if value.is_ascii() {
347
+ value.len()
348
+ } else {
349
+ self.is_encoded |= self.codec && crate::codec::has_encoding(value);
350
+ utf16_length(value)
351
+ };
352
+ self.units += units as u32;
353
+ self.strings.push(start);
354
+ self.strings.push(self.units);
355
+ (self.strings.len() / 2 - 1) as u32
356
+ }
357
+
358
+ fn kind(&mut self, kind: &str) -> u32 {
359
+ let Some(slot) = KINDS.iter().position(|known| *known == kind) else {
360
+ return self.interned(kind);
361
+ };
362
+ if let Some(reference) = self.kinds[slot] {
363
+ return reference;
364
+ }
365
+ let reference = self.interned(kind);
366
+ self.kinds[slot] = Some(reference);
367
+ reference
368
+ }
369
+
370
+ fn interned_name(&mut self, name: &str) -> u32 {
371
+ let bytes = name.as_bytes();
372
+ if bytes.len() > 8 {
373
+ return self.interned(name);
374
+ }
375
+ let mut word = [0; 8];
376
+ word[..bytes.len()].copy_from_slice(bytes);
377
+ let key = u64::from_le_bytes(word);
378
+ let length = bytes.len() as u8;
379
+ let slot = (key
380
+ .wrapping_add(u64::from(length))
381
+ .wrapping_mul(0x9E37_79B9_7F4A_7C15)
382
+ >> 56) as usize;
383
+ let (cached_key, cached_length, cached) = self.short_names[slot];
384
+ if cached != 0 && cached_key == key && cached_length == length {
385
+ return cached - 1;
386
+ }
387
+ let reference = self.interned(name);
388
+ self.short_names[slot] = (key, length, reference + 1);
389
+ reference
390
+ }
391
+
392
+ fn interned(&mut self, value: &str) -> u32 {
393
+ if let Some(reference) = self.interned.get(value) {
394
+ return *reference;
395
+ }
396
+ let reference = self.string(value);
397
+ self.interned.insert(value.to_owned(), reference);
398
+ reference
399
+ }
400
+
401
+ fn local_name(&mut self, name: &LocalName) -> u32 {
402
+ if let Some(reference) = self.local_names.get(name) {
403
+ return *reference;
404
+ }
405
+ let reference = self.interned(name);
406
+ self.local_names.insert(name.clone(), reference);
407
+ reference
408
+ }
409
+
410
+ fn namespace(&mut self, namespace: &Namespace) -> u32 {
411
+ if let Some(reference) = self.namespaces.get(namespace) {
412
+ return *reference;
413
+ }
414
+ let reference = self.interned(namespace);
415
+ self.namespaces.insert(namespace.clone(), reference);
416
+ reference
417
+ }
418
+
419
+ fn released(&mut self) {
420
+ let mut fields = [0; STRIDE];
421
+ fields[10] = RELEASED;
422
+ self.nodes.extend_from_slice(&fields);
423
+ }
424
+
425
+ fn encode(&mut self, node: &DomNode) {
426
+ let mut fields = [0; STRIDE];
427
+ fields[0] = self.kind(&node.kind);
428
+ fields[1] = pointer(node.parent);
429
+ fields[2] = pointer(node.prev);
430
+ fields[3] = pointer(node.next);
431
+ fields[4] = self.children.len() as u32;
432
+ fields[5] = node.children.len() as u32;
433
+ self.children.extend_from_slice(&node.children);
434
+ fields[6] = node
435
+ .name
436
+ .as_deref()
437
+ .map_or(0, |name| self.interned_name(name) + 1);
438
+ let mut flags = 0;
439
+ match node.data.as_deref() {
440
+ Some(data) if data.len() > LARGE_TEXT_BYTES => flags |= OPAQUE,
441
+ Some(data) => fields[7] = self.string(data) + 1,
442
+ None => {}
443
+ }
444
+ fields[8] = (self.attributes.len() / 2) as u32;
445
+ let has_large_attribute = node.attribs.values().any(|value| {
446
+ value
447
+ .as_str()
448
+ .is_some_and(|value| value.len() > LARGE_TEXT_BYTES)
449
+ });
450
+ if has_large_attribute {
451
+ flags |= OPAQUE;
452
+ } else {
453
+ fields[9] = node.attribs.len() as u32;
454
+ }
455
+ for (name, value) in node.attribs.iter().filter(|_| !has_large_attribute) {
456
+ let Some(value) = value.as_str() else {
457
+ flags |= OPAQUE;
458
+ break;
459
+ };
460
+ let name = self.interned_name(name);
461
+ let value = self.string(value);
462
+ self.attributes.extend_from_slice(&[name, value]);
463
+ }
464
+ fields[11] = ABSENT;
465
+ fields[12] = ABSENT;
466
+ let mut rest = Map::new();
467
+ if let Some(namespace) = &node.namespace {
468
+ flags |= HAS_NAMESPACE;
469
+ fields[13] = self.interned(namespace);
470
+ }
471
+ match (&node.attribs_namespace, &node.attribs_prefix) {
472
+ (Some(namespaces), Some(prefixes)) if namespaces.is_empty() && prefixes.is_empty() => {
473
+ flags |= EMPTY_ATTRIBUTE_MAPS;
474
+ }
475
+ (namespaces, prefixes) => {
476
+ if let Some(namespaces) = namespaces {
477
+ rest.insert(
478
+ "attribsNamespace".into(),
479
+ Value::Object((**namespaces).clone()),
480
+ );
481
+ }
482
+ if let Some(prefixes) = prefixes {
483
+ rest.insert("attribsPrefix".into(), Value::Object((**prefixes).clone()));
484
+ }
485
+ }
486
+ }
487
+ for (key, value) in &node.extras {
488
+ let index = value.as_u64().filter(|index| *index < u64::from(ABSENT));
489
+ match (key.as_str(), value) {
490
+ ("startIndex", Value::Null) => flags |= HAS_START_INDEX,
491
+ ("startIndex", _) if index.is_some() => {
492
+ flags |= HAS_START_INDEX;
493
+ fields[11] = index.unwrap() as u32;
494
+ }
495
+ ("endIndex", Value::Null) => flags |= HAS_END_INDEX,
496
+ ("endIndex", _) if index.is_some() => {
497
+ flags |= HAS_END_INDEX;
498
+ fields[12] = index.unwrap() as u32;
499
+ }
500
+ _ => {
501
+ rest.insert(key.clone(), value.clone());
502
+ }
503
+ }
504
+ }
505
+ if !rest.is_empty() {
506
+ match serde_json::to_string(&rest) {
507
+ Ok(json) => fields[14] = self.interned(&json) + 1,
508
+ Err(_) => flags |= OPAQUE,
509
+ }
510
+ }
511
+ fields[10] = flags;
512
+ self.nodes.extend_from_slice(&fields);
513
+ }
514
+
515
+ /// Mirrors `NativeDocument::immutable_dom_node` without building records
516
+ /// for elements, text and comments; other nodes use that record directly.
517
+ fn encode_immutable(&mut self, document: &NativeDocument) {
518
+ let html = document.html.borrow();
519
+ let ids = document.ids.borrow();
520
+ let reverse = document.reverse.borrow();
521
+ let Some(html) = html.as_ref() else {
522
+ return;
523
+ };
524
+ let local = |node: Option<ego_tree::NodeRef<'_, Node>>| {
525
+ node.and_then(|node| reverse.get(&node.id()).copied())
526
+ };
527
+ let text_type = self.interned("text");
528
+ let comment_type = self.interned("comment");
529
+ let tag_type = self.interned("tag");
530
+ let script_type = self.interned("script");
531
+ let style_type = self.interned("style");
532
+ for (index, node_id) in ids.iter().enumerate() {
533
+ let Some(node) = html.tree.get(*node_id) else {
534
+ self.released();
535
+ continue;
536
+ };
537
+ let is_plain_element = matches!(node.value(), Node::Element(element)
538
+ if element.name.prefix.is_none()
539
+ && element.attrs.iter().all(|(name, _)| name.ns.is_empty() && name.prefix.is_none()));
540
+ let is_direct = is_plain_element
541
+ || matches!(
542
+ node.value(),
543
+ Node::Text(_)
544
+ | Node::Comment(_)
545
+ | Node::Document
546
+ | Node::Fragment
547
+ | Node::Doctype(_)
548
+ );
549
+ if !is_direct {
550
+ match document.immutable_dom_node(index as u32) {
551
+ Some(record) => self.encode(&record),
552
+ None => self.released(),
553
+ }
554
+ continue;
555
+ }
556
+ let mut fields = [0; STRIDE];
557
+ fields[1] = pointer(local(node.parent()));
558
+ fields[2] = pointer(local(node.prev_sibling()));
559
+ fields[3] = pointer(local(node.next_sibling()));
560
+ fields[4] = self.children.len() as u32;
561
+ let first_child = self.children.len();
562
+ self.children.extend(
563
+ node.children()
564
+ .filter_map(|child| reverse.get(&child.id()).copied()),
565
+ );
566
+ fields[5] = (self.children.len() - first_child) as u32;
567
+ fields[8] = (self.attributes.len() / 2) as u32;
568
+ fields[11] = ABSENT;
569
+ fields[12] = ABSENT;
570
+ let mut flags = HAS_START_INDEX | HAS_END_INDEX;
571
+ match node.value() {
572
+ Node::Document => {
573
+ fields[0] = self.interned("root");
574
+ fields[14] = self.interned(quirks_mode_json(html.quirks_mode)) + 1;
575
+ flags = 0;
576
+ }
577
+ Node::Fragment => {
578
+ fields[0] = self.interned("root");
579
+ flags = 0;
580
+ }
581
+ Node::Doctype(doctype) => {
582
+ fields[0] = self.interned("directive");
583
+ fields[6] = self.interned("!doctype") + 1;
584
+ fields[7] = self.string(&crate::doctype_content(doctype)) + 1;
585
+ fields[14] = self.interned(&doctype_fields_json(doctype)) + 1;
586
+ flags = 0;
587
+ }
588
+ Node::Text(text) => {
589
+ fields[0] = text_type;
590
+ if text.len() > LARGE_TEXT_BYTES {
591
+ flags |= OPAQUE;
592
+ } else {
593
+ fields[7] = self.string(text) + 1;
594
+ }
595
+ }
596
+ Node::Comment(comment) => {
597
+ fields[0] = comment_type;
598
+ if comment.len() > LARGE_TEXT_BYTES {
599
+ flags |= OPAQUE;
600
+ } else {
601
+ fields[7] = self.string(comment) + 1;
602
+ }
603
+ }
604
+ Node::Element(element) => {
605
+ fields[0] = match element.name() {
606
+ "script" => script_type,
607
+ "style" => style_type,
608
+ _ => tag_type,
609
+ };
610
+ fields[6] = self.local_name(&element.name.local) + 1;
611
+ if element
612
+ .attrs
613
+ .iter()
614
+ .any(|(_, value)| value.len() > LARGE_TEXT_BYTES)
615
+ {
616
+ flags |= OPAQUE;
617
+ } else {
618
+ for (attribute, value) in &element.attrs {
619
+ let attribute = self.local_name(&attribute.local);
620
+ let value = self.string(value);
621
+ self.attributes.extend_from_slice(&[attribute, value]);
622
+ }
623
+ fields[9] = element.attrs.len() as u32;
624
+ }
625
+ fields[13] = self.namespace(&element.name.ns);
626
+ flags |= HAS_NAMESPACE | EMPTY_ATTRIBUTE_MAPS;
627
+ }
628
+ _ => unreachable!("other node types use the record path"),
629
+ }
630
+ fields[10] = flags;
631
+ self.nodes.extend_from_slice(&fields);
632
+ }
633
+ }
634
+ }
635
+
636
+ #[cfg(test)]
637
+ mod tests {
638
+ use super::*;
639
+ use crate::codec::{latin1_bytes, utf16_units};
640
+
641
+ #[test]
642
+ fn transcodes_text_like_the_standard_library() {
643
+ for text in [
644
+ "",
645
+ "ascii only",
646
+ "café ©",
647
+ "번개장터 “quoted” — dash",
648
+ "emoji 😀 and \u{e800}",
649
+ "\u{ff}\u{100}",
650
+ "a\u{10ffff}b",
651
+ ] {
652
+ let expected: Vec<u16> = text.encode_utf16().collect();
653
+ assert_eq!(utf16_units(text), expected, "{text}");
654
+ assert_eq!(utf16_length(text), expected.len(), "{text}");
655
+ let latin1 = latin1_bytes(text);
656
+ assert_eq!(
657
+ latin1.is_some(),
658
+ text.chars().all(|character| (character as u32) < 0x100),
659
+ "{text}"
660
+ );
661
+ if let Some(bytes) = latin1 {
662
+ assert_eq!(
663
+ bytes
664
+ .iter()
665
+ .map(|byte| u16::from(*byte))
666
+ .collect::<Vec<_>>(),
667
+ expected
668
+ );
669
+ }
670
+ }
671
+ }
672
+ }