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
use crossterm::style::Color;
use htui::html::css_parse;
use htui::html::html_parse;
use htui::layout::layout::TermMap;
use htui::tdom::node::{NodeArena, NodeId, NodeType};
use htui::tui::graph_api::TermLayout;

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

fn setup(html: &str, css: &str) -> (NodeId, NodeArena) {
    let mut html_parser = html_parse::Parser::new(html);
    let (root_id, mut arena, embedded) = html_parser.parse();
    let combined = if css.is_empty() {
        embedded
    } else if embedded.is_empty() {
        css.to_string()
    } else {
        format!("{embedded}\n{css}")
    };
    // Always cascade (an empty stylesheet still applies inline `style`
    // attributes and inheritance).
    let mut css_parser = css_parse::Parser::new(&combined);
    let rules = css_parser.parse();
    css_parse::css_into_node(&mut arena, root_id, &rules);
    (root_id, arena)
}

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

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

fn cell_containing(tl: &TermLayout, needle: &str) -> Option<(usize, usize, usize)> {
    // Returns (start_idx, row, col) of the first cell whose render text
    // equals `needle`. `char_width` does not apply here because we look up the
    // exact cell produced by the paint phase (no wide-char padding).
    for (idx, block) in tl.layout.iter().enumerate() {
        if block.word == needle {
            let cols = tl.columns as usize;
            return Some((idx, idx / cols, idx % cols));
        }
    }
    None
}

#[test]
fn test_parse_tag_map_ul_ol_li() {
    assert_eq!(html_parse::tag_name_to_node_type("ul"), Some(NodeType::Ul));
    assert_eq!(html_parse::tag_name_to_node_type("ol"), Some(NodeType::Ol));
    assert_eq!(html_parse::tag_name_to_node_type("li"), Some(NodeType::Li));
}

#[test]
fn test_parse_tag_map_uppercase_ul_ol_li() {
    assert_eq!(html_parse::tag_name_to_node_type("UL"), Some(NodeType::Ul));
    assert_eq!(html_parse::tag_name_to_node_type("OL"), Some(NodeType::Ol));
    assert_eq!(html_parse::tag_name_to_node_type("LI"), Some(NodeType::Li));
}

#[test]
fn test_ul_renders_bullet_markers() {
    init();
    let (root_id, arena) = setup("<ul><li>One</li><li>Two</li></ul>", "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let canvas: String = tl.layout.iter().map(|b| b.word.as_str()).collect();
    assert!(
        canvas.contains("•"),
        "unordered list must emit a bullet glyph"
    );
    assert!(canvas.contains("One"), "first item text missing");
    assert!(canvas.contains("Two"), "second item text missing");
}

#[test]
fn test_ol_renders_sequential_markers() {
    init();
    let (root_id, arena) = setup("<ol><li>First</li><li>Second</li><li>Third</li></ol>", "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let canvas: String = tl.layout.iter().map(|b| b.word.as_str()).collect();
    assert!(canvas.contains("1."), "ol must show ordinal '1.'");
    assert!(canvas.contains("2."), "ol must show ordinal '2.'");
    assert!(canvas.contains("3."), "ol must show ordinal '3.'");
    assert!(canvas.contains("First"));
    assert!(canvas.contains("Second"));
    assert!(canvas.contains("Third"));
}

#[test]
fn test_ol_ordinal_skips_non_li_siblings() {
    // Whitespace text nodes and inline text must not advance the counter.
    init();
    let (root_id, arena) = setup("<ol><li>A</li>garbage<li>B</li></ol>", "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let canvas: String = tl.layout.iter().map(|b| b.word.as_str()).collect();
    assert!(canvas.contains("1."), "first <li> should still be '1.'");
    assert!(
        canvas.contains("2."),
        "second <li> should be '2.', not '3.'"
    );
    assert!(!canvas.contains("3."), "counter must not skip ahead");
}

#[test]
fn test_empty_li_still_renders_marker() {
    init();
    let (root_id, arena) = setup("<ul><li></li><li>Two</li></ul>", "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let canvas: String = tl.layout.iter().map(|b| b.word.as_str()).collect();
    let bullet_count = canvas.matches('•').count();
    assert!(
        bullet_count >= 2,
        "expected at least 2 bullet markers (incl. empty <li>), got {bullet_count}"
    );
}

#[test]
fn test_li_items_each_occupy_their_own_row() {
    init();
    let (root_id, arena) = setup("<ul><li>A</li><li>B</li></ul>", "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let (a_idx, a_row, _) = cell_containing(&tl, "A").expect("missing 'A'");
    let (b_idx, b_row, _) = cell_containing(&tl, "B").expect("missing 'B'");
    assert_ne!(a_idx, b_idx, "A and B must be in different cells");
    assert_ne!(a_row, b_row, "A and B must be on different rows");
}

#[test]
fn test_ul_marker_sits_left_of_body() {
    // Bullet is at column 0; body text starts after the marker width.
    init();
    let (root_id, arena) = setup("<ul><li>Hello</li></ul>", "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let (_bullet_idx, bullet_row, bullet_col) = cell_containing(&tl, "•").expect("bullet missing");
    let (h_idx, h_row, h_col) = cell_containing(&tl, "H").expect("missing 'H'");
    assert_eq!(bullet_row, h_row, "bullet and body share the marker row");
    assert_eq!(bullet_col, 0, "bullet sits at column 0 for a top-level ul");
    assert!(
        h_col > bullet_col,
        "body 'H' (col {h_col}) must sit to the right of bullet (col {bullet_col}); h_idx={h_idx}"
    );
}

#[test]
fn test_nested_ul_indents_inner_items() {
    // Top-level ul: items at col 0; nested ul shifts its items by LIST_INDENT_PER_LEVEL.
    init();
    let html = "<ul><li>Outer A<ul><li>Inner A</li></ul></li><li>Outer B</li></ul>";
    let (root_id, arena) = setup(html, "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    // Find all bullet markers. The first one (row 0) belongs to the outer
    // 'Outer A' <li>, the third (or later) one to 'Inner A'.
    let bullet_cells: Vec<(usize, usize)> = tl
        .layout
        .iter()
        .enumerate()
        .filter_map(|(idx, b)| {
            if b.word == "•" {
                let row = idx / tl.columns as usize;
                let col = idx % tl.columns as usize;
                Some((row, col))
            } else {
                None
            }
        })
        .collect();
    assert!(
        bullet_cells.len() >= 3,
        "need at least 3 bullets (Outer A, Inner A, Outer B); got {}",
        bullet_cells.len()
    );

    let outer_a_col = bullet_cells[0].1;
    let inner_a_col = bullet_cells[1].1;
    let outer_b_col = bullet_cells[2].1;

    assert_eq!(
        outer_a_col, 0,
        "outer items' bullet must be flush at col 0 (row={}, idx={})",
        bullet_cells[0].0, bullet_cells[0].1
    );
    assert_eq!(
        outer_b_col, 0,
        "second outer item's bullet must also be at col 0 (row={})",
        bullet_cells[2].0
    );
    assert!(
        inner_a_col > outer_a_col,
        "inner item bullet (col {inner_a_col}) must sit to the right of outer (col {outer_a_col})"
    );
}

#[test]
fn test_nested_ol_in_ul_renders_mixed_markers() {
    init();
    let html = "<ul><li>Outer<ol><li>Inner One</li><li>Inner Two</li></ol></li></ul>";
    let (root_id, arena) = setup(html, "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let canvas: String = tl.layout.iter().map(|b| b.word.as_str()).collect();
    assert!(canvas.contains("•"), "outer ul must emit bullet");
    assert!(canvas.contains("1."), "inner ol must emit ordinal 1");
    assert!(canvas.contains("2."), "inner ol must emit ordinal 2");
}

#[test]
fn test_li_with_span_styling() {
    init();
    let html = "<ul><li>plain <span class=\"bold\">bold</span></li></ul>";
    let css = "ul li .bold { font-weight: bold; }";
    let (root_id, arena) = setup(html, css);
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let canvas: String = tl.layout.iter().map(|b| b.word.as_str()).collect();
    assert!(canvas.contains("plain"), "plain body text missing");
    assert!(canvas.contains("bold"), "span text missing");

    // The selector `ul li .bold` should set BOLD on the <span>'s letters.
    let bold_letters: String = tl
        .layout
        .iter()
        .filter(|b| b.style.contains(htui::tui::graph_api::FontStyle::BOLD))
        .map(|b| b.word.as_str())
        .collect();
    assert!(
        bold_letters.contains('b'),
        "expected at least one bold letter; got {bold_letters:?}"
    );
}

#[test]
fn test_li_with_css_color() {
    init();
    let html = "<ul><li>Red bullet item</li></ul>";
    let css = "ul li { color: red; }";
    let (root_id, arena) = setup(html, css);
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let has_red = tl
        .layout
        .iter()
        .any(|b| b.fcolor == Some(Color::Rgb { r: 255, g: 0, b: 0 }));
    assert!(
        has_red,
        "ul li should pick up `color: red` via selector match"
    );
}

#[test]
fn test_ul_with_bg_color_fills_list_rect() {
    init();
    let html = "<ul><li>first</li><li>second</li></ul>";
    let css = "ul { background-color: yellow; }";
    let (root_id, arena) = setup(html, css);
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let yellow_count = tl
        .layout
        .iter()
        .filter(|b| {
            b.bcolor
                == Some(Color::Rgb {
                    r: 255,
                    g: 255,
                    b: 0,
                })
        })
        .count();
    assert!(
        yellow_count > 0,
        "ul background-color should paint at least one cell"
    );
}

#[test]
fn test_orphan_li_uses_dash_marker() {
    init();
    let (root_id, arena) = setup("<li>orphan</li>", "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let canvas: String = tl.layout.iter().map(|b| b.word.as_str()).collect();
    // Orphan <li> falls back to a dash marker so it's not blank.
    assert!(
        canvas.contains('-'),
        "orphan <li> should render a dash marker; canvas={:?}",
        canvas
    );
    assert!(canvas.contains("orphan"), "orphan <li> body should render");
}

#[test]
fn test_ol_double_digit_ordinals() {
    init();
    // Item 10 -> marker "10. " (4 cells), still ahead of items 1..9.
    let html = "<ol><li>a</li><li>b</li><li>c</li><li>d</li><li>e</li><li>f</li><li>g</li><li>h</li><li>i</li><li>j</li></ol>";
    let (root_id, arena) = setup(html, "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let canvas: String = tl.layout.iter().map(|b| b.word.as_str()).collect();
    assert!(canvas.contains("10."), "double-digit ordinal must render");
    // All ten items should be present.
    for letter in ['a', 'b', 'c', 'd', 'e', 'j'] {
        assert!(canvas.contains(letter), "missing item body '{letter}'");
    }
}

#[test]
fn test_nested_ul_inside_li_stacks_below_parent_text() {
    init();
    // The parent <li>'s own text and its nested <ul> children stack on
    // separate rows.
    let html = "<ul><li>Nested list<ul><li>Inner A</li><li>Inner B</li></ul></li><li>Sibling of nested</li></ul>";
    let (root_id, arena) = setup(html, "");
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);

    let cols = term_map.columns as usize;
    let rows: Vec<String> = tl
        .layout
        .chunks(cols)
        .map(|chunk| chunk.iter().map(|b| b.word.as_str()).collect())
        .collect();
    let li_row = rows
        .iter()
        .position(|r| r.contains("Nested list"))
        .expect("parent li text should render intact");
    let inner_a_row = rows
        .iter()
        .position(|r| r.contains("Inner A"))
        .expect("first nested item should render");
    let inner_b_row = rows
        .iter()
        .position(|r| r.contains("Inner B"))
        .expect("second nested item should render");
    let sibling_row = rows
        .iter()
        .position(|r| r.contains("Sibling"))
        .expect("sibling li should render");
    assert!(
        li_row < inner_a_row && inner_a_row < inner_b_row && inner_b_row < sibling_row,
        "rows must stack li/{inner_a_row}/{inner_b_row}/{sibling_row}: {:?}",
        rows[li_row]
    );
}

#[test]
fn test_list_style_type_markers_render() {
    init();
    let (root_id, arena) = setup(
        r#"<ul style="list-style-type: square">
             <li>one</li><li>two</li>
           </ul>
           <ol start="3"><li>third</li><li>fourth</li></ol>
           <ul style="list-style: none"><li>plain</li></ul>"#,
        "",
    );
    let term_map = mock_term_map();
    let mut tl = TermLayout::new(&term_map, 1.0);
    tl.draw(&arena, root_id, &term_map);
    let canvas: String = tl.layout.iter().map(|b| b.word.as_str()).collect();

    assert!(canvas.contains("▪"), "square marker renders");
    assert!(
        canvas.contains("3. third") && canvas.contains("4. fourth"),
        "ol start=3 offsets ordinals"
    );
    assert!(
        canvas.contains("plain") && !canvas.contains("•"),
        "list-style: none suppresses the marker"
    );
}