@dvaji/cheerox 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/CONTRIBUTING.md +74 -0
  2. package/Cargo.lock +1094 -0
  3. package/Cargo.toml +46 -0
  4. package/LICENSE +21 -0
  5. package/README.md +121 -2
  6. package/RELEASING.md +114 -0
  7. package/THIRD_PARTY_NOTICES.md +7579 -0
  8. package/build.rs +3 -0
  9. package/cheerox.darwin-arm64.node +0 -0
  10. package/cheerox.darwin-x64.node +0 -0
  11. package/cheerox.linux-arm64-gnu.node +0 -0
  12. package/cheerox.linux-arm64-musl.node +0 -0
  13. package/cheerox.linux-x64-gnu.node +0 -0
  14. package/cheerox.linux-x64-musl.node +0 -0
  15. package/cheerox.win32-arm64-msvc.node +0 -0
  16. package/cheerox.win32-x64-msvc.node +0 -0
  17. package/dist/cheerio-compat-BS--qPzw.mjs +1648 -0
  18. package/dist/cheerio-compat-BS--qPzw.mjs.map +1 -0
  19. package/dist/cheerio-compat-DRLPzBdX.d.mts +88 -0
  20. package/dist/cheerio-compat-DRsINTwV.d.cts +88 -0
  21. package/dist/cheerio-compat-DRsINTwV.d.ts +88 -0
  22. package/dist/cheerio-compat-DSwBAJ1s.cjs +1719 -0
  23. package/dist/cheerio-compat-DSwBAJ1s.cjs.map +1 -0
  24. package/dist/cheerio-compat.cjs +18 -0
  25. package/dist/cheerio-compat.d.cts +2 -0
  26. package/dist/cheerio-compat.d.mts +2 -0
  27. package/dist/cheerio-compat.d.ts +2 -0
  28. package/dist/cheerio-compat.mjs +1 -0
  29. package/dist/index.cjs +13 -0
  30. package/dist/index.cjs.map +1 -0
  31. package/dist/index.d.cts +28 -0
  32. package/dist/index.d.mts +6 -0
  33. package/dist/index.d.ts +28 -0
  34. package/dist/index.mjs +53 -0
  35. package/dist/index.mjs.map +1 -0
  36. package/dist/legacy-parse.cjs +5 -0
  37. package/dist/legacy-parse.d.mts +8 -0
  38. package/dist/legacy-parse.d.ts +7 -0
  39. package/dist/legacy-parse.mjs +1 -0
  40. package/dist/modern-B7hyW4cI.d.cts +102 -0
  41. package/dist/modern-B7hyW4cI.d.ts +102 -0
  42. package/dist/modern-CHQGP0Yt.cjs +2858 -0
  43. package/dist/modern-CHQGP0Yt.cjs.map +1 -0
  44. package/dist/modern-CJvSD_y5.d.mts +102 -0
  45. package/dist/modern-CvXcTn82.mjs +2769 -0
  46. package/dist/modern-CvXcTn82.mjs.map +1 -0
  47. package/dist/modern.cjs +19 -0
  48. package/dist/modern.d.cts +3 -0
  49. package/dist/modern.d.mts +3 -0
  50. package/dist/modern.d.ts +3 -0
  51. package/dist/modern.mjs +3 -0
  52. package/dist/nodes-jbWWhssj.cjs +4210 -0
  53. package/dist/nodes-jbWWhssj.cjs.map +1 -0
  54. package/dist/nodes-sYxb5qyZ.mjs +3803 -0
  55. package/dist/nodes-sYxb5qyZ.mjs.map +1 -0
  56. package/dist/slim.cjs +16 -0
  57. package/dist/slim.cjs.map +1 -0
  58. package/dist/slim.d.cts +6 -0
  59. package/dist/slim.d.mts +6 -0
  60. package/dist/slim.d.ts +6 -0
  61. package/dist/slim.mjs +12 -0
  62. package/dist/slim.mjs.map +1 -0
  63. package/dist/types-CA01Zg3r.d.cts +526 -0
  64. package/dist/types-CA01Zg3r.d.mts +526 -0
  65. package/dist/types-CA01Zg3r.d.ts +526 -0
  66. package/dist/utils.cjs +31 -0
  67. package/dist/utils.cjs.map +1 -0
  68. package/dist/utils.d.cts +8 -0
  69. package/dist/utils.d.mts +8 -0
  70. package/dist/utils.d.ts +8 -0
  71. package/dist/utils.mjs +25 -0
  72. package/dist/utils.mjs.map +1 -0
  73. package/native/src/arena.rs +2109 -0
  74. package/native/src/attributes.rs +946 -0
  75. package/native/src/codec.rs +298 -0
  76. package/native/src/diagnostics.rs +1249 -0
  77. package/native/src/document_order.rs +711 -0
  78. package/native/src/extraction.rs +321 -0
  79. package/native/src/gc.rs +175 -0
  80. package/native/src/hook_sink.rs +801 -0
  81. package/native/src/hook_stream.rs +513 -0
  82. package/native/src/id_hash.rs +42 -0
  83. package/native/src/immutable.rs +333 -0
  84. package/native/src/lib.rs +4119 -0
  85. package/native/src/location.rs +392 -0
  86. package/native/src/markup_tokenizer.rs +668 -0
  87. package/native/src/runtime.rs +368 -0
  88. package/native/src/selector.rs +1596 -0
  89. package/native/src/sink.rs +274 -0
  90. package/native/src/snapshot.rs +672 -0
  91. package/native/src/stream.rs +334 -0
  92. package/native/src/tokenizer.rs +581 -0
  93. package/native/src/tracker.rs +232 -0
  94. package/native/vendor/README.md +26 -0
  95. package/native/vendor/html5ever-0.35.0/.cargo_vcs_info.json +6 -0
  96. package/native/vendor/html5ever-0.35.0/Cargo.lock +828 -0
  97. package/native/vendor/html5ever-0.35.0/Cargo.toml +85 -0
  98. package/native/vendor/html5ever-0.35.0/LICENSE-APACHE +201 -0
  99. package/native/vendor/html5ever-0.35.0/LICENSE-MIT +25 -0
  100. package/native/vendor/html5ever-0.35.0/README.md +60 -0
  101. package/native/vendor/html5ever-0.35.0/benches/html5ever.rs +86 -0
  102. package/native/vendor/html5ever-0.35.0/data/bench/lipsum-zh.html +19 -0
  103. package/native/vendor/html5ever-0.35.0/data/bench/lipsum.html +40 -0
  104. package/native/vendor/html5ever-0.35.0/data/bench/medium-fragment.html +24 -0
  105. package/native/vendor/html5ever-0.35.0/data/bench/small-fragment.html +7 -0
  106. package/native/vendor/html5ever-0.35.0/data/bench/strong.html +1 -0
  107. package/native/vendor/html5ever-0.35.0/data/bench/tiny-fragment.html +1 -0
  108. package/native/vendor/html5ever-0.35.0/examples/arena.rs +356 -0
  109. package/native/vendor/html5ever-0.35.0/examples/capi/tokenize.c +74 -0
  110. package/native/vendor/html5ever-0.35.0/examples/noop-tokenize.rs +47 -0
  111. package/native/vendor/html5ever-0.35.0/examples/noop-tree-builder.rs +132 -0
  112. package/native/vendor/html5ever-0.35.0/examples/print-tree-actions.rs +185 -0
  113. package/native/vendor/html5ever-0.35.0/examples/tokenize.rs +111 -0
  114. package/native/vendor/html5ever-0.35.0/src/driver.rs +148 -0
  115. package/native/vendor/html5ever-0.35.0/src/lib.rs +30 -0
  116. package/native/vendor/html5ever-0.35.0/src/macros.rs +34 -0
  117. package/native/vendor/html5ever-0.35.0/src/serialize/mod.rs +255 -0
  118. package/native/vendor/html5ever-0.35.0/src/tokenizer/char_ref/mod.rs +530 -0
  119. package/native/vendor/html5ever-0.35.0/src/tokenizer/interface.rs +99 -0
  120. package/native/vendor/html5ever-0.35.0/src/tokenizer/mod.rs +2628 -0
  121. package/native/vendor/html5ever-0.35.0/src/tokenizer/states.rs +97 -0
  122. package/native/vendor/html5ever-0.35.0/src/tree_builder/data.rs +170 -0
  123. package/native/vendor/html5ever-0.35.0/src/tree_builder/mod.rs +1848 -0
  124. package/native/vendor/html5ever-0.35.0/src/tree_builder/rules.rs +1494 -0
  125. package/native/vendor/html5ever-0.35.0/src/tree_builder/tag_sets.rs +114 -0
  126. package/native/vendor/html5ever-0.35.0/src/tree_builder/types.rs +91 -0
  127. package/native/vendor/html5ever-0.35.0/src/util/str.rs +52 -0
  128. package/native/vendor/html5gum-0.8.4/Cargo.lock +1006 -0
  129. package/native/vendor/html5gum-0.8.4/Cargo.toml +109 -0
  130. package/native/vendor/html5gum-0.8.4/LICENSE +19 -0
  131. package/native/vendor/html5gum-0.8.4/README.md +110 -0
  132. package/native/vendor/html5gum-0.8.4/benches/files/0050.html +2131 -0
  133. package/native/vendor/html5gum-0.8.4/benches/files/6D30.html +1004 -0
  134. package/native/vendor/html5gum-0.8.4/benches/files.rs +32 -0
  135. package/native/vendor/html5gum-0.8.4/benches/patterns.rs +31 -0
  136. package/native/vendor/html5gum-0.8.4/src/arrayvec.rs +29 -0
  137. package/native/vendor/html5gum-0.8.4/src/char_validator.rs +134 -0
  138. package/native/vendor/html5gum-0.8.4/src/emitters/callback.rs +676 -0
  139. package/native/vendor/html5gum-0.8.4/src/emitters/default.rs +207 -0
  140. package/native/vendor/html5gum-0.8.4/src/emitters/emitter.rs +344 -0
  141. package/native/vendor/html5gum-0.8.4/src/emitters/html5ever.rs +306 -0
  142. package/native/vendor/html5gum-0.8.4/src/emitters/mod.rs +25 -0
  143. package/native/vendor/html5gum-0.8.4/src/entities.rs +3342 -0
  144. package/native/vendor/html5gum-0.8.4/src/error.rs +96 -0
  145. package/native/vendor/html5gum-0.8.4/src/htmlstring.rs +130 -0
  146. package/native/vendor/html5gum-0.8.4/src/lib.rs +66 -0
  147. package/native/vendor/html5gum-0.8.4/src/machine.rs +2136 -0
  148. package/native/vendor/html5gum-0.8.4/src/machine_helper.rs +277 -0
  149. package/native/vendor/html5gum-0.8.4/src/read_helper.rs +244 -0
  150. package/native/vendor/html5gum-0.8.4/src/reader.rs +440 -0
  151. package/native/vendor/html5gum-0.8.4/src/span.rs +98 -0
  152. package/native/vendor/html5gum-0.8.4/src/state.rs +16 -0
  153. package/native/vendor/html5gum-0.8.4/src/testutils.rs +71 -0
  154. package/native/vendor/html5gum-0.8.4/src/tokenizer.rs +189 -0
  155. package/native/vendor/html5gum-0.8.4/src/utils.rs +88 -0
  156. package/native/vendor/markup5ever-0.35.0/.cargo_vcs_info.json +6 -0
  157. package/native/vendor/markup5ever-0.35.0/Cargo.toml +45 -0
  158. package/native/vendor/markup5ever-0.35.0/LICENSE-APACHE +201 -0
  159. package/native/vendor/markup5ever-0.35.0/LICENSE-MIT +25 -0
  160. package/native/vendor/markup5ever-0.35.0/interface/mod.rs +422 -0
  161. package/native/vendor/markup5ever-0.35.0/interface/tree_builder.rs +280 -0
  162. package/native/vendor/markup5ever-0.35.0/lib.rs +59 -0
  163. package/native/vendor/markup5ever-0.35.0/serialize.rs +77 -0
  164. package/native/vendor/markup5ever-0.35.0/util/buffer_queue.rs +333 -0
  165. package/native/vendor/markup5ever-0.35.0/util/smallcharset.rs +88 -0
  166. package/package.json +167 -4
  167. package/scripts/check-native.mjs +15 -0
  168. package/scripts/check-release.mjs +234 -0
  169. package/scripts/fix-declarations.mjs +39 -0
  170. package/scripts/generate-notices.mjs +188 -0
  171. package/scripts/notices/README.md +13 -0
  172. package/scripts/notices/cheerio-MIT.txt +21 -0
  173. package/scripts/notices/html5ever-AUTHORS.txt +20 -0
  174. package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
  175. package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
  176. package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
  177. package/scripts/notices/htmlparser2-MIT.txt +18 -0
  178. package/scripts/notices/napi-rs-MIT.txt +43 -0
  179. package/scripts/notices/neon-MIT.txt +19 -0
  180. package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
  181. package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
  182. package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
  183. package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
  184. package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
  185. package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
  186. package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
  187. package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
  188. package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
  189. package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
  190. package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
  191. package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
  192. package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
  193. package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
  194. package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
  195. package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
  196. package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
  197. package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
  198. package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
  199. package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
  200. package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
  201. package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
  202. package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
  203. package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
  204. package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
  205. package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
  206. package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
  207. package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
  208. package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
  209. package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
  210. package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
  211. package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
  212. package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
  213. package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
  214. package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
  215. package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
  216. package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
  217. package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
  218. package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
  219. package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
  220. package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
  221. package/scripts/notices/runtime.json +1632 -0
  222. package/scripts/notices/sources.json +561 -0
  223. package/scripts/notices/xray-MIT.txt +21 -0
  224. package/src/attribute-object.ts +71 -0
  225. package/src/attributes.ts +308 -0
  226. package/src/cheerio-compat/css.ts +58 -0
  227. package/src/cheerio-compat/render.ts +106 -0
  228. package/src/cheerio-compat/selectors.ts +51 -0
  229. package/src/cheerio-compat/types.ts +52 -0
  230. package/src/cheerio-compat.ts +2055 -0
  231. package/src/codec.ts +96 -0
  232. package/src/dom.ts +87 -0
  233. package/src/extract-plan.ts +97 -0
  234. package/src/extract.ts +322 -0
  235. package/src/hooks.ts +369 -0
  236. package/src/index.ts +48 -0
  237. package/src/io.ts +491 -0
  238. package/src/match.ts +207 -0
  239. package/src/modern.ts +259 -0
  240. package/src/native.ts +149 -0
  241. package/src/nodes.ts +831 -0
  242. package/src/parse.ts +53 -0
  243. package/src/parser-types.ts +157 -0
  244. package/src/router.ts +1129 -0
  245. package/src/selection.ts +682 -0
  246. package/src/slim.ts +12 -0
  247. package/src/snapshot.ts +291 -0
  248. package/src/transport.ts +113 -0
  249. package/src/types.ts +186 -0
  250. package/src/utils.ts +29 -0
  251. package/src/workers.ts +21 -0
  252. package/tsconfig.json +15 -0
  253. package/tsdown.config.mjs +17 -0
@@ -0,0 +1,274 @@
1
+ //! Fill the HTML5 integration-point hook omitted by scraper's tree sink.
2
+
3
+ use std::borrow::Cow;
4
+ use std::cell::{Ref, RefCell};
5
+ use std::collections::{HashMap, HashSet};
6
+ use std::rc::Rc;
7
+
8
+ use ego_tree::NodeId;
9
+ use html5ever::tendril::StrTendril;
10
+ use html5ever::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
11
+ use html5ever::{Attribute, QualName};
12
+ use scraper::{Html, HtmlTreeSink};
13
+
14
+ pub(crate) struct CompatibleSink(
15
+ HtmlTreeSink,
16
+ Option<RefCell<Vec<Cow<'static, str>>>>,
17
+ Option<Rc<crate::tracker::Locations<NodeId>>>,
18
+ );
19
+
20
+ impl CompatibleSink {
21
+ pub(crate) fn new(html: Html) -> Self {
22
+ Self(HtmlTreeSink::new(html), None, None)
23
+ }
24
+ pub(crate) fn with_errors(html: Html) -> Self {
25
+ Self(
26
+ HtmlTreeSink::new(html),
27
+ Some(RefCell::new(Vec::new())),
28
+ None,
29
+ )
30
+ }
31
+ pub(crate) fn with_locations(
32
+ html: Html,
33
+ locations: Rc<crate::tracker::Locations<NodeId>>,
34
+ ) -> Self {
35
+ Self(HtmlTreeSink::new(html), None, Some(locations))
36
+ }
37
+ fn element(&self, name: QualName, attrs: Vec<Attribute>, flags: ElementFlags) -> NodeId {
38
+ let local = self.2.as_ref().map(|_| name.local.to_string());
39
+ let id = self.0.create_element(name, attrs, flags);
40
+ if let (Some(locations), Some(local)) = (&self.2, local) {
41
+ locations.created(id, &local);
42
+ }
43
+ id
44
+ }
45
+ pub(crate) fn take_errors(&self) -> Vec<Cow<'static, str>> {
46
+ self.1
47
+ .as_ref()
48
+ .map(|errors| std::mem::take(&mut *errors.borrow_mut()))
49
+ .unwrap_or_default()
50
+ }
51
+ }
52
+
53
+ impl TreeSink for CompatibleSink {
54
+ type Handle = NodeId;
55
+ type Output = Html;
56
+ type ElemName<'a> = Ref<'a, QualName>;
57
+
58
+ fn finish(self) -> Html {
59
+ self.0.finish()
60
+ }
61
+
62
+ fn parse_error(&self, message: Cow<'static, str>) {
63
+ if let Some(errors) = &self.1 {
64
+ errors.borrow_mut().push(message.clone());
65
+ }
66
+ self.0.parse_error(message);
67
+ }
68
+
69
+ fn get_document(&self) -> NodeId {
70
+ self.0.get_document()
71
+ }
72
+
73
+ fn elem_name<'a>(&'a self, target: &'a NodeId) -> Ref<'a, QualName> {
74
+ self.0.elem_name(target)
75
+ }
76
+
77
+ fn create_element(&self, name: QualName, attrs: Vec<Attribute>, flags: ElementFlags) -> NodeId {
78
+ if attrs.len() < 2 || name.ns.as_ref() == super::HTML_NS {
79
+ return self.element(name, attrs, flags);
80
+ }
81
+ // parse5's htmlparser2 adapter keys attributes by local name. Keep the
82
+ // first insertion position and the last value/namespace when foreign
83
+ // attributes such as href and xlink:href have the same local name.
84
+ let mut positions = HashMap::new();
85
+ let mut collapsed = Vec::with_capacity(attrs.len());
86
+ for attr in attrs {
87
+ if let Some(position) = positions.get(&attr.name.local) {
88
+ collapsed[*position] = attr;
89
+ } else {
90
+ positions.insert(attr.name.local.clone(), collapsed.len());
91
+ collapsed.push(attr);
92
+ }
93
+ }
94
+ self.element(name, collapsed, flags)
95
+ }
96
+
97
+ fn create_comment(&self, text: StrTendril) -> NodeId {
98
+ let id = self.0.create_comment(text);
99
+ if let Some(locations) = &self.2 {
100
+ locations.token(id, "comment");
101
+ }
102
+ id
103
+ }
104
+
105
+ fn create_pi(&self, target: StrTendril, data: StrTendril) -> NodeId {
106
+ self.0.create_pi(target, data)
107
+ }
108
+
109
+ fn append(&self, parent: &NodeId, child: NodeOrText<NodeId>) {
110
+ let text = self.2.as_ref().and_then(|_| match &child {
111
+ NodeOrText::AppendText(text) => Some(text.to_string()),
112
+ _ => None,
113
+ });
114
+ self.0.append(parent, child);
115
+ if let (Some(locations), Some(text)) = (&self.2, text) {
116
+ if let Some(id) = self
117
+ .0
118
+ .0
119
+ .borrow()
120
+ .tree
121
+ .get(*parent)
122
+ .and_then(|node| node.last_child())
123
+ .filter(|node| node.value().is_text())
124
+ .map(|node| node.id())
125
+ {
126
+ locations.text(id, &text);
127
+ }
128
+ }
129
+ }
130
+
131
+ fn append_based_on_parent_node(
132
+ &self,
133
+ element: &NodeId,
134
+ previous: &NodeId,
135
+ child: NodeOrText<NodeId>,
136
+ ) {
137
+ if self
138
+ .0
139
+ .0
140
+ .borrow()
141
+ .tree
142
+ .get(*element)
143
+ .and_then(|node| node.parent())
144
+ .is_some()
145
+ {
146
+ self.append_before_sibling(element, child);
147
+ } else {
148
+ self.append(previous, child);
149
+ }
150
+ }
151
+
152
+ fn append_doctype_to_document(
153
+ &self,
154
+ name: StrTendril,
155
+ public_id: StrTendril,
156
+ system_id: StrTendril,
157
+ ) {
158
+ self.0
159
+ .append_doctype_to_document(name, public_id, system_id);
160
+ if let Some(locations) = &self.2 {
161
+ if let Some(id) = self
162
+ .0
163
+ .0
164
+ .borrow()
165
+ .tree
166
+ .root()
167
+ .children()
168
+ .find(|node| matches!(node.value(), scraper::Node::Doctype(_)))
169
+ .map(|node| node.id())
170
+ {
171
+ locations.token(id, "doctype");
172
+ }
173
+ }
174
+ }
175
+
176
+ fn get_template_contents(&self, target: &NodeId) -> NodeId {
177
+ self.0.get_template_contents(target)
178
+ }
179
+
180
+ fn on_stack_pop(&self, node: &NodeId, _current: Option<&NodeId>) {
181
+ if let Some(locations) = &self.2 {
182
+ locations.closed(*node);
183
+ }
184
+ }
185
+
186
+ fn same_node(&self, first: &NodeId, second: &NodeId) -> bool {
187
+ self.0.same_node(first, second)
188
+ }
189
+
190
+ fn set_quirks_mode(&self, mode: QuirksMode) {
191
+ self.0.set_quirks_mode(mode);
192
+ }
193
+
194
+ fn append_before_sibling(&self, sibling: &NodeId, new_node: NodeOrText<NodeId>) {
195
+ let text = self.2.as_ref().and_then(|_| match &new_node {
196
+ NodeOrText::AppendText(text) => Some(text.to_string()),
197
+ _ => None,
198
+ });
199
+ self.0.append_before_sibling(sibling, new_node);
200
+ if let (Some(locations), Some(text)) = (&self.2, text) {
201
+ if let Some(id) = self
202
+ .0
203
+ .0
204
+ .borrow()
205
+ .tree
206
+ .get(*sibling)
207
+ .and_then(|node| node.prev_sibling())
208
+ .filter(|node| node.value().is_text())
209
+ .map(|node| node.id())
210
+ {
211
+ locations.text(id, &text);
212
+ }
213
+ }
214
+ }
215
+
216
+ fn add_attrs_if_missing(&self, target: &NodeId, attrs: Vec<Attribute>) {
217
+ let mut existing: HashSet<_> = self
218
+ .0
219
+ .0
220
+ .borrow()
221
+ .tree
222
+ .get(*target)
223
+ .and_then(|node| node.value().as_element())
224
+ .map(|element| {
225
+ element
226
+ .attrs
227
+ .iter()
228
+ .map(|(name, _)| name.local.clone())
229
+ .collect()
230
+ })
231
+ .unwrap_or_default();
232
+ let attrs = attrs
233
+ .into_iter()
234
+ .filter(|attr| existing.insert(attr.name.local.clone()))
235
+ .collect();
236
+ self.0.add_attrs_if_missing(target, attrs);
237
+ }
238
+
239
+ fn remove_from_parent(&self, target: &NodeId) {
240
+ self.0.remove_from_parent(target);
241
+ }
242
+
243
+ fn reparent_children(&self, node: &NodeId, new_parent: &NodeId) {
244
+ // scraper reparents with ego_tree's `reparent_from_id_append`, which
245
+ // only points the first and last moved child at their new parent;
246
+ // the adoption agency algorithm then loses nodes moved later.
247
+ let mut html = self.0 .0.borrow_mut();
248
+ let children: Vec<NodeId> = html
249
+ .tree
250
+ .get(*node)
251
+ .map(|parent| parent.children().map(|child| child.id()).collect())
252
+ .unwrap_or_default();
253
+ if let Some(mut parent) = html.tree.get_mut(*new_parent) {
254
+ for child in children {
255
+ parent.append_id(child);
256
+ }
257
+ }
258
+ }
259
+
260
+ fn is_mathml_annotation_xml_integration_point(&self, handle: &NodeId) -> bool {
261
+ let html = self.0 .0.borrow();
262
+ html.tree
263
+ .get(*handle)
264
+ .and_then(|node| node.value().as_element())
265
+ .is_some_and(|element| {
266
+ element.name.ns.as_ref() == "http://www.w3.org/1998/Math/MathML"
267
+ && element.name() == "annotation-xml"
268
+ && element.attr("encoding").is_some_and(|encoding| {
269
+ encoding.eq_ignore_ascii_case("text/html")
270
+ || encoding.eq_ignore_ascii_case("application/xhtml+xml")
271
+ })
272
+ })
273
+ }
274
+ }