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
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
/* 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::cell::Cell;
use std::default::Default;
use std::rc::Rc;
use std::sync::{Arc, LazyLock};
use std::{char, mem};
use app_units::Au;
use cssparser::{Parser, ParserInput};
use dom_struct::dom_struct;
use euclid::default::Point2D;
use html5ever::{LocalName, Prefix, QualName, local_name, ns};
use js::context::JSContext;
use js::realm::AutoRealm;
use js::rust::HandleObject;
use mime::{self, Mime};
use net_traits::http_status::HttpStatus;
use net_traits::image_cache::{
Image, ImageCache, ImageCacheResult, ImageLoadListener, ImageOrMetadataAvailable,
ImageResponse, PendingImageId,
};
use net_traits::request::{CorsSettings, Destination, Initiator, RequestId};
use net_traits::{
FetchMetadata, FetchResponseMsg, NetworkError, ReferrerPolicy, ResourceFetchTiming,
};
use num_traits::ToPrimitive;
use pixels::{CorsStatus, ImageMetadata, Snapshot};
use regex::Regex;
use rustc_hash::FxHashSet;
use servo_url::ServoUrl;
use servo_url::origin::MutableOrigin;
use style::attr::{AttrValue, LengthOrPercentageOrAuto, parse_unsigned_integer};
use style::stylesheets::CssRuleType;
use style::values::specified::source_size_list::SourceSizeList;
use style_traits::ParsingMode;
use url::Url;
use crate::css::parser_context_for_anonymous_content;
use crate::document_loader::{LoadBlocker, LoadType};
use crate::dom::activation::Activatable;
use crate::dom::attr::Attr;
use crate::dom::bindings::cell::{DomRefCell, RefMut};
use crate::dom::bindings::codegen::Bindings::DOMRectBinding::DOMRect_Binding::DOMRectMethods;
use crate::dom::bindings::codegen::Bindings::ElementBinding::Element_Binding::ElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLImageElementBinding::HTMLImageElementMethods;
use crate::dom::bindings::codegen::Bindings::MouseEventBinding::MouseEventMethods;
use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::DomGlobal;
use crate::dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom};
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::csp::{GlobalCspReporting, Violation};
use crate::dom::document::Document;
use crate::dom::element::{
AttributeMutation, CustomElementCreationMode, Element, ElementCreator,
cors_setting_for_element, referrer_policy_for_element, reflect_cross_origin_attribute,
reflect_referrer_policy_attribute, set_cross_origin_attribute,
};
use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::html::htmlareaelement::HTMLAreaElement;
use crate::dom::html::htmlelement::HTMLElement;
use crate::dom::html::htmlformelement::{FormControl, HTMLFormElement};
use crate::dom::html::htmlmapelement::HTMLMapElement;
use crate::dom::html::htmlpictureelement::HTMLPictureElement;
use crate::dom::html::htmlsourceelement::HTMLSourceElement;
use crate::dom::medialist::MediaList;
use crate::dom::mouseevent::MouseEvent;
use crate::dom::node::{
BindContext, MoveContext, Node, NodeDamage, NodeTraits, ShadowIncluding, UnbindContext,
};
use crate::dom::performance::performanceresourcetiming::InitiatorType;
use crate::dom::promise::Promise;
use crate::dom::virtualmethods::VirtualMethods;
use crate::dom::window::Window;
use crate::fetch::{RequestWithGlobalScope, create_a_potential_cors_request};
use crate::microtask::{Microtask, MicrotaskRunnable};
use crate::network_listener::{self, FetchResponseListener, ResourceTimingListener};
use crate::realms::enter_auto_realm;
use crate::script_runtime::CanGc;
use crate::script_thread::ScriptThread;
/// Supported image MIME types as defined by
/// <https://mimesniff.spec.whatwg.org/#image-mime-type>.
/// Keep this in sync with 'detect_image_format' from components/pixels/lib.rs
const SUPPORTED_IMAGE_MIME_TYPES: &[&str] = &[
"image/bmp",
"image/gif",
"image/jpeg",
"image/jpg",
"image/pjpeg",
"image/png",
"image/apng",
"image/x-png",
"image/svg+xml",
"image/vnd.microsoft.icon",
"image/x-icon",
"image/webp",
];
#[derive(Clone, Copy, Debug)]
enum ParseState {
InDescriptor,
InParens,
AfterDescriptor,
}
/// <https://html.spec.whatwg.org/multipage/#source-set>
#[derive(MallocSizeOf)]
pub(crate) struct SourceSet {
image_sources: Vec<ImageSource>,
source_size: SourceSizeList,
}
impl SourceSet {
fn new() -> SourceSet {
SourceSet {
image_sources: Vec::new(),
source_size: SourceSizeList::empty(),
}
}
}
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
pub struct ImageSource {
pub url: String,
pub descriptor: Descriptor,
}
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
pub struct Descriptor {
pub width: Option<u32>,
pub density: Option<f64>,
}
/// <https://html.spec.whatwg.org/multipage/#img-req-state>
#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
enum State {
Unavailable,
PartiallyAvailable,
CompletelyAvailable,
Broken,
}
#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
enum ImageRequestPhase {
Pending,
Current,
}
/// <https://html.spec.whatwg.org/multipage/#image-request>
#[derive(JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
struct ImageRequest {
state: State,
#[no_trace]
parsed_url: Option<ServoUrl>,
source_url: Option<USVString>,
blocker: DomRefCell<Option<LoadBlocker>>,
#[no_trace]
image: Option<Image>,
#[no_trace]
metadata: Option<ImageMetadata>,
#[no_trace]
final_url: Option<ServoUrl>,
current_pixel_density: Option<f64>,
}
#[dom_struct]
pub(crate) struct HTMLImageElement {
htmlelement: HTMLElement,
image_request: Cell<ImageRequestPhase>,
current_request: DomRefCell<ImageRequest>,
pending_request: DomRefCell<ImageRequest>,
form_owner: MutNullableDom<HTMLFormElement>,
generation: Cell<u32>,
source_set: DomRefCell<SourceSet>,
/// <https://html.spec.whatwg.org/multipage/#concept-img-dimension-attribute-source>
/// Always non-null after construction.
dimension_attribute_source: MutNullableDom<Element>,
/// <https://html.spec.whatwg.org/multipage/#last-selected-source>
last_selected_source: DomRefCell<Option<USVString>>,
#[conditional_malloc_size_of]
image_decode_promises: DomRefCell<Vec<Rc<Promise>>>,
/// Line number this element was created on
line_number: u64,
}
impl HTMLImageElement {
// https://html.spec.whatwg.org/multipage/#check-the-usability-of-the-image-argument
pub(crate) fn is_usable(&self) -> Fallible<bool> {
// If image has an intrinsic width or intrinsic height (or both) equal to zero, then return bad.
if let Some(image) = &self.current_request.borrow().image {
let intrinsic_size = image.metadata();
if intrinsic_size.width == 0 || intrinsic_size.height == 0 {
return Ok(false);
}
}
match self.current_request.borrow().state {
// If image's current request's state is broken, then throw an "InvalidStateError" DOMException.
State::Broken => Err(Error::InvalidState(None)),
State::CompletelyAvailable => Ok(true),
// If image is not fully decodable, then return bad.
State::PartiallyAvailable | State::Unavailable => Ok(false),
}
}
pub(crate) fn image_data(&self) -> Option<Image> {
self.current_request.borrow().image.clone()
}
/// Gets the copy of the raster image data.
pub(crate) fn get_raster_image_data(&self) -> Option<Snapshot> {
let Some(raster_image) = self.image_data()?.as_raster_image() else {
warn!("Vector image is not supported as raster image source");
return None;
};
Some(raster_image.as_snapshot())
}
}
/// The context required for asynchronously loading an external image.
struct ImageContext {
/// Reference to the script thread image cache.
image_cache: Arc<dyn ImageCache>,
/// Indicates whether the request failed, and why
status: Result<(), NetworkError>,
/// The cache ID for this request.
id: PendingImageId,
/// Used to mark abort
aborted: bool,
/// The document associated with this request
doc: Trusted<Document>,
url: ServoUrl,
element: Trusted<HTMLImageElement>,
}
impl FetchResponseListener for ImageContext {
fn should_invoke(&self) -> bool {
!self.aborted
}
fn process_request_body(&mut self, _: RequestId) {}
fn process_response(
&mut self,
_: &mut js::context::JSContext,
request_id: RequestId,
metadata: Result<FetchMetadata, NetworkError>,
) {
debug!("got {:?} for {:?}", metadata.as_ref().map(|_| ()), self.url);
self.image_cache.notify_pending_response(
self.id,
FetchResponseMsg::ProcessResponse(request_id, metadata.clone()),
);
let metadata = metadata.ok().map(|meta| match meta {
FetchMetadata::Unfiltered(m) => m,
FetchMetadata::Filtered { unsafe_, .. } => unsafe_,
});
// Step 14.5 of https://html.spec.whatwg.org/multipage/#img-environment-changes
if let Some(metadata) = metadata.as_ref() {
if let Some(ref content_type) = metadata.content_type {
let mime: Mime = content_type.clone().into_inner().into();
if mime.type_() == mime::MULTIPART && mime.subtype().as_str() == "x-mixed-replace" {
self.aborted = true;
}
}
}
let status = metadata
.as_ref()
.map(|m| m.status.clone())
.unwrap_or_else(HttpStatus::new_error);
self.status = {
if status.is_error() {
Err(NetworkError::ResourceLoadError(
"No http status code received".to_owned(),
))
} else if status.is_success() {
Ok(())
} else {
Err(NetworkError::ResourceLoadError(format!(
"HTTP error code {}",
status.code()
)))
}
};
}
fn process_response_chunk(
&mut self,
_: &mut js::context::JSContext,
request_id: RequestId,
payload: Vec<u8>,
) {
if self.status.is_ok() {
self.image_cache.notify_pending_response(
self.id,
FetchResponseMsg::ProcessResponseChunk(request_id, payload.into()),
);
}
}
fn process_response_eof(
self,
cx: &mut js::context::JSContext,
request_id: RequestId,
response: Result<(), NetworkError>,
timing: ResourceFetchTiming,
) {
self.image_cache.notify_pending_response(
self.id,
FetchResponseMsg::ProcessResponseEOF(request_id, response.clone(), timing.clone()),
);
network_listener::submit_timing(cx, &self, &response, &timing);
}
fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
let global = &self.resource_timing_global();
let elem = self.element.root();
let source_position = elem
.upcast::<Element>()
.compute_source_position(elem.line_number as u32);
global.report_csp_violations(violations, None, Some(source_position));
}
}
impl ResourceTimingListener for ImageContext {
fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
(
InitiatorType::LocalName("img".to_string()),
self.url.clone(),
)
}
fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
self.doc.root().global()
}
}
#[expect(non_snake_case)]
impl HTMLImageElement {
/// Update the current image with a valid URL.
fn fetch_image(&self, img_url: &ServoUrl, cx: &mut js::context::JSContext) {
let window = self.owner_window();
let cache_result = window.image_cache().get_cached_image_status(
img_url.clone(),
window.origin().immutable().clone(),
cors_setting_for_element(self.upcast()),
);
match cache_result {
ImageCacheResult::Available(ImageOrMetadataAvailable::ImageAvailable {
image,
url,
}) => self.process_image_response(ImageResponse::Loaded(image, url), cx),
ImageCacheResult::Available(ImageOrMetadataAvailable::MetadataAvailable(
metadata,
id,
)) => {
self.process_image_response(ImageResponse::MetadataLoaded(metadata), cx);
self.register_image_cache_callback(id, ChangeType::Element);
},
ImageCacheResult::Pending(id) => {
self.register_image_cache_callback(id, ChangeType::Element);
},
ImageCacheResult::ReadyForRequest(id) => {
self.fetch_request(img_url, id);
self.register_image_cache_callback(id, ChangeType::Element);
},
ImageCacheResult::FailedToLoadOrDecode => {
self.process_image_response(ImageResponse::FailedToLoadOrDecode, cx)
},
};
}
fn register_image_cache_callback(&self, id: PendingImageId, change_type: ChangeType) {
let trusted_node = Trusted::new(self);
let generation = self.generation_id();
let window = self.owner_window();
let callback = window.register_image_cache_listener(id, move |response, _| {
let trusted_node = trusted_node.clone();
let window = trusted_node.root().owner_window();
let callback_type = change_type.clone();
window
.as_global_scope()
.task_manager()
.networking_task_source()
.queue(task!(process_image_response: move |cx| {
let element = trusted_node.root();
// Ignore any image response for a previous request that has been discarded.
if generation != element.generation_id() {
return;
}
match callback_type {
ChangeType::Element => {
element.process_image_response(response.response, cx);
}
ChangeType::Environment { selected_source, selected_pixel_density } => {
element.process_image_response_for_environment_change(
response.response, selected_source, generation, selected_pixel_density, cx
);
}
}
}));
});
window.image_cache().add_listener(ImageLoadListener::new(
callback,
window.pipeline_id(),
id,
));
}
fn fetch_request(&self, img_url: &ServoUrl, id: PendingImageId) {
let document = self.owner_document();
let window = self.owner_window();
let context = ImageContext {
image_cache: window.image_cache(),
status: Ok(()),
id,
aborted: false,
doc: Trusted::new(&document),
element: Trusted::new(self),
url: img_url.clone(),
};
// https://html.spec.whatwg.org/multipage/#update-the-image-data steps 17-20
// This function is also used to prefetch an image in `script::dom::servoparser::prefetch`.
let global = document.global();
let mut request = create_a_potential_cors_request(
Some(window.webview_id()),
img_url.clone(),
Destination::Image,
cors_setting_for_element(self.upcast()),
None,
global.get_referrer(),
)
.with_global_scope(&global)
.referrer_policy(referrer_policy_for_element(self.upcast()));
if self.uses_srcset_or_picture() {
request = request.initiator(Initiator::ImageSet);
}
// This is a background load because the load blocker already fulfills the
// purpose of delaying the document's load event.
document.fetch_background(request, context);
}
// Steps common to when an image has been loaded.
fn handle_loaded_image(&self, image: Image, url: ServoUrl, cx: &mut js::context::JSContext) {
self.current_request.borrow_mut().metadata = Some(image.metadata());
self.current_request.borrow_mut().final_url = Some(url);
self.current_request.borrow_mut().image = Some(image);
self.current_request.borrow_mut().state = State::CompletelyAvailable;
LoadBlocker::terminate(&self.current_request.borrow().blocker, cx);
// Mark the node dirty
self.upcast::<Node>().dirty(NodeDamage::Other);
self.resolve_image_decode_promises();
}
/// <https://html.spec.whatwg.org/multipage/#update-the-image-data>
fn process_image_response(&self, image: ImageResponse, cx: &mut js::context::JSContext) {
// Step 27. As soon as possible, jump to the first applicable entry from the following list:
// TODO => "If the resource type is multipart/x-mixed-replace"
// => "If the resource type and data corresponds to a supported image format ...""
let (trigger_image_load, trigger_image_error) = match (image, self.image_request.get()) {
(ImageResponse::Loaded(image, url), ImageRequestPhase::Current) => {
self.handle_loaded_image(image, url, cx);
(true, false)
},
(ImageResponse::Loaded(image, url), ImageRequestPhase::Pending) => {
self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
self.image_request.set(ImageRequestPhase::Current);
self.handle_loaded_image(image, url, cx);
(true, false)
},
(ImageResponse::MetadataLoaded(meta), ImageRequestPhase::Current) => {
// Otherwise, if the user agent is able to determine image request's image's width
// and height, and image request is the current request, prepare image request for
// presentation given the img element and set image request's state to partially
// available.
self.current_request.borrow_mut().state = State::PartiallyAvailable;
self.current_request.borrow_mut().metadata = Some(meta);
(false, false)
},
(ImageResponse::MetadataLoaded(_), ImageRequestPhase::Pending) => {
// If the user agent is able to determine image request's image's width and height,
// and image request is the pending request, set image request's state to partially
// available.
self.pending_request.borrow_mut().state = State::PartiallyAvailable;
(false, false)
},
(ImageResponse::FailedToLoadOrDecode, ImageRequestPhase::Current) => {
// Otherwise, if the user agent is able to determine that image request's image is
// corrupted in some fatal way such that the image dimensions cannot be obtained,
// and image request is the current request:
// Step 1. Abort the image request for image request.
self.abort_request(State::Broken, ImageRequestPhase::Current, cx);
self.load_broken_image_icon();
// Step 2. If maybe omit events is not set or previousURL is not equal to urlString,
// then fire an event named error at the img element.
// TODO: Add missing `maybe omit events` flag and previousURL.
(false, true)
},
(ImageResponse::FailedToLoadOrDecode, ImageRequestPhase::Pending) => {
// Otherwise, if the user agent is able to determine that image request's image is
// corrupted in some fatal way such that the image dimensions cannot be obtained,
// and image request is the pending request:
// Step 1. Abort the image request for the current request and the pending request.
self.abort_request(State::Broken, ImageRequestPhase::Current, cx);
self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
// Step 2. Upgrade the pending request to the current request.
mem::swap(
&mut *self.current_request.borrow_mut(),
&mut *self.pending_request.borrow_mut(),
);
self.image_request.set(ImageRequestPhase::Current);
// Step 3. Set the current request's state to broken.
self.current_request.borrow_mut().state = State::Broken;
self.load_broken_image_icon();
// Step 4. Fire an event named error at the img element.
(false, true)
},
};
// Fire image.onload and loadend
if trigger_image_load {
// TODO: https://html.spec.whatwg.org/multipage/#fire-a-progress-event-or-event
self.upcast::<EventTarget>().fire_event(cx, atom!("load"));
self.upcast::<EventTarget>()
.fire_event(cx, atom!("loadend"));
}
// Fire image.onerror
if trigger_image_error {
self.upcast::<EventTarget>().fire_event(cx, atom!("error"));
self.upcast::<EventTarget>()
.fire_event(cx, atom!("loadend"));
}
}
/// The response part of
/// <https://html.spec.whatwg.org/multipage/#reacting-to-environment-changes>.
fn process_image_response_for_environment_change(
&self,
image: ImageResponse,
selected_source: USVString,
generation: u32,
selected_pixel_density: f64,
cx: &mut js::context::JSContext,
) {
match image {
ImageResponse::Loaded(image, url) => {
self.pending_request.borrow_mut().metadata = Some(image.metadata());
self.pending_request.borrow_mut().final_url = Some(url);
self.pending_request.borrow_mut().image = Some(image);
self.finish_reacting_to_environment_change(
selected_source,
generation,
selected_pixel_density,
);
},
ImageResponse::FailedToLoadOrDecode => {
// > Step 15.6: If response's unsafe response is a network error or if the
// > image format is unsupported (as determined by applying the image
// > sniffing rules, again as mentioned earlier), or if the user agent is
// > able to determine that image request's image is corrupted in some fatal
// > way such that the image dimensions cannot be obtained, or if the
// > resource type is multipart/x-mixed-replace, then set the pending
// > request to null and abort these steps.
self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
},
ImageResponse::MetadataLoaded(meta) => {
self.pending_request.borrow_mut().metadata = Some(meta);
},
};
}
/// <https://html.spec.whatwg.org/multipage/#abort-the-image-request>
fn abort_request(
&self,
state: State,
request: ImageRequestPhase,
cx: &mut js::context::JSContext,
) {
let mut request = match request {
ImageRequestPhase::Current => self.current_request.borrow_mut(),
ImageRequestPhase::Pending => self.pending_request.borrow_mut(),
};
LoadBlocker::terminate(&request.blocker, cx);
request.state = state;
request.image = None;
request.metadata = None;
request.current_pixel_density = None;
if matches!(state, State::Broken) {
self.reject_image_decode_promises();
} else if matches!(state, State::CompletelyAvailable) {
self.resolve_image_decode_promises();
}
}
/// <https://html.spec.whatwg.org/multipage/#create-a-source-set>
fn create_source_set(&self) -> SourceSet {
let element = self.upcast::<Element>();
// Step 1. Let source set be an empty source set.
let mut source_set = SourceSet::new();
// Step 2. If srcset is not an empty string, then set source set to the result of parsing
// srcset.
if let Some(srcset) = element.get_attribute(&local_name!("srcset")) {
source_set.image_sources = parse_a_srcset_attribute(&srcset.value());
}
// Step 3. Set source set's source size to the result of parsing sizes with img.
if let Some(sizes) = element.get_attribute(&local_name!("sizes")) {
source_set.source_size = parse_a_sizes_attribute(&sizes.value());
}
// Step 4. If default source is not the empty string and source set does not contain an
// image source with a pixel density descriptor value of 1, and no image source with a width
// descriptor, append default source to source set.
let src = element.get_string_attribute(&local_name!("src"));
let no_density_source_of_1 = source_set
.image_sources
.iter()
.all(|source| source.descriptor.density != Some(1.));
let no_width_descriptor = source_set
.image_sources
.iter()
.all(|source| source.descriptor.width.is_none());
if !src.is_empty() && no_density_source_of_1 && no_width_descriptor {
source_set.image_sources.push(ImageSource {
url: src.to_string(),
descriptor: Descriptor {
width: None,
density: None,
},
})
}
// Step 5. Normalize the source densities of source set.
self.normalise_source_densities(&mut source_set);
// Step 6. Return source set.
source_set
}
/// <https://html.spec.whatwg.org/multipage/#update-the-source-set>
fn update_source_set(&self) {
// Step 1. Set el's source set to an empty source set.
*self.source_set.borrow_mut() = SourceSet::new();
// Step 2. Let elements be « el ».
// Step 3. If el is an img element whose parent node is a picture element, then replace the
// contents of elements with el's parent node's child elements, retaining relative order.
// Step 4. Let img be el if el is an img element, otherwise null.
let elem = self.upcast::<Element>();
let parent = elem.upcast::<Node>().GetParentElement();
let elements = match parent.as_ref() {
Some(p) => {
if p.is::<HTMLPictureElement>() {
p.upcast::<Node>()
.children()
.filter_map(DomRoot::downcast::<Element>)
.map(|n| DomRoot::from_ref(&*n))
.collect()
} else {
vec![DomRoot::from_ref(elem)]
}
},
None => vec![DomRoot::from_ref(elem)],
};
// Step 5. For each child in elements:
for element in &elements {
// Step 5.1. If child is el:
if *element == DomRoot::from_ref(elem) {
// Step 5.1.10. Set el's source set to the result of creating a source set given
// default source, srcset, sizes, and img.
*self.source_set.borrow_mut() = self.create_source_set();
// Step 5.1.11. Return.
return;
}
// Step 5.2. If child is not a source element, then continue.
if !element.is::<HTMLSourceElement>() {
continue;
}
let mut source_set = SourceSet::new();
// Step 5.3. If child does not have a srcset attribute, continue to the next child.
// Step 5.4. Parse child's srcset attribute and let source set be the returned source
// set.
match element.get_attribute(&local_name!("srcset")) {
Some(srcset) => {
source_set.image_sources = parse_a_srcset_attribute(&srcset.value());
},
_ => continue,
}
// Step 5.5. If source set has zero image sources, continue to the next child.
if source_set.image_sources.is_empty() {
continue;
}
// Step 5.6. If child has a media attribute, and its value does not match the
// environment, continue to the next child.
if let Some(media) = element.get_attribute(&local_name!("media")) {
if !MediaList::matches_environment(&element.owner_document(), &media.value()) {
continue;
}
}
// Step 5.7. Parse child's sizes attribute with img, and let source set's source size be
// the returned value.
if let Some(sizes) = element.get_attribute(&local_name!("sizes")) {
source_set.source_size = parse_a_sizes_attribute(&sizes.value());
}
// Step 5.8. If child has a type attribute, and its value is an unknown or unsupported
// MIME type, continue to the next child.
if let Some(type_) = element.get_attribute(&local_name!("type")) {
if !is_supported_image_mime_type(&type_.value()) {
continue;
}
}
// Step 5.9. If child has width or height attributes, set el's dimension attribute
// source to child. Otherwise, set el's dimension attribute source to el.
if element.get_attribute(&local_name!("width")).is_some() ||
element.get_attribute(&local_name!("height")).is_some()
{
self.dimension_attribute_source.set(Some(element));
} else {
self.dimension_attribute_source.set(Some(elem));
}
// Step 5.10. Normalize the source densities of source set.
self.normalise_source_densities(&mut source_set);
// Step 5.11. Set el's source set to source set.
*self.source_set.borrow_mut() = source_set;
// Step 5.12. Return.
return;
}
}
fn evaluate_source_size_list(&self, source_size_list: &SourceSizeList) -> Au {
let document = self.owner_document();
let quirks_mode = document.quirks_mode();
source_size_list.evaluate(document.window().layout().device(), quirks_mode)
}
/// <https://html.spec.whatwg.org/multipage/#normalise-the-source-densities>
fn normalise_source_densities(&self, source_set: &mut SourceSet) {
// Step 1. Let source size be source set's source size.
let source_size = self.evaluate_source_size_list(&source_set.source_size);
// Step 2. For each image source in source set:
for image_source in &mut source_set.image_sources {
// Step 2.1. If the image source has a pixel density descriptor, continue to the next
// image source.
if image_source.descriptor.density.is_some() {
continue;
}
// Step 2.2. Otherwise, if the image source has a width descriptor, replace the width
// descriptor with a pixel density descriptor with a value of the width descriptor value
// divided by source size and a unit of x.
if image_source.descriptor.width.is_some() {
let width = image_source.descriptor.width.unwrap();
image_source.descriptor.density = Some(width as f64 / source_size.to_f64_px());
} else {
// Step 2.3. Otherwise, give the image source a pixel density descriptor of 1x.
image_source.descriptor.density = Some(1_f64);
}
}
}
/// <https://html.spec.whatwg.org/multipage/#select-an-image-source>
fn select_image_source(&self) -> Option<(USVString, f64)> {
// Step 1. Update the source set for el.
self.update_source_set();
// Step 2. If el's source set is empty, return null as the URL and undefined as the pixel
// density.
if self.source_set.borrow().image_sources.is_empty() {
return None;
}
// Step 3. Return the result of selecting an image from el's source set.
self.select_image_source_from_source_set()
}
/// <https://html.spec.whatwg.org/multipage/#select-an-image-source-from-a-source-set>
fn select_image_source_from_source_set(&self) -> Option<(USVString, f64)> {
// Step 1. If an entry b in sourceSet has the same associated pixel density descriptor as an
// earlier entry a in sourceSet, then remove entry b. Repeat this step until none of the
// entries in sourceSet have the same associated pixel density descriptor as an earlier
// entry.
let source_set = self.source_set.borrow();
let len = source_set.image_sources.len();
// Using FxHash is ok here as the indices are just 0..len
let mut repeat_indices = FxHashSet::default();
for outer_index in 0..len {
if repeat_indices.contains(&outer_index) {
continue;
}
let imgsource = &source_set.image_sources[outer_index];
let pixel_density = imgsource.descriptor.density.unwrap();
for inner_index in (outer_index + 1)..len {
let imgsource2 = &source_set.image_sources[inner_index];
if pixel_density == imgsource2.descriptor.density.unwrap() {
repeat_indices.insert(inner_index);
}
}
}
let mut max = (0f64, 0);
let img_sources = &mut vec![];
for (index, image_source) in source_set.image_sources.iter().enumerate() {
if repeat_indices.contains(&index) {
continue;
}
let den = image_source.descriptor.density.unwrap();
if max.0 < den {
max = (den, img_sources.len());
}
img_sources.push(image_source);
}
// Step 2. In an implementation-defined manner, choose one image source from sourceSet. Let
// selectedSource be this choice.
let mut best_candidate = max;
let device_pixel_ratio = self
.owner_document()
.window()
.viewport_details()
.hidpi_scale_factor
.get() as f64;
for (index, image_source) in img_sources.iter().enumerate() {
let current_den = image_source.descriptor.density.unwrap();
if current_den < best_candidate.0 && current_den >= device_pixel_ratio {
best_candidate = (current_den, index);
}
}
let selected_source = img_sources.remove(best_candidate.1).clone();
// Step 3. Return selectedSource and its associated pixel density.
Some((
USVString(selected_source.url),
selected_source.descriptor.density.unwrap(),
))
}
fn init_image_request(
&self,
request: &mut RefMut<'_, ImageRequest>,
url: &ServoUrl,
src: &USVString,
cx: &mut js::context::JSContext,
) {
request.parsed_url = Some(url.clone());
request.source_url = Some(src.clone());
request.image = None;
request.metadata = None;
let document = self.owner_document();
LoadBlocker::terminate(&request.blocker, cx);
*request.blocker.borrow_mut() =
Some(LoadBlocker::new(&document, LoadType::Image(url.clone())));
}
/// <https://html.spec.whatwg.org/multipage/#update-the-image-data>
fn prepare_image_request(
&self,
selected_source: &USVString,
selected_pixel_density: f64,
image_url: &ServoUrl,
cx: &mut js::context::JSContext,
) {
match self.image_request.get() {
ImageRequestPhase::Pending => {
// Step 14. If the pending request is not null and urlString is the same as the
// pending request's current URL, then return.
if self
.pending_request
.borrow()
.parsed_url
.as_ref()
.is_some_and(|parsed_url| *parsed_url == *image_url)
{
return;
}
},
ImageRequestPhase::Current => {
// Step 16. Abort the image request for the pending request.
self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
// Step 17. Set image request to a new image request whose current URL is urlString.
let mut current_request = self.current_request.borrow_mut();
let mut pending_request = self.pending_request.borrow_mut();
match (current_request.parsed_url.as_ref(), current_request.state) {
(Some(parsed_url), State::PartiallyAvailable) => {
// Step 15. If urlString is the same as the current request's current URL
// and the current request's state is partially available, then abort the
// image request for the pending request, queue an element task on the DOM
// manipulation task source given the img element to restart the animation
// if restart animation is set, and return.
if *parsed_url == *image_url {
// TODO: queue a task to restart animation, if restart-animation is set
return;
}
// Step 18. If the current request's state is unavailable or broken, then
// set the current request to image request. Otherwise, set the pending
// request to image request.
self.image_request.set(ImageRequestPhase::Pending);
self.init_image_request(
&mut pending_request,
image_url,
selected_source,
cx,
);
pending_request.current_pixel_density = Some(selected_pixel_density);
},
(_, State::Broken) | (_, State::Unavailable) => {
// Step 18. If the current request's state is unavailable or broken, then
// set the current request to image request. Otherwise, set the pending
// request to image request.
self.init_image_request(
&mut current_request,
image_url,
selected_source,
cx,
);
current_request.current_pixel_density = Some(selected_pixel_density);
self.reject_image_decode_promises();
},
(_, _) => {
// Step 18. If the current request's state is unavailable or broken, then
// set the current request to image request. Otherwise, set the pending
// request to image request.
self.image_request.set(ImageRequestPhase::Pending);
self.init_image_request(
&mut pending_request,
image_url,
selected_source,
cx,
);
pending_request.current_pixel_density = Some(selected_pixel_density);
},
}
},
}
self.fetch_image(image_url, cx);
}
/// <https://html.spec.whatwg.org/multipage/#update-the-image-data>
fn update_the_image_data_sync_steps(&self, cx: &mut js::context::JSContext) {
// Step 10. Let selected source and selected pixel density be the URL and pixel density that
// results from selecting an image source, respectively.
let Some((selected_source, selected_pixel_density)) = self.select_image_source() else {
// Step 11. If selected source is null, then:
// Step 11.1. Set the current request's state to broken, abort the image request for the
// current request and the pending request, and set the pending request to null.
self.abort_request(State::Broken, ImageRequestPhase::Current, cx);
self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
self.image_request.set(ImageRequestPhase::Current);
// Step 11.2. Queue an element task on the DOM manipulation task source given the img
// element and the following steps:
let this = Trusted::new(self);
self.owner_global().task_manager().dom_manipulation_task_source().queue(
task!(image_null_source_error: move |cx| {
let this = this.root();
// Step 11.2.1. Change the current request's current URL to the empty string.
{
let mut current_request =
this.current_request.borrow_mut();
current_request.source_url = None;
current_request.parsed_url = None;
}
// Step 11.2.2. If all of the following are true:
// the element has a src attribute or it uses srcset or picture; and
// maybe omit events is not set or previousURL is not the empty string,
// then fire an event named error at the img element.
// TODO: Add missing `maybe omit events` flag and previousURL.
let has_src_attribute = this.upcast::<Element>().has_attribute(&local_name!("src"));
if has_src_attribute || this.uses_srcset_or_picture() {
this.upcast::<EventTarget>().fire_event(cx, atom!("error"));
}
}));
// Step 11.2.3. Return.
return;
};
// Step 12. Let urlString be the result of encoding-parsing-and-serializing a URL given
// selected source, relative to the element's node document.
let Ok(image_url) = self.owner_document().base_url().join(&selected_source) else {
// Step 13. If urlString is failure, then:
// Step 13.1. Abort the image request for the current request and the pending request.
// Step 13.2. Set the current request's state to broken.
self.abort_request(State::Broken, ImageRequestPhase::Current, cx);
self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
// Step 13.3. Set the pending request to null.
self.image_request.set(ImageRequestPhase::Current);
// Step 13.4. Queue an element task on the DOM manipulation task source given the img
// element and the following steps:
let this = Trusted::new(self);
self.owner_global()
.task_manager()
.dom_manipulation_task_source()
.queue(task!(image_selected_source_error: move |cx| {
let this = this.root();
// Step 13.4.1. Change the current request's current URL to selected source.
{
let mut current_request =
this.current_request.borrow_mut();
current_request.source_url = Some(selected_source);
current_request.parsed_url = None;
}
// Step 13.4.2. If maybe omit events is not set or previousURL is not equal to
// selected source, then fire an event named error at the img element.
// TODO: Add missing `maybe omit events` flag and previousURL.
this.upcast::<EventTarget>().fire_event(cx, atom!("error"));
}));
// Step 13.5. Return.
return;
};
self.prepare_image_request(&selected_source, selected_pixel_density, &image_url, cx);
}
/// <https://html.spec.whatwg.org/multipage/#update-the-image-data>
pub(crate) fn update_the_image_data(&self, cx: &mut js::context::JSContext) {
// Cancel any outstanding tasks that were queued before.
self.generation.set(self.generation.get() + 1);
// Step 1. If the element's node document is not fully active, then:
if !self.owner_document().is_active() {
// TODO Step 1.1. Continue running this algorithm in parallel.
// TODO Step 1.2. Wait until the element's node document is fully active.
// TODO Step 1.3. If another instance of this algorithm for this img element was started after
// this instance (even if it aborted and is no longer running), then return.
// TODO Step 1.4. Queue a microtask to continue this algorithm.
}
// Step 2. If the user agent cannot support images, or its support for images has been
// disabled, then abort the image request for the current request and the pending request,
// set the current request's state to unavailable, set the pending request to null, and
// return.
// Nothing specific to be done here since the user agent supports image processing.
// Always first set the current request to unavailable, ensuring img.complete is false.
// <https://html.spec.whatwg.org/multipage/#when-to-obtain-images>
self.current_request.borrow_mut().state = State::Unavailable;
// TODO Step 3. Let previousURL be the current request's current URL.
// Step 4. Let selected source be null and selected pixel density be undefined.
let mut selected_source = None;
let mut selected_pixel_density = None;
// Step 5. If the element does not use srcset or picture and it has a src attribute
// specified whose value is not the empty string, then set selected source to the value of
// the element's src attribute and set selected pixel density to 1.0.
let src = self
.upcast::<Element>()
.get_string_attribute(&local_name!("src"));
if !self.uses_srcset_or_picture() && !src.is_empty() {
selected_source = Some(USVString(src.to_string()));
selected_pixel_density = Some(1_f64);
};
// Step 6. Set the element's last selected source to selected source.
self.last_selected_source
.borrow_mut()
.clone_from(&selected_source);
// Step 7. If selected source is not null, then:
if let Some(selected_source) = selected_source {
// Step 7.1. Let urlString be the result of encoding-parsing-and-serializing a URL given
// selected source, relative to the element's node document.
// Step 7.2. If urlString is failure, then abort this inner set of steps.
if let Ok(image_url) = self.owner_document().base_url().join(&selected_source) {
// Step 7.3. Let key be a tuple consisting of urlString, the img element's
// crossorigin attribute's mode, and, if that mode is not No CORS, the node
// document's origin.
let window = self.owner_window();
let response = window.image_cache().get_image(
image_url.clone(),
window.origin().immutable().clone(),
cors_setting_for_element(self.upcast()),
);
// Step 7.4. If the list of available images contains an entry for key, then:
if let Some(image) = response {
// TODO Step 7.4.1. Set the ignore higher-layer caching flag for that entry.
// Step 7.4.2. Abort the image request for the current request and the pending
// request.
self.abort_request(State::CompletelyAvailable, ImageRequestPhase::Current, cx);
self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
// Step 7.4.3. Set the pending request to null.
self.image_request.set(ImageRequestPhase::Current);
// Step 7.4.4. Set the current request to a new image request whose image data
// is that of the entry and whose state is completely available.
let mut current_request = self.current_request.borrow_mut();
current_request.metadata = Some(image.metadata());
current_request.image = Some(image);
current_request.final_url = Some(image_url.clone());
// TODO Step 7.4.5. Prepare the current request for presentation given the img
// element.
self.upcast::<Node>().dirty(NodeDamage::Other);
// Step 7.4.6. Set the current request's current pixel density to selected pixel
// density.
current_request.current_pixel_density = selected_pixel_density;
// Step 7.4.7. Queue an element task on the DOM manipulation task source given
// the img element and the following steps:
let this = Trusted::new(self);
self.owner_global()
.task_manager()
.dom_manipulation_task_source()
.queue(task!(image_load_event: move |cx| {
let this = this.root();
// TODO Step 7.4.7.1. If restart animation is set, then restart the
// animation.
// Step 7.4.7.2. Set the current request's current URL to urlString.
{
let mut current_request =
this.current_request.borrow_mut();
current_request.source_url = Some(selected_source);
current_request.parsed_url = Some(image_url);
}
// Step 7.4.7.3. If maybe omit events is not set or previousURL is not
// equal to urlString, then fire an event named load at the img element.
// TODO: Add missing `maybe omit events` flag and previousURL.
this.upcast::<EventTarget>().fire_event(cx, atom!("load"));
}));
// Step 7.4.8. Abort the update the image data algorithm.
return;
}
}
}
// Step 8. Queue a microtask to perform the rest of this algorithm, allowing the task that
// invoked this algorithm to continue.
let task = ImageElementMicrotask::UpdateImageData {
elem: DomRoot::from_ref(self),
generation: self.generation.get(),
};
ScriptThread::await_stable_state(Microtask::ImageElement(task));
}
/// <https://html.spec.whatwg.org/multipage/#img-environment-changes>
pub(crate) fn react_to_environment_changes(&self) {
// Step 1. Await a stable state.
let task = ImageElementMicrotask::EnvironmentChanges {
elem: DomRoot::from_ref(self),
generation: self.generation.get(),
};
ScriptThread::await_stable_state(Microtask::ImageElement(task));
}
/// <https://html.spec.whatwg.org/multipage/#img-environment-changes>
fn react_to_environment_changes_sync_steps(
&self,
generation: u32,
cx: &mut js::context::JSContext,
) {
let document = self.owner_document();
let has_pending_request = matches!(self.image_request.get(), ImageRequestPhase::Pending);
// Step 2. If the img element does not use srcset or picture, its node document is not fully
// active, it has image data whose resource type is multipart/x-mixed-replace, or its
// pending request is not null, then return.
if !document.is_active() || !self.uses_srcset_or_picture() || has_pending_request {
return;
}
// Step 3. Let selected source and selected pixel density be the URL and pixel density that
// results from selecting an image source, respectively.
let Some((selected_source, selected_pixel_density)) = self.select_image_source() else {
// Step 4. If selected source is null, then return.
return;
};
// Step 5. If selected source and selected pixel density are the same as the element's last
// selected source and current pixel density, then return.
let mut same_selected_source = self
.last_selected_source
.borrow()
.as_ref()
.is_some_and(|source| *source == selected_source);
// There are missing steps for the element's last selected source in specification so let's
// check the current request's current URL as well.
// <https://github.com/whatwg/html/issues/5060>
same_selected_source = same_selected_source ||
self.current_request
.borrow()
.source_url
.as_ref()
.is_some_and(|source| *source == selected_source);
let same_selected_pixel_density = self
.current_request
.borrow()
.current_pixel_density
.is_some_and(|pixel_density| pixel_density == selected_pixel_density);
if same_selected_source && same_selected_pixel_density {
return;
}
// Step 6. Let urlString be the result of encoding-parsing-and-serializing a URL given
// selected source, relative to the element's node document.
// Step 7. If urlString is failure, then return.
let Ok(image_url) = document.base_url().join(&selected_source) else {
return;
};
// Step 13. Set the element's pending request to image request.
self.image_request.set(ImageRequestPhase::Pending);
self.init_image_request(
&mut self.pending_request.borrow_mut(),
&image_url,
&selected_source,
cx,
);
// Step 15. If the list of available images contains an entry for key, then set image
// request's image data to that of the entry. Continue to the next step.
let window = self.owner_window();
let cache_result = window.image_cache().get_cached_image_status(
image_url.clone(),
window.origin().immutable().clone(),
cors_setting_for_element(self.upcast()),
);
let change_type = ChangeType::Environment {
selected_source: selected_source.clone(),
selected_pixel_density,
};
match cache_result {
ImageCacheResult::Available(ImageOrMetadataAvailable::ImageAvailable { .. }) => {
self.finish_reacting_to_environment_change(
selected_source,
generation,
selected_pixel_density,
);
},
ImageCacheResult::Available(ImageOrMetadataAvailable::MetadataAvailable(m, id)) => {
self.process_image_response_for_environment_change(
ImageResponse::MetadataLoaded(m),
selected_source,
generation,
selected_pixel_density,
cx,
);
self.register_image_cache_callback(id, change_type);
},
ImageCacheResult::FailedToLoadOrDecode => {
self.process_image_response_for_environment_change(
ImageResponse::FailedToLoadOrDecode,
selected_source,
generation,
selected_pixel_density,
cx,
);
},
ImageCacheResult::ReadyForRequest(id) => {
self.fetch_request(&image_url, id);
self.register_image_cache_callback(id, change_type);
},
ImageCacheResult::Pending(id) => {
self.register_image_cache_callback(id, change_type);
},
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-img-decode>
fn react_to_decode_image_sync_steps(&self, promise: Rc<Promise>, can_gc: CanGc) {
// Step 2.2. If any of the following are true: this's node document is not fully active; or
// this's current request's state is broken, then reject promise with an "EncodingError"
// DOMException.
if !self.owner_document().is_fully_active() ||
matches!(self.current_request.borrow().state, State::Broken)
{
promise.reject_error(Error::Encoding(None), can_gc);
} else if matches!(
self.current_request.borrow().state,
State::CompletelyAvailable
) {
// this doesn't follow the spec, but it's been discussed in <https://github.com/whatwg/html/issues/4217>
promise.resolve_native(&(), can_gc);
} else if matches!(self.current_request.borrow().state, State::Unavailable) &&
self.current_request.borrow().source_url.is_none()
{
// Note: Despite being not explicitly stated in the specification but if current
// request's state is unavailable and current URL is empty string (<img> without "src"
// and "srcset" attributes) then reject promise with an "EncodingError" DOMException.
// <https://github.com/whatwg/html/issues/11769>
promise.reject_error(Error::Encoding(None), can_gc);
} else {
self.image_decode_promises.borrow_mut().push(promise);
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-img-decode>
fn resolve_image_decode_promises(&self) {
if self.image_decode_promises.borrow().is_empty() {
return;
}
// Step 2.3. If the decoding process completes successfully, then queue a global task on the
// DOM manipulation task source with global to resolve promise with undefined.
let trusted_image_decode_promises: Vec<TrustedPromise> = self
.image_decode_promises
.borrow()
.iter()
.map(|promise| TrustedPromise::new(promise.clone()))
.collect();
self.image_decode_promises.borrow_mut().clear();
self.owner_global()
.task_manager()
.dom_manipulation_task_source()
.queue(task!(fulfill_image_decode_promises: move |cx| {
for trusted_promise in trusted_image_decode_promises {
trusted_promise.root().resolve_native(&(), CanGc::from_cx(cx));
}
}));
}
/// <https://html.spec.whatwg.org/multipage/#dom-img-decode>
fn reject_image_decode_promises(&self) {
if self.image_decode_promises.borrow().is_empty() {
return;
}
// Step 2.3. Queue a global task on the DOM manipulation task source with global to reject
// promise with an "EncodingError" DOMException.
let trusted_image_decode_promises: Vec<TrustedPromise> = self
.image_decode_promises
.borrow()
.iter()
.map(|promise| TrustedPromise::new(promise.clone()))
.collect();
self.image_decode_promises.borrow_mut().clear();
self.owner_global()
.task_manager()
.dom_manipulation_task_source()
.queue(task!(reject_image_decode_promises: move |cx| {
for trusted_promise in trusted_image_decode_promises {
trusted_promise.root().reject_error(Error::Encoding(None), CanGc::from_cx(cx));
}
}));
}
/// <https://html.spec.whatwg.org/multipage/#img-environment-changes>
fn finish_reacting_to_environment_change(
&self,
selected_source: USVString,
generation: u32,
selected_pixel_density: f64,
) {
// Step 16. Queue an element task on the DOM manipulation task source given the img element
// and the following steps:
let this = Trusted::new(self);
self.owner_global()
.task_manager()
.dom_manipulation_task_source()
.queue(task!(image_load_event: move |cx| {
let this = this.root();
// Step 16.1. If the img element has experienced relevant mutations since this
// algorithm started, then set the pending request to null and abort these steps.
if this.generation.get() != generation {
this.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
this.image_request.set(ImageRequestPhase::Current);
return;
}
// Step 16.2. Set the img element's last selected source to selected source and the
// img element's current pixel density to selected pixel density.
*this.last_selected_source.borrow_mut() = Some(selected_source);
{
let mut pending_request = this.pending_request.borrow_mut();
// Step 16.3. Set the image request's state to completely available.
pending_request.state = State::CompletelyAvailable;
pending_request.current_pixel_density = Some(selected_pixel_density);
// Step 16.4. Add the image to the list of available images using the key key,
// with the ignore higher-layer caching flag set.
// Already a part of the list of available images due to Step 15.
// Step 16.5. Upgrade the pending request to the current request.
mem::swap(&mut *this.current_request.borrow_mut(), &mut *pending_request);
}
this.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
this.image_request.set(ImageRequestPhase::Current);
// TODO Step 16.6. Prepare image request for presentation given the img element.
this.upcast::<Node>().dirty(NodeDamage::Other);
// Step 16.7. Fire an event named load at the img element.
this.upcast::<EventTarget>().fire_event(cx, atom!("load"));
}));
}
/// <https://html.spec.whatwg.org/multipage/#use-srcset-or-picture>
fn uses_srcset_or_picture(&self) -> bool {
let element = self.upcast::<Element>();
let has_srcset_attribute = element.has_attribute(&local_name!("srcset"));
let has_parent_picture = element
.upcast::<Node>()
.GetParentElement()
.is_some_and(|parent| parent.is::<HTMLPictureElement>());
has_srcset_attribute || has_parent_picture
}
fn new_inherited(
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
creator: ElementCreator,
) -> HTMLImageElement {
HTMLImageElement {
htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
image_request: Cell::new(ImageRequestPhase::Current),
current_request: DomRefCell::new(ImageRequest {
state: State::Unavailable,
parsed_url: None,
source_url: None,
image: None,
metadata: None,
blocker: DomRefCell::new(None),
final_url: None,
current_pixel_density: None,
}),
pending_request: DomRefCell::new(ImageRequest {
state: State::Unavailable,
parsed_url: None,
source_url: None,
image: None,
metadata: None,
blocker: DomRefCell::new(None),
final_url: None,
current_pixel_density: None,
}),
form_owner: Default::default(),
generation: Default::default(),
source_set: DomRefCell::new(SourceSet::new()),
dimension_attribute_source: Default::default(),
last_selected_source: DomRefCell::new(None),
image_decode_promises: DomRefCell::new(vec![]),
line_number: creator.return_line_number(),
}
}
pub(crate) fn new(
cx: &mut js::context::JSContext,
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
proto: Option<HandleObject>,
creator: ElementCreator,
) -> DomRoot<HTMLImageElement> {
let image_element = Node::reflect_node_with_proto(
cx,
Box::new(HTMLImageElement::new_inherited(
local_name, prefix, document, creator,
)),
document,
proto,
);
image_element
.dimension_attribute_source
.set(Some(image_element.upcast()));
image_element
}
pub(crate) fn areas(&self) -> Option<Vec<DomRoot<HTMLAreaElement>>> {
let elem = self.upcast::<Element>();
let usemap_attr = elem.get_attribute(&local_name!("usemap"))?;
let value = usemap_attr.value();
if value.is_empty() || !value.is_char_boundary(1) {
return None;
}
let (first, last) = value.split_at(1);
if first != "#" || last.is_empty() {
return None;
}
let useMapElements = self
.owner_document()
.upcast::<Node>()
.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLMapElement>)
.find(|n| {
n.upcast::<Element>()
.get_name()
.is_some_and(|n| *n == *last)
});
useMapElements.map(|mapElem| mapElem.get_area_elements())
}
pub(crate) fn same_origin(&self, origin: &MutableOrigin) -> bool {
if let Some(ref image) = self.current_request.borrow().image {
return image.cors_status() == CorsStatus::Safe;
}
self.current_request
.borrow()
.final_url
.as_ref()
.is_some_and(|url| url.scheme() == "data" || url.origin().same_origin(origin))
}
fn generation_id(&self) -> u32 {
self.generation.get()
}
fn load_broken_image_icon(&self) {
let window = self.owner_window();
let Some(broken_image_icon) = window.image_cache().get_broken_image_icon() else {
return;
};
self.current_request.borrow_mut().metadata = Some(broken_image_icon.metadata);
self.current_request.borrow_mut().image = Some(Image::Raster(broken_image_icon));
self.upcast::<Node>().dirty(NodeDamage::Other);
}
/// Get the full URL of the current image of this `<img>` element, returning `None` if the URL
/// could not be joined with the `Document` URL.
pub(crate) fn full_image_url_for_user_interface(&self) -> Option<ServoUrl> {
self.owner_document()
.base_url()
.join(&self.CurrentSrc())
.ok()
}
}
#[derive(JSTraceable, MallocSizeOf)]
pub(crate) enum ImageElementMicrotask {
UpdateImageData {
elem: DomRoot<HTMLImageElement>,
generation: u32,
},
EnvironmentChanges {
elem: DomRoot<HTMLImageElement>,
generation: u32,
},
Decode {
elem: DomRoot<HTMLImageElement>,
#[conditional_malloc_size_of]
promise: Rc<Promise>,
},
}
impl MicrotaskRunnable for ImageElementMicrotask {
fn handler(&self, cx: &mut js::context::JSContext) {
match *self {
ImageElementMicrotask::UpdateImageData {
ref elem,
ref generation,
} => {
// <https://html.spec.whatwg.org/multipage/#update-the-image-data>
// Step 9. If another instance of this algorithm for this img element was started
// after this instance (even if it aborted and is no longer running), then return.
if elem.generation.get() == *generation {
elem.update_the_image_data_sync_steps(cx);
}
},
ImageElementMicrotask::EnvironmentChanges {
ref elem,
ref generation,
} => {
elem.react_to_environment_changes_sync_steps(*generation, cx);
},
ImageElementMicrotask::Decode {
ref elem,
ref promise,
} => {
elem.react_to_decode_image_sync_steps(promise.clone(), CanGc::from_cx(cx));
},
}
}
fn enter_realm<'cx>(&self, cx: &'cx mut js::context::JSContext) -> AutoRealm<'cx> {
match self {
&ImageElementMicrotask::UpdateImageData { ref elem, .. } |
&ImageElementMicrotask::EnvironmentChanges { ref elem, .. } |
&ImageElementMicrotask::Decode { ref elem, .. } => enter_auto_realm(cx, &**elem),
}
}
}
impl<'dom> LayoutDom<'dom, HTMLImageElement> {
#[expect(unsafe_code)]
fn current_request(self) -> &'dom ImageRequest {
unsafe { self.unsafe_get().current_request.borrow_for_layout() }
}
#[expect(unsafe_code)]
fn dimension_attribute_source(self) -> LayoutDom<'dom, Element> {
unsafe {
self.unsafe_get()
.dimension_attribute_source
.get_inner_as_layout()
.expect("dimension attribute source should be always non-null")
}
}
pub(crate) fn image_url(self) -> Option<ServoUrl> {
self.current_request().parsed_url.clone()
}
pub(crate) fn image_data(self) -> (Option<Image>, Option<ImageMetadata>) {
let current_request = self.current_request();
(current_request.image.clone(), current_request.metadata)
}
pub(crate) fn image_density(self) -> Option<f64> {
self.current_request().current_pixel_density
}
pub(crate) fn showing_broken_image_icon(self) -> bool {
matches!(self.current_request().state, State::Broken)
}
pub(crate) fn get_width(self) -> LengthOrPercentageOrAuto {
self.dimension_attribute_source()
.get_attr_for_layout(&ns!(), &local_name!("width"))
.map(AttrValue::as_dimension)
.cloned()
.unwrap_or(LengthOrPercentageOrAuto::Auto)
}
pub(crate) fn get_height(self) -> LengthOrPercentageOrAuto {
self.dimension_attribute_source()
.get_attr_for_layout(&ns!(), &local_name!("height"))
.map(AttrValue::as_dimension)
.cloned()
.unwrap_or(LengthOrPercentageOrAuto::Auto)
}
}
/// <https://html.spec.whatwg.org/multipage/#parse-a-sizes-attribute>
fn parse_a_sizes_attribute(value: &str) -> SourceSizeList {
let mut input = ParserInput::new(value);
let mut parser = Parser::new(&mut input);
let url_data = Url::parse("about:blank").unwrap().into();
// FIXME(emilio): why ::empty() instead of ::DEFAULT? Also, what do
// browsers do regarding quirks-mode in a media list?
let context =
parser_context_for_anonymous_content(CssRuleType::Style, ParsingMode::empty(), &url_data);
SourceSizeList::parse(&context, &mut parser)
}
impl HTMLImageElementMethods<crate::DomTypeHolder> for HTMLImageElement {
/// <https://html.spec.whatwg.org/multipage/#dom-image>
fn Image(
cx: &mut JSContext,
window: &Window,
proto: Option<HandleObject>,
width: Option<u32>,
height: Option<u32>,
) -> Fallible<DomRoot<HTMLImageElement>> {
// Step 1. Let document be the current global object's associated Document.
let document = window.Document();
// Step 2. Let img be the result of creating an element given document, "img", and the HTML
// namespace.
let element = Element::create(
cx,
QualName::new(None, ns!(html), local_name!("img")),
None,
&document,
ElementCreator::ScriptCreated,
CustomElementCreationMode::Synchronous,
proto,
);
let image = DomRoot::downcast::<HTMLImageElement>(element).unwrap();
// Step 3. If width is given, then set an attribute value for img using "width" and width.
if let Some(w) = width {
image.SetWidth(cx, w);
}
// Step 4. If height is given, then set an attribute value for img using "height" and
// height.
if let Some(h) = height {
image.SetHeight(cx, h);
}
// Step 5. Return img.
Ok(image)
}
// https://html.spec.whatwg.org/multipage/#dom-img-alt
make_getter!(Alt, "alt");
// https://html.spec.whatwg.org/multipage/#dom-img-alt
make_setter!(SetAlt, "alt");
// https://html.spec.whatwg.org/multipage/#dom-img-src
make_url_getter!(Src, "src");
// https://html.spec.whatwg.org/multipage/#dom-img-src
make_url_setter!(SetSrc, "src");
// https://html.spec.whatwg.org/multipage/#dom-img-srcset
make_url_getter!(Srcset, "srcset");
// https://html.spec.whatwg.org/multipage/#dom-img-src
make_url_setter!(SetSrcset, "srcset");
// <https://html.spec.whatwg.org/multipage/#dom-img-sizes>
make_getter!(Sizes, "sizes");
// <https://html.spec.whatwg.org/multipage/#dom-img-sizes>
make_setter!(SetSizes, "sizes");
/// <https://html.spec.whatwg.org/multipage/#dom-img-crossOrigin>
fn GetCrossOrigin(&self) -> Option<DOMString> {
reflect_cross_origin_attribute(self.upcast::<Element>())
}
/// <https://html.spec.whatwg.org/multipage/#dom-img-crossOrigin>
fn SetCrossOrigin(&self, cx: &mut JSContext, value: Option<DOMString>) {
set_cross_origin_attribute(cx, self.upcast::<Element>(), value);
}
// https://html.spec.whatwg.org/multipage/#dom-img-usemap
make_getter!(UseMap, "usemap");
// https://html.spec.whatwg.org/multipage/#dom-img-usemap
make_setter!(SetUseMap, "usemap");
// https://html.spec.whatwg.org/multipage/#dom-img-ismap
make_bool_getter!(IsMap, "ismap");
// https://html.spec.whatwg.org/multipage/#dom-img-ismap
make_bool_setter!(SetIsMap, "ismap");
// <https://html.spec.whatwg.org/multipage/#dom-img-width>
fn Width(&self) -> u32 {
let node = self.upcast::<Node>();
node.content_box()
.map(|rect| rect.size.width.to_px() as u32)
.unwrap_or_else(|| self.NaturalWidth())
}
// <https://html.spec.whatwg.org/multipage/#dom-img-width>
make_dimension_uint_setter!(SetWidth, "width");
// <https://html.spec.whatwg.org/multipage/#dom-img-height>
fn Height(&self) -> u32 {
let node = self.upcast::<Node>();
node.content_box()
.map(|rect| rect.size.height.to_px() as u32)
.unwrap_or_else(|| self.NaturalHeight())
}
// <https://html.spec.whatwg.org/multipage/#dom-img-height>
make_dimension_uint_setter!(SetHeight, "height");
/// <https://html.spec.whatwg.org/multipage/#dom-img-naturalwidth>
fn NaturalWidth(&self) -> u32 {
let request = self.current_request.borrow();
if matches!(request.state, State::Broken) {
return 0;
}
let pixel_density = request.current_pixel_density.unwrap_or(1f64);
match request.metadata {
Some(ref metadata) => (metadata.width as f64 / pixel_density) as u32,
None => 0,
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-img-naturalheight>
fn NaturalHeight(&self) -> u32 {
let request = self.current_request.borrow();
if matches!(request.state, State::Broken) {
return 0;
}
let pixel_density = request.current_pixel_density.unwrap_or(1f64);
match request.metadata {
Some(ref metadata) => (metadata.height as f64 / pixel_density) as u32,
None => 0,
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-img-complete>
fn Complete(&self) -> bool {
let element = self.upcast::<Element>();
// Step 1. If any of the following are true:
// both the src attribute and the srcset attribute are omitted;
let has_srcset_attribute = element.has_attribute(&local_name!("srcset"));
if !element.has_attribute(&local_name!("src")) && !has_srcset_attribute {
return true;
}
// the srcset attribute is omitted and the src attribute's value is the empty string;
let src = element.get_string_attribute(&local_name!("src"));
if !has_srcset_attribute && src.is_empty() {
return true;
}
// the img element's current request's state is completely available and its pending request
// is null; or the img element's current request's state is broken and its pending request
// is null, then return true.
if matches!(self.image_request.get(), ImageRequestPhase::Current) &&
matches!(
self.current_request.borrow().state,
State::CompletelyAvailable | State::Broken
)
{
return true;
}
// Step 2. Return false.
false
}
/// <https://html.spec.whatwg.org/multipage/#dom-img-currentsrc>
fn CurrentSrc(&self) -> USVString {
let current_request = self.current_request.borrow();
let url = ¤t_request.parsed_url;
match *url {
Some(ref url) => USVString(url.clone().into_string()),
None => {
let unparsed_url = ¤t_request.source_url;
match *unparsed_url {
Some(ref url) => url.clone(),
None => USVString("".to_owned()),
}
},
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-img-referrerpolicy>
fn ReferrerPolicy(&self) -> DOMString {
reflect_referrer_policy_attribute(self.upcast::<Element>())
}
// <https://html.spec.whatwg.org/multipage/#dom-img-referrerpolicy>
make_setter!(SetReferrerPolicy, "referrerpolicy");
/// <https://html.spec.whatwg.org/multipage/#dom-img-decode>
fn Decode(&self, cx: &mut JSContext) -> Rc<Promise> {
// Step 1. Let promise be a new promise.
let promise = Promise::new2(cx, &self.global());
// Step 2. Queue a microtask to perform the following steps:
let task = ImageElementMicrotask::Decode {
elem: DomRoot::from_ref(self),
promise: promise.clone(),
};
ScriptThread::await_stable_state(Microtask::ImageElement(task));
// Step 3. Return promise.
promise
}
// https://html.spec.whatwg.org/multipage/#dom-img-name
make_getter!(Name, "name");
// https://html.spec.whatwg.org/multipage/#dom-img-name
make_atomic_setter!(SetName, "name");
// https://html.spec.whatwg.org/multipage/#dom-img-align
make_getter!(Align, "align");
// https://html.spec.whatwg.org/multipage/#dom-img-align
make_setter!(SetAlign, "align");
// https://html.spec.whatwg.org/multipage/#dom-img-hspace
make_uint_getter!(Hspace, "hspace");
// https://html.spec.whatwg.org/multipage/#dom-img-hspace
make_uint_setter!(SetHspace, "hspace");
// https://html.spec.whatwg.org/multipage/#dom-img-vspace
make_uint_getter!(Vspace, "vspace");
// https://html.spec.whatwg.org/multipage/#dom-img-vspace
make_uint_setter!(SetVspace, "vspace");
// https://html.spec.whatwg.org/multipage/#dom-img-longdesc
make_url_getter!(LongDesc, "longdesc");
// https://html.spec.whatwg.org/multipage/#dom-img-longdesc
make_url_setter!(SetLongDesc, "longdesc");
// https://html.spec.whatwg.org/multipage/#dom-img-border
make_getter!(Border, "border");
// https://html.spec.whatwg.org/multipage/#dom-img-border
make_setter!(SetBorder, "border");
}
impl VirtualMethods for HTMLImageElement {
fn super_type(&self) -> Option<&dyn VirtualMethods> {
Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
}
fn adopting_steps(&self, cx: &mut JSContext, old_doc: &Document) {
self.super_type().unwrap().adopting_steps(cx, old_doc);
self.update_the_image_data(cx);
}
fn attribute_mutated(
&self,
cx: &mut js::context::JSContext,
attr: &Attr,
mutation: AttributeMutation,
) {
self.super_type()
.unwrap()
.attribute_mutated(cx, attr, mutation);
match attr.local_name() {
&local_name!("src") |
&local_name!("srcset") |
&local_name!("width") |
&local_name!("sizes") => {
// <https://html.spec.whatwg.org/multipage/#reacting-to-dom-mutations>
// The element's src, srcset, width, or sizes attributes are set, changed, or
// removed.
self.update_the_image_data(cx);
},
&local_name!("crossorigin") => {
// <https://html.spec.whatwg.org/multipage/#reacting-to-dom-mutations>
// The element's crossorigin attribute's state is changed.
let cross_origin_state_changed = match mutation {
AttributeMutation::Removed | AttributeMutation::Set(None, _) => true,
AttributeMutation::Set(Some(old_value), _) => {
let new_cors_setting =
CorsSettings::from_enumerated_attribute(&attr.value());
let old_cors_setting = CorsSettings::from_enumerated_attribute(old_value);
new_cors_setting != old_cors_setting
},
};
if cross_origin_state_changed {
self.update_the_image_data(cx);
}
},
&local_name!("referrerpolicy") => {
// <https://html.spec.whatwg.org/multipage/#reacting-to-dom-mutations>
// The element's referrerpolicy attribute's state is changed.
let referrer_policy_state_changed = match mutation {
AttributeMutation::Removed | AttributeMutation::Set(None, _) => {
ReferrerPolicy::from(&**attr.value()) != ReferrerPolicy::EmptyString
},
AttributeMutation::Set(Some(old_value), _) => {
ReferrerPolicy::from(&**attr.value()) != ReferrerPolicy::from(&**old_value)
},
};
if referrer_policy_state_changed {
self.update_the_image_data(cx);
}
},
_ => {},
}
}
fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
match attr.local_name() {
&local_name!("width") | &local_name!("height") => true,
_ => self
.super_type()
.unwrap()
.attribute_affects_presentational_hints(attr),
}
}
fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
match name {
&local_name!("width") | &local_name!("height") => {
AttrValue::from_dimension(value.into())
},
&local_name!("hspace") | &local_name!("vspace") => AttrValue::from_u32(value.into(), 0),
_ => self
.super_type()
.unwrap()
.parse_plain_attribute(name, value),
}
}
fn handle_event(&self, cx: &mut js::context::JSContext, event: &Event) {
if event.type_() != atom!("click") {
return;
}
let area_elements = self.areas();
let elements = match area_elements {
Some(x) => x,
None => return,
};
// Fetch click coordinates
let mouse_event = match event.downcast::<MouseEvent>() {
Some(x) => x,
None => return,
};
let point = Point2D::new(
mouse_event.ClientX().to_f32().unwrap(),
mouse_event.ClientY().to_f32().unwrap(),
);
let bcr = self.upcast::<Element>().GetBoundingClientRect(cx);
let bcr_p = Point2D::new(bcr.X() as f32, bcr.Y() as f32);
// Walk HTMLAreaElements
for element in elements {
let shape = element.get_shape_from_coords();
let shp = match shape {
Some(x) => x.absolute_coords(bcr_p),
None => return,
};
if shp.hit_test(&point) {
element.activation_behavior(cx, event, self.upcast());
return;
}
}
}
/// <https://html.spec.whatwg.org/multipage/#the-img-element:html-element-insertion-steps>
fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
if let Some(s) = self.super_type() {
s.bind_to_tree(cx, context);
}
let document = self.owner_document();
if context.tree_connected {
document.register_responsive_image(self);
}
let parent = self.upcast::<Node>().GetParentNode().unwrap();
// Step 1. If insertedNode's parent is a picture element, then, count this as a relevant
// mutation for insertedNode.
if parent.is::<HTMLPictureElement>() && std::ptr::eq(&*parent, context.parent) {
self.update_the_image_data(cx);
}
}
/// <https://html.spec.whatwg.org/multipage/#the-img-element:html-element-removing-steps>
fn unbind_from_tree(&self, cx: &mut js::context::JSContext, context: &UnbindContext) {
self.super_type().unwrap().unbind_from_tree(cx, context);
let document = self.owner_document();
document.unregister_responsive_image(self);
// Step 1. If oldParent is a picture element, then, count this as a relevant mutation for
// removedNode.
if context.parent.is::<HTMLPictureElement>() && !self.upcast::<Node>().has_parent() {
self.update_the_image_data(cx);
}
}
/// <https://html.spec.whatwg.org/multipage#the-img-element:html-element-moving-steps>
fn moving_steps(&self, cx: &mut JSContext, context: &MoveContext) {
if let Some(super_type) = self.super_type() {
super_type.moving_steps(cx, context);
}
// Step 1. If oldParent is a picture element, then, count this as a relevant mutation for movedNode.
if let Some(old_parent) = context.old_parent {
if old_parent.is::<HTMLPictureElement>() {
self.update_the_image_data(cx);
}
}
}
}
impl FormControl for HTMLImageElement {
fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
self.form_owner.get()
}
fn set_form_owner(&self, form: Option<&HTMLFormElement>) {
self.form_owner.set(form);
}
fn to_element(&self) -> &Element {
self.upcast::<Element>()
}
fn is_listed(&self) -> bool {
false
}
}
/// Collect sequence of code points
/// <https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points>
pub(crate) fn collect_sequence_characters(
s: &str,
mut predicate: impl FnMut(&char) -> bool,
) -> (&str, &str) {
let i = s.find(|ch| !predicate(&ch)).unwrap_or(s.len());
(&s[0..i], &s[i..])
}
/// <https://html.spec.whatwg.org/multipage/#valid-non-negative-integer>
/// TODO(#39315): Use the validation rule from Stylo
fn is_valid_non_negative_integer_string(s: &str) -> bool {
s.chars().all(|c| c.is_ascii_digit())
}
/// <https://html.spec.whatwg.org/multipage/#valid-floating-point-number>
/// TODO(#39315): Use the validation rule from Stylo
fn is_valid_floating_point_number_string(s: &str) -> bool {
static RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^-?(?:\d+\.\d+|\d+|\.\d+)(?:(e|E)(\+|\-)?\d+)?$").unwrap());
RE.is_match(s)
}
/// Parse an `srcset` attribute:
/// <https://html.spec.whatwg.org/multipage/#parsing-a-srcset-attribute>.
pub fn parse_a_srcset_attribute(input: &str) -> Vec<ImageSource> {
// > 1. Let input be the value passed to this algorithm.
// > 2. Let position be a pointer into input, initially pointing at the start of the string.
let mut current_index = 0;
// > 3. Let candidates be an initially empty source set.
let mut candidates = vec![];
while current_index < input.len() {
let remaining_string = &input[current_index..];
// > 4. Splitting loop: Collect a sequence of code points that are ASCII whitespace or
// > U+002C COMMA characters from input given position. If any U+002C COMMA
// > characters were collected, that is a parse error.
// NOTE: A parse error indicating a non-fatal mismatch between the input and the
// requirements will be silently ignored to match the behavior of other browsers.
// <https://html.spec.whatwg.org/multipage/#concept-microsyntax-parse-error>
let (collected_characters, string_after_whitespace) =
collect_sequence_characters(remaining_string, |character| {
*character == ',' || character.is_ascii_whitespace()
});
// Add the length of collected whitespace, to find the start of the URL we are going
// to parse.
current_index += collected_characters.len();
// > 5. If position is past the end of input, return candidates.
if string_after_whitespace.is_empty() {
return candidates;
}
// 6. Collect a sequence of code points that are not ASCII whitespace from input
// given position, and let that be url.
let (url, _) =
collect_sequence_characters(string_after_whitespace, |c| !char::is_ascii_whitespace(c));
// Add the length of `url` that we will parse to advance the index of the next part
// of the string to prase.
current_index += url.len();
// 7. Let descriptors be a new empty list.
let mut descriptors = Vec::new();
// > 8. If url ends with U+002C (,), then:
// > 1. Remove all trailing U+002C COMMA characters from url. If this removed
// > more than one character, that is a parse error.
if url.ends_with(',') {
let image_source = ImageSource {
url: url.trim_end_matches(',').into(),
descriptor: Descriptor {
width: None,
density: None,
},
};
candidates.push(image_source);
continue;
}
// Otherwise:
// > 8.1. Descriptor tokenizer: Skip ASCII whitespace within input given position.
let descriptors_string = &input[current_index..];
let (spaces, descriptors_string) =
collect_sequence_characters(descriptors_string, |character| {
character.is_ascii_whitespace()
});
current_index += spaces.len();
// > 8.2. Let current descriptor be the empty string.
let mut current_descriptor = String::new();
// > 8.3. Let state be "in descriptor".
let mut state = ParseState::InDescriptor;
// > 8.4. Let c be the character at position. Do the following depending on the value of
// > state. For the purpose of this step, "EOF" is a special character representing
// > that position is past the end of input.
let mut characters = descriptors_string.chars();
let mut character = characters.next();
if let Some(character) = character {
current_index += character.len_utf8();
}
loop {
match (state, character) {
(ParseState::InDescriptor, Some(character)) if character.is_ascii_whitespace() => {
// > If current descriptor is not empty, append current descriptor to
// > descriptors and let current descriptor be the empty string. Set
// > state to after descriptor.
if !current_descriptor.is_empty() {
descriptors.push(current_descriptor);
current_descriptor = String::new();
state = ParseState::AfterDescriptor;
}
},
(ParseState::InDescriptor, Some(',')) => {
// > Advance position to the next character in input. If current descriptor
// > is not empty, append current descriptor to descriptors. Jump to the
// > step labeled descriptor parser.
if !current_descriptor.is_empty() {
descriptors.push(current_descriptor);
}
break;
},
(ParseState::InDescriptor, Some('(')) => {
// > Append c to current descriptor. Set state to in parens.
current_descriptor.push('(');
state = ParseState::InParens;
},
(ParseState::InDescriptor, Some(character)) => {
// > Append c to current descriptor.
current_descriptor.push(character);
},
(ParseState::InDescriptor, None) => {
// > If current descriptor is not empty, append current descriptor to
// > descriptors. Jump to the step labeled descriptor parser.
if !current_descriptor.is_empty() {
descriptors.push(current_descriptor);
}
break;
},
(ParseState::InParens, Some(')')) => {
// > Append c to current descriptor. Set state to in descriptor.
current_descriptor.push(')');
state = ParseState::InDescriptor;
},
(ParseState::InParens, Some(character)) => {
// Append c to current descriptor.
current_descriptor.push(character);
},
(ParseState::InParens, None) => {
// > Append current descriptor to descriptors. Jump to the step
// > labeled descriptor parser.
descriptors.push(current_descriptor);
break;
},
(ParseState::AfterDescriptor, Some(character))
if character.is_ascii_whitespace() =>
{
// > Stay in this state.
},
(ParseState::AfterDescriptor, Some(_)) => {
// > Set state to in descriptor. Set position to the previous
// > character in input.
state = ParseState::InDescriptor;
continue;
},
(ParseState::AfterDescriptor, None) => {
// > Jump to the step labeled descriptor parser.
break;
},
}
character = characters.next();
if let Some(character) = character {
current_index += character.len_utf8();
}
}
// > 9. Descriptor parser: Let error be no.
let mut error = false;
// > 10. Let width be absent.
let mut width: Option<u32> = None;
// > 11. Let density be absent.
let mut density: Option<f64> = None;
// > 12. Let future-compat-h be absent.
let mut future_compat_h: Option<u32> = None;
// > 13. For each descriptor in descriptors, run the appropriate set of steps from
// > the following list:
for descriptor in descriptors.into_iter() {
let Some(last_character) = descriptor.chars().last() else {
break;
};
let first_part_of_string = &descriptor[0..descriptor.len() - last_character.len_utf8()];
match last_character {
// > If the descriptor consists of a valid non-negative integer followed by a
// > U+0077 LATIN SMALL LETTER W character
// > 1. If the user agent does not support the sizes attribute, let error be yes.
// > 2. If width and density are not both absent, then let error be yes.
// > 3. Apply the rules for parsing non-negative integers to the descriptor.
// > If the result is 0, let error be yes. Otherwise, let width be the result.
'w' if is_valid_non_negative_integer_string(first_part_of_string) &&
density.is_none() &&
width.is_none() =>
{
match parse_unsigned_integer(first_part_of_string.chars()) {
Ok(number) if number > 0 => {
width = Some(number);
continue;
},
_ => error = true,
}
},
// > If the descriptor consists of a valid floating-point number followed by a
// > U+0078 LATIN SMALL LETTER X character
// > 1. If width, density and future-compat-h are not all absent, then let
// > error be yes.
// > 2. Apply the rules for parsing floating-point number values to the
// > descriptor. If the result is less than 0, let error be yes. Otherwise, let
// > density be the result.
//
// The HTML specification has a procedure for parsing floats that is different enough from
// the one that stylo uses, that it's better to use Rust's float parser here. This is
// what Gecko does, but it also checks to see if the number is a valid HTML-spec compliant
// number first. Not doing that means that we might be parsing numbers that otherwise
// wouldn't parse.
'x' if is_valid_floating_point_number_string(first_part_of_string) &&
width.is_none() &&
density.is_none() &&
future_compat_h.is_none() =>
{
match first_part_of_string.parse::<f64>() {
Ok(number) if number.is_finite() && number >= 0. => {
density = Some(number);
continue;
},
_ => error = true,
}
},
// > If the descriptor consists of a valid non-negative integer followed by a
// > U+0068 LATIN SMALL LETTER H character
// > This is a parse error.
// > 1. If future-compat-h and density are not both absent, then let error be
// > yes.
// > 2. Apply the rules for parsing non-negative integers to the descriptor.
// > If the result is 0, let error be yes. Otherwise, let future-compat-h be the
// > result.
'h' if is_valid_non_negative_integer_string(first_part_of_string) &&
future_compat_h.is_none() &&
density.is_none() =>
{
match parse_unsigned_integer(first_part_of_string.chars()) {
Ok(number) if number > 0 => {
future_compat_h = Some(number);
continue;
},
_ => error = true,
}
},
// > Anything else
// > Let error be yes.
_ => error = true,
}
if error {
break;
}
}
// > 14. If future-compat-h is not absent and width is absent, let error be yes.
if future_compat_h.is_some() && width.is_none() {
error = true;
}
// Step 15. If error is still no, then append a new image source to candidates whose URL is
// url, associated with a width width if not absent and a pixel density density if not
// absent. Otherwise, there is a parse error.
if !error {
let image_source = ImageSource {
url: url.into(),
descriptor: Descriptor { width, density },
};
candidates.push(image_source);
}
// Step 16. Return to the step labeled splitting loop.
}
candidates
}
#[derive(Clone)]
enum ChangeType {
Environment {
selected_source: USVString,
selected_pixel_density: f64,
},
Element,
}
/// Returns true if the given image MIME type is supported.
fn is_supported_image_mime_type(input: &str) -> bool {
// Remove any leading and trailing HTTP whitespace from input.
let mime_type = input.trim();
// <https://mimesniff.spec.whatwg.org/#mime-type-essence>
let mime_type_essence = match mime_type.find(';') {
Some(semi) => &mime_type[..semi],
_ => mime_type,
};
// The HTML specification says the type attribute may be present and if present, the value
// must be a valid MIME type string. However an empty type attribute is implicitly supported
// to match the behavior of other browsers.
// <https://html.spec.whatwg.org/multipage/#attr-source-type>
if mime_type_essence.is_empty() {
return true;
}
SUPPORTED_IMAGE_MIME_TYPES.contains(&mime_type_essence)
}