@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.
- package/CONTRIBUTING.md +74 -0
- package/Cargo.lock +1091 -0
- package/Cargo.toml +47 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/RELEASING.md +114 -0
- package/THIRD_PARTY_NOTICES.md +7581 -0
- package/build.rs +3 -0
- package/cheerox.darwin-arm64.node +0 -0
- package/cheerox.darwin-x64.node +0 -0
- package/cheerox.linux-arm64-gnu.node +0 -0
- package/cheerox.linux-arm64-musl.node +0 -0
- package/cheerox.linux-x64-gnu.node +0 -0
- package/cheerox.linux-x64-musl.node +0 -0
- package/cheerox.win32-arm64-msvc.node +0 -0
- package/cheerox.win32-x64-msvc.node +0 -0
- package/dist/cheerio-compat-BKkfJEAr.mjs +1648 -0
- package/dist/cheerio-compat-BKkfJEAr.mjs.map +1 -0
- package/dist/cheerio-compat-DRLPzBdX.d.mts +88 -0
- package/dist/cheerio-compat-DRsINTwV.d.cts +88 -0
- package/dist/cheerio-compat-DRsINTwV.d.ts +88 -0
- package/dist/cheerio-compat-DcnYK9OB.cjs +1719 -0
- package/dist/cheerio-compat-DcnYK9OB.cjs.map +1 -0
- package/dist/cheerio-compat.cjs +18 -0
- package/dist/cheerio-compat.d.cts +2 -0
- package/dist/cheerio-compat.d.mts +2 -0
- package/dist/cheerio-compat.d.ts +2 -0
- package/dist/cheerio-compat.mjs +1 -0
- package/dist/index.cjs +13 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.mjs +53 -0
- package/dist/index.mjs.map +1 -0
- package/dist/legacy-parse.cjs +5 -0
- package/dist/legacy-parse.d.mts +8 -0
- package/dist/legacy-parse.d.ts +7 -0
- package/dist/legacy-parse.mjs +1 -0
- package/dist/modern-B7hyW4cI.d.cts +102 -0
- package/dist/modern-B7hyW4cI.d.ts +102 -0
- package/dist/modern-CHQGP0Yt.cjs +2858 -0
- package/dist/modern-CHQGP0Yt.cjs.map +1 -0
- package/dist/modern-CJvSD_y5.d.mts +102 -0
- package/dist/modern-CvXcTn82.mjs +2769 -0
- package/dist/modern-CvXcTn82.mjs.map +1 -0
- package/dist/modern.cjs +19 -0
- package/dist/modern.d.cts +3 -0
- package/dist/modern.d.mts +3 -0
- package/dist/modern.d.ts +3 -0
- package/dist/modern.mjs +3 -0
- package/dist/nodes-jbWWhssj.cjs +4210 -0
- package/dist/nodes-jbWWhssj.cjs.map +1 -0
- package/dist/nodes-sYxb5qyZ.mjs +3803 -0
- package/dist/nodes-sYxb5qyZ.mjs.map +1 -0
- package/dist/slim.cjs +16 -0
- package/dist/slim.cjs.map +1 -0
- package/dist/slim.d.cts +6 -0
- package/dist/slim.d.mts +6 -0
- package/dist/slim.d.ts +6 -0
- package/dist/slim.mjs +12 -0
- package/dist/slim.mjs.map +1 -0
- package/dist/types-CA01Zg3r.d.cts +526 -0
- package/dist/types-CA01Zg3r.d.mts +526 -0
- package/dist/types-CA01Zg3r.d.ts +526 -0
- package/dist/utils.cjs +31 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +8 -0
- package/dist/utils.d.mts +8 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.mjs +25 -0
- package/dist/utils.mjs.map +1 -0
- package/native/src/arena.rs +2109 -0
- package/native/src/attributes.rs +946 -0
- package/native/src/codec.rs +298 -0
- package/native/src/diagnostics.rs +1249 -0
- package/native/src/document_order.rs +711 -0
- package/native/src/extraction.rs +321 -0
- package/native/src/gc.rs +175 -0
- package/native/src/hook_sink.rs +801 -0
- package/native/src/hook_stream.rs +513 -0
- package/native/src/id_hash.rs +42 -0
- package/native/src/immutable.rs +333 -0
- package/native/src/lib.rs +4120 -0
- package/native/src/location.rs +392 -0
- package/native/src/markup_tokenizer.rs +668 -0
- package/native/src/runtime.rs +368 -0
- package/native/src/selector.rs +1596 -0
- package/native/src/sink.rs +274 -0
- package/native/src/snapshot.rs +672 -0
- package/native/src/stream.rs +334 -0
- package/native/src/tokenizer.rs +581 -0
- package/native/src/tracker.rs +232 -0
- package/native/vendor/README.md +35 -0
- package/native/vendor/html5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/html5ever-0.35.0/Cargo.lock +828 -0
- package/native/vendor/html5ever-0.35.0/Cargo.toml +85 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/html5ever-0.35.0/README.md +60 -0
- package/native/vendor/html5ever-0.35.0/benches/html5ever.rs +86 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum-zh.html +19 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum.html +40 -0
- package/native/vendor/html5ever-0.35.0/data/bench/medium-fragment.html +24 -0
- package/native/vendor/html5ever-0.35.0/data/bench/small-fragment.html +7 -0
- package/native/vendor/html5ever-0.35.0/data/bench/strong.html +1 -0
- package/native/vendor/html5ever-0.35.0/data/bench/tiny-fragment.html +1 -0
- package/native/vendor/html5ever-0.35.0/examples/arena.rs +356 -0
- package/native/vendor/html5ever-0.35.0/examples/capi/tokenize.c +74 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tokenize.rs +47 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tree-builder.rs +132 -0
- package/native/vendor/html5ever-0.35.0/examples/print-tree-actions.rs +185 -0
- package/native/vendor/html5ever-0.35.0/examples/tokenize.rs +111 -0
- package/native/vendor/html5ever-0.35.0/src/driver.rs +148 -0
- package/native/vendor/html5ever-0.35.0/src/lib.rs +30 -0
- package/native/vendor/html5ever-0.35.0/src/macros.rs +34 -0
- package/native/vendor/html5ever-0.35.0/src/serialize/mod.rs +255 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/char_ref/mod.rs +530 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/interface.rs +99 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/mod.rs +2628 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/states.rs +97 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/data.rs +170 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/mod.rs +1848 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/rules.rs +1494 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/tag_sets.rs +114 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/types.rs +91 -0
- package/native/vendor/html5ever-0.35.0/src/util/str.rs +52 -0
- package/native/vendor/html5gum-0.8.4/Cargo.lock +1006 -0
- package/native/vendor/html5gum-0.8.4/Cargo.toml +109 -0
- package/native/vendor/html5gum-0.8.4/LICENSE +19 -0
- package/native/vendor/html5gum-0.8.4/README.md +110 -0
- package/native/vendor/html5gum-0.8.4/benches/files/0050.html +2131 -0
- package/native/vendor/html5gum-0.8.4/benches/files/6D30.html +1004 -0
- package/native/vendor/html5gum-0.8.4/benches/files.rs +32 -0
- package/native/vendor/html5gum-0.8.4/benches/patterns.rs +31 -0
- package/native/vendor/html5gum-0.8.4/src/arrayvec.rs +29 -0
- package/native/vendor/html5gum-0.8.4/src/char_validator.rs +134 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/callback.rs +676 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/default.rs +207 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/emitter.rs +344 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/html5ever.rs +306 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/mod.rs +25 -0
- package/native/vendor/html5gum-0.8.4/src/entities.rs +3342 -0
- package/native/vendor/html5gum-0.8.4/src/error.rs +96 -0
- package/native/vendor/html5gum-0.8.4/src/htmlstring.rs +130 -0
- package/native/vendor/html5gum-0.8.4/src/lib.rs +66 -0
- package/native/vendor/html5gum-0.8.4/src/machine.rs +2136 -0
- package/native/vendor/html5gum-0.8.4/src/machine_helper.rs +277 -0
- package/native/vendor/html5gum-0.8.4/src/read_helper.rs +244 -0
- package/native/vendor/html5gum-0.8.4/src/reader.rs +440 -0
- package/native/vendor/html5gum-0.8.4/src/span.rs +98 -0
- package/native/vendor/html5gum-0.8.4/src/state.rs +16 -0
- package/native/vendor/html5gum-0.8.4/src/testutils.rs +71 -0
- package/native/vendor/html5gum-0.8.4/src/tokenizer.rs +189 -0
- package/native/vendor/html5gum-0.8.4/src/utils.rs +88 -0
- package/native/vendor/markup5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/markup5ever-0.35.0/Cargo.toml +45 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/markup5ever-0.35.0/interface/mod.rs +422 -0
- package/native/vendor/markup5ever-0.35.0/interface/tree_builder.rs +280 -0
- package/native/vendor/markup5ever-0.35.0/lib.rs +59 -0
- package/native/vendor/markup5ever-0.35.0/serialize.rs +77 -0
- package/native/vendor/markup5ever-0.35.0/util/buffer_queue.rs +333 -0
- package/native/vendor/markup5ever-0.35.0/util/smallcharset.rs +88 -0
- package/native/vendor/scraper-0.24.0/Cargo.toml +52 -0
- package/native/vendor/scraper-0.24.0/LICENSE +15 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/element.rs +212 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/mod.rs +236 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/serializable.rs +15 -0
- package/native/vendor/scraper-0.24.0/src/error/utils.rs +91 -0
- package/native/vendor/scraper-0.24.0/src/error.rs +118 -0
- package/native/vendor/scraper-0.24.0/src/html/mod.rs +270 -0
- package/native/vendor/scraper-0.24.0/src/html/serializable.rs +27 -0
- package/native/vendor/scraper-0.24.0/src/html/tree_sink.rs +285 -0
- package/native/vendor/scraper-0.24.0/src/lib.rs +180 -0
- package/native/vendor/scraper-0.24.0/src/node/serializable.rs +52 -0
- package/native/vendor/scraper-0.24.0/src/node.rs +465 -0
- package/native/vendor/scraper-0.24.0/src/selectable.rs +70 -0
- package/native/vendor/scraper-0.24.0/src/selector.rs +290 -0
- package/package.json +167 -4
- package/scripts/check-native.mjs +15 -0
- package/scripts/check-release.mjs +234 -0
- package/scripts/fix-declarations.mjs +39 -0
- package/scripts/generate-notices.mjs +194 -0
- package/scripts/notices/README.md +13 -0
- package/scripts/notices/cheerio-MIT.txt +21 -0
- package/scripts/notices/html5ever-AUTHORS.txt +20 -0
- package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
- package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
- package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
- package/scripts/notices/htmlparser2-MIT.txt +18 -0
- package/scripts/notices/napi-rs-MIT.txt +43 -0
- package/scripts/notices/neon-MIT.txt +19 -0
- package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
- package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
- package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
- package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
- package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
- package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
- package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
- package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
- package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
- package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
- package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
- package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
- package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
- package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
- package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
- package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
- package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
- package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
- package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
- package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
- package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
- package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
- package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
- package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
- package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
- package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
- package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
- package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
- package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
- package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
- package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
- package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
- package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
- package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
- package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
- package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
- package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
- package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
- package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
- package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
- package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
- package/scripts/notices/runtime.json +1632 -0
- package/scripts/notices/sources.json +561 -0
- package/scripts/notices/xray-MIT.txt +21 -0
- package/src/attribute-object.ts +71 -0
- package/src/attributes.ts +308 -0
- package/src/cheerio-compat/css.ts +58 -0
- package/src/cheerio-compat/render.ts +106 -0
- package/src/cheerio-compat/selectors.ts +51 -0
- package/src/cheerio-compat/types.ts +52 -0
- package/src/cheerio-compat.ts +2055 -0
- package/src/codec.ts +96 -0
- package/src/dom.ts +87 -0
- package/src/extract-plan.ts +97 -0
- package/src/extract.ts +322 -0
- package/src/hooks.ts +369 -0
- package/src/index.ts +48 -0
- package/src/io.ts +491 -0
- package/src/match.ts +207 -0
- package/src/modern.ts +259 -0
- package/src/native.ts +149 -0
- package/src/nodes.ts +831 -0
- package/src/parse.ts +53 -0
- package/src/parser-types.ts +157 -0
- package/src/router.ts +1129 -0
- package/src/selection.ts +682 -0
- package/src/slim.ts +12 -0
- package/src/snapshot.ts +291 -0
- package/src/transport.ts +113 -0
- package/src/types.ts +186 -0
- package/src/utils.ts +29 -0
- package/src/workers.ts +21 -0
- package/tsconfig.json +15 -0
- package/tsdown.config.mjs +17 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
//! Element references.
|
|
2
|
+
|
|
3
|
+
use std::fmt::{self, Debug};
|
|
4
|
+
use std::iter::FusedIterator;
|
|
5
|
+
use std::ops::Deref;
|
|
6
|
+
|
|
7
|
+
use ego_tree::iter::{Edge, Traverse};
|
|
8
|
+
use ego_tree::NodeRef;
|
|
9
|
+
use html5ever::serialize::{serialize, SerializeOpts, TraversalScope};
|
|
10
|
+
use selectors::matching::SelectorCaches;
|
|
11
|
+
|
|
12
|
+
use crate::node::Element;
|
|
13
|
+
use crate::{Node, Selector};
|
|
14
|
+
|
|
15
|
+
/// Wrapper around a reference to an element node.
|
|
16
|
+
///
|
|
17
|
+
/// This wrapper implements the `Element` trait from the `selectors` crate, which allows it to be
|
|
18
|
+
/// matched against CSS selectors.
|
|
19
|
+
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
20
|
+
pub struct ElementRef<'a> {
|
|
21
|
+
node: NodeRef<'a, Node>,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
impl<'a> ElementRef<'a> {
|
|
25
|
+
fn new(node: NodeRef<'a, Node>) -> Self {
|
|
26
|
+
ElementRef { node }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// Wraps a `NodeRef` only if it references a `Node::Element`.
|
|
30
|
+
pub fn wrap(node: NodeRef<'a, Node>) -> Option<Self> {
|
|
31
|
+
if node.value().is_element() {
|
|
32
|
+
Some(ElementRef::new(node))
|
|
33
|
+
} else {
|
|
34
|
+
None
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// Returns the `Element` referenced by `self`.
|
|
39
|
+
pub fn value(&self) -> &'a Element {
|
|
40
|
+
self.node.value().as_element().unwrap()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// Returns an iterator over descendent elements matching a selector.
|
|
44
|
+
pub fn select<'b>(&self, selector: &'b Selector) -> Select<'a, 'b> {
|
|
45
|
+
let mut inner = self.traverse();
|
|
46
|
+
inner.next(); // Skip Edge::Open(self).
|
|
47
|
+
|
|
48
|
+
Select {
|
|
49
|
+
scope: *self,
|
|
50
|
+
inner,
|
|
51
|
+
selector,
|
|
52
|
+
caches: Default::default(),
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
fn serialize(&self, traversal_scope: TraversalScope) -> String {
|
|
57
|
+
let opts = SerializeOpts {
|
|
58
|
+
scripting_enabled: false, // It's not clear what this does.
|
|
59
|
+
traversal_scope,
|
|
60
|
+
create_missing_parent: false,
|
|
61
|
+
};
|
|
62
|
+
let mut buf = Vec::new();
|
|
63
|
+
serialize(&mut buf, self, opts).unwrap();
|
|
64
|
+
String::from_utf8(buf).unwrap()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// Returns the HTML of this element.
|
|
68
|
+
pub fn html(&self) -> String {
|
|
69
|
+
self.serialize(TraversalScope::IncludeNode)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// Returns the inner HTML of this element.
|
|
73
|
+
pub fn inner_html(&self) -> String {
|
|
74
|
+
self.serialize(TraversalScope::ChildrenOnly(None))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// Returns the value of an attribute.
|
|
78
|
+
pub fn attr(&self, attr: &str) -> Option<&'a str> {
|
|
79
|
+
self.value().attr(attr)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/// Returns an iterator over descendent text nodes.
|
|
83
|
+
pub fn text(&self) -> Text<'a> {
|
|
84
|
+
Text {
|
|
85
|
+
inner: self.traverse(),
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// Iterate over all child nodes which are elements
|
|
90
|
+
///
|
|
91
|
+
/// # Example
|
|
92
|
+
///
|
|
93
|
+
/// ```
|
|
94
|
+
/// # use scraper::Html;
|
|
95
|
+
/// let fragment = Html::parse_fragment("foo<span>bar</span><a>baz</a>qux");
|
|
96
|
+
///
|
|
97
|
+
/// let children = fragment.root_element().child_elements().map(|element| element.value().name()).collect::<Vec<_>>();
|
|
98
|
+
/// assert_eq!(children, ["span", "a"]);
|
|
99
|
+
/// ```
|
|
100
|
+
pub fn child_elements(&self) -> impl Iterator<Item = ElementRef<'a>> {
|
|
101
|
+
self.children().filter_map(ElementRef::wrap)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/// Iterate over all descendent nodes which are elements
|
|
105
|
+
///
|
|
106
|
+
/// # Example
|
|
107
|
+
///
|
|
108
|
+
/// ```
|
|
109
|
+
/// # use scraper::Html;
|
|
110
|
+
/// let fragment = Html::parse_fragment("foo<span><b>bar</b></span><a><i>baz</i></a>qux");
|
|
111
|
+
///
|
|
112
|
+
/// let descendants = fragment.root_element().descendent_elements().map(|element| element.value().name()).collect::<Vec<_>>();
|
|
113
|
+
/// assert_eq!(descendants, ["html", "span", "b", "a", "i"]);
|
|
114
|
+
/// ```
|
|
115
|
+
pub fn descendent_elements(&self) -> impl Iterator<Item = ElementRef<'a>> {
|
|
116
|
+
self.descendants().filter_map(ElementRef::wrap)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
impl Debug for ElementRef<'_> {
|
|
121
|
+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
122
|
+
Debug::fmt(self.value(), f)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
impl<'a> Deref for ElementRef<'a> {
|
|
127
|
+
type Target = NodeRef<'a, Node>;
|
|
128
|
+
fn deref(&self) -> &NodeRef<'a, Node> {
|
|
129
|
+
&self.node
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/// Iterator over descendent elements matching a selector.
|
|
134
|
+
pub struct Select<'a, 'b> {
|
|
135
|
+
scope: ElementRef<'a>,
|
|
136
|
+
inner: Traverse<'a, Node>,
|
|
137
|
+
selector: &'b Selector,
|
|
138
|
+
caches: SelectorCaches,
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
impl Debug for Select<'_, '_> {
|
|
142
|
+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
143
|
+
fmt.debug_struct("Select")
|
|
144
|
+
.field("scope", &self.scope)
|
|
145
|
+
.field("inner", &self.inner)
|
|
146
|
+
.field("selector", &self.selector)
|
|
147
|
+
.field("caches", &"..")
|
|
148
|
+
.finish()
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
impl Clone for Select<'_, '_> {
|
|
153
|
+
fn clone(&self) -> Self {
|
|
154
|
+
Self {
|
|
155
|
+
scope: self.scope,
|
|
156
|
+
inner: self.inner.clone(),
|
|
157
|
+
selector: self.selector,
|
|
158
|
+
caches: Default::default(),
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
impl<'a> Iterator for Select<'a, '_> {
|
|
164
|
+
type Item = ElementRef<'a>;
|
|
165
|
+
|
|
166
|
+
fn next(&mut self) -> Option<ElementRef<'a>> {
|
|
167
|
+
for edge in &mut self.inner {
|
|
168
|
+
if let Edge::Open(node) = edge {
|
|
169
|
+
if let Some(element) = ElementRef::wrap(node) {
|
|
170
|
+
if self.selector.matches_with_scope_and_cache(
|
|
171
|
+
&element,
|
|
172
|
+
Some(self.scope),
|
|
173
|
+
&mut self.caches,
|
|
174
|
+
) {
|
|
175
|
+
return Some(element);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
None
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
impl FusedIterator for Select<'_, '_> {}
|
|
185
|
+
|
|
186
|
+
/// Iterator over descendent text nodes.
|
|
187
|
+
#[derive(Debug, Clone)]
|
|
188
|
+
pub struct Text<'a> {
|
|
189
|
+
inner: Traverse<'a, Node>,
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
impl<'a> Iterator for Text<'a> {
|
|
193
|
+
type Item = &'a str;
|
|
194
|
+
|
|
195
|
+
fn next(&mut self) -> Option<&'a str> {
|
|
196
|
+
for edge in &mut self.inner {
|
|
197
|
+
if let Edge::Open(node) = edge {
|
|
198
|
+
if let Node::Text(ref text) = node.value() {
|
|
199
|
+
return Some(&**text);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
None
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
impl FusedIterator for Text<'_> {}
|
|
208
|
+
|
|
209
|
+
mod element;
|
|
210
|
+
mod serializable;
|
|
211
|
+
|
|
212
|
+
#[cfg(test)]
|
|
213
|
+
mod tests {
|
|
214
|
+
use crate::html::Html;
|
|
215
|
+
use crate::selector::Selector;
|
|
216
|
+
|
|
217
|
+
#[test]
|
|
218
|
+
fn test_scope() {
|
|
219
|
+
let html = r"
|
|
220
|
+
<div>
|
|
221
|
+
<b>1</b>
|
|
222
|
+
<span>
|
|
223
|
+
<span><b>2</b></span>
|
|
224
|
+
<b>3</b>
|
|
225
|
+
</span>
|
|
226
|
+
</div>
|
|
227
|
+
";
|
|
228
|
+
let fragment = Html::parse_fragment(html);
|
|
229
|
+
let sel1 = Selector::parse("div > span").unwrap();
|
|
230
|
+
let sel2 = Selector::parse(":scope > b").unwrap();
|
|
231
|
+
|
|
232
|
+
let element1 = fragment.select(&sel1).next().unwrap();
|
|
233
|
+
let element2 = element1.select(&sel2).next().unwrap();
|
|
234
|
+
assert_eq!(element2.inner_html(), "3");
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
use std::io::Error;
|
|
2
|
+
|
|
3
|
+
use html5ever::serialize::{Serialize, Serializer, TraversalScope};
|
|
4
|
+
|
|
5
|
+
use crate::ElementRef;
|
|
6
|
+
|
|
7
|
+
impl Serialize for ElementRef<'_> {
|
|
8
|
+
fn serialize<S: Serializer>(
|
|
9
|
+
&self,
|
|
10
|
+
serializer: &mut S,
|
|
11
|
+
traversal_scope: TraversalScope,
|
|
12
|
+
) -> Result<(), Error> {
|
|
13
|
+
crate::node::serializable::serialize(**self, serializer, traversal_scope)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
use cssparser::Token;
|
|
2
|
+
|
|
3
|
+
pub(crate) fn render_token(token: &Token<'_>) -> String {
|
|
4
|
+
match token {
|
|
5
|
+
Token::Ident(ident) => ident.to_string(),
|
|
6
|
+
Token::AtKeyword(value) => format!("@{value}"),
|
|
7
|
+
Token::Hash(name) | Token::IDHash(name) => format!("#{name}"),
|
|
8
|
+
Token::QuotedString(value) => format!("\"{value}\""),
|
|
9
|
+
Token::UnquotedUrl(value) => value.to_string(),
|
|
10
|
+
Token::Number {
|
|
11
|
+
has_sign: signed,
|
|
12
|
+
value: num,
|
|
13
|
+
int_value: _,
|
|
14
|
+
}
|
|
15
|
+
| Token::Percentage {
|
|
16
|
+
has_sign: signed,
|
|
17
|
+
unit_value: num,
|
|
18
|
+
int_value: _,
|
|
19
|
+
} => render_number(*signed, *num, token),
|
|
20
|
+
Token::Dimension {
|
|
21
|
+
has_sign: signed,
|
|
22
|
+
value: num,
|
|
23
|
+
int_value: _,
|
|
24
|
+
unit,
|
|
25
|
+
} => format!("{}{}", render_int(*signed, *num), unit),
|
|
26
|
+
Token::WhiteSpace(_) => String::from(" "),
|
|
27
|
+
Token::Comment(comment) => format!("/* {comment} */"),
|
|
28
|
+
Token::Function(name) => format!("{name}()"),
|
|
29
|
+
Token::BadString(string) => format!("<Bad String {string:?}>"),
|
|
30
|
+
Token::BadUrl(url) => format!("<Bad URL {url:?}>"),
|
|
31
|
+
// Single-character token
|
|
32
|
+
Token::Colon => ":".into(),
|
|
33
|
+
Token::Semicolon => ";".into(),
|
|
34
|
+
Token::Comma => ",".into(),
|
|
35
|
+
Token::IncludeMatch => "~=".into(),
|
|
36
|
+
Token::DashMatch => "|=".into(),
|
|
37
|
+
Token::PrefixMatch => "^=".into(),
|
|
38
|
+
Token::SuffixMatch => "$=".into(),
|
|
39
|
+
Token::SubstringMatch => "*=".into(),
|
|
40
|
+
Token::CDO => "<!--".into(),
|
|
41
|
+
Token::CDC => "-->".into(),
|
|
42
|
+
Token::ParenthesisBlock => "<(".into(),
|
|
43
|
+
Token::SquareBracketBlock => "<[".into(),
|
|
44
|
+
Token::CurlyBracketBlock => "<{".into(),
|
|
45
|
+
Token::CloseParenthesis => "<)".into(),
|
|
46
|
+
Token::CloseSquareBracket => "<]".into(),
|
|
47
|
+
Token::CloseCurlyBracket => "<}".into(),
|
|
48
|
+
Token::Delim(delim) => (*delim).into(),
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
fn render_number(signed: bool, num: f32, token: &Token) -> String {
|
|
53
|
+
let num = render_int(signed, num);
|
|
54
|
+
|
|
55
|
+
match token {
|
|
56
|
+
Token::Number { .. } => num,
|
|
57
|
+
Token::Percentage { .. } => format!("{num}%"),
|
|
58
|
+
_ => panic!("render_number is not supposed to be called on a non-numerical token"),
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
fn render_int(signed: bool, num: f32) -> String {
|
|
63
|
+
if signed {
|
|
64
|
+
render_int_signed(num)
|
|
65
|
+
} else {
|
|
66
|
+
render_int_unsigned(num)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
fn render_int_signed(num: f32) -> String {
|
|
71
|
+
if num > 0.0 {
|
|
72
|
+
format!("+{num}")
|
|
73
|
+
} else {
|
|
74
|
+
format!("-{num}")
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fn render_int_unsigned(num: f32) -> String {
|
|
79
|
+
format!("{num}")
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
#[cfg(test)]
|
|
83
|
+
mod tests {
|
|
84
|
+
use crate::Selector;
|
|
85
|
+
|
|
86
|
+
#[test]
|
|
87
|
+
fn regression_test_issue212() {
|
|
88
|
+
let err = Selector::parse("div138293@!#@!!@#").unwrap_err();
|
|
89
|
+
assert_eq!(err.to_string(), "Token \"@\" was not expected");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
//! Custom error types for diagnostics
|
|
2
|
+
//! Includes re-exported error types from dependencies
|
|
3
|
+
|
|
4
|
+
mod utils;
|
|
5
|
+
|
|
6
|
+
use std::{error::Error, fmt::Display};
|
|
7
|
+
|
|
8
|
+
use cssparser::{BasicParseErrorKind, ParseErrorKind, Token};
|
|
9
|
+
use selectors::parser::SelectorParseErrorKind;
|
|
10
|
+
|
|
11
|
+
/// Error type that is returned when calling `Selector::parse`
|
|
12
|
+
#[derive(Debug, Clone)]
|
|
13
|
+
pub enum SelectorErrorKind<'a> {
|
|
14
|
+
/// A `Token` was not expected
|
|
15
|
+
UnexpectedToken(Token<'a>),
|
|
16
|
+
|
|
17
|
+
/// End-Of-Line was unexpected
|
|
18
|
+
EndOfLine,
|
|
19
|
+
|
|
20
|
+
/// `@` rule is invalid
|
|
21
|
+
InvalidAtRule(String),
|
|
22
|
+
|
|
23
|
+
/// The body of an `@` rule is invalid
|
|
24
|
+
InvalidAtRuleBody,
|
|
25
|
+
|
|
26
|
+
/// The qualified rule is invalid
|
|
27
|
+
QualRuleInvalid,
|
|
28
|
+
|
|
29
|
+
/// Expected a `::` for a pseudoelement
|
|
30
|
+
ExpectedColonOnPseudoElement(Token<'a>),
|
|
31
|
+
|
|
32
|
+
/// Expected an identity for a pseudoelement
|
|
33
|
+
ExpectedIdentityOnPseudoElement(Token<'a>),
|
|
34
|
+
|
|
35
|
+
/// A `SelectorParseErrorKind` error that isn't really supposed to happen did
|
|
36
|
+
UnexpectedSelectorParseError(SelectorParseErrorKind<'a>),
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
impl<'a> From<cssparser::ParseError<'a, SelectorParseErrorKind<'a>>> for SelectorErrorKind<'a> {
|
|
40
|
+
fn from(original: cssparser::ParseError<'a, SelectorParseErrorKind<'a>>) -> Self {
|
|
41
|
+
// NOTE: This could be improved, but I dont
|
|
42
|
+
// exactly know how
|
|
43
|
+
match original.kind {
|
|
44
|
+
ParseErrorKind::Basic(err) => SelectorErrorKind::from(err),
|
|
45
|
+
ParseErrorKind::Custom(err) => SelectorErrorKind::from(err),
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
impl<'a> From<BasicParseErrorKind<'a>> for SelectorErrorKind<'a> {
|
|
51
|
+
fn from(err: BasicParseErrorKind<'a>) -> Self {
|
|
52
|
+
match err {
|
|
53
|
+
BasicParseErrorKind::UnexpectedToken(token) => Self::UnexpectedToken(token),
|
|
54
|
+
BasicParseErrorKind::EndOfInput => Self::EndOfLine,
|
|
55
|
+
BasicParseErrorKind::AtRuleInvalid(rule) => Self::InvalidAtRule(rule.to_string()),
|
|
56
|
+
BasicParseErrorKind::AtRuleBodyInvalid => Self::InvalidAtRuleBody,
|
|
57
|
+
BasicParseErrorKind::QualifiedRuleInvalid => Self::QualRuleInvalid,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
impl<'a> From<SelectorParseErrorKind<'a>> for SelectorErrorKind<'a> {
|
|
63
|
+
fn from(err: SelectorParseErrorKind<'a>) -> Self {
|
|
64
|
+
match err {
|
|
65
|
+
SelectorParseErrorKind::PseudoElementExpectedColon(token) => {
|
|
66
|
+
Self::ExpectedColonOnPseudoElement(token)
|
|
67
|
+
}
|
|
68
|
+
SelectorParseErrorKind::PseudoElementExpectedIdent(token) => {
|
|
69
|
+
Self::ExpectedIdentityOnPseudoElement(token)
|
|
70
|
+
}
|
|
71
|
+
other => Self::UnexpectedSelectorParseError(other),
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
impl Display for SelectorErrorKind<'_> {
|
|
77
|
+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
78
|
+
write!(
|
|
79
|
+
f,
|
|
80
|
+
"{}",
|
|
81
|
+
match self {
|
|
82
|
+
Self::UnexpectedToken(token) => {
|
|
83
|
+
format!("Token {:?} was not expected", utils::render_token(token))
|
|
84
|
+
}
|
|
85
|
+
Self::EndOfLine => "Unexpected EOL".to_string(),
|
|
86
|
+
Self::InvalidAtRule(rule) => format!("Invalid @-rule {rule:?}"),
|
|
87
|
+
Self::InvalidAtRuleBody => "The body of an @-rule was invalid".to_string(),
|
|
88
|
+
Self::QualRuleInvalid => "The qualified name was invalid".to_string(),
|
|
89
|
+
Self::ExpectedColonOnPseudoElement(token) => format!(
|
|
90
|
+
"Expected a ':' token for pseudoelement, got {:?} instead",
|
|
91
|
+
utils::render_token(token)
|
|
92
|
+
),
|
|
93
|
+
Self::ExpectedIdentityOnPseudoElement(token) => format!(
|
|
94
|
+
"Expected identity for pseudoelement, got {:?} instead",
|
|
95
|
+
utils::render_token(token)
|
|
96
|
+
),
|
|
97
|
+
Self::UnexpectedSelectorParseError(err) => format!(
|
|
98
|
+
"Unexpected error occurred. Please report this to the developer\n{err:#?}"
|
|
99
|
+
),
|
|
100
|
+
}
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
impl Error for SelectorErrorKind<'_> {
|
|
106
|
+
fn description(&self) -> &str {
|
|
107
|
+
match self {
|
|
108
|
+
Self::UnexpectedToken(_) => "Token was not expected",
|
|
109
|
+
Self::EndOfLine => "Unexpected EOL",
|
|
110
|
+
Self::InvalidAtRule(_) => "Invalid @-rule",
|
|
111
|
+
Self::InvalidAtRuleBody => "The body of an @-rule was invalid",
|
|
112
|
+
Self::QualRuleInvalid => "The qualified name was invalid",
|
|
113
|
+
Self::ExpectedColonOnPseudoElement(_) => "Missing colon character on pseudoelement",
|
|
114
|
+
Self::ExpectedIdentityOnPseudoElement(_) => "Missing pseudoelement identity",
|
|
115
|
+
Self::UnexpectedSelectorParseError(_) => "Unexpected error",
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|