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
//! Tests for the `dataset` / `hidden` global attributes on the JS side:
//! `el.dataset` is a live `DOMStringMap` over the element's `data-*`
//! attributes (camelCase↔kebab-case mapping, live reads / writes, WebIDL
//! `DOMString` conversion on writes, `delete`-only removal, `Object.keys`
//! enumeration), and `el.hidden` toggles the `hidden` attribute (the UA
//! `[hidden] { display: none }` rule). Pure in-memory — no terminal required.

use htui::events::EventBindings;
use htui::layout::layout::TermMap;
use htui::script::ScriptEngine;
use htui::tdom::node::{NodeArena, NodeId};
use htui::tui::graph_api::TermLayout;

/// Evaluate `String(expr)` and return its text.
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()
}

/// `NodeId` of the element whose `id` attribute equals `id`.
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")
}

/// The value of a `data-*` attribute, read from the live arena after JS ran.
fn data_attr(engine: &ScriptEngine, id: &str, name: &str) -> Option<String> {
    let arena = engine.arena();
    let node = find_node_by_id(&arena, id);
    arena
        .get(node)
        .and_then(|n| n.attributes.get(name).cloned())
}

/// Whether any rendered text node in the box tree equals `needle`.
///
/// Observation goes through the real render pipeline (canvas): generation is
/// a shallow per-node classifier, so "does it render" is answered by laying
/// out and drawing, exactly what the render thread does.
fn canvas_contains(engine: &ScriptEngine, needle: &str) -> bool {
    let map = TermMap {
        columns: 80,
        rows: 24,
    };
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&engine.arena(), engine.root_id(), &map);
    tl.layout
        .iter()
        .map(|b| b.word.as_str())
        .collect::<String>()
        .contains(needle)
}

// ============================================================
// dataset reads
// ============================================================

#[test]
fn dataset_reads_data_attributes_with_camel_case_mapping() {
    let mut engine = ScriptEngine::new(
        r#"<div id="d" data-foo="bar" data-max-count="3"></div>"#,
        "",
        EventBindings::default(),
    )
    .expect("engine initializes");
    assert_eq!(
        eval_str(&mut engine, "document.getElementById('d').dataset.foo"),
        "bar"
    );
    assert_eq!(
        eval_str(&mut engine, "document.getElementById('d').dataset.maxCount"),
        "3",
        "camelCase property maps to the kebab-case data-* attribute"
    );
    assert_eq!(
        eval_str(
            &mut engine,
            "String(document.getElementById('d').dataset.missing)"
        ),
        "undefined",
        "an absent data-* attribute reads as undefined"
    );
    // A kebab-case key reads the same attribute (browser bracket access).
    assert_eq!(
        eval_str(
            &mut engine,
            "document.getElementById('d').dataset['max-count']"
        ),
        "3"
    );
}

#[test]
fn dataset_empty_when_no_data_attributes() {
    let mut engine = ScriptEngine::new(r#"<div id="d"></div>"#, "", EventBindings::default())
        .expect("engine initializes");
    assert_eq!(
        eval_str(
            &mut engine,
            "Object.keys(document.getElementById('d').dataset).length"
        ),
        "0"
    );
    assert_eq!(
        eval_str(
            &mut engine,
            "String(document.getElementById('d').dataset.nope)"
        ),
        "undefined"
    );
}

// ============================================================
// dataset writes
// ============================================================

#[test]
fn dataset_write_sets_the_kebab_cased_attribute() {
    let mut engine = ScriptEngine::new(r#"<div id="d"></div>"#, "", EventBindings::default())
        .expect("engine initializes");
    eval_str(
        &mut engine,
        "document.getElementById('d').dataset.maxCount = '42'",
    );
    assert_eq!(data_attr(&engine, "d", "data-max-count"), Some("42".into()));
    // Live read-back through the same property.
    assert_eq!(
        eval_str(&mut engine, "document.getElementById('d').dataset.maxCount"),
        "42"
    );
    // Existing attributes are updated in place.
    let mut engine = ScriptEngine::new(
        r#"<div id="e" data-src="a"></div>"#,
        "",
        EventBindings::default(),
    )
    .expect("engine initializes");
    eval_str(
        &mut engine,
        "document.getElementById('e').dataset.src = 'b'",
    );
    assert_eq!(data_attr(&engine, "e", "data-src"), Some("b".into()));
}

#[test]
fn dataset_write_uses_webidl_string_conversion() {
    let mut engine = ScriptEngine::new(
        r#"<div id="d" data-tmp="x" data-n="1" data-u="2"></div>"#,
        "",
        EventBindings::default(),
    )
    .expect("engine initializes");
    eval_str(
        &mut engine,
        "globalThis.ds = document.getElementById('d').dataset",
    );
    // An empty string sets an empty attribute value (a browser keeps the
    // attribute present); removal is `delete`-only.
    eval_str(&mut engine, "globalThis.ds.tmp = ''");
    assert_eq!(data_attr(&engine, "d", "data-tmp"), Some("".into()));
    assert_eq!(eval_str(&mut engine, "globalThis.ds.tmp"), "");
    assert_eq!(
        eval_str(&mut engine, "'tmp' in globalThis.ds"),
        "true",
        "an empty-string write must keep the attribute present"
    );
    // WebIDL `DOMString` conversion: null / undefined stringify.
    eval_str(&mut engine, "globalThis.ds.n = null");
    assert_eq!(data_attr(&engine, "d", "data-n"), Some("null".into()));
    eval_str(&mut engine, "globalThis.ds.u = undefined");
    assert_eq!(data_attr(&engine, "d", "data-u"), Some("undefined".into()));
    // Removal stays `delete`-only.
    eval_str(&mut engine, "delete globalThis.ds.tmp");
    assert_eq!(data_attr(&engine, "d", "data-tmp"), None);
    assert_eq!(
        eval_str(&mut engine, "'tmp' in globalThis.ds"),
        "false",
        "delete must remove the attribute"
    );
}

#[test]
fn dataset_delete_and_in_operator() {
    let mut engine = ScriptEngine::new(
        r#"<div id="d" data-a="1" data-b="2"></div>"#,
        "",
        EventBindings::default(),
    )
    .expect("engine initializes");
    assert_eq!(
        eval_str(&mut engine, "'a' in document.getElementById('d').dataset"),
        "true"
    );
    eval_str(&mut engine, "delete document.getElementById('d').dataset.a");
    assert_eq!(
        eval_str(&mut engine, "'a' in document.getElementById('d').dataset"),
        "false"
    );
    assert_eq!(data_attr(&engine, "d", "data-a"), None);
    assert_eq!(
        eval_str(&mut engine, "'b' in document.getElementById('d').dataset"),
        "true",
        "deleting one key must not touch the others"
    );
}

#[test]
fn dataset_enumerates_kebab_cased_keys() {
    let mut engine = ScriptEngine::new(
        r#"<div id="d" data-alpha="1" data-max-count="2" data-z="3"></div>"#,
        "",
        EventBindings::default(),
    )
    .expect("engine initializes");
    assert_eq!(
        eval_str(
            &mut engine,
            "JSON.stringify(Object.keys(document.getElementById('d').dataset))"
        ),
        r#"["alpha","max-count","z"]"#,
        "Object.keys(dataset) lists the kebab-cased data-* names in order"
    );
}

#[test]
fn dataset_unchanged_write_does_not_mark_dirty() {
    let mut engine = ScriptEngine::new(
        r#"<div id="d" data-foo="bar"></div>"#,
        "",
        EventBindings::default(),
    )
    .expect("engine initializes");
    eval_str(
        &mut engine,
        "document.getElementById('d').dataset.foo = 'bar'",
    );
    let dirty = engine.take_dirty();
    assert!(
        dirty.is_empty(),
        "writing the same value must not schedule a re-render"
    );
}

#[test]
fn dataset_is_live_across_mutations() {
    let mut engine = ScriptEngine::new(
        r#"<div id="d" data-a="1"></div>"#,
        "",
        EventBindings::default(),
    )
    .expect("engine initializes");
    // A held dataset reference reflects later DOM mutations.
    eval_str(
        &mut engine,
        "globalThis.ds = document.getElementById('d').dataset",
    );
    eval_str(&mut engine, "globalThis.ds.b = '2'");
    assert_eq!(data_attr(&engine, "d", "data-b"), Some("2".into()));
    assert_eq!(
        eval_str(&mut engine, "globalThis.ds.b"),
        "2",
        "a held dataset reference stays live"
    );
}

// ============================================================
// el.hidden
// ============================================================

#[test]
fn hidden_reads_the_hidden_attribute() {
    let mut engine = ScriptEngine::new(
        r#"<div id="d" hidden></div><div id="e"></div>"#,
        "",
        EventBindings::default(),
    )
    .expect("engine initializes");
    assert_eq!(
        eval_str(&mut engine, "document.getElementById('d').hidden"),
        "true"
    );
    assert_eq!(
        eval_str(&mut engine, "document.getElementById('e').hidden"),
        "false"
    );
}

#[test]
fn hidden_js_property_toggles_rendering() {
    let mut engine = ScriptEngine::new(
        r#"<div id="wrap"><p id="a">visible</p><p id="b">gone</p></div>"#,
        "",
        EventBindings::default(),
    )
    .expect("engine initializes");

    // Initial: both <p>s render.
    assert!(
        canvas_contains(&engine, "gone"),
        "the unhidden element must render"
    );

    // `el.hidden = true` sets the attribute and marks the node dirty.
    eval_str(&mut engine, "document.getElementById('b').hidden = true");
    assert_eq!(
        eval_str(&mut engine, "document.getElementById('b').hidden"),
        "true"
    );
    let dirty = engine.take_dirty();
    assert!(
        !dirty.is_empty(),
        "hiding an element must schedule a re-render"
    );

    // The re-layout drops the hidden element and its subtree.
    assert!(
        !canvas_contains(&engine, "gone"),
        "a hidden element must not render"
    );
    assert!(canvas_contains(&engine, "visible"));

    // `el.hidden = false` restores it.
    eval_str(&mut engine, "document.getElementById('b').hidden = false");
    assert_eq!(
        eval_str(&mut engine, "document.getElementById('b').hidden"),
        "false"
    );
    assert!(
        canvas_contains(&engine, "gone"),
        "unhiding must restore the element"
    );
}

#[test]
fn hidden_unchanged_write_does_not_mark_dirty() {
    let mut engine =
        ScriptEngine::new(r#"<div id="d" hidden></div>"#, "", EventBindings::default())
            .expect("engine initializes");
    eval_str(&mut engine, "document.getElementById('d').hidden = true");
    assert!(
        engine.take_dirty().is_empty(),
        "re-setting an already-present hidden attribute must not re-render"
    );
    eval_str(&mut engine, "document.getElementById('d').hidden = false");
    assert!(
        !engine.take_dirty().is_empty(),
        "clearing the hidden attribute must re-render"
    );
}

#[test]
fn dataset_access_returns_the_same_live_instance() {
    let mut engine = ScriptEngine::new(r#"<div id="d"></div>"#, "", EventBindings::default())
        .expect("engine initializes");
    assert_eq!(
        eval_str(
            &mut engine,
            "document.getElementById('d').dataset === document.getElementById('d').dataset"
        ),
        "true",
        "repeated access returns the same instance (browser identity)"
    );
}

#[test]
fn dataset_proto_keys_keep_object_semantics_and_names_are_validated() {
    let mut engine = ScriptEngine::new(
        r#"<div id="d" data-foo="1" data-constructor="x"></div>"#,
        "",
        EventBindings::default(),
    )
    .expect("engine initializes");
    eval_str(
        &mut engine,
        "globalThis.ds = document.getElementById('d').dataset",
    );
    // WebIDL named-property visibility: Object.prototype's own property
    // names keep their normal semantics and are never shadowed by data-*.
    assert_eq!(
        eval_str(&mut engine, "typeof globalThis.ds.constructor"),
        "function",
        "constructor must come from the prototype, not the data-constructor attr"
    );
    assert_eq!(eval_str(&mut engine, "'toString' in globalThis.ds"), "true");
    assert_eq!(
        eval_str(&mut engine, "globalThis.ds.hasOwnProperty('foo')"),
        "true",
        "prototype methods must work and see the data keys"
    );
    // The setter validates the transformed candidate name.
    assert_eq!(
        eval_str(
            &mut engine,
            "(function () { try { globalThis.ds['a b'] = 1; return 'no-throw'; } catch (e) { return e.name; } })()"
        ),
        "SyntaxError"
    );
    assert_eq!(
        eval_str(
            &mut engine,
            "(function () { try { globalThis.ds.UPPER = 1; return 'no-throw'; } catch (e) { return e.name; } })()"
        ),
        "SyntaxError",
        "UPPER transforms to '-u-p-p-e-r', which cannot start a name"
    );
    // Valid names still write; the deleter silently ignores invalid names.
    eval_str(&mut engine, "globalThis.ds['a-b'] = 2");
    assert_eq!(data_attr(&engine, "d", "data-a-b"), Some("2".into()));
    assert_eq!(
        eval_str(&mut engine, "delete globalThis.ds['a b']"),
        "true",
        "deleting an invalid name is a no-op, not a throw"
    );
}