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
use crate::{
html::grammar::{tokenizer::TokenizerState, NodeOrMarker},
xpath::grammar::{
data_model::DoctypeNode,
XpathItemTreeNode,
},
};
use super::{
chars,
tokenizer::{HtmlToken, Parser, TagToken, TagTokenType},
Acknowledgement, HtmlParseError, HtmlParser, HtmlParserError, InsertionMode, QuirksMode,
HTML_NAMESPACE,
};
pub(crate) mod in_body_insertion_mode;
pub(crate) mod in_foreign_content;
/// Determine the quirks mode from a DOCTYPE token per WHATWG 13.2.6.4.1.
fn determine_quirks_mode(
force_quirks: bool,
name: &str,
public_id: Option<&str>,
system_id: Option<&str>,
) -> QuirksMode {
if force_quirks || name != "html" {
return QuirksMode::Quirks;
}
let public_lower = public_id.map(|s| s.to_ascii_lowercase());
let public_lower_ref = public_lower.as_deref();
// Exact public identifier matches that trigger quirks mode.
const QUIRKY_PUBLIC_MATCHES: &[&str] = &[
"-//w3o//dtd w3 html strict 3.0//en//",
"-/w3c/dtd html 4.0 transitional/en",
"html",
];
if let Some(pid) = public_lower_ref {
if QUIRKY_PUBLIC_MATCHES.contains(&pid) {
return QuirksMode::Quirks;
}
}
// Exact system identifier match that triggers quirks mode.
if let Some(sid) = system_id {
if sid.eq_ignore_ascii_case("http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
return QuirksMode::Quirks;
}
}
// Public identifier prefixes that trigger quirks mode (case-insensitive).
const QUIRKY_PUBLIC_PREFIXES: &[&str] = &[
"+//silmaril//dtd html pro v0r11 19970101//",
"-//as//dtd html 3.0 aswedit + extensions//",
"-//advasoft ltd//dtd html 3.0 aswedit + extensions//",
"-//ietf//dtd html 2.0 level 1//",
"-//ietf//dtd html 2.0 level 2//",
"-//ietf//dtd html 2.0 strict level 1//",
"-//ietf//dtd html 2.0 strict level 2//",
"-//ietf//dtd html 2.0 strict//",
"-//ietf//dtd html 2.0//",
"-//ietf//dtd html 2.1e//",
"-//ietf//dtd html 3.0//",
"-//ietf//dtd html 3.2 final//",
"-//ietf//dtd html 3.2//",
"-//ietf//dtd html 3//",
"-//ietf//dtd html level 0//",
"-//ietf//dtd html level 1//",
"-//ietf//dtd html level 2//",
"-//ietf//dtd html level 3//",
"-//ietf//dtd html strict level 0//",
"-//ietf//dtd html strict level 1//",
"-//ietf//dtd html strict level 2//",
"-//ietf//dtd html strict level 3//",
"-//ietf//dtd html strict//",
"-//ietf//dtd html//",
"-//metrius//dtd metrius presentational//",
"-//microsoft//dtd internet explorer 2.0 html strict//",
"-//microsoft//dtd internet explorer 2.0 html//",
"-//microsoft//dtd internet explorer 2.0 tables//",
"-//microsoft//dtd internet explorer 3.0 html strict//",
"-//microsoft//dtd internet explorer 3.0 html//",
"-//microsoft//dtd internet explorer 3.0 tables//",
"-//netscape comm. corp.//dtd html//",
"-//netscape comm. corp.//dtd strict html//",
"-//o'reilly and associates//dtd html 2.0//",
"-//o'reilly and associates//dtd html extended 1.0//",
"-//o'reilly and associates//dtd html extended relaxed 1.0//",
"-//sq//dtd html 2.0 hotmetal + extensions//",
"-//softquad software//dtd hotmetal pro 6.0::19990601::extensions to html 4.0//",
"-//softquad//dtd hotmetal pro 4.0::19971010::extensions to html 4.0//",
"-//spyglass//dtd html 2.0 extended//",
"-//sun microsystems corp.//dtd hotjava html//",
"-//sun microsystems corp.//dtd hotjava strict html//",
"-//w3c//dtd html 3 1995-03-24//",
"-//w3c//dtd html 3.2 draft//",
"-//w3c//dtd html 3.2 final//",
"-//w3c//dtd html 3.2//",
"-//w3c//dtd html 3.2s draft//",
"-//w3c//dtd html 4.0 frameset//",
"-//w3c//dtd html 4.0 transitional//",
"-//w3c//dtd html experimental 19960712//",
"-//w3c//dtd html experimental 970421//",
"-//w3c//dtd w3 html//",
"-//w3o//dtd w3 html 3.0//",
"-//webtechs//dtd mozilla html 2.0//",
"-//webtechs//dtd mozilla html//",
];
if let Some(pid) = public_lower_ref {
if QUIRKY_PUBLIC_PREFIXES.iter().any(|prefix| pid.starts_with(prefix)) {
return QuirksMode::Quirks;
}
}
// Public identifier prefixes that trigger quirks if system identifier is missing.
const HTML4_PUBLIC_PREFIXES: &[&str] = &[
"-//w3c//dtd html 4.01 frameset//",
"-//w3c//dtd html 4.01 transitional//",
];
if system_id.is_none() {
if let Some(pid) = public_lower_ref {
if HTML4_PUBLIC_PREFIXES.iter().any(|prefix| pid.starts_with(prefix)) {
return QuirksMode::Quirks;
}
}
}
// Limited quirks mode checks.
const LIMITED_QUIRKY_PUBLIC_PREFIXES: &[&str] = &[
"-//w3c//dtd xhtml 1.0 frameset//",
"-//w3c//dtd xhtml 1.0 transitional//",
];
if let Some(pid) = public_lower_ref {
if LIMITED_QUIRKY_PUBLIC_PREFIXES.iter().any(|prefix| pid.starts_with(prefix)) {
return QuirksMode::LimitedQuirks;
}
}
// HTML 4.01 Frameset/Transitional with system identifier → limited quirks.
if system_id.is_some() {
if let Some(pid) = public_lower_ref {
if HTML4_PUBLIC_PREFIXES.iter().any(|prefix| pid.starts_with(prefix)) {
return QuirksMode::LimitedQuirks;
}
}
}
QuirksMode::NoQuirks
}
impl HtmlParser {
/// <https://html.spec.whatwg.org/multipage/parsing.html#the-initial-insertion-mode>
pub(super) fn initial_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
HtmlToken::Character(
c @ (chars::CHARACTER_TABULATION
| chars::LINE_FEED
| chars::FORM_FEED
| chars::CARRIAGE_RETURN
| chars::SPACE),
) => {
// WHATWG says ignore, but we preserve for round-trip fidelity
self.insert_character_at_document_level(c)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
HtmlToken::Comment(comment) => {
// Insert a comment as the last child of the Document object.
let parent = self
.root_node
.ok_or(HtmlParseError::new("root node is None"))?;
self.insert_a_comment(comment, Some(parent))?;
}
HtmlToken::DocType(doctype) => {
// Determine quirks mode per WHATWG 13.2.6.4.1.
self.quirks_mode = determine_quirks_mode(
doctype.force_quirks,
&doctype.name,
doctype.public_identifier.as_deref(),
doctype.system_identifier.as_deref(),
);
// Append a DocumentType node to the Document node.
let doctype_id = DoctypeNode::create(
doctype.name,
doctype.public_identifier,
doctype.system_identifier,
&mut self.arena,
);
self.root_node
.ok_or(HtmlParseError::new("root node is None"))?
.append(doctype_id, &mut self.arena);
self.insertion_mode = InsertionMode::BeforeHtml;
}
_ => {
// WHATWG 13.2.6.4.1: If the document is not an iframe srcdoc document,
// this is a parse error; set the Document to quirks mode.
self.quirks_mode = QuirksMode::Quirks;
self.insertion_mode = InsertionMode::BeforeHtml;
self.token_emitted(token)?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#the-before-html-insertion-mode>
pub(super) fn before_html_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
fn anything_else(parser: &mut HtmlParser, token: HtmlToken) -> Result<(), HtmlParseError> {
let result = parser.create_element(String::from("html"), HTML_NAMESPACE)?;
// append the node to the document
let node_id = parser.new_node(XpathItemTreeNode::ElementNode(result));
parser
.root_node
.ok_or(HtmlParseError::new("root node is None"))?
.append(node_id, &mut parser.arena);
parser.open_elements.push(node_id);
parser.insertion_mode = InsertionMode::BeforeHead;
parser.token_emitted(token)?;
Ok(())
}
match token {
HtmlToken::DocType(_) => {
// parse error, ignore the token
}
HtmlToken::Comment(token) => {
let parent = self
.root_node
.ok_or(HtmlParseError::new("root node is None"))?;
self.insert_a_comment(token, Some(parent))?;
}
HtmlToken::Character(
c @ (chars::CHARACTER_TABULATION
| chars::LINE_FEED
| chars::FORM_FEED
| chars::CARRIAGE_RETURN
| chars::SPACE),
) => {
// WHATWG says ignore, but we preserve for round-trip fidelity
self.insert_character_at_document_level(c)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
let result = self.create_an_element_for_the_token(token, HTML_NAMESPACE)?;
// insert the result
let node_id = self.insert_create_an_element_for_the_token_result(result)?;
// append it to the document
self.root_node
.ok_or(HtmlParseError::new("root node is None"))?
.append(node_id, &mut self.arena);
self.insertion_mode = InsertionMode::BeforeHead;
}
HtmlToken::TagToken(TagTokenType::EndTag(TagToken { tag_name, .. }))
if ["head", "body", "html", "br"].contains(&tag_name.as_ref()) =>
{
anything_else(
self,
HtmlToken::TagToken(TagTokenType::EndTag(TagToken::new(tag_name))),
)?;
}
HtmlToken::TagToken(TagTokenType::EndTag(_)) => {
// parse error, ignore the token
}
_ => {
anything_else(self, token)?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#the-before-head-insertion-mode>
pub(super) fn before_head_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
fn anything_else(parser: &mut HtmlParser, token: HtmlToken) -> Result<(), HtmlParseError> {
let node_id = parser.insert_an_html_element(TagToken::new(String::from("head")))?;
parser.head_element_pointer = Some(node_id);
parser.insertion_mode = InsertionMode::InHead;
parser.token_emitted(token)?;
Ok(())
}
match token {
HtmlToken::Character(
c @ (chars::CHARACTER_TABULATION
| chars::LINE_FEED
| chars::FORM_FEED
| chars::CARRIAGE_RETURN
| chars::SPACE),
) => {
// WHATWG says ignore, but we preserve for round-trip fidelity
self.insert_character(c)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
HtmlToken::Comment(comment) => {
self.insert_a_comment(comment, None)?;
}
HtmlToken::DocType(_) => {
// parse error, ignore the token
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
// process the token using the rules for the "in body" insertion mode
self.using_the_rules_for(
HtmlToken::TagToken(TagTokenType::StartTag(token)),
InsertionMode::InBody,
)?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "head" => {
let node_id = self.insert_an_html_element(token)?;
self.head_element_pointer = Some(node_id);
self.insertion_mode = InsertionMode::InHead;
}
HtmlToken::TagToken(TagTokenType::EndTag(token))
if ["head", "body", "html", "br"].contains(&token.tag_name.as_ref()) =>
{
anything_else(self, HtmlToken::TagToken(TagTokenType::EndTag(token)))?;
}
HtmlToken::TagToken(TagTokenType::EndTag(_)) => {
// parse error, ignore the token
}
_ => anything_else(self, token)?,
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead>
pub(super) fn in_head_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
fn anything_else(parser: &mut HtmlParser, token: HtmlToken) -> Result<(), HtmlParseError> {
parser
.open_elements
.pop()
.ok_or(HtmlParseError::new("open elements is empty"))?;
parser.insertion_mode = InsertionMode::AfterHead;
parser.token_emitted(token)?;
Ok(())
}
match token {
HtmlToken::Character(c)
if chars::WHITESPACE_CHARS.contains(&c) =>
{
self.insert_character(c)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
HtmlToken::Comment(comment) => {
self.insert_a_comment(comment, None)?;
}
HtmlToken::DocType(_) => {
// parse error, ignore the token
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
// process the token using the rules for the "in body" insertion mode
self.using_the_rules_for(
HtmlToken::TagToken(TagTokenType::StartTag(token)),
InsertionMode::InBody,
)?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["base", "basefont", "bgsound", "link"].contains(&token.tag_name.as_str()) =>
{
self.insert_an_html_element(token)?;
self.open_elements
.pop()
.ok_or(HtmlParseError::new("open elements is empty"))?;
// acknowledge the self closing tag
return Ok(Acknowledgement::yes());
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "meta" => {
self.insert_an_html_element(token)?;
self.open_elements
.pop()
.ok_or(HtmlParseError::new("open elements is empty"))?;
// Encoding detection from <meta> charset/http-equiv is not applicable:
// Skyscraper operates on already-decoded &str input, not raw bytes.
// acknowledge the self closing tag
return Ok(Acknowledgement::yes());
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "title" => {
return self.generic_rcdata_element_parsing_algorithm(token);
}
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["noframes", "style"].contains(&token.tag_name.as_str()) =>
{
return self.generic_raw_text_element_parsing_algorithm(token);
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "noscript" => {
// Scripting is disabled in Skyscraper, so:
// Insert an HTML element for the token.
self.insert_an_html_element(token)?;
// Switch the insertion mode to "in head noscript".
self.insertion_mode = InsertionMode::InHeadNoscript;
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "script" => {
let _node = self.insert_an_html_element(token)?;
// Script preparation, execution, and template handling are intentionally
// not implemented — Skyscraper is a non-scripting parser.
self.original_insertion_mode = Some(self.insertion_mode);
self.insertion_mode = InsertionMode::Text;
// set tokenizer state to script data state
return Ok(Acknowledgement {
self_closed: false,
tokenizer_state: Some(TokenizerState::ScriptData),
});
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "head" => {
self.open_elements
.pop()
.ok_or(HtmlParseError::new("open elements is empty"))?;
self.insertion_mode = InsertionMode::AfterHead;
}
HtmlToken::TagToken(TagTokenType::EndTag(token))
if ["body", "html", "br"].contains(&token.tag_name.as_str()) =>
{
anything_else(self, HtmlToken::TagToken(TagTokenType::EndTag(token)))?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "template" => {
self.active_formatting_elements.push(NodeOrMarker::Marker);
self.frameset_ok = false;
self.insertion_mode = InsertionMode::InTemplate;
self.template_insertion_modes
.push(InsertionMode::InTemplate);
// Declarative shadow DOM is not supported, so shadowrootmode is
// always in the None state. Per WHATWG step 9, when any of the
// three conditions is false we simply insert an HTML element.
self.insert_an_html_element(token)?;
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "template" => {
if !self.open_elements_has_element("template") {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected template end tag",
)))?;
return Ok(Acknowledgement::no());
}
self.generate_all_implied_end_tags_thoroughly()?;
let current_node = self.current_node_as_element_result()?;
if current_node.name != "template" {
self.handle_error(HtmlParserError::MinorError(String::from(
"template end tag not found",
)))?;
}
self.pop_until_tag_name("template")?;
self.clear_the_list_of_active_formatting_elements_up_to_the_last_marker()?;
self.template_insertion_modes.pop();
self.reset_the_insertion_mode_appropriately()?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "head" => {
// parse error, ignore the token
}
HtmlToken::TagToken(TagTokenType::EndTag(_)) => {
// Any other end tag: parse error, ignore the token.
}
_ => {
anything_else(self, token)?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inheadnoscript>
pub(super) fn in_head_noscript_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
fn anything_else(parser: &mut HtmlParser, token: HtmlToken) -> Result<(), HtmlParseError> {
// Parse error.
// Pop the current node (which will be a noscript element) from the stack of open
// elements; the new current node will be a head element.
parser
.open_elements
.pop()
.ok_or(HtmlParseError::new("open elements is empty"))?;
// Switch the insertion mode to "in head".
parser.insertion_mode = InsertionMode::InHead;
// Reprocess the token.
parser.token_emitted(token)?;
Ok(())
}
match token {
// A DOCTYPE token: Parse error. Ignore the token.
HtmlToken::DocType(_) => {
// parse error, ignore
}
// A comment token: Process the token using the rules for the "in head" insertion mode.
HtmlToken::Comment(comment) => {
return self.in_head_insertion_mode(HtmlToken::Comment(comment));
}
// A character token that is one of U+0009, U+000A, U+000C, U+000D, or U+0020:
// Process the token using the rules for the "in head" insertion mode.
HtmlToken::Character(c)
if chars::WHITESPACE_CHARS.contains(&c) =>
{
return self.in_head_insertion_mode(HtmlToken::Character(c));
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
// A start tag whose tag name is "html":
// Process the token using the rules for the "in body" insertion mode.
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
return self
.in_body_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
// An end tag whose tag name is "noscript":
// Pop the current node (noscript) from the stack of open elements;
// the new current node will be a head element.
// Switch the insertion mode to "in head".
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "noscript" => {
self.open_elements
.pop()
.ok_or(HtmlParseError::new("open elements is empty"))?;
self.insertion_mode = InsertionMode::InHead;
}
// A start tag whose tag name is one of: "basefont", "bgsound", "link", "meta",
// "noframes", "style":
// Process the token using the rules for the "in head" insertion mode.
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["basefont", "bgsound", "link", "meta", "noframes", "style"]
.contains(&token.tag_name.as_str()) =>
{
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
// An end tag whose tag name is "br":
// Act as described in the "anything else" entry below.
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "br" => {
anything_else(self, HtmlToken::TagToken(TagTokenType::EndTag(token)))?;
}
// A start tag whose tag name is one of: "head", "noscript":
// Parse error. Ignore the token.
HtmlToken::TagToken(TagTokenType::StartTag(_token))
if ["head", "noscript"].contains(&_token.tag_name.as_str()) =>
{
// parse error, ignore
}
// Any other end tag: Parse error. Ignore the token.
HtmlToken::TagToken(TagTokenType::EndTag(_)) => {
// parse error, ignore
}
// Anything else: Parse error.
// Pop the current node (noscript), switch to "in head", reprocess.
_ => {
anything_else(self, token)?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#the-after-head-insertion-mode>
pub(super) fn after_head_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
fn anything_else(parser: &mut HtmlParser, token: HtmlToken) -> Result<(), HtmlParseError> {
parser.insert_an_html_element(TagToken::new(String::from("body")))?;
parser.insertion_mode = InsertionMode::InBody;
parser.token_emitted(token)?;
Ok(())
}
match token {
HtmlToken::Character(c)
if chars::WHITESPACE_CHARS.contains(&c) =>
{
self.insert_character(c)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
HtmlToken::Comment(comment) => {
// A comment token: Insert a comment.
self.insert_a_comment(comment, None)?;
}
HtmlToken::DocType(_) => {
// A DOCTYPE token: Parse error. Ignore the token.
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
// Process the token using the rules for the "in body" insertion mode.
return self
.in_body_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "body" => {
self.insert_an_html_element(token)?;
self.frameset_ok = false;
self.insertion_mode = InsertionMode::InBody;
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "frameset" => {
self.insert_an_html_element(token)?;
self.insertion_mode = InsertionMode::InFrameset;
}
HtmlToken::TagToken(TagTokenType::StartTag(token))
if [
"base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style",
"template", "title",
]
.contains(&token.tag_name.as_str()) =>
{
// Parse error.
// Push the node pointed to by the head element pointer onto the stack of open
// elements.
let head_node_id = self
.head_element_pointer
.ok_or(HtmlParseError::new("head element pointer is None"))?;
self.open_elements.push(head_node_id);
// Process the token using the rules for the "in head" insertion mode.
let ack =
self.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
// Remove the node pointed to by the head element pointer from the stack of open
// elements. (It might not be the current node at this point.)
self.open_elements.retain(|&id| id != head_node_id);
return ack;
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "template" => {
// Process the token using the rules for the "in head" insertion mode.
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::EndTag(token)));
}
HtmlToken::TagToken(TagTokenType::EndTag(token))
if ["body", "html", "br"].contains(&token.tag_name.as_str()) =>
{
anything_else(self, HtmlToken::TagToken(TagTokenType::EndTag(token)))?;
}
HtmlToken::TagToken(TagTokenType::StartTag(_token)) if _token.tag_name == "head" => {
// A start tag whose tag name is "head": Parse error. Ignore the token.
}
HtmlToken::TagToken(TagTokenType::EndTag(_)) => {
// Any other end tag: Parse error. Ignore the token.
}
_ => {
anything_else(self, token)?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incdata>
pub(super) fn text_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
HtmlToken::Character(c) => {
self.insert_character(c)?;
}
HtmlToken::Characters(ref s) => {
self.insert_characters(s)?;
}
HtmlToken::EndOfFile => {
// Parse error.
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected end-of-file in text insertion mode",
)))?;
// If the current node is a script element, then set its
// "already started" flag. (Scripting is not supported.)
// Pop the current node off the stack of open elements.
self.open_elements
.pop()
.ok_or(HtmlParseError::new("open elements is empty"))?;
// Switch the insertion mode to the original insertion mode.
self.insertion_mode = self
.original_insertion_mode
.ok_or(HtmlParseError::new("original insertion mode is None"))?;
// Reprocess the token.
self.token_emitted(HtmlToken::EndOfFile)?;
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "script" => {
let _script = self.current_node_as_element_result()?;
self.open_elements
.pop()
.ok_or(HtmlParseError::new("open elements is empty"))?;
self.insertion_mode = self
.original_insertion_mode
.ok_or(HtmlParseError::new("original insertion mode is None"))?;
// lots of unsupported scripting logic would go here
// it is intentionally not included
}
HtmlToken::TagToken(TagTokenType::EndTag(_token)) => {
self.open_elements
.pop()
.ok_or(HtmlParseError::new("open elements is empty"))?;
self.insertion_mode = self
.original_insertion_mode
.ok_or(HtmlParseError::new("original insertion mode is None"))?;
}
_ => {
// ignore
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intemplate>
pub(super) fn in_template_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
HtmlToken::Character(_)
| HtmlToken::Characters(_)
| HtmlToken::Comment(_)
| HtmlToken::DocType(_) => {
self.using_the_rules_for(token, InsertionMode::InBody)?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token))
if [
"base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style",
"template", "title",
]
.contains(&token.tag_name.as_str()) =>
{
self.using_the_rules_for(
HtmlToken::TagToken(TagTokenType::StartTag(token)),
InsertionMode::InHead,
)?;
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "template" => {
self.using_the_rules_for(
HtmlToken::TagToken(TagTokenType::EndTag(token)),
InsertionMode::InHead,
)?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["caption", "colgroup", "tbody", "tfoot", "thead"]
.contains(&token.tag_name.as_str()) =>
{
self.template_insertion_modes.pop();
self.template_insertion_modes.push(InsertionMode::InTable);
self.insertion_mode = InsertionMode::InTable;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["col"].contains(&token.tag_name.as_str()) =>
{
self.template_insertion_modes.pop();
self.template_insertion_modes
.push(InsertionMode::InColumnGroup);
self.insertion_mode = InsertionMode::InColumnGroup;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["tr"].contains(&token.tag_name.as_str()) =>
{
self.template_insertion_modes.pop();
self.template_insertion_modes
.push(InsertionMode::InTableBody);
self.insertion_mode = InsertionMode::InTableBody;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["td", "th"].contains(&token.tag_name.as_str()) =>
{
self.template_insertion_modes.pop();
self.template_insertion_modes.push(InsertionMode::InRow);
self.insertion_mode = InsertionMode::InRow;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) => {
self.template_insertion_modes.pop();
self.template_insertion_modes.push(InsertionMode::InBody);
self.insertion_mode = InsertionMode::InBody;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
HtmlToken::TagToken(TagTokenType::EndTag(_token)) => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected end tag",
)))?;
}
HtmlToken::EndOfFile => {
if !self.open_elements_has_element("template") {
self.stop_parsing()?;
return Ok(Acknowledgement::no());
}
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected end of file",
)))?;
self.pop_until_tag_name("template")?;
self.clear_the_list_of_active_formatting_elements_up_to_the_last_marker()?;
self.template_insertion_modes.pop();
self.reset_the_insertion_mode_appropriately()?;
self.token_emitted(token)?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-afterbody>
pub(super) fn after_body_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
HtmlToken::Character(c)
if chars::WHITESPACE_CHARS.contains(&c) =>
{
// WHATWG says use InBody rules, but we insert as a child of
// the html element (after body) for round-trip fidelity.
let html_node = *self.open_elements.first().ok_or(
HtmlParseError::new("no elements on open elements stack"),
)?;
self.insert_character_at_node(html_node, c)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
HtmlToken::Comment(comment) => {
// Insert a comment as the last child of the first element in
// the stack of open elements (the html element).
let html_node = *self.open_elements.first().ok_or(
HtmlParseError::new("no elements on open elements stack"),
)?;
self.insert_a_comment(comment, Some(html_node))?;
}
HtmlToken::DocType(_) => {
// Parse error. Ignore the token.
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected DOCTYPE after body",
)))?;
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
self.using_the_rules_for(
HtmlToken::TagToken(TagTokenType::StartTag(token)),
InsertionMode::InBody,
)?;
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "html" => {
if self.is_fragment_parser() {
// Parse error. Ignore the token.
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected </html> end tag in fragment parser after body",
)))?;
} else {
self.insertion_mode = InsertionMode::AfterAfterBody;
}
}
HtmlToken::EndOfFile => {
self.stop_parsing()?;
}
_ => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected token after body",
)))?;
self.insertion_mode = InsertionMode::InBody;
self.token_emitted(token)?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#the-after-after-body-insertion-mode>
pub(super) fn after_after_body_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
HtmlToken::Comment(comment) => {
// Insert a comment as the last child of the Document object.
let parent = self
.root_node
.ok_or(HtmlParseError::new("root node is None"))?;
self.insert_a_comment(comment, Some(parent))?;
}
HtmlToken::DocType(_) => {
self.using_the_rules_for(token, InsertionMode::InBody)?;
}
HtmlToken::Character(c)
if chars::WHITESPACE_CHARS.contains(&c) =>
{
// WHATWG says use InBody rules, but we preserve at document
// level for round-trip fidelity (whitespace after </html>).
self.insert_character_at_document_level(c)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
self.using_the_rules_for(
HtmlToken::TagToken(TagTokenType::StartTag(token)),
InsertionMode::InBody,
)?;
}
HtmlToken::EndOfFile => {
self.stop_parsing()?;
}
_ => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected token after after body",
)))?;
self.insertion_mode = InsertionMode::InBody;
self.token_emitted(token)?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inframeset>
pub(super) fn in_frameset_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// A character token that is one of U+0009, U+000A, U+000C, U+000D, or U+0020
HtmlToken::Character(c)
if chars::WHITESPACE_CHARS.contains(&c) =>
{
self.insert_character(c)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
// A comment token
HtmlToken::Comment(comment) => {
self.insert_a_comment(comment, None)?;
}
// A DOCTYPE token: parse error, ignore
HtmlToken::DocType(_) => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected DOCTYPE in frameset",
)))?;
}
// A start tag whose tag name is "html"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
return self
.in_body_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
// A start tag whose tag name is "frameset"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if token.tag_name == "frameset" =>
{
self.insert_an_html_element(token)?;
}
// An end tag whose tag name is "frameset"
HtmlToken::TagToken(TagTokenType::EndTag(token))
if token.tag_name == "frameset" =>
{
// If the current node is the root html element, this is a parse error; ignore.
let is_root = self.open_elements.len() == 1;
if is_root {
self.handle_error(HtmlParserError::MinorError(String::from(
"frameset end tag at root html element",
)))?;
} else {
// Pop the current node from the stack of open elements.
self.open_elements.pop();
// If the parser was not created as part of the HTML fragment parsing algorithm
// (fragment case), and the current node is no longer a frameset element, then
// switch the insertion mode to "after frameset".
if !self.is_fragment_parser() {
let is_frameset = self
.current_node_as_element()
.map(|el| el.name == "frameset")
.unwrap_or(false);
if !is_frameset {
self.insertion_mode = InsertionMode::AfterFrameset;
}
}
}
}
// A start tag whose tag name is "frame"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "frame" => {
self.insert_an_html_element(token)?;
// Immediately pop the current node off the stack of open elements.
self.open_elements.pop();
// Acknowledge the token's self-closing flag, if it is set.
return Ok(Acknowledgement::yes());
}
// A start tag whose tag name is "noframes"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if token.tag_name == "noframes" =>
{
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
// An end-of-file token
HtmlToken::EndOfFile => {
if self.open_elements.len() > 1 {
// If the current node is not the root html element, this is a parse error.
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected EOF in frameset",
)))?;
}
self.stop_parsing()?;
}
// Anything else: parse error, ignore the token.
_ => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected token in frameset",
)))?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-afterframeset>
pub(super) fn after_frameset_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// A character token that is one of U+0009, U+000A, U+000C, U+000D, or U+0020
HtmlToken::Character(c)
if chars::WHITESPACE_CHARS.contains(&c) =>
{
self.insert_character(c)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
// A comment token
HtmlToken::Comment(comment) => {
self.insert_a_comment(comment, None)?;
}
// A DOCTYPE token: parse error, ignore
HtmlToken::DocType(_) => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected DOCTYPE after frameset",
)))?;
}
// A start tag whose tag name is "html"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
return self
.in_body_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
// An end tag whose tag name is "html"
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "html" => {
self.insertion_mode = InsertionMode::AfterAfterFrameset;
}
// A start tag whose tag name is "noframes"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if token.tag_name == "noframes" =>
{
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
// An end-of-file token
HtmlToken::EndOfFile => {
self.stop_parsing()?;
}
// Anything else: parse error, ignore the token.
_ => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected token after frameset",
)))?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#the-after-after-frameset-insertion-mode>
pub(super) fn after_after_frameset_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// A comment token: insert a comment as the last child of the Document object.
HtmlToken::Comment(comment) => {
let parent = self
.root_node
.ok_or(HtmlParseError::new("root node is None"))?;
self.insert_a_comment(comment, Some(parent))?;
}
// A DOCTYPE token: process using the rules for the "in body" insertion mode.
HtmlToken::DocType(_) => {
self.using_the_rules_for(token, InsertionMode::InBody)?;
}
// A character token that is one of U+0009, U+000A, U+000C, U+000D, or U+0020:
// process using the rules for the "in body" insertion mode.
HtmlToken::Character(c)
if chars::WHITESPACE_CHARS.contains(&c) =>
{
self.using_the_rules_for(HtmlToken::Character(c), InsertionMode::InBody)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
// A start tag whose tag name is "html"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
self.using_the_rules_for(
HtmlToken::TagToken(TagTokenType::StartTag(token)),
InsertionMode::InBody,
)?;
}
// A start tag whose tag name is "noframes"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if token.tag_name == "noframes" =>
{
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
// An end-of-file token
HtmlToken::EndOfFile => {
self.stop_parsing()?;
}
// Anything else: parse error, ignore the token.
_ => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected token after after frameset",
)))?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intable>
pub(super) fn in_table_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// A character token, if the current node is table, tbody, template, tfoot, thead, or tr element:
HtmlToken::Character(c) => {
let is_table_text_element = self
.current_node_as_element()
.map(|el| {
matches!(
el.name.as_str(),
"table" | "tbody" | "template" | "tfoot" | "thead" | "tr"
)
})
.unwrap_or(false);
if is_table_text_element {
// Let the pending table character tokens be an empty list of tokens.
self.pending_table_character_tokens = Vec::new();
// Let the original insertion mode be the current insertion mode.
self.original_insertion_mode = Some(self.insertion_mode);
// Switch the insertion mode to "in table text" and reprocess the token.
self.insertion_mode = InsertionMode::InTableText;
self.token_emitted(HtmlToken::Character(c))?;
} else {
// Anything else
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected character token in table",
)))?;
self.foster_parenting = true;
let result =
self.using_the_rules_for(HtmlToken::Character(c), InsertionMode::InBody);
self.foster_parenting = false;
result?;
}
}
// Batched characters in table: fall back to per-character processing
// because the mode may switch between InTable and InTableText per char.
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
// A comment token
HtmlToken::Comment(comment) => {
self.insert_a_comment(comment, None)?;
}
// A DOCTYPE token
HtmlToken::DocType(_) => {
// Parse error. Ignore the token.
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected DOCTYPE in table",
)))?;
}
// A start tag whose tag name is "caption"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "caption" => {
self.clear_the_stack_back_to_a_table_context();
self.active_formatting_elements
.push(NodeOrMarker::Marker);
self.insert_an_html_element(token)?;
self.insertion_mode = InsertionMode::InCaption;
}
// A start tag whose tag name is "colgroup"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if token.tag_name == "colgroup" =>
{
self.clear_the_stack_back_to_a_table_context();
self.insert_an_html_element(token)?;
self.insertion_mode = InsertionMode::InColumnGroup;
}
// A start tag whose tag name is "col"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "col" => {
self.clear_the_stack_back_to_a_table_context();
self.insert_an_html_element(TagToken::new(String::from("colgroup")))?;
self.insertion_mode = InsertionMode::InColumnGroup;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
// A start tag whose tag name is one of: "tbody", "tfoot", "thead"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["tbody", "tfoot", "thead"].contains(&token.tag_name.as_str()) =>
{
self.clear_the_stack_back_to_a_table_context();
self.insert_an_html_element(token)?;
self.insertion_mode = InsertionMode::InTableBody;
}
// A start tag whose tag name is one of: "td", "th", "tr"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["td", "th", "tr"].contains(&token.tag_name.as_str()) =>
{
self.clear_the_stack_back_to_a_table_context();
self.insert_an_html_element(TagToken::new(String::from("tbody")))?;
self.insertion_mode = InsertionMode::InTableBody;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
// A start tag whose tag name is "table"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "table" => {
self.handle_error(HtmlParserError::MinorError(String::from(
"nested table start tag",
)))?;
if !self.has_an_element_in_table_scope("table") {
// Ignore the token.
} else {
self.pop_until_tag_name("table")?;
self.reset_the_insertion_mode_appropriately()?;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
}
// An end tag whose tag name is "table"
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "table" => {
if !self.has_an_element_in_table_scope("table") {
// Parse error. Ignore the token.
self.handle_error(HtmlParserError::MinorError(String::from(
"table end tag without table in scope",
)))?;
} else {
self.pop_until_tag_name("table")?;
self.reset_the_insertion_mode_appropriately()?;
}
}
// An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html",
// "tbody", "td", "tfoot", "th", "thead", "tr"
HtmlToken::TagToken(TagTokenType::EndTag(token))
if [
"body", "caption", "col", "colgroup", "html", "tbody", "td", "tfoot", "th",
"thead", "tr",
]
.contains(&token.tag_name.as_str()) =>
{
// Parse error. Ignore the token.
self.handle_error(HtmlParserError::MinorError(format!(
"unexpected end tag </{}> in table",
token.tag_name
)))?;
}
// A start tag whose tag name is one of: "style", "script", "template"
// An end tag whose tag name is "template"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["style", "script", "template"].contains(&token.tag_name.as_str()) =>
{
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "template" => {
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::EndTag(token)));
}
// A start tag whose tag name is "input"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "input" => {
let is_hidden_input = token.attributes.iter().any(|attr| {
attr.name.eq_ignore_ascii_case("type")
&& attr.value.eq_ignore_ascii_case("hidden")
});
if !is_hidden_input {
// Anything else
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected input in table (not hidden)",
)))?;
self.foster_parenting = true;
let result = self.using_the_rules_for(
HtmlToken::TagToken(TagTokenType::StartTag(token)),
InsertionMode::InBody,
);
self.foster_parenting = false;
result?;
} else {
// Parse error.
self.handle_error(HtmlParserError::MinorError(String::from(
"input type=hidden in table",
)))?;
self.insert_an_html_element(token)?;
self.open_elements.pop();
// Acknowledge the token's self-closing flag, if it is set.
return Ok(Acknowledgement::yes());
}
}
// A start tag whose tag name is "form"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "form" => {
self.handle_error(HtmlParserError::MinorError(String::from(
"form start tag in table",
)))?;
if self.open_elements_has_element("template")
|| self.form_element_pointer.is_some()
{
// Ignore the token.
} else {
let form_id = self.insert_an_html_element(token)?;
self.form_element_pointer = Some(form_id);
self.open_elements.pop();
}
}
// An end-of-file token
HtmlToken::EndOfFile => {
return self.in_body_insertion_mode(HtmlToken::EndOfFile);
}
// Anything else
_ => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected token in table, foster parenting",
)))?;
self.foster_parenting = true;
let result = self.using_the_rules_for(token, InsertionMode::InBody);
self.foster_parenting = false;
result?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intabletext>
pub(super) fn in_table_text_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// A character token that is U+0000 NULL
HtmlToken::Character('\0') => {
// Parse error. Ignore the token.
self.handle_error(HtmlParserError::MinorError(String::from(
"null character in table text",
)))?;
}
// Any other character token
HtmlToken::Character(c) => {
self.pending_table_character_tokens
.push(HtmlToken::Character(c));
}
// Batched characters: push each individually into pending tokens.
HtmlToken::Characters(s) => {
for c in s.chars() {
if c == '\0' {
self.handle_error(HtmlParserError::MinorError(String::from(
"null character in table text",
)))?;
} else {
self.pending_table_character_tokens
.push(HtmlToken::Character(c));
}
}
}
// Anything else
_ => {
// If any of the tokens in the pending table character tokens list
// are character tokens that are not ASCII whitespace:
let has_non_whitespace = self.pending_table_character_tokens.iter().any(|t| {
if let HtmlToken::Character(c) = t {
![
chars::CHARACTER_TABULATION,
chars::LINE_FEED,
chars::FORM_FEED,
chars::CARRIAGE_RETURN,
chars::SPACE,
]
.contains(c)
} else {
false
}
});
let pending_tokens = std::mem::take(&mut self.pending_table_character_tokens);
if has_non_whitespace {
// This is a parse error. Reprocess the character tokens using
// the rules for the "anything else" entry in the "in table" insertion mode.
self.handle_error(HtmlParserError::MinorError(String::from(
"non-whitespace character in table text",
)))?;
for pending_token in pending_tokens {
self.foster_parenting = true;
let result =
self.using_the_rules_for(pending_token, InsertionMode::InBody);
self.foster_parenting = false;
result?;
}
} else {
// Otherwise, insert the characters given by the pending table character
// tokens list.
for pending_token in pending_tokens {
if let HtmlToken::Character(c) = pending_token {
self.insert_character(c)?;
}
}
}
// Switch the insertion mode to the original insertion mode and reprocess the token.
self.insertion_mode = self
.original_insertion_mode
.ok_or(HtmlParseError::new("original insertion mode is None"))?;
self.token_emitted(token)?;
}
}
Ok(Acknowledgement::no())
}
/// Close the caption element: generate implied end tags, pop until caption,
/// clear active formatting elements, and switch to InTable.
/// Returns true if the caption was closed, false if no caption was in table scope.
fn close_the_caption(&mut self) -> Result<bool, HtmlParseError> {
if !self.has_an_element_in_table_scope("caption") {
self.handle_error(HtmlParserError::MinorError(String::from(
"no caption in table scope",
)))?;
return Ok(false);
}
self.generate_implied_end_tags(None)?;
if let Some(el) = self.current_node_as_element() {
if el.name != "caption" {
self.handle_error(HtmlParserError::MinorError(String::from(
"current node is not caption when closing caption",
)))?;
}
}
self.pop_until_tag_name("caption")?;
self.clear_the_list_of_active_formatting_elements_up_to_the_last_marker()?;
self.insertion_mode = InsertionMode::InTable;
Ok(true)
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incaption>
pub(super) fn in_caption_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// An end tag whose tag name is "caption"
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "caption" => {
self.close_the_caption()?;
}
// A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "td",
// "tfoot", "th", "thead", "tr"
// An end tag whose tag name is "table"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if [
"caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr",
]
.contains(&token.tag_name.as_str()) =>
{
if self.close_the_caption()? {
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "table" => {
if self.close_the_caption()? {
self.token_emitted(HtmlToken::TagToken(TagTokenType::EndTag(token)))?;
}
}
// An end tag whose tag name is one of: "body", "col", "colgroup", "html", "tbody",
// "td", "tfoot", "th", "thead", "tr"
HtmlToken::TagToken(TagTokenType::EndTag(token))
if [
"body", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead",
"tr",
]
.contains(&token.tag_name.as_str()) =>
{
// Parse error. Ignore the token.
self.handle_error(HtmlParserError::MinorError(format!(
"unexpected end tag </{}> in caption",
token.tag_name
)))?;
}
// Anything else: process using InBody rules
_ => {
return self.in_body_insertion_mode(token);
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incolumngroup>
pub(super) fn in_column_group_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// A character token that is whitespace
HtmlToken::Character(c)
if chars::WHITESPACE_CHARS.contains(&c) =>
{
self.insert_character(c)?;
}
HtmlToken::Characters(s) => {
for c in s.chars() {
self.token_emitted(HtmlToken::Character(c))?;
}
}
// A comment token
HtmlToken::Comment(comment) => {
self.insert_a_comment(comment, None)?;
}
// A DOCTYPE token
HtmlToken::DocType(_) => {
// Parse error. Ignore.
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected DOCTYPE in column group",
)))?;
}
// A start tag whose tag name is "html"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
return self
.in_body_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
// A start tag whose tag name is "col"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "col" => {
self.insert_an_html_element(token)?;
self.open_elements.pop();
return Ok(Acknowledgement::yes());
}
// An end tag whose tag name is "colgroup"
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "colgroup" => {
let is_colgroup = self
.current_node_as_element()
.map(|el| el.name == "colgroup")
.unwrap_or(false);
if !is_colgroup {
// Parse error. Ignore.
self.handle_error(HtmlParserError::MinorError(String::from(
"current node is not colgroup",
)))?;
} else {
self.open_elements.pop();
self.insertion_mode = InsertionMode::InTable;
}
}
// An end tag whose tag name is "col"
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "col" => {
// Parse error. Ignore.
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected </col> end tag",
)))?;
}
// A start tag whose tag name is "template"
// An end tag whose tag name is "template"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if token.tag_name == "template" =>
{
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "template" => {
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::EndTag(token)));
}
// An end-of-file token
HtmlToken::EndOfFile => {
return self.in_body_insertion_mode(HtmlToken::EndOfFile);
}
// Anything else
_ => {
let is_colgroup = self
.current_node_as_element()
.map(|el| el.name == "colgroup")
.unwrap_or(false);
if !is_colgroup {
// Parse error. Ignore.
self.handle_error(HtmlParserError::MinorError(String::from(
"current node is not colgroup, ignoring token",
)))?;
} else {
self.open_elements.pop();
self.insertion_mode = InsertionMode::InTable;
self.token_emitted(token)?;
}
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intablebody>
pub(super) fn in_table_body_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// A start tag whose tag name is "tr"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "tr" => {
self.clear_the_stack_back_to_a_table_body_context();
self.insert_an_html_element(token)?;
self.insertion_mode = InsertionMode::InRow;
}
// A start tag whose tag name is one of: "th", "td"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["th", "td"].contains(&token.tag_name.as_str()) =>
{
self.handle_error(HtmlParserError::MinorError(String::from(
"td/th start tag directly in table body",
)))?;
self.clear_the_stack_back_to_a_table_body_context();
self.insert_an_html_element(TagToken::new(String::from("tr")))?;
self.insertion_mode = InsertionMode::InRow;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
// An end tag whose tag name is one of: "tbody", "tfoot", "thead"
HtmlToken::TagToken(TagTokenType::EndTag(ref end_token))
if ["tbody", "tfoot", "thead"].contains(&end_token.tag_name.as_str()) =>
{
if !self.has_an_element_in_table_scope(&end_token.tag_name) {
self.handle_error(HtmlParserError::MinorError(format!(
"no {} in table scope",
end_token.tag_name
)))?;
} else {
self.clear_the_stack_back_to_a_table_body_context();
self.open_elements.pop();
self.insertion_mode = InsertionMode::InTable;
}
}
// A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody",
// "tfoot", "thead"
// An end tag whose tag name is "table"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["caption", "col", "colgroup", "tbody", "tfoot", "thead"]
.contains(&token.tag_name.as_str()) =>
{
if !self.has_an_element_in_table_scope("tbody")
&& !self.has_an_element_in_table_scope("thead")
&& !self.has_an_element_in_table_scope("tfoot")
{
self.handle_error(HtmlParserError::MinorError(String::from(
"no tbody/thead/tfoot in table scope",
)))?;
} else {
self.clear_the_stack_back_to_a_table_body_context();
self.open_elements.pop();
self.insertion_mode = InsertionMode::InTable;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "table" => {
if !self.has_an_element_in_table_scope("tbody")
&& !self.has_an_element_in_table_scope("thead")
&& !self.has_an_element_in_table_scope("tfoot")
{
self.handle_error(HtmlParserError::MinorError(String::from(
"no tbody/thead/tfoot in table scope",
)))?;
} else {
self.clear_the_stack_back_to_a_table_body_context();
self.open_elements.pop();
self.insertion_mode = InsertionMode::InTable;
self.token_emitted(HtmlToken::TagToken(TagTokenType::EndTag(token)))?;
}
}
// An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html",
// "td", "th", "tr"
HtmlToken::TagToken(TagTokenType::EndTag(token))
if [
"body", "caption", "col", "colgroup", "html", "td", "th", "tr",
]
.contains(&token.tag_name.as_str()) =>
{
// Parse error. Ignore the token.
self.handle_error(HtmlParserError::MinorError(format!(
"unexpected end tag </{}> in table body",
token.tag_name
)))?;
}
// Anything else: process using InTable rules
_ => {
return self.in_table_insertion_mode(token);
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inrow>
pub(super) fn in_row_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// A start tag whose tag name is one of: "th", "td"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["th", "td"].contains(&token.tag_name.as_str()) =>
{
self.clear_the_stack_back_to_a_table_row_context();
self.insert_an_html_element(token)?;
self.insertion_mode = InsertionMode::InCell;
self.active_formatting_elements
.push(NodeOrMarker::Marker);
}
// An end tag whose tag name is "tr"
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "tr" => {
if !self.has_an_element_in_table_scope("tr") {
self.handle_error(HtmlParserError::MinorError(String::from(
"no tr in table scope",
)))?;
} else {
self.clear_the_stack_back_to_a_table_row_context();
self.open_elements.pop(); // pop the tr
self.insertion_mode = InsertionMode::InTableBody;
}
}
// A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody",
// "tfoot", "thead", "tr"
// An end tag whose tag name is "table"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if [
"caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr",
]
.contains(&token.tag_name.as_str()) =>
{
if !self.has_an_element_in_table_scope("tr") {
self.handle_error(HtmlParserError::MinorError(String::from(
"no tr in table scope",
)))?;
} else {
self.clear_the_stack_back_to_a_table_row_context();
self.open_elements.pop(); // pop the tr
self.insertion_mode = InsertionMode::InTableBody;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
}
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "table" => {
if !self.has_an_element_in_table_scope("tr") {
self.handle_error(HtmlParserError::MinorError(String::from(
"no tr in table scope",
)))?;
} else {
self.clear_the_stack_back_to_a_table_row_context();
self.open_elements.pop(); // pop the tr
self.insertion_mode = InsertionMode::InTableBody;
self.token_emitted(HtmlToken::TagToken(TagTokenType::EndTag(token)))?;
}
}
// An end tag whose tag name is one of: "tbody", "tfoot", "thead"
HtmlToken::TagToken(TagTokenType::EndTag(ref end_token))
if ["tbody", "tfoot", "thead"].contains(&end_token.tag_name.as_str()) =>
{
if !self.has_an_element_in_table_scope(&end_token.tag_name) {
self.handle_error(HtmlParserError::MinorError(format!(
"no {} in table scope",
end_token.tag_name
)))?;
} else if !self.has_an_element_in_table_scope("tr") {
// Ignore the token.
} else {
self.clear_the_stack_back_to_a_table_row_context();
self.open_elements.pop(); // pop the tr
self.insertion_mode = InsertionMode::InTableBody;
self.token_emitted(token)?;
}
}
// An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html",
// "td", "th"
HtmlToken::TagToken(TagTokenType::EndTag(token))
if ["body", "caption", "col", "colgroup", "html", "td", "th"]
.contains(&token.tag_name.as_str()) =>
{
// Parse error. Ignore the token.
self.handle_error(HtmlParserError::MinorError(format!(
"unexpected end tag </{}> in row",
token.tag_name
)))?;
}
// Anything else: process using InTable rules
_ => {
return self.in_table_insertion_mode(token);
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incell>
pub(super) fn in_cell_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// An end tag whose tag name is one of: "td", "th"
HtmlToken::TagToken(TagTokenType::EndTag(ref end_token))
if ["td", "th"].contains(&end_token.tag_name.as_str()) =>
{
if !self.has_an_element_in_table_scope(&end_token.tag_name) {
self.handle_error(HtmlParserError::MinorError(format!(
"no {} in table scope",
end_token.tag_name
)))?;
} else {
self.generate_implied_end_tags(None)?;
if let Some(el) = self.current_node_as_element() {
if el.name != end_token.tag_name {
self.handle_error(HtmlParserError::MinorError(format!(
"current node is not {}",
end_token.tag_name
)))?;
}
}
let tag_name = end_token.tag_name.clone();
self.pop_until_tag_name(&tag_name)?;
self.clear_the_list_of_active_formatting_elements_up_to_the_last_marker()?;
self.insertion_mode = InsertionMode::InRow;
}
}
// A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "td",
// "tfoot", "th", "thead", "tr"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if [
"caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr",
]
.contains(&token.tag_name.as_str()) =>
{
// Assert: stack has td or th in table scope
if !self.has_an_element_in_table_scope("td")
&& !self.has_an_element_in_table_scope("th")
{
self.handle_error(HtmlParserError::MinorError(String::from(
"no td or th in table scope",
)))?;
} else {
self.close_the_cell()?;
self.token_emitted(HtmlToken::TagToken(TagTokenType::StartTag(token)))?;
}
}
// An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html"
HtmlToken::TagToken(TagTokenType::EndTag(token))
if ["body", "caption", "col", "colgroup", "html"]
.contains(&token.tag_name.as_str()) =>
{
// Parse error. Ignore the token.
self.handle_error(HtmlParserError::MinorError(format!(
"unexpected end tag </{}> in cell",
token.tag_name
)))?;
}
// An end tag whose tag name is one of: "table", "tbody", "tfoot", "thead", "tr"
HtmlToken::TagToken(TagTokenType::EndTag(ref end_token))
if ["table", "tbody", "tfoot", "thead", "tr"]
.contains(&end_token.tag_name.as_str()) =>
{
if !self.has_an_element_in_table_scope(&end_token.tag_name) {
self.handle_error(HtmlParserError::MinorError(format!(
"no {} in table scope",
end_token.tag_name
)))?;
} else {
self.close_the_cell()?;
self.token_emitted(token)?;
}
}
// Anything else: process using InBody rules
_ => {
return self.in_body_insertion_mode(token);
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inselect>
pub(super) fn in_select_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// A character token that is U+0000 NULL
HtmlToken::Character('\0') => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected null character in select",
)))?;
}
// Any other character token
HtmlToken::Character(c) => {
self.insert_character(c)?;
}
// Batched characters in select
HtmlToken::Characters(ref s) => {
let filtered: String;
let text = if s.contains('\0') {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected null character in select",
)))?;
filtered = s.replace('\0', "");
if filtered.is_empty() {
return Ok(Acknowledgement::no());
}
&filtered
} else {
s
};
self.insert_characters(text)?;
}
// A comment token
HtmlToken::Comment(comment) => {
self.insert_a_comment(comment, None)?;
}
// A DOCTYPE token
HtmlToken::DocType(_) => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected DOCTYPE in select",
)))?;
}
// A start tag whose tag name is "html"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "html" => {
return self
.in_body_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
// A start tag whose tag name is "option"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "option" => {
// If the current node is an option element, pop that node
if let Some(el) = self.current_node_as_element() {
if el.name == "option" {
self.open_elements.pop();
}
}
self.insert_an_html_element(token)?;
}
// A start tag whose tag name is "optgroup"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if token.tag_name == "optgroup" =>
{
// If the current node is an option element, pop that node
if let Some(el) = self.current_node_as_element() {
if el.name == "option" {
self.open_elements.pop();
}
}
// If the current node is an optgroup element, pop that node
if let Some(el) = self.current_node_as_element() {
if el.name == "optgroup" {
self.open_elements.pop();
}
}
self.insert_an_html_element(token)?;
}
// A start tag whose tag name is "hr"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "hr" => {
// If the current node is an option element, pop that node
if let Some(el) = self.current_node_as_element() {
if el.name == "option" {
self.open_elements.pop();
}
}
// If the current node is an optgroup element, pop that node
if let Some(el) = self.current_node_as_element() {
if el.name == "optgroup" {
self.open_elements.pop();
}
}
self.insert_an_html_element(token)?;
// Pop the current node off the stack of open elements
self.open_elements.pop();
// Acknowledge the token's self-closing flag, if it is set
return Ok(Acknowledgement::yes());
}
// An end tag whose tag name is "optgroup"
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "optgroup" => {
// First, if the current node is an option element, and the node immediately
// before it in the stack of open elements is an optgroup element, then pop
// the current node from the stack of open elements.
let len = self.open_elements.len();
if len >= 2 {
let is_current_option = self
.current_node_as_element()
.map(|el| el.name == "option")
.unwrap_or(false);
if is_current_option {
let prev_id = self.open_elements[len - 2];
let is_prev_optgroup = self
.arena
.get(prev_id)
.and_then(|node| match node.get() {
XpathItemTreeNode::ElementNode(el) => Some(el.name == "optgroup"),
_ => None,
})
.unwrap_or(false);
if is_prev_optgroup {
self.open_elements.pop();
}
}
}
// If the current node is an optgroup element, pop that node
if let Some(el) = self.current_node_as_element() {
if el.name == "optgroup" {
self.open_elements.pop();
} else {
// Otherwise, this is a parse error; ignore the token.
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected </optgroup> in select",
)))?;
}
}
}
// An end tag whose tag name is "option"
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "option" => {
if let Some(el) = self.current_node_as_element() {
if el.name == "option" {
self.open_elements.pop();
} else {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected </option> in select",
)))?;
}
}
}
// An end tag whose tag name is "select"
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "select" => {
if !self.has_an_element_in_select_scope("select") {
// Parse error. Ignore the token. (fragment case)
self.handle_error(HtmlParserError::MinorError(String::from(
"no select element in select scope",
)))?;
} else {
self.pop_until_tag_name("select")?;
self.reset_the_insertion_mode_appropriately()?;
}
}
// A start tag whose tag name is "select"
HtmlToken::TagToken(TagTokenType::StartTag(token)) if token.tag_name == "select" => {
// Parse error.
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected <select> in select",
)))?;
if !self.has_an_element_in_select_scope("select") {
// Ignore the token. (fragment case)
} else {
// Act as if </select> was seen
self.pop_until_tag_name("select")?;
self.reset_the_insertion_mode_appropriately()?;
}
}
// A start tag whose tag name is one of: "input", "keygen", "textarea"
HtmlToken::TagToken(TagTokenType::StartTag(ref start_token))
if ["input", "keygen", "textarea"].contains(&start_token.tag_name.as_str()) =>
{
// Parse error.
self.handle_error(HtmlParserError::MinorError(format!(
"unexpected <{}> in select",
start_token.tag_name
)))?;
if !self.has_an_element_in_select_scope("select") {
// Ignore the token. (fragment case)
} else {
self.pop_until_tag_name("select")?;
self.reset_the_insertion_mode_appropriately()?;
// Reprocess the token.
self.token_emitted(token)?;
}
}
// A start tag whose tag name is one of: "script", "template"
HtmlToken::TagToken(TagTokenType::StartTag(token))
if ["script", "template"].contains(&token.tag_name.as_str()) =>
{
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::StartTag(token)));
}
// An end tag whose tag name is "template"
HtmlToken::TagToken(TagTokenType::EndTag(token)) if token.tag_name == "template" => {
return self
.in_head_insertion_mode(HtmlToken::TagToken(TagTokenType::EndTag(token)));
}
// An end-of-file token
HtmlToken::EndOfFile => {
return self.in_body_insertion_mode(HtmlToken::EndOfFile);
}
// Anything else: parse error, ignore the token.
_ => {
self.handle_error(HtmlParserError::MinorError(String::from(
"unexpected token in select",
)))?;
}
}
Ok(Acknowledgement::no())
}
/// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inselectintable>
pub(super) fn in_select_in_table_insertion_mode(
&mut self,
token: HtmlToken,
) -> Result<Acknowledgement, HtmlParseError> {
match token {
// A start tag whose tag name is one of: "caption", "table", "tbody", "tfoot",
// "thead", "tr", "td", "th"
HtmlToken::TagToken(TagTokenType::StartTag(ref start_token))
if [
"caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th",
]
.contains(&start_token.tag_name.as_str()) =>
{
// Parse error.
self.handle_error(HtmlParserError::MinorError(format!(
"unexpected <{}> in select in table",
start_token.tag_name
)))?;
// Pop elements until a select element has been popped
self.pop_until_tag_name("select")?;
self.reset_the_insertion_mode_appropriately()?;
// Reprocess the token.
self.token_emitted(token)?;
}
// An end tag whose tag name is one of: "caption", "table", "tbody", "tfoot",
// "thead", "tr", "td", "th"
HtmlToken::TagToken(TagTokenType::EndTag(ref end_token))
if [
"caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th",
]
.contains(&end_token.tag_name.as_str()) =>
{
// Parse error.
self.handle_error(HtmlParserError::MinorError(format!(
"unexpected </{}> in select in table",
end_token.tag_name
)))?;
if !self.has_an_element_in_table_scope(&end_token.tag_name) {
// Ignore the token.
} else {
self.pop_until_tag_name("select")?;
self.reset_the_insertion_mode_appropriately()?;
// Reprocess the token.
self.token_emitted(token)?;
}
}
// Anything else: process using the rules for the "in select" insertion mode
_ => {
return self.in_select_insertion_mode(token);
}
}
Ok(Acknowledgement::no())
}
}