@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,270 @@
|
|
|
1
|
+
//! HTML documents and fragments.
|
|
2
|
+
|
|
3
|
+
#[cfg(feature = "errors")]
|
|
4
|
+
use std::borrow::Cow;
|
|
5
|
+
use std::fmt;
|
|
6
|
+
use std::iter::FusedIterator;
|
|
7
|
+
|
|
8
|
+
use ego_tree::iter::Nodes;
|
|
9
|
+
use ego_tree::Tree;
|
|
10
|
+
use html5ever::serialize::SerializeOpts;
|
|
11
|
+
use html5ever::tree_builder::QuirksMode;
|
|
12
|
+
use html5ever::{driver, serialize, QualName};
|
|
13
|
+
use selectors::matching::SelectorCaches;
|
|
14
|
+
use tendril::TendrilSink;
|
|
15
|
+
|
|
16
|
+
use crate::selector::Selector;
|
|
17
|
+
use crate::{ElementRef, Node};
|
|
18
|
+
|
|
19
|
+
pub use tree_sink::HtmlTreeSink;
|
|
20
|
+
|
|
21
|
+
/// An HTML tree.
|
|
22
|
+
///
|
|
23
|
+
/// Parsing does not fail hard. Instead, the `quirks_mode` is set and errors are added to the
|
|
24
|
+
/// `errors` field. The `tree` will still be populated as best as possible.
|
|
25
|
+
///
|
|
26
|
+
/// Implements the `TreeSink` trait from the `html5ever` crate, which allows HTML to be parsed.
|
|
27
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
28
|
+
pub struct Html {
|
|
29
|
+
#[cfg(feature = "errors")]
|
|
30
|
+
/// Parse errors.
|
|
31
|
+
pub errors: Vec<Cow<'static, str>>,
|
|
32
|
+
|
|
33
|
+
/// The quirks mode.
|
|
34
|
+
pub quirks_mode: QuirksMode,
|
|
35
|
+
|
|
36
|
+
/// The node tree.
|
|
37
|
+
pub tree: Tree<Node>,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
impl Html {
|
|
41
|
+
/// Creates an empty HTML document.
|
|
42
|
+
pub fn new_document() -> Self {
|
|
43
|
+
Html {
|
|
44
|
+
#[cfg(feature = "errors")]
|
|
45
|
+
errors: Vec::new(),
|
|
46
|
+
quirks_mode: QuirksMode::NoQuirks,
|
|
47
|
+
tree: Tree::new(Node::Document),
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// Creates an empty HTML fragment.
|
|
52
|
+
pub fn new_fragment() -> Self {
|
|
53
|
+
Html {
|
|
54
|
+
#[cfg(feature = "errors")]
|
|
55
|
+
errors: Vec::new(),
|
|
56
|
+
quirks_mode: QuirksMode::NoQuirks,
|
|
57
|
+
tree: Tree::new(Node::Fragment),
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Parses a string of HTML as a document.
|
|
62
|
+
///
|
|
63
|
+
/// This is a convenience method for the following:
|
|
64
|
+
///
|
|
65
|
+
/// ```
|
|
66
|
+
/// # extern crate html5ever;
|
|
67
|
+
/// # extern crate scraper;
|
|
68
|
+
/// # extern crate tendril;
|
|
69
|
+
/// # fn main() {
|
|
70
|
+
/// # let document = "";
|
|
71
|
+
/// use html5ever::driver::{self, ParseOpts};
|
|
72
|
+
/// use scraper::{Html, HtmlTreeSink};
|
|
73
|
+
/// use tendril::TendrilSink;
|
|
74
|
+
///
|
|
75
|
+
/// let parser = driver::parse_document(HtmlTreeSink::new(Html::new_document()), ParseOpts::default());
|
|
76
|
+
/// let html = parser.one(document);
|
|
77
|
+
/// # }
|
|
78
|
+
/// ```
|
|
79
|
+
pub fn parse_document(document: &str) -> Self {
|
|
80
|
+
let parser =
|
|
81
|
+
driver::parse_document(HtmlTreeSink::new(Self::new_document()), Default::default());
|
|
82
|
+
parser.one(document)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/// Parses a string of HTML as a fragment.
|
|
86
|
+
pub fn parse_fragment(fragment: &str) -> Self {
|
|
87
|
+
let parser = driver::parse_fragment(
|
|
88
|
+
HtmlTreeSink::new(Self::new_fragment()),
|
|
89
|
+
Default::default(),
|
|
90
|
+
QualName::new(None, ns!(html), local_name!("body")),
|
|
91
|
+
Vec::new(),
|
|
92
|
+
false,
|
|
93
|
+
);
|
|
94
|
+
parser.one(fragment)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// Returns an iterator over elements matching a selector.
|
|
98
|
+
pub fn select<'a, 'b>(&'a self, selector: &'b Selector) -> Select<'a, 'b> {
|
|
99
|
+
Select {
|
|
100
|
+
inner: self.tree.nodes(),
|
|
101
|
+
selector,
|
|
102
|
+
caches: Default::default(),
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// Returns the root `<html>` element.
|
|
107
|
+
pub fn root_element(&self) -> ElementRef<'_> {
|
|
108
|
+
let root_node = self
|
|
109
|
+
.tree
|
|
110
|
+
.root()
|
|
111
|
+
.children()
|
|
112
|
+
.find(|child| child.value().is_element())
|
|
113
|
+
.expect("html node missing");
|
|
114
|
+
ElementRef::wrap(root_node).unwrap()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// Serialize entire document into HTML.
|
|
118
|
+
pub fn html(&self) -> String {
|
|
119
|
+
let opts = SerializeOpts {
|
|
120
|
+
scripting_enabled: false, // It's not clear what this does.
|
|
121
|
+
traversal_scope: serialize::TraversalScope::IncludeNode,
|
|
122
|
+
create_missing_parent: false,
|
|
123
|
+
};
|
|
124
|
+
let mut buf = Vec::new();
|
|
125
|
+
serialize(&mut buf, self, opts).unwrap();
|
|
126
|
+
String::from_utf8(buf).unwrap()
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// Iterator over elements matching a selector.
|
|
131
|
+
pub struct Select<'a, 'b> {
|
|
132
|
+
inner: Nodes<'a, Node>,
|
|
133
|
+
selector: &'b Selector,
|
|
134
|
+
caches: SelectorCaches,
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
impl fmt::Debug for Select<'_, '_> {
|
|
138
|
+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
139
|
+
fmt.debug_struct("Select")
|
|
140
|
+
.field("inner", &self.inner)
|
|
141
|
+
.field("selector", &self.selector)
|
|
142
|
+
.field("caches", &"..")
|
|
143
|
+
.finish()
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
impl Clone for Select<'_, '_> {
|
|
148
|
+
fn clone(&self) -> Self {
|
|
149
|
+
Self {
|
|
150
|
+
inner: self.inner.clone(),
|
|
151
|
+
selector: self.selector,
|
|
152
|
+
caches: Default::default(),
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
impl<'a> Iterator for Select<'a, '_> {
|
|
158
|
+
type Item = ElementRef<'a>;
|
|
159
|
+
|
|
160
|
+
fn next(&mut self) -> Option<ElementRef<'a>> {
|
|
161
|
+
for node in self.inner.by_ref() {
|
|
162
|
+
if let Some(element) = ElementRef::wrap(node) {
|
|
163
|
+
if element.parent().is_some()
|
|
164
|
+
&& self
|
|
165
|
+
.selector
|
|
166
|
+
.matches_with_scope_and_cache(&element, None, &mut self.caches)
|
|
167
|
+
{
|
|
168
|
+
return Some(element);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
None
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
176
|
+
let (_lower, upper) = self.inner.size_hint();
|
|
177
|
+
|
|
178
|
+
(0, upper)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
impl DoubleEndedIterator for Select<'_, '_> {
|
|
183
|
+
fn next_back(&mut self) -> Option<Self::Item> {
|
|
184
|
+
for node in self.inner.by_ref().rev() {
|
|
185
|
+
if let Some(element) = ElementRef::wrap(node) {
|
|
186
|
+
if element.parent().is_some()
|
|
187
|
+
&& self
|
|
188
|
+
.selector
|
|
189
|
+
.matches_with_scope_and_cache(&element, None, &mut self.caches)
|
|
190
|
+
{
|
|
191
|
+
return Some(element);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
None
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
impl FusedIterator for Select<'_, '_> {}
|
|
200
|
+
|
|
201
|
+
mod serializable;
|
|
202
|
+
mod tree_sink;
|
|
203
|
+
|
|
204
|
+
#[cfg(test)]
|
|
205
|
+
mod tests {
|
|
206
|
+
use super::Html;
|
|
207
|
+
use super::Selector;
|
|
208
|
+
|
|
209
|
+
#[test]
|
|
210
|
+
fn root_element_fragment() {
|
|
211
|
+
let html = Html::parse_fragment(r#"<a href="http://github.com">1</a>"#);
|
|
212
|
+
let root_ref = html.root_element();
|
|
213
|
+
let href = root_ref
|
|
214
|
+
.select(&Selector::parse("a").unwrap())
|
|
215
|
+
.next()
|
|
216
|
+
.unwrap();
|
|
217
|
+
assert_eq!(href.inner_html(), "1");
|
|
218
|
+
assert_eq!(href.value().attr("href").unwrap(), "http://github.com");
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
#[test]
|
|
222
|
+
fn root_element_document_doctype() {
|
|
223
|
+
let html = Html::parse_document("<!DOCTYPE html>\n<title>abc</title>");
|
|
224
|
+
let root_ref = html.root_element();
|
|
225
|
+
let title = root_ref
|
|
226
|
+
.select(&Selector::parse("title").unwrap())
|
|
227
|
+
.next()
|
|
228
|
+
.unwrap();
|
|
229
|
+
assert_eq!(title.inner_html(), "abc");
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
#[test]
|
|
233
|
+
fn root_element_document_comment() {
|
|
234
|
+
let html = Html::parse_document("<!-- comment --><title>abc</title>");
|
|
235
|
+
let root_ref = html.root_element();
|
|
236
|
+
let title = root_ref
|
|
237
|
+
.select(&Selector::parse("title").unwrap())
|
|
238
|
+
.next()
|
|
239
|
+
.unwrap();
|
|
240
|
+
assert_eq!(title.inner_html(), "abc");
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
#[test]
|
|
244
|
+
fn select_is_reversible() {
|
|
245
|
+
let html = Html::parse_document("<p>element1</p><p>element2</p><p>element3</p>");
|
|
246
|
+
let selector = Selector::parse("p").unwrap();
|
|
247
|
+
let result: Vec<_> = html
|
|
248
|
+
.select(&selector)
|
|
249
|
+
.rev()
|
|
250
|
+
.map(|e| e.inner_html())
|
|
251
|
+
.collect();
|
|
252
|
+
assert_eq!(result, vec!["element3", "element2", "element1"]);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
#[test]
|
|
256
|
+
fn select_has_a_size_hint() {
|
|
257
|
+
let html = Html::parse_document("<p>element1</p><p>element2</p><p>element3</p>");
|
|
258
|
+
let selector = Selector::parse("p").unwrap();
|
|
259
|
+
let (lower, upper) = html.select(&selector).size_hint();
|
|
260
|
+
assert_eq!(lower, 0);
|
|
261
|
+
assert_eq!(upper, Some(10));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#[cfg(feature = "atomic")]
|
|
265
|
+
#[test]
|
|
266
|
+
fn html_is_send() {
|
|
267
|
+
fn send_sync<S: Send>() {}
|
|
268
|
+
send_sync::<Html>();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
use std::io::Error;
|
|
2
|
+
|
|
3
|
+
use html5ever::serialize::{Serialize, Serializer, TraversalScope};
|
|
4
|
+
|
|
5
|
+
use crate::Html;
|
|
6
|
+
|
|
7
|
+
impl Serialize for Html {
|
|
8
|
+
fn serialize<S: Serializer>(
|
|
9
|
+
&self,
|
|
10
|
+
serializer: &mut S,
|
|
11
|
+
traversal_scope: TraversalScope,
|
|
12
|
+
) -> Result<(), Error> {
|
|
13
|
+
crate::node::serializable::serialize(self.tree.root(), serializer, traversal_scope)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#[cfg(test)]
|
|
18
|
+
mod tests {
|
|
19
|
+
use crate::Html;
|
|
20
|
+
|
|
21
|
+
#[test]
|
|
22
|
+
fn test_serialize() {
|
|
23
|
+
let src = r#"<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"></head><body><p>Hello world!</p></body></html>"#;
|
|
24
|
+
let html = Html::parse_document(src);
|
|
25
|
+
assert_eq!(html.html(), src);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
use super::Html;
|
|
2
|
+
use crate::node::{Comment, Doctype, Element, Node, ProcessingInstruction, Text};
|
|
3
|
+
use crate::tendril_util::make as make_tendril;
|
|
4
|
+
use ego_tree::NodeId;
|
|
5
|
+
use html5ever::tendril::StrTendril;
|
|
6
|
+
use html5ever::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
|
|
7
|
+
use html5ever::Attribute;
|
|
8
|
+
use html5ever::QualName;
|
|
9
|
+
use std::borrow::Cow;
|
|
10
|
+
use std::cell::{Ref, RefCell};
|
|
11
|
+
|
|
12
|
+
/// Wraps `Html` instances as sinks to drive parsing
|
|
13
|
+
#[derive(Debug)]
|
|
14
|
+
pub struct HtmlTreeSink(pub RefCell<Html>);
|
|
15
|
+
|
|
16
|
+
impl HtmlTreeSink {
|
|
17
|
+
/// Wrap a `Html`instance as a sink to drive parsing
|
|
18
|
+
pub fn new(html: Html) -> Self {
|
|
19
|
+
Self(RefCell::new(html))
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// Note: does not support the `<template>` element.
|
|
24
|
+
impl TreeSink for HtmlTreeSink {
|
|
25
|
+
type Output = Html;
|
|
26
|
+
type Handle = NodeId;
|
|
27
|
+
type ElemName<'a> = Ref<'a, QualName>;
|
|
28
|
+
|
|
29
|
+
fn finish(self) -> Html {
|
|
30
|
+
self.0.into_inner()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Signal a parse error.
|
|
34
|
+
fn parse_error(&self, msg: Cow<'static, str>) {
|
|
35
|
+
#[cfg(feature = "errors")]
|
|
36
|
+
self.0.borrow_mut().errors.push(msg);
|
|
37
|
+
#[cfg(not(feature = "errors"))]
|
|
38
|
+
let _ = msg;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Set the document's quirks mode.
|
|
42
|
+
fn set_quirks_mode(&self, mode: QuirksMode) {
|
|
43
|
+
self.0.borrow_mut().quirks_mode = mode;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Get a handle to the Document node.
|
|
47
|
+
fn get_document(&self) -> Self::Handle {
|
|
48
|
+
self.0.borrow().tree.root().id()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Do two handles refer to the same node?
|
|
52
|
+
fn same_node(&self, x: &Self::Handle, y: &Self::Handle) -> bool {
|
|
53
|
+
x == y
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// What is the name of this element?
|
|
57
|
+
//
|
|
58
|
+
// Should never be called on a non-element node; feel free to panic!.
|
|
59
|
+
fn elem_name<'a>(&'a self, target: &Self::Handle) -> Ref<'a, QualName> {
|
|
60
|
+
Ref::map(self.0.borrow(), |this| {
|
|
61
|
+
&this
|
|
62
|
+
.tree
|
|
63
|
+
.get(*target)
|
|
64
|
+
.unwrap()
|
|
65
|
+
.value()
|
|
66
|
+
.as_element()
|
|
67
|
+
.unwrap()
|
|
68
|
+
.name
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Create an element.
|
|
73
|
+
//
|
|
74
|
+
// When creating a template element (name.expanded() == expanded_name!(html "template")), an
|
|
75
|
+
// associated document fragment called the "template contents" should also be created. Later
|
|
76
|
+
// calls to self.get_template_contents() with that given element return it.
|
|
77
|
+
fn create_element(
|
|
78
|
+
&self,
|
|
79
|
+
name: QualName,
|
|
80
|
+
attrs: Vec<Attribute>,
|
|
81
|
+
_flags: ElementFlags,
|
|
82
|
+
) -> Self::Handle {
|
|
83
|
+
let fragment = name.expanded() == expanded_name!(html "template");
|
|
84
|
+
|
|
85
|
+
let mut this = self.0.borrow_mut();
|
|
86
|
+
let mut node = this.tree.orphan(Node::Element(Element::new(name, attrs)));
|
|
87
|
+
|
|
88
|
+
if fragment {
|
|
89
|
+
node.append(Node::Fragment);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
node.id()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Create a comment node.
|
|
96
|
+
fn create_comment(&self, text: StrTendril) -> Self::Handle {
|
|
97
|
+
self.0
|
|
98
|
+
.borrow_mut()
|
|
99
|
+
.tree
|
|
100
|
+
.orphan(Node::Comment(Comment {
|
|
101
|
+
comment: make_tendril(text),
|
|
102
|
+
}))
|
|
103
|
+
.id()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Append a DOCTYPE element to the Document node.
|
|
107
|
+
fn append_doctype_to_document(
|
|
108
|
+
&self,
|
|
109
|
+
name: StrTendril,
|
|
110
|
+
public_id: StrTendril,
|
|
111
|
+
system_id: StrTendril,
|
|
112
|
+
) {
|
|
113
|
+
let name = make_tendril(name);
|
|
114
|
+
let public_id = make_tendril(public_id);
|
|
115
|
+
let system_id = make_tendril(system_id);
|
|
116
|
+
let doctype = Doctype {
|
|
117
|
+
name,
|
|
118
|
+
public_id,
|
|
119
|
+
system_id,
|
|
120
|
+
};
|
|
121
|
+
self.0
|
|
122
|
+
.borrow_mut()
|
|
123
|
+
.tree
|
|
124
|
+
.root_mut()
|
|
125
|
+
.append(Node::Doctype(doctype));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Append a node as the last child of the given node. If this would produce adjacent sibling
|
|
129
|
+
// text nodes, it should concatenate the text instead.
|
|
130
|
+
//
|
|
131
|
+
// The child node will not already have a parent.
|
|
132
|
+
fn append(&self, parent: &Self::Handle, child: NodeOrText<Self::Handle>) {
|
|
133
|
+
let mut this = self.0.borrow_mut();
|
|
134
|
+
let mut parent = this.tree.get_mut(*parent).unwrap();
|
|
135
|
+
|
|
136
|
+
match child {
|
|
137
|
+
NodeOrText::AppendNode(id) => {
|
|
138
|
+
parent.append_id(id);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
NodeOrText::AppendText(text) => {
|
|
142
|
+
let text = make_tendril(text);
|
|
143
|
+
|
|
144
|
+
let did_concat = parent.last_child().is_some_and(|mut n| match n.value() {
|
|
145
|
+
Node::Text(t) => {
|
|
146
|
+
t.text.push_tendril(&text);
|
|
147
|
+
true
|
|
148
|
+
}
|
|
149
|
+
_ => false,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if !did_concat {
|
|
153
|
+
parent.append(Node::Text(Text { text }));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Append a node as the sibling immediately before the given node. If that node has no parent,
|
|
160
|
+
// do nothing and return Err(new_node).
|
|
161
|
+
//
|
|
162
|
+
// The tree builder promises that sibling is not a text node. However its old previous sibling,
|
|
163
|
+
// which would become the new node's previous sibling, could be a text node. If the new node is
|
|
164
|
+
// also a text node, the two should be merged, as in the behavior of append.
|
|
165
|
+
//
|
|
166
|
+
// NB: new_node may have an old parent, from which it should be removed.
|
|
167
|
+
fn append_before_sibling(&self, sibling: &Self::Handle, new_node: NodeOrText<Self::Handle>) {
|
|
168
|
+
let mut this = self.0.borrow_mut();
|
|
169
|
+
|
|
170
|
+
if let NodeOrText::AppendNode(id) = new_node {
|
|
171
|
+
this.tree.get_mut(id).unwrap().detach();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
let mut sibling = this.tree.get_mut(*sibling).unwrap();
|
|
175
|
+
if sibling.parent().is_some() {
|
|
176
|
+
match new_node {
|
|
177
|
+
NodeOrText::AppendNode(id) => {
|
|
178
|
+
sibling.insert_id_before(id);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
NodeOrText::AppendText(text) => {
|
|
182
|
+
let text = make_tendril(text);
|
|
183
|
+
|
|
184
|
+
let did_concat = sibling.prev_sibling().is_some_and(|mut n| match n.value() {
|
|
185
|
+
Node::Text(t) => {
|
|
186
|
+
t.text.push_tendril(&text);
|
|
187
|
+
true
|
|
188
|
+
}
|
|
189
|
+
_ => false,
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
if !did_concat {
|
|
193
|
+
sibling.insert_before(Node::Text(Text { text }));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Detach the given node from its parent.
|
|
201
|
+
fn remove_from_parent(&self, target: &Self::Handle) {
|
|
202
|
+
self.0.borrow_mut().tree.get_mut(*target).unwrap().detach();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Remove all the children from node and append them to new_parent.
|
|
206
|
+
fn reparent_children(&self, node: &Self::Handle, new_parent: &Self::Handle) {
|
|
207
|
+
self.0
|
|
208
|
+
.borrow_mut()
|
|
209
|
+
.tree
|
|
210
|
+
.get_mut(*new_parent)
|
|
211
|
+
.unwrap()
|
|
212
|
+
.reparent_from_id_append(*node);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Add each attribute to the given element, if no attribute with that name already exists. The
|
|
216
|
+
// tree builder promises this will never be called with something else than an element.
|
|
217
|
+
fn add_attrs_if_missing(&self, target: &Self::Handle, attrs: Vec<Attribute>) {
|
|
218
|
+
let mut this = self.0.borrow_mut();
|
|
219
|
+
let mut node = this.tree.get_mut(*target).unwrap();
|
|
220
|
+
let element = match *node.value() {
|
|
221
|
+
Node::Element(ref mut e) => e,
|
|
222
|
+
_ => unreachable!(),
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
for attr in attrs {
|
|
226
|
+
element
|
|
227
|
+
.attrs
|
|
228
|
+
.or_insert(attr.name, make_tendril(attr.value));
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Get a handle to a template's template contents.
|
|
233
|
+
//
|
|
234
|
+
// The tree builder promises this will never be called with something else than a template
|
|
235
|
+
// element.
|
|
236
|
+
fn get_template_contents(&self, target: &Self::Handle) -> Self::Handle {
|
|
237
|
+
self.0
|
|
238
|
+
.borrow()
|
|
239
|
+
.tree
|
|
240
|
+
.get(*target)
|
|
241
|
+
.unwrap()
|
|
242
|
+
.first_child()
|
|
243
|
+
.unwrap()
|
|
244
|
+
.id()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Mark a HTML <script> element as "already started".
|
|
248
|
+
fn mark_script_already_started(&self, _node: &Self::Handle) {}
|
|
249
|
+
|
|
250
|
+
// Create Processing Instruction.
|
|
251
|
+
fn create_pi(&self, target: StrTendril, data: StrTendril) -> Self::Handle {
|
|
252
|
+
let target = make_tendril(target);
|
|
253
|
+
let data = make_tendril(data);
|
|
254
|
+
self.0
|
|
255
|
+
.borrow_mut()
|
|
256
|
+
.tree
|
|
257
|
+
.orphan(Node::ProcessingInstruction(ProcessingInstruction {
|
|
258
|
+
target,
|
|
259
|
+
data,
|
|
260
|
+
}))
|
|
261
|
+
.id()
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
fn append_based_on_parent_node(
|
|
265
|
+
&self,
|
|
266
|
+
element: &Self::Handle,
|
|
267
|
+
prev_element: &Self::Handle,
|
|
268
|
+
child: NodeOrText<Self::Handle>,
|
|
269
|
+
) {
|
|
270
|
+
let has_parent = self
|
|
271
|
+
.0
|
|
272
|
+
.borrow()
|
|
273
|
+
.tree
|
|
274
|
+
.get(*element)
|
|
275
|
+
.unwrap()
|
|
276
|
+
.parent()
|
|
277
|
+
.is_some();
|
|
278
|
+
|
|
279
|
+
if has_parent {
|
|
280
|
+
self.append_before_sibling(element, child)
|
|
281
|
+
} else {
|
|
282
|
+
self.append(prev_element, child)
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|