@dvaji/cheerox 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +74 -0
- package/Cargo.lock +1091 -0
- package/Cargo.toml +47 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/RELEASING.md +114 -0
- package/THIRD_PARTY_NOTICES.md +7581 -0
- package/build.rs +3 -0
- package/cheerox.darwin-arm64.node +0 -0
- package/cheerox.darwin-x64.node +0 -0
- package/cheerox.linux-arm64-gnu.node +0 -0
- package/cheerox.linux-arm64-musl.node +0 -0
- package/cheerox.linux-x64-gnu.node +0 -0
- package/cheerox.linux-x64-musl.node +0 -0
- package/cheerox.win32-arm64-msvc.node +0 -0
- package/cheerox.win32-x64-msvc.node +0 -0
- package/dist/cheerio-compat-BKkfJEAr.mjs +1648 -0
- package/dist/cheerio-compat-BKkfJEAr.mjs.map +1 -0
- package/dist/cheerio-compat-DRLPzBdX.d.mts +88 -0
- package/dist/cheerio-compat-DRsINTwV.d.cts +88 -0
- package/dist/cheerio-compat-DRsINTwV.d.ts +88 -0
- package/dist/cheerio-compat-DcnYK9OB.cjs +1719 -0
- package/dist/cheerio-compat-DcnYK9OB.cjs.map +1 -0
- package/dist/cheerio-compat.cjs +18 -0
- package/dist/cheerio-compat.d.cts +2 -0
- package/dist/cheerio-compat.d.mts +2 -0
- package/dist/cheerio-compat.d.ts +2 -0
- package/dist/cheerio-compat.mjs +1 -0
- package/dist/index.cjs +13 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.mjs +53 -0
- package/dist/index.mjs.map +1 -0
- package/dist/legacy-parse.cjs +5 -0
- package/dist/legacy-parse.d.mts +8 -0
- package/dist/legacy-parse.d.ts +7 -0
- package/dist/legacy-parse.mjs +1 -0
- package/dist/modern-B7hyW4cI.d.cts +102 -0
- package/dist/modern-B7hyW4cI.d.ts +102 -0
- package/dist/modern-CHQGP0Yt.cjs +2858 -0
- package/dist/modern-CHQGP0Yt.cjs.map +1 -0
- package/dist/modern-CJvSD_y5.d.mts +102 -0
- package/dist/modern-CvXcTn82.mjs +2769 -0
- package/dist/modern-CvXcTn82.mjs.map +1 -0
- package/dist/modern.cjs +19 -0
- package/dist/modern.d.cts +3 -0
- package/dist/modern.d.mts +3 -0
- package/dist/modern.d.ts +3 -0
- package/dist/modern.mjs +3 -0
- package/dist/nodes-jbWWhssj.cjs +4210 -0
- package/dist/nodes-jbWWhssj.cjs.map +1 -0
- package/dist/nodes-sYxb5qyZ.mjs +3803 -0
- package/dist/nodes-sYxb5qyZ.mjs.map +1 -0
- package/dist/slim.cjs +16 -0
- package/dist/slim.cjs.map +1 -0
- package/dist/slim.d.cts +6 -0
- package/dist/slim.d.mts +6 -0
- package/dist/slim.d.ts +6 -0
- package/dist/slim.mjs +12 -0
- package/dist/slim.mjs.map +1 -0
- package/dist/types-CA01Zg3r.d.cts +526 -0
- package/dist/types-CA01Zg3r.d.mts +526 -0
- package/dist/types-CA01Zg3r.d.ts +526 -0
- package/dist/utils.cjs +31 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +8 -0
- package/dist/utils.d.mts +8 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.mjs +25 -0
- package/dist/utils.mjs.map +1 -0
- package/native/src/arena.rs +2109 -0
- package/native/src/attributes.rs +946 -0
- package/native/src/codec.rs +298 -0
- package/native/src/diagnostics.rs +1249 -0
- package/native/src/document_order.rs +711 -0
- package/native/src/extraction.rs +321 -0
- package/native/src/gc.rs +175 -0
- package/native/src/hook_sink.rs +801 -0
- package/native/src/hook_stream.rs +513 -0
- package/native/src/id_hash.rs +42 -0
- package/native/src/immutable.rs +333 -0
- package/native/src/lib.rs +4120 -0
- package/native/src/location.rs +392 -0
- package/native/src/markup_tokenizer.rs +668 -0
- package/native/src/runtime.rs +368 -0
- package/native/src/selector.rs +1596 -0
- package/native/src/sink.rs +274 -0
- package/native/src/snapshot.rs +672 -0
- package/native/src/stream.rs +334 -0
- package/native/src/tokenizer.rs +581 -0
- package/native/src/tracker.rs +232 -0
- package/native/vendor/README.md +35 -0
- package/native/vendor/html5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/html5ever-0.35.0/Cargo.lock +828 -0
- package/native/vendor/html5ever-0.35.0/Cargo.toml +85 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/html5ever-0.35.0/README.md +60 -0
- package/native/vendor/html5ever-0.35.0/benches/html5ever.rs +86 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum-zh.html +19 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum.html +40 -0
- package/native/vendor/html5ever-0.35.0/data/bench/medium-fragment.html +24 -0
- package/native/vendor/html5ever-0.35.0/data/bench/small-fragment.html +7 -0
- package/native/vendor/html5ever-0.35.0/data/bench/strong.html +1 -0
- package/native/vendor/html5ever-0.35.0/data/bench/tiny-fragment.html +1 -0
- package/native/vendor/html5ever-0.35.0/examples/arena.rs +356 -0
- package/native/vendor/html5ever-0.35.0/examples/capi/tokenize.c +74 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tokenize.rs +47 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tree-builder.rs +132 -0
- package/native/vendor/html5ever-0.35.0/examples/print-tree-actions.rs +185 -0
- package/native/vendor/html5ever-0.35.0/examples/tokenize.rs +111 -0
- package/native/vendor/html5ever-0.35.0/src/driver.rs +148 -0
- package/native/vendor/html5ever-0.35.0/src/lib.rs +30 -0
- package/native/vendor/html5ever-0.35.0/src/macros.rs +34 -0
- package/native/vendor/html5ever-0.35.0/src/serialize/mod.rs +255 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/char_ref/mod.rs +530 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/interface.rs +99 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/mod.rs +2628 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/states.rs +97 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/data.rs +170 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/mod.rs +1848 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/rules.rs +1494 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/tag_sets.rs +114 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/types.rs +91 -0
- package/native/vendor/html5ever-0.35.0/src/util/str.rs +52 -0
- package/native/vendor/html5gum-0.8.4/Cargo.lock +1006 -0
- package/native/vendor/html5gum-0.8.4/Cargo.toml +109 -0
- package/native/vendor/html5gum-0.8.4/LICENSE +19 -0
- package/native/vendor/html5gum-0.8.4/README.md +110 -0
- package/native/vendor/html5gum-0.8.4/benches/files/0050.html +2131 -0
- package/native/vendor/html5gum-0.8.4/benches/files/6D30.html +1004 -0
- package/native/vendor/html5gum-0.8.4/benches/files.rs +32 -0
- package/native/vendor/html5gum-0.8.4/benches/patterns.rs +31 -0
- package/native/vendor/html5gum-0.8.4/src/arrayvec.rs +29 -0
- package/native/vendor/html5gum-0.8.4/src/char_validator.rs +134 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/callback.rs +676 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/default.rs +207 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/emitter.rs +344 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/html5ever.rs +306 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/mod.rs +25 -0
- package/native/vendor/html5gum-0.8.4/src/entities.rs +3342 -0
- package/native/vendor/html5gum-0.8.4/src/error.rs +96 -0
- package/native/vendor/html5gum-0.8.4/src/htmlstring.rs +130 -0
- package/native/vendor/html5gum-0.8.4/src/lib.rs +66 -0
- package/native/vendor/html5gum-0.8.4/src/machine.rs +2136 -0
- package/native/vendor/html5gum-0.8.4/src/machine_helper.rs +277 -0
- package/native/vendor/html5gum-0.8.4/src/read_helper.rs +244 -0
- package/native/vendor/html5gum-0.8.4/src/reader.rs +440 -0
- package/native/vendor/html5gum-0.8.4/src/span.rs +98 -0
- package/native/vendor/html5gum-0.8.4/src/state.rs +16 -0
- package/native/vendor/html5gum-0.8.4/src/testutils.rs +71 -0
- package/native/vendor/html5gum-0.8.4/src/tokenizer.rs +189 -0
- package/native/vendor/html5gum-0.8.4/src/utils.rs +88 -0
- package/native/vendor/markup5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/markup5ever-0.35.0/Cargo.toml +45 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/markup5ever-0.35.0/interface/mod.rs +422 -0
- package/native/vendor/markup5ever-0.35.0/interface/tree_builder.rs +280 -0
- package/native/vendor/markup5ever-0.35.0/lib.rs +59 -0
- package/native/vendor/markup5ever-0.35.0/serialize.rs +77 -0
- package/native/vendor/markup5ever-0.35.0/util/buffer_queue.rs +333 -0
- package/native/vendor/markup5ever-0.35.0/util/smallcharset.rs +88 -0
- package/native/vendor/scraper-0.24.0/Cargo.toml +52 -0
- package/native/vendor/scraper-0.24.0/LICENSE +15 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/element.rs +212 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/mod.rs +236 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/serializable.rs +15 -0
- package/native/vendor/scraper-0.24.0/src/error/utils.rs +91 -0
- package/native/vendor/scraper-0.24.0/src/error.rs +118 -0
- package/native/vendor/scraper-0.24.0/src/html/mod.rs +270 -0
- package/native/vendor/scraper-0.24.0/src/html/serializable.rs +27 -0
- package/native/vendor/scraper-0.24.0/src/html/tree_sink.rs +285 -0
- package/native/vendor/scraper-0.24.0/src/lib.rs +180 -0
- package/native/vendor/scraper-0.24.0/src/node/serializable.rs +52 -0
- package/native/vendor/scraper-0.24.0/src/node.rs +465 -0
- package/native/vendor/scraper-0.24.0/src/selectable.rs +70 -0
- package/native/vendor/scraper-0.24.0/src/selector.rs +290 -0
- package/package.json +167 -4
- package/scripts/check-native.mjs +15 -0
- package/scripts/check-release.mjs +234 -0
- package/scripts/fix-declarations.mjs +39 -0
- package/scripts/generate-notices.mjs +194 -0
- package/scripts/notices/README.md +13 -0
- package/scripts/notices/cheerio-MIT.txt +21 -0
- package/scripts/notices/html5ever-AUTHORS.txt +20 -0
- package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
- package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
- package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
- package/scripts/notices/htmlparser2-MIT.txt +18 -0
- package/scripts/notices/napi-rs-MIT.txt +43 -0
- package/scripts/notices/neon-MIT.txt +19 -0
- package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
- package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
- package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
- package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
- package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
- package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
- package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
- package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
- package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
- package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
- package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
- package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
- package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
- package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
- package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
- package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
- package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
- package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
- package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
- package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
- package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
- package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
- package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
- package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
- package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
- package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
- package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
- package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
- package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
- package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
- package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
- package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
- package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
- package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
- package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
- package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
- package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
- package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
- package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
- package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
- package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
- package/scripts/notices/runtime.json +1632 -0
- package/scripts/notices/sources.json +561 -0
- package/scripts/notices/xray-MIT.txt +21 -0
- package/src/attribute-object.ts +71 -0
- package/src/attributes.ts +308 -0
- package/src/cheerio-compat/css.ts +58 -0
- package/src/cheerio-compat/render.ts +106 -0
- package/src/cheerio-compat/selectors.ts +51 -0
- package/src/cheerio-compat/types.ts +52 -0
- package/src/cheerio-compat.ts +2055 -0
- package/src/codec.ts +96 -0
- package/src/dom.ts +87 -0
- package/src/extract-plan.ts +97 -0
- package/src/extract.ts +322 -0
- package/src/hooks.ts +369 -0
- package/src/index.ts +48 -0
- package/src/io.ts +491 -0
- package/src/match.ts +207 -0
- package/src/modern.ts +259 -0
- package/src/native.ts +149 -0
- package/src/nodes.ts +831 -0
- package/src/parse.ts +53 -0
- package/src/parser-types.ts +157 -0
- package/src/router.ts +1129 -0
- package/src/selection.ts +682 -0
- package/src/slim.ts +12 -0
- package/src/snapshot.ts +291 -0
- package/src/transport.ts +113 -0
- package/src/types.ts +186 -0
- package/src/utils.ts +29 -0
- package/src/workers.ts +21 -0
- package/tsconfig.json +15 -0
- package/tsdown.config.mjs +17 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
//! Read-only selection over a parsed document. Queries, matching and ordering
|
|
2
|
+
//! run on the parse tree, so a document is copied into the mutable arena only
|
|
3
|
+
//! when it is first changed.
|
|
4
|
+
use ego_tree::iter::Edge;
|
|
5
|
+
use ego_tree::NodeId;
|
|
6
|
+
use scraper::{Html, Node};
|
|
7
|
+
use serde_json::Value;
|
|
8
|
+
|
|
9
|
+
use crate::selector::SelectDom;
|
|
10
|
+
|
|
11
|
+
const NONE: u32 = u32::MAX;
|
|
12
|
+
|
|
13
|
+
/// Parent and child links by local id. Local ids are preorder positions, so
|
|
14
|
+
/// ascending order is document order.
|
|
15
|
+
pub(crate) struct TreeIndex {
|
|
16
|
+
parent: Vec<u32>,
|
|
17
|
+
child_start: Vec<u32>,
|
|
18
|
+
child_end: Vec<u32>,
|
|
19
|
+
children: Vec<u32>,
|
|
20
|
+
name_index: crate::id_hash::IdMap<String, Vec<u32>>,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
impl TreeIndex {
|
|
24
|
+
pub(crate) fn build(html: &Html, ids: &[NodeId]) -> Self {
|
|
25
|
+
let count = ids.len();
|
|
26
|
+
let mut parent = vec![NONE; count];
|
|
27
|
+
let mut stack: Vec<u32> = Vec::new();
|
|
28
|
+
let mut next = 0u32;
|
|
29
|
+
for edge in html.tree.root().traverse() {
|
|
30
|
+
match edge {
|
|
31
|
+
Edge::Open(_) => {
|
|
32
|
+
if (next as usize) < count {
|
|
33
|
+
parent[next as usize] = stack.last().copied().unwrap_or(NONE);
|
|
34
|
+
}
|
|
35
|
+
stack.push(next);
|
|
36
|
+
next += 1;
|
|
37
|
+
}
|
|
38
|
+
Edge::Close(_) => {
|
|
39
|
+
stack.pop();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
let mut counts = vec![0u32; count];
|
|
44
|
+
for parent in &parent {
|
|
45
|
+
if let Some(slot) = counts.get_mut(*parent as usize) {
|
|
46
|
+
*slot += 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
let mut total = 0u32;
|
|
50
|
+
let child_start: Vec<u32> = counts
|
|
51
|
+
.iter()
|
|
52
|
+
.map(|count| {
|
|
53
|
+
let start = total;
|
|
54
|
+
total += count;
|
|
55
|
+
start
|
|
56
|
+
})
|
|
57
|
+
.collect();
|
|
58
|
+
let mut child_end = child_start.clone();
|
|
59
|
+
let mut children = vec![0u32; total as usize];
|
|
60
|
+
for (local, parent) in parent.iter().enumerate() {
|
|
61
|
+
if let Some(end) = child_end.get_mut(*parent as usize) {
|
|
62
|
+
children[*end as usize] = local as u32;
|
|
63
|
+
*end += 1;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Tag-name candidate index (lowercased), mirroring the arena's.
|
|
67
|
+
// Only nodes still attached to the tree are indexed.
|
|
68
|
+
let mut name_index: crate::id_hash::IdMap<String, Vec<u32>> =
|
|
69
|
+
crate::id_hash::IdMap::default();
|
|
70
|
+
let mut reachable = vec![false; count];
|
|
71
|
+
if count > 0 {
|
|
72
|
+
reachable[0] = true;
|
|
73
|
+
let mut stack = vec![0u32];
|
|
74
|
+
while let Some(node) = stack.pop() {
|
|
75
|
+
let start = child_start[node as usize] as usize;
|
|
76
|
+
let end = child_end[node as usize] as usize;
|
|
77
|
+
for &child in &children[start..end] {
|
|
78
|
+
if (child as usize) < reachable.len() && !reachable[child as usize] {
|
|
79
|
+
reachable[child as usize] = true;
|
|
80
|
+
stack.push(child);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
for local in 0..count {
|
|
86
|
+
if !reachable[local] {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if let Some(node) = html.tree.get(ids[local]) {
|
|
90
|
+
if let Node::Element(element) = node.value() {
|
|
91
|
+
let key = element.name().to_ascii_lowercase();
|
|
92
|
+
name_index.entry(key).or_default().push(local as u32);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
Self {
|
|
97
|
+
parent,
|
|
98
|
+
child_start,
|
|
99
|
+
child_end,
|
|
100
|
+
children,
|
|
101
|
+
name_index,
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// The index of a tree some of whose nodes were detached after parsing,
|
|
106
|
+
/// when traversal order no longer matches local ids.
|
|
107
|
+
pub(crate) fn build_mapped(
|
|
108
|
+
html: &Html,
|
|
109
|
+
ids: &[NodeId],
|
|
110
|
+
reverse: &crate::id_hash::IdMap<NodeId, u32>,
|
|
111
|
+
) -> Self {
|
|
112
|
+
let local = |node: Option<ego_tree::NodeRef<'_, Node>>| {
|
|
113
|
+
node.and_then(|node| reverse.get(&node.id()).copied())
|
|
114
|
+
};
|
|
115
|
+
let mut parent = vec![NONE; ids.len()];
|
|
116
|
+
let mut child_start = vec![0u32; ids.len()];
|
|
117
|
+
let mut child_end = vec![0u32; ids.len()];
|
|
118
|
+
let mut children = Vec::with_capacity(ids.len());
|
|
119
|
+
for (index, id) in ids.iter().enumerate() {
|
|
120
|
+
let Some(node) = html.tree.get(*id) else {
|
|
121
|
+
continue;
|
|
122
|
+
};
|
|
123
|
+
parent[index] = local(node.parent()).unwrap_or(NONE);
|
|
124
|
+
child_start[index] = children.len() as u32;
|
|
125
|
+
children.extend(node.children().filter_map(|child| local(Some(child))));
|
|
126
|
+
child_end[index] = children.len() as u32;
|
|
127
|
+
}
|
|
128
|
+
// Tag-name candidate index (lowercased), mirroring the arena's.
|
|
129
|
+
// Only nodes still attached to the tree are indexed.
|
|
130
|
+
let mut name_index: crate::id_hash::IdMap<String, Vec<u32>> =
|
|
131
|
+
crate::id_hash::IdMap::default();
|
|
132
|
+
let mut reachable = vec![false; ids.len()];
|
|
133
|
+
if !ids.is_empty() {
|
|
134
|
+
reachable[0] = true;
|
|
135
|
+
let mut stack = vec![0u32];
|
|
136
|
+
while let Some(node) = stack.pop() {
|
|
137
|
+
let start = child_start[node as usize] as usize;
|
|
138
|
+
let end = child_end[node as usize] as usize;
|
|
139
|
+
for &child in &children[start..end] {
|
|
140
|
+
if (child as usize) < reachable.len() && !reachable[child as usize] {
|
|
141
|
+
reachable[child as usize] = true;
|
|
142
|
+
stack.push(child);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
for (index, id) in ids.iter().enumerate() {
|
|
148
|
+
if !reachable[index] {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if let Some(node) = html.tree.get(*id) {
|
|
152
|
+
if let Node::Element(element) = node.value() {
|
|
153
|
+
let key = element.name().to_ascii_lowercase();
|
|
154
|
+
name_index.entry(key).or_default().push(index as u32);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
Self {
|
|
159
|
+
parent,
|
|
160
|
+
child_start,
|
|
161
|
+
child_end,
|
|
162
|
+
children,
|
|
163
|
+
name_index,
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
pub(crate) fn parent(&self, id: u32) -> Option<u32> {
|
|
168
|
+
self.parent
|
|
169
|
+
.get(id as usize)
|
|
170
|
+
.copied()
|
|
171
|
+
.filter(|parent| *parent != NONE)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
pub(crate) fn children(&self, id: u32) -> &[u32] {
|
|
175
|
+
match (
|
|
176
|
+
self.child_start.get(id as usize),
|
|
177
|
+
self.child_end.get(id as usize),
|
|
178
|
+
) {
|
|
179
|
+
(Some(start), Some(end)) => &self.children[*start as usize..*end as usize],
|
|
180
|
+
_ => &[],
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/// Whether `ancestor` is a proper ancestor of `id`.
|
|
185
|
+
pub(crate) fn contains(&self, ancestor: u32, id: u32) -> bool {
|
|
186
|
+
let mut current = self.parent(id);
|
|
187
|
+
while let Some(node) = current {
|
|
188
|
+
if node == ancestor {
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
current = self.parent(node);
|
|
192
|
+
}
|
|
193
|
+
false
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/// The parse tree seen through the same interface as the mutable arena.
|
|
198
|
+
pub(crate) struct TreeDom<'a> {
|
|
199
|
+
pub html: &'a Html,
|
|
200
|
+
pub ids: &'a [NodeId],
|
|
201
|
+
pub index: &'a TreeIndex,
|
|
202
|
+
pub options: &'a Value,
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
impl TreeDom<'_> {
|
|
206
|
+
fn node(&self, id: u32) -> Option<ego_tree::NodeRef<'_, Node>> {
|
|
207
|
+
self.html.tree.get(*self.ids.get(id as usize)?)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// The value the arena would hold: attributes are keyed by local name and
|
|
211
|
+
/// a repeated local name keeps its last value.
|
|
212
|
+
pub(crate) fn element_attr(&self, id: u32, name: &str) -> Option<&str> {
|
|
213
|
+
match self.node(id)?.value() {
|
|
214
|
+
Node::Element(element) => element
|
|
215
|
+
.attrs
|
|
216
|
+
.iter()
|
|
217
|
+
.rev()
|
|
218
|
+
.find(|(key, _)| key.local.as_ref() == name)
|
|
219
|
+
.map(|(_, value)| value.as_ref()),
|
|
220
|
+
_ => None,
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
pub(crate) fn is_element(&self, id: u32) -> bool {
|
|
225
|
+
matches!(
|
|
226
|
+
self.node(id).map(|node| node.value()),
|
|
227
|
+
Some(Node::Element(_))
|
|
228
|
+
)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
impl SelectDom for TreeDom<'_> {
|
|
233
|
+
fn kind(&self, id: u32) -> &str {
|
|
234
|
+
match self.node(id).map(|node| node.value()) {
|
|
235
|
+
Some(Node::Element(element)) => match element.name() {
|
|
236
|
+
"script" => "script",
|
|
237
|
+
"style" => "style",
|
|
238
|
+
_ => "tag",
|
|
239
|
+
},
|
|
240
|
+
Some(Node::Text(_)) => "text",
|
|
241
|
+
Some(Node::Comment(_)) => "comment",
|
|
242
|
+
Some(Node::Doctype(_) | Node::ProcessingInstruction(_)) => "directive",
|
|
243
|
+
Some(Node::Document | Node::Fragment) => "root",
|
|
244
|
+
None => "",
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
fn name(&self, id: u32) -> Option<&str> {
|
|
248
|
+
match self.node(id)?.value() {
|
|
249
|
+
Node::Element(element) => Some(element.name()),
|
|
250
|
+
Node::Doctype(_) => Some("!doctype"),
|
|
251
|
+
Node::ProcessingInstruction(instruction) => Some(&instruction.target),
|
|
252
|
+
_ => None,
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
fn attr(&self, id: u32, name: &str) -> Option<&str> {
|
|
256
|
+
self.element_attr(id, name)
|
|
257
|
+
}
|
|
258
|
+
fn parent(&self, id: u32) -> Option<u32> {
|
|
259
|
+
self.index.parent(id)
|
|
260
|
+
}
|
|
261
|
+
fn children(&self, id: u32) -> &[u32] {
|
|
262
|
+
self.index.children(id)
|
|
263
|
+
}
|
|
264
|
+
fn text(&self, id: u32) -> String {
|
|
265
|
+
let mut output = String::new();
|
|
266
|
+
let mut stack = vec![id];
|
|
267
|
+
while let Some(id) = stack.pop() {
|
|
268
|
+
match self.node(id).map(|node| node.value()) {
|
|
269
|
+
Some(Node::Text(text)) => output.push_str(text),
|
|
270
|
+
Some(_) => stack.extend(self.index.children(id).iter().rev().copied()),
|
|
271
|
+
None => {}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
output
|
|
275
|
+
}
|
|
276
|
+
fn xml_mode(&self) -> bool {
|
|
277
|
+
crate::arena::xml_mode(self.options)
|
|
278
|
+
}
|
|
279
|
+
fn lower_case_tags(&self) -> bool {
|
|
280
|
+
self.options
|
|
281
|
+
.get("lowerCaseTags")
|
|
282
|
+
.and_then(Value::as_bool)
|
|
283
|
+
.unwrap_or(!self.xml_mode())
|
|
284
|
+
}
|
|
285
|
+
fn lower_case_attribute_names(&self) -> bool {
|
|
286
|
+
self.options
|
|
287
|
+
.get("lowerCaseAttributeNames")
|
|
288
|
+
.and_then(Value::as_bool)
|
|
289
|
+
.unwrap_or(!self.xml_mode())
|
|
290
|
+
}
|
|
291
|
+
fn custom_pseudo(&self, id: u32, name: &str, arg: Option<&str>) -> Option<bool> {
|
|
292
|
+
let key = format!("{}({})", name, arg.unwrap_or(""));
|
|
293
|
+
self.options
|
|
294
|
+
.get("__pseudoMatches")?
|
|
295
|
+
.get(&key)?
|
|
296
|
+
.as_array()
|
|
297
|
+
.map(|ids| ids.iter().any(|value| value.as_u64() == Some(id as u64)))
|
|
298
|
+
}
|
|
299
|
+
fn pseudo_alias(&self, name: &str) -> Option<&str> {
|
|
300
|
+
self.options
|
|
301
|
+
.get("pseudos")
|
|
302
|
+
.and_then(Value::as_object)
|
|
303
|
+
.and_then(|pseudos| pseudos.get(name))
|
|
304
|
+
.and_then(Value::as_str)
|
|
305
|
+
}
|
|
306
|
+
fn elements_named(&self, lowercase_name: &str) -> Option<&[u32]> {
|
|
307
|
+
self.index
|
|
308
|
+
.name_index
|
|
309
|
+
.get(lowercase_name)
|
|
310
|
+
.map(|v| v.as_slice())
|
|
311
|
+
}
|
|
312
|
+
fn is_id_order_document_order(&self) -> bool {
|
|
313
|
+
true
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/// Whether a class attribute contains `needle` as a whitespace-separated token,
|
|
318
|
+
/// exactly as the arena's `hasClass` reads it.
|
|
319
|
+
pub(crate) fn has_class_token(classes: &str, needle: &str) -> bool {
|
|
320
|
+
!needle.is_empty()
|
|
321
|
+
&& classes.match_indices(needle).any(|(index, _)| {
|
|
322
|
+
(index == 0
|
|
323
|
+
|| classes[..index]
|
|
324
|
+
.chars()
|
|
325
|
+
.next_back()
|
|
326
|
+
.is_some_and(char::is_whitespace))
|
|
327
|
+
&& (index + needle.len() == classes.len()
|
|
328
|
+
|| classes[index + needle.len()..]
|
|
329
|
+
.chars()
|
|
330
|
+
.next()
|
|
331
|
+
.is_some_and(char::is_whitespace))
|
|
332
|
+
})
|
|
333
|
+
}
|