servo-script 0.2.0

A component of the servo web-engine.
Documentation
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::array::from_ref;
use std::cell::{Cell, RefCell};
use std::cmp::Ordering;
use std::f64::consts::PI;
use std::mem;
use std::rc::Rc;
use std::str::FromStr;
use std::time::{Duration, Instant};

use embedder_traits::{
    Cursor, EditingActionEvent, EmbedderMsg, ImeEvent, InputEvent, InputEventId, InputEventOutcome,
    InputEventResult, KeyboardEvent as EmbedderKeyboardEvent, MouseButton, MouseButtonAction,
    MouseButtonEvent, MouseLeftViewportEvent, TouchEvent as EmbedderTouchEvent, TouchEventType,
    TouchId, UntrustedNodeAddress, WheelEvent as EmbedderWheelEvent,
};
#[cfg(feature = "gamepad")]
use embedder_traits::{
    GamepadEvent as EmbedderGamepadEvent, GamepadSupportedHapticEffects, GamepadUpdateType,
};
use euclid::{Point2D, Vector2D};
use js::context::JSContext;
use js::jsapi::JSAutoRealm;
use keyboard_types::{Code, Key, KeyState, Modifiers, NamedKey};
use layout_api::{ScrollContainerQueryFlags, node_id_from_scroll_id};
use rustc_hash::FxHashMap;
use script_bindings::codegen::GenericBindings::DocumentBinding::DocumentMethods;
use script_bindings::codegen::GenericBindings::ElementBinding::ScrollLogicalPosition;
use script_bindings::codegen::GenericBindings::EventBinding::EventMethods;
use script_bindings::codegen::GenericBindings::HTMLElementBinding::HTMLElementMethods;
use script_bindings::codegen::GenericBindings::HTMLLabelElementBinding::HTMLLabelElementMethods;
use script_bindings::codegen::GenericBindings::KeyboardEventBinding::KeyboardEventMethods;
use script_bindings::codegen::GenericBindings::NavigatorBinding::NavigatorMethods;
use script_bindings::codegen::GenericBindings::PerformanceBinding::PerformanceMethods;
use script_bindings::codegen::GenericBindings::ShadowRootBinding::ShadowRootMethods;
use script_bindings::codegen::GenericBindings::TouchBinding::TouchMethods;
use script_bindings::codegen::GenericBindings::WindowBinding::{ScrollBehavior, WindowMethods};
use script_bindings::inheritance::Castable;
use script_bindings::match_domstring_ascii;
use script_bindings::num::Finite;
use script_bindings::reflector::DomObject;
use script_bindings::root::{Dom, DomRoot, DomSlice};
use script_bindings::script_runtime::CanGc;
use script_bindings::str::DOMString;
use script_traits::ConstellationInputEvent;
use servo_base::generic_channel::GenericCallback;
use servo_config::pref;
use servo_constellation_traits::{
    KeyboardScroll, ScriptToConstellationMessage, SequentialFocusDirection,
};
use style::Atom;
use style_traits::CSSPixel;
use webrender_api::ExternalScrollId;

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::inheritance::{ElementTypeId, HTMLElementTypeId, NodeTypeId};
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::root::MutNullableDom;
use crate::dom::bindings::trace::NoTrace;
use crate::dom::clipboardevent::ClipboardEventType;
use crate::dom::document::FireMouseEventType;
use crate::dom::document::focus::{
    FocusableArea, SequentialFocusNavigationMechanism, SequentialFocusNavigationSearch,
};
use crate::dom::event::{EventBubbles, EventCancelable, EventComposed, EventFlags};
#[cfg(feature = "gamepad")]
use crate::dom::gamepad::gamepad::{Gamepad, contains_user_gesture};
#[cfg(feature = "gamepad")]
use crate::dom::gamepad::gamepadevent::GamepadEventType;
use crate::dom::inputevent::HitTestResult;
use crate::dom::interactive_element_command::InteractiveElementCommand;
use crate::dom::keyboardevent::KeyboardEvent;
use crate::dom::node::{self, Node, NodeTraits, ShadowIncluding};
use crate::dom::pointerevent::{PointerEvent, PointerId};
use crate::dom::scrolling_box::{ScrollAxisState, ScrollRequirement, ScrollingBoxAxis};
use crate::dom::types::{
    ClipboardEvent, CompositionEvent, DataTransfer, Element, Event, EventTarget, GlobalScope,
    HTMLAnchorElement, HTMLElement, HTMLIFrameElement, HTMLLabelElement, MouseEvent, Touch,
    TouchEvent, TouchList, WheelEvent, Window,
};
use crate::drag_data_store::{DragDataStore, Kind, Mode};
use crate::realms::enter_realm;

/// A data structure used for tracking the current click count. This can be
/// reset to 0 if a mouse button event happens at a sufficient distance or time
/// from the previous one.
///
/// From <https://w3c.github.io/uievents/#current-click-count>:
/// > Implementations MUST maintain the current click count when generating mouse
/// > events. This MUST be a non-negative integer indicating the number of consecutive
/// > clicks of a pointing device button within a specific time. The delay after which
/// > the count resets is specific to the environment configuration.
#[derive(Default, JSTraceable, MallocSizeOf)]
struct ClickCountingInfo {
    time: Option<Instant>,
    #[no_trace]
    point: Option<Point2D<f32, CSSPixel>>,
    #[no_trace]
    button: Option<MouseButton>,
    count: usize,
}

impl ClickCountingInfo {
    fn reset_click_count_if_necessary(
        &mut self,
        button: MouseButton,
        point_in_frame: Point2D<f32, CSSPixel>,
    ) {
        let (Some(previous_button), Some(previous_point), Some(previous_time)) =
            (self.button, self.point, self.time)
        else {
            assert_eq!(self.count, 0);
            return;
        };

        let double_click_timeout =
            Duration::from_millis(pref!(dom_document_dblclick_timeout) as u64);
        let double_click_distance_threshold = pref!(dom_document_dblclick_dist) as u64;

        // Calculate distance between this click and the previous click.
        let line = point_in_frame - previous_point;
        let distance = (line.dot(line) as f64).sqrt();
        if previous_button != button ||
            Instant::now().duration_since(previous_time) > double_click_timeout ||
            distance > double_click_distance_threshold as f64
        {
            self.count = 0;
            self.time = None;
            self.point = None;
        }
    }

    fn increment_click_count(
        &mut self,
        button: MouseButton,
        point: Point2D<f32, CSSPixel>,
    ) -> usize {
        self.time = Some(Instant::now());
        self.point = Some(point);
        self.button = Some(button);
        self.count += 1;
        self.count
    }
}

/// The [`DocumentEventHandler`] is a structure responsible for handling input events for
/// the [`crate::Document`] and storing data related to event handling. It exists to
/// decrease the size of the [`crate::Document`] structure.
#[derive(JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub(crate) struct DocumentEventHandler {
    /// The [`Window`] element for this [`DocumentEventHandler`].
    window: Dom<Window>,
    /// Pending input events, to be handled at the next rendering opportunity.
    #[no_trace]
    #[ignore_malloc_size_of = "InputEvent contains data from outside crates"]
    pending_input_events: DomRefCell<Vec<ConstellationInputEvent>>,
    /// The index of the last mouse move event in the pending input events queue.
    mouse_move_event_index: DomRefCell<Option<usize>>,
    /// The [`InputEventId`]s of mousemove events that have been coalesced.
    #[no_trace]
    #[ignore_malloc_size_of = "InputEventId contains data from outside crates"]
    coalesced_move_event_ids: DomRefCell<Vec<InputEventId>>,
    /// The index of the last wheel event in the pending input events queue.
    /// This is non-standard behaviour.
    /// According to <https://www.w3.org/TR/pointerevents/#dfn-coalesced-events>,
    /// we should only coalesce `pointermove` events.
    wheel_event_index: DomRefCell<Option<usize>>,
    /// The [`InputEventId`]s of wheel events that have been coalesced.
    #[no_trace]
    #[ignore_malloc_size_of = "InputEventId contains data from outside crates"]
    coalesced_wheel_event_ids: DomRefCell<Vec<InputEventId>>,
    /// <https://w3c.github.io/uievents/#event-type-dblclick>
    click_counting_info: DomRefCell<ClickCountingInfo>,
    #[no_trace]
    last_mouse_button_down_point: Cell<Option<Point2D<f32, CSSPixel>>>,
    /// The number of currently down buttons, used to decide which kind
    /// of pointer event to dispatch on MouseDown/MouseUp.
    down_button_count: Cell<u32>,
    /// The element that is currently hovered by the cursor.
    current_hover_target: MutNullableDom<Element>,
    /// The element that was most recently clicked.
    most_recently_clicked_element: MutNullableDom<Element>,
    /// The most recent mouse movement point, used for processing `mouseleave` events.
    #[no_trace]
    most_recent_mousemove_point: Cell<Option<Point2D<f32, CSSPixel>>>,
    /// The currently set [`Cursor`] or `None` if the `Document` isn't being hovered
    /// by the cursor.
    #[no_trace]
    current_cursor: Cell<Option<Cursor>>,
    /// <http://w3c.github.io/touch-events/#dfn-active-touch-point>
    active_touch_points: DomRefCell<Vec<Dom<Touch>>>,
    /// The active keyboard modifiers for the WebView. This is updated when receiving any input event.
    #[no_trace]
    active_keyboard_modifiers: Cell<Modifiers>,
    /// Map from touch identifier to pointer ID for active touch points
    active_pointer_ids: DomRefCell<FxHashMap<i32, i32>>,
    /// Counter for generating unique pointer IDs for touch inputs
    next_touch_pointer_id: Cell<i32>,
    /// A map holding information about currently registered access key handlers.
    access_key_handlers: DomRefCell<FxHashMap<NoTrace<Code>, Dom<HTMLElement>>>,
    /// <https://html.spec.whatwg.org/multipage/#sequential-focus-navigation-starting-point>
    sequential_focus_navigation_starting_point: MutNullableDom<Node>,
}

impl DocumentEventHandler {
    pub(crate) fn new(window: &Window) -> Self {
        Self {
            window: Dom::from_ref(window),
            pending_input_events: Default::default(),
            mouse_move_event_index: Default::default(),
            coalesced_move_event_ids: Default::default(),
            wheel_event_index: Default::default(),
            coalesced_wheel_event_ids: Default::default(),
            click_counting_info: Default::default(),
            last_mouse_button_down_point: Default::default(),
            down_button_count: Cell::new(0),
            current_hover_target: Default::default(),
            most_recently_clicked_element: Default::default(),
            most_recent_mousemove_point: Default::default(),
            current_cursor: Default::default(),
            active_touch_points: Default::default(),
            active_keyboard_modifiers: Default::default(),
            active_pointer_ids: Default::default(),
            next_touch_pointer_id: Cell::new(1),
            access_key_handlers: Default::default(),
            sequential_focus_navigation_starting_point: Default::default(),
        }
    }

    /// Note a pending input event, to be processed at the next `update_the_rendering` task.
    pub(crate) fn note_pending_input_event(&self, event: ConstellationInputEvent) {
        let mut pending_input_events = self.pending_input_events.borrow_mut();
        if matches!(event.event.event, InputEvent::MouseMove(..)) {
            // First try to replace any existing mouse move event.
            if let Some(mouse_move_event) = self
                .mouse_move_event_index
                .borrow()
                .and_then(|index| pending_input_events.get_mut(index))
            {
                self.coalesced_move_event_ids
                    .borrow_mut()
                    .push(mouse_move_event.event.id);
                *mouse_move_event = event;
                return;
            }

            *self.mouse_move_event_index.borrow_mut() = Some(pending_input_events.len());
        }

        if let InputEvent::Wheel(ref new_wheel_event) = event.event.event {
            // Coalesce with any existing pending wheel event by summing deltas.
            if let Some(existing_constellation_wheel_event) = self
                .wheel_event_index
                .borrow()
                .and_then(|index| pending_input_events.get_mut(index))
            {
                if let InputEvent::Wheel(ref mut existing_wheel_event) =
                    existing_constellation_wheel_event.event.event
                {
                    if existing_wheel_event.delta.mode == new_wheel_event.delta.mode {
                        self.coalesced_wheel_event_ids
                            .borrow_mut()
                            .push(existing_constellation_wheel_event.event.id);
                        existing_wheel_event.delta.x += new_wheel_event.delta.x;
                        existing_wheel_event.delta.y += new_wheel_event.delta.y;
                        existing_wheel_event.delta.z += new_wheel_event.delta.z;
                        existing_wheel_event.point = new_wheel_event.point;
                        existing_constellation_wheel_event.event.id = event.event.id;
                        return;
                    }
                }
            }

            *self.wheel_event_index.borrow_mut() = Some(pending_input_events.len());
        }

        pending_input_events.push(event);
    }

    /// Whether or not this [`Document`] has any pending input events to be processed during
    /// "update the rendering."
    pub(crate) fn has_pending_input_events(&self) -> bool {
        !self.pending_input_events.borrow().is_empty()
    }

    pub(crate) fn alternate_action_keyboard_modifier_active(&self) -> bool {
        #[cfg(target_os = "macos")]
        {
            self.active_keyboard_modifiers
                .get()
                .contains(Modifiers::META)
        }
        #[cfg(not(target_os = "macos"))]
        {
            self.active_keyboard_modifiers
                .get()
                .contains(Modifiers::CONTROL)
        }
    }

    pub(crate) fn handle_pending_input_events(&self, cx: &mut JSContext) {
        debug_assert!(
            !self.pending_input_events.borrow().is_empty(),
            "handle_pending_input_events called with no events"
        );
        let _realm = enter_realm(&*self.window);

        // Reset the mouse and wheel event indices.
        *self.mouse_move_event_index.borrow_mut() = None;
        *self.wheel_event_index.borrow_mut() = None;
        let pending_input_events = mem::take(&mut *self.pending_input_events.borrow_mut());
        let mut coalesced_move_event_ids =
            mem::take(&mut *self.coalesced_move_event_ids.borrow_mut());
        let mut coalesced_wheel_event_ids =
            mem::take(&mut *self.coalesced_wheel_event_ids.borrow_mut());

        let mut input_event_outcomes = Vec::with_capacity(
            pending_input_events.len() +
                coalesced_move_event_ids.len() +
                coalesced_wheel_event_ids.len(),
        );
        // TODO: For some of these we still aren't properly calculating whether or not
        // the event was handled or if `preventDefault()` was called on it. Each of
        // these cases needs to be examined and some of them either fire more than one
        // event or fire events later. We have to make a good decision about what to
        // return to the embedder when that happens.
        for event in pending_input_events {
            self.active_keyboard_modifiers
                .set(event.active_keyboard_modifiers);
            let result = match event.event.event {
                InputEvent::MouseButton(mouse_button_event) => {
                    self.handle_native_mouse_button_event(cx, mouse_button_event, &event);
                    InputEventResult::default()
                },
                InputEvent::MouseMove(_) => {
                    self.handle_native_mouse_move_event(cx, &event);
                    input_event_outcomes.extend(
                        mem::take(&mut coalesced_move_event_ids)
                            .into_iter()
                            .map(|id| InputEventOutcome {
                                id,
                                result: InputEventResult::default(),
                            }),
                    );
                    InputEventResult::default()
                },
                InputEvent::MouseLeftViewport(mouse_leave_event) => {
                    self.handle_mouse_left_viewport_event(cx, &event, &mouse_leave_event);
                    InputEventResult::default()
                },
                InputEvent::Touch(touch_event) => self.handle_touch_event(cx, touch_event, &event),
                InputEvent::Wheel(wheel_event) => {
                    let result = self.handle_wheel_event(cx, wheel_event, &event);
                    input_event_outcomes.extend(
                        mem::take(&mut coalesced_wheel_event_ids)
                            .into_iter()
                            .map(|id| InputEventOutcome { id, result }),
                    );
                    result
                },
                InputEvent::Keyboard(keyboard_event) => {
                    self.handle_keyboard_event(cx, keyboard_event)
                },
                InputEvent::Ime(ime_event) => self.handle_ime_event(cx, ime_event),
                #[cfg(feature = "gamepad")]
                InputEvent::Gamepad(gamepad_event) => {
                    self.handle_gamepad_event(gamepad_event);
                    InputEventResult::default()
                },
                InputEvent::EditingAction(editing_action_event) => {
                    self.handle_editing_action(cx, None, editing_action_event)
                },
            };

            input_event_outcomes.push(InputEventOutcome {
                id: event.event.id,
                result,
            });
        }

        self.notify_embedder_that_events_were_handled(input_event_outcomes);
    }

    fn notify_embedder_that_events_were_handled(
        &self,
        input_event_outcomes: Vec<InputEventOutcome>,
    ) {
        // Wait to to notify the embedder that the event was handled until all pending DOM
        // event processing is finished.
        let trusted_window = Trusted::new(&*self.window);
        self.window
            .as_global_scope()
            .task_manager()
            .dom_manipulation_task_source()
            .queue(task!(notify_webdriver_input_event_completed: move || {
                let window = trusted_window.root();
                window.send_to_embedder(
                    EmbedderMsg::InputEventsHandled(window.webview_id(), input_event_outcomes));
            }));
    }

    /// When an event should be fired on the element that has focus, this returns the target. If
    /// there is no associated element with the focused area (such as when the viewport is focused),
    /// then the body is returned. If no body is returned then the `Window` is returned.
    fn target_for_events_following_focus(&self) -> DomRoot<EventTarget> {
        let document = self.window.Document();
        match &*document.focus_handler().focused_area() {
            FocusableArea::Node { node, .. } => DomRoot::from_ref(node.upcast()),
            FocusableArea::IFrameViewport { iframe_element, .. } => {
                DomRoot::from_ref(iframe_element.upcast())
            },
            FocusableArea::Viewport => document
                .GetBody()
                .map(DomRoot::upcast)
                .unwrap_or_else(|| DomRoot::from_ref(self.window.upcast())),
        }
    }

    pub(crate) fn set_cursor(&self, cursor: Option<Cursor>) {
        if cursor == self.current_cursor.get() {
            return;
        }
        self.current_cursor.set(cursor);
        self.window.send_to_embedder(EmbedderMsg::SetCursor(
            self.window.webview_id(),
            cursor.unwrap_or_default(),
        ));
    }

    fn handle_mouse_left_viewport_event(
        &self,
        cx: &mut JSContext,
        input_event: &ConstellationInputEvent,
        mouse_leave_event: &MouseLeftViewportEvent,
    ) {
        if let Some(current_hover_target) = self.current_hover_target.get() {
            let current_hover_target = current_hover_target.upcast::<Node>();
            for element in current_hover_target
                .inclusive_ancestors(ShadowIncluding::Yes)
                .filter_map(DomRoot::downcast::<Element>)
            {
                element.set_hover_state(false);
                self.element_for_activation(element).set_active_state(false);
            }

            if let Some(hit_test_result) = self
                .most_recent_mousemove_point
                .get()
                .and_then(|point| self.window.hit_test_from_point_in_viewport(point))
            {
                let mouse_out_event = MouseEvent::new_for_platform_motion_event(
                    cx,
                    &self.window,
                    FireMouseEventType::Out,
                    &hit_test_result,
                    input_event,
                );

                // Fire pointerout before mouseout
                mouse_out_event
                    .to_pointer_hover_event("pointerout", CanGc::from_cx(cx))
                    .upcast::<Event>()
                    .fire(current_hover_target.upcast(), CanGc::from_cx(cx));

                mouse_out_event
                    .upcast::<Event>()
                    .fire(current_hover_target.upcast(), CanGc::from_cx(cx));

                self.handle_mouse_enter_leave_event(
                    cx,
                    DomRoot::from_ref(current_hover_target),
                    None,
                    FireMouseEventType::Leave,
                    &hit_test_result,
                    input_event,
                );
            }
        }

        // We do not want to always inform the embedder that cursor has been set to the
        // default cursor, in order to avoid a timing issue when moving between `<iframe>`
        // elements. There is currently no way to control which `SetCursor` message will
        // reach the embedder first. This is safer when leaving the `WebView` entirely.
        if !mouse_leave_event.focus_moving_to_another_iframe {
            // If focus is moving to another frame, it will decide what the new status
            // text is, but if this mouse leave event is leaving the WebView entirely,
            // then clear it.
            self.window
                .send_to_embedder(EmbedderMsg::Status(self.window.webview_id(), None));
            self.set_cursor(None);
        } else {
            self.current_cursor.set(None);
        }

        self.current_hover_target.set(None);
        self.most_recent_mousemove_point.set(None);
    }

    fn handle_mouse_enter_leave_event(
        &self,
        cx: &mut JSContext,
        event_target: DomRoot<Node>,
        related_target: Option<DomRoot<Node>>,
        event_type: FireMouseEventType,
        hit_test_result: &HitTestResult,
        input_event: &ConstellationInputEvent,
    ) {
        assert!(matches!(
            event_type,
            FireMouseEventType::Enter | FireMouseEventType::Leave
        ));

        let common_ancestor = match related_target.as_ref() {
            Some(related_target) => event_target
                .common_ancestor_in_flat_tree(related_target)
                .unwrap_or_else(|| DomRoot::from_ref(&*event_target)),
            None => DomRoot::from_ref(&*event_target),
        };

        // We need to create a target chain in case the event target shares
        // its boundaries with its ancestors.
        let mut targets = vec![];
        let mut current = Some(event_target);
        while let Some(node) = current {
            if node == common_ancestor {
                break;
            }
            current = node.parent_in_flat_tree();
            targets.push(node);
        }

        // The order for dispatching mouseenter/pointerenter events starts from the topmost
        // common ancestor of the event target and the related target.
        if event_type == FireMouseEventType::Enter {
            targets = targets.into_iter().rev().collect();
        }

        let pointer_event_name = match event_type {
            FireMouseEventType::Enter => "pointerenter",
            FireMouseEventType::Leave => "pointerleave",
            _ => unreachable!(),
        };

        for target in targets {
            let mouse_event = MouseEvent::new_for_platform_motion_event(
                cx,
                &self.window,
                event_type,
                hit_test_result,
                input_event,
            );
            mouse_event
                .upcast::<Event>()
                .set_related_target(related_target.as_ref().map(|target| target.upcast()));

            // Fire pointer event before mouse event
            mouse_event
                .to_pointer_hover_event(pointer_event_name, CanGc::from_cx(cx))
                .upcast::<Event>()
                .fire(target.upcast(), CanGc::from_cx(cx));

            // Fire mouse event
            mouse_event
                .upcast::<Event>()
                .fire(target.upcast(), CanGc::from_cx(cx));
        }
    }

    /// <https://w3c.github.io/uievents/#handle-native-mouse-move>
    fn handle_native_mouse_move_event(
        &self,
        cx: &mut JSContext,
        input_event: &ConstellationInputEvent,
    ) {
        // Ignore all incoming events without a hit test.
        let Some(hit_test_result) = self.window.hit_test_from_input_event(input_event) else {
            return;
        };

        let old_mouse_move_point = self
            .most_recent_mousemove_point
            .replace(Some(hit_test_result.point_in_frame));
        if old_mouse_move_point == Some(hit_test_result.point_in_frame) {
            return;
        }

        // Update the cursor when the mouse moves, if it has changed.
        self.set_cursor(Some(hit_test_result.cursor));

        let Some(new_target) = hit_test_result
            .node
            .inclusive_ancestors(ShadowIncluding::Yes)
            .find_map(DomRoot::downcast::<Element>)
        else {
            return;
        };

        let old_hover_target = self.current_hover_target.get();
        let target_has_changed = old_hover_target
            .as_ref()
            .is_none_or(|old_target| *old_target != new_target);

        // Here we know the target has changed, so we must update the state,
        // dispatch mouseout to the previous one, mouseover to the new one.
        if target_has_changed {
            // Dispatch pointerout/mouseout and pointerleave/mouseleave to previous target.
            if let Some(old_target) = self.current_hover_target.get() {
                let old_target_is_ancestor_of_new_target = old_target
                    .upcast::<Node>()
                    .is_ancestor_of(new_target.upcast::<Node>());

                // If the old target is an ancestor of the new target, this can be skipped
                // completely, since the node's hover state will be reset below.
                if !old_target_is_ancestor_of_new_target {
                    for element in old_target
                        .upcast::<Node>()
                        .inclusive_ancestors(ShadowIncluding::No)
                        .filter_map(DomRoot::downcast::<Element>)
                    {
                        element.set_hover_state(false);
                        self.element_for_activation(element).set_active_state(false);
                    }
                }

                let mouse_out_event = MouseEvent::new_for_platform_motion_event(
                    cx,
                    &self.window,
                    FireMouseEventType::Out,
                    &hit_test_result,
                    input_event,
                );
                mouse_out_event
                    .upcast::<Event>()
                    .set_related_target(Some(new_target.upcast()));

                // Fire pointerout before mouseout
                mouse_out_event
                    .to_pointer_hover_event("pointerout", CanGc::from_cx(cx))
                    .upcast::<Event>()
                    .fire(old_target.upcast(), CanGc::from_cx(cx));

                mouse_out_event
                    .upcast::<Event>()
                    .fire(old_target.upcast(), CanGc::from_cx(cx));

                if !old_target_is_ancestor_of_new_target {
                    let event_target = DomRoot::from_ref(old_target.upcast::<Node>());
                    let moving_into = Some(DomRoot::from_ref(new_target.upcast::<Node>()));
                    self.handle_mouse_enter_leave_event(
                        cx,
                        event_target,
                        moving_into,
                        FireMouseEventType::Leave,
                        &hit_test_result,
                        input_event,
                    );
                }
            }

            // Dispatch pointerover/mouseover and pointerenter/mouseenter to new target.
            for element in new_target
                .upcast::<Node>()
                .inclusive_ancestors(ShadowIncluding::Yes)
                .filter_map(DomRoot::downcast::<Element>)
            {
                element.set_hover_state(true);
            }

            let mouse_over_event = MouseEvent::new_for_platform_motion_event(
                cx,
                &self.window,
                FireMouseEventType::Over,
                &hit_test_result,
                input_event,
            );
            mouse_over_event
                .upcast::<Event>()
                .set_related_target(old_hover_target.as_ref().map(|target| target.upcast()));

            // Fire pointerover before mouseover
            mouse_over_event
                .to_pointer_hover_event("pointerover", CanGc::from_cx(cx))
                .upcast::<Event>()
                .dispatch(new_target.upcast(), false, CanGc::from_cx(cx));

            mouse_over_event.upcast::<Event>().dispatch(
                new_target.upcast(),
                false,
                CanGc::from_cx(cx),
            );

            let moving_from =
                old_hover_target.map(|old_target| DomRoot::from_ref(old_target.upcast::<Node>()));
            let event_target = DomRoot::from_ref(new_target.upcast::<Node>());
            self.handle_mouse_enter_leave_event(
                cx,
                event_target,
                moving_from,
                FireMouseEventType::Enter,
                &hit_test_result,
                input_event,
            );
        }

        // Send mousemove event to topmost target, unless it's an iframe, in which case
        // `Paint` should have also sent an event to the inner document.
        let mouse_event = MouseEvent::new_for_platform_motion_event(
            cx,
            &self.window,
            FireMouseEventType::Move,
            &hit_test_result,
            input_event,
        );

        // Send pointermove event before mousemove.
        let pointer_event =
            mouse_event.to_pointer_event(Atom::from("pointermove"), CanGc::from_cx(cx));
        pointer_event.upcast::<Event>().set_composed(true);
        pointer_event
            .upcast::<Event>()
            .fire(new_target.upcast(), CanGc::from_cx(cx));

        // Send mousemove event to topmost target, unless it's an iframe, in which case
        // `Paint` should have also sent an event to the inner document.
        mouse_event
            .upcast::<Event>()
            .fire(new_target.upcast(), CanGc::from_cx(cx));

        self.update_current_hover_target_and_status(Some(new_target));
    }

    fn update_current_hover_target_and_status(&self, new_hover_target: Option<DomRoot<Element>>) {
        let current_hover_target = self.current_hover_target.get();
        if current_hover_target == new_hover_target {
            return;
        }

        let previous_hover_target = self.current_hover_target.get();
        self.current_hover_target.set(new_hover_target.as_deref());

        // If the new hover target is an anchor with a status value, inform the embedder
        // of the new value.
        if let Some(target) = self.current_hover_target.get() {
            if let Some(anchor) = target
                .upcast::<Node>()
                .inclusive_ancestors(ShadowIncluding::Yes)
                .find_map(DomRoot::downcast::<HTMLAnchorElement>)
            {
                let status = anchor
                    .full_href_url_for_user_interface()
                    .map(|url| url.to_string());
                self.window
                    .send_to_embedder(EmbedderMsg::Status(self.window.webview_id(), status));
                return;
            }
        }

        // No state was set above, which means that the new value of the status in the embedder
        // should be `None`. Set that now. If `previous_hover_target` is `None` that means this
        // is the first mouse move event we are seeing after getting the cursor. In that case,
        // we also clear the status.
        if previous_hover_target.is_none_or(|previous_hover_target| {
            previous_hover_target
                .upcast::<Node>()
                .inclusive_ancestors(ShadowIncluding::Yes)
                .any(|node| node.is::<HTMLAnchorElement>())
        }) {
            self.window
                .send_to_embedder(EmbedderMsg::Status(self.window.webview_id(), None));
        }
    }

    pub(crate) fn handle_refresh_cursor(&self) {
        let Some(most_recent_mousemove_point) = self.most_recent_mousemove_point.get() else {
            return;
        };

        let Some(hit_test_result) = self
            .window
            .hit_test_from_point_in_viewport(most_recent_mousemove_point)
        else {
            return;
        };

        self.set_cursor(Some(hit_test_result.cursor));
    }

    fn element_for_activation(&self, element: DomRoot<Element>) -> DomRoot<Element> {
        let node: &Node = element.upcast();
        if node.is_in_ua_widget() {
            if let Some(containing_shadow_root) = node.containing_shadow_root() {
                return containing_shadow_root.Host();
            }
        }

        // If the element is a label, the activable element is the control element.
        if node.type_id() ==
            NodeTypeId::Element(ElementTypeId::HTMLElement(
                HTMLElementTypeId::HTMLLabelElement,
            ))
        {
            let label = element.downcast::<HTMLLabelElement>().unwrap();
            if let Some(control) = label.GetControl() {
                return DomRoot::from_ref(control.upcast::<Element>());
            }
        }

        element
    }

    /// <https://w3c.github.io/uievents/#mouseevent-algorithms>
    /// Handles native mouse down, mouse up, mouse click.
    fn handle_native_mouse_button_event(
        &self,
        cx: &mut JSContext,
        event: MouseButtonEvent,
        input_event: &ConstellationInputEvent,
    ) {
        // Ignore all incoming events without a hit test.
        let Some(hit_test_result) = self.window.hit_test_from_input_event(input_event) else {
            return;
        };

        debug!(
            "{:?}: at {:?}",
            event.action, hit_test_result.point_in_frame
        );

        // Set the sequential focus navigation starting point for any mouse button down event, no
        // matter if the target is not a node.
        if event.action == MouseButtonAction::Down {
            self.set_sequential_focus_navigation_starting_point(&hit_test_result.node);
        }

        let Some(element) = hit_test_result
            .node
            .inclusive_ancestors(ShadowIncluding::Yes)
            .find_map(DomRoot::downcast::<Element>)
        else {
            return;
        };

        let node = element.upcast::<Node>();
        debug!("{:?} on {:?}", event.action, node.debug_str());

        // <https://html.spec.whatwg.org/multipage/#selector-active>
        // If the element is being actively pointed at the element is being activated.
        // Disabled elements can also be activated.
        if event.action == MouseButtonAction::Down {
            self.element_for_activation(element.clone())
                .set_active_state(true);
        }
        if event.action == MouseButtonAction::Up {
            self.element_for_activation(element.clone())
                .set_active_state(false);
        }

        // https://w3c.github.io/uievents/#hit-test
        // Prevent mouse event if element is disabled.
        // TODO: also inert.
        if element.is_actually_disabled() {
            return;
        }

        let mouse_event_type = match event.action {
            embedder_traits::MouseButtonAction::Up => atom!("mouseup"),
            embedder_traits::MouseButtonAction::Down => atom!("mousedown"),
        };

        // From <https://w3c.github.io/pointerevents/#dfn-mousedown>
        // and <https://w3c.github.io/pointerevents/#mouseup>:
        //
        // UIEvent.detail: indicates the current click count incremented by one. For
        // example, if no click happened before the mousedown, detail will contain
        // the value 1
        if event.action == MouseButtonAction::Down {
            self.click_counting_info
                .borrow_mut()
                .reset_click_count_if_necessary(event.button, hit_test_result.point_in_frame);
        }

        let dom_event = DomRoot::upcast::<Event>(MouseEvent::for_platform_button_event(
            cx,
            mouse_event_type,
            event,
            input_event.pressed_mouse_buttons,
            &self.window,
            &hit_test_result,
            input_event.active_keyboard_modifiers,
            self.click_counting_info.borrow().count + 1,
        ));

        match event.action {
            MouseButtonAction::Down => {
                self.last_mouse_button_down_point
                    .set(Some(hit_test_result.point_in_frame));

                // Step 6. Dispatch pointerdown event.
                let down_button_count = self.down_button_count.get();

                let event_type = if down_button_count == 0 {
                    "pointerdown"
                } else {
                    "pointermove"
                };
                let pointer_event = dom_event
                    .downcast::<MouseEvent>()
                    .unwrap()
                    .to_pointer_event(event_type.into(), CanGc::from_cx(cx));

                pointer_event
                    .upcast::<Event>()
                    .fire(node.upcast(), CanGc::from_cx(cx));

                self.down_button_count.set(down_button_count + 1);

                // Step 7. Let result = dispatch event at target
                let result = dom_event.dispatch(node.upcast(), false, CanGc::from_cx(cx));

                // Step 8. If result is true and target is a focusable area
                // that is click focusable, then Run the focusing steps at target.
                if result {
                    // Note that this differs from the specification, because we are going to look
                    // for the first inclusive ancestor that is click focusable and then focus it.
                    // See documentation for [`Node::find_click_focusable_area`].
                    self.window
                        .Document()
                        .focus_handler()
                        .focus(cx, node.find_click_focusable_area());
                }

                // Step 9. If mbutton is the secondary mouse button, then
                // Maybe show context menu with native, target.
                if let MouseButton::Right = event.button {
                    self.maybe_show_context_menu(
                        node.upcast(),
                        &hit_test_result,
                        input_event,
                        CanGc::from_cx(cx),
                    );
                }
            },
            // https://w3c.github.io/pointerevents/#dfn-handle-native-mouse-up
            MouseButtonAction::Up => {
                // Step 6. Dispatch pointerup event.
                let down_button_count = self.down_button_count.get();

                if down_button_count > 0 {
                    self.down_button_count.set(down_button_count - 1);
                }

                let event_type = if down_button_count == 0 {
                    "pointerup"
                } else {
                    "pointermove"
                };
                let pointer_event = dom_event
                    .downcast::<MouseEvent>()
                    .unwrap()
                    .to_pointer_event(event_type.into(), CanGc::from_cx(cx));

                pointer_event
                    .upcast::<Event>()
                    .fire(node.upcast(), CanGc::from_cx(cx));

                // Step 7. dispatch event at target.
                dom_event.dispatch(node.upcast(), false, CanGc::from_cx(cx));

                // Click counts should still work for other buttons even though they
                // do not trigger "click" and "dblclick" events, so we increment
                // even when those events are not fired.
                self.click_counting_info
                    .borrow_mut()
                    .increment_click_count(event.button, hit_test_result.point_in_frame);

                self.maybe_trigger_click_for_mouse_button_down_event(
                    cx,
                    event,
                    input_event,
                    &hit_test_result,
                    &element,
                );
            },
        }
    }

    /// <https://w3c.github.io/pointerevents/#handle-native-mouse-click>
    /// <https://w3c.github.io/pointerevents/#handle-native-mouse-double-click>
    fn maybe_trigger_click_for_mouse_button_down_event(
        &self,
        cx: &mut JSContext,
        event: MouseButtonEvent,
        input_event: &ConstellationInputEvent,
        hit_test_result: &HitTestResult,
        element: &Element,
    ) {
        if event.button != MouseButton::Left {
            return;
        }

        let Some(last_mouse_button_down_point) = self.last_mouse_button_down_point.take() else {
            return;
        };

        let distance = last_mouse_button_down_point.distance_to(hit_test_result.point_in_frame);
        let maximum_click_distance = 10.0 * self.window.device_pixel_ratio().get();
        if distance > maximum_click_distance {
            return;
        }

        // From <https://w3c.github.io/pointerevents/#click>
        // > The click event type MUST be dispatched on the topmost event target indicated by the
        // > pointer, when the user presses down and releases the primary pointer button.
        //
        // For nodes inside a text input UA shadow DOM, dispatch dblclick at the shadow host.
        // TODO: This should likely be handled via event retargeting.
        let element = match hit_test_result.node.find_click_focusable_area() {
            FocusableArea::Node { node, .. } => DomRoot::downcast::<Element>(node),
            _ => None,
        }
        .unwrap_or_else(|| DomRoot::from_ref(element));
        self.most_recently_clicked_element.set(Some(&*element));

        let click_count = self.click_counting_info.borrow().count;
        element.set_click_in_progress(true);
        MouseEvent::for_platform_button_event(
            cx,
            atom!("click"),
            event,
            input_event.pressed_mouse_buttons,
            &self.window,
            hit_test_result,
            input_event.active_keyboard_modifiers,
            click_count,
        )
        .upcast::<Event>()
        .dispatch(element.upcast(), false, CanGc::from_cx(cx));
        element.set_click_in_progress(false);

        // The firing of "dbclick" events is dependent on the platform, so we have
        // some flexibility here. Some browsers on some platforms only fire a
        // "dbclick" when the click count is 2 and others essentially fire one for
        // every 2 clicks in a sequence. In all cases, browsers set the click count
        // `detail` property to 2.
        //
        // We follow the latter approach here, considering that every sequence of
        // even numbered clicks is a series of double clicks.
        if click_count % 2 == 0 {
            MouseEvent::for_platform_button_event(
                cx,
                Atom::from("dblclick"),
                event,
                input_event.pressed_mouse_buttons,
                &self.window,
                hit_test_result,
                input_event.active_keyboard_modifiers,
                2,
            )
            .upcast::<Event>()
            .dispatch(element.upcast(), false, CanGc::from_cx(cx));
        }
    }

    /// <https://www.w3.org/TR/pointerevents4/#maybe-show-context-menu>
    fn maybe_show_context_menu(
        &self,
        target: &EventTarget,
        hit_test_result: &HitTestResult,
        input_event: &ConstellationInputEvent,
        can_gc: CanGc,
    ) {
        // <https://w3c.github.io/pointerevents/#contextmenu>
        let menu_event = PointerEvent::new(
            &self.window,                // window
            "contextmenu".into(),        // type
            EventBubbles::Bubbles,       // can_bubble
            EventCancelable::Cancelable, // cancelable
            Some(&self.window),          // view
            0,                           // detail
            hit_test_result.point_in_frame.to_i32(),
            hit_test_result.point_in_frame.to_i32(),
            hit_test_result
                .point_relative_to_initial_containing_block
                .to_i32(),
            input_event.active_keyboard_modifiers,
            2i16, // button, right mouse button
            input_event.pressed_mouse_buttons,
            None,                     // related_target
            None,                     // point_in_target
            PointerId::Mouse as i32,  // pointer_id
            1,                        // width
            1,                        // height
            0.5,                      // pressure
            0.0,                      // tangential_pressure
            0,                        // tilt_x
            0,                        // tilt_y
            0,                        // twist
            PI / 2.0,                 // altitude_angle
            0.0,                      // azimuth_angle
            DOMString::from("mouse"), // pointer_type
            true,                     // is_primary
            vec![],                   // coalesced_events
            vec![],                   // predicted_events
            can_gc,
        );
        menu_event.upcast::<Event>().set_composed(true);

        // Step 3. Let result = dispatch menuevent at target.
        let result = menu_event.upcast::<Event>().fire(target, can_gc);

        // Step 4. If result is true, then show the UA context menu
        if result {
            self.window
                .Document()
                .embedder_controls()
                .show_context_menu(hit_test_result);
        };
    }

    fn handle_touch_event(
        &self,
        cx: &mut JSContext,
        event: EmbedderTouchEvent,
        input_event: &ConstellationInputEvent,
    ) -> InputEventResult {
        // Ignore all incoming events without a hit test.
        let Some(hit_test_result) = self.window.hit_test_from_input_event(input_event) else {
            self.update_active_touch_points_when_early_return(event);
            return Default::default();
        };

        let TouchId(identifier) = event.touch_id;

        let Some(element) = hit_test_result
            .node
            .inclusive_ancestors(ShadowIncluding::Yes)
            .find_map(DomRoot::downcast::<Element>)
        else {
            self.update_active_touch_points_when_early_return(event);
            return Default::default();
        };

        let current_target = DomRoot::upcast::<EventTarget>(element.clone());
        let window = &*self.window;

        let client_x = Finite::wrap(hit_test_result.point_in_frame.x as f64);
        let client_y = Finite::wrap(hit_test_result.point_in_frame.y as f64);
        let page_x =
            Finite::wrap(hit_test_result.point_in_frame.x as f64 + window.PageXOffset() as f64);
        let page_y =
            Finite::wrap(hit_test_result.point_in_frame.y as f64 + window.PageYOffset() as f64);

        // This is used to construct pointerevent and touchdown event.
        let pointer_touch = Touch::new(
            window,
            identifier,
            &current_target,
            client_x,
            client_y, // TODO: Get real screen coordinates?
            client_x,
            client_y,
            page_x,
            page_y,
            CanGc::from_cx(cx),
        );

        // Dispatch pointer event before updating active touch points and before touch event.
        let pointer_event_name = match event.event_type {
            TouchEventType::Down => "pointerdown",
            TouchEventType::Move => "pointermove",
            TouchEventType::Up => "pointerup",
            TouchEventType::Cancel => "pointercancel",
        };

        // Get or create pointer ID for this touch
        let pointer_id = self.get_or_create_pointer_id_for_touch(identifier);
        let is_primary = self.is_primary_pointer(pointer_id);

        // For touch devices (which don't support hover), fire pointerover/pointerenter
        // <https://w3c.github.io/pointerevents/#mapping-for-devices-that-do-not-support-hover>
        if matches!(event.event_type, TouchEventType::Down) {
            // Fire pointerover
            let pointer_over = pointer_touch.to_pointer_event(
                window,
                "pointerover",
                pointer_id,
                is_primary,
                input_event.active_keyboard_modifiers,
                true, // cancelable
                Some(hit_test_result.point_in_node),
                CanGc::from_cx(cx),
            );
            pointer_over
                .upcast::<Event>()
                .fire(&current_target, CanGc::from_cx(cx));

            // Fire pointerenter hierarchically (from topmost ancestor to target)
            self.fire_pointer_event_for_touch(
                &element,
                &pointer_touch,
                pointer_id,
                "pointerenter",
                is_primary,
                input_event,
                &hit_test_result,
                CanGc::from_cx(cx),
            );
        }

        let pointer_event = pointer_touch.to_pointer_event(
            window,
            pointer_event_name,
            pointer_id,
            is_primary,
            input_event.active_keyboard_modifiers,
            event.is_cancelable(),
            Some(hit_test_result.point_in_node),
            CanGc::from_cx(cx),
        );
        pointer_event
            .upcast::<Event>()
            .fire(&current_target, CanGc::from_cx(cx));

        // For touch devices, fire pointerout/pointerleave after pointerup/pointercancel
        // <https://w3c.github.io/pointerevents/#mapping-for-devices-that-do-not-support-hover>
        if matches!(
            event.event_type,
            TouchEventType::Up | TouchEventType::Cancel
        ) {
            // Fire pointerout
            let pointer_out = pointer_touch.to_pointer_event(
                window,
                "pointerout",
                pointer_id,
                is_primary,
                input_event.active_keyboard_modifiers,
                true, // cancelable
                Some(hit_test_result.point_in_node),
                CanGc::from_cx(cx),
            );
            pointer_out
                .upcast::<Event>()
                .fire(&current_target, CanGc::from_cx(cx));

            // Fire pointerleave hierarchically (from target to topmost ancestor)
            self.fire_pointer_event_for_touch(
                &element,
                &pointer_touch,
                pointer_id,
                "pointerleave",
                is_primary,
                input_event,
                &hit_test_result,
                CanGc::from_cx(cx),
            );
        }

        let (touch_dispatch_target, changed_touch) = match event.event_type {
            TouchEventType::Down => {
                // Add a new touch point
                self.active_touch_points
                    .borrow_mut()
                    .push(Dom::from_ref(&*pointer_touch));
                // <https://html.spec.whatwg.org/multipage/#selector-active>
                // If the element is being actively pointed at the element is being activated.
                self.element_for_activation(element).set_active_state(true);
                (current_target, pointer_touch)
            },
            _ => {
                // From <https://w3c.github.io/touch-events/#dfn-touchend>:
                // > For move/up/cancel:
                // > The target of this event must be the same Element on which the touch
                // > point started when it was first placed on the surface, even if the touch point
                // > has since moved outside the interactive area of the target element.
                let mut active_touch_points = self.active_touch_points.borrow_mut();
                let Some(index) = active_touch_points
                    .iter()
                    .position(|point| point.Identifier() == identifier)
                else {
                    warn!("No active touch point for {:?}", event.event_type);
                    return Default::default();
                };
                // This is the original target that was selected during `touchstart` event handling.
                let original_target = active_touch_points[index].Target();

                let touch_with_touchstart_target = Touch::new(
                    window,
                    identifier,
                    &original_target,
                    client_x,
                    client_y,
                    client_x,
                    client_y,
                    page_x,
                    page_y,
                    CanGc::from_cx(cx),
                );

                // Update or remove the stored touch
                match event.event_type {
                    TouchEventType::Move => {
                        active_touch_points[index] = Dom::from_ref(&*touch_with_touchstart_target);
                    },
                    TouchEventType::Up | TouchEventType::Cancel => {
                        active_touch_points.swap_remove(index);
                        self.remove_pointer_id_for_touch(identifier);
                        // <https://html.spec.whatwg.org/multipage/#selector-active>
                        // If the element is being actively pointed at the element is being activated.
                        self.element_for_activation(element).set_active_state(false);
                    },
                    TouchEventType::Down => unreachable!("Should have been handled above"),
                }
                (original_target, touch_with_touchstart_target)
            },
        };

        rooted_vec!(let mut target_touches);
        target_touches.extend(
            self.active_touch_points
                .borrow()
                .iter()
                .filter(|touch| touch.Target() == touch_dispatch_target)
                .cloned(),
        );

        let event_name = match event.event_type {
            TouchEventType::Down => "touchstart",
            TouchEventType::Move => "touchmove",
            TouchEventType::Up => "touchend",
            TouchEventType::Cancel => "touchcancel",
        };

        let touch_event = TouchEvent::new(
            window,
            event_name.into(),
            EventBubbles::Bubbles,
            EventCancelable::from(event.is_cancelable()),
            EventComposed::Composed,
            Some(window),
            0i32,
            &TouchList::new(
                window,
                self.active_touch_points.borrow().r(),
                CanGc::from_cx(cx),
            ),
            &TouchList::new(window, from_ref(&&*changed_touch), CanGc::from_cx(cx)),
            &TouchList::new(window, target_touches.r(), CanGc::from_cx(cx)),
            // FIXME: modifier keys
            false,
            false,
            false,
            false,
            CanGc::from_cx(cx),
        );
        let event = touch_event.upcast::<Event>();
        event.fire(&touch_dispatch_target, CanGc::from_cx(cx));
        event.flags().into()
    }

    /// Updates the active touch points when a hit test fails early.
    ///
    /// - For `Down`: No action needed; a failed down event won't create an active point.
    /// - For `Move`: No action needed; position information is unavailable, so we cannot update.
    /// - For `Up`/`Cancel`: Remove the corresponding touch point and its pointer ID mapping.
    ///
    /// When a touchup or touchcancel occurs at that touch point,
    /// a warning is triggered: Received touchup/touchcancel event for a non-active touch point.
    fn update_active_touch_points_when_early_return(&self, event: EmbedderTouchEvent) {
        match event.event_type {
            TouchEventType::Down | TouchEventType::Move => {},
            TouchEventType::Up | TouchEventType::Cancel => {
                let mut active_touch_points = self.active_touch_points.borrow_mut();
                if let Some(index) = active_touch_points
                    .iter()
                    .position(|t| t.Identifier() == event.touch_id.0)
                {
                    active_touch_points.swap_remove(index);
                    self.remove_pointer_id_for_touch(event.touch_id.0);
                } else {
                    warn!(
                        "Received {:?} for a non-active touch point {}",
                        event.event_type, event.touch_id.0
                    );
                }
            },
        }
    }

    /// The entry point for all key processing for web content
    fn handle_keyboard_event(
        &self,
        cx: &mut JSContext,
        keyboard_event: EmbedderKeyboardEvent,
    ) -> InputEventResult {
        let target = &self.target_for_events_following_focus();
        let keyevent = KeyboardEvent::new_with_platform_keyboard_event(
            cx,
            &self.window,
            keyboard_event.event.state.event_type().into(),
            &keyboard_event.event,
        );

        let event = keyevent.upcast::<Event>();

        event.set_composed(true);

        event.fire(target, CanGc::from_cx(cx));

        let mut flags = event.flags();
        if flags.contains(EventFlags::Canceled) {
            return flags.into();
        }

        // https://w3c.github.io/uievents/#keys-cancelable-keys
        // it MUST prevent the respective beforeinput and input
        // (and keypress if supported) events from being generated
        // TODO: keypress should be deprecated and superceded by beforeinput

        let is_character_value_key = matches!(
            keyboard_event.event.key,
            Key::Character(_) | Key::Named(NamedKey::Enter)
        );
        if keyboard_event.event.state == KeyState::Down &&
            is_character_value_key &&
            !keyboard_event.event.is_composing
        {
            // https://w3c.github.io/uievents/#keypress-event-order
            let keypress_event = KeyboardEvent::new_with_platform_keyboard_event(
                cx,
                &self.window,
                atom!("keypress"),
                &keyboard_event.event,
            );
            keypress_event.upcast::<Event>().set_composed(true);
            let event = keypress_event.upcast::<Event>();
            event.fire(target, CanGc::from_cx(cx));
            flags = event.flags();
        }

        flags.into()
    }

    fn handle_ime_event(&self, cx: &mut JSContext, event: ImeEvent) -> InputEventResult {
        let document = self.window.Document();
        let composition_event = match event {
            ImeEvent::Dismissed => {
                document.focus_handler().focus(cx, FocusableArea::Viewport);
                return Default::default();
            },
            ImeEvent::Composition(composition_event) => composition_event,
        };

        // spec: https://w3c.github.io/uievents/#compositionstart
        // spec: https://w3c.github.io/uievents/#compositionupdate
        // spec: https://w3c.github.io/uievents/#compositionend
        // > Event.target : focused element processing the composition
        let focused_area = document.focus_handler().focused_area();
        let Some(focused_element) = focused_area.element() else {
            // Event is only dispatched if there is a focused element.
            return Default::default();
        };

        let cancelable = composition_event.state == keyboard_types::CompositionState::Start;
        let event = CompositionEvent::new(
            &self.window,
            composition_event.state.event_type().into(),
            true,
            cancelable,
            Some(&self.window),
            0,
            DOMString::from(composition_event.data),
            CanGc::from_cx(cx),
        );

        let event = event.upcast::<Event>();
        event.fire(focused_element.upcast(), CanGc::from_cx(cx));
        event.flags().into()
    }

    fn handle_wheel_event(
        &self,
        cx: &mut JSContext,
        event: EmbedderWheelEvent,
        input_event: &ConstellationInputEvent,
    ) -> InputEventResult {
        // Ignore all incoming events without a hit test.
        let Some(hit_test_result) = self.window.hit_test_from_input_event(input_event) else {
            return Default::default();
        };

        let Some(el) = hit_test_result
            .node
            .inclusive_ancestors(ShadowIncluding::Yes)
            .find_map(DomRoot::downcast::<Element>)
        else {
            return Default::default();
        };

        let node = el.upcast::<Node>();
        debug!(
            "wheel: on {:?} at {:?}",
            node.debug_str(),
            hit_test_result.point_in_frame
        );

        // https://w3c.github.io/uievents/#event-wheelevents
        let dom_event = WheelEvent::new(
            &self.window,
            "wheel".into(),
            EventBubbles::Bubbles,
            EventCancelable::Cancelable,
            Some(&self.window),
            0i32,
            hit_test_result.point_in_frame.to_i32(),
            hit_test_result.point_in_frame.to_i32(),
            hit_test_result
                .point_relative_to_initial_containing_block
                .to_i32(),
            input_event.active_keyboard_modifiers,
            0i16,
            input_event.pressed_mouse_buttons,
            None,
            None,
            // winit defines positive wheel delta values as revealing more content left/up.
            // https://docs.rs/winit-gtk/latest/winit/event/enum.MouseScrollDelta.html
            // This is the opposite of wheel delta in uievents
            // https://w3c.github.io/uievents/#dom-wheeleventinit-deltaz
            Finite::wrap(-event.delta.x),
            Finite::wrap(-event.delta.y),
            Finite::wrap(-event.delta.z),
            event.delta.mode as u32,
            CanGc::from_cx(cx),
        );

        let dom_event = dom_event.upcast::<Event>();
        dom_event.set_trusted(true);
        dom_event.set_composed(true);
        dom_event.fire(node.upcast(), CanGc::from_cx(cx));

        dom_event.flags().into()
    }

    #[cfg(feature = "gamepad")]
    fn handle_gamepad_event(&self, gamepad_event: EmbedderGamepadEvent) {
        match gamepad_event {
            EmbedderGamepadEvent::Connected(index, name, bounds, supported_haptic_effects) => {
                self.handle_gamepad_connect(
                    index.0,
                    name,
                    bounds.axis_bounds,
                    bounds.button_bounds,
                    supported_haptic_effects,
                );
            },
            EmbedderGamepadEvent::Disconnected(index) => {
                self.handle_gamepad_disconnect(index.0);
            },
            EmbedderGamepadEvent::Updated(index, update_type) => {
                self.receive_new_gamepad_button_or_axis(index.0, update_type);
            },
        };
    }

    /// <https://www.w3.org/TR/gamepad/#dfn-gamepadconnected>
    #[cfg(feature = "gamepad")]
    fn handle_gamepad_connect(
        &self,
        // As the spec actually defines how to set the gamepad index, the GilRs index
        // is currently unused, though in practice it will almost always be the same.
        // More infra is currently needed to track gamepads across windows.
        _index: usize,
        name: String,
        axis_bounds: (f64, f64),
        button_bounds: (f64, f64),
        supported_haptic_effects: GamepadSupportedHapticEffects,
    ) {
        // TODO: 2. If document is not null and is not allowed to use the "gamepad" permission,
        //          then abort these steps.
        let trusted_window = Trusted::new(&*self.window);
        self.window
            .upcast::<GlobalScope>()
            .task_manager()
            .gamepad_task_source()
            .queue(task!(gamepad_connected: move |cx| {
                let window = trusted_window.root();

                let navigator = window.Navigator();
                let selected_index = navigator.select_gamepad_index();
                let gamepad = Gamepad::new(
                    &window,
                    selected_index,
                    name,
                    "standard".into(),
                    axis_bounds,
                    button_bounds,
                    supported_haptic_effects,
                    false,
                    CanGc::from_cx(cx),
                );
                navigator.set_gamepad(selected_index as usize, &gamepad, CanGc::from_cx(cx));
            }));
    }

    /// <https://www.w3.org/TR/gamepad/#dfn-gamepaddisconnected>
    #[cfg(feature = "gamepad")]
    fn handle_gamepad_disconnect(&self, index: usize) {
        let trusted_window = Trusted::new(&*self.window);
        self.window
            .upcast::<GlobalScope>()
            .task_manager()
            .gamepad_task_source()
            .queue(task!(gamepad_disconnected: move |cx| {
                let window = trusted_window.root();
                let navigator = window.Navigator();
                if let Some(gamepad) = navigator.get_gamepad(index) {
                    if window.Document().is_fully_active() {
                        gamepad.update_connected(false, gamepad.exposed(), CanGc::from_cx(cx));
                        navigator.remove_gamepad(index);
                    }
                }
            }));
    }

    /// <https://www.w3.org/TR/gamepad/#receiving-inputs>
    #[cfg(feature = "gamepad")]
    fn receive_new_gamepad_button_or_axis(&self, index: usize, update_type: GamepadUpdateType) {
        let trusted_window = Trusted::new(&*self.window);

        // <https://w3c.github.io/gamepad/#dfn-update-gamepad-state>
        self.window.upcast::<GlobalScope>().task_manager().gamepad_task_source().queue(
                task!(update_gamepad_state: move || {
                    let window = trusted_window.root();
                    let navigator = window.Navigator();
                    if let Some(gamepad) = navigator.get_gamepad(index) {
                        let current_time = window.Performance().Now();
                        gamepad.update_timestamp(*current_time);
                        match update_type {
                            GamepadUpdateType::Axis(index, value) => {
                                gamepad.map_and_normalize_axes(index, value);
                            },
                            GamepadUpdateType::Button(index, value) => {
                                gamepad.map_and_normalize_buttons(index, value);
                            }
                        };
                        if !navigator.has_gamepad_gesture() && contains_user_gesture(update_type) {
                            navigator.set_has_gamepad_gesture(true);
                            navigator.GetGamepads()
                                .iter()
                                .filter_map(|g| g.as_ref())
                                .for_each(|gamepad| {
                                    gamepad.set_exposed(true);
                                    gamepad.update_timestamp(*current_time);
                                    let new_gamepad = Trusted::new(&**gamepad);
                                    if window.Document().is_fully_active() {
                                        window.upcast::<GlobalScope>().task_manager().gamepad_task_source().queue(
                                            task!(update_gamepad_connect: move |cx| {
                                                let gamepad = new_gamepad.root();
                                                gamepad.notify_event(GamepadEventType::Connected, CanGc::from_cx(cx));
                                            })
                                        );
                                    }
                                });
                        }
                    }
                })
            );
    }

    /// <https://www.w3.org/TR/clipboard-apis/#clipboard-actions>
    pub(crate) fn handle_editing_action(
        &self,
        cx: &mut JSContext,
        element: Option<DomRoot<Element>>,
        action: EditingActionEvent,
    ) -> InputEventResult {
        let clipboard_event_type = match action {
            EditingActionEvent::Copy => ClipboardEventType::Copy,
            EditingActionEvent::Cut => ClipboardEventType::Cut,
            EditingActionEvent::Paste => ClipboardEventType::Paste,
        };

        // The script_triggered flag is set if the action runs because of a script, e.g. document.execCommand()
        let script_triggered = false;

        // The script_may_access_clipboard flag is set
        // if action is paste and the script thread is allowed to read from clipboard or
        // if action is copy or cut and the script thread is allowed to modify the clipboard
        let script_may_access_clipboard = false;

        // Step 1 If the script-triggered flag is set and the script-may-access-clipboard flag is unset
        if script_triggered && !script_may_access_clipboard {
            return InputEventResult::empty();
        }

        // Step 2 Fire a clipboard event
        let clipboard_event =
            self.fire_clipboard_event(element.clone(), clipboard_event_type, CanGc::from_cx(cx));

        // Step 3 If a script doesn't call preventDefault()
        // the event will be handled inside target's VirtualMethods::handle_event
        let event = clipboard_event.upcast::<Event>();
        if !event.IsTrusted() {
            return event.flags().into();
        }

        // Step 4 If the event was canceled, then
        if event.DefaultPrevented() {
            let event_type = event.Type();
            match_domstring_ascii!(event_type,

                "copy" => {
                    // Step 4.1 Call the write content to the clipboard algorithm,
                    // passing on the DataTransferItemList items, a clear-was-called flag and a types-to-clear list.
                    if let Some(clipboard_data) = clipboard_event.get_clipboard_data() {
                        let drag_data_store =
                            clipboard_data.data_store().expect("This shouldn't fail");
                        self.write_content_to_the_clipboard(&drag_data_store);
                    }
                },
                "cut" => {
                    // Step 4.1 Call the write content to the clipboard algorithm,
                    // passing on the DataTransferItemList items, a clear-was-called flag and a types-to-clear list.
                    if let Some(clipboard_data) = clipboard_event.get_clipboard_data() {
                        let drag_data_store =
                            clipboard_data.data_store().expect("This shouldn't fail");
                        self.write_content_to_the_clipboard(&drag_data_store);
                    }

                    // Step 4.2 Fire a clipboard event named clipboardchange
                    self.fire_clipboard_event(element, ClipboardEventType::Change, CanGc::from_cx(cx));
                },
                // Step 4.1 Return false.
                // Note: This function deviates from the specification a bit by returning
                // the `InputEventResult` below.
                "paste" => (),
                _ => (),
            )
        }

        // Step 5: Return true from the action.
        // In this case we are returning the `InputEventResult` instead of true or false.
        event.flags().into()
    }

    /// <https://www.w3.org/TR/clipboard-apis/#fire-a-clipboard-event>
    pub(crate) fn fire_clipboard_event(
        &self,
        target: Option<DomRoot<Element>>,
        clipboard_event_type: ClipboardEventType,
        can_gc: CanGc,
    ) -> DomRoot<ClipboardEvent> {
        let clipboard_event = ClipboardEvent::new(
            &self.window,
            None,
            clipboard_event_type.as_str().into(),
            EventBubbles::Bubbles,
            EventCancelable::Cancelable,
            None,
            can_gc,
        );

        // Step 1 Let clear_was_called be false
        // Step 2 Let types_to_clear an empty list
        let mut drag_data_store = DragDataStore::new();

        // Step 4 let clipboard-entry be the sequence number of clipboard content, null if the OS doesn't support it.

        // Step 5 let trusted be true if the event is generated by the user agent, false otherwise
        let trusted = true;

        // Step 6 if the context is editable:
        let target = target
            .map(DomRoot::upcast)
            .unwrap_or_else(|| self.target_for_events_following_focus());

        // Step 6.2 else TODO require Selection see https://github.com/w3c/clipboard-apis/issues/70
        // Step 7
        match clipboard_event_type {
            ClipboardEventType::Copy | ClipboardEventType::Cut => {
                // Step 7.2.1
                drag_data_store.set_mode(Mode::ReadWrite);
            },
            ClipboardEventType::Paste => {
                let (callback, receiver) =
                    GenericCallback::new_blocking().expect("Could not create callback");
                self.window.send_to_embedder(EmbedderMsg::GetClipboardText(
                    self.window.webview_id(),
                    callback,
                ));
                let text_contents = receiver
                    .recv()
                    .map(Result::unwrap_or_default)
                    .unwrap_or_default();

                // Step 7.1.1
                drag_data_store.set_mode(Mode::ReadOnly);
                // Step 7.1.2 If trusted or the implementation gives script-generated events access to the clipboard
                if trusted {
                    // Step 7.1.2.1 For each clipboard-part on the OS clipboard:

                    // Step 7.1.2.1.1 If clipboard-part contains plain text, then
                    let data = DOMString::from(text_contents);
                    let type_ = DOMString::from("text/plain");
                    let _ = drag_data_store.add(Kind::Text { data, type_ });

                    // Step 7.1.2.1.2 TODO If clipboard-part represents file references, then for each file reference
                    // Step 7.1.2.1.3 TODO If clipboard-part contains HTML- or XHTML-formatted text then

                    // Step 7.1.3 Update clipboard-event-data’s files to match clipboard-event-data’s items
                    // Step 7.1.4 Update clipboard-event-data’s types to match clipboard-event-data’s items
                }
            },
            ClipboardEventType::Change => (),
        }

        // Step 3
        let clipboard_event_data = DataTransfer::new(
            &self.window,
            Rc::new(RefCell::new(Some(drag_data_store))),
            can_gc,
        );

        // Step 8
        clipboard_event.set_clipboard_data(Some(&clipboard_event_data));

        // Step 9
        let event = clipboard_event.upcast::<Event>();
        event.set_trusted(trusted);

        // Step 10 Set event’s composed to true.
        event.set_composed(true);

        // Step 11
        event.dispatch(&target, false, can_gc);

        DomRoot::from(clipboard_event)
    }

    /// <https://www.w3.org/TR/clipboard-apis/#write-content-to-the-clipboard>
    fn write_content_to_the_clipboard(&self, drag_data_store: &DragDataStore) {
        // Step 1
        if drag_data_store.list_len() > 0 {
            // Step 1.1 Clear the clipboard.
            self.window
                .send_to_embedder(EmbedderMsg::ClearClipboard(self.window.webview_id()));
            // Step 1.2
            for item in drag_data_store.iter_item_list() {
                match item {
                    Kind::Text { data, .. } => {
                        // Step 1.2.1.1 Ensure encoding is correct per OS and locale conventions
                        // Step 1.2.1.2 Normalize line endings according to platform conventions
                        // Step 1.2.1.3
                        self.window.send_to_embedder(EmbedderMsg::SetClipboardText(
                            self.window.webview_id(),
                            data.to_string(),
                        ));
                    },
                    Kind::File { .. } => {
                        // Step 1.2.2 If data is of a type listed in the mandatory data types list, then
                        // Step 1.2.2.1 Place part on clipboard with the appropriate OS clipboard format description
                        // Step 1.2.3 Else this is left to the implementation
                    },
                }
            }
        } else {
            // Step 2.1
            if drag_data_store.clear_was_called {
                // Step 2.1.1 If types-to-clear list is empty, clear the clipboard
                self.window
                    .send_to_embedder(EmbedderMsg::ClearClipboard(self.window.webview_id()));
                // Step 2.1.2 Else remove the types in the list from the clipboard
                // As of now this can't be done with Arboard, and it's possible that will be removed from the spec
            }
        }
    }

    /// Handle a scroll event triggered by user interactions from the embedder.
    /// <https://drafts.csswg.org/cssom-view/#scrolling-events>
    #[expect(unsafe_code)]
    pub(crate) fn handle_embedder_scroll_event(&self, scrolled_node: ExternalScrollId) {
        // If it is a viewport scroll.
        let document = self.window.Document();
        if scrolled_node.is_root() {
            document.handle_viewport_scroll_event();
        } else {
            // Otherwise, check whether it is for a relevant element within the document. For a `::before` or `::after`
            // pseudo element we follow Gecko or Chromium's behavior to ensure that the event reaches the originating
            // node.
            let node_id = node_id_from_scroll_id(scrolled_node.0 as usize);
            let node = unsafe {
                node::from_untrusted_node_address(UntrustedNodeAddress::from_id(node_id))
            };
            let Some(element) = node
                .inclusive_ancestors(ShadowIncluding::Yes)
                .find_map(DomRoot::downcast::<Element>)
            else {
                return;
            };

            element.handle_scroll_event();
        }
    }

    /// <https://w3c.github.io/uievents/#keydown>
    ///
    /// > If the key is the Enter or (Space) key and the current focus is on a state-changing element,
    /// > the default action MUST be to dispatch a click event, and a DOMActivate event if that event
    /// > type is supported by the user agent.
    pub(crate) fn maybe_dispatch_simulated_click(
        &self,
        node: &Node,
        event: &KeyboardEvent,
        can_gc: CanGc,
    ) -> bool {
        if event.key() != Key::Named(NamedKey::Enter) && event.original_code() != Some(Code::Space)
        {
            return false;
        }

        // Check whether this node is a state-changing element. Note that the specification doesn't
        // seem to have a good definition of what "state-changing" means, so we merely check to
        // see if the element is activatable here.
        if node
            .downcast::<Element>()
            .and_then(Element::as_maybe_activatable)
            .is_none()
        {
            return false;
        }

        node.fire_synthetic_pointer_event_not_trusted(atom!("click"), can_gc);
        true
    }

    pub(crate) fn run_default_keyboard_event_handler(
        &self,
        cx: &mut js::context::JSContext,
        node: &Node,
        event: &KeyboardEvent,
    ) {
        if event.upcast::<Event>().type_() != atom!("keydown") {
            return;
        }

        if self.maybe_dispatch_simulated_click(node, event, CanGc::from_cx(cx)) {
            return;
        }

        if self.maybe_handle_accesskey(cx, event) {
            return;
        }

        let mut is_space = false;
        let scroll = match event.key() {
            Key::Named(NamedKey::ArrowDown) => KeyboardScroll::Down,
            Key::Named(NamedKey::ArrowLeft) => KeyboardScroll::Left,
            Key::Named(NamedKey::ArrowRight) => KeyboardScroll::Right,
            Key::Named(NamedKey::ArrowUp) => KeyboardScroll::Up,
            Key::Named(NamedKey::End) => KeyboardScroll::End,
            Key::Named(NamedKey::Home) => KeyboardScroll::Home,
            Key::Named(NamedKey::PageDown) => KeyboardScroll::PageDown,
            Key::Named(NamedKey::PageUp) => KeyboardScroll::PageUp,
            Key::Character(string) if &string == " " => {
                is_space = true;
                if event.modifiers().contains(Modifiers::SHIFT) {
                    KeyboardScroll::PageUp
                } else {
                    KeyboardScroll::PageDown
                }
            },
            Key::Named(NamedKey::Tab) => {
                // From <https://w3c.github.io/uievents/#keydown>:
                //
                // > If the key is the Tab key, the default action MUST be to shift the document focus
                // > from the currently focused element (if any) to the new focused element, as
                // > described in Focus Event Types
                self.sequential_focus_navigation_via_keyboard_event(cx, event);
                return;
            },
            _ => return,
        };

        if !event.modifiers().is_empty() && !is_space {
            return;
        }

        self.do_keyboard_scroll(scroll);
    }

    pub(crate) fn set_sequential_focus_navigation_starting_point(&self, node: &Node) {
        self.sequential_focus_navigation_starting_point
            .set(Some(node));
    }

    pub(crate) fn sequential_focus_navigation_starting_point(&self) -> Option<DomRoot<Node>> {
        self.sequential_focus_navigation_starting_point
            .get()
            .filter(|node| node.is_connected())
    }

    fn sequential_focus_navigation_via_keyboard_event(
        &self,
        cx: &mut JSContext,
        event: &KeyboardEvent,
    ) {
        let direction = if event.modifiers().contains(Modifiers::SHIFT) {
            SequentialFocusDirection::Backward
        } else {
            SequentialFocusDirection::Forward
        };

        self.sequential_focus_navigation(cx, direction);
    }

    /// <https://html.spec.whatwg.org/multipage/#sequential-focus-navigation:currently-focused-area-of-a-top-level-traversable>
    fn sequential_focus_navigation(&self, cx: &mut JSContext, direction: SequentialFocusDirection) {
        // > When the user requests that focus move from the currently focused area of a top-level
        // > traversable to the next or previous focusable area (e.g., as the default action of
        // > pressing the tab key), or when the user requests that focus sequentially move to a
        // > top-level traversable in the first place (e.g., from the browser's location bar), the
        // > user agent must use the following algorithm:

        // > 1. Let starting point be the currently focused area of a top-level traversable, if the
        // > user requested to move focus sequentially from there, or else the top-level traversable
        // > itself, if the user instead requested to move focus from outside the top-level
        // > traversable.
        //
        // Note: Here `None` represents the current traversible.
        let mut starting_point = self
            .window
            .Document()
            .focus_handler()
            .focused_area()
            .element()
            .map(|element| DomRoot::from_ref(element.upcast::<Node>()));

        // > 2. If there is a sequential focus navigation starting point defined and it is inside
        // > starting point, then let starting point be the sequential focus navigation starting point
        // > instead.
        if let Some(sequential_focus_navigation_starting_point) =
            self.sequential_focus_navigation_starting_point()
        {
            if starting_point.as_ref().is_none_or(|starting_point| {
                starting_point.is_ancestor_of(&sequential_focus_navigation_starting_point)
            }) {
                starting_point = Some(sequential_focus_navigation_starting_point);
            }
        }

        // > 3. Let direction be "forward" if the user requested the next control, and "backward" if
        // > the user requested the previous control.
        //
        // Note: This is handled by the `direction` argument to this method.
        self.sequential_focus_navigation_loop(
            cx,
            starting_point,
            direction,
            false, /* allow_focusing_viewport */
        );
    }

    /// The inner loop ("Loop") of:
    /// <https://html.spec.whatwg.org/multipage/#sequential-focus-navigation:currently-focused-area-of-a-top-level-traversable>
    pub(crate) fn sequential_focus_navigation_loop(
        &self,
        cx: &mut JSContext,
        starting_point: Option<DomRoot<Node>>,
        direction: SequentialFocusDirection,
        allow_focusing_viewport: bool,
    ) {
        // > 4. Loop: Let selection mechanism be "sequential" if starting point is a navigable or if
        // > starting point is in its Document's sequential focus navigation order.
        // > Otherwise, starting point is not in its Document's sequential focus navigation order;
        // > let selection mechanism be "DOM".
        let starting_point_is_navigable = starting_point
            .as_ref()
            .is_none_or(|starting_point| starting_point.is::<HTMLIFrameElement>());
        let selection_mechanism = starting_point
            .as_ref()
            .and_then(|node| node.downcast::<Element>())
            .filter(|element| element.is_sequentially_focusable())
            .map(|element| {
                SequentialFocusNavigationMechanism::Sequential(
                    element.explicitly_set_tab_index().unwrap_or_default(),
                )
            })
            .unwrap_or_else(|| {
                if starting_point_is_navigable {
                    SequentialFocusNavigationMechanism::Navigable
                } else {
                    SequentialFocusNavigationMechanism::Dom
                }
            });

        // > 5. Let candidate be the result of running the sequential navigation search algorithm
        // > with starting point, direction, and selection mechanism.
        let candidate = SequentialFocusNavigationSearch::new(
            self.window.as_rooted(),
            direction,
            selection_mechanism,
            starting_point,
        )
        .search();

        // > 6. If candidate is not null, then run the focusing steps for candidate and return.
        if let Some(candidate) = candidate {
            self.focus_and_scroll_to_element_for_key_event(cx, &candidate);
            // We can't simply run the focusing steps, because:
            //  1. The focusing steps do not scroll to the element.
            //  2. When focus shifts to a child navigable (iframe) we have special behavior to reach
            //     across document boundaries to focus the first focusable element in the iframe.
            match candidate.downcast::<HTMLIFrameElement>() {
                Some(iframe_element) => self
                    .window
                    .Document()
                    .focus_handler()
                    .sequentially_focus_child_iframe_local_or_remote(cx, iframe_element, direction),
                None => self.focus_and_scroll_to_element_for_key_event(cx, &candidate),
            }
            return;
        }

        // > 7. Otherwise, unset the sequential focus navigation starting point.
        self.sequential_focus_navigation_starting_point.clear();

        // This is not in the specification, but there's a difference between moving focus into
        // a child `<iframe>` and within a Document. If no suitable focusable area can be found
        // when moving into an `<iframe>`, we want to focus the `<iframe>`'s viewport itself.
        if allow_focusing_viewport {
            self.window
                .Document()
                .focus_handler()
                .focus(cx, FocusableArea::Viewport);
            return;
        }

        // > 8. If starting point is a top-level traversable, or a focusable area in the top-level
        // > traversable, the user agent should transfer focus to its own controls appropriately (if
        // > any), honouring direction, and then return.
        // TODO: Implement this.
        if self.window.is_top_level() {
            return;
        }

        // > 9. Otherwise, starting point is a focusable area in a child navigable. Set starting
        // > point to that child navigable's parent and return to the step labeled loop.
        self.window
            .Document()
            .focus_handler()
            .sequentially_focus_parent_local_or_remote(cx, direction);
    }

    pub(crate) fn do_keyboard_scroll(&self, scroll: KeyboardScroll) {
        let scroll_axis = match scroll {
            KeyboardScroll::Left | KeyboardScroll::Right => ScrollingBoxAxis::X,
            _ => ScrollingBoxAxis::Y,
        };

        let document = self.window.Document();
        let mut scrolling_box = document
            .focus_handler()
            .focused_area()
            .element()
            .or(self.most_recently_clicked_element.get().as_deref())
            .and_then(|element| element.scrolling_box(ScrollContainerQueryFlags::Inclusive))
            .unwrap_or_else(|| {
                document.viewport_scrolling_box(ScrollContainerQueryFlags::Inclusive)
            });

        while !scrolling_box.can_keyboard_scroll_in_axis(scroll_axis) {
            // Always fall back to trying to scroll the entire document.
            if scrolling_box.is_viewport() {
                break;
            }
            let parent = scrolling_box.parent().unwrap_or_else(|| {
                document.viewport_scrolling_box(ScrollContainerQueryFlags::Inclusive)
            });
            scrolling_box = parent;
        }

        let calculate_current_scroll_offset_and_delta = || {
            const LINE_HEIGHT: f32 = 76.0;
            const LINE_WIDTH: f32 = 76.0;

            let current_scroll_offset = scrolling_box.scroll_position();
            (
                current_scroll_offset,
                match scroll {
                    KeyboardScroll::Home => Vector2D::new(0.0, -current_scroll_offset.y),
                    KeyboardScroll::End => Vector2D::new(
                        0.0,
                        -current_scroll_offset.y + scrolling_box.content_size().height -
                            scrolling_box.size().height,
                    ),
                    KeyboardScroll::PageDown => {
                        Vector2D::new(0.0, scrolling_box.size().height - 2.0 * LINE_HEIGHT)
                    },
                    KeyboardScroll::PageUp => {
                        Vector2D::new(0.0, 2.0 * LINE_HEIGHT - scrolling_box.size().height)
                    },
                    KeyboardScroll::Up => Vector2D::new(0.0, -LINE_HEIGHT),
                    KeyboardScroll::Down => Vector2D::new(0.0, LINE_HEIGHT),
                    KeyboardScroll::Left => Vector2D::new(-LINE_WIDTH, 0.0),
                    KeyboardScroll::Right => Vector2D::new(LINE_WIDTH, 0.0),
                },
            )
        };

        // If trying to scroll the viewport of this `Window` and this is the root `Document`
        // of the `WebView`, then send the srolling operation to the renderer, so that it
        // can properly pan any pinch zoom viewport.
        let parent_pipeline = self.window.parent_info();
        if scrolling_box.is_viewport() && parent_pipeline.is_none() {
            let (_, delta) = calculate_current_scroll_offset_and_delta();
            self.window
                .paint_api()
                .scroll_viewport_by_delta(self.window.webview_id(), delta);
        }

        // If this is the viewport and we cannot scroll, try to ask a parent viewport to scroll,
        // if we are inside an `<iframe>`.
        if !scrolling_box.can_keyboard_scroll_in_axis(scroll_axis) {
            assert!(scrolling_box.is_viewport());

            let window_proxy = document.window().window_proxy();
            if let Some(iframe) = window_proxy.frame_element() {
                // When the `<iframe>` is local (in this ScriptThread), we can
                // synchronously chain up the keyboard scrolling event.
                let cx = GlobalScope::get_cx();
                let iframe_window = iframe.owner_window();
                let _ac = JSAutoRealm::new(*cx, iframe_window.reflector().get_jsobject().get());
                iframe_window
                    .Document()
                    .event_handler()
                    .do_keyboard_scroll(scroll);
            } else if let Some(parent_pipeline) = parent_pipeline {
                // Otherwise, if we have a parent (presumably from a different origin)
                // asynchronously ask the Constellation to forward the event to the parent
                // pipeline, if we have one.
                document.window().send_to_constellation(
                    ScriptToConstellationMessage::ForwardKeyboardScroll(parent_pipeline, scroll),
                );
            };
            return;
        }

        let (current_scroll_offset, delta) = calculate_current_scroll_offset_and_delta();
        scrolling_box.scroll_to(delta + current_scroll_offset, ScrollBehavior::Auto);
    }

    /// Get or create a pointer ID for the given touch identifier.
    /// Returns the pointer ID to use for this touch.
    fn get_or_create_pointer_id_for_touch(&self, touch_id: i32) -> i32 {
        let mut active_pointer_ids = self.active_pointer_ids.borrow_mut();

        if let Some(&pointer_id) = active_pointer_ids.get(&touch_id) {
            return pointer_id;
        }

        let pointer_id = self.next_touch_pointer_id.get();
        active_pointer_ids.insert(touch_id, pointer_id);
        self.next_touch_pointer_id.set(pointer_id + 1);
        pointer_id
    }

    /// Remove the pointer ID mapping for the given touch identifier.
    fn remove_pointer_id_for_touch(&self, touch_id: i32) {
        self.active_pointer_ids.borrow_mut().remove(&touch_id);
    }

    /// Check if this is the primary pointer (for touch events).
    /// The first touch to make contact is the primary pointer.
    fn is_primary_pointer(&self, pointer_id: i32) -> bool {
        // For touch, the primary pointer is the one with the smallest pointer ID
        // that is still active.
        self.active_pointer_ids
            .borrow()
            .values()
            .min()
            .is_some_and(|primary_pointer| *primary_pointer == pointer_id)
    }

    /// Fire pointerenter events hierarchically from topmost ancestor to target element.
    /// Fire pointerleave events hierarchically from target element to topmost ancestor.
    /// Used for touch devices that don't support hover.
    #[allow(clippy::too_many_arguments)]
    fn fire_pointer_event_for_touch(
        &self,
        target_element: &Element,
        touch: &Touch,
        pointer_id: i32,
        event_name: &str,
        is_primary: bool,
        input_event: &ConstellationInputEvent,
        hit_test_result: &HitTestResult,
        can_gc: CanGc,
    ) {
        // Collect ancestors from target to root
        let mut targets: Vec<DomRoot<Node>> = vec![];
        let mut current: Option<DomRoot<Node>> = Some(DomRoot::from_ref(target_element.upcast()));
        while let Some(node) = current {
            targets.push(DomRoot::from_ref(&*node));
            current = node.parent_in_flat_tree();
        }

        // Reverse to dispatch from topmost ancestor to target
        if event_name == "pointerenter" {
            targets.reverse();
        }

        for target in targets {
            let pointer_event = touch.to_pointer_event(
                &self.window,
                event_name,
                pointer_id,
                is_primary,
                input_event.active_keyboard_modifiers,
                false,
                Some(hit_test_result.point_in_node),
                can_gc,
            );
            pointer_event
                .upcast::<Event>()
                .fire(target.upcast(), can_gc);
        }
    }

    pub(crate) fn has_assigned_access_key(&self, element: &HTMLElement) -> bool {
        self.access_key_handlers
            .borrow()
            .values()
            .any(|value| &**value == element)
    }

    pub(crate) fn unassign_access_key(&self, element: &HTMLElement) {
        self.access_key_handlers
            .borrow_mut()
            .retain(|_, value| &**value != element)
    }

    pub(crate) fn assign_access_key(&self, element: &HTMLElement, code: Code) {
        let mut access_key_handlers = self.access_key_handlers.borrow_mut();
        // If an element is already assigned this access key, ignore the request.
        access_key_handlers
            .entry(code.into())
            .or_insert(Dom::from_ref(element));
    }

    fn maybe_handle_accesskey(
        &self,
        cx: &mut js::context::JSContext,
        event: &KeyboardEvent,
    ) -> bool {
        #[cfg(target_os = "macos")]
        let access_key_modifiers = Modifiers::CONTROL | Modifiers::ALT;
        #[cfg(not(target_os = "macos"))]
        let access_key_modifiers = Modifiers::SHIFT | Modifiers::ALT;

        if event.modifiers() != access_key_modifiers {
            return false;
        }

        let Ok(code) = Code::from_str(&event.Code().str()) else {
            return false;
        };

        let Some(html_element) = self
            .access_key_handlers
            .borrow()
            .get(&code.into())
            .map(|html_element| html_element.as_rooted())
        else {
            return false;
        };

        // From <https://html.spec.whatwg.org/multipage/#the-accesskey-attribute>:
        // > When the user presses the key combination corresponding to the assigned access key for
        // > an element, if the element defines a command, the command's Hidden State facet is false
        // > (visible), the command's Disabled State facet is also false (enabled), the element is in
        // > a document that has a non-null browsing context, and neither the element nor any of its
        // > ancestors has a hidden attribute specified, then the user agent must trigger the Action
        // > of the command.
        let Ok(command) = InteractiveElementCommand::try_from(&*html_element) else {
            return false;
        };

        if command.disabled() || command.hidden() {
            return false;
        }

        let node = html_element.upcast::<Node>();
        if !node.is_connected() {
            return false;
        }

        for node in node.inclusive_ancestors(ShadowIncluding::Yes) {
            if node
                .downcast::<HTMLElement>()
                .is_some_and(|html_element| html_element.Hidden())
            {
                return false;
            }
        }

        // This behavior is unspecified, but all browsers do this. When activating the element it is
        // focused and scrolled into view.
        self.focus_and_scroll_to_element_for_key_event(cx, html_element.upcast());
        command.perform_action(cx);
        true
    }

    fn focus_and_scroll_to_element_for_key_event(&self, cx: &mut JSContext, element: &Element) {
        element.upcast::<Node>().run_the_focusing_steps(cx, None);
        let scroll_axis = ScrollAxisState {
            position: ScrollLogicalPosition::Center,
            requirement: ScrollRequirement::IfNotVisible,
        };
        element.scroll_into_view_with_options(
            ScrollBehavior::Auto,
            scroll_axis,
            scroll_axis,
            None,
            None,
        );
    }
}

fn compare_tab_indices(a: i32, b: i32) -> Ordering {
    if a == b {
        Ordering::Equal
    } else if a == 0 {
        Ordering::Greater
    } else if b == 0 {
        Ordering::Less
    } else {
        a.cmp(&b)
    }
}

pub(crate) fn character_to_code(character: char) -> Option<Code> {
    Some(match character.to_ascii_lowercase() {
        '`' => Code::Backquote,
        '\\' => Code::Backslash,
        '[' | '{' => Code::BracketLeft,
        ']' | '}' => Code::BracketRight,
        ',' | '<' => Code::Comma,
        '0' => Code::Digit0,
        '1' => Code::Digit1,
        '2' => Code::Digit2,
        '3' => Code::Digit3,
        '4' => Code::Digit4,
        '5' => Code::Digit5,
        '6' => Code::Digit6,
        '7' => Code::Digit7,
        '8' => Code::Digit8,
        '9' => Code::Digit9,
        '=' => Code::Equal,
        'a' => Code::KeyA,
        'b' => Code::KeyB,
        'c' => Code::KeyC,
        'd' => Code::KeyD,
        'e' => Code::KeyE,
        'f' => Code::KeyF,
        'g' => Code::KeyG,
        'h' => Code::KeyH,
        'i' => Code::KeyI,
        'j' => Code::KeyJ,
        'k' => Code::KeyK,
        'l' => Code::KeyL,
        'm' => Code::KeyM,
        'n' => Code::KeyN,
        'o' => Code::KeyO,
        'p' => Code::KeyP,
        'q' => Code::KeyQ,
        'r' => Code::KeyR,
        's' => Code::KeyS,
        't' => Code::KeyT,
        'u' => Code::KeyU,
        'v' => Code::KeyV,
        'w' => Code::KeyW,
        'x' => Code::KeyX,
        'y' => Code::KeyY,
        'z' => Code::KeyZ,
        '-' => Code::Minus,
        '.' => Code::Period,
        '\'' | '"' => Code::Quote,
        ';' => Code::Semicolon,
        '/' => Code::Slash,
        ' ' => Code::Space,
        _ => return None,
    })
}