@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
package/dist/slim.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_modern = require("./modern-
|
|
2
|
+
const require_modern = require("./modern-DMcsyA4b.cjs");
|
|
3
3
|
// src/slim.ts
|
|
4
4
|
/** Parser-compatible slim entry point, backed by the native fragment parser. */
|
|
5
5
|
function load(content, options, _isDocument) {
|
package/dist/slim.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { l as merge, o as load$1, r as dom, t as contains } from "./modern-
|
|
1
|
+
import { l as merge, o as load$1, r as dom, t as contains } from "./modern-BwcwZX6l.mjs";
|
|
2
2
|
// src/slim.ts
|
|
3
3
|
/** Parser-compatible slim entry point, backed by the native fragment parser. */
|
|
4
4
|
function load(content, options, _isDocument) {
|
package/native/src/lib.rs
CHANGED
|
@@ -913,7 +913,8 @@ impl NativeDocument {
|
|
|
913
913
|
.map(|(name, value)| {
|
|
914
914
|
let qualified = element
|
|
915
915
|
.attrs
|
|
916
|
-
.
|
|
916
|
+
.iter()
|
|
917
|
+
.map(|(key, _)| key)
|
|
917
918
|
.rev()
|
|
918
919
|
.find(|key| key.local.as_ref() == name)
|
|
919
920
|
.cloned()
|
|
@@ -4116,4 +4117,511 @@ mod tests {
|
|
|
4116
4117
|
assert_eq!(node["sourceCodeLocation"]["endOffset"], 28);
|
|
4117
4118
|
assert_eq!(node["sourceCodeLocation"]["startTag"]["endOffset"], 20);
|
|
4118
4119
|
}
|
|
4120
|
+
|
|
4121
|
+
#[test]
|
|
4122
|
+
#[ignore = "diagnostic timing; run explicitly in release mode"]
|
|
4123
|
+
fn profile_arena_sink() {
|
|
4124
|
+
use html5ever::tendril::StrTendril;
|
|
4125
|
+
use html5ever::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
|
|
4126
|
+
use html5ever::{Attribute, QualName};
|
|
4127
|
+
|
|
4128
|
+
/// Detaches `id` from its parent and siblings.
|
|
4129
|
+
fn arena_detach(nodes: &mut [arena::DomNode], id: u32) {
|
|
4130
|
+
let (parent, previous, next) = match (
|
|
4131
|
+
nodes[id as usize].parent,
|
|
4132
|
+
nodes[id as usize].prev,
|
|
4133
|
+
nodes[id as usize].next,
|
|
4134
|
+
) {
|
|
4135
|
+
(Some(parent), previous, next) => (parent, previous, next),
|
|
4136
|
+
_ => return,
|
|
4137
|
+
};
|
|
4138
|
+
if let Some(previous) = previous {
|
|
4139
|
+
nodes[previous as usize].next = next;
|
|
4140
|
+
}
|
|
4141
|
+
if let Some(next) = next {
|
|
4142
|
+
nodes[next as usize].prev = previous;
|
|
4143
|
+
}
|
|
4144
|
+
let siblings = &mut nodes[parent as usize].children;
|
|
4145
|
+
if let Some(position) = siblings.iter().position(|child| *child == id) {
|
|
4146
|
+
siblings.remove(position);
|
|
4147
|
+
}
|
|
4148
|
+
let record = &mut nodes[id as usize];
|
|
4149
|
+
record.parent = None;
|
|
4150
|
+
record.prev = None;
|
|
4151
|
+
record.next = None;
|
|
4152
|
+
}
|
|
4153
|
+
|
|
4154
|
+
/// Links `id` as the last child of `parent`.
|
|
4155
|
+
fn arena_link_last_child(nodes: &mut [arena::DomNode], parent: u32, id: u32) {
|
|
4156
|
+
let previous = nodes[parent as usize].children.last().copied();
|
|
4157
|
+
let record = &mut nodes[id as usize];
|
|
4158
|
+
record.parent = Some(parent);
|
|
4159
|
+
record.prev = previous;
|
|
4160
|
+
record.next = None;
|
|
4161
|
+
if let Some(previous) = previous {
|
|
4162
|
+
nodes[previous as usize].next = Some(id);
|
|
4163
|
+
}
|
|
4164
|
+
nodes[parent as usize].children.push(id);
|
|
4165
|
+
}
|
|
4166
|
+
|
|
4167
|
+
/// Appends text to `parent`, concatenating into a trailing text node.
|
|
4168
|
+
fn arena_push_text(
|
|
4169
|
+
nodes: &mut Vec<arena::DomNode>,
|
|
4170
|
+
names: &mut Vec<QualName>,
|
|
4171
|
+
parent: u32,
|
|
4172
|
+
text: &StrTendril,
|
|
4173
|
+
) {
|
|
4174
|
+
if let Some(&last) = nodes[parent as usize].children.last() {
|
|
4175
|
+
if nodes[last as usize].kind.as_ref() == "text" {
|
|
4176
|
+
nodes[last as usize]
|
|
4177
|
+
.data
|
|
4178
|
+
.as_mut()
|
|
4179
|
+
.expect("text node without data")
|
|
4180
|
+
.push_str(text);
|
|
4181
|
+
return;
|
|
4182
|
+
}
|
|
4183
|
+
}
|
|
4184
|
+
let id = nodes.len() as u32;
|
|
4185
|
+
let mut record = arena::DomNode::new("text");
|
|
4186
|
+
record.id = id;
|
|
4187
|
+
record.data = Some(String::from(text));
|
|
4188
|
+
nodes.push(record);
|
|
4189
|
+
names.push(placeholder_name());
|
|
4190
|
+
arena_link_last_child(nodes, parent, id);
|
|
4191
|
+
}
|
|
4192
|
+
|
|
4193
|
+
/// Inserts text immediately before `sibling`, concatenating into a
|
|
4194
|
+
/// preceding text node.
|
|
4195
|
+
fn arena_insert_text_before(
|
|
4196
|
+
nodes: &mut Vec<arena::DomNode>,
|
|
4197
|
+
names: &mut Vec<QualName>,
|
|
4198
|
+
sibling: u32,
|
|
4199
|
+
text: &StrTendril,
|
|
4200
|
+
) {
|
|
4201
|
+
let (parent, previous) = {
|
|
4202
|
+
let record = &nodes[sibling as usize];
|
|
4203
|
+
(record.parent.expect("sibling without parent"), record.prev)
|
|
4204
|
+
};
|
|
4205
|
+
if let Some(previous) = previous {
|
|
4206
|
+
if nodes[previous as usize].kind.as_ref() == "text" {
|
|
4207
|
+
nodes[previous as usize]
|
|
4208
|
+
.data
|
|
4209
|
+
.as_mut()
|
|
4210
|
+
.expect("text node without data")
|
|
4211
|
+
.push_str(text);
|
|
4212
|
+
return;
|
|
4213
|
+
}
|
|
4214
|
+
}
|
|
4215
|
+
let id = nodes.len() as u32;
|
|
4216
|
+
let mut record = arena::DomNode::new("text");
|
|
4217
|
+
record.id = id;
|
|
4218
|
+
record.data = Some(String::from(text));
|
|
4219
|
+
record.parent = Some(parent);
|
|
4220
|
+
record.prev = previous;
|
|
4221
|
+
record.next = Some(sibling);
|
|
4222
|
+
if let Some(previous) = previous {
|
|
4223
|
+
nodes[previous as usize].next = Some(id);
|
|
4224
|
+
}
|
|
4225
|
+
nodes[sibling as usize].prev = Some(id);
|
|
4226
|
+
let siblings = &mut nodes[parent as usize].children;
|
|
4227
|
+
let position = siblings
|
|
4228
|
+
.iter()
|
|
4229
|
+
.position(|child| *child == sibling)
|
|
4230
|
+
.expect("sibling not under parent");
|
|
4231
|
+
siblings.insert(position, id);
|
|
4232
|
+
nodes.push(record);
|
|
4233
|
+
names.push(placeholder_name());
|
|
4234
|
+
}
|
|
4235
|
+
|
|
4236
|
+
/// A placeholder name keeping non-element handles aligned with the
|
|
4237
|
+
/// name vector; `elem_name` is only ever called on elements.
|
|
4238
|
+
fn placeholder_name() -> QualName {
|
|
4239
|
+
QualName::new(
|
|
4240
|
+
None,
|
|
4241
|
+
html5ever::ns!(html),
|
|
4242
|
+
html5ever::LocalName::from("unused"),
|
|
4243
|
+
)
|
|
4244
|
+
}
|
|
4245
|
+
|
|
4246
|
+
/// A TreeSink that builds arena-shaped records directly, allocating
|
|
4247
|
+
/// exactly what `convert_node` allocates (String names and data,
|
|
4248
|
+
/// `Value::String` attribute values, per-element namespace/prefix
|
|
4249
|
+
/// boxes, SmallVec children), so the measurement reflects the
|
|
4250
|
+
/// arena-primary refactor's construction cost.
|
|
4251
|
+
struct ArenaSink {
|
|
4252
|
+
nodes: std::cell::RefCell<Vec<arena::DomNode>>,
|
|
4253
|
+
names: std::cell::RefCell<Vec<QualName>>,
|
|
4254
|
+
quirks: std::cell::Cell<QuirksMode>,
|
|
4255
|
+
}
|
|
4256
|
+
|
|
4257
|
+
impl ArenaSink {
|
|
4258
|
+
fn new() -> Self {
|
|
4259
|
+
let mut document = arena::DomNode::new("root");
|
|
4260
|
+
document.id = 0;
|
|
4261
|
+
// Handles index both vectors: slot 0 belongs to the document,
|
|
4262
|
+
// whose name is never queried through `elem_name`.
|
|
4263
|
+
let placeholder = QualName::new(
|
|
4264
|
+
None,
|
|
4265
|
+
html5ever::ns!(html),
|
|
4266
|
+
html5ever::LocalName::from("document"),
|
|
4267
|
+
);
|
|
4268
|
+
Self {
|
|
4269
|
+
nodes: std::cell::RefCell::new(vec![document]),
|
|
4270
|
+
names: std::cell::RefCell::new(vec![placeholder]),
|
|
4271
|
+
quirks: std::cell::Cell::new(QuirksMode::NoQuirks),
|
|
4272
|
+
}
|
|
4273
|
+
}
|
|
4274
|
+
}
|
|
4275
|
+
|
|
4276
|
+
impl TreeSink for ArenaSink {
|
|
4277
|
+
type Handle = u32;
|
|
4278
|
+
type Output = Vec<arena::DomNode>;
|
|
4279
|
+
type ElemName<'a> = std::cell::Ref<'a, QualName>;
|
|
4280
|
+
|
|
4281
|
+
fn finish(self) -> Self::Output {
|
|
4282
|
+
let mode = match self.quirks.get() {
|
|
4283
|
+
QuirksMode::Quirks => "quirks",
|
|
4284
|
+
QuirksMode::LimitedQuirks => "limited-quirks",
|
|
4285
|
+
QuirksMode::NoQuirks => "no-quirks",
|
|
4286
|
+
};
|
|
4287
|
+
let mut nodes = self.nodes.into_inner();
|
|
4288
|
+
nodes[0]
|
|
4289
|
+
.extras
|
|
4290
|
+
.insert("x-mode".into(), serde_json::Value::String(mode.into()));
|
|
4291
|
+
nodes
|
|
4292
|
+
}
|
|
4293
|
+
fn parse_error(&self, _message: std::borrow::Cow<'static, str>) {}
|
|
4294
|
+
fn get_document(&self) -> u32 {
|
|
4295
|
+
0
|
|
4296
|
+
}
|
|
4297
|
+
fn same_node(&self, x: &u32, y: &u32) -> bool {
|
|
4298
|
+
x == y
|
|
4299
|
+
}
|
|
4300
|
+
fn elem_name<'a>(&'a self, target: &'a u32) -> Self::ElemName<'a> {
|
|
4301
|
+
std::cell::Ref::map(self.names.borrow(), |names| &names[*target as usize])
|
|
4302
|
+
}
|
|
4303
|
+
fn create_element(
|
|
4304
|
+
&self,
|
|
4305
|
+
name: QualName,
|
|
4306
|
+
attrs: Vec<Attribute>,
|
|
4307
|
+
_flags: ElementFlags,
|
|
4308
|
+
) -> u32 {
|
|
4309
|
+
let mut nodes = self.nodes.borrow_mut();
|
|
4310
|
+
let id = nodes.len() as u32;
|
|
4311
|
+
let mut record = arena::DomNode::new(if name.local.as_ref() == "script" {
|
|
4312
|
+
"script"
|
|
4313
|
+
} else if name.local.as_ref() == "style" {
|
|
4314
|
+
"style"
|
|
4315
|
+
} else {
|
|
4316
|
+
"tag"
|
|
4317
|
+
});
|
|
4318
|
+
record.id = id;
|
|
4319
|
+
record.name = Some(String::from(name.local.as_ref()));
|
|
4320
|
+
record.namespace = Some(arena::namespace_name(&name.ns));
|
|
4321
|
+
let mut attribs = arena::Attributes::with_capacity(attrs.len());
|
|
4322
|
+
let mut namespaces = serde_json::Map::new();
|
|
4323
|
+
let mut prefixes = serde_json::Map::new();
|
|
4324
|
+
for attr in attrs {
|
|
4325
|
+
let key = String::from(attr.name.local.as_ref());
|
|
4326
|
+
if !attr.name.ns.is_empty() {
|
|
4327
|
+
namespaces.insert(
|
|
4328
|
+
key.clone(),
|
|
4329
|
+
serde_json::Value::String(String::from(attr.name.ns.as_ref())),
|
|
4330
|
+
);
|
|
4331
|
+
}
|
|
4332
|
+
if let Some(prefix) = &attr.name.prefix {
|
|
4333
|
+
prefixes.insert(
|
|
4334
|
+
key.clone(),
|
|
4335
|
+
serde_json::Value::String(String::from(&**prefix)),
|
|
4336
|
+
);
|
|
4337
|
+
}
|
|
4338
|
+
attribs.insert(key, serde_json::Value::String(String::from(&*attr.value)));
|
|
4339
|
+
}
|
|
4340
|
+
record.attribs = attribs;
|
|
4341
|
+
record.attribs_namespace = Some(Box::new(namespaces));
|
|
4342
|
+
record.attribs_prefix = Some(Box::new(prefixes));
|
|
4343
|
+
if let Some(prefix) = &name.prefix {
|
|
4344
|
+
record.extras.insert(
|
|
4345
|
+
"prefix".into(),
|
|
4346
|
+
serde_json::Value::String(String::from(&**prefix)),
|
|
4347
|
+
);
|
|
4348
|
+
}
|
|
4349
|
+
let is_template = name.local.as_ref() == "template";
|
|
4350
|
+
nodes.push(record);
|
|
4351
|
+
self.names.borrow_mut().push(name);
|
|
4352
|
+
let element = id;
|
|
4353
|
+
if is_template {
|
|
4354
|
+
// The integration-point fragment child a template carries.
|
|
4355
|
+
let fragment = nodes.len() as u32;
|
|
4356
|
+
let mut record = arena::DomNode::new("root");
|
|
4357
|
+
record.id = fragment;
|
|
4358
|
+
nodes.push(record);
|
|
4359
|
+
arena_link_last_child(&mut nodes, element, fragment);
|
|
4360
|
+
return fragment;
|
|
4361
|
+
}
|
|
4362
|
+
element
|
|
4363
|
+
}
|
|
4364
|
+
fn create_comment(&self, text: StrTendril) -> u32 {
|
|
4365
|
+
let mut nodes = self.nodes.borrow_mut();
|
|
4366
|
+
let id = nodes.len() as u32;
|
|
4367
|
+
let mut record = arena::DomNode::new("comment");
|
|
4368
|
+
record.id = id;
|
|
4369
|
+
record.data = Some(String::from(&*text));
|
|
4370
|
+
nodes.push(record);
|
|
4371
|
+
self.names.borrow_mut().push(placeholder_name());
|
|
4372
|
+
id
|
|
4373
|
+
}
|
|
4374
|
+
fn append_doctype_to_document(
|
|
4375
|
+
&self,
|
|
4376
|
+
name: StrTendril,
|
|
4377
|
+
public_id: StrTendril,
|
|
4378
|
+
system_id: StrTendril,
|
|
4379
|
+
) {
|
|
4380
|
+
let mut nodes = self.nodes.borrow_mut();
|
|
4381
|
+
let id = nodes.len() as u32;
|
|
4382
|
+
let mut record = arena::DomNode::new("directive");
|
|
4383
|
+
record.id = id;
|
|
4384
|
+
record.name = Some("!doctype".into());
|
|
4385
|
+
record.extras.insert(
|
|
4386
|
+
"x-name".into(),
|
|
4387
|
+
serde_json::Value::String(String::from(&*name)),
|
|
4388
|
+
);
|
|
4389
|
+
record.extras.insert(
|
|
4390
|
+
"x-publicId".into(),
|
|
4391
|
+
serde_json::Value::String(String::from(&*public_id)),
|
|
4392
|
+
);
|
|
4393
|
+
record.extras.insert(
|
|
4394
|
+
"x-systemId".into(),
|
|
4395
|
+
serde_json::Value::String(String::from(&*system_id)),
|
|
4396
|
+
);
|
|
4397
|
+
let mut data = format!("!DOCTYPE {}", name);
|
|
4398
|
+
if !public_id.is_empty() {
|
|
4399
|
+
data.push_str(" PUBLIC ");
|
|
4400
|
+
data.push_str(&public_id);
|
|
4401
|
+
if !system_id.is_empty() {
|
|
4402
|
+
data.push_str(" \"");
|
|
4403
|
+
data.push_str(&system_id);
|
|
4404
|
+
data.push('"');
|
|
4405
|
+
}
|
|
4406
|
+
} else if !system_id.is_empty() {
|
|
4407
|
+
data.push_str(" SYSTEM \"");
|
|
4408
|
+
data.push_str(&system_id);
|
|
4409
|
+
data.push('"');
|
|
4410
|
+
}
|
|
4411
|
+
record.data = Some(data);
|
|
4412
|
+
nodes.push(record);
|
|
4413
|
+
self.names.borrow_mut().push(placeholder_name());
|
|
4414
|
+
arena_link_last_child(&mut nodes, 0, id);
|
|
4415
|
+
}
|
|
4416
|
+
fn append(&self, parent: &u32, child: NodeOrText<u32>) {
|
|
4417
|
+
let mut nodes = self.nodes.borrow_mut();
|
|
4418
|
+
match child {
|
|
4419
|
+
NodeOrText::AppendNode(id) => {
|
|
4420
|
+
arena_detach(&mut nodes, id);
|
|
4421
|
+
arena_link_last_child(&mut nodes, *parent, id);
|
|
4422
|
+
}
|
|
4423
|
+
NodeOrText::AppendText(text) => {
|
|
4424
|
+
arena_push_text(&mut nodes, &mut self.names.borrow_mut(), *parent, &text)
|
|
4425
|
+
}
|
|
4426
|
+
}
|
|
4427
|
+
}
|
|
4428
|
+
fn append_before_sibling(&self, sibling: &u32, new_node: NodeOrText<u32>) {
|
|
4429
|
+
let mut nodes = self.nodes.borrow_mut();
|
|
4430
|
+
if let NodeOrText::AppendNode(id) = new_node {
|
|
4431
|
+
arena_detach(&mut nodes, id);
|
|
4432
|
+
}
|
|
4433
|
+
let (parent, previous) = {
|
|
4434
|
+
let record = &nodes[*sibling as usize];
|
|
4435
|
+
(record.parent, record.prev)
|
|
4436
|
+
};
|
|
4437
|
+
let Some(parent) = parent else { return };
|
|
4438
|
+
match new_node {
|
|
4439
|
+
NodeOrText::AppendNode(id) => {
|
|
4440
|
+
let record = &mut nodes[id as usize];
|
|
4441
|
+
record.parent = Some(parent);
|
|
4442
|
+
record.prev = previous;
|
|
4443
|
+
record.next = Some(*sibling);
|
|
4444
|
+
if let Some(previous) = previous {
|
|
4445
|
+
nodes[previous as usize].next = Some(id);
|
|
4446
|
+
}
|
|
4447
|
+
nodes[*sibling as usize].prev = Some(id);
|
|
4448
|
+
let siblings = &mut nodes[parent as usize].children;
|
|
4449
|
+
let position = siblings
|
|
4450
|
+
.iter()
|
|
4451
|
+
.position(|child| *child == id)
|
|
4452
|
+
.expect("detached node not found under parent");
|
|
4453
|
+
siblings.insert(position, id);
|
|
4454
|
+
}
|
|
4455
|
+
NodeOrText::AppendText(text) => arena_insert_text_before(
|
|
4456
|
+
&mut nodes,
|
|
4457
|
+
&mut self.names.borrow_mut(),
|
|
4458
|
+
*sibling,
|
|
4459
|
+
&text,
|
|
4460
|
+
),
|
|
4461
|
+
}
|
|
4462
|
+
}
|
|
4463
|
+
fn append_based_on_parent_node(
|
|
4464
|
+
&self,
|
|
4465
|
+
element: &u32,
|
|
4466
|
+
previous_element: &u32,
|
|
4467
|
+
child: NodeOrText<u32>,
|
|
4468
|
+
) {
|
|
4469
|
+
let has_parent = self.nodes.borrow()[*element as usize].parent.is_some();
|
|
4470
|
+
if has_parent {
|
|
4471
|
+
self.append_before_sibling(element, child);
|
|
4472
|
+
} else {
|
|
4473
|
+
self.append(previous_element, child);
|
|
4474
|
+
}
|
|
4475
|
+
}
|
|
4476
|
+
fn remove_from_parent(&self, target: &u32) {
|
|
4477
|
+
let mut nodes = self.nodes.borrow_mut();
|
|
4478
|
+
arena_detach(&mut nodes, *target);
|
|
4479
|
+
}
|
|
4480
|
+
fn reparent_children(&self, node: &u32, new_parent: &u32) {
|
|
4481
|
+
let mut nodes = self.nodes.borrow_mut();
|
|
4482
|
+
let children = std::mem::take(&mut nodes[*node as usize].children);
|
|
4483
|
+
let count = children.len();
|
|
4484
|
+
if count == 0 {
|
|
4485
|
+
return;
|
|
4486
|
+
}
|
|
4487
|
+
let first = children[0];
|
|
4488
|
+
// The moved run keeps its internal sibling links; only the
|
|
4489
|
+
// edges touching the old and new parents are relinked below.
|
|
4490
|
+
let _last = children[count - 1];
|
|
4491
|
+
for child in &children {
|
|
4492
|
+
nodes[*child as usize].parent = Some(*new_parent);
|
|
4493
|
+
}
|
|
4494
|
+
let previous = nodes[*new_parent as usize].children.last().copied();
|
|
4495
|
+
if let Some(previous) = previous {
|
|
4496
|
+
nodes[previous as usize].next = Some(first);
|
|
4497
|
+
}
|
|
4498
|
+
nodes[first as usize].prev = previous;
|
|
4499
|
+
nodes[*new_parent as usize]
|
|
4500
|
+
.children
|
|
4501
|
+
.extend(children.iter().copied());
|
|
4502
|
+
}
|
|
4503
|
+
fn add_attrs_if_missing(&self, target: &u32, attrs: Vec<Attribute>) {
|
|
4504
|
+
let mut nodes = self.nodes.borrow_mut();
|
|
4505
|
+
let record = &mut nodes[*target as usize];
|
|
4506
|
+
for attr in attrs {
|
|
4507
|
+
record.attribs.push_unique(
|
|
4508
|
+
String::from(attr.name.local.as_ref()),
|
|
4509
|
+
serde_json::Value::String(String::from(&*attr.value)),
|
|
4510
|
+
);
|
|
4511
|
+
}
|
|
4512
|
+
}
|
|
4513
|
+
fn get_template_contents(&self, target: &u32) -> u32 {
|
|
4514
|
+
*self.nodes.borrow()[*target as usize]
|
|
4515
|
+
.children
|
|
4516
|
+
.first()
|
|
4517
|
+
.expect("template without fragment contents")
|
|
4518
|
+
}
|
|
4519
|
+
fn mark_script_already_started(&self, _: &u32) {}
|
|
4520
|
+
fn set_quirks_mode(&self, mode: QuirksMode) {
|
|
4521
|
+
self.quirks.set(mode);
|
|
4522
|
+
}
|
|
4523
|
+
fn create_pi(&self, target: StrTendril, data: StrTendril) -> u32 {
|
|
4524
|
+
let mut nodes = self.nodes.borrow_mut();
|
|
4525
|
+
let id = nodes.len() as u32;
|
|
4526
|
+
let mut record = arena::DomNode::new("directive");
|
|
4527
|
+
record.id = id;
|
|
4528
|
+
record.name = Some(String::from(&*target));
|
|
4529
|
+
record.data = Some(String::from(&*data));
|
|
4530
|
+
nodes.push(record);
|
|
4531
|
+
self.names.borrow_mut().push(placeholder_name());
|
|
4532
|
+
id
|
|
4533
|
+
}
|
|
4534
|
+
}
|
|
4535
|
+
|
|
4536
|
+
let card = |i: usize| {
|
|
4537
|
+
format!(
|
|
4538
|
+
"<div class=\"card c{}\" id=\"d{}\" data-index=\"{}\"><header><a href=\"/p/{}\" title=\"Item {}\"><img src=\"/i/{}.png\" alt=\"x{}\" width=\"10\"></a></header><p>Lorem ipsum {} <b>bold</b> & <i>more</i></p><ul><li>a</li><li class=\"x\">b</li></ul></div>",
|
|
4539
|
+
i % 7, i, i, i, i, i, i, i
|
|
4540
|
+
)
|
|
4541
|
+
};
|
|
4542
|
+
let mut source = String::from("<!doctype html><html><head><title>Page</title><meta name=\"description\" content=\"d\"><script>var x = 1 < 2;</script></head><body><nav><a href=\"/\">Home</a></nav><main>");
|
|
4543
|
+
for i in 0..2000 {
|
|
4544
|
+
source.push_str(&card(i));
|
|
4545
|
+
}
|
|
4546
|
+
source.push_str("</main><footer>f</footer></body></html>");
|
|
4547
|
+
println!("fixture bytes: {}", source.len());
|
|
4548
|
+
|
|
4549
|
+
// Correctness first: the direct arena must match the existing parse
|
|
4550
|
+
// + lazy conversion for this fixture.
|
|
4551
|
+
{
|
|
4552
|
+
let document =
|
|
4553
|
+
NativeDocument::parse_options(&source, true, serde_json::json!({}), None);
|
|
4554
|
+
document.ensure_arena().unwrap();
|
|
4555
|
+
let reference = document.arena.borrow();
|
|
4556
|
+
let reference = reference.as_ref().unwrap();
|
|
4557
|
+
let start = std::time::Instant::now();
|
|
4558
|
+
let arena = html5ever::driver::parse_document(ArenaSink::new(), Default::default())
|
|
4559
|
+
.one(source.as_str());
|
|
4560
|
+
let direct_build = start.elapsed();
|
|
4561
|
+
println!(
|
|
4562
|
+
"arena sink built {} nodes in {:?}",
|
|
4563
|
+
arena.len(),
|
|
4564
|
+
direct_build
|
|
4565
|
+
);
|
|
4566
|
+
assert_eq!(arena.len(), reference.nodes.len(), "node count differs");
|
|
4567
|
+
for (index, (candidate, expected)) in
|
|
4568
|
+
arena.iter().zip(reference.nodes.iter()).enumerate()
|
|
4569
|
+
{
|
|
4570
|
+
assert_eq!(candidate.kind, expected.kind, "kind at {index}");
|
|
4571
|
+
assert_eq!(candidate.name, expected.name, "name at {index}");
|
|
4572
|
+
assert_eq!(candidate.data, expected.data, "data at {index}");
|
|
4573
|
+
assert_eq!(candidate.children, expected.children, "children at {index}");
|
|
4574
|
+
assert_eq!(candidate.parent, expected.parent, "parent at {index}");
|
|
4575
|
+
assert_eq!(
|
|
4576
|
+
candidate.attribs.len(),
|
|
4577
|
+
expected.attribs.len(),
|
|
4578
|
+
"attr count at {index}"
|
|
4579
|
+
);
|
|
4580
|
+
for (name, value) in &expected.attribs {
|
|
4581
|
+
let candidate_value = candidate
|
|
4582
|
+
.attribs
|
|
4583
|
+
.get(name)
|
|
4584
|
+
.and_then(|value| value.as_str())
|
|
4585
|
+
.unwrap_or("");
|
|
4586
|
+
assert_eq!(
|
|
4587
|
+
candidate_value,
|
|
4588
|
+
value.as_str().unwrap_or(""),
|
|
4589
|
+
"attribute {name} at {index}"
|
|
4590
|
+
);
|
|
4591
|
+
}
|
|
4592
|
+
}
|
|
4593
|
+
std::hint::black_box(arena);
|
|
4594
|
+
}
|
|
4595
|
+
|
|
4596
|
+
let mut direct_samples = Vec::new();
|
|
4597
|
+
let mut current_samples = Vec::new();
|
|
4598
|
+
for iteration in 0..40 {
|
|
4599
|
+
let start = std::time::Instant::now();
|
|
4600
|
+
let arena = html5ever::driver::parse_document(ArenaSink::new(), Default::default())
|
|
4601
|
+
.one(source.as_str());
|
|
4602
|
+
let direct = start.elapsed();
|
|
4603
|
+
std::hint::black_box(&arena);
|
|
4604
|
+
|
|
4605
|
+
let start = std::time::Instant::now();
|
|
4606
|
+
let document =
|
|
4607
|
+
NativeDocument::parse_options(&source, true, serde_json::json!({}), None);
|
|
4608
|
+
document.ensure_arena().unwrap();
|
|
4609
|
+
let current = start.elapsed();
|
|
4610
|
+
std::hint::black_box(&document);
|
|
4611
|
+
|
|
4612
|
+
if iteration >= 5 {
|
|
4613
|
+
direct_samples.push(direct);
|
|
4614
|
+
current_samples.push(current);
|
|
4615
|
+
}
|
|
4616
|
+
}
|
|
4617
|
+
let median = |mut samples: Vec<std::time::Duration>| {
|
|
4618
|
+
samples.sort_unstable();
|
|
4619
|
+
samples[samples.len() / 2]
|
|
4620
|
+
};
|
|
4621
|
+
println!(
|
|
4622
|
+
"direct arena sink: {:?}; current parse + lazy conversion: {:?}",
|
|
4623
|
+
median(direct_samples),
|
|
4624
|
+
median(current_samples),
|
|
4625
|
+
);
|
|
4626
|
+
}
|
|
4119
4627
|
}
|
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.
|