web-api-cat 0.7.14

Bindings between boa-cat (JS engine) and the DOM (html-cat tree) plus fetch (net-cat). v0.7.14 adds `getElementsByTagName(name)` (with `'*'` universal and empty-input-yields-empty per spec) and `getElementsByClassName(classNames)` (whitespace-separated; ALL classes must match) on both `Element` and `document`. Both reuse the v0.7.11 selector machinery -- they're typed wrappers around `find_all_descendants` with the input rewritten as a selector string. Empty / whitespace-only inputs short-circuit to an empty NodeList without invoking the matcher (spec requires this; the underlying matcher would otherwise match everything). Seventh sub-crate of a Servo-replacement webview runtime targeting Tauri.
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! `addEventListener` / `removeEventListener` / `dispatchEvent`
//! (v0.7.4): `EventTarget` mixin on every element, bubble dispatch
//! up the `__parent__` chain.

use boa_cat::env::Env;
use boa_cat::evaluate_program_with;
use boa_cat::fuel::Fuel;
use boa_cat::heap::Heap;
use boa_cat::value::Value;
use ecma_lex_cat::lex;
use ecma_parse_cat::parse_script;
use web_api_cat::Error;

fn run(html: &str, script: &str) -> Result<Value, Error> {
    let html_doc = html_cat::parse(html)?;
    let (env, heap) = web_api_cat::install(Env::empty(), Heap::new(), &html_doc);
    let tokens = lex(script).map_err(boa_cat::Error::from)?;
    let program = parse_script(&tokens).map_err(boa_cat::Error::from)?;
    let (value, _heap) =
        evaluate_program_with(&program, env, heap, Fuel::new(500_000)).map_err(Error::from)?;
    Ok(value)
}

fn fail(_msg: &'static str) -> Error {
    Error::Engine(boa_cat::Error::Unsupported { feature: "test" })
}

#[test]
fn add_listener_then_dispatch_fires_the_handler() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let count = 0;
        const host = document.getElementById('host');
        host.addEventListener('click', () => { count = count + 1; });
        host.dispatchEvent({ type: 'click' });
        count",
    )?;
    matches!(value, Value::Number(n) if (n - 1.0).abs() < 1e-9)
        .then_some(())
        .ok_or_else(|| fail("expected handler to fire once on dispatch"))
}

#[test]
fn dispatch_event_returns_true() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "const host = document.getElementById('host');
        host.dispatchEvent({ type: 'noop' })",
    )?;
    matches!(value, Value::Boolean(true))
        .then_some(())
        .ok_or_else(|| fail("expected dispatchEvent to return true"))
}

#[test]
fn multiple_listeners_fire_in_registration_order() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let trace = '';
        const host = document.getElementById('host');
        host.addEventListener('click', () => { trace = trace + 'a'; });
        host.addEventListener('click', () => { trace = trace + 'b'; });
        host.addEventListener('click', () => { trace = trace + 'c'; });
        host.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "abc")
        .then_some(())
        .ok_or_else(|| fail("expected listeners to fire in registration order"))
}

#[test]
fn listeners_for_different_types_dont_cross_fire() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let clicks = 0;
        let submits = 0;
        const host = document.getElementById('host');
        host.addEventListener('click', () => { clicks = clicks + 1; });
        host.addEventListener('submit', () => { submits = submits + 1; });
        host.dispatchEvent({ type: 'click' });
        host.dispatchEvent({ type: 'click' });
        host.dispatchEvent({ type: 'submit' });
        clicks * 10 + submits",
    )?;
    matches!(value, Value::Number(n) if (n - 21.0).abs() < 1e-9)
        .then_some(())
        .ok_or_else(|| fail("expected clicks=2 submits=1 (encoded as 21)"))
}

#[test]
fn handler_receives_the_event_object_with_type() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let received = '';
        const host = document.getElementById('host');
        host.addEventListener('foo', (e) => { received = e.type; });
        host.dispatchEvent({ type: 'foo' });
        received",
    )?;
    matches!(value, Value::String(ref s) if s == "foo")
        .then_some(())
        .ok_or_else(|| fail("expected handler to receive event with .type"))
}

#[test]
fn remove_event_listener_drops_the_handler() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let count = 0;
        const host = document.getElementById('host');
        const handler = () => { count = count + 1; };
        host.addEventListener('click', handler);
        host.dispatchEvent({ type: 'click' });
        host.removeEventListener('click', handler);
        host.dispatchEvent({ type: 'click' });
        count",
    )?;
    matches!(value, Value::Number(n) if (n - 1.0).abs() < 1e-9)
        .then_some(())
        .ok_or_else(|| fail("expected count=1 after add/dispatch/remove/dispatch"))
}

#[test]
fn bubble_dispatch_fires_parent_listener() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        child.addEventListener('click', () => { trace = trace + 'c'; });
        parent.addEventListener('click', () => { trace = trace + 'p'; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "cp")
        .then_some(())
        .ok_or_else(|| fail("expected bubble: child then parent"))
}

#[test]
fn dispatch_on_root_doesnt_bubble_past_document_element() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let count = 0;
        const host = document.getElementById('host');
        const root = document.documentElement;
        root.addEventListener('click', () => { count = count + 1; });
        host.dispatchEvent({ type: 'click' });
        count",
    )?;
    matches!(value, Value::Number(n) if (n - 1.0).abs() < 1e-9)
        .then_some(())
        .ok_or_else(|| fail("expected root listener to fire once via bubble"))
}

#[test]
fn listener_throw_does_not_abort_remaining_listeners() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let trace = '';
        const host = document.getElementById('host');
        host.addEventListener('click', () => { trace = trace + 'a'; throw 'boom'; });
        host.addEventListener('click', () => { trace = trace + 'b'; });
        try { host.dispatchEvent({ type: 'click' }); } catch (e) {}
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "ab")
        .then_some(())
        .ok_or_else(|| fail("expected second listener to still fire after first throws"))
}

#[test]
fn add_event_listener_works_on_created_element() -> Result<(), Error> {
    let value = run(
        "<html><body></body></html>",
        "let count = 0;
        const el = document.createElement('span');
        el.addEventListener('ping', () => { count = count + 1; });
        el.dispatchEvent({ type: 'ping' });
        count",
    )?;
    matches!(value, Value::Number(n) if (n - 1.0).abs() < 1e-9)
        .then_some(())
        .ok_or_else(|| fail("expected event system to work on createElement output"))
}

#[test]
fn handler_can_close_over_outer_state() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let counter = { n: 0 };
        const host = document.getElementById('host');
        host.addEventListener('tick', () => { counter.n = counter.n + 1; });
        host.dispatchEvent({ type: 'tick' });
        host.dispatchEvent({ type: 'tick' });
        host.dispatchEvent({ type: 'tick' });
        counter.n",
    )?;
    matches!(value, Value::Number(n) if (n - 3.0).abs() < 1e-9)
        .then_some(())
        .ok_or_else(|| fail("expected handler closure to accumulate state across 3 dispatches"))
}

#[test]
fn dispatch_with_no_listeners_is_a_silent_noop() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "const host = document.getElementById('host');
        host.dispatchEvent({ type: 'unhandled' })",
    )?;
    matches!(value, Value::Boolean(true))
        .then_some(())
        .ok_or_else(|| fail("expected silent no-op when no listeners are registered"))
}

#[test]
fn prevent_default_makes_dispatch_event_return_false() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "const host = document.getElementById('host');
        host.addEventListener('click', (e) => { e.preventDefault(); });
        host.dispatchEvent({ type: 'click', cancelable: true })",
    )?;
    matches!(value, Value::Boolean(false))
        .then_some(())
        .ok_or_else(|| {
            fail(
                "expected dispatchEvent to return false after preventDefault on a cancelable event",
            )
        })
}

#[test]
fn prevent_default_sets_default_prevented_flag_on_event() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let observed = 'unset';
        const host = document.getElementById('host');
        host.addEventListener('click', (e) => { e.preventDefault(); });
        host.addEventListener('click', (e) => { observed = e.defaultPrevented; });
        host.dispatchEvent({ type: 'click', cancelable: true });
        observed",
    )?;
    matches!(value, Value::Boolean(true))
        .then_some(())
        .ok_or_else(|| fail("expected defaultPrevented flag visible to subsequent listeners"))
}

#[test]
fn dispatch_without_prevent_default_returns_true() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "const host = document.getElementById('host');
        host.addEventListener('click', () => {});
        host.dispatchEvent({ type: 'click' })",
    )?;
    matches!(value, Value::Boolean(true))
        .then_some(())
        .ok_or_else(|| {
            fail("expected dispatchEvent to return true when no preventDefault was called")
        })
}

#[test]
fn stop_propagation_halts_bubble_after_current_level() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        child.addEventListener('click', (e) => { trace = trace + 'c'; e.stopPropagation(); });
        parent.addEventListener('click', () => { trace = trace + 'p'; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "c")
        .then_some(())
        .ok_or_else(|| fail("expected stopPropagation to skip the parent listener"))
}

#[test]
fn stop_propagation_still_fires_remaining_listeners_at_current_level() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        child.addEventListener('click', (e) => { trace = trace + 'a'; e.stopPropagation(); });
        child.addEventListener('click', () => { trace = trace + 'b'; });
        parent.addEventListener('click', () => { trace = trace + 'p'; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "ab")
        .then_some(())
        .ok_or_else(|| fail("expected stopPropagation to keep sibling listeners but skip parent"))
}

#[test]
fn stop_immediate_propagation_halts_remaining_listeners_at_current_level() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        child.addEventListener('click', (e) => { trace = trace + 'a'; e.stopImmediatePropagation(); });
        child.addEventListener('click', () => { trace = trace + 'b'; });
        parent.addEventListener('click', () => { trace = trace + 'p'; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "a")
        .then_some(())
        .ok_or_else(|| fail("expected stopImmediatePropagation to skip BOTH sibling and parent"))
}

#[test]
fn event_target_stays_as_original_dispatch_target() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let observed = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        parent.addEventListener('click', (e) => { observed = e.target.tagName; });
        child.dispatchEvent({ type: 'click' });
        observed",
    )?;
    matches!(value, Value::String(ref s) if s == "span")
        .then_some(())
        .ok_or_else(|| fail("expected event.target to be the original dispatch target (span)"))
}

#[test]
fn event_current_target_reflects_current_bubble_level() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        child.addEventListener('click', (e) => { trace = trace + e.currentTarget.tagName; });
        parent.addEventListener('click', (e) => { trace = trace + ',' + e.currentTarget.tagName; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s.eq_ignore_ascii_case("span,div"))
        .then_some(())
        .ok_or_else(|| fail("expected currentTarget to update per bubble level"))
}

#[test]
fn event_type_is_preserved_through_decoration() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let received = '';
        const host = document.getElementById('host');
        host.addEventListener('myevent', (e) => { received = e.type; });
        host.dispatchEvent({ type: 'myevent' });
        received",
    )?;
    matches!(value, Value::String(ref s) if s == "myevent")
        .then_some(())
        .ok_or_else(|| fail("expected event.type to survive dispatch decoration"))
}

#[test]
fn prevent_default_does_not_affect_bubble() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        child.addEventListener('click', (e) => { e.preventDefault(); trace = trace + 'c'; });
        parent.addEventListener('click', () => { trace = trace + 'p'; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "cp")
        .then_some(())
        .ok_or_else(|| fail("expected bubble to still fire parent listener after preventDefault"))
}

#[test]
fn capture_listener_on_parent_fires_before_target_listener() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        parent.addEventListener('click', () => { trace = trace + 'pc'; }, true);
        child.addEventListener('click', () => { trace = trace + 'c'; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "pcc")
        .then_some(())
        .ok_or_else(|| fail("expected parent capture listener to fire BEFORE target listener"))
}

#[test]
fn bubble_listener_on_parent_fires_after_target_listener() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        parent.addEventListener('click', () => { trace = trace + 'pb'; });
        child.addEventListener('click', () => { trace = trace + 'c'; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "cpb")
        .then_some(())
        .ok_or_else(|| fail("expected child target listener BEFORE parent bubble listener"))
}

#[test]
fn capture_and_bubble_listeners_on_same_parent_both_fire() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        parent.addEventListener('click', () => { trace = trace + 'pc'; }, true);
        parent.addEventListener('click', () => { trace = trace + 'pb'; });
        child.addEventListener('click', () => { trace = trace + 'c'; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "pccpb")
        .then_some(())
        .ok_or_else(|| fail("expected order: parent capture, target, parent bubble (PCCpb)"))
}

#[test]
fn capture_via_options_object() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        parent.addEventListener('click', () => { trace = trace + 'pc'; }, { capture: true });
        child.addEventListener('click', () => { trace = trace + 'c'; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "pcc")
        .then_some(())
        .ok_or_else(|| fail("expected { capture: true } option to register as capture listener"))
}

#[test]
fn stop_propagation_in_capture_phase_skips_target_and_bubble() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='parent'><span id='child'>x</span></div></body></html>",
        "let trace = '';
        const child = document.getElementById('child');
        const parent = document.getElementById('parent');
        parent.addEventListener('click', (e) => { trace = trace + 'pc'; e.stopPropagation(); }, true);
        child.addEventListener('click', () => { trace = trace + 'c'; });
        parent.addEventListener('click', () => { trace = trace + 'pb'; });
        child.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "pc")
        .then_some(())
        .ok_or_else(|| {
            fail("expected stopPropagation in capture to skip both AT_TARGET and BUBBLE phases")
        })
}

#[test]
fn target_capture_listener_fires_at_target_phase_alongside_bubble() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let trace = '';
        const host = document.getElementById('host');
        host.addEventListener('click', () => { trace = trace + 'a'; }, true);
        host.addEventListener('click', () => { trace = trace + 'b'; });
        host.dispatchEvent({ type: 'click' });
        trace",
    )?;
    matches!(value, Value::String(ref s) if s == "ab")
        .then_some(())
        .ok_or_else(|| {
            fail("expected target's capture+bubble listeners to fire in registration order")
        })
}

#[test]
fn remove_event_listener_respects_capture_flag() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let count = 0;
        const host = document.getElementById('host');
        const handler = () => { count = count + 1; };
        host.addEventListener('click', handler, true);
        host.dispatchEvent({ type: 'click' });
        host.removeEventListener('click', handler);
        host.dispatchEvent({ type: 'click' });
        count",
    )?;
    matches!(value, Value::Number(n) if (n - 2.0).abs() < 1e-9)
        .then_some(())
        .ok_or_else(|| {
            fail("expected remove without capture=true to NOT remove a capture-registered handler")
        })
}

#[test]
fn remove_event_listener_with_matching_capture_drops_it() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "let count = 0;
        const host = document.getElementById('host');
        const handler = () => { count = count + 1; };
        host.addEventListener('click', handler, true);
        host.dispatchEvent({ type: 'click' });
        host.removeEventListener('click', handler, true);
        host.dispatchEvent({ type: 'click' });
        count",
    )?;
    matches!(value, Value::Number(n) if (n - 1.0).abs() < 1e-9)
        .then_some(())
        .ok_or_else(|| {
            fail("expected matching (callback, true) removal to drop the capture handler")
        })
}

#[test]
fn prevent_default_is_idempotent() -> Result<(), Error> {
    let value = run(
        "<html><body><div id='host'></div></body></html>",
        "const host = document.getElementById('host');
        host.addEventListener('click', (e) => {
            e.preventDefault();
            e.preventDefault();
            e.preventDefault();
        });
        host.dispatchEvent({ type: 'click', cancelable: true })",
    )?;
    matches!(value, Value::Boolean(false))
        .then_some(())
        .ok_or_else(|| fail("expected three preventDefault calls to still return false"))
}