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
//! `<details>` / `<summary>` tests — the disclosure widget.
//!
//! A `<details>` element renders as a toggle: while the `open` attribute is
//! absent only its first `<summary>` child is visible (nothing at all when
//! there is no `<summary>`); with `open` present every child renders. The
//! `<summary>` carries a browser-style disclosure marker (`▸` closed / `▾`
//! open), and clicking it natively toggles the `open` attribute and fires a
//! `toggle` event on the `<details>`.

use crossterm::style::Color;
use htui::events::{EventBindings, Geometry, HitMap, HoverState};
use htui::html::{css_parse, html_parse};
use htui::layout::layout::TermMap;
use htui::layout::{BoxConstraint, Cursor, layout_tree};
use htui::script::{ScriptEngine, ScriptMsg, apply_message};
use htui::tdom::node::{NodeArena, NodeId, NodeType};
use htui::tui::graph_api::TermLayout;
use std::sync::Arc;

const COLS: u16 = 80;
const ROWS: u16 = 24;

fn init() {
    let _ = env_logger::builder().is_test(true).try_init();
}

fn setup(html: &str) -> (NodeId, NodeArena) {
    let mut html_parser = html_parse::Parser::new(html);
    let (root_id, mut arena, embedded) = html_parser.parse();
    if !embedded.trim().is_empty() {
        let mut css_parser = css_parse::Parser::new(&embedded);
        let rules = css_parser.parse();
        css_parse::css_into_node(&mut arena, root_id, &rules);
    }
    (root_id, arena)
}

fn mock_term_map() -> TermMap {
    TermMap {
        columns: COLS,
        rows: ROWS,
    }
}

/// Layout the document and return the box tree plus the arena needed to map
/// boxes back to node types.
fn layout_boxes(html: &str) -> (Vec<htui::layout::LayoutBox>, NodeArena) {
    let (root_id, arena) = setup(html);
    let containing = BoxConstraint::root(&mock_term_map());
    let mut cursor = Cursor::new();
    let boxes = layout_tree(
        &arena,
        root_id,
        containing,
        &mut cursor,
        COLS as usize,
        ROWS as usize,
    );
    (boxes, arena)
}

/// Render the document onto the terminal canvas.
fn draw(html: &str) -> (TermMap, TermLayout) {
    let term_map = mock_term_map();
    let (root_id, arena) = setup(html);
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);
    (term_map, tl)
}

/// Recursively walk the layout tree (depth-first) and return the first box
/// whose node type matches `target`.
fn find_box_by_type<'a>(
    boxes: &'a [htui::layout::LayoutBox],
    target: &NodeType,
    arena: &NodeArena,
) -> Option<&'a htui::layout::LayoutBox> {
    for b in boxes {
        if &arena[b.node_id].node_type == target {
            return Some(b);
        }
        if let Some(found) = find_box_by_type(&b.children, target, arena) {
            return Some(found);
        }
    }
    None
}

/// Whether any canvas cell carries exactly `word` as its glyph.
fn canvas_has_word(tl: &TermLayout, word: &str) -> bool {
    tl.layout.iter().any(|b| b.word == word)
}

/// Build the engine + geometry for a document (mirrors the runtime path: the
/// engine owns the live arena and runs scripts at construction).
fn build_geometry(html: &str) -> (ScriptEngine, Geometry) {
    let engine = ScriptEngine::new(html, "", EventBindings::default()).expect("engine initializes");
    let arena = engine.arena();
    let root_id = engine.root_id();
    let term_map = TermMap {
        columns: COLS,
        rows: ROWS,
    };
    let containing = BoxConstraint::root(&term_map);
    let mut cursor = Cursor::new();
    let boxes = layout_tree(
        &arena,
        root_id,
        containing,
        &mut cursor,
        term_map.columns as usize,
        term_map.rows as usize,
    );
    let geo = Geometry {
        arena: Arc::new(arena),
        boxes,
        root_id,
    };
    (engine, geo)
}

#[test]
fn test_closed_details_renders_only_first_summary() {
    init();
    // No `open` attribute: the browser hides every child but the first
    // `<summary>`. The `<p>` after it must produce no box.
    let (boxes, arena) = layout_boxes("<details><summary>More</summary><p>Secret</p></details>");
    let summary = find_box_by_type(&boxes, &NodeType::Summary, &arena)
        .expect("summary must render its own box");
    assert_eq!(summary.rect.height, 1, "summary is one row");
    assert!(
        find_box_by_type(&boxes, &NodeType::P, &arena).is_none(),
        "hidden content must not produce a box"
    );
    let (_, tl) = draw("<details><summary>More</summary><p>Secret</p></details>");
    assert!(
        canvas_has_word(&tl, "▸"),
        "closed details shows the closed marker"
    );
    assert!(canvas_has_word(&tl, "M"), "summary text renders");
    assert!(!canvas_has_word(&tl, "S"), "hidden content must not paint");
}

#[test]
fn test_open_details_renders_all_children() {
    init();
    let (boxes, arena) =
        layout_boxes("<details open><summary>More</summary><p>Shown</p></details>");
    assert!(find_box_by_type(&boxes, &NodeType::Summary, &arena).is_some());
    let p = find_box_by_type(&boxes, &NodeType::P, &arena).expect("open details shows content");
    assert_eq!(
        p.rect.y, 2,
        "content starts below the summary row + its 1-row UA margin-top"
    );
    let (_, tl) = draw("<details open><summary>More</summary><p>Shown</p></details>");
    assert!(
        canvas_has_word(&tl, "▾"),
        "open details shows the open marker"
    );
    assert!(canvas_has_word(&tl, "S"), "content paints when open");
}

#[test]
fn test_closed_details_without_summary_renders_nothing() {
    init();
    // No `<summary>` child at all: a closed details hides everything, and an
    // open one shows it all.
    let (boxes, arena) = layout_boxes("<details><p>Hidden</p></details>");
    assert!(
        find_box_by_type(&boxes, &NodeType::P, &arena).is_none(),
        "closed details without a summary renders nothing"
    );
    let (boxes, arena) = layout_boxes("<details open><p>Shown</p></details>");
    assert!(
        find_box_by_type(&boxes, &NodeType::P, &arena).is_some(),
        "open details without a summary still renders content"
    );
}

#[test]
fn test_closed_details_keeps_only_the_first_summary() {
    init();
    // Content before the summary is hidden too; a second `<summary>` is
    // hidden exactly like any other non-first child (browsers only special
    // case the first summary).
    let html = "<details><p>before</p><summary>Toggle</summary><p>between</p><summary>again</summary></details>";
    let (boxes, arena) = layout_boxes(html);
    let summary =
        find_box_by_type(&boxes, &NodeType::Summary, &arena).expect("first summary renders");
    let mut summary_count = 0;
    collect_box_count(&boxes, &NodeType::Summary, &arena, &mut summary_count);
    assert_eq!(
        summary_count, 1,
        "only the first summary renders when closed"
    );
    assert!(summary.rect.height == 1);
    assert!(
        find_box_by_type(&boxes, &NodeType::P, &arena).is_none(),
        "non-summary content stays hidden"
    );
    let (_, tl) = draw(html);
    assert!(canvas_has_word(&tl, "T"));
    assert!(
        !canvas_has_word(&tl, "f"),
        "content before the summary stays hidden"
    );
    assert!(
        !canvas_has_word(&tl, "w"),
        "content after the summary stays hidden"
    );
    assert!(
        !canvas_has_word(&tl, "i"),
        "the second summary stays hidden"
    );
}

fn collect_box_count(
    boxes: &[htui::layout::LayoutBox],
    target: &NodeType,
    arena: &NodeArena,
    out: &mut usize,
) {
    for b in boxes {
        if &arena[b.node_id].node_type == target {
            *out += 1;
        }
        collect_box_count(&b.children, target, arena, out);
    }
}

#[test]
fn test_summary_outside_details_has_no_marker() {
    init();
    // A standalone `<summary>` is a plain block — the disclosure marker only
    // exists inside a `<details>` (browser UA `summary::marker`).
    let (_, tl) = draw("<summary>plain</summary>");
    assert!(canvas_has_word(&tl, "p"));
    assert!(
        !canvas_has_word(&tl, "▸"),
        "no disclosure marker outside details"
    );
    assert!(!canvas_has_word(&tl, "▾"));
}

#[test]
fn test_summary_marker_styles_with_css() {
    init();
    // The marker run carries the summary's own fg / bg / font style.
    let (_, tl) = draw("<details><summary>More</summary></details>");
    let idx = tl
        .layout
        .iter()
        .position(|b| b.word == "▸")
        .expect("marker cell");
    assert_eq!(
        tl.layout[idx].fcolor, None,
        "default fg is the terminal's own"
    );
    let (_, tl) =
        draw("<style>summary { color: blue; }</style><details><summary>More</summary></details>");
    let idx = tl
        .layout
        .iter()
        .position(|b| b.word == "▸")
        .expect("marker cell");
    let blue = Color::Rgb { r: 0, g: 0, b: 255 };
    assert_eq!(
        tl.layout[idx].fcolor,
        Some(blue),
        "marker inherits summary color"
    );
}

#[test]
fn test_clicking_summary_toggles_open_and_fires_toggle() {
    init();
    let (mut engine, geo) = build_geometry(
        r#"<script>
          var toggles = [];
          var d = document.getElementById('d');
          d.addEventListener('toggle', function () {
            toggles.push(d.getAttribute('open') === null ? 'closed' : 'open');
          });
          globalThis.__toggles = toggles;
        </script>
        <details id="d"><summary>More</summary><p>Secret</p></details>"#,
    );
    let map = HitMap::build(&geo);
    let region = map.resolve_by_id("d").expect("details region");
    let (x, y) = center(region.rect);

    let mut hover = HoverState::new();
    let ev = apply_message(&mut engine, &mut hover, &map, ScriptMsg::Click { x, y });
    assert!(
        engine.arena()[region.node_id]
            .attributes
            .contains_key("open"),
        "clicking the summary must set the open attribute"
    );
    assert!(ev.dirty.is_some(), "the toggle marks the details dirty");
    assert_eq!(
        eval_str(&mut engine, "String(globalThis.__toggles.join(','))"),
        "open",
        "toggle event fires after the state flip with the new state"
    );

    // Second click closes it again.
    let _ev = apply_message(&mut engine, &mut hover, &map, ScriptMsg::Click { x, y });
    assert!(
        !engine.arena()[region.node_id]
            .attributes
            .contains_key("open"),
        "second click removes the open attribute"
    );
    assert_eq!(
        eval_str(&mut engine, "String(globalThis.__toggles.join(','))"),
        "open,closed",
        "every state flip fires exactly one toggle event"
    );
}

#[test]
fn test_clicking_summary_content_toggles_the_details() {
    init();
    // The activation target is a box-producing descendant of the summary (a
    // block-level `<div>`): the default action still resolves to the owning
    // details by walking up the DOM.
    let (mut engine, geo) = build_geometry(
        r#"<details id="d"><summary>More<div id="s">Block</div></summary><p>Secret</p></details>"#,
    );
    let map = HitMap::build(&geo);
    let div = map.resolve_by_id("s").expect("div region");
    let (x, y) = center(div.rect);

    let mut hover = HoverState::new();
    let _ = apply_message(&mut engine, &mut hover, &map, ScriptMsg::Click { x, y });
    let details_id = find_node_by_id(&engine.arena(), "d");
    assert!(
        engine.arena()[details_id].attributes.contains_key("open"),
        "clicking content inside the summary toggles the details"
    );
}

#[test]
fn test_clicking_details_content_does_not_toggle() {
    init();
    // Clicking the expanded content itself (not the summary) must not flip
    // the disclosure state — only the summary has the activation behavior.
    let (mut engine, geo) = build_geometry(
        r#"<details id="d" open><summary>More</summary><p id="c">Secret</p></details>"#,
    );
    let map = HitMap::build(&geo);
    let content = map.resolve_by_id("c").expect("content region");
    let (x, y) = center(content.rect);

    let mut hover = HoverState::new();
    let _ = apply_message(&mut engine, &mut hover, &map, ScriptMsg::Click { x, y });
    let details_id = find_node_by_id(&engine.arena(), "d");
    assert!(
        engine.arena()[details_id].attributes.contains_key("open"),
        "clicking expanded content keeps the details open"
    );
}

#[test]
fn test_toggle_mutation_reflows_to_reveal_content() {
    init();
    // After the toggle marks the details dirty, a re-layout from the engine's
    // live arena must show the newly revealed content (and the open marker).
    let (mut engine, geo) =
        build_geometry(r#"<details id="d"><summary>More</summary><p>Secret</p></details>"#);
    let map = HitMap::build(&geo);
    let region = map.resolve_by_id("d").expect("details region");
    let (x, y) = center(region.rect);

    let mut hover = HoverState::new();
    let _ = apply_message(&mut engine, &mut hover, &map, ScriptMsg::Click { x, y });

    let arena = engine.arena();
    let root_id = engine.root_id();
    let term_map = TermMap {
        columns: COLS,
        rows: ROWS,
    };
    let containing = BoxConstraint::root(&term_map);
    let mut cursor = Cursor::new();
    let boxes = layout_tree(
        &arena,
        root_id,
        containing,
        &mut cursor,
        term_map.columns as usize,
        term_map.rows as usize,
    );
    assert!(
        find_box_by_type(&boxes, &NodeType::P, &arena).is_some(),
        "content appears after opening"
    );
    assert!(
        find_box_by_type(&boxes, &NodeType::Summary, &arena).is_some(),
        "summary stays visible"
    );
}

#[test]
fn test_ontoggle_attribute_runs_on_click() {
    init();
    // `ontoggle` compiles to a listener exactly like the other on* events.
    let (mut engine, geo) = build_geometry(
        r#"<script>
          var seen = [];
          globalThis.__seen = seen;
        </script>
        <details id="d" ontoggle="seen.push(event.type)"><summary>More</summary><p>x</p></details>"#,
    );
    let map = HitMap::build(&geo);
    let region = map.resolve_by_id("d").expect("details region");
    let (x, y) = center(region.rect);

    let mut hover = HoverState::new();
    let _ = apply_message(&mut engine, &mut hover, &map, ScriptMsg::Click { x, y });
    assert_eq!(
        eval_str(&mut engine, "String(globalThis.__seen.join(','))"),
        "toggle",
        "ontoggle attribute fires on the details"
    );
}

#[test]
fn test_closed_details_hides_own_inline_text() {
    init();
    // Text directly inside a closed details (outside any summary) belongs to
    // the hidden children — the element's own inline runs must be filtered
    // just like its block children.
    let (_, tl) = draw("<details>secret text</details>");
    let painted: Vec<&str> = tl
        .layout
        .iter()
        .filter(|b| !b.word.trim().is_empty())
        .map(|b| b.word.as_str())
        .collect();
    assert!(
        painted.is_empty(),
        "a closed details with no summary paints no text: {painted:?}"
    );
    let (boxes, arena) = layout_boxes("<details>lead<summary>s</summary>tail</details>");
    assert!(
        find_box_by_type(&boxes, &NodeType::Summary, &arena).is_some(),
        "the summary still renders"
    );
    let (_, tl) = draw("<details>lead<summary>s</summary>tail</details>");
    assert!(canvas_has_word(&tl, "s"), "summary text paints");
    assert!(
        !canvas_has_word(&tl, "l"),
        "text before the summary stays hidden"
    );
    assert!(
        !canvas_has_word(&tl, "t"),
        "text after the summary stays hidden"
    );
}

#[test]
fn test_open_details_shows_own_inline_text() {
    init();
    let (_, tl) = draw("<details open>visible text</details>");
    assert!(canvas_has_word(&tl, "v"), "open details paints its text");
}

// ============================================================
// Helpers
// ============================================================

fn center(rect: htui::layout::Rect) -> (u32, u32) {
    (
        rect.x as u32 + rect.width.max(1) as u32 / 2,
        rect.y as u32 + rect.height.max(1) as u32 / 2,
    )
}

fn eval_str(engine: &mut ScriptEngine, expr: &str) -> String {
    let v = engine
        .eval_source(&format!("String({expr})"))
        .expect("eval must succeed");
    v.as_string()
        .map(|s| s.to_std_string_lossy())
        .unwrap_or_default()
}

fn find_node_by_id(arena: &NodeArena, id: &str) -> NodeId {
    arena
        .iter()
        .find(|(_, n)| n.attributes.get("id").map(String::as_str) == Some(id))
        .map(|(nid, _)| nid)
        .expect("element with id exists")
}