@dvaji/cheerox 0.1.1 → 0.1.3
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-DSwBAJ1s.cjs → cheerio-compat-8HucV2qE.cjs} +9 -7
- package/dist/cheerio-compat-8HucV2qE.cjs.map +1 -0
- package/dist/{cheerio-compat-DRLPzBdX.d.mts → cheerio-compat-BM4_lqVU.d.mts} +3 -3
- package/dist/{cheerio-compat-DRsINTwV.d.ts → cheerio-compat-BnFvIIuE.d.cts} +3 -3
- package/dist/{cheerio-compat-DRsINTwV.d.cts → cheerio-compat-BnFvIIuE.d.ts} +3 -3
- package/dist/{cheerio-compat-BS--qPzw.mjs → cheerio-compat-Dwarm-h8.mjs} +9 -7
- package/dist/cheerio-compat-Dwarm-h8.mjs.map +1 -0
- package/dist/cheerio-compat.cjs +1 -1
- package/dist/cheerio-compat.d.cts +1 -1
- package/dist/cheerio-compat.d.mts +1 -1
- package/dist/cheerio-compat.d.ts +1 -1
- package/dist/cheerio-compat.mjs +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/{modern-CvXcTn82.mjs → modern-BwcwZX6l.mjs} +2 -2
- package/dist/{modern-CvXcTn82.mjs.map → modern-BwcwZX6l.mjs.map} +1 -1
- package/dist/{modern-CHQGP0Yt.cjs → modern-DMcsyA4b.cjs} +2 -2
- package/dist/{modern-CHQGP0Yt.cjs.map → modern-DMcsyA4b.cjs.map} +1 -1
- package/dist/modern.cjs +2 -2
- package/dist/modern.mjs +2 -2
- package/dist/{nodes-jbWWhssj.cjs → nodes-Be5sB8rQ.cjs} +23 -15
- package/dist/nodes-Be5sB8rQ.cjs.map +1 -0
- package/dist/{nodes-sYxb5qyZ.mjs → nodes-C95i5k2P.mjs} +23 -15
- package/dist/nodes-C95i5k2P.mjs.map +1 -0
- package/dist/slim.cjs +1 -1
- package/dist/slim.mjs +1 -1
- package/native/src/lib.rs +509 -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/src/cheerio-compat.ts +14 -4
- package/src/native.ts +23 -9
- package/dist/cheerio-compat-BS--qPzw.mjs.map +0 -1
- package/dist/cheerio-compat-DSwBAJ1s.cjs.map +0 -1
- package/dist/nodes-jbWWhssj.cjs.map +0 -1
- package/dist/nodes-sYxb5qyZ.mjs.map +0 -1
|
@@ -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
|
@@ -114,7 +114,7 @@ export function validateNativeBinary(name, data) {
|
|
|
114
114
|
function allowedFile(path) {
|
|
115
115
|
return requiredFiles.includes(path) || /^dist\/[\w./-]+\.(?:[cm]?js|map|[cm]?ts)$/.test(path)
|
|
116
116
|
|| /^(?:src\/[\w/-]+\.ts|native\/src\/[\w/-]+\.rs)$/.test(path)
|
|
117
|
-
|| /^native\/vendor\/(?:(?:html5ever|markup5ever)-0\.35\.0|html5gum-0\.8\.4)\/(?:[\w./-]+\.(?:rs|c|h|html|md|toml|orig|json)|Cargo\.lock|LICENSE(?:-(?:MIT|APACHE))?)$/.test(path)
|
|
117
|
+
|| /^native\/vendor\/(?:(?:html5ever|markup5ever)-0\.35\.0|html5gum-0\.8\.4|scraper-0\.24\.0)\/(?:[\w./-]+\.(?:rs|c|h|html|md|toml|orig|json)|Cargo\.lock|LICENSE(?:-(?:MIT|APACHE))?)$/.test(path)
|
|
118
118
|
|| /^scripts\/notices\/[\w-]+\.(?:txt|md|json)$/.test(path)
|
|
119
119
|
|| nativeTargets.some(target => path === `cheerox.${target}.node`);
|
|
120
120
|
}
|
|
@@ -125,7 +125,7 @@ export function validatePackageFiles(files, { allTargets = true } = {}) {
|
|
|
125
125
|
assert.ok(allowedFile(path), `Unexpected package file: ${path}`);
|
|
126
126
|
}
|
|
127
127
|
for (const path of requiredFiles) assert.ok(files.get(path)?.length > 0, `Package is missing ${path}`);
|
|
128
|
-
for (const path of ['native/vendor/html5gum-0.8.4/Cargo.toml', 'native/vendor/html5gum-0.8.4/src/lib.rs', 'native/vendor/html5gum-0.8.4/LICENSE']) assert.ok(files.get(path)?.length > 0, `Package is missing ${path}`);
|
|
128
|
+
for (const path of ['native/vendor/html5gum-0.8.4/Cargo.toml', 'native/vendor/html5gum-0.8.4/src/lib.rs', 'native/vendor/html5gum-0.8.4/LICENSE', 'native/vendor/scraper-0.24.0/Cargo.toml', 'native/vendor/scraper-0.24.0/src/lib.rs', 'native/vendor/scraper-0.24.0/src/node.rs', 'native/vendor/scraper-0.24.0/LICENSE']) assert.ok(files.get(path)?.length > 0, `Package is missing ${path}`);
|
|
129
129
|
assert.match(files.get('LICENSE').toString(), /MIT License/);
|
|
130
130
|
assert.match(files.get('THIRD_PARTY_NOTICES.md').toString(), /cheerio/i);
|
|
131
131
|
assert.match(files.get('README.md').toString(), /drop-in replacement/i);
|
|
@@ -18,7 +18,7 @@ const legalName = /^(?:licen[cs]e(?:[-_.].*)?|unlicense|copying(?:[-_.].*)?|copy
|
|
|
18
18
|
const metadata = JSON.parse(execFileSync(process.env.CARGO || 'cargo', [
|
|
19
19
|
'metadata', '--locked', '--format-version', '1',
|
|
20
20
|
], { cwd: root, encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 }));
|
|
21
|
-
const vendored = new Set(['html5ever@0.35.0', 'markup5ever@0.35.0', 'html5gum@0.8.4']);
|
|
21
|
+
const vendored = new Set(['html5ever@0.35.0', 'markup5ever@0.35.0', 'html5gum@0.8.4', 'scraper@0.24.0']);
|
|
22
22
|
// The Rust crate keeps its internal name `cheerox`; the npm package is `@dvaji/cheerox`.
|
|
23
23
|
for (const pkg of metadata.packages) {
|
|
24
24
|
if (!pkg.source && pkg.name !== 'cheerox' && !vendored.has(`${pkg.name}@${pkg.version}`)) throw new Error(`Review notices for the new local dependency ${pkg.name}@${pkg.version}`);
|
|
@@ -118,7 +118,7 @@ const parts = [
|
|
|
118
118
|
'',
|
|
119
119
|
'This file preserves third-party license and attribution notices for the Rust native addon and the upstream Cheerio compatibility tests. cheerox itself is distributed under its MIT LICENSE. Third-party components retain their own licenses.',
|
|
120
120
|
'',
|
|
121
|
-
'Generated by `node scripts/generate-notices.mjs` from `cargo metadata --locked`, crate license files, and the pinned supplemental sources recorded in `scripts/notices/sources.json`. It includes build dependencies and dependencies for all target platforms; a particular binary may use a subset. Identical notice texts are reproduced once and referenced below. The html5ever, markup5ever and html5gum crates contain local observer-hook and opt-in UTF-16 transport patches; their complete patched source and original notices are included in `native/vendor`. Other dependency source files are unmodified.',
|
|
121
|
+
'Generated by `node scripts/generate-notices.mjs` from `cargo metadata --locked`, crate license files, and the pinned supplemental sources recorded in `scripts/notices/sources.json`. It includes build dependencies and dependencies for all target platforms; a particular binary may use a subset. Identical notice texts are reproduced once and referenced below. The html5ever, markup5ever and html5gum crates contain local observer-hook and opt-in UTF-16 transport patches, and the scraper crate carries a vector-backed attribute store; their complete patched source and original notices are included in `native/vendor`. Other dependency source files are unmodified.',
|
|
122
122
|
'',
|
|
123
123
|
`Cargo.lock SHA-256: \`${sha256(readFileSync(join(root, 'Cargo.lock')))}\`.`,
|
|
124
124
|
'',
|
|
@@ -126,7 +126,7 @@ const parts = [
|
|
|
126
126
|
'',
|
|
127
127
|
'The native addon includes code from packages licensed under MPL-2.0. The complete, unmodified Source Code Form of each dependency is available without charge from the version-specific source download links below. These downloads contain the corresponding upstream source and notices. MPL-covered source remains available under MPL-2.0, whose full text is included in this file. The MIT license for cheerox does not restrict rights granted by third-party source licenses.',
|
|
128
128
|
'',
|
|
129
|
-
'The locally patched html5ever and markup5ever components are licensed under MIT OR Apache-2.0; html5gum is licensed under MIT. Their upstream archives are linked below; the exact patched Source Code Form is shipped in the npm package under `native/vendor` and maintained in [this repository](https://github.com/dvaJi/cheerox/tree/main/native/vendor). See `native/vendor/README.md` for the patch scope. Their original license and copyright files remain intact.',
|
|
129
|
+
'The locally patched html5ever and markup5ever components are licensed under MIT OR Apache-2.0; html5gum is licensed under MIT; the locally patched scraper is licensed under ISC. Their upstream archives are linked below; the exact patched Source Code Form is shipped in the npm package under `native/vendor` and maintained in [this repository](https://github.com/dvaJi/cheerox/tree/main/native/vendor). See `native/vendor/README.md` for the patch scope. Their original license and copyright files remain intact.',
|
|
130
130
|
'',
|
|
131
131
|
'## Cheerio compatibility tests',
|
|
132
132
|
'',
|
|
@@ -180,7 +180,13 @@ for (const [digest, text] of [...texts].sort(([a], [b]) => compare(a, b))) {
|
|
|
180
180
|
}
|
|
181
181
|
const generated = parts.join('\n') + '\n';
|
|
182
182
|
if (check) {
|
|
183
|
-
if (!existsSync(output) || read(output) !== generated)
|
|
183
|
+
if (!existsSync(output) || read(output) !== generated) {
|
|
184
|
+
const existing = existsSync(output) ? read(output) : '';
|
|
185
|
+
let at = 0;
|
|
186
|
+
while (at < existing.length && at < generated.length && existing[at] === generated[at]) at++;
|
|
187
|
+
const context = (text) => text.slice(at, at + 160).replace(/\n/g, '\\n');
|
|
188
|
+
throw new Error(`THIRD_PARTY_NOTICES.md is stale at byte ${at}. Committed: "${context(existing)}" Generated: "${context(generated)}". Run npm run notices:generate and commit the updated file.`);
|
|
189
|
+
}
|
|
184
190
|
console.log(`Third-party notices match ${records.length} locked Rust packages and Cheerio 1.2.0.`);
|
|
185
191
|
} else {
|
|
186
192
|
writeFileSync(output, generated);
|
package/src/cheerio-compat.ts
CHANGED
|
@@ -181,13 +181,13 @@ export function update(arrInput: unknown, parent: LegacyNode | null): LegacyNode
|
|
|
181
181
|
}
|
|
182
182
|
|
|
183
183
|
/** lib/parse.js: parse into a fresh document and return its `<root>` node. */
|
|
184
|
-
export function parse(content: string | Buffer | ArrayLike<LegacyNode> | LegacyNode, options?: LegacyOptions | null): LegacyNode {
|
|
184
|
+
export function parse(content: string | Buffer | ArrayLike<LegacyNode> | LegacyNode, options?: LegacyOptions | null, isDocument?: boolean): LegacyNode {
|
|
185
185
|
const opts = legacyOptions(options);
|
|
186
186
|
let document: NativeDocument;
|
|
187
187
|
let root: LegacyNode;
|
|
188
188
|
if (typeof content === 'string' || Buffer.isBuffer(content)) {
|
|
189
189
|
document = routeDocument(
|
|
190
|
-
new (requireNative().NativeDocument)(Buffer.isBuffer(content) ? content.toString() : content, false, JSON.stringify(cleanParserOptions(opts))),
|
|
190
|
+
new (requireNative().NativeDocument)(Buffer.isBuffer(content) ? content.toString() : content, isDocument ?? false, JSON.stringify(cleanParserOptions(opts))),
|
|
191
191
|
opts,
|
|
192
192
|
);
|
|
193
193
|
root = rootNode(document, document.rootId()) as LegacyNode;
|
|
@@ -636,6 +636,16 @@ const attributesApi: CheerioPrototype = {
|
|
|
636
636
|
case 'tagName':
|
|
637
637
|
case 'nodeName':
|
|
638
638
|
return this[0].name!.toUpperCase();
|
|
639
|
+
// As in cheerio 1.x: these render through the facade's serializer so
|
|
640
|
+
// the current render options apply.
|
|
641
|
+
case 'innerHTML':
|
|
642
|
+
return this[0] && isTag(this[0])
|
|
643
|
+
? staticHtml.call(this, (this[0] as LegacyNode).children, this.options)
|
|
644
|
+
: null;
|
|
645
|
+
case 'outerHTML':
|
|
646
|
+
return this[0] && isTag(this[0])
|
|
647
|
+
? staticHtml.call(this, [this[0] as LegacyNode], this.options)
|
|
648
|
+
: null;
|
|
639
649
|
default:
|
|
640
650
|
return getProp(this[0], name);
|
|
641
651
|
}
|
|
@@ -1989,12 +1999,12 @@ function staticContains(container?: LegacyNode, contained?: LegacyNode): boolean
|
|
|
1989
1999
|
}
|
|
1990
2000
|
|
|
1991
2001
|
/** $.load(str) */
|
|
1992
|
-
function load(content: string | Buffer | ArrayLike<LegacyNode> | LegacyNode, options?: LegacyOptions | null): CheerioStatic {
|
|
2002
|
+
function load(content: string | Buffer | ArrayLike<LegacyNode> | LegacyNode, options?: LegacyOptions | null, isDocument?: boolean): CheerioStatic {
|
|
1993
2003
|
const Cheerio = moduleCheerio;
|
|
1994
2004
|
|
|
1995
2005
|
options = defaults({ ...(options ?? {}) }, Cheerio.prototype.options) as LegacyOptions;
|
|
1996
2006
|
|
|
1997
|
-
const root = parse(content as string | Buffer, options);
|
|
2007
|
+
const root = parse(content as string | Buffer, options, isDocument ?? true);
|
|
1998
2008
|
|
|
1999
2009
|
const initialize = function (this: CheerioInstance, selector?: any, context?: any, r?: any, opts?: LegacyOptions) {
|
|
2000
2010
|
if (!(this instanceof initialize)) {
|
package/src/native.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
|
-
import { existsSync } from 'node:fs';
|
|
3
2
|
import { dirname, join } from 'node:path';
|
|
4
3
|
import { fileURLToPath } from 'node:url';
|
|
5
4
|
import { wrapNativeBinding } from './transport.js';
|
|
@@ -104,6 +103,28 @@ export function packageDirectory(): string {
|
|
|
104
103
|
return typeof __dirname === 'string' ? __dirname : dirname(fileURLToPath(import.meta.url));
|
|
105
104
|
}
|
|
106
105
|
|
|
106
|
+
/** The platform directory component used by binary names and companion packages. */
|
|
107
|
+
export function platformComponent(): string {
|
|
108
|
+
const libc = process.platform === 'linux'
|
|
109
|
+
? ((process.report?.getReport() as { header?: { glibcVersionRuntime?: string } })?.header?.glibcVersionRuntime ? 'gnu' : 'musl')
|
|
110
|
+
: undefined;
|
|
111
|
+
return `${process.platform}-${process.arch}${libc ? `-${libc}` : process.platform === 'win32' ? '-msvc' : ''}`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Where the native addon may be found, most preferred first: the
|
|
116
|
+
* platform-specific companion package published alongside the main package
|
|
117
|
+
* (`@dvaji/cheerox-<platform>`), then the binaries bundled with the package
|
|
118
|
+
* (or present in a parent directory for source checkouts).
|
|
119
|
+
*/
|
|
120
|
+
export function nativeBindingCandidates(platform: string, directories: string[]): string[] {
|
|
121
|
+
const names = [`cheerox.${platform}.node`, 'cheerox.node'];
|
|
122
|
+
return [
|
|
123
|
+
`@dvaji/cheerox-${platform}`,
|
|
124
|
+
...new Set(directories.flatMap((directory) => names.map((name) => join(directory, name)))),
|
|
125
|
+
];
|
|
126
|
+
}
|
|
127
|
+
|
|
107
128
|
/** Resolve the native addon bundled with the npm package. */
|
|
108
129
|
export function nativeBinding(): NativeBinding | undefined {
|
|
109
130
|
if (attempted) return cached;
|
|
@@ -114,15 +135,8 @@ export function nativeBinding(): NativeBinding | undefined {
|
|
|
114
135
|
const moduleDirectory = packageDirectory();
|
|
115
136
|
const require = createRequire(join(moduleDirectory, 'cheerox-loader.cjs'));
|
|
116
137
|
const directories = [moduleDirectory, dirname(moduleDirectory)];
|
|
117
|
-
const libc = process.platform === 'linux'
|
|
118
|
-
? ((process.report?.getReport() as { header?: { glibcVersionRuntime?: string } })?.header?.glibcVersionRuntime ? 'gnu' : 'musl')
|
|
119
|
-
: undefined;
|
|
120
|
-
const platform = `${process.platform}-${process.arch}${libc ? `-${libc}` : process.platform === 'win32' ? '-msvc' : ''}`;
|
|
121
|
-
const names = [`cheerox.${platform}.node`, 'cheerox.node'];
|
|
122
|
-
const candidates = [...new Set(directories.flatMap((directory) => names.map((name) => join(directory, name))))];
|
|
123
138
|
|
|
124
|
-
for (const candidate of
|
|
125
|
-
if (!existsSync(candidate)) continue;
|
|
139
|
+
for (const candidate of nativeBindingCandidates(platformComponent(), directories)) {
|
|
126
140
|
try {
|
|
127
141
|
const loaded: NativeBinding = require(candidate);
|
|
128
142
|
if (typeof loaded.NativeDocument === 'function') {
|