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
//! `querySelector` / `querySelectorAll` tests.
//!
//! `document.querySelector(sel)` / `document.querySelectorAll(sel)` return the
//! first / all elements matching a CSS selector, in document order; the same
//! methods on an element scope the search to its subtree. Invalid selectors
//! throw a JS `SyntaxError` like a browser.

use htui::events::EventBindings;
use htui::script::ScriptEngine;

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

/// Evaluate a JS snippet in a fresh engine for `html`, returning the engine.
fn engine(html: &str) -> ScriptEngine {
    ScriptEngine::new(html, "", EventBindings::default()).expect("engine initializes")
}

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()
}

/// Run a statement / declaration script.
fn eval(engine: &mut ScriptEngine, code: &str) {
    engine.eval_source(code).expect("eval must succeed");
}

/// Run a `<script>` body and evaluate `expr` to a string.
fn run(html_body: &str, script: &str, expr: &str) -> String {
    let mut e = engine(&format!("{html_body}<script>{script}</script>"));
    eval_str(&mut e, expr)
}

const DOC: &str = r#"<div class="panel">
      <p id="intro" class="lead">hello</p>
      <p class="lead">second</p>
      <span>deep <em>em</em></span>
    </div>
    <ul>
      <li>one</li>
      <li>two</li>
      <li>three</li>
    </ul>"#;

#[test]
fn test_query_selector_tag_and_id_and_class() {
    init();
    assert_eq!(
        run(
            DOC,
            "globalThis.__t = document.querySelector('p').textContent;",
            "__t"
        ),
        "hello",
        "tag selector returns the first match in document order"
    );
    assert_eq!(
        run(
            DOC,
            "globalThis.__i = document.querySelector('#intro').textContent;",
            "__i"
        ),
        "hello",
        "id selector"
    );
    assert_eq!(
        run(
            DOC,
            "globalThis.__c = document.querySelector('.lead').textContent;",
            "__c"
        ),
        "hello",
        "class selector returns the first `.lead`"
    );
}

#[test]
fn test_query_selector_combinators() {
    init();
    assert_eq!(
        run(
            DOC,
            "globalThis.__d = document.querySelector('div span em').textContent;",
            "__d"
        ),
        "em",
        "descendant chain"
    );
    assert_eq!(
        run(
            DOC,
            "globalThis.__c = document.querySelector('div > p').id;",
            "__c"
        ),
        "intro",
        "child combinator"
    );
    // `~` walks every previous sibling (text nodes between elements do not
    // block it), so the span matches the earlier `<p>`s.
    assert_eq!(
        run(
            DOC,
            "globalThis.__s = document.querySelector('p ~ span') === null ? 'none' : 'hit';",
            "__s"
        ),
        "hit",
        "subsequent sibling combinator"
    );
    // `+` requires the immediately-preceding element sibling (no element
    // between them); use adjacent tags with no whitespace text between.
    let html = "<i>a</i><b>b</b><div>d</div>";
    assert_eq!(
        run(
            html,
            "globalThis.__a = document.querySelector('i + b').textContent;",
            "__a"
        ),
        "b",
        "adjacent sibling combinator"
    );
    assert_eq!(
        run(
            html,
            "globalThis.__u = document.querySelector('*').nodeType;",
            "__u"
        ),
        "1",
        "universal selector matches an element node"
    );
}

#[test]
fn test_query_selector_nth_child() {
    init();
    // No whitespace text between the list items, so the sibling indices are
    // clean (the engine counts text nodes toward `:nth-child`, like the CSS
    // stylesheet matcher).
    let html = "<ul><li>a</li><li>b</li><li>c</li></ul>";
    assert_eq!(
        run(
            html,
            "globalThis.__n = document.querySelector('li:nth-child(2)').textContent;",
            "__n"
        ),
        "b",
        ":nth-child(2) picks the second li"
    );
}

#[test]
fn test_query_selector_no_match_returns_null() {
    init();
    assert_eq!(
        run(
            DOC,
            "globalThis.__n = document.querySelector('article') === null;",
            "__n"
        ),
        "true",
        "no match returns null"
    );
}

#[test]
fn test_query_selector_invalid_selector_throws() {
    init();
    // Browsers throw SyntaxError for invalid selectors.
    let mut e = engine("<p>x</p>");
    let _ = e
        .eval_source("try { document.querySelector('..'); globalThis.__err = 'none'; } catch (err) { globalThis.__err = err.name; }")
        .expect("eval");
    assert_eq!(
        eval_str(&mut e, "__err"),
        "SyntaxError",
        "invalid selector surfaces as a JS SyntaxError"
    );
}

#[test]
fn test_query_selector_all_document_order() {
    init();
    assert_eq!(
        run(
            DOC,
            "var ls = document.querySelectorAll('li');
             globalThis.__len = ls.length;
             globalThis.__joined = Array.prototype.map.call(ls, function (l) { return l.textContent; }).join(',');
             globalThis.__first = ls[0].textContent;",
            "__len + '|' + __joined + '|' + __first"
        ),
        "3|one,two,three|one",
        "querySelectorAll returns every match in document order"
    );
    assert_eq!(
        run(
            DOC,
            "globalThis.__n = document.querySelectorAll('.lead').length;",
            "__n"
        ),
        "2",
        "compound class selector matches both leads"
    );
    assert_eq!(
        run(
            DOC,
            "globalThis.__n = document.querySelectorAll('*').length;",
            "__n"
        ),
        "12",
        "universal selector enumerates every element (incl. html/head/body)"
    );
}

#[test]
fn test_element_query_selector_scoped() {
    init();
    // Element-scoped queries search the subtree and never return the element
    // itself.
    let html = r#"<div id="outer"><p id="inner">inside</p></div><p id="out">outside</p>"#;
    assert_eq!(
        run(
            html,
            "var outer = document.getElementById('outer');
             globalThis.__scoped = outer.querySelector('p').id;
             globalThis.__self = outer.querySelector('#outer') === null;
             globalThis.__outside = outer.querySelector('#out') === null;
             globalThis.__all = outer.querySelectorAll('p').length;",
            "__scoped + '|' + __self + '|' + __outside + '|' + __all"
        ),
        "inner|true|true|1",
        "element.querySelector scopes to the subtree, excluding the element itself"
    );
    // The document query still sees everything.
    assert_eq!(
        run(
            html,
            "globalThis.__all = document.querySelectorAll('p').length;",
            "__all"
        ),
        "2",
        "document.querySelectorAll spans the whole document"
    );
}

#[test]
fn test_query_selector_returns_live_wrapper() {
    init();
    // The returned wrapper is the same live element object: mutating it is
    // visible through getElementById and the DOM.
    let mut e = engine(
        r#"<p id="a">one</p>
        <script>
          var el = document.querySelector('p');
          el.textContent = 'changed';
          globalThis.__via = document.getElementById('a').textContent;
        </script>"#,
    );
    assert_eq!(
        eval_str(&mut e, "__via"),
        "changed",
        "querySelector returns the live element wrapper"
    );
}

#[test]
fn test_query_selector_matches_new_elements() {
    init();
    // The engine runs its own arena, so a selector matches whatever the DOM
    // holds at query time — including non-`<style>`/`<script>` elements.
    let mut e = engine(
        r#"<div id="d"><span>s</span></div>
        <script>
          var s = document.querySelector('div span');
          globalThis.__t = s.textContent;
          var em = document.createElement('em');
          em.textContent = 'made';
          document.getElementById('d').appendChild(em);
          globalThis.__after = document.querySelector('#d em').textContent;
        </script>"#,
    );
    assert_eq!(eval_str(&mut e, "__t"), "s", "selector reads existing DOM");
    assert_eq!(
        eval_str(&mut e, "__after"),
        "made",
        "selector sees elements created by JS"
    );
}

#[test]
fn test_matches_and_closest() {
    init();
    let mut e = engine(
        r#"<div class="panel" id="p"><ul id="u"><li class="lead">one</li><li>two</li></ul></div>"#,
    );
    eval(&mut e, "var lead = document.querySelector('li.lead')");
    eval(&mut e, "var plain = document.querySelectorAll('li')[1]");
    // `matches` on the element itself.
    assert_eq!(eval_str(&mut e, "String(lead.matches('li.lead'))"), "true");
    assert_eq!(eval_str(&mut e, "String(lead.matches('.panel'))"), "false");
    // `closest` walks ancestor-or-self.
    assert_eq!(
        eval_str(&mut e, "lead.closest('ul').id"),
        "u",
        "closest finds the nearest matching ancestor"
    );
    assert_eq!(
        eval_str(&mut e, "lead.closest('.panel li').tagName"),
        "LI",
        "closest matches the element itself when the chain fits"
    );
    assert_eq!(
        eval_str(&mut e, "String(lead.closest('table'))"),
        "null",
        "no match returns null"
    );
    assert_eq!(
        eval_str(&mut e, "String(plain.matches(':not(.lead)'))"),
        "true",
        "structural pseudo-classes work through matches()"
    );
    // Invalid selector throws SyntaxError, like querySelector.
    eval(&mut e, "globalThis.__err = ''");
    eval(
        &mut e,
        "try { lead.matches('>>') } catch (x) { globalThis.__err = x.name }",
    );
    assert_eq!(eval_str(&mut e, "__err"), "SyntaxError");
}

#[test]
fn test_has_attribute_and_toggle_attribute() {
    init();
    let mut e = engine(r#"<input id="i" value="v"><input id="j" value="" checked>"#);
    eval(&mut e, "var i = document.getElementById('i')");
    eval(&mut e, "var j = document.getElementById('j')");
    assert_eq!(eval_str(&mut e, "String(i.hasAttribute('value'))"), "true");
    // An empty value still counts as present.
    assert_eq!(
        eval_str(&mut e, "String(j.hasAttribute('value'))"),
        "true",
        "empty-valued attribute is present"
    );
    assert_eq!(
        eval_str(&mut e, "String(i.hasAttribute('checked'))"),
        "false"
    );
    // toggleAttribute flips presence and reports the result.
    assert_eq!(
        eval_str(&mut e, "String(i.toggleAttribute('disabled'))"),
        "true"
    );
    assert_eq!(
        eval_str(&mut e, "String(i.hasAttribute('disabled'))"),
        "true"
    );
    assert_eq!(
        eval_str(&mut e, "String(i.toggleAttribute('disabled'))"),
        "false"
    );
    assert_eq!(
        eval_str(&mut e, "String(i.hasAttribute('disabled'))"),
        "false"
    );
    // `force` drives add / remove unconditionally.
    eval(&mut e, "j.toggleAttribute('checked', true)");
    assert_eq!(
        eval_str(&mut e, "String(j.hasAttribute('checked'))"),
        "true"
    );
    eval(&mut e, "j.toggleAttribute('checked', true)");
    assert_eq!(
        eval_str(&mut e, "String(j.hasAttribute('checked'))"),
        "true"
    );
}

#[test]
fn test_element_sibling_accessors() {
    init();
    let mut e = engine(
        r#"<div>text<b id="b">b</b> tail<i id="i">i</i><!-- c --><span id="s">s</span></div>"#,
    );
    eval(&mut e, "var b = document.getElementById('b')");
    eval(&mut e, "var i = document.getElementById('i')");
    eval(&mut e, "var s = document.getElementById('s')");
    // Element siblings skip text / comment nodes between them.
    assert_eq!(eval_str(&mut e, "String(b.previousElementSibling)"), "null");
    assert_eq!(eval_str(&mut e, "i.previousElementSibling.id"), "b");
    assert_eq!(eval_str(&mut e, "s.previousElementSibling.id"), "i");
    assert_eq!(eval_str(&mut e, "b.nextElementSibling.id"), "i");
    assert_eq!(eval_str(&mut e, "s.nextElementSibling"), "null");
    // Contrast with the raw node siblings (text / comment nodes included).
    assert_eq!(eval_str(&mut e, "i.previousSibling.nodeType"), "3");
    assert_eq!(eval_str(&mut e, "s.previousSibling.nodeType"), "8");
}

#[test]
fn test_structural_pseudo_classes_via_query_selector() {
    init();
    let mut e = engine(
        r#"<ul id="u"><li>one</li><li>two</li><li>three</li></ul>
           <form>
             <input id="cb" type="checkbox" checked>
             <input id="tx">
             <button id="bd" disabled>b</button>
           </form>"#,
    );
    assert_eq!(
        eval_str(&mut e, "document.querySelectorAll('li:first-child').length"),
        "1"
    );
    assert_eq!(
        eval_str(
            &mut e,
            "document.querySelector('li:last-child').textContent"
        ),
        "three"
    );
    assert_eq!(
        eval_str(
            &mut e,
            "document.querySelectorAll('li:nth-last-child(2)').length"
        ),
        "1",
        "second-to-last li matches"
    );
    assert_eq!(
        eval_str(
            &mut e,
            "document.querySelector('li:nth-last-child(2)').textContent"
        ),
        "two"
    );
    assert_eq!(
        eval_str(&mut e, "document.querySelectorAll('li:not(.x)').length"),
        "3",
        ":not matches every li (none carry .x)"
    );
    assert_eq!(
        eval_str(&mut e, "document.querySelectorAll(':checked').length"),
        "1",
        "the checked checkbox is :checked"
    );
    assert_eq!(
        eval_str(&mut e, "document.querySelector(':checked').id"),
        "cb"
    );
    assert_eq!(
        eval_str(&mut e, "document.querySelectorAll(':disabled').length"),
        "1",
        "the disabled button is :disabled"
    );
    assert_eq!(
        eval_str(&mut e, "document.querySelectorAll('input:enabled').length"),
        "2",
        "both inputs are :enabled (checked state does not disable)"
    );
    // `<html>` is the root element.
    assert_eq!(
        eval_str(&mut e, "document.querySelector(':root').tagName"),
        "HTML"
    );
    // A `<li>` with no element children but whitespace text is not :empty;
    // build a truly empty one to compare.
    eval(
        &mut e,
        "var blank = document.createElement('div'); document.body.appendChild(blank)",
    );
    assert_eq!(
        eval_str(
            &mut e,
            "String(document.querySelector('div:empty') === blank)"
        ),
        "true",
        "a freshly created div with no children is :empty"
    );
}