@dvaji/cheerox 0.1.2 → 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 -1
- package/Cargo.toml +1 -1
- package/THIRD_PARTY_NOTICES.md +1 -1
- 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-DcnYK9OB.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-BKkfJEAr.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 +507 -0
- package/package.json +1 -1
- package/src/cheerio-compat.ts +14 -4
- package/src/native.ts +23 -9
- package/dist/cheerio-compat-BKkfJEAr.mjs.map +0 -1
- package/dist/cheerio-compat-DcnYK9OB.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
|
@@ -4117,4 +4117,511 @@ mod tests {
|
|
|
4117
4117
|
assert_eq!(node["sourceCodeLocation"]["endOffset"], 28);
|
|
4118
4118
|
assert_eq!(node["sourceCodeLocation"]["startTag"]["endOffset"], 20);
|
|
4119
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
|
+
}
|
|
4120
4627
|
}
|
package/package.json
CHANGED
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') {
|