1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use crate::scene::{ElementKind, NodeId, SceneGraph};
use html5ever::parse_document;
use html5ever::tendril::TendrilSink;
use markup5ever_rcdom::{Handle, NodeData, RcDom};
pub fn parse_html(html: &str) -> SceneGraph {
let (scene, _, _, _, _) = parse_html_full(html);
scene
}
/// Single-pass parse: returns (scene, inline_styles, inline_scripts, ext_style_hrefs, ext_script_srcs)
pub fn parse_html_full(
html: &str,
) -> (
SceneGraph,
Vec<String>,
Vec<String>,
Vec<String>,
Vec<String>,
) {
let dom = parse_document(RcDom::default(), Default::default())
.from_utf8()
.read_from(&mut html.as_bytes())
.expect("failed to parse HTML");
let mut scene = SceneGraph::new();
let mut styles = Vec::new();
let mut scripts = Vec::new();
let mut ext_styles = Vec::new();
let mut ext_scripts = Vec::new();
walk_node_full(
&dom.document,
&mut scene,
None,
&mut styles,
&mut scripts,
&mut ext_styles,
&mut ext_scripts,
);
(scene, styles, scripts, ext_styles, ext_scripts)
}
fn walk_node_full(
handle: &Handle,
scene: &mut SceneGraph,
parent: Option<NodeId>,
styles: &mut Vec<String>,
scripts: &mut Vec<String>,
ext_styles: &mut Vec<String>,
ext_scripts: &mut Vec<String>,
) {
match &handle.data {
NodeData::Document => {
for child in handle.children.borrow().iter() {
walk_node_full(
child,
scene,
parent,
styles,
scripts,
ext_styles,
ext_scripts,
);
}
}
NodeData::Element { name, attrs, .. } => {
let tag = name.local.to_string();
// Extract <style> contents
if tag == "style" {
for child in handle.children.borrow().iter() {
if let NodeData::Text { contents } = &child.data {
styles.push(contents.borrow().to_string());
}
}
return;
}
// Extract <script> contents or src
if tag == "script" {
let attrs_ref = attrs.borrow();
let src = attrs_ref
.iter()
.find(|a| a.name.local.to_string() == "src")
.map(|a| a.value.to_string());
if let Some(src_path) = src {
ext_scripts.push(src_path);
} else {
for child in handle.children.borrow().iter() {
if let NodeData::Text { contents } = &child.data {
scripts.push(contents.borrow().to_string());
}
}
}
return;
}
// Extract <link rel="stylesheet" href="...">
if tag == "link" {
let attrs_ref = attrs.borrow();
let is_stylesheet = attrs_ref.iter().any(|a| {
a.name.local.to_string() == "rel" && a.value.to_string() == "stylesheet"
});
if is_stylesheet {
if let Some(href) = attrs_ref
.iter()
.find(|a| a.name.local.to_string() == "href")
{
ext_styles.push(href.value.to_string());
}
}
return;
}
// Skip non-renderable head elements
match tag.as_str() {
"head" | "meta" | "title" => {
for child in handle.children.borrow().iter() {
walk_node_full(
child,
scene,
parent,
styles,
scripts,
ext_styles,
ext_scripts,
);
}
return;
}
_ => {}
}
// Build scene node
let kind = ElementKind::from_tag(&tag);
let id = scene.add_node(kind, tag.clone());
let attrs_ref = attrs.borrow();
for attr in attrs_ref.iter() {
let attr_name = attr.name.local.to_string();
let attr_value = attr.value.to_string();
match attr_name.as_str() {
"id" => {
scene.get_mut(id).element_id = Some(attr_value.clone());
}
"class" => {
scene.get_mut(id).classes = attr_value
.split_whitespace()
.map(|s| s.to_string())
.collect();
}
_ => {}
}
if let Some(event) = attr_name.strip_prefix("on") {
scene
.get_mut(id)
.event_handlers
.insert(event.to_string(), attr_value.clone());
}
scene.get_mut(id).attributes.insert(attr_name, attr_value);
}
if let Some(parent_id) = parent {
scene.add_child(parent_id, id);
}
for child in handle.children.borrow().iter() {
walk_node_full(
child,
scene,
Some(id),
styles,
scripts,
ext_styles,
ext_scripts,
);
}
}
NodeData::Text { contents } => {
let text = contents.borrow().to_string();
let trimmed = text.trim();
if trimmed.is_empty() {
return;
}
let id = scene.add_node(ElementKind::Text, "text".to_string());
scene.get_mut(id).text_content = Some(trimmed.to_string());
if let Some(parent_id) = parent {
scene.add_child(parent_id, id);
}
}
_ => {}
}
}
// Legacy single-purpose walker, kept for parse_html() convenience wrapper
#[allow(dead_code)]
fn walk_node(handle: &Handle, scene: &mut SceneGraph, parent: Option<NodeId>) {
let node = handle;
match &node.data {
NodeData::Document => {
for child in node.children.borrow().iter() {
walk_node(child, scene, parent);
}
}
NodeData::Element { name, attrs, .. } => {
let tag = name.local.to_string();
let kind = ElementKind::from_tag(&tag);
// Skip head, meta, title, script (handled separately), link, style (handled separately)
match tag.as_str() {
"head" | "meta" | "title" | "link" => {
// Still walk children to find script/style tags
for child in node.children.borrow().iter() {
if let NodeData::Element { name, .. } = &child.data {
let child_tag = name.local.to_string();
if child_tag == "style" || child_tag == "script" {
walk_node(child, scene, parent);
}
}
}
return;
}
_ => {}
}
let id = scene.add_node(kind, tag.clone());
// Process attributes
let attrs = attrs.borrow();
for attr in attrs.iter() {
let attr_name = attr.name.local.to_string();
let attr_value = attr.value.to_string();
match attr_name.as_str() {
"id" => {
scene.get_mut(id).element_id = Some(attr_value.clone());
}
"class" => {
scene.get_mut(id).classes = attr_value
.split_whitespace()
.map(|s| s.to_string())
.collect();
}
"style" => {
// Inline styles are parsed later by the CSS module
}
_ => {}
}
// Store event handlers
if let Some(event) = attr_name.strip_prefix("on") {
scene
.get_mut(id)
.event_handlers
.insert(event.to_string(), attr_value.clone());
}
scene.get_mut(id).attributes.insert(attr_name, attr_value);
}
if let Some(parent_id) = parent {
scene.add_child(parent_id, id);
}
// Walk children
for child in node.children.borrow().iter() {
walk_node(child, scene, Some(id));
}
}
NodeData::Text { contents } => {
let text = contents.borrow().to_string();
let trimmed = text.trim();
if trimmed.is_empty() {
return;
}
let id = scene.add_node(ElementKind::Text, "text".to_string());
scene.get_mut(id).text_content = Some(trimmed.to_string());
if let Some(parent_id) = parent {
scene.add_child(parent_id, id);
}
}
_ => {
// Comments, processing instructions, etc. - skip
}
}
}