@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,180 @@
1
+ //! HTML parsing and querying with CSS selectors.
2
+ //!
3
+ //! `scraper` is on [Crates.io][crate] and [GitHub][github].
4
+ //!
5
+ //! [crate]: https://crates.io/crates/scraper
6
+ //! [github]: https://github.com/programble/scraper
7
+ //!
8
+ //! Scraper provides an interface to Servo's `html5ever` and `selectors` crates, for browser-grade
9
+ //! parsing and querying.
10
+ //!
11
+ //! # Examples
12
+ //!
13
+ //! ## Parsing a document
14
+ //!
15
+ //! ```
16
+ //! use scraper::Html;
17
+ //!
18
+ //! let html = r#"
19
+ //! <!DOCTYPE html>
20
+ //! <meta charset="utf-8">
21
+ //! <title>Hello, world!</title>
22
+ //! <h1 class="foo">Hello, <i>world!</i></h1>
23
+ //! "#;
24
+ //!
25
+ //! let document = Html::parse_document(html);
26
+ //! ```
27
+ //!
28
+ //! ## Parsing a fragment
29
+ //!
30
+ //! ```
31
+ //! use scraper::Html;
32
+ //! let fragment = Html::parse_fragment("<h1>Hello, <i>world!</i></h1>");
33
+ //! ```
34
+ //!
35
+ //! ## Parsing a selector
36
+ //!
37
+ //! ```
38
+ //! use scraper::Selector;
39
+ //! let selector = Selector::parse("h1.foo").unwrap();
40
+ //! ```
41
+ //!
42
+ //! ## Selecting elements
43
+ //!
44
+ //! ```
45
+ //! use scraper::{Html, Selector};
46
+ //!
47
+ //! let html = r#"
48
+ //! <ul>
49
+ //! <li>Foo</li>
50
+ //! <li>Bar</li>
51
+ //! <li>Baz</li>
52
+ //! </ul>
53
+ //! "#;
54
+ //!
55
+ //! let fragment = Html::parse_fragment(html);
56
+ //! let selector = Selector::parse("li").unwrap();
57
+ //!
58
+ //! for element in fragment.select(&selector) {
59
+ //! assert_eq!("li", element.value().name());
60
+ //! }
61
+ //! ```
62
+ //!
63
+ //! ## Selecting descendent elements
64
+ //!
65
+ //! ```
66
+ //! use scraper::{Html, Selector};
67
+ //!
68
+ //! let html = r#"
69
+ //! <ul>
70
+ //! <li>Foo</li>
71
+ //! <li>Bar</li>
72
+ //! <li>Baz</li>
73
+ //! </ul>
74
+ //! "#;
75
+ //!
76
+ //! let fragment = Html::parse_fragment(html);
77
+ //! let ul_selector = Selector::parse("ul").unwrap();
78
+ //! let li_selector = Selector::parse("li").unwrap();
79
+ //!
80
+ //! let ul = fragment.select(&ul_selector).next().unwrap();
81
+ //! for element in ul.select(&li_selector) {
82
+ //! assert_eq!("li", element.value().name());
83
+ //! }
84
+ //! ```
85
+ //!
86
+ //! ## Accessing element attributes
87
+ //!
88
+ //! ```
89
+ //! use scraper::{Html, Selector};
90
+ //!
91
+ //! let fragment = Html::parse_fragment(r#"<input name="foo" value="bar">"#);
92
+ //! let selector = Selector::parse(r#"input[name="foo"]"#).unwrap();
93
+ //!
94
+ //! let input = fragment.select(&selector).next().unwrap();
95
+ //! assert_eq!(Some("bar"), input.value().attr("value"));
96
+ //! ```
97
+ //!
98
+ //! ## Serializing HTML and inner HTML
99
+ //!
100
+ //! ```
101
+ //! use scraper::{Html, Selector};
102
+ //!
103
+ //! let fragment = Html::parse_fragment("<h1>Hello, <i>world!</i></h1>");
104
+ //! let selector = Selector::parse("h1").unwrap();
105
+ //!
106
+ //! let h1 = fragment.select(&selector).next().unwrap();
107
+ //!
108
+ //! assert_eq!("<h1>Hello, <i>world!</i></h1>", h1.html());
109
+ //! assert_eq!("Hello, <i>world!</i>", h1.inner_html());
110
+ //! ```
111
+ //!
112
+ //! ## Accessing descendent text
113
+ //!
114
+ //! ```
115
+ //! use scraper::{Html, Selector};
116
+ //!
117
+ //! let fragment = Html::parse_fragment("<h1>Hello, <i>world!</i></h1>");
118
+ //! let selector = Selector::parse("h1").unwrap();
119
+ //!
120
+ //! let h1 = fragment.select(&selector).next().unwrap();
121
+ //! let text = h1.text().collect::<Vec<_>>();
122
+ //!
123
+ //! assert_eq!(vec!["Hello, ", "world!"], text);
124
+ //! ```
125
+
126
+ #![warn(
127
+ missing_docs,
128
+ missing_debug_implementations,
129
+ missing_copy_implementations,
130
+ trivial_casts,
131
+ trivial_numeric_casts,
132
+ unused_extern_crates,
133
+ unused_import_braces,
134
+ unused_qualifications,
135
+ variant_size_differences
136
+ )]
137
+
138
+ #[macro_use]
139
+ extern crate html5ever;
140
+
141
+ pub use crate::element_ref::ElementRef;
142
+ pub use crate::html::{Html, HtmlTreeSink};
143
+ pub use crate::node::Node;
144
+ pub use crate::selector::Selector;
145
+
146
+ pub use selectors::{attr::CaseSensitivity, Element};
147
+
148
+ pub mod element_ref;
149
+ pub mod error;
150
+ pub mod html;
151
+ pub mod node;
152
+ pub mod selectable;
153
+ pub mod selector;
154
+
155
+ #[cfg(feature = "atomic")]
156
+ pub(crate) mod tendril_util {
157
+ use html5ever::tendril;
158
+ /// Atomic equivalent to the default `StrTendril` type.
159
+ pub type StrTendril = tendril::Tendril<tendril::fmt::UTF8, tendril::Atomic>;
160
+
161
+ /// Convert a standard tendril into an atomic one.
162
+ pub fn make(s: tendril::StrTendril) -> StrTendril {
163
+ s.into_send().into()
164
+ }
165
+ }
166
+
167
+ #[cfg(not(feature = "atomic"))]
168
+ pub(crate) mod tendril_util {
169
+ use html5ever::tendril;
170
+ /// Primary string tendril type.
171
+ pub type StrTendril = tendril::StrTendril;
172
+
173
+ /// Return unaltered.
174
+ pub fn make(s: StrTendril) -> StrTendril {
175
+ s
176
+ }
177
+ }
178
+
179
+ pub use tendril_util::StrTendril;
180
+
@@ -0,0 +1,52 @@
1
+ use std::io::Error;
2
+
3
+ use ego_tree::{iter::Edge, NodeRef};
4
+ use html5ever::serialize::{Serializer, TraversalScope};
5
+
6
+ use crate::Node;
7
+
8
+ /// Serialize an HTML node using html5ever serializer.
9
+ pub(crate) fn serialize<S: Serializer>(
10
+ self_node: NodeRef<Node>,
11
+ serializer: &mut S,
12
+ traversal_scope: TraversalScope,
13
+ ) -> Result<(), Error> {
14
+ for edge in self_node.traverse() {
15
+ match edge {
16
+ Edge::Open(node) => {
17
+ if node == self_node && traversal_scope == TraversalScope::ChildrenOnly(None) {
18
+ continue;
19
+ }
20
+
21
+ match *node.value() {
22
+ Node::Doctype(ref doctype) => {
23
+ serializer.write_doctype(doctype.name())?;
24
+ }
25
+ Node::Comment(ref comment) => {
26
+ serializer.write_comment(comment)?;
27
+ }
28
+ Node::Text(ref text) => {
29
+ serializer.write_text(text)?;
30
+ }
31
+ Node::Element(ref elem) => {
32
+ let attrs = elem.attrs.iter().map(|(k, v)| (k, &v[..]));
33
+ serializer.start_elem(elem.name.clone(), attrs)?;
34
+ }
35
+ _ => (),
36
+ }
37
+ }
38
+
39
+ Edge::Close(node) => {
40
+ if node == self_node && traversal_scope == TraversalScope::ChildrenOnly(None) {
41
+ continue;
42
+ }
43
+
44
+ if let Some(elem) = node.value().as_element() {
45
+ serializer.end_elem(elem.name.clone())?;
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+ Ok(())
52
+ }
@@ -0,0 +1,465 @@
1
+ //! HTML nodes.
2
+
3
+ use std::fmt;
4
+ use std::ops::Deref;
5
+ use std::slice::Iter as SliceIter;
6
+
7
+ use crate::{CaseSensitivity, StrTendril};
8
+ use html5ever::{Attribute, LocalName, QualName};
9
+ use std::cell::OnceCell;
10
+
11
+ /// An HTML node.
12
+ // `Element` is usally the most common variant and hence boxing it
13
+ // will most likely not improve performance overall.
14
+ #[allow(variant_size_differences)]
15
+ #[derive(Clone, PartialEq, Eq)]
16
+ pub enum Node {
17
+ /// The document root.
18
+ Document,
19
+
20
+ /// The fragment root.
21
+ Fragment,
22
+
23
+ /// A doctype.
24
+ Doctype(Doctype),
25
+
26
+ /// A comment.
27
+ Comment(Comment),
28
+
29
+ /// Text.
30
+ Text(Text),
31
+
32
+ /// An element.
33
+ Element(Element),
34
+
35
+ /// A processing instruction.
36
+ ProcessingInstruction(ProcessingInstruction),
37
+ }
38
+
39
+ impl Node {
40
+ /// Returns true if node is the document root.
41
+ pub fn is_document(&self) -> bool {
42
+ matches!(*self, Node::Document)
43
+ }
44
+
45
+ /// Returns true if node is the fragment root.
46
+ pub fn is_fragment(&self) -> bool {
47
+ matches!(*self, Node::Fragment)
48
+ }
49
+
50
+ /// Returns true if node is a doctype.
51
+ pub fn is_doctype(&self) -> bool {
52
+ matches!(*self, Node::Doctype(_))
53
+ }
54
+
55
+ /// Returns true if node is a comment.
56
+ pub fn is_comment(&self) -> bool {
57
+ matches!(*self, Node::Comment(_))
58
+ }
59
+
60
+ /// Returns true if node is text.
61
+ pub fn is_text(&self) -> bool {
62
+ matches!(*self, Node::Text(_))
63
+ }
64
+
65
+ /// Returns true if node is an element.
66
+ pub fn is_element(&self) -> bool {
67
+ matches!(*self, Node::Element(_))
68
+ }
69
+
70
+ /// Returns self as a doctype.
71
+ pub fn as_doctype(&self) -> Option<&Doctype> {
72
+ match *self {
73
+ Node::Doctype(ref d) => Some(d),
74
+ _ => None,
75
+ }
76
+ }
77
+
78
+ /// Returns self as a comment.
79
+ pub fn as_comment(&self) -> Option<&Comment> {
80
+ match *self {
81
+ Node::Comment(ref c) => Some(c),
82
+ _ => None,
83
+ }
84
+ }
85
+
86
+ /// Returns self as text.
87
+ pub fn as_text(&self) -> Option<&Text> {
88
+ match *self {
89
+ Node::Text(ref t) => Some(t),
90
+ _ => None,
91
+ }
92
+ }
93
+
94
+ /// Returns self as an element.
95
+ pub fn as_element(&self) -> Option<&Element> {
96
+ match *self {
97
+ Node::Element(ref e) => Some(e),
98
+ _ => None,
99
+ }
100
+ }
101
+
102
+ /// Returns self as an element.
103
+ pub fn as_processing_instruction(&self) -> Option<&ProcessingInstruction> {
104
+ match *self {
105
+ Node::ProcessingInstruction(ref pi) => Some(pi),
106
+ _ => None,
107
+ }
108
+ }
109
+ }
110
+
111
+ // Always use one line.
112
+ impl fmt::Debug for Node {
113
+ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
114
+ match *self {
115
+ Node::Document => write!(f, "Document"),
116
+ Node::Fragment => write!(f, "Fragment"),
117
+ Node::Doctype(ref d) => write!(f, "Doctype({d:?})"),
118
+ Node::Comment(ref c) => write!(f, "Comment({c:?})"),
119
+ Node::Text(ref t) => write!(f, "Text({t:?})"),
120
+ Node::Element(ref e) => write!(f, "Element({e:?})"),
121
+ Node::ProcessingInstruction(ref pi) => write!(f, "ProcessingInstruction({pi:?})"),
122
+ }
123
+ }
124
+ }
125
+
126
+ /// A doctype.
127
+ #[derive(Clone, PartialEq, Eq)]
128
+ pub struct Doctype {
129
+ /// The doctype name.
130
+ pub name: StrTendril,
131
+
132
+ /// The doctype public ID.
133
+ pub public_id: StrTendril,
134
+
135
+ /// The doctype system ID.
136
+ pub system_id: StrTendril,
137
+ }
138
+
139
+ impl Doctype {
140
+ /// Returns the doctype name.
141
+ pub fn name(&self) -> &str {
142
+ self.name.deref()
143
+ }
144
+
145
+ /// Returns the doctype public ID.
146
+ pub fn public_id(&self) -> &str {
147
+ self.public_id.deref()
148
+ }
149
+
150
+ /// Returns the doctype system ID.
151
+ pub fn system_id(&self) -> &str {
152
+ self.system_id.deref()
153
+ }
154
+ }
155
+
156
+ impl fmt::Debug for Doctype {
157
+ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
158
+ write!(
159
+ f,
160
+ "<!DOCTYPE {} PUBLIC {:?} {:?}>",
161
+ self.name(),
162
+ self.public_id(),
163
+ self.system_id()
164
+ )
165
+ }
166
+ }
167
+
168
+ /// An HTML comment.
169
+ #[derive(Clone, PartialEq, Eq)]
170
+ pub struct Comment {
171
+ /// The comment text.
172
+ pub comment: StrTendril,
173
+ }
174
+
175
+ impl Deref for Comment {
176
+ type Target = str;
177
+
178
+ fn deref(&self) -> &str {
179
+ self.comment.deref()
180
+ }
181
+ }
182
+
183
+ impl fmt::Debug for Comment {
184
+ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
185
+ write!(f, "<!-- {:?} -->", self.deref())
186
+ }
187
+ }
188
+
189
+ /// HTML text.
190
+ #[derive(Clone, PartialEq, Eq)]
191
+ pub struct Text {
192
+ /// The text.
193
+ pub text: StrTendril,
194
+ }
195
+
196
+ impl Deref for Text {
197
+ type Target = str;
198
+
199
+ fn deref(&self) -> &str {
200
+ self.text.deref()
201
+ }
202
+ }
203
+
204
+ impl fmt::Debug for Text {
205
+ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
206
+ write!(f, "{:?}", self.deref())
207
+ }
208
+ }
209
+
210
+ /// A Map of attributes that preserves the order of the attributes.
211
+ ///
212
+ /// The published crate uses `indexmap::IndexMap<QualName, StrTendril>` here;
213
+ /// this vendored copy uses a vector-backed map so HTML tree construction pays
214
+ /// one insertion instead of a hash per attribute. Elements hold a handful of
215
+ /// attributes, so a linear scan costs less than hashing.
216
+ #[derive(Clone, Debug, Default, PartialEq, Eq)]
217
+ pub struct Attributes {
218
+ entries: Vec<(QualName, StrTendril)>,
219
+ }
220
+
221
+ impl Attributes {
222
+ /// Creates an empty map with room for `capacity` entries.
223
+ pub fn with_capacity(capacity: usize) -> Self {
224
+ Self {
225
+ entries: Vec::with_capacity(capacity),
226
+ }
227
+ }
228
+
229
+ /// Returns the value of an attribute by qualified name.
230
+ pub fn get(&self, name: &QualName) -> Option<&StrTendril> {
231
+ self.entries
232
+ .iter()
233
+ .find(|(key, _)| key == name)
234
+ .map(|(_, value)| value)
235
+ }
236
+
237
+ /// Returns true when an attribute with that qualified name exists.
238
+ pub fn contains_key(&self, name: &QualName) -> bool {
239
+ self.entries.iter().any(|(key, _)| key == name)
240
+ }
241
+
242
+ /// Adds an attribute unless a duplicate exists, keeping the first value
243
+ /// and the insertion order, as the tree builder requires.
244
+ pub fn or_insert(&mut self, name: QualName, value: StrTendril) {
245
+ if !self.contains_key(&name) {
246
+ self.entries.push((name, value));
247
+ }
248
+ }
249
+
250
+ /// Returns true when the map holds no attributes.
251
+ pub fn is_empty(&self) -> bool {
252
+ self.entries.is_empty()
253
+ }
254
+
255
+ /// The number of attributes.
256
+ pub fn len(&self) -> usize {
257
+ self.entries.len()
258
+ }
259
+
260
+ /// Iterates the entries in insertion order.
261
+ pub fn iter(&self) -> AttributesIter<'_> {
262
+ AttributesIter {
263
+ inner: self.entries.iter(),
264
+ }
265
+ }
266
+ }
267
+ impl Extend<(QualName, StrTendril)> for Attributes {
268
+ fn extend<T: IntoIterator<Item = (QualName, StrTendril)>>(&mut self, iter: T) {
269
+ for (name, value) in iter {
270
+ self.or_insert(name, value);
271
+ }
272
+ }
273
+ }
274
+
275
+ impl FromIterator<(QualName, StrTendril)> for Attributes {
276
+ fn from_iter<T: IntoIterator<Item = (QualName, StrTendril)>>(iter: T) -> Self {
277
+ let mut map = Self::default();
278
+ map.extend(iter);
279
+ map
280
+ }
281
+ }
282
+
283
+ impl<'a> IntoIterator for &'a Attributes {
284
+ type Item = (&'a QualName, &'a StrTendril);
285
+ type IntoIter = AttributesIter<'a>;
286
+
287
+ fn into_iter(self) -> Self::IntoIter {
288
+ self.iter()
289
+ }
290
+ }
291
+
292
+ /// An iterator over a node's attributes, yielding the qualified name and the
293
+ /// value in insertion order.
294
+ #[derive(Clone, Debug)]
295
+ pub struct AttributesIter<'a> {
296
+ inner: SliceIter<'a, (QualName, StrTendril)>,
297
+ }
298
+
299
+ impl<'a> Iterator for AttributesIter<'a> {
300
+ type Item = (&'a QualName, &'a StrTendril);
301
+
302
+ fn next(&mut self) -> Option<Self::Item> {
303
+ self.inner.next().map(|(name, value)| (name, value))
304
+ }
305
+
306
+ fn size_hint(&self) -> (usize, Option<usize>) {
307
+ self.inner.size_hint()
308
+ }
309
+ }
310
+
311
+ impl ExactSizeIterator for AttributesIter<'_> {}
312
+
313
+ impl DoubleEndedIterator for AttributesIter<'_> {
314
+ fn next_back(&mut self) -> Option<Self::Item> {
315
+ self.inner.next_back().map(|(name, value)| (name, value))
316
+ }
317
+ }
318
+
319
+ /// An HTML element.
320
+ #[derive(Clone, PartialEq, Eq)]
321
+ pub struct Element {
322
+ /// The element name.
323
+ pub name: QualName,
324
+
325
+ /// The element attributes.
326
+ pub attrs: Attributes,
327
+
328
+ id: OnceCell<Option<StrTendril>>,
329
+
330
+ classes: OnceCell<Box<[LocalName]>>,
331
+ }
332
+
333
+ impl Element {
334
+ #[doc(hidden)]
335
+ pub fn new(name: QualName, attributes: Vec<Attribute>) -> Self {
336
+ let attrs = attributes
337
+ .into_iter()
338
+ .map(|attr| (attr.name, crate::tendril_util::make(attr.value)))
339
+ .collect::<Attributes>();
340
+
341
+ Element {
342
+ attrs,
343
+ name,
344
+ id: OnceCell::new(),
345
+ classes: OnceCell::new(),
346
+ }
347
+ }
348
+
349
+ /// Returns the element name.
350
+ pub fn name(&self) -> &str {
351
+ self.name.local.deref()
352
+ }
353
+
354
+ /// Returns the element ID.
355
+ pub fn id(&self) -> Option<&str> {
356
+ self.id
357
+ .get_or_init(|| {
358
+ self.attrs
359
+ .iter()
360
+ .find(|(name, _)| name.local.as_ref() == "id")
361
+ .map(|(_, value)| value.clone())
362
+ })
363
+ .as_deref()
364
+ }
365
+
366
+ /// Returns true if element has the class.
367
+ pub fn has_class(&self, class: &str, case_sensitive: CaseSensitivity) -> bool {
368
+ self.classes()
369
+ .any(|c| case_sensitive.eq(c.as_bytes(), class.as_bytes()))
370
+ }
371
+
372
+ /// Returns an iterator over the element's classes.
373
+ pub fn classes(&self) -> Classes<'_> {
374
+ let classes = self.classes.get_or_init(|| {
375
+ let mut classes = self
376
+ .attrs
377
+ .iter()
378
+ .filter(|(name, _)| name.local.as_ref() == "class")
379
+ .flat_map(|(_, value)| value.split_ascii_whitespace().map(LocalName::from))
380
+ .collect::<Vec<_>>();
381
+
382
+ classes.sort_unstable();
383
+ classes.dedup();
384
+
385
+ classes.into_boxed_slice()
386
+ });
387
+
388
+ Classes {
389
+ inner: classes.iter(),
390
+ }
391
+ }
392
+
393
+ /// Returns the value of an attribute.
394
+ pub fn attr(&self, attr: &str) -> Option<&str> {
395
+ let qualname = QualName::new(None, ns!(), LocalName::from(attr));
396
+
397
+ self.attrs.get(&qualname).map(Deref::deref)
398
+ }
399
+
400
+ /// Returns an iterator over the element's attributes.
401
+ pub fn attrs(&self) -> Attrs<'_> {
402
+ Attrs {
403
+ inner: self.attrs.iter(),
404
+ }
405
+ }
406
+ }
407
+
408
+ /// Iterator over classes.
409
+ #[allow(missing_debug_implementations)]
410
+ #[derive(Clone)]
411
+ pub struct Classes<'a> {
412
+ inner: SliceIter<'a, LocalName>,
413
+ }
414
+
415
+ impl<'a> Iterator for Classes<'a> {
416
+ type Item = &'a str;
417
+
418
+ fn next(&mut self) -> Option<&'a str> {
419
+ self.inner.next().map(Deref::deref)
420
+ }
421
+ }
422
+
423
+ /// Iterator over attributes.
424
+ #[allow(missing_debug_implementations)]
425
+ #[derive(Clone)]
426
+ pub struct Attrs<'a> {
427
+ inner: AttributesIter<'a>,
428
+ }
429
+
430
+ impl<'a> Iterator for Attrs<'a> {
431
+ type Item = (&'a str, &'a str);
432
+
433
+ fn next(&mut self) -> Option<(&'a str, &'a str)> {
434
+ self.inner.next().map(|(k, v)| (k.local.deref(), v.deref()))
435
+ }
436
+ }
437
+
438
+ impl fmt::Debug for Element {
439
+ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
440
+ write!(f, "<{}", self.name())?;
441
+ for (key, value) in self.attrs() {
442
+ write!(f, " {key}={value:?}")?;
443
+ }
444
+ write!(f, ">")
445
+ }
446
+ }
447
+
448
+ /// HTML Processing Instruction.
449
+ #[derive(Debug, Clone, PartialEq, Eq)]
450
+ pub struct ProcessingInstruction {
451
+ /// The PI target.
452
+ pub target: StrTendril,
453
+ /// The PI data.
454
+ pub data: StrTendril,
455
+ }
456
+
457
+ impl Deref for ProcessingInstruction {
458
+ type Target = str;
459
+
460
+ fn deref(&self) -> &str {
461
+ self.data.deref()
462
+ }
463
+ }
464
+
465
+ pub(crate) mod serializable;