rusteerio 0.0.0-stage → 0.1.0

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.
Files changed (206) hide show
  1. package/CONTRIBUTING.md +73 -0
  2. package/Cargo.lock +1051 -0
  3. package/Cargo.toml +36 -0
  4. package/LICENSE +21 -0
  5. package/README.md +252 -2
  6. package/RELEASING.md +138 -0
  7. package/THIRD_PARTY_NOTICES.md +7443 -0
  8. package/build.rs +3 -0
  9. package/dist/chunk-DKULX6VO.mjs +3185 -0
  10. package/dist/chunk-DKULX6VO.mjs.map +1 -0
  11. package/dist/chunk-OHPGEY3E.mjs +32 -0
  12. package/dist/chunk-OHPGEY3E.mjs.map +1 -0
  13. package/dist/index.cjs +3230 -0
  14. package/dist/index.cjs.map +1 -0
  15. package/dist/index.d.mts +113 -0
  16. package/dist/index.d.ts +113 -0
  17. package/dist/index.mjs +38 -0
  18. package/dist/index.mjs.map +1 -0
  19. package/dist/slim.cjs +2441 -0
  20. package/dist/slim.cjs.map +1 -0
  21. package/dist/slim.d.mts +11 -0
  22. package/dist/slim.d.ts +11 -0
  23. package/dist/slim.mjs +19 -0
  24. package/dist/slim.mjs.map +1 -0
  25. package/dist/types-BP6TpxE5.d.mts +532 -0
  26. package/dist/types-BP6TpxE5.d.ts +532 -0
  27. package/dist/utils.cjs +61 -0
  28. package/dist/utils.cjs.map +1 -0
  29. package/dist/utils.d.mts +13 -0
  30. package/dist/utils.d.ts +13 -0
  31. package/dist/utils.mjs +17 -0
  32. package/dist/utils.mjs.map +1 -0
  33. package/native/src/arena.rs +1781 -0
  34. package/native/src/attributes.rs +923 -0
  35. package/native/src/codec.rs +172 -0
  36. package/native/src/diagnostics.rs +1249 -0
  37. package/native/src/extraction.rs +226 -0
  38. package/native/src/gc.rs +166 -0
  39. package/native/src/hook_sink.rs +727 -0
  40. package/native/src/hook_stream.rs +513 -0
  41. package/native/src/lib.rs +2095 -0
  42. package/native/src/location.rs +392 -0
  43. package/native/src/runtime.rs +189 -0
  44. package/native/src/selector.rs +1287 -0
  45. package/native/src/sink.rs +261 -0
  46. package/native/src/stream.rs +334 -0
  47. package/native/src/tokenizer.rs +355 -0
  48. package/native/src/tracker.rs +232 -0
  49. package/native/vendor/README.md +25 -0
  50. package/native/vendor/html5ever-0.35.0/.cargo_vcs_info.json +6 -0
  51. package/native/vendor/html5ever-0.35.0/Cargo.lock +828 -0
  52. package/native/vendor/html5ever-0.35.0/Cargo.toml +85 -0
  53. package/native/vendor/html5ever-0.35.0/LICENSE-APACHE +201 -0
  54. package/native/vendor/html5ever-0.35.0/LICENSE-MIT +25 -0
  55. package/native/vendor/html5ever-0.35.0/README.md +60 -0
  56. package/native/vendor/html5ever-0.35.0/benches/html5ever.rs +86 -0
  57. package/native/vendor/html5ever-0.35.0/data/bench/lipsum-zh.html +19 -0
  58. package/native/vendor/html5ever-0.35.0/data/bench/lipsum.html +40 -0
  59. package/native/vendor/html5ever-0.35.0/data/bench/medium-fragment.html +24 -0
  60. package/native/vendor/html5ever-0.35.0/data/bench/small-fragment.html +7 -0
  61. package/native/vendor/html5ever-0.35.0/data/bench/strong.html +1 -0
  62. package/native/vendor/html5ever-0.35.0/data/bench/tiny-fragment.html +1 -0
  63. package/native/vendor/html5ever-0.35.0/examples/arena.rs +356 -0
  64. package/native/vendor/html5ever-0.35.0/examples/capi/tokenize.c +74 -0
  65. package/native/vendor/html5ever-0.35.0/examples/noop-tokenize.rs +47 -0
  66. package/native/vendor/html5ever-0.35.0/examples/noop-tree-builder.rs +132 -0
  67. package/native/vendor/html5ever-0.35.0/examples/print-tree-actions.rs +185 -0
  68. package/native/vendor/html5ever-0.35.0/examples/tokenize.rs +111 -0
  69. package/native/vendor/html5ever-0.35.0/src/driver.rs +148 -0
  70. package/native/vendor/html5ever-0.35.0/src/lib.rs +30 -0
  71. package/native/vendor/html5ever-0.35.0/src/macros.rs +34 -0
  72. package/native/vendor/html5ever-0.35.0/src/serialize/mod.rs +255 -0
  73. package/native/vendor/html5ever-0.35.0/src/tokenizer/char_ref/mod.rs +451 -0
  74. package/native/vendor/html5ever-0.35.0/src/tokenizer/interface.rs +99 -0
  75. package/native/vendor/html5ever-0.35.0/src/tokenizer/mod.rs +2305 -0
  76. package/native/vendor/html5ever-0.35.0/src/tokenizer/states.rs +97 -0
  77. package/native/vendor/html5ever-0.35.0/src/tree_builder/data.rs +170 -0
  78. package/native/vendor/html5ever-0.35.0/src/tree_builder/mod.rs +1848 -0
  79. package/native/vendor/html5ever-0.35.0/src/tree_builder/rules.rs +1494 -0
  80. package/native/vendor/html5ever-0.35.0/src/tree_builder/tag_sets.rs +114 -0
  81. package/native/vendor/html5ever-0.35.0/src/tree_builder/types.rs +91 -0
  82. package/native/vendor/html5ever-0.35.0/src/util/str.rs +52 -0
  83. package/native/vendor/html5gum-0.8.4/Cargo.lock +1006 -0
  84. package/native/vendor/html5gum-0.8.4/Cargo.toml +109 -0
  85. package/native/vendor/html5gum-0.8.4/LICENSE +19 -0
  86. package/native/vendor/html5gum-0.8.4/README.md +110 -0
  87. package/native/vendor/html5gum-0.8.4/benches/files/0050.html +2131 -0
  88. package/native/vendor/html5gum-0.8.4/benches/files/6D30.html +1004 -0
  89. package/native/vendor/html5gum-0.8.4/benches/files.rs +32 -0
  90. package/native/vendor/html5gum-0.8.4/benches/patterns.rs +31 -0
  91. package/native/vendor/html5gum-0.8.4/src/arrayvec.rs +29 -0
  92. package/native/vendor/html5gum-0.8.4/src/char_validator.rs +134 -0
  93. package/native/vendor/html5gum-0.8.4/src/emitters/callback.rs +676 -0
  94. package/native/vendor/html5gum-0.8.4/src/emitters/default.rs +207 -0
  95. package/native/vendor/html5gum-0.8.4/src/emitters/emitter.rs +344 -0
  96. package/native/vendor/html5gum-0.8.4/src/emitters/html5ever.rs +306 -0
  97. package/native/vendor/html5gum-0.8.4/src/emitters/mod.rs +25 -0
  98. package/native/vendor/html5gum-0.8.4/src/entities.rs +3342 -0
  99. package/native/vendor/html5gum-0.8.4/src/error.rs +96 -0
  100. package/native/vendor/html5gum-0.8.4/src/htmlstring.rs +130 -0
  101. package/native/vendor/html5gum-0.8.4/src/lib.rs +66 -0
  102. package/native/vendor/html5gum-0.8.4/src/machine.rs +2136 -0
  103. package/native/vendor/html5gum-0.8.4/src/machine_helper.rs +277 -0
  104. package/native/vendor/html5gum-0.8.4/src/read_helper.rs +244 -0
  105. package/native/vendor/html5gum-0.8.4/src/reader.rs +440 -0
  106. package/native/vendor/html5gum-0.8.4/src/span.rs +98 -0
  107. package/native/vendor/html5gum-0.8.4/src/state.rs +16 -0
  108. package/native/vendor/html5gum-0.8.4/src/testutils.rs +71 -0
  109. package/native/vendor/html5gum-0.8.4/src/tokenizer.rs +189 -0
  110. package/native/vendor/html5gum-0.8.4/src/utils.rs +88 -0
  111. package/native/vendor/markup5ever-0.35.0/.cargo_vcs_info.json +6 -0
  112. package/native/vendor/markup5ever-0.35.0/Cargo.toml +45 -0
  113. package/native/vendor/markup5ever-0.35.0/LICENSE-APACHE +201 -0
  114. package/native/vendor/markup5ever-0.35.0/LICENSE-MIT +25 -0
  115. package/native/vendor/markup5ever-0.35.0/interface/mod.rs +422 -0
  116. package/native/vendor/markup5ever-0.35.0/interface/tree_builder.rs +280 -0
  117. package/native/vendor/markup5ever-0.35.0/lib.rs +59 -0
  118. package/native/vendor/markup5ever-0.35.0/serialize.rs +77 -0
  119. package/native/vendor/markup5ever-0.35.0/util/buffer_queue.rs +333 -0
  120. package/native/vendor/markup5ever-0.35.0/util/smallcharset.rs +88 -0
  121. package/package.json +137 -4
  122. package/rusteerio.darwin-arm64.node +0 -0
  123. package/rusteerio.darwin-x64.node +0 -0
  124. package/rusteerio.linux-arm64-gnu.node +0 -0
  125. package/rusteerio.linux-arm64-musl.node +0 -0
  126. package/rusteerio.linux-x64-gnu.node +0 -0
  127. package/rusteerio.linux-x64-musl.node +0 -0
  128. package/rusteerio.win32-arm64-msvc.node +0 -0
  129. package/rusteerio.win32-x64-msvc.node +0 -0
  130. package/scripts/check-native.mjs +15 -0
  131. package/scripts/check-release.mjs +235 -0
  132. package/scripts/fix-declarations.mjs +27 -0
  133. package/scripts/generate-notices.mjs +176 -0
  134. package/scripts/notices/README.md +13 -0
  135. package/scripts/notices/cheerio-MIT.txt +21 -0
  136. package/scripts/notices/html5ever-AUTHORS.txt +20 -0
  137. package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
  138. package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
  139. package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
  140. package/scripts/notices/napi-rs-MIT.txt +43 -0
  141. package/scripts/notices/neon-MIT.txt +19 -0
  142. package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
  143. package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
  144. package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
  145. package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
  146. package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
  147. package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
  148. package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
  149. package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
  150. package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
  151. package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
  152. package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
  153. package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
  154. package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
  155. package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
  156. package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
  157. package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
  158. package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
  159. package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
  160. package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
  161. package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
  162. package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
  163. package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
  164. package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
  165. package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
  166. package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
  167. package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
  168. package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
  169. package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
  170. package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
  171. package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
  172. package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
  173. package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
  174. package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
  175. package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
  176. package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
  177. package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
  178. package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
  179. package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
  180. package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
  181. package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
  182. package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
  183. package/scripts/notices/runtime.json +1632 -0
  184. package/scripts/notices/sources.json +555 -0
  185. package/scripts/notices/xray-MIT.txt +21 -0
  186. package/src/attributes.ts +271 -0
  187. package/src/codec.ts +74 -0
  188. package/src/dom.ts +87 -0
  189. package/src/extract-plan.ts +97 -0
  190. package/src/extract.ts +130 -0
  191. package/src/hooks.ts +220 -0
  192. package/src/index.ts +223 -0
  193. package/src/io.ts +491 -0
  194. package/src/native.ts +135 -0
  195. package/src/nodes.ts +290 -0
  196. package/src/parse.ts +53 -0
  197. package/src/parser-types.ts +157 -0
  198. package/src/router.ts +327 -0
  199. package/src/selection.ts +566 -0
  200. package/src/slim.ts +12 -0
  201. package/src/transport.ts +104 -0
  202. package/src/types.ts +186 -0
  203. package/src/utils.ts +29 -0
  204. package/src/workers.ts +21 -0
  205. package/tsconfig.json +15 -0
  206. package/tsup.config.ts +16 -0
@@ -0,0 +1,207 @@
1
+ //! The default emitter is what powers the simple SAX-like API that you see in the README.
2
+ use std::collections::btree_map::Entry;
3
+ use std::collections::BTreeMap;
4
+ use std::mem::take;
5
+
6
+ use crate::{Error, HtmlString, Span, SpanBound, Spanned};
7
+
8
+ use crate::emitters::callback::{Callback, CallbackEmitter, CallbackEvent};
9
+
10
+ use super::{Emitter, ForwardingEmitter};
11
+
12
+ #[derive(Debug, Default)]
13
+ struct OurCallback<S: SpanBound> {
14
+ tag_name: Vec<u8>,
15
+ tag_start_span: S,
16
+ attribute_name: Spanned<HtmlString, S>,
17
+ attribute_map: BTreeMap<HtmlString, Spanned<HtmlString, S>>,
18
+ }
19
+
20
+ impl<S: SpanBound> Callback<Token<S>, S> for OurCallback<S> {
21
+ fn handle_event(&mut self, event: CallbackEvent<'_>, span: Span<S>) -> Option<Token<S>> {
22
+ crate::utils::trace_log!("event: {:?}", event);
23
+ match event {
24
+ CallbackEvent::OpenStartTag { name } => {
25
+ self.tag_name.clear();
26
+ self.tag_name.extend(name);
27
+ self.tag_start_span = span.start;
28
+ None
29
+ }
30
+ CallbackEvent::AttributeName { name } => {
31
+ self.attribute_name.clear();
32
+ match self.attribute_map.entry(name.to_owned().into()) {
33
+ Entry::Occupied(_) => Some(Token::Error(Spanned {
34
+ value: Error::DuplicateAttribute,
35
+ span,
36
+ })),
37
+ Entry::Vacant(vacant) => {
38
+ self.attribute_name.extend(name);
39
+ vacant.insert(Spanned {
40
+ value: Default::default(),
41
+ span,
42
+ });
43
+ None
44
+ }
45
+ }
46
+ }
47
+ CallbackEvent::AttributeValue { value } => {
48
+ if !self.attribute_name.is_empty() {
49
+ let attr = self.attribute_map.get_mut(&*self.attribute_name).unwrap();
50
+ attr.extend(value);
51
+ attr.span.end = span.end.offset(1);
52
+ }
53
+ None
54
+ }
55
+ CallbackEvent::CloseStartTag { self_closing } => Some(Token::StartTag(StartTag {
56
+ self_closing,
57
+ name: take(&mut self.tag_name).into(),
58
+ span: Span {
59
+ start: self.tag_start_span,
60
+ end: span.end,
61
+ },
62
+ attributes: take(&mut self.attribute_map),
63
+ })),
64
+ CallbackEvent::EndTag { name } => {
65
+ self.attribute_map.clear();
66
+ Some(Token::EndTag(EndTag {
67
+ name: name.to_owned().into(),
68
+ span,
69
+ }))
70
+ }
71
+ CallbackEvent::String { value } => Some(Token::String(Spanned {
72
+ value: value.to_owned().into(),
73
+ span,
74
+ })),
75
+ CallbackEvent::Comment { value } => Some(Token::Comment(Spanned {
76
+ value: value.to_owned().into(),
77
+ span,
78
+ })),
79
+ CallbackEvent::Doctype {
80
+ name,
81
+ public_identifier,
82
+ system_identifier,
83
+ force_quirks,
84
+ } => Some(Token::Doctype(Spanned {
85
+ value: Doctype {
86
+ force_quirks,
87
+ name: name.to_owned().into(),
88
+ public_identifier: public_identifier.map(|x| x.to_owned().into()),
89
+ system_identifier: system_identifier.map(|x| x.to_owned().into()),
90
+ },
91
+ span,
92
+ })),
93
+ CallbackEvent::Error(error) => Some(Token::Error(Spanned { value: error, span })),
94
+ }
95
+ }
96
+ }
97
+
98
+ /// This is the emitter you implicitly use with [crate::Tokenizer::new]. Refer to the [crate
99
+ /// docs](crate) for how usage looks like.
100
+ #[derive(Debug)]
101
+ pub struct DefaultEmitter<S: SpanBound = ()> {
102
+ inner: CallbackEmitter<OurCallback<S>, Token<S>, S>,
103
+ }
104
+
105
+ impl Default for DefaultEmitter<()> {
106
+ fn default() -> Self {
107
+ Self {
108
+ inner: Default::default(),
109
+ }
110
+ }
111
+ }
112
+
113
+ impl<S: SpanBound> DefaultEmitter<S> {
114
+ /// Create a new [`DefaultEmitter`] for a certain [`Span`]type which you can pass to
115
+ /// [`crate::Tokenizer::new_with_emitter`].
116
+ #[must_use]
117
+ pub fn new_with_span() -> Self {
118
+ Self {
119
+ inner: Default::default(),
120
+ }
121
+ }
122
+
123
+ /// Whether to use [crate::naive_next_state] to switch states automatically.
124
+ ///
125
+ /// The default is off.
126
+ pub fn naively_switch_states(&mut self, yes: bool) {
127
+ self.inner.naively_switch_states(yes)
128
+ }
129
+ }
130
+
131
+ impl<S: SpanBound> ForwardingEmitter for DefaultEmitter<S> {
132
+ type Token = Token<S>;
133
+
134
+ fn inner(&mut self) -> &mut impl Emitter<Token = Self::Token> {
135
+ &mut self.inner
136
+ }
137
+ }
138
+
139
+ /// A HTML end/close tag, such as `<p>` or `<a>`.
140
+ #[derive(Debug, Clone, PartialEq, Eq, Default)]
141
+ pub struct StartTag<S: SpanBound> {
142
+ /// Whether this tag is self-closing. If it is self-closing, no following [EndTag] should be
143
+ /// expected.
144
+ pub self_closing: bool,
145
+
146
+ /// The start tag's name, such as `"p"` or `"a"`.
147
+ pub name: HtmlString,
148
+
149
+ /// A mapping for any HTML attributes this start tag may have.
150
+ ///
151
+ /// Duplicate attributes are ignored after the first one as per WHATWG spec. Implement your own
152
+ /// [crate::Emitter] to tweak this behavior.
153
+ pub attributes: BTreeMap<HtmlString, Spanned<HtmlString, S>>,
154
+ /// The span of the start tag. Includes exactly the `<p attr="value">`.
155
+ pub span: Span<S>,
156
+ }
157
+
158
+ /// A HTML end/close tag, such as `</p>` or `</a>`.
159
+ #[derive(Debug, Clone, PartialEq, Eq, Default)]
160
+ pub struct EndTag<S: SpanBound> {
161
+ /// The ending tag's name, such as `"p"` or `"a"`.
162
+ pub name: HtmlString,
163
+ /// The span of the end tag. Includes exactly the `</p>`.
164
+ pub span: Span<S>,
165
+ }
166
+
167
+ /// A doctype. Some examples:
168
+ ///
169
+ /// * `<!DOCTYPE {name}>`
170
+ /// * `<!DOCTYPE {name} PUBLIC '{public_identifier}'>`
171
+ /// * `<!DOCTYPE {name} SYSTEM '{system_identifier}'>`
172
+ /// * `<!DOCTYPE {name} PUBLIC '{public_identifier}' '{system_identifier}'>`
173
+ #[derive(Debug, Eq, PartialEq, Clone)]
174
+ pub struct Doctype {
175
+ /// The ["force quirks"](https://html.spec.whatwg.org/#force-quirks-flag) flag.
176
+ pub force_quirks: bool,
177
+
178
+ /// The doctype's name. For HTML documents this is "html".
179
+ pub name: HtmlString,
180
+
181
+ /// The doctype's public identifier.
182
+ pub public_identifier: Option<HtmlString>,
183
+
184
+ /// The doctype's system identifier.
185
+ pub system_identifier: Option<HtmlString>,
186
+ }
187
+
188
+ /// The token type used by default. You can define your own token type by implementing the
189
+ /// [`crate::Emitter`] trait and using [`crate::Tokenizer::new_with_emitter`].
190
+ #[derive(Debug, PartialEq, Eq, Clone)]
191
+ pub enum Token<S: SpanBound = ()> {
192
+ /// A HTML start tag.
193
+ StartTag(StartTag<S>),
194
+ /// A HTML end tag.
195
+ EndTag(EndTag<S>),
196
+ /// A literal string.
197
+ String(Spanned<HtmlString, S>),
198
+ /// A HTML comment.
199
+ Comment(Spanned<HtmlString, S>),
200
+ /// A HTML doctype declaration.
201
+ Doctype(Spanned<Doctype, S>),
202
+ /// A HTML parsing error.
203
+ ///
204
+ /// Can be skipped over, the tokenizer is supposed to recover from the error and continues with
205
+ /// more tokens afterward.
206
+ Error(Spanned<Error, S>),
207
+ }
@@ -0,0 +1,344 @@
1
+ use crate::{Error, State};
2
+
3
+ macro_rules! create_emitter {
4
+ (
5
+ $(#[$trait_outer:meta])+
6
+ pub trait $trait_name:ident {
7
+ $(#[$token:meta])*
8
+ type Token;
9
+ $(
10
+ $(#[$outer:meta])*
11
+ fn $name:ident($($args:tt)*) $(-> $ret:ty)?
12
+ $($body:block)?
13
+ )+
14
+ }
15
+
16
+ $(#[$forward_outer:meta])*
17
+ pub trait $forward_name:ident {
18
+ }
19
+ ) => {
20
+ $(#[$trait_outer])*
21
+ pub trait $trait_name {
22
+ $(#[$token])*
23
+ type Token;
24
+
25
+ $(
26
+ create_emitter!{ @fun $(#[$outer])* fn $name($($args)*) $(-> $ret)? $($body)? }
27
+ )+
28
+ }
29
+
30
+ $(#[$forward_outer])*
31
+ pub trait $forward_name {
32
+ $(#[$token])*
33
+ type Token;
34
+ /// Get a mutable reference to the inner [Emitter].
35
+ fn inner(&mut self) -> &mut impl Emitter<Token = Self::Token>;
36
+
37
+ $(
38
+ create_emitter!{ @default_forward $(#[$outer])* fn $name($($args)*) $(-> $ret)? }
39
+ )+
40
+ }
41
+
42
+ impl<T: $forward_name> $trait_name for T {
43
+ type Token = <Self as $forward_name>::Token;
44
+
45
+ $(
46
+ create_emitter!{ @to_forward $forward_name $(#[$outer])* fn $name($($args)*) $(-> $ret)? }
47
+ )+
48
+ }
49
+ };
50
+ (@fun $(#[$outer:meta])* fn $name:ident($($args:tt)*) $(-> $ret:ty)? $body:block) => {
51
+ $(#[$outer])*
52
+ fn $name($($args)*) $(-> $ret)?
53
+ $body
54
+ };
55
+ (@fun $(#[$outer:meta])* fn $name:ident($($args:tt)*) $(-> $ret:ty)?) => {
56
+ $(#[$outer])*
57
+ fn $name($($args)*) $(-> $ret)?;
58
+ };
59
+ (@default_forward $(#[$outer:meta])* fn $name:ident(&mut self $(,$($arg_name:ident: $arg_ty:ty),* $(,)?)?) $(-> $ret:ty)?) => {
60
+ #[allow(unused_attributes)]
61
+ $(#[$outer])*
62
+ #[inline]
63
+ fn $name(&mut self $(, $($arg_name: $arg_ty),*)?) $(-> $ret)? {
64
+ self.inner().$name($($($arg_name),*)?)
65
+ }
66
+ };
67
+ (@to_forward $forward_name:ident $(#[$outer:meta])* fn $name:ident(&mut self $(,$($arg_name:ident: $arg_ty:ty),* $(,)?)?) $(-> $ret:tt $(<$ret2:tt $(:: $ret3:tt)?>)?)?) => {
68
+ #[allow(unused_attributes)]
69
+ $(#[$outer])*
70
+ #[inline]
71
+ fn $name(&mut self $(, $($arg_name: $arg_ty),*)?) $(-> $ret $(<$ret2 $(:: $ret3)?>)?)? {
72
+ <Self as $forward_name>::$name(self $(, $($arg_name),*)?)
73
+ }
74
+ };
75
+
76
+ }
77
+
78
+ create_emitter! {
79
+ /// An emitter is an object providing methods to the tokenizer to produce tokens.
80
+ ///
81
+ /// If you want to mostly wrap another embedder, consider implementing [ForwardingEmitter].
82
+ ///
83
+ /// Domain-specific applications of the HTML tokenizer can manually implement this trait to
84
+ /// customize per-token allocations, or avoid them altogether.
85
+ ///
86
+ /// An emitter is assumed to have these internal states:
87
+ ///
88
+ /// * _last start tag_: The most recently emitted start tag's name
89
+ /// * _current token_: Can be a tag, doctype or comment token. There's only one current token.
90
+ /// * _current attribute_: The currently processed HTML attribute, consisting of two strings for name and value.
91
+ ///
92
+ /// The following methods are describing what kind of behavior the WHATWG spec expects, but that
93
+ /// doesn't mean you need to follow it. For example:
94
+ ///
95
+ /// * If your usage of the tokenizer will ignore all errors, none of the error handling and
96
+ /// validation requirements apply to you. You can implement `emit_error` as noop and omit all
97
+ /// checks that would emit errors.
98
+ ///
99
+ /// * If you don't care about attributes at all, you can make all related methods a noop.
100
+ ///
101
+ /// The state machine needs to have a functional implementation of
102
+ /// `current_is_appropriate_end_tag_token` to do correct transitions, however.
103
+ pub trait Emitter {
104
+ /// The token type emitted by this emitter. This controls what type of values the [`crate::Tokenizer`]
105
+ /// yields when used as an iterator.
106
+ type Token;
107
+
108
+ /// Set the name of the _last start tag_.
109
+ ///
110
+ /// This is primarily for testing purposes. This is *not* supposed to override the tag name of
111
+ /// the current tag.
112
+ fn set_last_start_tag(&mut self, last_start_tag: Option<&[u8]>)
113
+
114
+ /// The state machine has reached the end of the file. It will soon call `pop_token` for the
115
+ /// last time.
116
+ fn emit_eof(&mut self)
117
+
118
+ /// A (probably recoverable) parsing error has occured.
119
+ fn emit_error(&mut self, error: Error)
120
+
121
+ /// Whether this emitter cares about errors at all.
122
+ ///
123
+ /// If your implementation of `emit_error` is a noop, you can override this function to return
124
+ /// `false` and decorate it with `#[inline]` to aid the compiler in optimizing out more dead
125
+ /// code.
126
+ ///
127
+ /// This method should return the same value at all times. Returning different values on each
128
+ /// call might cause bogus errors to be emitted.
129
+ #[inline]
130
+ #[must_use]
131
+ fn should_emit_errors(&mut self) -> bool {
132
+ // should_emit_errors takes self so that users can implement it as a runtime option in
133
+ // their program. It takes &mut for no particular reason other than _potential_
134
+ // convenience, and just because we can provide that guarantee to the emitter.
135
+ true
136
+ }
137
+
138
+ /// Opt in to rusteerio's internal lossless UTF-16 source protocol.
139
+ fn rusteerio_codec(&mut self) -> bool { false }
140
+
141
+ /// After every state change, the tokenizer calls this method to retrieve a new token that can
142
+ /// be returned via the tokenizer's iterator interface.
143
+ fn pop_token(&mut self) -> Option<Self::Token>
144
+
145
+ /// Start a new string.
146
+ fn init_string(&mut self) {
147
+ }
148
+
149
+ /// Emit a bunch of plain characters as character tokens.
150
+ fn emit_string(&mut self, c: &[u8])
151
+
152
+ /// Emit decoded character-reference bytes. The default is identical to
153
+ /// upstream emission; rusteerio overrides this boundary to escape literal
154
+ /// private-use scalars in its lossless UTF-16 transport.
155
+ fn emit_char_ref(&mut self, value: &[u8], is_attribute: bool) {
156
+ if is_attribute { self.push_attribute_value(value); }
157
+ else { self.emit_string(value); }
158
+ }
159
+
160
+ /// Set the _current token_ to a start tag.
161
+ fn init_start_tag(&mut self)
162
+
163
+ /// Set the _current token_ to an end tag.
164
+ fn init_end_tag(&mut self)
165
+
166
+ /// Set the _current token_ to a comment.
167
+ fn init_comment(&mut self)
168
+
169
+ /// Emit the _current token_, assuming it is a tag.
170
+ ///
171
+ /// Also get the current attribute and append it to the to-be-emitted tag. See docstring for
172
+ /// [`Emitter::init_attribute`] for how duplicates should be handled.
173
+ ///
174
+ /// If a start tag is emitted, update the _last start tag_.
175
+ ///
176
+ /// If the current token is not a start/end tag, this method may panic.
177
+ ///
178
+ /// The return value is used to switch the tokenizer to a new state. Used in tree building.
179
+ ///
180
+ /// If this method always returns `None`, states are never switched, which leads to artifacts
181
+ /// like contents of `<script>` tags being incorrectly interpreted as HTML.
182
+ ///
183
+ /// It's not possible to implement this method correctly in line with the spec without
184
+ /// implementing a full-blown tree builder as per [tree
185
+ /// construction](https://html.spec.whatwg.org/#tree-construction), which this crate does not
186
+ /// offer.
187
+ ///
188
+ /// You can approximate correct behavior using [`naive_next_state`], but the caveats of doing
189
+ /// so are not well-understood.
190
+ ///
191
+ /// See the `tokenize_with_state_switches` cargo example for a practical example where this
192
+ /// matters.
193
+ #[must_use]
194
+ fn emit_current_tag(&mut self) -> Option<State>
195
+
196
+ /// Emit the _current token_, assuming it is a comment.
197
+ ///
198
+ /// If the current token is not a comment, this method may panic.
199
+ fn emit_current_comment(&mut self)
200
+
201
+ /// Emit the _current token_, assuming it is a doctype.
202
+ ///
203
+ /// If the current token is not a doctype, this method may panic.
204
+ fn emit_current_doctype(&mut self)
205
+
206
+ /// Assuming the _current token_ is a start tag, set the self-closing flag.
207
+ ///
208
+ /// If the current token is not a start or end tag, this method may panic.
209
+ ///
210
+ /// If the current token is an end tag, the emitter should emit the
211
+ /// [`crate::Error::EndTagWithTrailingSolidus`] error.
212
+ fn set_self_closing(&mut self)
213
+
214
+ /// Assuming the _current token_ is a doctype, set its "force quirks" flag to true.
215
+ ///
216
+ /// If the current token is not a doctype, this method pay panic.
217
+ fn set_force_quirks(&mut self)
218
+
219
+ /// Assuming the _current token_ is a start/end tag, append a string to the current tag's name.
220
+ ///
221
+ /// If the current token is not a start or end tag, this method may panic.
222
+ fn push_tag_name(&mut self, s: &[u8])
223
+
224
+ /// Assuming the _current token_ is a comment, append a string to the comment's contents.
225
+ ///
226
+ /// If the current token is not a comment, this method may panic.
227
+ fn push_comment(&mut self, s: &[u8])
228
+
229
+ /// Assuming the _current token_ is a doctype, append a string to the doctype's name.
230
+ ///
231
+ /// If the current token is not a doctype, this method may panic.
232
+ fn push_doctype_name(&mut self, s: &[u8])
233
+
234
+ /// Set the _current token_ to a new doctype token:
235
+ ///
236
+ /// * the name should be empty
237
+ /// * the "public identifier" should be null (different from empty)
238
+ /// * the "system identifier" should be null (different from empty)
239
+ /// * the "force quirks" flag should be `false`
240
+ fn init_doctype(&mut self)
241
+
242
+ /// Set the _current attribute_ to a new one, starting with empty name and value strings.
243
+ ///
244
+ /// The old attribute, if any, should be put on the _current token_. If an attribute with that
245
+ /// name already exists, WHATWG says the new one should be ignored and a
246
+ /// [`crate::Error::DuplicateAttribute`] error should be emitted.
247
+ ///
248
+ /// If the current token is an end tag token, a [`crate::Error::EndTagWithAttributes`] error should be
249
+ /// emitted.
250
+ ///
251
+ /// If the current token is no tag at all, this method may panic.
252
+ fn init_attribute(&mut self)
253
+
254
+ /// Start with the attribute value after the aatribute's name.
255
+ ///
256
+ /// If there is no current attribute, this method may panic.
257
+ fn init_attribute_value(&mut self) {
258
+ }
259
+
260
+ /// Append a string to the current attribute's name.
261
+ ///
262
+ /// If there is no current attribute, this method may panic.
263
+ fn push_attribute_name(&mut self, s: &[u8])
264
+
265
+ /// Append a string to the current attribute's value.
266
+ ///
267
+ /// If there is no current attribute, this method may panic.
268
+ fn push_attribute_value(&mut self, s: &[u8])
269
+
270
+ /// Assuming the _current token_ is a doctype, set its "public identifier" to the given string.
271
+ ///
272
+ /// If the current token is not a doctype, this method may panic.
273
+ fn set_doctype_public_identifier(&mut self, value: &[u8])
274
+
275
+ /// Assuming the _current token_ is a doctype, set its "system identifier" to the given string.
276
+ ///
277
+ /// If the current token is not a doctype, this method may panic.
278
+ fn set_doctype_system_identifier(&mut self, value: &[u8])
279
+
280
+ /// Assuming the _current token_ is a doctype, append a string to its "public identifier" to the given string.
281
+ ///
282
+ /// If the current token is not a doctype, this method may panic.
283
+ fn push_doctype_public_identifier(&mut self, s: &[u8])
284
+
285
+ /// Assuming the _current token_ is a doctype, append a string to its "system identifier" to the given string.
286
+ ///
287
+ /// If the current token is not a doctype, this method may panic.
288
+ fn push_doctype_system_identifier(&mut self, s: &[u8])
289
+
290
+ /// Start a new tag/comment or something starting with `<`.
291
+ fn start_open_tag(&mut self) {
292
+ }
293
+
294
+ /// Return true if all of these hold. Return false otherwise.
295
+ ///
296
+ /// * the _current token_ is an end tag
297
+ /// * the _last start tag_ exists
298
+ /// * the current end tag token's name equals to the last start tag's name.
299
+ ///
300
+ /// See also [WHATWG's definition of "appropriate end tag
301
+ /// token"](https://html.spec.whatwg.org/#appropriate-end-tag-token).
302
+ fn current_is_appropriate_end_tag_token(&mut self) -> bool
303
+
304
+ /// By default, this always returns false and thus
305
+ /// all CDATA sections are tokenized as bogus comments.
306
+ ///
307
+ /// See [markup declaration open
308
+ /// state](https://html.spec.whatwg.org/multipage/#markup-declaration-open-state).
309
+ fn adjusted_current_node_present_but_not_in_html_namespace(&mut self) -> bool {
310
+ false
311
+ }
312
+
313
+ /// Move the reader position by the given amount.
314
+ ///
315
+ /// Useful for [crate::Span]s.
316
+ #[inline]
317
+ fn move_position(&mut self, _offset: isize) {}
318
+ }
319
+
320
+ /// An [Emitter] which forwards most methods to an inner emitter.
321
+ ///
322
+ /// If you want to modify most methods, consider implementing [Emitter] directly
323
+ pub trait ForwardingEmitter {}
324
+ }
325
+
326
+ /// Take an educated guess at the next state using the name of a just-now emitted start tag.
327
+ ///
328
+ /// This can be used to implement [`Emitter::emit_current_tag`] for most HTML scraping applications,
329
+ /// but is unsuitable for implementing a browser.
330
+ ///
331
+ /// The mapping was inspired by `lol-html` which has additional safeguards to detect ambiguous
332
+ /// parsing state: <https://github.com/cloudflare/lol-html/blob/f40a9f767c41caf07851548d7470649a6019548c/src/parser/tree_builder_simulator/mod.rs#L73-L86>
333
+ #[must_use]
334
+ pub fn naive_next_state(tag_name: &[u8]) -> Option<State> {
335
+ match tag_name {
336
+ b"textarea" | b"title" => Some(State::RcData),
337
+ b"plaintext" => Some(State::PlainText),
338
+ b"script" => Some(State::ScriptData),
339
+ b"style" | b"iframe" | b"xmp" | b"noembed" | b"noframe" | b"noscript" => {
340
+ Some(State::RawText)
341
+ }
342
+ _ => None,
343
+ }
344
+ }