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
//! Geometry for replaced elements: `<input>` / `<textarea>` / `<button>` /
//! `<select>` / `<img>`, driven by the payload attached at generation time.

use crate::{
    graphics::{
        IMAGE_PIXELS_PER_CELL_COL, IMAGE_PIXELS_PER_CELL_ROW, char_width, image_intrinsic_size,
    },
    html::{
        box_edges::BoxEdges,
        css_parse::{BoxSizing, clamp_min_max},
    },
    layout::{
        BoxConstraint, Cursor, LayoutBox, LayoutContext, PaintStyle, Rect, ReplacedContent,
        layoutbox::generation::{GeneratedBox, GeneratedPayload},
    },
    tdom::node::NodeArena,
};

use super::select_overlay::select_options;

/// Geometry for replaced elements driven solely by the payload attached at
/// generation time. Tag-specific data (the resource path and intrinsic
/// `width` / `height` attributes) is read from the DOM only when the
/// payload indicates an image; the dispatch never inspects `NodeType` to
/// decide which geometry algorithm to run.
pub(super) fn layout_replaced(
    arena: &NodeArena,
    generated: &GeneratedBox,
    containing: BoxConstraint,
    cursor: &mut Cursor,
    context: LayoutContext,
    pre_allocated_rect: Option<Rect>,
) -> Option<LayoutBox> {
    match &generated.payload {
        GeneratedPayload::Input => layout_input(
            arena,
            generated,
            containing,
            cursor,
            context,
            pre_allocated_rect,
        ),
        GeneratedPayload::Textarea => layout_textarea(
            arena,
            generated,
            containing,
            cursor,
            context,
            pre_allocated_rect,
        ),
        GeneratedPayload::Button => layout_button(
            arena,
            generated,
            containing,
            cursor,
            context,
            pre_allocated_rect,
        ),
        GeneratedPayload::Select => layout_select(
            arena,
            generated,
            containing,
            cursor,
            context,
            pre_allocated_rect,
        ),
        GeneratedPayload::Image { src } if !src.is_empty() => layout_image(
            arena,
            generated,
            containing,
            cursor,
            context,
            pre_allocated_rect,
            src,
        ),
        _ => None,
    }
}

/// Geometry for `<input>`: a single-line field sized from the HTML `width`
/// attribute (cells) or a fixed default width when absent.
///
/// The width is deliberately *independent of the value* (mirroring a browser
/// `size` attribute): typing never changes the layout, so the edit path only
/// repaints the field's rectangle instead of re-running layout.
fn layout_input(
    arena: &NodeArena,
    generated: &GeneratedBox,
    containing: BoxConstraint,
    cursor: &mut Cursor,
    context: LayoutContext,
    pre_allocated_rect: Option<Rect>,
) -> Option<LayoutBox> {
    let style = replaced_chrome(arena, generated, containing.width, context);
    let (x, y, block_width, height) = match pre_allocated_rect {
        Some(rect) => (rect.x, rect.y, rect.width.max(1), rect.height.max(1)),
        None => {
            let node = &arena[generated.node_id];
            // `<input type=checkbox|radio>` is a two-cell bare terminal toggle
            // (glyph + one spacing column so it never glues onto adjacent
            // text); a button input (`button` / `submit` / `reset` / `image`)
            // sizes itself from its label like `<button>` (label + padding);
            // every other kind is a text field sized from the HTML `width`
            // attribute (cells) or a fixed default width when absent. A CSS
            // `border:` opt-in frame adds its thickness on both axes — except
            // for toggles, which paint no chrome of their own so they reserve
            // no border cells.
            let is_toggle = node
                .get_type()
                .is_some_and(|t| matches!(t.to_ascii_lowercase().as_str(), "checkbox" | "radio"));
            let (border_h, border_v) = if is_toggle {
                (0, 0)
            } else {
                (style.1, style.2)
            };
            let (natural_w, natural_h) = if is_toggle {
                (2, 1)
            } else if let Some(label) = node.input_button_label() {
                let text_w: usize = label.chars().map(|c| char_width(c).max(1)).sum();
                // The `.max(4)` floor exists for legibility, but a container
                // narrower than 4 cells must win — the floor may not push the
                // control past its containing block.
                (
                    (text_w.saturating_add(4))
                        .min(containing.width)
                        .max(containing.width.min(4)),
                    1,
                )
            } else {
                let attr_w = node
                    .get_width()
                    .map(|w| (w as usize).min(containing.width).max(1))
                    .unwrap_or_else(|| 8usize.min(containing.width.max(1)));
                (attr_w, 1)
            };
            control_placement(
                arena,
                generated,
                containing,
                context,
                cursor,
                &style.0.box_edges,
                (border_h, border_v),
                (natural_w, natural_h),
            )
        }
    };
    Some(LayoutBox::new_replaced(
        generated.node_id,
        Rect::new(x, y, block_width, height),
        ReplacedContent::Input,
        style.0,
    ))
}

/// Resolve a replaced control's chrome in a single pass: the [`PaintStyle`]
/// (CSS border edges over the default replaced-element style) and the border
/// thickness on both axes. Layout reserves the border cells up front so a
/// declared `border:` renders as a real frame around the content; the control
/// painters then draw it from the same style.
fn replaced_chrome(
    arena: &NodeArena,
    generated: &GeneratedBox,
    containing_width: usize,
    context: LayoutContext,
) -> (PaintStyle, usize, usize) {
    let node = &arena[generated.node_id];
    let edges = node
        .css_attr
        .get_box_edges(containing_width, context.font, context.viewport);
    (
        PaintStyle {
            box_edges: edges,
            ..PaintStyle::default()
        },
        edges.border.horizontal_thickness(),
        edges.border.vertical_thickness(),
    )
}

/// Shared sizing + placement for a replaced control in normal block flow.
///
/// The border-box is resolved from the CSS `width` / `height` chain — a
/// declared CSS size wins, otherwise the control's natural (attribute- or
/// label-derived) size is used — with `box-sizing` honoured, then clamped by
/// `min-*` / `max-*`. The box is offset by its declared margins: horizontal
/// `auto` margins centre the control in the containing block (a single
/// `auto` pushes it to the opposite side), mirroring `layout_block`.
///
/// Returns `(x, y, block_width, block_height)` — the border-box rectangle.
fn control_placement(
    arena: &NodeArena,
    generated: &GeneratedBox,
    containing: BoxConstraint,
    context: LayoutContext,
    cursor: &mut Cursor,
    edges: &BoxEdges,
    border: (usize, usize),
    natural: (usize, usize),
) -> (usize, usize, usize, usize) {
    let node = &arena[generated.node_id];
    let css = &node.css_attr;
    let sizing = css.get_box_sizing();
    let (border_h, border_v) = border;
    let (natural_w, natural_h) = natural;

    // Width: declared / min / max all resolve into *border-box* space (the
    // same convention as `layout_block`'s geometry), then clamp through the
    // canonical helper — when min exceeds max, the minimum wins (CSS).
    let to_border_w = |w: usize| match sizing {
        BoxSizing::BorderBox => w,
        BoxSizing::ContentBox => w.saturating_add(border_h),
    };
    let declared_w = css
        .get_width(containing.width, context.font, context.viewport)
        .map(to_border_w);
    let min_w = css
        .get_min_width(containing.width, context.font, context.viewport)
        .map(to_border_w);
    let max_w = css
        .get_max_width(containing.width, context.font, context.viewport)
        .map(to_border_w);
    let block_width = clamp_min_max(
        declared_w.unwrap_or_else(|| natural_w.saturating_add(border_h)),
        min_w,
        max_w,
    )
    .max(1);

    // Height: a declared CSS `height` wins over the natural row count. The
    // control sits in normal flow, where the containing block's *height* is
    // indefinite — percentage heights compute to `auto` (CSS 10.5), which
    // `get_height(None, …)` models directly.
    let css_h = css.get_height(None, context.font, context.viewport);
    let block_height = css_h.unwrap_or(natural_h.saturating_add(border_v)).max(1);

    // Horizontal margins: an `auto` pair centres, a single `auto` pushes the
    // control to the opposite side (CSS 10.3.3 for replaced elements).
    let margin = edges.margin;
    let auto = css.get_margin_auto();
    let free =
        (containing.width as i64 - block_width as i64 - margin.left as i64 - margin.right as i64)
            .max(0) as usize;
    let x = if auto.left && auto.right {
        (containing.x as i64 + margin.left as i64 + (free / 2) as i64).max(0) as usize
    } else if auto.left {
        (containing.x as i64 + margin.left as i64 + free as i64).max(0) as usize
    } else {
        (containing.x as i64 + margin.left as i64).max(0) as usize
    };

    // Vertical margins advance the flow cursor; a replaced control is an
    // atomic inline-level box, so adjacent margins do not collapse.
    let y0 = cursor.allocate(margin.top + block_height + margin.bottom);
    let y = y0 + margin.top;
    (x, y, block_width, block_height)
}

/// Geometry for `<textarea>`: a fixed multi-line editable field.
///
/// Width is `cols` (browser default 20) and height is `rows` (browser
/// default 2), plus any CSS `border` thickness. The size is independent of
/// the value, so editing only repaints the field's rectangle — same as
/// `<input>`.
fn layout_textarea(
    arena: &NodeArena,
    generated: &GeneratedBox,
    containing: BoxConstraint,
    cursor: &mut Cursor,
    context: LayoutContext,
    pre_allocated_rect: Option<Rect>,
) -> Option<LayoutBox> {
    let style = replaced_chrome(arena, generated, containing.width, context);
    let (x, y, block_width, height) = match pre_allocated_rect {
        Some(rect) => (rect.x, rect.y, rect.width.max(1), rect.height.max(1)),
        None => {
            let node = &arena[generated.node_id];
            let cols: usize = node
                .attributes
                .get("cols")
                .and_then(|v| v.parse().ok())
                .unwrap_or(20);
            let rows: usize = node
                .attributes
                .get("rows")
                .and_then(|v| v.parse().ok())
                .unwrap_or(2);
            let natural_w = cols.max(1).min(containing.width);
            control_placement(
                arena,
                generated,
                containing,
                context,
                cursor,
                &style.0.box_edges,
                (style.1, style.2),
                (natural_w, rows.max(1)),
            )
        }
    };
    Some(LayoutBox::new_replaced(
        generated.node_id,
        Rect::new(x, y, block_width, height),
        ReplacedContent::Textarea,
        style.0,
    ))
}

/// Geometry for `<button>`: a single-row push-button sized from its text
/// content (label + padding). A `disabled` button still takes the same space
/// (matching browsers).
fn layout_button(
    arena: &NodeArena,
    generated: &GeneratedBox,
    containing: BoxConstraint,
    cursor: &mut Cursor,
    context: LayoutContext,
    pre_allocated_rect: Option<Rect>,
) -> Option<LayoutBox> {
    let style = replaced_chrome(arena, generated, containing.width, context);
    let (x, y, block_width, height) = match pre_allocated_rect {
        Some(rect) => (rect.x, rect.y, rect.width.max(1), rect.height.max(1)),
        None => {
            let node = &arena[generated.node_id];
            let text_w: usize = node
                .text_content(arena)
                .chars()
                .map(|c| char_width(c).max(1))
                .sum();
            control_placement(
                arena,
                generated,
                containing,
                context,
                cursor,
                &style.0.box_edges,
                (style.1, style.2),
                // Cap to the containing block like the `<input>` button and
                // `<select>` paths: a long label must not hand the box a
                // rect wider than the terminal (the paint phase then skips
                // or truncates it).
                ((text_w.saturating_add(4)).min(containing.width).max(1), 1),
            )
        }
    };
    Some(LayoutBox::new_replaced(
        generated.node_id,
        Rect::new(x, y, block_width, height),
        ReplacedContent::Button,
        style.0,
    ))
}

/// Geometry for `<select>`: a single-line dropdown field.
///
/// Width is the longest option label plus the chevron / padding, or the HTML
/// `width` attribute when set. The dropdown overlay is painted by the render
/// thread, so the box itself is always one row.
fn layout_select(
    arena: &NodeArena,
    generated: &GeneratedBox,
    containing: BoxConstraint,
    cursor: &mut Cursor,
    context: LayoutContext,
    pre_allocated_rect: Option<Rect>,
) -> Option<LayoutBox> {
    let style = replaced_chrome(arena, generated, containing.width, context);
    let (x, y, block_width, height) = match pre_allocated_rect {
        Some(rect) => (rect.x, rect.y, rect.width.max(1), rect.height.max(1)),
        None => {
            let node = &arena[generated.node_id];
            let text_w: usize = select_options(arena, generated.node_id)
                .iter()
                .map(|opt| opt.label.chars().map(char_width).sum::<usize>())
                .max()
                .unwrap_or(0);
            let natural_w = node
                .get_width()
                .map(|w| (w as usize).min(containing.width).max(1))
                .unwrap_or({
                    // Same rule as the `<input>` button: the 4-cell
                    // legibility floor never exceeds the containing block.
                    (text_w.saturating_add(4))
                        .min(containing.width)
                        .max(containing.width.min(4))
                });
            control_placement(
                arena,
                generated,
                containing,
                context,
                cursor,
                &style.0.box_edges,
                (style.1, style.2),
                (natural_w, 1),
            )
        }
    };
    Some(LayoutBox::new_replaced(
        generated.node_id,
        Rect::new(x, y, block_width, height),
        ReplacedContent::Select,
        style.0,
    ))
}

/// Geometry for `<img>`, driven solely by the payload attached at generation
/// time. Tag-specific data (the resource path and intrinsic `width` /
/// `height` attributes) is read from the DOM only when the payload indicates
/// an image; the dispatch never inspects `NodeType` to decide which geometry
/// algorithm to run.
fn layout_image(
    arena: &NodeArena,
    generated: &GeneratedBox,
    containing: BoxConstraint,
    cursor: &mut Cursor,
    context: LayoutContext,
    pre_allocated_rect: Option<Rect>,
    src: &str,
) -> Option<LayoutBox> {
    let src = src.to_string();
    let (x, y, block_width, img_rows) = match pre_allocated_rect {
        Some(rect) => {
            let width = rect.width.max(1);
            let height = rect.height.max(1);
            (rect.x, rect.y, width, height)
        }
        None => {
            let node = &arena[generated.node_id];
            let (img_cols, img_rows) = resolve_image_cell_size(
                node.get_width(),
                node.get_height(),
                image_intrinsic_size(&src),
                containing.width,
            );
            // Placement goes through the shared replaced-control path so CSS
            // `width` / `height` / `min-*` / `max-*` and box edges apply.
            // An `img` is inline-level: left-aligned in its flow with margins
            // active, matching other replaced controls.
            // padding participate in the box size like any control; painting
            // them on the pixels stays future work (`paint_image` fills the
            // rect).
            let edges =
                node.css_attr
                    .get_box_edges(containing.width, context.font, context.viewport);
            let (x, y, block_width, block_height) = control_placement(
                arena,
                generated,
                containing,
                context,
                cursor,
                &edges,
                (
                    edges.border.horizontal_thickness(),
                    edges.border.vertical_thickness(),
                ),
                (img_cols, img_rows),
            );
            (x, y, block_width, block_height)
        }
    };

    Some(LayoutBox::new_replaced(
        generated.node_id,
        Rect::new(x, y, block_width, img_rows),
        ReplacedContent::Image {
            path: src,
            cols: block_width,
            rows: img_rows,
        },
        PaintStyle::default(),
    ))
}

/// Resolve the natural `<img>` display size in cells from the HTML `width` /
/// `height` attributes (source pixels) and the file's intrinsic size.
///
/// Mirrors browser sizing:
/// - Both attributes: the image is stretched to exactly `w × h` source
///   pixels (width still capped to the containing block).
/// - One attribute: the other axis follows the intrinsic aspect ratio.
/// - Neither: the intrinsic size, capped to the containing width; the height
///   follows the aspect ratio.
/// - No readable intrinsic size: `containing_width` square (historical
///   fallback).
fn resolve_image_cell_size(
    attr_w: Option<u32>,
    attr_h: Option<u32>,
    intrinsic: Option<(u32, u32)>,
    containing_width: usize,
) -> (usize, usize) {
    let containing_w = containing_width.max(1) as u32;
    let (intrinsic_w, intrinsic_h) = intrinsic.unwrap_or((containing_w, containing_w));
    // Width: an explicit attribute wins, otherwise the intrinsic width; both
    // are capped to the containing block so the box never overflows it.
    let pixel_w = attr_w.unwrap_or(intrinsic_w).min(containing_w).max(1);
    // Height: an explicit attribute wins, otherwise derived from the resolved
    // width preserving the source aspect ratio.
    let pixel_h = match attr_h {
        Some(h) => h.max(1),
        None => {
            let h = pixel_w as u64 * intrinsic_h as u64 / intrinsic_w.max(1) as u64;
            (h as u32).max(1)
        }
    };
    let img_cols = pixel_w.div_ceil(IMAGE_PIXELS_PER_CELL_COL).max(1) as usize;
    let img_rows = pixel_h.div_ceil(IMAGE_PIXELS_PER_CELL_ROW).max(1) as usize;
    // An explicit `height` attribute has no upper bound: a bogus value
    // (e.g. `height="4000000000"`) reserved billions of rows, pushing every
    // element after the image past the canvas cap (1000 × 500 cells) and
    // making the rest of the document unreachable. Real terminal images
    // stay far below this bound.
    const MAX_ATTR_ROWS: usize = 10_000;
    (img_cols, img_rows.min(MAX_ATTR_ROWS))
}