@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,70 @@
1
+ //! Provides the [`Selectable`] to abstract over collections of elements
2
+
3
+ use crate::{
4
+ element_ref::{self, ElementRef},
5
+ html::{self, Html},
6
+ selector::Selector,
7
+ };
8
+
9
+ /// Trait to abstract over collections of elements to which a [CSS selector][Selector] can be applied
10
+ ///
11
+ /// The mainly enables writing helper functions which are generic over [`Html`] and [`ElementRef`], e.g.
12
+ ///
13
+ /// ```
14
+ /// use scraper::{selectable::Selectable, selector::Selector};
15
+ ///
16
+ /// fn text_of_first_match<'a, S>(selectable: S, selector: &Selector) -> Option<String>
17
+ /// where
18
+ /// S: Selectable<'a>,
19
+ /// {
20
+ /// selectable.select(selector).next().map(|element| element.text().collect())
21
+ /// }
22
+ /// ```
23
+ pub trait Selectable<'a> {
24
+ /// Iterator over [element references][ElementRef] matching a [CSS selector[Selector]
25
+ type Select<'b>: Iterator<Item = ElementRef<'a>>;
26
+
27
+ /// Applies the given `selector` to the collection of elements represented by `self`
28
+ fn select(self, selector: &Selector) -> Self::Select<'_>;
29
+ }
30
+
31
+ impl<'a> Selectable<'a> for &'a Html {
32
+ type Select<'b> = html::Select<'a, 'b>;
33
+
34
+ fn select(self, selector: &Selector) -> Self::Select<'_> {
35
+ Html::select(self, selector)
36
+ }
37
+ }
38
+
39
+ impl<'a> Selectable<'a> for ElementRef<'a> {
40
+ type Select<'b> = element_ref::Select<'a, 'b>;
41
+
42
+ fn select(self, selector: &Selector) -> Self::Select<'_> {
43
+ ElementRef::select(&self, selector)
44
+ }
45
+ }
46
+
47
+ #[cfg(test)]
48
+ mod tests {
49
+ use super::*;
50
+
51
+ fn select_one<'a, S>(selectable: S, selector: &Selector) -> Option<ElementRef<'a>>
52
+ where
53
+ S: Selectable<'a>,
54
+ {
55
+ selectable.select(selector).next()
56
+ }
57
+
58
+ #[test]
59
+ fn html_and_element_ref_are_selectable() {
60
+ let fragment = Html::parse_fragment(
61
+ r#"<select class="foo"><option value="bar">foobar</option></select>"#,
62
+ );
63
+
64
+ let selector = Selector::parse("select.foo").unwrap();
65
+ let element = select_one(&fragment, &selector).unwrap();
66
+
67
+ let selector = Selector::parse("select.foo option[value='bar']").unwrap();
68
+ let _element = select_one(element, &selector).unwrap();
69
+ }
70
+ }
@@ -0,0 +1,290 @@
1
+ //! CSS selectors.
2
+
3
+ use std::convert::TryFrom;
4
+ use std::fmt;
5
+
6
+ pub use cssparser::ToCss;
7
+ use html5ever::{LocalName, Namespace};
8
+ use precomputed_hash::PrecomputedHash;
9
+ use selectors::{
10
+ matching,
11
+ parser::{self, ParseRelative, SelectorList, SelectorParseErrorKind},
12
+ };
13
+
14
+ #[cfg(feature = "serde")]
15
+ use serde::{de::Visitor, Deserialize, Serialize};
16
+
17
+ use crate::error::SelectorErrorKind;
18
+ use crate::ElementRef;
19
+
20
+ /// Wrapper around CSS selectors.
21
+ ///
22
+ /// Represents a "selector group", i.e. a comma-separated list of selectors.
23
+ #[derive(Debug, Clone, PartialEq, Eq)]
24
+ pub struct Selector {
25
+ /// The CSS selectors.
26
+ selectors: SelectorList<Simple>,
27
+ }
28
+
29
+ impl Selector {
30
+ /// Parses a CSS selector group.
31
+ pub fn parse(selectors: &str) -> Result<Self, SelectorErrorKind<'_>> {
32
+ let mut parser_input = cssparser::ParserInput::new(selectors);
33
+ let mut parser = cssparser::Parser::new(&mut parser_input);
34
+
35
+ SelectorList::parse(&Parser, &mut parser, ParseRelative::No)
36
+ .map(|selectors| Self { selectors })
37
+ .map_err(SelectorErrorKind::from)
38
+ }
39
+
40
+ /// Returns true if the element matches this selector.
41
+ pub fn matches(&self, element: &ElementRef) -> bool {
42
+ self.matches_with_scope(element, None)
43
+ }
44
+
45
+ /// Returns true if the element matches this selector.
46
+ /// The optional `scope` argument is used to specify which element has `:scope` pseudo-class.
47
+ /// When it is `None`, `:scope` will match the root element.
48
+ pub fn matches_with_scope(&self, element: &ElementRef, scope: Option<ElementRef>) -> bool {
49
+ self.matches_with_scope_and_cache(element, scope, &mut Default::default())
50
+ }
51
+
52
+ // The `nth_index_cache` must not be used after `self` is dropped
53
+ // to avoid incorrect results (even though no undefined behaviour is possible)
54
+ // due to the usage of selector memory addresses as cache keys.
55
+ pub(crate) fn matches_with_scope_and_cache(
56
+ &self,
57
+ element: &ElementRef,
58
+ scope: Option<ElementRef>,
59
+ caches: &mut matching::SelectorCaches,
60
+ ) -> bool {
61
+ let mut context = matching::MatchingContext::new(
62
+ matching::MatchingMode::Normal,
63
+ None,
64
+ caches,
65
+ matching::QuirksMode::NoQuirks,
66
+ matching::NeedsSelectorFlags::No,
67
+ matching::MatchingForInvalidation::No,
68
+ );
69
+ context.scope_element = scope.map(|x| selectors::Element::opaque(&x));
70
+ self.selectors
71
+ .slice()
72
+ .iter()
73
+ .any(|s| matching::matches_selector(s, 0, None, element, &mut context))
74
+ }
75
+ }
76
+
77
+ impl ToCss for Selector {
78
+ fn to_css<W>(&self, dest: &mut W) -> fmt::Result
79
+ where
80
+ W: fmt::Write,
81
+ {
82
+ self.selectors.to_css(dest)
83
+ }
84
+ }
85
+
86
+ #[cfg(feature = "serde")]
87
+ impl Serialize for Selector {
88
+ fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
89
+ serializer.serialize_str(&self.to_css_string())
90
+ }
91
+ }
92
+
93
+ #[cfg(feature = "serde")]
94
+ impl<'de> Deserialize<'de> for Selector {
95
+ fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
96
+ deserializer.deserialize_str(SelectorVisitor)
97
+ }
98
+ }
99
+
100
+ #[cfg(feature = "serde")]
101
+ struct SelectorVisitor;
102
+
103
+ #[cfg(feature = "serde")]
104
+ impl Visitor<'_> for SelectorVisitor {
105
+ type Value = Selector;
106
+
107
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
108
+ write!(formatter, "a css selector string")
109
+ }
110
+
111
+ fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
112
+ Selector::parse(v).map_err(serde::de::Error::custom)
113
+ }
114
+ }
115
+
116
+ /// An implementation of `Parser` for `selectors`
117
+ #[derive(Clone, Copy, Debug)]
118
+ pub struct Parser;
119
+ impl<'i> parser::Parser<'i> for Parser {
120
+ type Impl = Simple;
121
+ type Error = SelectorParseErrorKind<'i>;
122
+
123
+ fn parse_is_and_where(&self) -> bool {
124
+ true
125
+ }
126
+
127
+ fn parse_has(&self) -> bool {
128
+ true
129
+ }
130
+ }
131
+
132
+ /// A simple implementation of `SelectorImpl` with no pseudo-classes or pseudo-elements.
133
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
134
+ pub struct Simple;
135
+
136
+ impl parser::SelectorImpl for Simple {
137
+ type AttrValue = CssString;
138
+ type Identifier = CssLocalName;
139
+ type LocalName = CssLocalName;
140
+ type NamespacePrefix = CssLocalName;
141
+ type NamespaceUrl = Namespace;
142
+ type BorrowedNamespaceUrl = Namespace;
143
+ type BorrowedLocalName = CssLocalName;
144
+
145
+ type NonTSPseudoClass = NonTSPseudoClass;
146
+ type PseudoElement = PseudoElement;
147
+
148
+ // see: https://github.com/servo/servo/pull/19747#issuecomment-357106065
149
+ type ExtraMatchingData<'a> = ();
150
+ }
151
+
152
+ /// Wraps [`String`] so that it can be used with [`selectors`]
153
+ #[derive(Debug, Clone, PartialEq, Eq)]
154
+ pub struct CssString(pub String);
155
+
156
+ impl<'a> From<&'a str> for CssString {
157
+ fn from(val: &'a str) -> Self {
158
+ Self(val.to_owned())
159
+ }
160
+ }
161
+
162
+ impl AsRef<str> for CssString {
163
+ fn as_ref(&self) -> &str {
164
+ &self.0
165
+ }
166
+ }
167
+
168
+ impl ToCss for CssString {
169
+ fn to_css<W>(&self, dest: &mut W) -> fmt::Result
170
+ where
171
+ W: fmt::Write,
172
+ {
173
+ cssparser::serialize_string(&self.0, dest)
174
+ }
175
+ }
176
+
177
+ /// Wraps [`LocalName`] so that it can be used with [`selectors`]
178
+ #[derive(Debug, Default, Clone, PartialEq, Eq)]
179
+ pub struct CssLocalName(pub LocalName);
180
+
181
+ impl<'a> From<&'a str> for CssLocalName {
182
+ fn from(val: &'a str) -> Self {
183
+ Self(val.into())
184
+ }
185
+ }
186
+
187
+ impl ToCss for CssLocalName {
188
+ fn to_css<W>(&self, dest: &mut W) -> fmt::Result
189
+ where
190
+ W: fmt::Write,
191
+ {
192
+ dest.write_str(&self.0)
193
+ }
194
+ }
195
+
196
+ impl PrecomputedHash for CssLocalName {
197
+ fn precomputed_hash(&self) -> u32 {
198
+ self.0.precomputed_hash()
199
+ }
200
+ }
201
+
202
+ /// Non Tree-Structural Pseudo-Class.
203
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
204
+ pub enum NonTSPseudoClass {}
205
+
206
+ impl parser::NonTSPseudoClass for NonTSPseudoClass {
207
+ type Impl = Simple;
208
+
209
+ fn is_active_or_hover(&self) -> bool {
210
+ false
211
+ }
212
+
213
+ fn is_user_action_state(&self) -> bool {
214
+ false
215
+ }
216
+ }
217
+
218
+ impl ToCss for NonTSPseudoClass {
219
+ fn to_css<W>(&self, dest: &mut W) -> fmt::Result
220
+ where
221
+ W: fmt::Write,
222
+ {
223
+ dest.write_str("")
224
+ }
225
+ }
226
+
227
+ /// CSS Pseudo-Element
228
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
229
+ pub enum PseudoElement {}
230
+
231
+ impl parser::PseudoElement for PseudoElement {
232
+ type Impl = Simple;
233
+ }
234
+
235
+ impl ToCss for PseudoElement {
236
+ fn to_css<W>(&self, dest: &mut W) -> fmt::Result
237
+ where
238
+ W: fmt::Write,
239
+ {
240
+ dest.write_str("")
241
+ }
242
+ }
243
+
244
+ impl<'i> TryFrom<&'i str> for Selector {
245
+ type Error = SelectorErrorKind<'i>;
246
+
247
+ fn try_from(s: &'i str) -> Result<Self, Self::Error> {
248
+ Selector::parse(s)
249
+ }
250
+ }
251
+
252
+ #[cfg(test)]
253
+ mod tests {
254
+ use super::*;
255
+ use std::convert::TryInto;
256
+
257
+ #[test]
258
+ fn selector_conversions() {
259
+ let s = "#testid.testclass";
260
+ let _sel: Selector = s.try_into().unwrap();
261
+
262
+ let s = s.to_owned();
263
+ let _sel: Selector = (*s).try_into().unwrap();
264
+ }
265
+
266
+ #[test]
267
+ #[should_panic]
268
+ fn invalid_selector_conversions() {
269
+ let s = "<failing selector>";
270
+ let _sel: Selector = s.try_into().unwrap();
271
+ }
272
+
273
+ #[test]
274
+ fn has_selector() {
275
+ let s = ":has(a)";
276
+ let _sel: Selector = s.try_into().unwrap();
277
+ }
278
+
279
+ #[test]
280
+ fn is_selector() {
281
+ let s = ":is(a)";
282
+ let _sel: Selector = s.try_into().unwrap();
283
+ }
284
+
285
+ #[test]
286
+ fn where_selector() {
287
+ let s = ":where(a)";
288
+ let _sel: Selector = s.try_into().unwrap();
289
+ }
290
+ }
package/package.json CHANGED
@@ -1,6 +1,169 @@
1
1
  {
2
2
  "name": "@dvaji/cheerox",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.2",
4
+ "description": "Cheerio 0.22.0-compatible HTML/XML processing on a Rust engine, with parallel, non-blocking extraction",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/dvaJi/cheerox.git"
9
+ },
10
+ "homepage": "https://github.com/dvaJi/cheerox#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/dvaJi/cheerox/issues"
13
+ },
14
+ "keywords": [
15
+ "cheerio",
16
+ "rust",
17
+ "html",
18
+ "scraping",
19
+ "napi",
20
+ "parallel"
21
+ ],
22
+ "main": "./dist/index.cjs",
23
+ "module": "./dist/index.mjs",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "import": {
28
+ "types": "./dist/index.d.mts",
29
+ "default": "./dist/index.mjs"
30
+ },
31
+ "require": {
32
+ "types": "./dist/index.d.ts",
33
+ "default": "./dist/index.cjs"
34
+ }
35
+ },
36
+ "./modern": {
37
+ "import": {
38
+ "types": "./dist/modern.d.mts",
39
+ "default": "./dist/modern.mjs"
40
+ },
41
+ "require": {
42
+ "types": "./dist/modern.d.ts",
43
+ "default": "./dist/modern.cjs"
44
+ }
45
+ },
46
+ "./package.json": "./package.json",
47
+ "./slim": {
48
+ "import": {
49
+ "types": "./dist/slim.d.mts",
50
+ "default": "./dist/slim.mjs"
51
+ },
52
+ "require": {
53
+ "types": "./dist/slim.d.ts",
54
+ "default": "./dist/slim.cjs"
55
+ }
56
+ },
57
+ "./utils": {
58
+ "import": {
59
+ "types": "./dist/utils.d.mts",
60
+ "default": "./dist/utils.mjs"
61
+ },
62
+ "require": {
63
+ "types": "./dist/utils.d.ts",
64
+ "default": "./dist/utils.cjs"
65
+ }
66
+ }
67
+ },
68
+ "files": [
69
+ "dist",
70
+ "*.node",
71
+ "src",
72
+ "native/src",
73
+ "Cargo.toml",
74
+ "Cargo.lock",
75
+ "build.rs",
76
+ "tsconfig.json",
77
+ "tsdown.config.mjs",
78
+ "scripts/check-native.mjs",
79
+ "scripts/check-release.mjs",
80
+ "scripts/fix-declarations.mjs",
81
+ "scripts/generate-notices.mjs",
82
+ "LICENSE",
83
+ "THIRD_PARTY_NOTICES.md",
84
+ "README.md",
85
+ "CONTRIBUTING.md",
86
+ "RELEASING.md",
87
+ "scripts/notices",
88
+ "native/vendor"
89
+ ],
90
+ "engines": {
91
+ "node": ">=20.19.0"
92
+ },
93
+ "overrides": {
94
+ "esbuild": "0.28.2"
95
+ },
96
+ "scripts": {
97
+ "build": "npm run build:native && npm run build:ts",
98
+ "build:native": "napi build --platform --release --js false --dts native-binding.d.ts --output-dir .",
99
+ "build:ts": "tsdown --config-loader native && node scripts/fix-declarations.mjs && node scripts/write-entrypoints.mjs",
100
+ "check": "tsc --noEmit",
101
+ "test": "node --test test/*.test.mjs bench/*.test.mjs bench/suite/*.test.mjs",
102
+ "test:legacy": "node scripts/check-legacy.mjs && mocha --config test/legacy/.mocharc.cjs",
103
+ "test:legacy-oracle": "node scripts/write-legacy-oracle.mjs && mocha --config test/.mocharc-oracle.cjs",
104
+ "test:differential": "node scripts/differential.mjs",
105
+ "test:types": "tsc -p test/tsconfig.json --noEmit",
106
+ "test:native": "cargo test",
107
+ "test:compat": "npm run test:legacy && npm run test:upstream",
108
+ "bench": "node bench/benchmark.mjs",
109
+ "bench:legacy": "node bench/legacy.mjs",
110
+ "prepack": "npm run build:ts && node scripts/check-native.mjs",
111
+ "prepublishOnly": "npm run release:check",
112
+ "test:upstream": "node scripts/check-upstream.mjs && vitest run --config vitest.config.mts",
113
+ "test:package": "node scripts/test-package.mjs",
114
+ "bench:engine": "node bench/engine.mjs",
115
+ "bench:fair": "node --expose-gc bench/fair.mjs",
116
+ "bench:operations": "node bench/operations.mjs",
117
+ "bench:gate": "node bench/gate.mjs",
118
+ "release:check": "node scripts/check-release.mjs",
119
+ "notices:generate": "node scripts/generate-notices.mjs",
120
+ "notices:check": "node scripts/generate-notices.mjs --check"
121
+ },
122
+ "devDependencies": {
123
+ "@napi-rs/cli": "^3.10.9",
124
+ "@types/node": "^26.6.6",
125
+ "cheerio": "1.2.0",
126
+ "cheerio-legacy-reference": "npm:cheerio@^0.22.0",
127
+ "domhandler": "^6.0.1",
128
+ "domutils": "^4.0.2",
129
+ "encoding-sniffer": "^1.0.2",
130
+ "expect.js": "^0.3.1",
131
+ "htmlparser2": "^3.10.1",
132
+ "htmlparser2-modern": "npm:htmlparser2@^10.1.0",
133
+ "lodash.assignin": "^4.2.0",
134
+ "lodash.defaults": "^4.2.0",
135
+ "lodash.filter": "^4.6.0",
136
+ "lodash.flatten": "^4.4.0",
137
+ "lodash.foreach": "^4.5.0",
138
+ "lodash.map": "^4.6.0",
139
+ "lodash.merge": "^4.6.2",
140
+ "lodash.pick": "^4.4.0",
141
+ "lodash.reduce": "^4.6.0",
142
+ "lodash.reject": "^4.6.0",
143
+ "lodash.some": "^4.6.0",
144
+ "mocha": "^12.0.3",
145
+ "parse5": "^8.0.1",
146
+ "parse5-htmlparser2-tree-adapter": "^8.0.1",
147
+ "tsdown": "^0.23.4",
148
+ "typescript": "^7.0.2",
149
+ "vitest": "^5.0.5"
150
+ },
151
+ "napi": {
152
+ "binaryName": "cheerox",
153
+ "targets": [
154
+ "x86_64-unknown-linux-gnu",
155
+ "aarch64-unknown-linux-gnu",
156
+ "x86_64-unknown-linux-musl",
157
+ "aarch64-unknown-linux-musl",
158
+ "x86_64-apple-darwin",
159
+ "aarch64-apple-darwin",
160
+ "x86_64-pc-windows-msvc",
161
+ "aarch64-pc-windows-msvc"
162
+ ]
163
+ },
164
+ "author": "Francisco Pizarro",
165
+ "publishConfig": {
166
+ "access": "public",
167
+ "registry": "https://registry.npmjs.org/"
168
+ }
169
+ }
@@ -0,0 +1,15 @@
1
+ import assert from 'node:assert/strict';
2
+ import { readdirSync, readFileSync } from 'node:fs';
3
+ import { nativeTargets, validateNativeBinary } from './check-release.mjs';
4
+ import { nativeStatus } from '../dist/index.mjs';
5
+
6
+ const status = nativeStatus();
7
+ assert.ok(status.available, `Build the native addon before packaging: ${status.error}`);
8
+ if (process.argv.includes('--all')) {
9
+ const files = new Set(readdirSync(new URL('../', import.meta.url)));
10
+ for (const target of nativeTargets) {
11
+ assert.ok(files.has(`cheerox.${target}.node`), `Release is missing the ${target} native binary`);
12
+ }
13
+ }
14
+
15
+ for (const name of readdirSync(new URL('../', import.meta.url)).filter(name => /^cheerox\..+\.node$/.test(name))) validateNativeBinary(name, readFileSync(new URL(`../${name}`, import.meta.url)));