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,185 @@
1
+ // Copyright 2014-2017 The html5ever Project Developers. See the
2
+ // COPYRIGHT file at the top-level directory of this distribution.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5
+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6
+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7
+ // option. This file may not be copied, modified, or distributed
8
+ // except according to those terms.
9
+
10
+ #[macro_use]
11
+ extern crate html5ever;
12
+
13
+ use std::borrow::Cow;
14
+ use std::cell::{Cell, Ref, RefCell};
15
+ use std::collections::HashMap;
16
+ use std::io;
17
+
18
+ use html5ever::parse_document;
19
+ use html5ever::tendril::*;
20
+ use html5ever::tree_builder::{
21
+ AppendNode, AppendText, ElementFlags, NodeOrText, QuirksMode, TreeSink,
22
+ };
23
+ use html5ever::{Attribute, QualName};
24
+
25
+ struct Sink {
26
+ next_id: Cell<usize>,
27
+ names: RefCell<HashMap<usize, &'static QualName>>,
28
+ }
29
+
30
+ impl Sink {
31
+ fn get_id(&self) -> usize {
32
+ let id = self.next_id.get();
33
+ self.next_id.set(id + 2);
34
+ id
35
+ }
36
+ }
37
+
38
+ impl TreeSink for Sink {
39
+ type Handle = usize;
40
+ type Output = Self;
41
+ type ElemName<'a> = Ref<'a, QualName>;
42
+ fn finish(self) -> Self {
43
+ self
44
+ }
45
+
46
+ fn parse_error(&self, msg: Cow<'static, str>) {
47
+ println!("Parse error: {msg}");
48
+ }
49
+
50
+ fn get_document(&self) -> usize {
51
+ 0
52
+ }
53
+
54
+ fn get_template_contents(&self, target: &usize) -> usize {
55
+ if let Some(expanded_name!(html "template")) =
56
+ self.names.borrow().get(target).map(|n| n.expanded())
57
+ {
58
+ target + 1
59
+ } else {
60
+ panic!("not a template element")
61
+ }
62
+ }
63
+
64
+ fn set_quirks_mode(&self, mode: QuirksMode) {
65
+ println!("Set quirks mode to {mode:?}");
66
+ }
67
+
68
+ fn same_node(&self, x: &usize, y: &usize) -> bool {
69
+ x == y
70
+ }
71
+
72
+ fn elem_name(&self, target: &usize) -> Self::ElemName<'_> {
73
+ Ref::map(self.names.borrow(), |map| {
74
+ *map.get(target).expect("not an element")
75
+ })
76
+ }
77
+
78
+ fn create_element(&self, name: QualName, _: Vec<Attribute>, _: ElementFlags) -> usize {
79
+ let id = self.get_id();
80
+ println!("Created {name:?} as {id}");
81
+ // N.B. We intentionally leak memory here to minimize the implementation complexity
82
+ // of this example code. A real implementation would either want to use a real
83
+ // real DOM tree implentation, or else use an arena as the backing store for
84
+ // memory used by the parser.
85
+ self.names
86
+ .borrow_mut()
87
+ .insert(id, Box::leak(Box::new(name)));
88
+ id
89
+ }
90
+
91
+ fn create_comment(&self, text: StrTendril) -> usize {
92
+ let id = self.get_id();
93
+ println!("Created comment \"{}\" as {}", text.escape_default(), id);
94
+ id
95
+ }
96
+
97
+ #[allow(unused_variables)]
98
+ fn create_pi(&self, target: StrTendril, value: StrTendril) -> usize {
99
+ unimplemented!()
100
+ }
101
+
102
+ fn append(&self, parent: &usize, child: NodeOrText<usize>) {
103
+ match child {
104
+ AppendNode(n) => println!("Append node {n} to {parent}"),
105
+ AppendText(t) => println!("Append text to {}: \"{}\"", parent, t.escape_default()),
106
+ }
107
+ }
108
+
109
+ fn append_before_sibling(&self, sibling: &usize, new_node: NodeOrText<usize>) {
110
+ match new_node {
111
+ AppendNode(n) => println!("Append node {n} before {sibling}"),
112
+ AppendText(t) => println!("Append text before {}: \"{}\"", sibling, t.escape_default()),
113
+ }
114
+ }
115
+
116
+ fn append_based_on_parent_node(
117
+ &self,
118
+ element: &Self::Handle,
119
+ _prev_element: &Self::Handle,
120
+ child: NodeOrText<Self::Handle>,
121
+ ) {
122
+ self.append_before_sibling(element, child);
123
+ }
124
+
125
+ fn append_doctype_to_document(
126
+ &self,
127
+ name: StrTendril,
128
+ public_id: StrTendril,
129
+ system_id: StrTendril,
130
+ ) {
131
+ println!("Append doctype: {name} {public_id} {system_id}");
132
+ }
133
+
134
+ fn add_attrs_if_missing(&self, target: &usize, attrs: Vec<Attribute>) {
135
+ assert!(self.names.borrow().contains_key(target), "not an element");
136
+ println!("Add missing attributes to {target}:");
137
+ for attr in attrs.into_iter() {
138
+ println!(" {:?} = {}", attr.name, attr.value);
139
+ }
140
+ }
141
+
142
+ fn associate_with_form(
143
+ &self,
144
+ _target: &usize,
145
+ _form: &usize,
146
+ _nodes: (&usize, Option<&usize>),
147
+ ) {
148
+ // No form owner support.
149
+ }
150
+
151
+ fn remove_from_parent(&self, target: &usize) {
152
+ println!("Remove {target} from parent");
153
+ }
154
+
155
+ fn reparent_children(&self, node: &usize, new_parent: &usize) {
156
+ println!("Move children from {node} to {new_parent}");
157
+ }
158
+
159
+ fn mark_script_already_started(&self, node: &usize) {
160
+ println!("Mark script {node} as already started");
161
+ }
162
+
163
+ fn set_current_line(&self, line_number: u64) {
164
+ println!("Set current line to {line_number}");
165
+ }
166
+
167
+ fn pop(&self, elem: &usize) {
168
+ println!("Popped element {elem}");
169
+ }
170
+ }
171
+
172
+ /// Same example as the "noop-tree-builder", but this time every function implemented in our
173
+ /// Sink object prints a log, so it's easier to get an understating of when each function is
174
+ /// called.
175
+ fn main() {
176
+ let sink = Sink {
177
+ next_id: Cell::new(1),
178
+ names: RefCell::new(HashMap::new()),
179
+ };
180
+ let stdin = io::stdin();
181
+ parse_document(sink, Default::default())
182
+ .from_utf8()
183
+ .read_from(&mut stdin.lock())
184
+ .unwrap();
185
+ }
@@ -0,0 +1,111 @@
1
+ // Copyright 2014-2017 The html5ever Project Developers. See the
2
+ // COPYRIGHT file at the top-level directory of this distribution.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5
+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6
+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7
+ // option. This file may not be copied, modified, or distributed
8
+ // except according to those terms.
9
+
10
+ extern crate html5ever;
11
+
12
+ use std::cell::Cell;
13
+ use std::io;
14
+
15
+ use html5ever::tendril::*;
16
+ use html5ever::tokenizer::BufferQueue;
17
+ use html5ever::tokenizer::{CharacterTokens, EndTag, NullCharacterToken, StartTag, TagToken};
18
+ use html5ever::tokenizer::{
19
+ ParseError, Token, TokenSink, TokenSinkResult, Tokenizer, TokenizerOpts,
20
+ };
21
+
22
+ #[derive(Clone)]
23
+ struct TokenPrinter {
24
+ in_char_run: Cell<bool>,
25
+ }
26
+
27
+ impl TokenPrinter {
28
+ fn is_char(&self, is_char: bool) {
29
+ match (self.in_char_run.get(), is_char) {
30
+ (false, true) => print!("CHAR : \""),
31
+ (true, false) => println!("\""),
32
+ _ => (),
33
+ }
34
+ self.in_char_run.set(is_char);
35
+ }
36
+
37
+ fn do_char(&self, c: char) {
38
+ self.is_char(true);
39
+ print!("{}", c.escape_default().collect::<String>());
40
+ }
41
+ }
42
+
43
+ impl TokenSink for TokenPrinter {
44
+ type Handle = ();
45
+
46
+ fn process_token(&self, token: Token, _line_number: u64) -> TokenSinkResult<()> {
47
+ match token {
48
+ CharacterTokens(b) => {
49
+ for c in b.chars() {
50
+ self.do_char(c);
51
+ }
52
+ },
53
+ NullCharacterToken => self.do_char('\0'),
54
+ TagToken(tag) => {
55
+ self.is_char(false);
56
+ // This is not proper HTML serialization, of course.
57
+ match tag.kind {
58
+ StartTag => print!("TAG : <\x1b[32m{}\x1b[0m", tag.name),
59
+ EndTag => print!("TAG : <\x1b[31m/{}\x1b[0m", tag.name),
60
+ }
61
+ for attr in tag.attrs.iter() {
62
+ print!(
63
+ " \x1b[36m{}\x1b[0m='\x1b[34m{}\x1b[0m'",
64
+ attr.name.local, attr.value
65
+ );
66
+ }
67
+ if tag.self_closing {
68
+ print!(" \x1b[31m/\x1b[0m");
69
+ }
70
+ println!(">");
71
+ },
72
+ ParseError(err) => {
73
+ self.is_char(false);
74
+ println!("ERROR: {err}");
75
+ },
76
+ _ => {
77
+ self.is_char(false);
78
+ println!("OTHER: {token:?}");
79
+ },
80
+ }
81
+ TokenSinkResult::Continue
82
+ }
83
+ }
84
+
85
+ /// In this example we implement the TokenSink trait in such a way that each token is printed.
86
+ /// If a there's an error while processing a token it is printed as well.
87
+ fn main() {
88
+ let sink = TokenPrinter {
89
+ in_char_run: Cell::new(false),
90
+ };
91
+
92
+ // Read HTML from standard input
93
+ let mut chunk = ByteTendril::new();
94
+ io::stdin().read_to_tendril(&mut chunk).unwrap();
95
+
96
+ let input = BufferQueue::default();
97
+ input.push_back(chunk.try_reinterpret().unwrap());
98
+
99
+ let tok = Tokenizer::new(
100
+ sink,
101
+ TokenizerOpts {
102
+ profile: true,
103
+ ..Default::default()
104
+ },
105
+ );
106
+ let _ = tok.feed(&input);
107
+
108
+ assert!(input.is_empty());
109
+ tok.end();
110
+ tok.sink.is_char(false);
111
+ }
@@ -0,0 +1,148 @@
1
+ // Copyright 2014-2017 The html5ever Project Developers. See the
2
+ // COPYRIGHT file at the top-level directory of this distribution.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5
+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6
+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7
+ // option. This file may not be copied, modified, or distributed
8
+ // except according to those terms.
9
+
10
+ //! High-level interface to the parser.
11
+
12
+ use crate::buffer_queue::BufferQueue;
13
+ use crate::tokenizer::{Tokenizer, TokenizerOpts};
14
+ use crate::tree_builder::{create_element, TreeBuilder, TreeBuilderOpts, TreeSink};
15
+ use crate::{Attribute, QualName};
16
+ use markup5ever::TokenizerResult;
17
+ use std::borrow::Cow;
18
+
19
+ use crate::tendril;
20
+ use crate::tendril::stream::{TendrilSink, Utf8LossyDecoder};
21
+ use crate::tendril::StrTendril;
22
+
23
+ /// All-encompassing options struct for the parser.
24
+ #[derive(Clone, Default)]
25
+ pub struct ParseOpts {
26
+ /// Tokenizer options.
27
+ pub tokenizer: TokenizerOpts,
28
+
29
+ /// Tree builder options.
30
+ pub tree_builder: TreeBuilderOpts,
31
+ }
32
+
33
+ /// Parse an HTML document
34
+ ///
35
+ /// The returned value implements `tendril::TendrilSink`
36
+ /// so that Unicode input may be provided incrementally,
37
+ /// or all at once with the `one` method.
38
+ ///
39
+ /// If your input is bytes, use `Parser::from_utf8`.
40
+ pub fn parse_document<Sink>(sink: Sink, opts: ParseOpts) -> Parser<Sink>
41
+ where
42
+ Sink: TreeSink,
43
+ {
44
+ let tb = TreeBuilder::new(sink, opts.tree_builder);
45
+ let tok = Tokenizer::new(tb, opts.tokenizer);
46
+ Parser {
47
+ tokenizer: tok,
48
+ input_buffer: BufferQueue::default(),
49
+ }
50
+ }
51
+
52
+ /// Parse an HTML fragment
53
+ ///
54
+ /// The returned value implements `tendril::TendrilSink`
55
+ /// so that Unicode input may be provided incrementally,
56
+ /// or all at once with the `one` method.
57
+ ///
58
+ /// If your input is bytes, use `Parser::from_utf8`.
59
+ pub fn parse_fragment<Sink>(
60
+ sink: Sink,
61
+ opts: ParseOpts,
62
+ context_name: QualName,
63
+ context_attrs: Vec<Attribute>,
64
+ context_element_allows_scripting: bool,
65
+ ) -> Parser<Sink>
66
+ where
67
+ Sink: TreeSink,
68
+ {
69
+ let context_elem = create_element(&sink, context_name, context_attrs);
70
+ parse_fragment_for_element(
71
+ sink,
72
+ opts,
73
+ context_elem,
74
+ context_element_allows_scripting,
75
+ None,
76
+ )
77
+ }
78
+
79
+ /// Like `parse_fragment`, but with an existing context element
80
+ /// and optionally a form element.
81
+ pub fn parse_fragment_for_element<Sink>(
82
+ sink: Sink,
83
+ opts: ParseOpts,
84
+ context_element: Sink::Handle,
85
+ context_element_allows_scripting: bool,
86
+ form_element: Option<Sink::Handle>,
87
+ ) -> Parser<Sink>
88
+ where
89
+ Sink: TreeSink,
90
+ {
91
+ let tree_builder =
92
+ TreeBuilder::new_for_fragment(sink, context_element, form_element, opts.tree_builder);
93
+ let tokenizer_options = TokenizerOpts {
94
+ initial_state: Some(
95
+ tree_builder.tokenizer_state_for_context_elem(context_element_allows_scripting),
96
+ ),
97
+ ..opts.tokenizer
98
+ };
99
+ let tokenizer = Tokenizer::new(tree_builder, tokenizer_options);
100
+ Parser {
101
+ tokenizer,
102
+ input_buffer: BufferQueue::default(),
103
+ }
104
+ }
105
+
106
+ /// An HTML parser,
107
+ /// ready to receive Unicode input through the `tendril::TendrilSink` trait’s methods.
108
+ pub struct Parser<Sink>
109
+ where
110
+ Sink: TreeSink,
111
+ {
112
+ pub tokenizer: Tokenizer<TreeBuilder<Sink::Handle, Sink>>,
113
+ pub input_buffer: BufferQueue,
114
+ }
115
+
116
+ impl<Sink: TreeSink> TendrilSink<tendril::fmt::UTF8> for Parser<Sink> {
117
+ fn process(&mut self, t: StrTendril) {
118
+ self.input_buffer.push_back(t);
119
+ // FIXME: Properly support </script> somehow.
120
+ while let TokenizerResult::Script(_) = self.tokenizer.feed(&self.input_buffer) {}
121
+ }
122
+
123
+ // FIXME: Is it too noisy to report every character decoding error?
124
+ fn error(&mut self, desc: Cow<'static, str>) {
125
+ self.tokenizer.sink.sink.parse_error(desc)
126
+ }
127
+
128
+ type Output = Sink::Output;
129
+
130
+ fn finish(self) -> Self::Output {
131
+ // FIXME: Properly support </script> somehow.
132
+ while let TokenizerResult::Script(_) = self.tokenizer.feed(&self.input_buffer) {}
133
+ assert!(self.input_buffer.is_empty());
134
+ self.tokenizer.end();
135
+ self.tokenizer.sink.sink.finish()
136
+ }
137
+ }
138
+
139
+ impl<Sink: TreeSink> Parser<Sink> {
140
+ /// Wrap this parser into a `TendrilSink` that accepts UTF-8 bytes.
141
+ ///
142
+ /// Use this when your input is bytes that are known to be in the UTF-8 encoding.
143
+ /// Decoding is lossy, like `String::from_utf8_lossy`.
144
+ #[allow(clippy::wrong_self_convention)]
145
+ pub fn from_utf8(self) -> Utf8LossyDecoder<Self> {
146
+ Utf8LossyDecoder::new(self)
147
+ }
148
+ }
@@ -0,0 +1,30 @@
1
+ // Copyright 2014-2017 The html5ever Project Developers. See the
2
+ // COPYRIGHT file at the top-level directory of this distribution.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5
+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6
+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7
+ // option. This file may not be copied, modified, or distributed
8
+ // except according to those terms.
9
+
10
+ #![crate_name = "html5ever"]
11
+ #![crate_type = "dylib"]
12
+ #![cfg_attr(test, deny(warnings))]
13
+ #![allow(unused_parens)]
14
+ #![warn(unreachable_pub)]
15
+
16
+ pub use driver::{parse_document, parse_fragment, ParseOpts, Parser};
17
+ pub use markup5ever::*;
18
+
19
+ pub use serialize::serialize;
20
+
21
+ mod util {
22
+ pub(crate) mod str;
23
+ }
24
+
25
+ pub(crate) mod macros;
26
+
27
+ pub mod driver;
28
+ pub mod serialize;
29
+ pub mod tokenizer;
30
+ pub mod tree_builder;
@@ -0,0 +1,34 @@
1
+ // Copyright 2014-2017 The html5ever Project Developers. See the
2
+ // COPYRIGHT file at the top-level directory of this distribution.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5
+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6
+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7
+ // option. This file may not be copied, modified, or distributed
8
+ // except according to those terms.
9
+
10
+ macro_rules! unwrap_or_return {
11
+ ($opt:expr) => {{
12
+ let Some(x) = $opt else {
13
+ return;
14
+ };
15
+ x
16
+ }};
17
+ ($opt:expr, $retval:expr) => {{
18
+ let Some(x) = $opt else {
19
+ return $retval;
20
+ };
21
+ x
22
+ }};
23
+ }
24
+ pub(crate) use unwrap_or_return;
25
+
26
+ macro_rules! time {
27
+ ($e:expr) => {{
28
+ let t0 = ::std::time::Instant::now();
29
+ let result = $e;
30
+ let dt = t0.elapsed().as_nanos() as u64;
31
+ (result, dt)
32
+ }};
33
+ }
34
+ pub(crate) use time;