@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,676 @@
|
|
|
1
|
+
//! Consume the parsed HTML as a series of events through a callback.
|
|
2
|
+
//!
|
|
3
|
+
//! While using the [crate::DefaultEmitter] provides an easy-to-use API with low performance, and
|
|
4
|
+
//! implementing your own [crate::Emitter] brings maximal performance and maximal pain, this is a middle
|
|
5
|
+
//! ground. All strings are borrowed from some intermediate buffer instead of individually
|
|
6
|
+
//! allocated.
|
|
7
|
+
//!
|
|
8
|
+
//! ```
|
|
9
|
+
//! // Extract all text between span tags, in a naive (but fast) way. Does not handle tags inside of the span. See `examples/` as well.
|
|
10
|
+
//! use html5gum::{Span, Tokenizer};
|
|
11
|
+
//! use html5gum::emitters::callback::{CallbackEvent, CallbackEmitter};
|
|
12
|
+
//!
|
|
13
|
+
//! let mut is_in_span = false;
|
|
14
|
+
//! let emitter = CallbackEmitter::new(move |event: CallbackEvent<'_>, _span: Span<()>| -> Option<Vec<u8>> {
|
|
15
|
+
//! match event {
|
|
16
|
+
//! CallbackEvent::OpenStartTag { name } => {
|
|
17
|
+
//! is_in_span = name == b"span";
|
|
18
|
+
//! },
|
|
19
|
+
//! CallbackEvent::String { value } if is_in_span => {
|
|
20
|
+
//! return Some(value.to_vec());
|
|
21
|
+
//! }
|
|
22
|
+
//! CallbackEvent::EndTag { .. } => {
|
|
23
|
+
//! is_in_span = false;
|
|
24
|
+
//! }
|
|
25
|
+
//! _ => {}
|
|
26
|
+
//! }
|
|
27
|
+
//!
|
|
28
|
+
//! None
|
|
29
|
+
//! });
|
|
30
|
+
//!
|
|
31
|
+
//! let input = r#"<h1><span class=hello>Hello</span> world!</h1>"#;
|
|
32
|
+
//! let Ok(text_fragments) = Tokenizer::new_with_emitter(input, emitter)
|
|
33
|
+
//! .collect::<Result<Vec<_>, _>>();
|
|
34
|
+
//!
|
|
35
|
+
//! assert_eq!(text_fragments, vec![b"Hello".to_vec()]);
|
|
36
|
+
//! ```
|
|
37
|
+
|
|
38
|
+
use std::collections::VecDeque;
|
|
39
|
+
use std::convert::Infallible;
|
|
40
|
+
use std::marker::PhantomData;
|
|
41
|
+
use std::mem::swap;
|
|
42
|
+
|
|
43
|
+
use crate::utils::trace_log;
|
|
44
|
+
use crate::{naive_next_state, Emitter, Error, Span, SpanBound, State};
|
|
45
|
+
|
|
46
|
+
/// Events used by [CallbackEmitter].
|
|
47
|
+
///
|
|
48
|
+
/// This operates at a slightly lower level than [crate::Token], as start tags are split up into multiple
|
|
49
|
+
/// events.
|
|
50
|
+
#[derive(Debug)]
|
|
51
|
+
pub enum CallbackEvent<'a> {
|
|
52
|
+
/// Visit the `"<mytag"` in `"<mytag mykey=myvalue>"`. Signifies the beginning of a new start
|
|
53
|
+
/// tag.
|
|
54
|
+
///
|
|
55
|
+
/// Attributes have not yet been read.
|
|
56
|
+
OpenStartTag {
|
|
57
|
+
/// The name of the start tag.
|
|
58
|
+
name: &'a [u8],
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
/// Visit an attribute name, for example `"mykey"` in `"<mytag mykey=myvalue>"`.
|
|
62
|
+
///
|
|
63
|
+
/// The attribute value has not yet been read.
|
|
64
|
+
AttributeName {
|
|
65
|
+
/// The name of the attribute.
|
|
66
|
+
name: &'a [u8],
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
/// Visit an attribute value, for example `"myvalue"` in `"<mytag mykey=myvalue>"`.
|
|
70
|
+
///
|
|
71
|
+
/// Things like whitespace, quote handling is taken care of.
|
|
72
|
+
///
|
|
73
|
+
/// After this event, the start tag may be closed using `CloseStartTag`, or another
|
|
74
|
+
/// `AttributeName` may follow.
|
|
75
|
+
AttributeValue {
|
|
76
|
+
/// The value of the attribute.
|
|
77
|
+
value: &'a [u8],
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
/// Visit the end of the start tag, for example `">"` in `"<mytag mykey=myvalue>"`.
|
|
81
|
+
///
|
|
82
|
+
CloseStartTag {
|
|
83
|
+
/// Whether the tag ended with `"/>"`.
|
|
84
|
+
///
|
|
85
|
+
/// Note that in HTML5 this difference is largely ignored, and tags are considered
|
|
86
|
+
/// self-closing based on a hardcoded list of names, not based on syntax.
|
|
87
|
+
self_closing: bool,
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
/// Visit `"</mytag>"`.
|
|
91
|
+
///
|
|
92
|
+
/// Note: Because of strangeness in the HTML spec, attributes may be observed outside of start
|
|
93
|
+
/// tags, before this event. It's best to ignore them as they are not valid HTML, but can still
|
|
94
|
+
/// be observed through most HTML parsers.
|
|
95
|
+
EndTag {
|
|
96
|
+
/// The name of the end tag.
|
|
97
|
+
name: &'a [u8],
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
/// Visit a string, as in, the actual text between tags. The content. Remember actual content
|
|
101
|
+
/// in HTML, before SPAs took over? I remember.
|
|
102
|
+
///
|
|
103
|
+
/// It's guaranteed that all consecutive "character tokens" (as the spec calls them) are folded
|
|
104
|
+
/// into one string event.
|
|
105
|
+
String {
|
|
106
|
+
/// A series of character tokens.
|
|
107
|
+
value: &'a [u8],
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
/// Visit a comment, like `<!-- DON'T HACK THIS WEBSITE -->`
|
|
111
|
+
Comment {
|
|
112
|
+
/// The contents of the comment.
|
|
113
|
+
value: &'a [u8],
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
/// Visit `<!DOCTYPE html>`.
|
|
117
|
+
Doctype {
|
|
118
|
+
/// Name of the docstring.
|
|
119
|
+
name: &'a [u8],
|
|
120
|
+
/// Public identifier (see spec)
|
|
121
|
+
public_identifier: Option<&'a [u8]>,
|
|
122
|
+
/// System identifier (see spec)
|
|
123
|
+
system_identifier: Option<&'a [u8]>,
|
|
124
|
+
/// Enable quirksmode
|
|
125
|
+
force_quirks: bool,
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
/// Visit a parsing error.
|
|
129
|
+
Error(Error),
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
#[derive(Debug, Clone, Copy)]
|
|
133
|
+
enum CurrentTag {
|
|
134
|
+
Start,
|
|
135
|
+
End,
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
#[derive(Debug)]
|
|
139
|
+
struct CallbackState<F, T, S> {
|
|
140
|
+
callback: F,
|
|
141
|
+
emitted_tokens: VecDeque<T>,
|
|
142
|
+
phantom: PhantomData<S>,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// This trait is implemented for all functions that have the same signature as
|
|
146
|
+
/// [Callback::handle_event]. The trait only exists in case you want to implement it on a nameable
|
|
147
|
+
/// type.
|
|
148
|
+
pub trait Callback<T, S: SpanBound> {
|
|
149
|
+
/// Perform some action on a parsing event, and, optionally, return a value that can be yielded
|
|
150
|
+
/// from the [crate::Tokenizer] iterator.
|
|
151
|
+
fn handle_event(&mut self, event: CallbackEvent<'_>, span: Span<S>) -> Option<T>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
impl<F, T, S: SpanBound> Callback<T, S> for F
|
|
155
|
+
where
|
|
156
|
+
F: FnMut(CallbackEvent<'_>, Span<S>) -> Option<T>,
|
|
157
|
+
{
|
|
158
|
+
fn handle_event(&mut self, event: CallbackEvent<'_>, span: Span<S>) -> Option<T> {
|
|
159
|
+
self(event, span)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
impl<F, T, S: SpanBound> CallbackState<F, T, S>
|
|
164
|
+
where
|
|
165
|
+
F: Callback<T, S>,
|
|
166
|
+
{
|
|
167
|
+
fn emit_event(&mut self, event: CallbackEvent<'_>, span: Span<S>) {
|
|
168
|
+
let res = self.callback.handle_event(event, span);
|
|
169
|
+
if let Some(token) = res {
|
|
170
|
+
self.emitted_tokens.push_front(token);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
impl<F, T, S> Default for CallbackState<F, T, S>
|
|
176
|
+
where
|
|
177
|
+
F: Default,
|
|
178
|
+
{
|
|
179
|
+
fn default() -> Self {
|
|
180
|
+
CallbackState {
|
|
181
|
+
callback: F::default(),
|
|
182
|
+
emitted_tokens: VecDeque::default(),
|
|
183
|
+
phantom: PhantomData,
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
#[derive(Debug, Default)]
|
|
189
|
+
struct EmitterState<S: SpanBound> {
|
|
190
|
+
naively_switch_states: bool,
|
|
191
|
+
|
|
192
|
+
current_characters: Vec<u8>,
|
|
193
|
+
current_characters_start: S,
|
|
194
|
+
current_characters_end: S,
|
|
195
|
+
current_comment: Vec<u8>,
|
|
196
|
+
|
|
197
|
+
last_start_tag: Vec<u8>,
|
|
198
|
+
current_tag_had_attributes: bool,
|
|
199
|
+
current_tag_type: Option<CurrentTag>,
|
|
200
|
+
current_tag_self_closing: bool,
|
|
201
|
+
current_tag_name: Vec<u8>,
|
|
202
|
+
current_attribute_name: Vec<u8>,
|
|
203
|
+
current_attribute_value: Vec<u8>,
|
|
204
|
+
current_attribute_name_start: S,
|
|
205
|
+
current_attribute_name_end: S,
|
|
206
|
+
current_attribute_value_start: S,
|
|
207
|
+
current_attribute_value_end: S,
|
|
208
|
+
|
|
209
|
+
// strings related to doctype
|
|
210
|
+
doctype_name: Vec<u8>,
|
|
211
|
+
doctype_has_public_identifier: bool,
|
|
212
|
+
doctype_has_system_identifier: bool,
|
|
213
|
+
doctype_public_identifier: Vec<u8>,
|
|
214
|
+
doctype_system_identifier: Vec<u8>,
|
|
215
|
+
doctype_force_quirks: bool,
|
|
216
|
+
|
|
217
|
+
current_taglike_span: S,
|
|
218
|
+
position: S,
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/// The emitter class to pass to [crate::Tokenizer::new_with_emitter]. Please refer to the
|
|
222
|
+
/// module-level documentation on [crate::emitters::callback] for usage.
|
|
223
|
+
#[derive(Debug)]
|
|
224
|
+
pub struct CallbackEmitter<F, T = Infallible, S: SpanBound = ()>
|
|
225
|
+
where
|
|
226
|
+
// add this requirement, so that users get better errors, if the callback does not implement `Callback`.
|
|
227
|
+
F: Callback<T, S>,
|
|
228
|
+
{
|
|
229
|
+
// this struct is only split out so [CallbackState::emit_event] can borrow things concurrently
|
|
230
|
+
// with other attributes.
|
|
231
|
+
callback_state: CallbackState<F, T, S>,
|
|
232
|
+
emitter_state: EmitterState<S>,
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
impl<F, T, S: SpanBound> Default for CallbackEmitter<F, T, S>
|
|
236
|
+
where
|
|
237
|
+
F: Default + Callback<T, S>,
|
|
238
|
+
{
|
|
239
|
+
fn default() -> Self {
|
|
240
|
+
CallbackEmitter {
|
|
241
|
+
callback_state: CallbackState::default(),
|
|
242
|
+
emitter_state: EmitterState::default(),
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
impl<F, T, S: SpanBound> CallbackEmitter<F, T, S>
|
|
248
|
+
where
|
|
249
|
+
F: Callback<T, S>,
|
|
250
|
+
{
|
|
251
|
+
/// Create a new emitter.
|
|
252
|
+
///
|
|
253
|
+
/// The given callback may return optional tokens that then become available through the
|
|
254
|
+
/// [crate::Tokenizer]'s iterator. If that's not used, return `Option<Infallible>`.
|
|
255
|
+
pub fn new(callback: F) -> Self {
|
|
256
|
+
CallbackEmitter {
|
|
257
|
+
callback_state: CallbackState {
|
|
258
|
+
callback,
|
|
259
|
+
emitted_tokens: VecDeque::new(),
|
|
260
|
+
phantom: PhantomData,
|
|
261
|
+
},
|
|
262
|
+
emitter_state: EmitterState::default(),
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/// Get mutable access to the inner callback.
|
|
267
|
+
pub fn callback_mut(&mut self) -> &mut F {
|
|
268
|
+
&mut self.callback_state.callback
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/// Whether to use [`naive_next_state`] to switch states automatically.
|
|
272
|
+
///
|
|
273
|
+
/// The default is off.
|
|
274
|
+
pub fn naively_switch_states(&mut self, yes: bool) {
|
|
275
|
+
self.emitter_state.naively_switch_states = yes;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
fn flush_attribute_name(&mut self) {
|
|
279
|
+
self.flush_open_start_tag();
|
|
280
|
+
|
|
281
|
+
if !self.emitter_state.current_attribute_name.is_empty() {
|
|
282
|
+
self.callback_state.emit_event(
|
|
283
|
+
CallbackEvent::AttributeName {
|
|
284
|
+
name: &self.emitter_state.current_attribute_name,
|
|
285
|
+
},
|
|
286
|
+
Span {
|
|
287
|
+
start: self.emitter_state.current_attribute_name_start,
|
|
288
|
+
end: self.emitter_state.current_attribute_name_end,
|
|
289
|
+
},
|
|
290
|
+
);
|
|
291
|
+
self.emitter_state.current_attribute_name.clear();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
fn flush_attribute(&mut self) {
|
|
296
|
+
self.flush_attribute_name();
|
|
297
|
+
|
|
298
|
+
if !self.emitter_state.current_attribute_value.is_empty() {
|
|
299
|
+
self.callback_state.emit_event(
|
|
300
|
+
CallbackEvent::AttributeValue {
|
|
301
|
+
value: &self.emitter_state.current_attribute_value,
|
|
302
|
+
},
|
|
303
|
+
Span {
|
|
304
|
+
start: self.emitter_state.current_attribute_value_start,
|
|
305
|
+
end: self.emitter_state.current_attribute_value_end,
|
|
306
|
+
},
|
|
307
|
+
);
|
|
308
|
+
self.emitter_state.current_attribute_value.clear();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
fn flush_open_start_tag(&mut self) {
|
|
313
|
+
self.flush_current_characters();
|
|
314
|
+
|
|
315
|
+
if matches!(self.emitter_state.current_tag_type, Some(CurrentTag::Start))
|
|
316
|
+
&& !self.emitter_state.current_tag_name.is_empty()
|
|
317
|
+
{
|
|
318
|
+
self.callback_state.emit_event(
|
|
319
|
+
CallbackEvent::OpenStartTag {
|
|
320
|
+
name: &self.emitter_state.current_tag_name,
|
|
321
|
+
},
|
|
322
|
+
Span {
|
|
323
|
+
start: self.emitter_state.current_taglike_span,
|
|
324
|
+
end: self.emitter_state.position.offset(-1),
|
|
325
|
+
},
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
self.emitter_state.last_start_tag.clear();
|
|
329
|
+
swap(
|
|
330
|
+
&mut self.emitter_state.last_start_tag,
|
|
331
|
+
&mut self.emitter_state.current_tag_name,
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
fn flush_current_characters(&mut self) {
|
|
337
|
+
if self.emitter_state.current_characters.is_empty() {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
self.callback_state.emit_event(
|
|
342
|
+
CallbackEvent::String {
|
|
343
|
+
value: &self.emitter_state.current_characters,
|
|
344
|
+
},
|
|
345
|
+
Span {
|
|
346
|
+
start: self.emitter_state.current_characters_start,
|
|
347
|
+
end: self.emitter_state.current_characters_end,
|
|
348
|
+
},
|
|
349
|
+
);
|
|
350
|
+
self.emitter_state.current_characters.clear();
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
impl<F, T, S: SpanBound> Emitter for CallbackEmitter<F, T, S>
|
|
355
|
+
where
|
|
356
|
+
F: Callback<T, S>,
|
|
357
|
+
{
|
|
358
|
+
type Token = T;
|
|
359
|
+
|
|
360
|
+
#[inline]
|
|
361
|
+
fn move_position(&mut self, offset: isize) {
|
|
362
|
+
self.emitter_state.position = self.emitter_state.position.offset(offset);
|
|
363
|
+
trace_log!(
|
|
364
|
+
"callbacks: move_position, offset={}, now={:?}",
|
|
365
|
+
offset,
|
|
366
|
+
self.emitter_state.position
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
fn set_last_start_tag(&mut self, last_start_tag: Option<&[u8]>) {
|
|
371
|
+
self.emitter_state.last_start_tag.clear();
|
|
372
|
+
self.emitter_state
|
|
373
|
+
.last_start_tag
|
|
374
|
+
.extend(last_start_tag.unwrap_or_default());
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
fn emit_eof(&mut self) {
|
|
378
|
+
self.flush_current_characters();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
fn emit_error(&mut self, error: Error) {
|
|
382
|
+
self.callback_state.emit_event(
|
|
383
|
+
CallbackEvent::Error(error),
|
|
384
|
+
Span {
|
|
385
|
+
start: self.emitter_state.position,
|
|
386
|
+
end: self.emitter_state.position,
|
|
387
|
+
},
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
fn pop_token(&mut self) -> Option<Self::Token> {
|
|
392
|
+
self.callback_state.emitted_tokens.pop_back()
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
fn init_string(&mut self) {
|
|
396
|
+
// Only reset the start position if we're not already accumulating characters
|
|
397
|
+
// This prevents overwriting the start position when returning from character
|
|
398
|
+
// reference states that have already emitted buffered content
|
|
399
|
+
if self.emitter_state.current_characters.is_empty() {
|
|
400
|
+
self.emitter_state.current_characters_start = self.emitter_state.position;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
fn emit_string(&mut self, s: &[u8]) {
|
|
405
|
+
self.emitter_state.current_characters_end = self.emitter_state.position;
|
|
406
|
+
crate::utils::trace_log!(
|
|
407
|
+
"callbacks: emit_string, len={}, start={:?}, end={:?}",
|
|
408
|
+
s.len(),
|
|
409
|
+
self.emitter_state.current_characters_start,
|
|
410
|
+
self.emitter_state.current_characters_end
|
|
411
|
+
);
|
|
412
|
+
self.emitter_state.current_characters.extend(s);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
fn init_start_tag(&mut self) {
|
|
416
|
+
self.emitter_state.current_tag_name.clear();
|
|
417
|
+
self.emitter_state.current_tag_type = Some(CurrentTag::Start);
|
|
418
|
+
self.emitter_state.current_tag_self_closing = false;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
fn init_end_tag(&mut self) {
|
|
422
|
+
self.emitter_state.current_tag_name.clear();
|
|
423
|
+
self.emitter_state.current_tag_type = Some(CurrentTag::End);
|
|
424
|
+
self.emitter_state.current_tag_had_attributes = false;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
fn init_comment(&mut self) {
|
|
428
|
+
self.flush_current_characters();
|
|
429
|
+
self.emitter_state.current_comment.clear();
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
fn emit_current_tag(&mut self) -> Option<State> {
|
|
433
|
+
self.flush_attribute();
|
|
434
|
+
match self.emitter_state.current_tag_type {
|
|
435
|
+
Some(CurrentTag::Start) => {
|
|
436
|
+
self.flush_open_start_tag();
|
|
437
|
+
let s = self.emitter_state.position;
|
|
438
|
+
self.callback_state.emit_event(
|
|
439
|
+
CallbackEvent::CloseStartTag {
|
|
440
|
+
self_closing: self.emitter_state.current_tag_self_closing,
|
|
441
|
+
},
|
|
442
|
+
Span {
|
|
443
|
+
start: s.offset(-1),
|
|
444
|
+
end: s,
|
|
445
|
+
},
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
Some(CurrentTag::End) => {
|
|
449
|
+
if self.emitter_state.current_tag_had_attributes {
|
|
450
|
+
self.emit_error(Error::EndTagWithAttributes);
|
|
451
|
+
}
|
|
452
|
+
self.emitter_state.last_start_tag.clear();
|
|
453
|
+
self.callback_state.emit_event(
|
|
454
|
+
CallbackEvent::EndTag {
|
|
455
|
+
name: &self.emitter_state.current_tag_name,
|
|
456
|
+
},
|
|
457
|
+
Span {
|
|
458
|
+
start: self.emitter_state.current_taglike_span,
|
|
459
|
+
end: self.emitter_state.position,
|
|
460
|
+
},
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
_ => {}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
let next_state = if self.emitter_state.naively_switch_states {
|
|
467
|
+
naive_next_state(&self.emitter_state.last_start_tag)
|
|
468
|
+
} else {
|
|
469
|
+
None
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
// After flushing characters, initialize a new string span for text-accumulating states
|
|
473
|
+
if next_state.is_some() {
|
|
474
|
+
self.init_string();
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
next_state
|
|
478
|
+
}
|
|
479
|
+
fn emit_current_comment(&mut self) {
|
|
480
|
+
self.callback_state.emit_event(
|
|
481
|
+
CallbackEvent::Comment {
|
|
482
|
+
value: &self.emitter_state.current_comment,
|
|
483
|
+
},
|
|
484
|
+
Span {
|
|
485
|
+
start: self.emitter_state.current_taglike_span,
|
|
486
|
+
end: self.emitter_state.position,
|
|
487
|
+
},
|
|
488
|
+
);
|
|
489
|
+
self.emitter_state.current_comment.clear();
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
fn emit_current_doctype(&mut self) {
|
|
493
|
+
self.callback_state.emit_event(
|
|
494
|
+
CallbackEvent::Doctype {
|
|
495
|
+
name: &self.emitter_state.doctype_name,
|
|
496
|
+
public_identifier: if self.emitter_state.doctype_has_public_identifier {
|
|
497
|
+
Some(&self.emitter_state.doctype_public_identifier)
|
|
498
|
+
} else {
|
|
499
|
+
None
|
|
500
|
+
},
|
|
501
|
+
system_identifier: if self.emitter_state.doctype_has_system_identifier {
|
|
502
|
+
Some(&self.emitter_state.doctype_system_identifier)
|
|
503
|
+
} else {
|
|
504
|
+
None
|
|
505
|
+
},
|
|
506
|
+
force_quirks: self.emitter_state.doctype_force_quirks,
|
|
507
|
+
},
|
|
508
|
+
Span {
|
|
509
|
+
start: self.emitter_state.current_taglike_span,
|
|
510
|
+
end: self.emitter_state.position,
|
|
511
|
+
},
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
fn set_self_closing(&mut self) {
|
|
516
|
+
trace_log!("set_self_closing");
|
|
517
|
+
if matches!(self.emitter_state.current_tag_type, Some(CurrentTag::End)) {
|
|
518
|
+
self.callback_state.emit_event(
|
|
519
|
+
CallbackEvent::Error(Error::EndTagWithTrailingSolidus),
|
|
520
|
+
Span {
|
|
521
|
+
start: self.emitter_state.position,
|
|
522
|
+
end: self.emitter_state.position,
|
|
523
|
+
},
|
|
524
|
+
);
|
|
525
|
+
} else {
|
|
526
|
+
self.emitter_state.current_tag_self_closing = true;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
fn set_force_quirks(&mut self) {
|
|
531
|
+
self.emitter_state.doctype_force_quirks = true;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
fn push_tag_name(&mut self, s: &[u8]) {
|
|
535
|
+
self.emitter_state.current_tag_name.extend(s);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
fn push_comment(&mut self, s: &[u8]) {
|
|
539
|
+
self.emitter_state.current_comment.extend(s);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
fn push_doctype_name(&mut self, s: &[u8]) {
|
|
543
|
+
self.emitter_state.doctype_name.extend(s);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
fn init_doctype(&mut self) {
|
|
547
|
+
self.flush_current_characters();
|
|
548
|
+
self.emitter_state.doctype_name.clear();
|
|
549
|
+
self.emitter_state.doctype_has_public_identifier = false;
|
|
550
|
+
self.emitter_state.doctype_has_system_identifier = false;
|
|
551
|
+
self.emitter_state.doctype_public_identifier.clear();
|
|
552
|
+
self.emitter_state.doctype_system_identifier.clear();
|
|
553
|
+
self.emitter_state.doctype_force_quirks = false;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
fn init_attribute(&mut self) {
|
|
557
|
+
self.flush_open_start_tag();
|
|
558
|
+
self.flush_attribute();
|
|
559
|
+
self.emitter_state.current_tag_had_attributes = true;
|
|
560
|
+
self.emitter_state.current_attribute_name_start = self.emitter_state.position.offset(-1);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
fn push_attribute_name(&mut self, s: &[u8]) {
|
|
564
|
+
self.emitter_state.current_attribute_name.extend(s);
|
|
565
|
+
self.emitter_state.current_attribute_name_end = self.emitter_state.position;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
fn init_attribute_value(&mut self) {
|
|
569
|
+
self.emitter_state.current_attribute_value_start = self.emitter_state.position;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
fn push_attribute_value(&mut self, s: &[u8]) {
|
|
573
|
+
self.flush_attribute_name();
|
|
574
|
+
self.emitter_state.current_attribute_value.extend(s);
|
|
575
|
+
self.emitter_state.current_attribute_value_end = self.emitter_state.position;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
fn set_doctype_public_identifier(&mut self, value: &[u8]) {
|
|
579
|
+
self.emitter_state.doctype_has_public_identifier = true;
|
|
580
|
+
self.emitter_state.doctype_public_identifier.clear();
|
|
581
|
+
self.emitter_state.doctype_public_identifier.extend(value);
|
|
582
|
+
}
|
|
583
|
+
fn set_doctype_system_identifier(&mut self, value: &[u8]) {
|
|
584
|
+
self.emitter_state.doctype_has_system_identifier = true;
|
|
585
|
+
self.emitter_state.doctype_system_identifier.clear();
|
|
586
|
+
self.emitter_state.doctype_system_identifier.extend(value);
|
|
587
|
+
}
|
|
588
|
+
fn push_doctype_public_identifier(&mut self, value: &[u8]) {
|
|
589
|
+
self.emitter_state.doctype_public_identifier.extend(value);
|
|
590
|
+
}
|
|
591
|
+
fn push_doctype_system_identifier(&mut self, value: &[u8]) {
|
|
592
|
+
self.emitter_state.doctype_system_identifier.extend(value);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
fn start_open_tag(&mut self) {
|
|
596
|
+
self.emitter_state.current_taglike_span = self.emitter_state.position.offset(-1);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
fn current_is_appropriate_end_tag_token(&mut self) -> bool {
|
|
600
|
+
if self.emitter_state.last_start_tag.is_empty() {
|
|
601
|
+
crate::utils::trace_log!(
|
|
602
|
+
"current_is_appropriate_end_tag_token: no, because last_start_tag is empty"
|
|
603
|
+
);
|
|
604
|
+
return false;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if !matches!(self.emitter_state.current_tag_type, Some(CurrentTag::End)) {
|
|
608
|
+
crate::utils::trace_log!(
|
|
609
|
+
"current_is_appropriate_end_tag_token: no, because current_tag_type is not end"
|
|
610
|
+
);
|
|
611
|
+
return false;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
crate::utils::trace_log!(
|
|
615
|
+
"current_is_appropriate_end_tag_token: last_start_tag = {:?}",
|
|
616
|
+
self.emitter_state.last_start_tag
|
|
617
|
+
);
|
|
618
|
+
crate::utils::trace_log!(
|
|
619
|
+
"current_is_appropriate_end_tag_token: current_tag = {:?}",
|
|
620
|
+
self.emitter_state.current_tag_name
|
|
621
|
+
);
|
|
622
|
+
self.emitter_state.last_start_tag == self.emitter_state.current_tag_name
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
#[test]
|
|
627
|
+
fn round_trip() {
|
|
628
|
+
use crate::Tokenizer;
|
|
629
|
+
|
|
630
|
+
let mut rt = Vec::new();
|
|
631
|
+
let emitter = CallbackEmitter::new(|event: CallbackEvent<'_>, _: crate::Span<()>| {
|
|
632
|
+
match event {
|
|
633
|
+
CallbackEvent::OpenStartTag { name } => {
|
|
634
|
+
rt.push(b'<');
|
|
635
|
+
rt.extend(name);
|
|
636
|
+
}
|
|
637
|
+
CallbackEvent::AttributeName { name } => {
|
|
638
|
+
rt.push(b' ');
|
|
639
|
+
rt.extend(name);
|
|
640
|
+
}
|
|
641
|
+
CallbackEvent::AttributeValue { value } => {
|
|
642
|
+
rt.push(b'=');
|
|
643
|
+
rt.push(b'"');
|
|
644
|
+
rt.extend(value);
|
|
645
|
+
rt.push(b'"');
|
|
646
|
+
}
|
|
647
|
+
CallbackEvent::String { value } => {
|
|
648
|
+
rt.extend(value);
|
|
649
|
+
}
|
|
650
|
+
CallbackEvent::CloseStartTag { self_closing } => {
|
|
651
|
+
if self_closing {
|
|
652
|
+
rt.push(b'/');
|
|
653
|
+
}
|
|
654
|
+
rt.push(b'>');
|
|
655
|
+
}
|
|
656
|
+
CallbackEvent::EndTag { name } => {
|
|
657
|
+
rt.extend(b"</");
|
|
658
|
+
rt.extend(name);
|
|
659
|
+
rt.push(b'>');
|
|
660
|
+
}
|
|
661
|
+
CallbackEvent::Comment { value } => {
|
|
662
|
+
rt.extend(b"<!--");
|
|
663
|
+
rt.extend(value);
|
|
664
|
+
rt.extend(b"-->");
|
|
665
|
+
}
|
|
666
|
+
CallbackEvent::Doctype { .. } => {}
|
|
667
|
+
CallbackEvent::Error(_) => {}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
None::<core::convert::Infallible>
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
let source = " <!-- a --> <h1>Hello</h1> world <a href=\"foo\" title=\"baz\">bar</a>";
|
|
674
|
+
Tokenizer::new_with_emitter(source, emitter).finish();
|
|
675
|
+
assert_eq!(source.as_bytes(), rt, "{} != {}", source, rt.escape_ascii());
|
|
676
|
+
}
|