@dvaji/cheerox 0.1.1 → 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/Cargo.lock +1 -4
- package/Cargo.toml +3 -2
- package/THIRD_PARTY_NOTICES.md +5 -3
- 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-BS--qPzw.mjs → cheerio-compat-BKkfJEAr.mjs} +2 -2
- package/dist/{cheerio-compat-BS--qPzw.mjs.map → cheerio-compat-BKkfJEAr.mjs.map} +1 -1
- package/dist/{cheerio-compat-DSwBAJ1s.cjs → cheerio-compat-DcnYK9OB.cjs} +2 -2
- package/dist/{cheerio-compat-DSwBAJ1s.cjs.map → cheerio-compat-DcnYK9OB.cjs.map} +1 -1
- package/dist/cheerio-compat.cjs +1 -1
- package/dist/cheerio-compat.mjs +1 -1
- package/native/src/lib.rs +2 -1
- package/native/vendor/README.md +12 -3
- 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 +1 -1
- package/scripts/check-release.mjs +2 -2
- package/scripts/generate-notices.mjs +10 -4
package/dist/cheerio-compat.cjs
CHANGED
|
@@ -2,7 +2,7 @@ Object.defineProperties(exports, {
|
|
|
2
2
|
__esModule: { value: true },
|
|
3
3
|
[Symbol.toStringTag]: { value: "Module" }
|
|
4
4
|
});
|
|
5
|
-
const require_cheerio_compat = require("./cheerio-compat-
|
|
5
|
+
const require_cheerio_compat = require("./cheerio-compat-DcnYK9OB.cjs");
|
|
6
6
|
exports.cheerio = require_cheerio_compat.moduleCheerio;
|
|
7
7
|
exports.default = require_cheerio_compat.moduleCheerio;
|
|
8
8
|
exports.contains = require_cheerio_compat.staticContains;
|
package/dist/cheerio-compat.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { r as cheerio, r as default, a as contains, t as evaluate, s as html, o as legacyStatics, n as load, i as parse, c as parseHTML, l as root, u as text, f as update, d as xml } from "./cheerio-compat-
|
|
1
|
+
export { r as cheerio, r as default, a as contains, t as evaluate, s as html, o as legacyStatics, n as load, i as parse, c as parseHTML, l as root, u as text, f as update, d as xml } from "./cheerio-compat-BKkfJEAr.mjs";
|
package/native/src/lib.rs
CHANGED
package/native/vendor/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
# Vendored HTML5 integration hooks
|
|
1
|
+
# Vendored HTML5 integration hooks and attribute storage
|
|
2
2
|
|
|
3
3
|
These directories contain html5ever 0.35.0, markup5ever 0.35.0 and html5gum 0.8.4,
|
|
4
4
|
with their original license files (MIT OR Apache-2.0 for html5ever/markup5ever;
|
|
5
|
-
MIT for html5gum).
|
|
5
|
+
MIT for html5gum), and scraper 0.24.0 with its original license file (ISC).
|
|
6
|
+
Their exact patched sources ship in
|
|
6
7
|
the npm package and are selected by the root Cargo manifest's
|
|
7
8
|
`[patch.crates-io]` entries.
|
|
8
9
|
|
|
@@ -19,8 +20,16 @@ representation of lone JavaScript UTF-16 surrogates.
|
|
|
19
20
|
The flag is disabled for upstream users of these crates. The original HTML5
|
|
20
21
|
character-reference decoding rules remain in place.
|
|
21
22
|
|
|
23
|
+
The vendored scraper keeps its public surface but replaces the
|
|
24
|
+
`deterministic`-feature `indexmap::IndexMap` attribute storage with an
|
|
25
|
+
insertion-ordered vector-backed map: elements hold a handful of attributes, so
|
|
26
|
+
one ordered insertion costs less than a hash per attribute. Duplicate
|
|
27
|
+
attributes keep the first value, as the HTML tree builder requires, and the
|
|
28
|
+
`main`, `deterministic` and `default` features, the `getopts`/`indexmap`
|
|
29
|
+
dependencies, the binary and the examples are removed.
|
|
22
30
|
The patches preserve the HTML5 tree-building algorithms. Review changes against
|
|
23
31
|
the original [html5ever archive](https://crates.io/api/v1/crates/html5ever/0.35.0/download)
|
|
24
32
|
and [markup5ever archive](https://crates.io/api/v1/crates/markup5ever/0.35.0/download),
|
|
25
|
-
|
|
33
|
+
the [html5gum archive](https://crates.io/api/v1/crates/html5gum/0.8.4/download),
|
|
34
|
+
and the [scraper archive](https://crates.io/api/v1/crates/scraper/0.24.0/download).
|
|
26
35
|
Do not remove or edit the original third-party notices when updating these sources.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Vendored from crates.io scraper 0.24.0.
|
|
2
|
+
#
|
|
3
|
+
# Local changes versus the published crate, matching cheerox's needs:
|
|
4
|
+
# - `Attributes` is an insertion-ordered vector-backed map instead of
|
|
5
|
+
# `indexmap::IndexMap`, removing per-element hashing and index allocation
|
|
6
|
+
# from HTML tree construction.
|
|
7
|
+
# - The `main`, `deterministic` and `default` features, the `getopts` and
|
|
8
|
+
# `indexmap` dependencies, the binary and the examples are removed.
|
|
9
|
+
[package]
|
|
10
|
+
edition = "2021"
|
|
11
|
+
name = "scraper"
|
|
12
|
+
version = "0.24.0"
|
|
13
|
+
license = "ISC"
|
|
14
|
+
repository = "https://github.com/causal-agent/scraper"
|
|
15
|
+
description = "HTML parsing and querying with CSS selectors (vendored, vector-backed attributes)"
|
|
16
|
+
build = false
|
|
17
|
+
autolib = false
|
|
18
|
+
autobins = false
|
|
19
|
+
autoexamples = false
|
|
20
|
+
autotests = false
|
|
21
|
+
autobenches = false
|
|
22
|
+
|
|
23
|
+
[lib]
|
|
24
|
+
name = "scraper"
|
|
25
|
+
path = "src/lib.rs"
|
|
26
|
+
|
|
27
|
+
[features]
|
|
28
|
+
atomic = []
|
|
29
|
+
errors = []
|
|
30
|
+
serde = ["dep:serde"]
|
|
31
|
+
|
|
32
|
+
[dependencies.cssparser]
|
|
33
|
+
version = "0.35.0"
|
|
34
|
+
|
|
35
|
+
[dependencies.ego-tree]
|
|
36
|
+
version = "0.10.0"
|
|
37
|
+
|
|
38
|
+
[dependencies.html5ever]
|
|
39
|
+
version = "0.35.0"
|
|
40
|
+
|
|
41
|
+
[dependencies.precomputed-hash]
|
|
42
|
+
version = "0.1.1"
|
|
43
|
+
|
|
44
|
+
[dependencies.selectors]
|
|
45
|
+
version = "0.31.0"
|
|
46
|
+
|
|
47
|
+
[dependencies.serde]
|
|
48
|
+
version = "1.0.218"
|
|
49
|
+
optional = true
|
|
50
|
+
|
|
51
|
+
[dependencies.tendril]
|
|
52
|
+
version = "0.4.3"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Copyright © 2016, June McEnroe <june@causal.agency>
|
|
2
|
+
Copyright © 2017, Vivek Kushwaha <yoursvivek@gmail.com>
|
|
3
|
+
Copyright © 2024-2025, rust-scraper Contributors
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
use html5ever::Namespace;
|
|
2
|
+
use selectors::{
|
|
3
|
+
attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint},
|
|
4
|
+
bloom::BloomFilter,
|
|
5
|
+
matching, Element, OpaqueElement,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
use super::ElementRef;
|
|
9
|
+
use crate::selector::{CssLocalName, CssString, NonTSPseudoClass, PseudoElement, Simple};
|
|
10
|
+
|
|
11
|
+
/// Note: will never match against non-tree-structure pseudo-classes.
|
|
12
|
+
impl Element for ElementRef<'_> {
|
|
13
|
+
type Impl = Simple;
|
|
14
|
+
|
|
15
|
+
fn opaque(&self) -> OpaqueElement {
|
|
16
|
+
OpaqueElement::new(self.node.value())
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fn parent_element(&self) -> Option<Self> {
|
|
20
|
+
self.parent().and_then(ElementRef::wrap)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
fn parent_node_is_shadow_root(&self) -> bool {
|
|
24
|
+
false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fn containing_shadow_host(&self) -> Option<Self> {
|
|
28
|
+
None
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
fn is_pseudo_element(&self) -> bool {
|
|
32
|
+
false
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn is_part(&self, _name: &CssLocalName) -> bool {
|
|
36
|
+
false
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
fn is_same_type(&self, other: &Self) -> bool {
|
|
40
|
+
self.value().name == other.value().name
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
fn imported_part(&self, _: &CssLocalName) -> Option<CssLocalName> {
|
|
44
|
+
None
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fn prev_sibling_element(&self) -> Option<Self> {
|
|
48
|
+
self.prev_siblings()
|
|
49
|
+
.find(|sibling| sibling.value().is_element())
|
|
50
|
+
.map(ElementRef::new)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fn next_sibling_element(&self) -> Option<Self> {
|
|
54
|
+
self.next_siblings()
|
|
55
|
+
.find(|sibling| sibling.value().is_element())
|
|
56
|
+
.map(ElementRef::new)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fn first_element_child(&self) -> Option<Self> {
|
|
60
|
+
self.children()
|
|
61
|
+
.find(|child| child.value().is_element())
|
|
62
|
+
.map(ElementRef::new)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fn is_html_element_in_html_document(&self) -> bool {
|
|
66
|
+
// FIXME: Is there more to this?
|
|
67
|
+
self.value().name.ns == ns!(html)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
fn has_local_name(&self, name: &CssLocalName) -> bool {
|
|
71
|
+
self.value().name.local == name.0
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
fn has_namespace(&self, namespace: &Namespace) -> bool {
|
|
75
|
+
&self.value().name.ns == namespace
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fn attr_matches(
|
|
79
|
+
&self,
|
|
80
|
+
ns: &NamespaceConstraint<&Namespace>,
|
|
81
|
+
local_name: &CssLocalName,
|
|
82
|
+
operation: &AttrSelectorOperation<&CssString>,
|
|
83
|
+
) -> bool {
|
|
84
|
+
self.value().attrs.iter().any(|(key, value)| {
|
|
85
|
+
!matches!(*ns, NamespaceConstraint::Specific(url) if *url != key.ns)
|
|
86
|
+
&& local_name.0 == key.local
|
|
87
|
+
&& operation.eval_str(value)
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn match_non_ts_pseudo_class(
|
|
92
|
+
&self,
|
|
93
|
+
_pc: &NonTSPseudoClass,
|
|
94
|
+
_context: &mut matching::MatchingContext<'_, Self::Impl>,
|
|
95
|
+
) -> bool {
|
|
96
|
+
false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fn match_pseudo_element(
|
|
100
|
+
&self,
|
|
101
|
+
_pe: &PseudoElement,
|
|
102
|
+
_context: &mut matching::MatchingContext<Self::Impl>,
|
|
103
|
+
) -> bool {
|
|
104
|
+
false
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fn is_link(&self) -> bool {
|
|
108
|
+
self.value().name() == "link"
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
fn is_html_slot_element(&self) -> bool {
|
|
112
|
+
true
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
fn has_id(&self, id: &CssLocalName, case_sensitivity: CaseSensitivity) -> bool {
|
|
116
|
+
match self.value().id() {
|
|
117
|
+
Some(val) => case_sensitivity.eq(id.0.as_bytes(), val.as_bytes()),
|
|
118
|
+
None => false,
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fn has_class(&self, name: &CssLocalName, case_sensitivity: CaseSensitivity) -> bool {
|
|
123
|
+
self.value().has_class(&name.0, case_sensitivity)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
fn has_custom_state(&self, _name: &CssLocalName) -> bool {
|
|
127
|
+
false
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
fn is_empty(&self) -> bool {
|
|
131
|
+
!self
|
|
132
|
+
.children()
|
|
133
|
+
.any(|child| child.value().is_element() || child.value().is_text())
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
fn is_root(&self) -> bool {
|
|
137
|
+
self.parent()
|
|
138
|
+
.is_some_and(|parent| parent.value().is_document())
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
fn apply_selector_flags(&self, _flags: matching::ElementSelectorFlags) {}
|
|
142
|
+
|
|
143
|
+
fn add_element_unique_hashes(&self, _filter: &mut BloomFilter) -> bool {
|
|
144
|
+
// FIXME: Do we want to add `self.node.id()` here?
|
|
145
|
+
false
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
#[cfg(test)]
|
|
150
|
+
mod tests {
|
|
151
|
+
use crate::html::Html;
|
|
152
|
+
use crate::selector::{CssLocalName, Selector};
|
|
153
|
+
use selectors::attr::CaseSensitivity;
|
|
154
|
+
use selectors::Element;
|
|
155
|
+
|
|
156
|
+
#[test]
|
|
157
|
+
fn test_has_id() {
|
|
158
|
+
let html = "<p id='link_id_456'>hey there</p>";
|
|
159
|
+
let fragment = Html::parse_fragment(html);
|
|
160
|
+
let sel = Selector::parse("p").unwrap();
|
|
161
|
+
|
|
162
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
163
|
+
assert!(element.has_id(
|
|
164
|
+
&CssLocalName::from("link_id_456"),
|
|
165
|
+
CaseSensitivity::CaseSensitive
|
|
166
|
+
));
|
|
167
|
+
|
|
168
|
+
let html = "<p>hey there</p>";
|
|
169
|
+
let fragment = Html::parse_fragment(html);
|
|
170
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
171
|
+
assert!(!element.has_id(
|
|
172
|
+
&CssLocalName::from("any_link_id"),
|
|
173
|
+
CaseSensitivity::CaseSensitive
|
|
174
|
+
));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#[test]
|
|
178
|
+
fn test_is_link() {
|
|
179
|
+
let html = "<link href='https://www.example.com'>";
|
|
180
|
+
let fragment = Html::parse_fragment(html);
|
|
181
|
+
let sel = Selector::parse("link").unwrap();
|
|
182
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
183
|
+
assert!(element.is_link());
|
|
184
|
+
|
|
185
|
+
let html = "<p>hey there</p>";
|
|
186
|
+
let fragment = Html::parse_fragment(html);
|
|
187
|
+
let sel = Selector::parse("p").unwrap();
|
|
188
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
189
|
+
assert!(!element.is_link());
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
#[test]
|
|
193
|
+
fn test_has_class() {
|
|
194
|
+
let html = "<p class='my_class'>hey there</p>";
|
|
195
|
+
let fragment = Html::parse_fragment(html);
|
|
196
|
+
let sel = Selector::parse("p").unwrap();
|
|
197
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
198
|
+
assert!(element.has_class(
|
|
199
|
+
&CssLocalName::from("my_class"),
|
|
200
|
+
CaseSensitivity::CaseSensitive
|
|
201
|
+
));
|
|
202
|
+
|
|
203
|
+
let html = "<p>hey there</p>";
|
|
204
|
+
let fragment = Html::parse_fragment(html);
|
|
205
|
+
let sel = Selector::parse("p").unwrap();
|
|
206
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
207
|
+
assert!(!element.has_class(
|
|
208
|
+
&CssLocalName::from("my_class"),
|
|
209
|
+
CaseSensitivity::CaseSensitive
|
|
210
|
+
));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -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
|
+
}
|