htui 0.2.0

A terminal HTML/CSS renderer with an embedded JavaScript engine — build interactive TUIs from HTML, CSS and JS
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
use indexmap::IndexMap;
use std::sync::Arc;

use html5ever::driver::ParseOpts;
use html5ever::tendril::TendrilSink;
use html5ever::{LocalName, QualName, namespace_url, ns};
use log::{debug, info, warn};
use markup5ever_rcdom::{Handle, NodeData, RcDom};

use crate::resources;
use crate::source::SharedSourceText;
use crate::tdom::node::{Node, NodeArena, NodeId, NodeType, SharedNodeArena, document_base_href};

/// Parsed HTML document: the DOM arena plus the `<style>` CSS and `<script>`
/// JS sources extracted during parsing (inline, or external files loaded via
/// `<link rel="stylesheet">` / `<script src="...">`).
#[derive(Debug, Clone)]
pub struct ParsedDocument {
    pub root_id: NodeId,
    pub arena: SharedNodeArena,
    pub css: SharedSourceText,
    pub scripts: Arc<[String]>,
}

/// HTML parser.
#[derive(Debug)]
pub struct Parser {
    input: String,
}

impl Parser {
    /// Create a parser for the given HTML source.
    pub fn new(input: &str) -> Self {
        Self {
            input: input.to_string(),
        }
    }

    /// Parse HTML and return (root_id, arena, embedded_css).
    pub fn parse(&mut self) -> (NodeId, NodeArena, String) {
        let doc = self.parse_with_scripts();
        let arena = (*doc.arena).clone();
        (doc.root_id, arena, doc.css.to_string())
    }

    /// Parse HTML and return the arena plus the embedded `<style>` CSS and
    /// `<script>` JS sources.
    pub fn parse_with_scripts(&mut self) -> ParsedDocument {
        let opts = ParseOpts::default();
        let dom = html5ever::parse_document(RcDom::default(), opts).one(self.input.clone());

        let mut arena = NodeArena::default();
        let root_id = arena.insert(Node::from(NodeType::Document));
        let mut css_sources: Vec<String> = Vec::new();
        let mut scripts: Vec<String> = Vec::new();

        convert_children(
            &dom.document,
            root_id,
            &mut arena,
            &mut css_sources,
            &mut scripts,
        );

        debug!(
            "parse done: root={}, {} B, CSS: {} B, JS: {} B",
            root_id,
            arena.len(),
            css_sources.join("\n").len(),
            scripts.join("\n").len()
        );
        let arena = Arc::new(arena);
        let css = css_sources.join("\n");
        ParsedDocument {
            root_id,
            arena,
            css: Arc::from(css),
            scripts: scripts.into(),
        }
    }
}

fn convert_attributes(html5_attrs: &[html5ever::Attribute]) -> IndexMap<String, String> {
    // Insertion order mirrors the source document (html5ever reports
    // attributes in source order), so the `innerHTML` serializer can reproduce
    // a browser's attribute order.
    html5_attrs
        .iter()
        .map(|a| (a.name.local.to_string(), a.value.to_string()))
        .collect()
}

/// How embedded `<style>` / `<script>` content is treated during conversion.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ConvertMode {
    /// Top-level document parse: `<style>` CSS and `<script>` sources are
    /// extracted (CSS is applied, scripts run).
    Document,
    /// Inside a `<template>`: everything is inert — `<style>` stays a DOM
    /// node and `<script>` is flattened as plain text (template content never
    /// renders or executes).
    Template,
    /// `innerHTML` fragment: `<style>` CSS is extracted (the browser applies
    /// it), `<script>` is skipped entirely (the browser creates the element
    /// but never runs it; htui has no script DOM node).
    InnerHtml,
}

fn convert_children(
    handle: &Handle,
    parent_id: NodeId,
    arena: &mut NodeArena,
    css_sources: &mut Vec<String>,
    scripts: &mut Vec<String>,
) {
    convert_children_with_mode(
        handle,
        parent_id,
        arena,
        css_sources,
        scripts,
        ConvertMode::Document,
    );
}

/// Recursive DOM conversion, treating `<style>` / `<script>` according to the
/// conversion context (see [`ConvertMode`]).
fn convert_children_with_mode(
    handle: &Handle,
    parent_id: NodeId,
    arena: &mut NodeArena,
    css_sources: &mut Vec<String>,
    scripts: &mut Vec<String>,
    mode: ConvertMode,
) {
    let children = handle.children.borrow().clone();
    for child_handle in &children {
        convert_one_child(child_handle, parent_id, arena, css_sources, scripts, mode);
    }
}

/// Convert one rcdom child node into the tdom arena under `parent_id`.
fn convert_one_child(
    child_handle: &Handle,
    parent_id: NodeId,
    arena: &mut NodeArena,
    css_sources: &mut Vec<String>,
    scripts: &mut Vec<String>,
    mode: ConvertMode,
) {
    match &child_handle.data {
        NodeData::Document => {
            convert_children_with_mode(child_handle, parent_id, arena, css_sources, scripts, mode);
        }
        NodeData::Element {
            name,
            attrs,
            template_contents,
            ..
        } => {
            let tag = name.local.as_ref();
            if tag.eq_ignore_ascii_case("script") {
                match mode {
                    // Top-level parse: the source is extracted verbatim (like
                    // `<style>`) rather than becoming a DOM node — the script
                    // layer evaluates it against the rendered document.
                    ConvertMode::Document => {
                        // `<script src="...">` loads an external script
                        // through [`resources::load`] (a `mem://` virtual key;
                        // prefix optional). A `src` attribute wins over any
                        // inline content, mirroring the browser's rule that an
                        // external script's body is ignored.
                        let script_attrs = {
                            let ar = attrs.borrow();
                            convert_attributes(&ar)
                        };
                        if let Some(src) = script_attrs.get("src") {
                            // Relative `src` resolves against the document base
                            // URL (`<base href>`) established earlier in the
                            // document.
                            let src = resources::resolve_url(
                                document_base_href(arena, parent_id).as_deref(),
                                src,
                            );
                            info!("外部脚本: {}", src);
                            match load_text(&src) {
                                Ok(js) if !js.trim().is_empty() => scripts.push(js),
                                Ok(_) => warn!("外部脚本 {} 为空", src),
                                Err(e) => warn!("读取 {} 失败: {}", src, e),
                            }
                        } else {
                            let js = extract_text_content(child_handle);
                            if !js.trim().is_empty() {
                                info!("提取 <script> JS: {} 字节", js.len());
                                scripts.push(js);
                            }
                        }
                    }
                    // Template content: no script DOM node type exists, so the
                    // source is flattened as plain text (inert).
                    ConvertMode::Template => {
                        convert_children_with_mode(
                            child_handle,
                            parent_id,
                            arena,
                            css_sources,
                            scripts,
                            ConvertMode::Template,
                        );
                    }
                    // `innerHTML`: the browser creates the element but never
                    // executes it; htui has no script DOM node, so the block
                    // is dropped entirely (it must not render its source).
                    ConvertMode::InnerHtml => {}
                }
                return;
            }
            match tag_name_to_node_type(tag) {
                Some(node_type) => {
                    // `<style>` inside a template stays an inert DOM node;
                    // on the document / `innerHTML` paths its CSS is extracted
                    // and applied.
                    if node_type == NodeType::Style
                        && matches!(mode, ConvertMode::Document | ConvertMode::InnerHtml)
                    {
                        let css = extract_text_content(child_handle);
                        if !css.trim().is_empty() {
                            info!("提取 <style> CSS: {} 字节", css.len());
                            css_sources.push(css);
                        }
                        return;
                    }
                    if node_type == NodeType::Link {
                        let link_attrs = {
                            let ar = attrs.borrow();
                            convert_attributes(&ar)
                        };
                        let loads_css =
                            matches!(mode, ConvertMode::Document | ConvertMode::InnerHtml);
                        if loads_css
                            && let (Some(rel), Some(href)) =
                                (link_attrs.get("rel"), link_attrs.get("href"))
                            && rel.to_lowercase().contains("stylesheet")
                        {
                            // Relative `href` resolves against the
                            // document base URL (`<base href>`).
                            let href = resources::resolve_url(
                                document_base_href(arena, parent_id).as_deref(),
                                href,
                            );
                            info!("外部样式表: {}", href);
                            match load_text(&href) {
                                Ok(c) => {
                                    css_sources.push(c);
                                }
                                Err(e) => warn!("读取 {} 失败: {}", href, e),
                            }
                        }
                        // Fall through: `<link>` stays in the DOM as a
                        // non-rendered node (stylesheets are extracted
                        // above), so it is addressable by CSS / DOM
                        // queries like a browser's `<link>`.
                    }
                    let child_id = arena.insert(Node::from(node_type));
                    {
                        let ar = attrs.borrow();
                        arena[child_id].attributes = convert_attributes(&ar);
                    }
                    Node::append_child(arena, parent_id, child_id);
                    if node_type == NodeType::Template {
                        // The parser moves a template's children into a
                        // dedicated contents fragment; walk that instead of
                        // the (empty) element child list. The content stays
                        // inert and never renders.
                        if let Some(contents) = template_contents.borrow().as_ref() {
                            convert_children_with_mode(
                                contents,
                                child_id,
                                arena,
                                css_sources,
                                scripts,
                                ConvertMode::Template,
                            );
                        }
                    } else {
                        convert_children_with_mode(
                            child_handle,
                            child_id,
                            arena,
                            css_sources,
                            scripts,
                            mode,
                        );
                    }
                }
                None => {
                    warn!("不支持标签 <{}>,压平", tag);
                    convert_children_with_mode(
                        child_handle,
                        parent_id,
                        arena,
                        css_sources,
                        scripts,
                        mode,
                    );
                }
            }
        }
        NodeData::Text { contents } => {
            let text = contents.borrow().to_string();
            // Every text node is preserved verbatim (including
            // whitespace-only runs) so the DOM mirrors the source and
            // `childNodes` / `textContent` stay faithful. The layout pass
            // decides whether a whitespace-only run actually paints
            // (see `Node::text_is_painted`), so rendering is unchanged.
            let text_id = arena.insert(Node::text(&text));
            Node::append_child(arena, parent_id, text_id);
        }
        NodeData::Comment { contents } => {
            // Comments are DOM nodes (`nodeType` 8) that never render.
            let comment_id = arena.insert(Node::comment(contents));
            Node::append_child(arena, parent_id, comment_id);
        }
        NodeData::Doctype { name, .. } => {
            let doctype_id = arena.insert(Node::doctype(name));
            Node::append_child(arena, parent_id, doctype_id);
        }
        NodeData::ProcessingInstruction { .. } => {}
    }
}

/// Parse an HTML fragment and attach it to `parent_id`, replacing that node's
/// existing children (the `innerHTML` setter semantics).
///
/// The fragment is parsed with html5ever's fragment parser in the context of
/// the target element's tag, so table / list insertion modes match the
/// browser. `<style>` CSS (and `<link rel=stylesheet>` bodies loaded through
/// the resource layer) is pushed to `css_sources`; `<script>` blocks are
/// skipped entirely — the browser creates script elements but never runs
/// them, and htui has no script DOM node to keep.
pub fn parse_fragment_into(
    arena: &mut NodeArena,
    parent_id: NodeId,
    html: &str,
    css_sources: &mut Vec<String>,
) {
    let context_tag = arena
        .get(parent_id)
        .and_then(|n| n.node_type.tag_name())
        .unwrap_or("div");
    let ctx = QualName::new(None, ns!(html), LocalName::from(context_tag));
    let dom = html5ever::parse_fragment(RcDom::default(), ParseOpts::default(), ctx, Vec::new())
        .one(html);

    // Replacement semantics: the target's existing children are dropped first.
    let old_children = arena
        .get_mut(parent_id)
        .map(|n| std::mem::take(&mut n.children))
        .unwrap_or_default();
    for child in old_children {
        if let Some(c) = arena.get_mut(child) {
            c.parent = None;
        }
    }

    // `parse_fragment` wraps the fragment in a synthetic `<html>` element;
    // unwrap it so only the fragment's own nodes become children.
    let document_children = dom.document.children.borrow().clone();
    let mut scripts = Vec::new();
    for child in &document_children {
        if let NodeData::Element { name, .. } = &child.data
            && name.local.as_ref().eq_ignore_ascii_case("html")
        {
            convert_children_with_mode(
                child,
                parent_id,
                arena,
                css_sources,
                &mut scripts,
                ConvertMode::InnerHtml,
            );
            return;
        }
    }
    for child in &document_children {
        convert_one_child(
            child,
            parent_id,
            arena,
            css_sources,
            &mut scripts,
            ConvertMode::InnerHtml,
        );
    }
}

fn extract_text_content(handle: &Handle) -> String {
    let children = handle.children.borrow();
    let mut text = String::new();
    for child in children.iter() {
        if let NodeData::Text { contents } = &child.data {
            text.push_str(&contents.borrow());
        }
    }
    text
}

/// Load a `src` / `href` reference as UTF-8 text through [`resources::load`]
/// (a `mem://` key; the prefix is optional).
///
/// Non-UTF-8 content yields an error, matching how a script or stylesheet
/// body must be text.
fn load_text(src: &str) -> Result<String, String> {
    let bytes = resources::load(src).map_err(|e| e.to_string())?;
    String::from_utf8(bytes.as_ref().to_vec()).map_err(|e| e.to_string())
}

/// Maps an HTML tag name to its corresponding [`NodeType`].
///
/// Returns `None` for unsupported tags (they are flattened).
///
/// Delegates to [`NodeType::from_tag_name`] — the single tag table shared
/// with CSS selector parsing — so both mappings can never drift apart.
#[doc(hidden)]
pub fn tag_name_to_node_type(tag: &str) -> Option<NodeType> {
    NodeType::from_tag_name(tag)
}

/// Serialize a node's children as an HTML fragment (the `innerHTML` getter).
///
/// Mirrors the browser's fragment serialization algorithm: text escapes `&`
/// `<` `>` and non-breaking spaces, comments keep their delimiters, attribute
/// values escape `&` `"` and non-breaking spaces, and void elements emit no
/// closing tag. Attributes are emitted in source order (preserved by the
/// `IndexMap` attribute store).
pub fn serialize_inner_html(arena: &NodeArena, node_id: NodeId) -> String {
    let Some(node) = arena.get(node_id) else {
        return String::new();
    };
    let mut out = String::new();
    for &child in &node.children {
        serialize_node(arena, child, &mut out);
    }
    out
}

/// Serialize an element **including its own tags** (the `outerHTML`
/// getter). Non-element nodes fall back to their fragment serialization
/// (text / comment bodies), like the DOM serializer's node form.
pub fn serialize_outer_html(arena: &NodeArena, node_id: NodeId) -> String {
    let mut out = String::new();
    serialize_node(arena, node_id, &mut out);
    out
}

/// Append the serialized form of `node_id` to `out`.
fn serialize_node(arena: &NodeArena, node_id: NodeId, out: &mut String) {
    let node = &arena[node_id];
    match node.node_type {
        NodeType::Text => {
            if let Some(data) = &node.data {
                escape_text(data, out);
            }
        }
        NodeType::Comment => {
            out.push_str("<!--");
            if let Some(data) = &node.data {
                out.push_str(data);
            }
            out.push_str("-->");
        }
        NodeType::DocumentFragment => {
            for &child in &node.children {
                serialize_node(arena, child, out);
            }
        }
        _ => {
            let Some(tag) = node.node_type.tag_name() else {
                return;
            };
            // Attributes serialize in insertion order (source order), which
            // the fragment parser preserves via `IndexMap`.
            out.push('<');
            out.push_str(tag);
            for (k, v) in &node.attributes {
                out.push(' ');
                out.push_str(k);
                out.push_str("=\"");
                escape_attr(v, out);
                out.push('"');
            }
            out.push('>');
            if node.node_type.is_self_closing() {
                return;
            }
            for &child in &node.children {
                serialize_node(arena, child, out);
            }
            out.push_str("</");
            out.push_str(tag);
            out.push('>');
        }
    }
}

/// Escape text content for HTML serialization.
fn escape_text(s: &str, out: &mut String) {
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '\u{00a0}' => out.push_str("&nbsp;"),
            c => out.push(c),
        }
    }
}

/// Escape an attribute value for HTML serialization.
fn escape_attr(s: &str, out: &mut String) {
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '"' => out.push_str("&quot;"),
            '\u{00a0}' => out.push_str("&nbsp;"),
            c => out.push(c),
        }
    }
}