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
|
import deques
import macros
import options
import sequtils
import streams
import strformat
import strutils
import tables
import unicode
import css/sheet
import data/charset
import encoding/decoderstream
import html/dom
import html/tags
import html/htmltokenizer
import js/javascript
import types/url
import utils/twtstr
type
CharsetConfidence = enum
CONFIDENCE_TENTATIVE, CONFIDENCE_CERTAIN, CONFIDENCE_IRRELEVANT
DOMParser = ref object # JS interface
OpenElements = seq[Element]
HTML5Parser = object
case fragment: bool
of true: ctx: Element
else: discard
needsreinterpret: bool
charset: Charset
confidence: CharsetConfidence
openElements: OpenElements
insertionMode: InsertionMode
oldInsertionMode: InsertionMode
templateModes: seq[InsertionMode]
head: Element
tokenizer: Tokenizer
document: Document
form: HTMLFormElement
fosterParenting: bool
scripting: bool
activeFormatting: seq[(Element, Token)] # nil => marker
framesetok: bool
ignoreLF: bool
pendingTableChars: string
pendingTableCharsWhitespace: bool
AdjustedInsertionLocation = tuple[inside: Node, before: Node]
# 13.2.4.1
InsertionMode = enum
INITIAL, BEFORE_HTML, BEFORE_HEAD, IN_HEAD, IN_HEAD_NOSCRIPT, AFTER_HEAD,
IN_BODY, TEXT, IN_TABLE, IN_TABLE_TEXT, IN_CAPTION, IN_COLUMN_GROUP,
IN_TABLE_BODY, IN_ROW, IN_CELL, IN_SELECT, IN_SELECT_IN_TABLE, IN_TEMPLATE,
AFTER_BODY, IN_FRAMESET, AFTER_FRAMESET, AFTER_AFTER_BODY,
AFTER_AFTER_FRAMESET
proc resetInsertionMode(parser: var HTML5Parser) =
template switch_insertion_mode_and_return(mode: InsertionMode) =
parser.insertionMode = mode
return
for i in countdown(parser.openElements.high, 0):
var node = parser.openElements[i]
let last = i == 0
if parser.fragment:
node = parser.ctx
if node.tagType == TAG_SELECT:
if not last:
for j in countdown(parser.openElements.high, 1):
let ancestor = parser.openElements[j]
case ancestor.tagType
of TAG_TEMPLATE: break
of TAG_TABLE: switch_insertion_mode_and_return IN_SELECT_IN_TABLE
else: discard
switch_insertion_mode_and_return IN_SELECT
case node.tagType
of TAG_TD, TAG_TH:
if not last:
switch_insertion_mode_and_return IN_CELL
of TAG_TR: switch_insertion_mode_and_return IN_ROW
of TAG_TBODY, TAG_THEAD, TAG_TFOOT: switch_insertion_mode_and_return IN_CAPTION
of TAG_COLGROUP: switch_insertion_mode_and_return IN_COLUMN_GROUP
of TAG_TABLE: switch_insertion_mode_and_return IN_TABLE
of TAG_TEMPLATE: switch_insertion_mode_and_return parser.templateModes[^1]
of TAG_HEAD:
if not last:
switch_insertion_mode_and_return IN_HEAD
of TAG_BODY: switch_insertion_mode_and_return IN_BODY
of TAG_FRAMESET: switch_insertion_mode_and_return IN_FRAMESET
of TAG_HTML:
if parser.head != nil:
switch_insertion_mode_and_return BEFORE_HEAD
else:
switch_insertion_mode_and_return AFTER_HEAD
else: discard
if last:
switch_insertion_mode_and_return IN_BODY
func currentNode(parser: HTML5Parser): Element =
return parser.openElements[^1]
func adjustedCurrentNode(parser: HTML5Parser): Element =
if parser.fragment: parser.ctx
else: parser.currentNode
template parse_error() = discard
func lastElementOfTag(parser: HTML5Parser, tagType: TagType): tuple[element: Element, pos: int] =
for i in countdown(parser.openElements.high, 0):
if parser.openElements[i].tagType == tagType:
return (parser.openElements[i], i)
return (nil, -1)
template last_child_of(n: Node): AdjustedInsertionLocation =
(n, nil)
# 13.2.6.1
func appropriatePlaceForInsert(parser: HTML5Parser, target: Element): AdjustedInsertionLocation =
assert parser.openElements[0].tagType == TAG_HTML
if parser.fosterParenting and target.tagType in {TAG_TABLE, TAG_TBODY, TAG_TFOOT, TAG_THEAD, TAG_TR}:
let lastTemplate = parser.lastElementOfTag(TAG_TEMPLATE)
let lastTable = parser.lastElementOfTag(TAG_TABLE)
if lastTemplate.element != nil and (lastTable.element == nil or lastTable.pos < lastTemplate.pos):
return last_child_of(HTMLTemplateElement(lastTemplate.element).content)
if lastTable.element == nil:
return last_child_of(parser.openElements[0])
if lastTable.element.parentNode != nil:
return (lastTable.element.parentNode, lastTable.element)
let previousElement = parser.openElements[lastTable.pos - 1]
result = last_child_of(previousElement)
else:
result = last_child_of(target)
if result.inside.nodeType == ELEMENT_NODE and Element(result.inside).tagType == TAG_TEMPLATE:
result = (HTMLTemplateElement(result.inside).content, nil)
func appropriatePlaceForInsert(parser: HTML5Parser): AdjustedInsertionLocation =
parser.appropriatePlaceForInsert(parser.currentNode)
func hasElement(elements: seq[Element], tag: TagType): bool =
for element in elements:
if element.tagType == tag:
return true
return false
func hasElementInSpecificScope(elements: seq[Element], target: Element, list: set[TagType]): bool =
for i in countdown(elements.high, 0):
if elements[i] == target:
return true
if elements[i].tagType in list:
return false
assert false
func hasElementInSpecificScope(elements: seq[Element], target: TagType, list: set[TagType]): bool =
for i in countdown(elements.high, 0):
if elements[i].tagType == target:
return true
if elements[i].tagType in list:
return false
assert false
func hasElementInSpecificScope(elements: seq[Element], target: set[TagType], list: set[TagType]): bool =
for i in countdown(elements.high, 0):
if elements[i].tagType in target:
return true
if elements[i].tagType in list:
return false
assert false
const Scope = {TAG_APPLET, TAG_CAPTION, TAG_HTML, TAG_TABLE, TAG_TD, TAG_TH,
TAG_MARQUEE, TAG_OBJECT, TAG_TEMPLATE} #TODO SVG (NOTE MathML not implemented)
func hasElementInScope(elements: seq[Element], target: TagType): bool =
return elements.hasElementInSpecificScope(target, Scope)
func hasElementInScope(elements: seq[Element], target: set[TagType]): bool =
return elements.hasElementInSpecificScope(target, Scope)
func hasElementInScope(elements: seq[Element], target: Element): bool =
return elements.hasElementInSpecificScope(target, Scope)
func hasElementInListItemScope(elements: seq[Element], target: TagType): bool =
return elements.hasElementInSpecificScope(target, Scope + {TAG_OL, TAG_UL})
func hasElementInButtonScope(elements: seq[Element], target: TagType): bool =
return elements.hasElementInSpecificScope(target, Scope + {TAG_BUTTON})
func hasElementInTableScope(elements: seq[Element], target: TagType): bool =
return elements.hasElementInSpecificScope(target, {TAG_HTML, TAG_TABLE, TAG_TEMPLATE})
func hasElementInTableScope(elements: seq[Element], target: set[TagType]): bool =
return elements.hasElementInSpecificScope(target, {TAG_HTML, TAG_TABLE, TAG_TEMPLATE})
func hasElementInSelectScope(elements: seq[Element], target: TagType): bool =
for i in countdown(elements.high, 0):
if elements[i].tagType == target:
return true
if elements[i].tagType notin {TAG_OPTION, TAG_OPTGROUP}:
return false
assert false
func createElement(parser: HTML5Parser, token: Token, namespace: Namespace, intendedParent: Node): Element =
#TODO custom elements
let document = intendedParent.document
let localName = token.tagname
let element = document.newHTMLElement(localName, namespace, tagType = token.tagtype, attrs = token.attrs)
if element.isResettable():
element.resetElement()
if element.tagType in SupportedFormAssociatedElements and parser.form != nil and
not parser.openElements.hasElement(TAG_TEMPLATE) and
(element.tagType notin ListedElements or not element.attrb("form")) and
intendedParent.inSameTree(parser.form):
let element = FormAssociatedElement(element)
element.setForm(parser.form)
element.parserInserted = true
return element
proc pushElement(parser: var HTML5Parser, node: Element) =
parser.openElements.add(node)
parser.tokenizer.hasnonhtml = not parser.adjustedCurrentNode().inHTMLNamespace()
proc popElement(parser: var HTML5Parser): Element =
result = parser.openElements.pop()
if result.tagType == TAG_TEXTAREA:
result.resetElement()
if parser.openElements.len == 0:
parser.tokenizer.hasnonhtml = false
else:
parser.tokenizer.hasnonhtml = not parser.adjustedCurrentNode().inHTMLNamespace()
template pop_current_node = discard parser.popElement()
proc insert(location: AdjustedInsertionLocation, node: Node) =
location.inside.insert(node, location.before)
proc insertForeignElement(parser: var HTML5Parser, token: Token, namespace: Namespace): Element =
let location = parser.appropriatePlaceForInsert()
let element = parser.createElement(token, namespace, location.inside)
if location.inside.preInsertionValidity(element, location.before):
#TODO custom elements
location.insert(element)
parser.pushElement(element)
return element
proc insertHTMLElement(parser: var HTML5Parser, token: Token): Element =
return parser.insertForeignElement(token, Namespace.HTML)
proc adjustSVGAttributes(token: Token) =
const adjusted = {
"attributename": "attributeName",
"attributetype": "attributeType",
"basefrequency": "baseFrequency",
"baseprofile": "baseProfile",
"calcmode": "calcMode",
"clippathunits": "clipPathUnits",
"diffuseconstant": "diffuseConstant",
"edgemode": "edgeMode",
"filterunits": "filterUnits",
"glyphref": "glyphRef",
"gradienttransform": "gradientTransform",
"gradientunits": "gradientUnits",
"kernelmatrix": "kernelMatrix",
"kernelunitlength": "kernelUnitLength",
"keypoints": "keyPoints",
"keysplines": "keySplines",
"keytimes": "keyTimes",
"lengthadjust": "lengthAdjust",
"limitingconeangle": "limitingConeAngle",
"markerheight": "markerHeight",
"markerunits": "markerUnits",
"markerwidth": "markerWidth",
"maskcontentunits": "maskContentUnits",
"maskunits": "maskUnits",
"numoctaves": "numOctaves",
"pathlength": "pathLength",
"patterncontentunits": "patternContentUnits",
"patterntransform": "patternTransform",
"patternunits": "patternUnits",
"pointsatx": "pointsAtX",
"pointsaty": "pointsAtY",
"pointsatz": "pointsAtZ",
"preservealpha": "preserveAlpha",
"preserveaspectratio": "preserveAspectRatio",
"primitiveunits": "primitiveUnits",
"refx": "refX",
"refy": "refY",
"repeatcount": "repeatCount",
"repeatdur": "repeatDur",
"requiredextensions": "requiredExtensions",
"requiredfeatures": "requiredFeatures",
"specularconstant": "specularConstant",
"specularexponent": "specularExponent",
"spreadmethod": "spreadMethod",
"startoffset": "startOffset",
"stddeviation": "stdDeviation",
"stitchtiles": "stitchTiles",
"surfacescale": "surfaceScale",
"systemlanguage": "systemLanguage",
"tablevalues": "tableValues",
"targetx": "targetX",
"targety": "targetY",
"textlength": "textLength",
"viewbox": "viewBox",
"viewtarget": "viewTarget",
"xchannelselector": "xChannelSelector",
"ychannelselector": "yChannelSelector",
"zoomandpan": "zoomAndPan",
}.toTable()
var todo: seq[string]
for k in token.attrs.keys:
if k in adjusted:
todo.add(k)
for s in todo:
token.attrs[adjusted[s]] = token.attrs[s]
template insert_character_impl(parser: var HTML5Parser, data: typed) =
let location = parser.appropriatePlaceForInsert()
if location.inside.nodeType == DOCUMENT_NODE:
return
let insertNode = if location.before == nil:
location.inside.lastChild
else:
location.before.previousSibling
if insertNode != nil and insertNode.nodeType == TEXT_NODE:
dom.Text(insertNode).data &= data
else:
let text = location.inside.document.createTextNode($data)
location.insert(text)
if location.inside.nodeType == ELEMENT_NODE:
let parent = Element(location.inside)
if parent.tagType == TAG_STYLE:
let parent = HTMLStyleElement(parent)
parent.sheet_invalid = true
proc insertCharacter(parser: var HTML5Parser, data: string) =
insert_character_impl(parser, data)
proc insertCharacter(parser: var HTML5Parser, data: char) =
insert_character_impl(parser, data)
proc insertCharacter(parser: var HTML5Parser, data: Rune) =
insert_character_impl(parser, data)
proc insertComment(parser: var HTML5Parser, token: Token, position: AdjustedInsertionLocation) =
position.insert(position.inside.document.createComment(token.data))
proc insertComment(parser: var HTML5Parser, token: Token) =
let position = parser.appropriatePlaceForInsert()
position.insert(position.inside.document.createComment(token.data))
const PublicIdentifierEquals = [
"-//W3O//DTD W3 HTML Strict 3.0//EN//",
"-/W3C/DTD HTML 4.0 Transitional/EN",
"HTML"
]
const PublicIdentifierStartsWith = [
"+//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//",
]
const SystemIdentifierMissingAndPublicIdentifierStartsWith = [
"-//W3C//DTD HTML 4.01 Frameset//",
"-//W3C//DTD HTML 4.01 Transitional//"
]
const PublicIdentifierStartsWithLimited = [
"-//W3C//DTD XHTML 1.0 Frameset//",
"-//W3C//DTD XHTML 1.0 Transitional//"
]
const SystemIdentifierNotMissingAndPublicIdentifierStartsWith = [
"-//W3C//DTD HTML 4.01 Frameset//",
"-//W3C//DTD HTML 4.01 Transitional//"
]
func quirksConditions(token: Token): bool =
if token.quirks: return true
if token.name.isnone or token.name.get != "html": return true
if token.sysid.issome:
if token.sysid.get == "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd":
return true
if token.pubid.issome:
if token.pubid.get in PublicIdentifierEquals:
return true
for id in PublicIdentifierStartsWith:
if token.pubid.get.startsWithNoCase(id):
return true
if token.sysid.isnone:
for id in SystemIdentifierMissingAndPublicIdentifierStartsWith:
if token.pubid.get.startsWithNoCase(id):
return true
return false
func limitedQuirksConditions(token: Token): bool =
if token.pubid.isnone: return false
for id in PublicIdentifierStartsWithLimited:
if token.pubid.get.startsWithNoCase(id):
return true
if token.sysid.isnone: return false
for id in SystemIdentifierNotMissingAndPublicIdentifierStartsWith:
if token.pubid.get.startsWithNoCase(id):
return true
return false
# 13.2.6.2
proc genericRawtextElementParsingAlgorithm(parser: var HTML5Parser, token: Token) =
discard parser.insertHTMLElement(token)
parser.tokenizer.state = RAWTEXT
parser.oldInsertionMode = parser.insertionMode
parser.insertionMode = TEXT
proc genericRCDATAElementParsingAlgorithm(parser: var HTML5Parser, token: Token) =
discard parser.insertHTMLElement(token)
parser.tokenizer.state = RCDATA
parser.oldInsertionMode = parser.insertionMode
parser.insertionMode = TEXT
# 13.2.6.3
proc generateImpliedEndTags(parser: var HTML5Parser) =
const tags = {TAG_DD, TAG_DT, TAG_LI, TAG_OPTGROUP, TAG_OPTION, TAG_P,
TAG_RB, TAG_RP, TAG_RT, TAG_RTC}
while parser.currentNode.tagType in tags:
discard parser.popElement()
proc generateImpliedEndTags(parser: var HTML5Parser, exclude: TagType) =
let tags = {TAG_DD, TAG_DT, TAG_LI, TAG_OPTGROUP, TAG_OPTION, TAG_P,
TAG_RB, TAG_RP, TAG_RT, TAG_RTC} - {exclude}
while parser.currentNode.tagType in tags:
discard parser.popElement()
proc generateImpliedEndTagsThoroughly(parser: var HTML5Parser) =
const tags = {TAG_CAPTION, TAG_COLGROUP, TAG_DD, TAG_DT, TAG_LI,
TAG_OPTGROUP, TAG_OPTION, TAG_P, TAG_RB, TAG_RP, TAG_RT,
TAG_RTC, TAG_TBODY, TAG_TD, TAG_TFOOT, TAG_TH, TAG_THEAD,
TAG_TR}
while parser.currentNode.tagType in tags:
discard parser.popElement()
# 13.2.4.3
proc pushOntoActiveFormatting(parser: var HTML5Parser, element: Element, token: Token) =
var count = 0
for i in countdown(parser.activeFormatting.high, 0):
let it = parser.activeFormatting[i]
if it[0] == nil: break
if it[0].tagType != element.tagType: continue
if it[0].tagType == TAG_UNKNOWN:
if it[0].localName != element.localName: continue
if it[0].namespace != element.namespace: continue
var fail = false
for k, v in it[0].attrs:
if k notin element.attrs:
fail = true
break
if v != element.attrs[k]:
fail = true
break
if fail: continue
for k, v in element.attrs:
if k notin it[0].attrs:
fail = true
break
if fail: continue
inc count
if count == 3:
parser.activeFormatting.delete(i)
break
parser.activeFormatting.add((element, token))
proc reconstructActiveFormatting(parser: var HTML5Parser) =
type State = enum
REWIND, ADVANCE, CREATE
if parser.activeFormatting.len == 0:
return
if parser.activeFormatting[^1][0] == nil or parser.openElements.hasElement(parser.activeFormatting[^1][0].tagType):
return
var i = parser.activeFormatting.high
template entry: Element = (parser.activeFormatting[i][0])
var state = REWIND
while true:
{.computedGoto.}
case state
of REWIND:
if i == 0:
state = CREATE
continue
dec i
if entry != nil and not parser.openElements.hasElement(entry.tagType):
continue
state = ADVANCE
of ADVANCE:
inc i
state = CREATE
of CREATE:
parser.activeFormatting[i] = (parser.insertHTMLElement(parser.activeFormatting[i][1]), parser.activeFormatting[i][1])
if i != parser.activeFormatting.high:
state = ADVANCE
continue
break
proc clearActiveFormattingTillMarker(parser: var HTML5Parser) =
while parser.activeFormatting.len > 0 and parser.activeFormatting.pop()[0] != nil: discard
func isHTMLIntegrationPoint(node: Element): bool =
return false #TODO SVG (NOTE MathML not implemented)
func extractEncFromMeta(s: string): Charset =
var i = 0
while true: # Loop:
var j = 0
while i < s.len:
template check(c: static char) =
if s[i] in {c, c.toUpperAscii()}: inc j
else: j = 0
case j
of 0: check 'c'
of 1: check 'h'
of 2: check 'a'
of 3: check 'r'
of 4: check 's'
of 5: check 'e'
of 6: check 't'
of 7:
inc j
break
else: discard
inc i
if j < 7: return CHARSET_UNKNOWN
while i < s.len and s[i] in AsciiWhitespace: inc i
if i >= s.len or s[i] != '=': continue
while i < s.len and s[i] in AsciiWhitespace: inc i
break
inc i
if i >= s.len: return CHARSET_UNKNOWN
if s[i] in {'"', '\''}:
let s2 = s.substr(i + 1).until(s[i])
if s2.len == 0 or s2[^1] != s[i]:
return CHARSET_UNKNOWN
return getCharset(s2)
return getCharset(s.substr(i).until({';', ' '}))
proc changeEncoding(parser: var HTML5Parser, cs: Charset) =
if parser.charset in {CHARSET_UTF_16_LE, CHARSET_UTF_16_BE}:
parser.confidence = CONFIDENCE_CERTAIN
return
parser.confidence = CONFIDENCE_CERTAIN
if cs == parser.charset:
return
if cs == CHARSET_X_USER_DEFINED:
parser.charset = CHARSET_WINDOWS_1252
else:
parser.charset = cs
parser.needsreinterpret = true
# Following is an implementation of the state (?) machine defined in
# https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhtml
# It uses the ad-hoc pattern matching macro `match' to apply the following
# transformations:
# * First, pairs of patterns and actions are stored in tuples (and `discard'
# statements...)
# * These pairs are then assigned to token types, later mapped to legs of the
# first case statement.
# * Another case statement is constructed where needed, e.g. for switching on
# characters/tags/etc.
# * Finally, the whole thing is wrapped in a named block, to implement a
# pseudo-goto by breaking out only when the else statement needn't be
# executed.
#
# For example, the following code:
#
# match token:
# TokenType.COMMENT => (block: echo "comment")
# ("<p>", "<a>", "</div>") => (block: echo "p, a or closing div")
# ("<div>", "</p>") => (block: anything_else)
# (TokenType.START_TAG, TokenType.END_TAG) => (block: assert false, "invalid")
# _ => (block: echo "anything else")
#
# (effectively) generates this:
#
# block inside_not_else:
# case token.t
# of TokenType.COMMENT:
# echo "comment"
# break inside_not_else
# of TokenType.START_TAG:
# case token.tagtype
# of {TAG_P, TAG_A}:
# echo "p, a or closing div"
# break inside_not_else
# of TAG_DIV: discard
# else:
# assert false
# break inside_not_else
# of TokenType.END_TAG:
# case token.tagtype
# of TAG_DIV:
# echo "p, a or closing div"
# break inside_not_else
# of TAG_P: discard
# else:
# assert false
# break inside_not_else
# else: discard
# echo "anything else"
#
# This duplicates any code that applies for several token types, except for the
# else branch.
macro match(token: Token, body: typed): untyped =
type OfBranchStore = object
ofBranches: seq[(seq[NimNode], NimNode)]
defaultBranch: NimNode
painted: bool
# Stores 'of' branches
var ofBranches: array[TokenType, OfBranchStore]
# Stores 'else', 'elif' branches
var defaultBranch: NimNode
const tokenTypes = (func(): Table[string, TokenType] =
for tt in TokenType:
result[$tt] = tt)()
for disc in body:
let tup = disc[0] # access actual tuple
let pattern = `tup`[0]
let lambda = `tup`[1]
var action = lambda.findChild(it.kind notin {nnkSym, nnkEmpty, nnkFormalParams})
if pattern.kind != nnkDiscardStmt and not (action.len == 2 and action[1].kind == nnkDiscardStmt and action[1][0] == newStrLitNode("anything_else")):
action = quote do:
`action`
#eprint token #debug
break inside_not_else
var patterns = @[pattern]
while patterns.len > 0:
let pattern = patterns.pop()
case pattern.kind
of nnkSym: # simple symbols; we assume these are the enums
ofBranches[tokenTypes[pattern.strVal]].defaultBranch = action
ofBranches[tokenTypes[pattern.strVal]].painted = true
of nnkCharLit:
ofBranches[CHARACTER_ASCII].ofBranches.add((@[pattern], action))
ofBranches[CHARACTER_ASCII].painted = true
of nnkCurly:
case pattern[0].kind
of nnkCharLit:
ofBranches[CHARACTER_ASCII].ofBranches.add((@[pattern], action))
ofBranches[CHARACTER_ASCII].painted = true
else: error fmt"Unsupported curly of kind {pattern[0].kind}"
of nnkStrLit:
var tempTokenizer = newTokenizer(pattern.strVal)
for token in tempTokenizer.tokenize:
let tt = int(token.tagtype)
case token.t
of START_TAG, END_TAG:
var found = false
for i in 0..ofBranches[token.t].ofBranches.high:
if ofBranches[token.t].ofBranches[i][1] == action:
found = true
ofBranches[token.t].ofBranches[i][0].add((quote do: TagType(`tt`)))
ofBranches[token.t].painted = true
break
if not found:
ofBranches[token.t].ofBranches.add((@[(quote do: TagType(`tt`))], action))
ofBranches[token.t].painted = true
else: error fmt"{pattern.strVal}: Unsupported token {token} of kind {token.t}"
break
of nnkDiscardStmt:
defaultBranch = action
of nnkTupleConstr:
for child in pattern:
patterns.add(child)
else: error fmt"{pattern}: Unsupported pattern of kind {pattern.kind}"
func tokenBranchOn(tok: TokenType): NimNode =
case tok
of START_TAG, END_TAG:
return quote do: token.tagtype
of CHARACTER:
return quote do: token.r
of CHARACTER_ASCII:
return quote do: token.c
else: error fmt"Unsupported branching of token {tok}"
template add_to_case(branch: typed) =
if branch[0].len == 1:
tokenCase.add(newNimNode(nnkOfBranch).add(branch[0][0]).add(branch[1]))
else:
var curly = newNimNode(nnkCurly)
for node in branch[0]:
curly.add(node)
tokenCase.add(newNimNode(nnkOfBranch).add(curly).add(branch[1]))
# Build case statements
var mainCase = newNimNode(nnkCaseStmt).add(quote do: `token`.t)
for tt in TokenType:
let ofBranch = newNimNode(nnkOfBranch).add(quote do: TokenType(`tt`))
let tokenCase = newNimNode(nnkCaseStmt)
if ofBranches[tt].defaultBranch != nil:
if ofBranches[tt].ofBranches.len > 0:
tokenCase.add(tokenBranchOn(tt))
for branch in ofBranches[tt].ofBranches:
add_to_case branch
tokenCase.add(newNimNode(nnkElse).add(ofBranches[tt].defaultBranch))
ofBranch.add(tokenCase)
mainCase.add(ofBranch)
else:
ofBranch.add(ofBranches[tt].defaultBranch)
mainCase.add(ofBranch)
else:
if ofBranches[tt].ofBranches.len > 0:
tokenCase.add(tokenBranchOn(tt))
for branch in ofBranches[tt].ofBranches:
add_to_case branch
ofBranch.add(tokenCase)
tokenCase.add(newNimNode(nnkElse).add(quote do: discard))
mainCase.add(ofBranch)
else:
discard
for t in TokenType:
if not ofBranches[t].painted:
mainCase.add(newNimNode(nnkElse).add(quote do: discard))
break
var stmts = newStmtList().add(mainCase)
for stmt in defaultBranch:
stmts.add(stmt)
result = newBlockStmt(ident("inside_not_else"), stmts)
proc processInHTMLContent(parser: var HTML5Parser, token: Token, insertionMode = parser.insertionMode) =
template pop_all_nodes =
while parser.openElements.len > 1: pop_current_node
template anything_else = discard "anything_else"
macro `=>`(v: typed, body: untyped): untyped =
quote do:
discard (`v`, proc() = `body`)
template _ = discard
template reprocess(tok: Token) =
parser.processInHTMLContent(tok)
case insertionMode
of INITIAL:
match token:
AsciiWhitespace => (block: discard)
TokenType.COMMENT => (block: parser.insertComment(token, last_child_of(parser.document)))
TokenType.DOCTYPE => (block:
if token.name.isnone or token.name.get != "html" or token.pubid.issome or (token.sysid.issome and token.sysid.get != "about:legacy-compat"):
parse_error
let doctype = parser.document.newDocumentType(token.name.get(""), token.pubid.get(""), token.sysid.get(""))
parser.document.append(doctype)
if not parser.document.is_iframe_srcdoc and not parser.document.parser_cannot_change_the_mode_flag:
if quirksConditions(token):
parser.document.mode = QUIRKS
elif limitedQuirksConditions(token):
parser.document.mode = LIMITED_QUIRKS
parser.insertionMode = BEFORE_HTML
)
_ => (block:
if not parser.document.is_iframe_srcdoc:
parse_error
if not parser.document.parser_cannot_change_the_mode_flag:
parser.document.mode = QUIRKS
parser.insertionMode = BEFORE_HTML
reprocess token
)
of BEFORE_HTML:
match token:
TokenType.DOCTYPE => (block: parse_error)
TokenType.COMMENT => (block: parser.insertComment(token, last_child_of(parser.document)))
AsciiWhitespace => (block: discard)
"<html>" => (block:
let element = parser.createElement(token, Namespace.HTML, parser.document)
parser.document.append(element)
parser.pushElement(element)
parser.insertionMode = BEFORE_HEAD
)
("</head>", "</body>", "</html>", "</br>") => (block: anything_else)
TokenType.END_TAG => (block: parse_error)
_ => (block:
let element = parser.document.newHTMLElement(TAG_HTML, Namespace.HTML)
parser.document.append(element)
parser.pushElement(element)
parser.insertionMode = BEFORE_HEAD
reprocess token
)
of BEFORE_HEAD:
match token:
AsciiWhitespace => (block: discard)
TokenType.COMMENT => (block: parser.insertComment(token))
TokenType.DOCTYPE => (block: parse_error)
"<html>" => (block: parser.processInHTMLContent(token, IN_BODY))
"<head>" => (block:
parser.head = parser.insertHTMLElement(token)
parser.insertionMode = IN_HEAD
)
("</head>", "</body>", "</html>", "</br>") => (block: anything_else)
TokenType.END_TAG => (block: parse_error)
_ => (block:
parser.head = parser.insertHTMLElement(Token(t: START_TAG, tagtype: TAG_HEAD))
parser.insertionMode = IN_HEAD
reprocess token
)
of IN_HEAD:
match token:
AsciiWhitespace => (block: discard)
TokenType.COMMENT => (block: parser.insertComment(token))
TokenType.DOCTYPE => (block: parse_error)
"<html>" => (block: parser.processInHTMLContent(token, IN_BODY))
("<base>", "<basefont>", "<bgsound>", "<link>") => (block:
discard parser.insertHTMLElement(token)
pop_current_node
)
"<meta>" => (block:
let element = parser.insertHTMLElement(token)
pop_current_node
if parser.confidence == CONFIDENCE_TENTATIVE:
let cs = getCharset(element.attr("charset"))
if cs != CHARSET_UNKNOWN:
parser.changeEncoding(cs)
elif element.attr("http-equiv").equalsIgnoreCase("Content-Type"):
let cs = extractEncFromMeta(element.attr("content"))
if cs != CHARSET_UNKNOWN:
parser.changeEncoding(cs)
)
"<title>" => (block: parser.genericRCDATAElementParsingAlgorithm(token))
"<noscript>" => (block:
if not parser.scripting:
discard parser.insertHTMLElement(token)
parser.insertionMode = IN_HEAD_NOSCRIPT
else:
parser.genericRawtextElementParsingAlgorithm(token)
)
("<noframes>", "<style>") => (block: parser.genericRawtextElementParsingAlgorithm(token))
"<script>" => (block:
let location = parser.appropriatePlaceForInsert()
let element = HTMLScriptElement(parser.createElement(token, Namespace.HTML, location.inside))
element.parserDocument = parser.document
element.forceAsync = false
if parser.fragment:
element.alreadyStarted = true
#TODO document.write (?)
location.insert(element)
parser.pushElement(element)
parser.tokenizer.state = SCRIPT_DATA
parser.oldInsertionMode = parser.insertionMode
parser.insertionMode = TEXT
)
"</head>" => (block:
pop_current_node
parser.insertionMode = AFTER_HEAD
)
("</body>", "</html>", "</br>") => (block: anything_else)
"<template>" => (block:
discard parser.insertHTMLElement(token)
parser.activeFormatting.add((nil, nil))
parser.framesetok = false
parser.insertionMode = IN_TEMPLATE
parser.templateModes.add(IN_TEMPLATE)
)
"</template>" => (block:
if not parser.openElements.hasElement(TAG_TEMPLATE):
parse_error
else:
parser.generateImpliedEndTagsThoroughly()
if parser.currentNode.tagType != TAG_TEMPLATE:
parse_error
while parser.popElement().tagType != TAG_TEMPLATE: discard
parser.clearActiveFormattingTillMarker()
discard parser.templateModes.pop()
parser.resetInsertionMode()
)
("<head>", TokenType.END_TAG) => (block: parse_error)
_ => (block:
pop_current_node
parser.insertionMode = AFTER_HEAD
reprocess token
)
of IN_HEAD_NOSCRIPT:
match token:
TokenType.DOCTYPE => (block: parse_error)
"<html>" => (block: parser.processInHTMLContent(token, IN_BODY))
"</noscript>" => (block:
pop_current_node
parser.insertionMode = IN_HEAD
)
(AsciiWhitespace,
TokenType.COMMENT,
"<basefont>", "<bgsound>", "<link>", "<meta>", "<noframes>", "<style>") => (block:
parser.processInHTMLContent(token, IN_HEAD))
"</br>" => (block: anything_else)
("<head>", "<noscript>") => (block: parse_error)
TokenType.END_TAG => (block: parse_error)
_ => (block:
pop_current_node
parser.insertionMode = IN_HEAD
reprocess token
)
of AFTER_HEAD:
match token:
AsciiWhitespace => (block: parser.insertCharacter(token.c))
TokenType.COMMENT => (block: parser.insertComment(token))
TokenType.DOCTYPE => (block: parse_error)
"<html>" => (block: parser.processInHTMLContent(token, IN_BODY))
"<body>" => (block:
discard parser.insertHTMLElement(token)
parser.framesetok = false
parser.insertionMode = IN_BODY
)
"<frameset>" => (block:
discard parser.insertHTMLElement(token)
parser.insertionMode = IN_FRAMESET
)
("<base>", "<basefont>", "<bgsound>", "<link>", "<meta>", "<noframes>", "<script>", "<style>", "<template>", "<title>") => (block:
parse_error
parser.pushElement(parser.head)
parser.processInHTMLContent(token, IN_HEAD)
for i in countdown(parser.openElements.high, 0):
if parser.openElements[i] == parser.head:
parser.openElements.delete(i)
)
"</template>" => (block: parser.processInHTMLContent(token, IN_HEAD))
("</body>", "</html>", "</br>") => (block: anything_else)
("<head>", TokenType.END_TAG) => (block: parse_error)
_ => (block:
discard parser.insertHTMLElement(Token(t: START_TAG, tagtype: TAG_BODY))
parser.insertionMode = IN_BODY
reprocess token
)
of IN_BODY:
proc closeP(parser: var HTML5Parser) =
parser.generateImpliedEndTags(TAG_P)
if parser.currentNode.tagType != TAG_P: parse_error
while parser.popElement().tagType != TAG_P: discard
proc adoptionAgencyAlgorithm(parser: var HTML5Parser, token: Token): bool =
if parser.currentNode.tagType != TAG_UNKNOWN and parser.currentNode.tagtype == token.tagtype or parser.currentNode.localName == token.tagname:
var fail = true
for it in parser.activeFormatting:
if it[0] == parser.currentNode:
fail = false
if fail:
pop_current_node
return false
var i = 0
while true:
if i >= 8: return false
inc i
if parser.activeFormatting.len == 0: return true
var formatting: Element
var formattingIndex: int
for j in countdown(parser.activeFormatting.high, 0):
let element = parser.activeFormatting[j][0]
if element == nil:
return true
if element.tagType != TAG_UNKNOWN and element.tagtype == token.tagtype or element.qualifiedName == token.tagname:
formatting = element
formattingIndex = j
break
if j == 0:
return true
let stackIndex = parser.openElements.find(formatting)
if stackIndex < 0:
parse_error
parser.activeFormatting.delete(formattingIndex)
return false
if not parser.openElements.hasElementInScope(formatting):
parse_error
return false
if formatting != parser.currentNode: parse_error
var furthestBlock: Element = nil
var furthestBlockIndex: int
for j in countdown(parser.openElements.high, 0):
if parser.openElements[j] == formatting:
break
if parser.openElements[j].tagType in SpecialElements:
furthestBlock = parser.openElements[j]
furthestBlockIndex = j
break
if furthestBlock == nil:
while parser.popElement() != formatting: discard
parser.activeFormatting.delete(formattingIndex)
return false
let commonAncestor = parser.openElements[stackIndex - 1]
var bookmark = formattingIndex
var node = furthestBlock
var aboveNode = parser.openElements[furthestBlockIndex - 1]
var lastNode = furthestBlock
var j = 0
while true:
inc j
node = aboveNode
let nodeStackIndex = parser.openElements.find(node)
if node == formatting: break
var nodeFormattingIndex = -1
for i in countdown(parser.activeFormatting.high, 0):
if parser.activeFormatting[i][0] == node:
nodeFormattingIndex = i
break
if j > 3 and nodeFormattingIndex >= 0:
parser.activeFormatting.delete(nodeFormattingIndex)
if nodeFormattingIndex < bookmark:
dec bookmark # a previous node got deleted, so decrease bookmark by one
if nodeFormattingIndex < 0:
aboveNode = parser.openElements[nodeStackIndex - 1]
parser.openElements.delete(nodeStackIndex)
if nodeStackIndex < furthestBlockIndex:
dec furthestBlockIndex
furthestBlock = parser.openElements[furthestBlockIndex]
continue
let element = parser.createElement(parser.activeFormatting[nodeFormattingIndex][1], Namespace.HTML, commonAncestor)
parser.activeFormatting[nodeFormattingIndex] = (element, parser.activeFormatting[nodeFormattingIndex][1])
parser.openElements[nodeStackIndex] = element
aboveNode = parser.openElements[nodeStackIndex - 1]
node = element
if lastNode == furthestBlock:
bookmark = nodeFormattingIndex + 1
node.append(lastNode)
lastNode = node
let location = parser.appropriatePlaceForInsert(commonAncestor)
location.inside.insert(lastNode, location.before)
let token = parser.activeFormatting[formattingIndex][1]
let element = parser.createElement(token, Namespace.HTML, furthestBlock)
var tomove: seq[Node]
j = furthestBlock.childList.high
while j >= 0:
let child = furthestBlock.childList[j]
child.remove(j, true)
tomove.add(child)
dec j
for child in tomove:
element.append(child)
furthestBlock.append(element)
parser.activeFormatting.insert((element, token), bookmark)
parser.activeFormatting.delete(formattingIndex)
parser.openElements.insert(element, furthestBlockIndex)
parser.openElements.delete(stackIndex)
template any_other_start_tag() =
parser.reconstructActiveFormatting()
discard parser.insertHTMLElement(token)
template any_other_end_tag() =
for i in countdown(parser.openElements.high, 0):
let node = parser.openElements[i]
if node.tagType != TAG_UNKNOWN and node.tagType == token.tagtype or node.localName == token.tagname:
parser.generateImpliedEndTags(token.tagtype)
if node != parser.currentNode: parse_error
while parser.popElement() != node: discard
break
elif node.tagType in SpecialElements:
parse_error
return
match token:
'\0' => (block: parse_error)
AsciiWhitespace => (block:
parser.reconstructActiveFormatting()
parser.insertCharacter(token.c)
)
TokenType.CHARACTER_ASCII => (block:
parser.reconstructActiveFormatting()
parser.insertCharacter(token.c)
parser.framesetOk = false
)
TokenType.CHARACTER => (block:
parser.reconstructActiveFormatting()
parser.insertCharacter(token.r)
parser.framesetOk = false
)
TokenType.COMMENT => (block: parser.insertComment(token))
TokenType.DOCTYPE => (block: parse_error)
"<html>" => (block:
parse_error
if parser.openElements.hasElement(TAG_TEMPLATE):
discard
else:
for k, v in token.attrs:
if k notin parser.openElements[0].attrs:
parser.openElements[0].attr(k, v)
)
("<base>", "<basefont>", "<bgsound>", "<link>", "<meta>", "<noframes>", "<script>", "<style>", "<template>", "<title>",
"</template>") => (block: parser.processInHTMLContent(token, IN_HEAD))
"<body>" => (block:
parse_error
if parser.openElements.len == 1 or parser.openElements[1].tagType != TAG_BODY or parser.openElements.hasElement(TAG_TEMPLATE):
discard
else:
parser.framesetOk = false
for k, v in token.attrs:
if k notin parser.openElements[1].attrs:
parser.openElements[1].attr(k, v)
)
"<frameset>" => (block:
parse_error
if parser.openElements.len == 1 or parser.openElements[1].tagType != TAG_BODY or not parser.framesetOk:
discard
else:
if parser.openElements[1].parentNode != nil:
parser.openElements[1].remove()
pop_all_nodes
)
TokenType.EOF => (block:
if parser.templateModes.len > 0:
parser.processInHTMLContent(token, IN_TEMPLATE)
else:
#NOTE parse error omitted
discard # stop
)
"</body>" => (block:
if not parser.openElements.hasElementInScope(TAG_BODY):
parse_error
else:
#NOTE parse error omitted
parser.insertionMode = AFTER_BODY
)
"</html>" => (block:
if not parser.openElements.hasElementInScope(TAG_BODY):
parse_error
else:
#NOTE parse error omitted
parser.insertionMode = AFTER_BODY
reprocess token
)
("<address>", "<article>", "<aside>", "<blockquote>", "<center>",
"<details>", "<dialog>", "<dir>", "<div>", "<dl>", "<fieldset>",
"<figcaption>", "<figure>", "<footer>", "<header>", "<hgroup>", "<main>",
"<menu>", "<nav>", "<ol>", "<p>", "<section>", "<summary>", "<ul>") => (block:
if parser.openElements.hasElementInButtonScope(TAG_P):
parser.closeP()
discard parser.insertHTMLElement(token)
)
("<h1>", "<h2>", "<h3>", "<h4>", "<h5>", "<h6>") => (block:
if parser.openElements.hasElementInButtonScope(TAG_P):
parser.closeP()
if parser.currentNode.tagType in HTagTypes:
parse_error
pop_current_node
discard parser.insertHTMLElement(token)
)
("<pre>", "<listing>") => (block:
if parser.openElements.hasElementInButtonScope(TAG_P):
parser.closeP()
discard parser.insertHTMLElement(token)
parser.ignoreLF = true
parser.framesetOk = false
)
"<form>" => (block:
let hasTemplate = parser.openElements.hasElement(TAG_TEMPLATE)
if parser.form != nil and not hasTemplate:
parse_error
else:
if parser.openElements.hasElementInButtonScope(TAG_P):
parser.closeP()
let element = parser.insertHTMLElement(token)
if not hasTemplate:
parser.form = HTMLFormElement(element)
)
"<li>" => (block:
parser.framesetOk = false
for i in countdown(parser.openElements.high, 0):
let node = parser.openElements[i]
case node.tagType
of TAG_LI:
parser.generateImpliedEndTags(TAG_LI)
if parser.currentNode.tagType != TAG_LI: parse_error
while parser.popElement().tagType != TAG_LI: discard
break
of SpecialElements - {TAG_ADDRESS, TAG_DIV, TAG_P, TAG_LI}:
break
else: discard
if parser.openElements.hasElementInButtonScope(TAG_P):
parser.closeP()
discard parser.insertHTMLElement(token)
)
("<dd>", "<dt>") => (block:
parser.framesetOk = false
for i in countdown(parser.openElements.high, 0):
let node = parser.openElements[i]
case node.tagType
of TAG_DD:
parser.generateImpliedEndTags(TAG_DD)
if parser.currentNode.tagType != TAG_DD: parse_error
while parser.popElement().tagType != TAG_DD: discard
break
of TAG_DT:
parser.generateImpliedEndTags(TAG_DT)
if parser.currentNode.tagType != TAG_DT: parse_error
while parser.popElement().tagType != TAG_DT: discard
break
of SpecialElements - {TAG_ADDRESS, TAG_DIV, TAG_P, TAG_DD, TAG_DT}:
break
else: discard
if parser.openElements.hasElementInButtonScope(TAG_P):
parser.closeP()
discard parser.insertHTMLElement(token)
)
"<plaintext>" => (block:
if parser.openElements.hasElementInButtonScope(TAG_P):
parser.closeP()
discard parser.insertHTMLElement(token)
parser.tokenizer.state = PLAINTEXT
)
"<button>" => (block:
if parser.openElements.hasElementInScope(TAG_BUTTON):
parse_error
parser.generateImpliedEndTags()
while parser.popElement().tagType != TAG_BUTTON: discard
parser.reconstructActiveFormatting()
discard parser.insertHTMLElement(token)
parser.framesetOk = false
)
("</address>", "</article>", "</aside>", "</blockquote>", "</button>",
"</center>", "</details>", "</dialog>", "</dir>", "</div>", "</dl>",
"</fieldset>", "</figcaption>", "</figure>", "</footer>", "</header>",
"</hgroup>", "</listing>", "</main>", "</menu>", "</nav>", "</ol>",
"</pre>", "</section>", "</summary>", "</ul>") => (block:
if not parser.openElements.hasElementInScope(token.tagtype):
parse_error
else:
parser.generateImpliedEndTags()
if parser.currentNode.tagType != token.tagtype: parse_error
while parser.popElement().tagType != token.tagtype: discard
)
"</form>" => (block:
if not parser.openElements.hasElement(TAG_TEMPLATE):
let node = parser.form
parser.form = nil
if node == nil or not parser.openElements.hasElementInScope(node.tagType):
parse_error
return
parser.generateImpliedEndTags()
if parser.currentNode != node: parse_error
parser.openElements.delete(parser.openElements.find(node))
else:
if not parser.openElements.hasElementInScope(TAG_FORM):
parse_error
return
parser.generateImpliedEndTags()
if parser.currentNode.tagType != TAG_FORM: parse_error
while parser.popElement().tagType != TAG_FORM: discard
)
"</p>" => (block:
if not parser.openElements.hasElementInButtonScope(TAG_P):
parse_error
discard parser.insertHTMLElement(Token(t: START_TAG, tagtype: TAG_P))
parser.closeP()
)
"</li>" => (block:
if not parser.openElements.hasElementInListItemScope(TAG_LI):
parse_error
else:
parser.generateImpliedEndTags(TAG_LI)
if parser.currentNode.tagType != TAG_LI: parse_error
while parser.popElement().tagType != TAG_LI: discard
)
("</dd>", "</dt>") => (block:
if not parser.openElements.hasElementInScope(token.tagtype):
parse_error
else:
parser.generateImpliedEndTags(token.tagtype)
if parser.currentNode.tagType != token.tagtype: parse_error
while parser.popElement().tagType != token.tagtype: discard
)
("</h1>", "</h2>", "</h3>", "</h4>", "</h5>", "</h6>") => (block:
if not parser.openElements.hasElementInScope(HTagTypes):
parse_error
else:
parser.generateImpliedEndTags()
if parser.currentNode.tagType != token.tagtype: parse_error
while parser.popElement().tagType notin HTagTypes: discard
)
"</sarcasm>" => (block:
#*deep breath*
anything_else
)
"<a>" => (block:
var anchor: Element = nil
for i in countdown(parser.activeFormatting.high, 0):
let format = parser.activeFormatting[i]
if format[0] == nil:
break
if format[0].tagType == TAG_A:
anchor = format[0]
break
if anchor != nil:
parse_error
if parser.adoptionAgencyAlgorithm(token):
any_other_end_tag
return
for i in 0..parser.activeFormatting.high:
if parser.activeFormatting[i][0] == anchor:
parser.activeFormatting.delete(i)
break
for i in 0..parser.openElements.high:
if parser.openElements[i] == anchor:
parser.openElements.delete(i)
break
parser.reconstructActiveFormatting()
let element = parser.insertHTMLElement(token)
parser.pushOntoActiveFormatting(element, token)
)
("<b>", "<big>", "<code>", "<em>", "<font>", "<i>", "<s>", "<small>",
"<strike>", "<strong>", "<tt>", "<u>") => (block:
parser.reconstructActiveFormatting()
let element = parser.insertHTMLElement(token)
parser.pushOntoActiveFormatting(element, token)
)
"<nobr>" => (block:
parser.reconstructActiveFormatting()
if parser.openElements.hasElementInScope(TAG_NOBR):
parse_error
if parser.adoptionAgencyAlgorithm(token):
any_other_end_tag
return
parser.reconstructActiveFormatting()
let element = parser.insertHTMLElement(token)
parser.pushOntoActiveFormatting(element, token)
)
("</a>", "</b>", "</big>", "</code>", "</em>", "</font>", "</i>",
"</nobr>", "</s>", "</small>", "</strike>", "</strong>", "</tt>",
"</u>") => (block:
if parser.adoptionAgencyAlgorithm(token):
any_other_end_tag
return
)
("<applet>", "<marquee>", "<object>") => (block:
parser.reconstructActiveFormatting()
discard parser.insertHTMLElement(token)
parser.activeFormatting.add((nil, nil))
parser.framesetOk = false
)
("</applet>", "</marquee>", "</object>") => (block:
if not parser.openElements.hasElementInScope(token.tagtype):
parse_error
else:
parser.generateImpliedEndTags()
if parser.currentNode.tagType != token.tagtype: parse_error
while parser.popElement().tagType != token.tagtype: discard
parser.clearActiveFormattingTillMarker()
)
"<table>" => (block:
if parser.document.mode != QUIRKS:
if parser.openElements.hasElementInButtonScope(TAG_P):
parser.closeP()
discard parser.insertHTMLElement(token)
parser.framesetOk = false
parser.insertionMode = IN_TABLE
)
"</br>" => (block:
parse_error
parser.processInHTMLContent(Token(t: START_TAG, tagtype: TAG_BR))
)
("<area>", "<br>", "<embed>", "<img>", "<keygen>", "<wbr>") => (block:
parser.reconstructActiveFormatting()
discard parser.insertHTMLElement(token)
pop_current_node
parser.framesetOk = false
)
"<input>" => (block:
parser.reconstructActiveFormatting()
discard parser.insertHTMLElement(token)
pop_current_node
if not token.attrs.getOrDefault("type").equalsIgnoreCase("hidden"):
parser.framesetOk = false
)
("<param>", "<source>", "<track>") => (block:
discard parser.insertHTMLElement(token)
pop_current_node
)
"<hr>" => (block:
if parser.openElements.hasElementInButtonScope(TAG_P):
parser.closeP()
discard parser.insertHTMLElement(token)
pop_current_node
parser.framesetOk = false
)
"<image>" => (block:
#TODO ew
let token = Token(t: START_TAG, tagtype: TAG_IMG, tagname: "img", selfclosing: token.selfclosing, attrs: token.attrs)
reprocess token
)
"<textarea>" => (block:
discard parser.insertHTMLElement(token)
parser.ignoreLF = true
parser.tokenizer.state = RCDATA
parser.oldInsertionMode = parser.insertionMode
parser.framesetOk = false
parser.insertionMode = TEXT
)
"<xmp>" => (block:
if parser.openElements.hasElementInButtonScope(TAG_P):
parser.closeP()
parser.reconstructActiveFormatting()
parser.framesetOk = false
parser.genericRawtextElementParsingAlgorithm(token)
)
"<iframe>" => (block:
parser.framesetOk = false
parser.genericRawtextElementParsingAlgorithm(token)
)
"<noembed>" => (block:
parser.genericRawtextElementParsingAlgorithm(token)
)
"<noscript>" => (block:
if parser.scripting:
parser.genericRawtextElementParsingAlgorithm(token)
else:
any_other_start_tag
)
"<select>" => (block:
parser.reconstructActiveFormatting()
discard parser.insertHTMLElement(token)
parser.framesetOk = false
if parser.insertionMode in {IN_TABLE, IN_CAPTION, IN_TABLE_BODY, IN_CELL}:
parser.insertionMode = IN_SELECT_IN_TABLE
else:
parser.insertionMode = IN_SELECT
)
("<optgroup>", "<option>") => (block:
if parser.currentNode.tagType == TAG_OPTION:
pop_current_node
parser.reconstructActiveFormatting()
discard parser.insertHTMLElement(token)
)
("<rb>", "<rtc>") => (block:
if parser.openElements.hasElementInScope(TAG_RUBY):
parser.generateImpliedEndTags()
if parser.currentNode.tagType != TAG_RUBY: parse_error
discard parser.insertHTMLElement(token)
)
("<rp>", "<rt>") => (block:
if parser.openElements.hasElementInScope(TAG_RUBY):
parser.generateImpliedEndTags(TAG_RTC)
if parser.currentNode.tagType notin {TAG_RUBY, TAG_RTC}: parse_error
discard parser.insertHTMLElement(token)
)
#NOTE <math> (not implemented)
#TODO <svg> (SVG)
("<caption>", "<col>", "<colgroup>", "<frame>", "<head>", "<tbody>",
"<td>", "<tfoot>", "<th>", "<thead>", "<tr>") => (block: parse_error)
TokenType.START_TAG => (block: any_other_start_tag)
TokenType.END_TAG => (block: any_other_end_tag)
of TEXT:
match token:
TokenType.CHARACTER_ASCII => (block:
assert token.c != '\0'
parser.insertCharacter(token.c)
)
TokenType.CHARACTER => (block:
parser.insertCharacter(token.r)
)
TokenType.EOF => (block:
parse_error
if parser.currentNode.tagType == TAG_SCRIPT:
HTMLScriptElement(parser.currentNode).alreadyStarted = true
pop_current_node
parser.insertionMode = parser.oldInsertionMode
reprocess token
)
"</script>" => (block:
#TODO microtask
let script = HTMLScriptElement(parser.popElement())
parser.insertionMode = parser.oldInsertionMode
#TODO document.write() (?)
script.prepare()
while parser.document.parserBlockingScript != nil:
let script = parser.document.parserBlockingScript
parser.document.parserBlockingScript = nil
#TODO style sheet
script.execute()
)
TokenType.END_TAG => (block:
pop_current_node
parser.insertionMode = parser.oldInsertionMode
)
of IN_TABLE:
template clear_the_stack_back_to_a_table_context() =
while parser.currentNode.tagType notin {TAG_TABLE, TAG_TEMPLATE, TAG_HTML}:
pop_current_node
match token:
(TokenType.CHARACTER_ASCII, TokenType.CHARACTER) => (block:
if parser.currentNode.tagType in {TAG_TABLE, TAG_TBODY, TAG_TFOOT, TAG_THEAD, TAG_TR}:
parser.pendingTableChars = ""
parser.pendingTableCharsWhitespace = true
parser.oldInsertionMode = parser.insertionMode
parser.insertionMode = IN_TABLE_TEXT
reprocess token
else: # anything else
parse_error
parser.fosterParenting = true
parser.processInHTMLContent(token, IN_BODY)
parser.fosterParenting = false
)
TokenType.COMMENT => (block: parser.insertComment(token))
TokenType.DOCTYPE => (block: parse_error)
"<caption>" => (block:
clear_the_stack_back_to_a_table_context
parser.activeFormatting.add((nil, nil))
discard parser.insertHTMLElement(token)
parser.insertionMode = IN_CAPTION
)
"<colgroup>" => (block:
clear_the_stack_back_to_a_table_context
discard parser.insertHTMLElement(Token(t: START_TAG, tagtype: TAG_COLGROUP))
parser.insertionMode = IN_COLUMN_GROUP
)
("<tbody>", "<tfoot>", "<thead>") => (block:
clear_the_stack_back_to_a_table_context
discard parser.insertHTMLElement(token)
parser.insertionMode = IN_TABLE_BODY
)
("<td>", "<th>", "<tr>") => (block:
clear_the_stack_back_to_a_table_context
discard parser.insertHTMLElement(Token(t: START_TAG, tagtype: TAG_TBODY))
parser.insertionMode = IN_TABLE_BODY
)
"<table>" => (block:
parse_error
if not parser.openElements.hasElementInScope(TAG_TABLE):
discard
else:
while parser.popElement().tagType != TAG_TABLE: discard
parser.resetInsertionMode()
reprocess token
)
"</table>" => (block:
if not parser.openElements.hasElementInScope(TAG_TABLE):
parse_error
else:
while parser.popElement().tagType != TAG_TABLE: discard
parser.resetInsertionMode()
)
("</body>", "</caption>", "</col>", "</colgroup>", "</html>", "</tbody>",
"</td>", "</tfoot>", "</th>", "</thead>", "</tr>") => (block:
parse_error
)
("<style>", "<script>", "<template>", "</template>") => (block:
parser.processInHTMLContent(token, IN_HEAD)
)
"<input>" => (block:
if not token.attrs.getOrDefault("type").equalsIgnoreCase("hidden"):
# anything else
parse_error
parser.fosterParenting = true
parser.processInHTMLContent(token, IN_BODY)
parser.fosterParenting = false
else:
parse_error
discard parser.insertHTMLElement(token)
pop_current_node
)
"<form>" => (block:
parse_error
if parser.form != nil or parser.openElements.hasElement(TAG_TEMPLATE):
discard
else:
parser.form = HTMLFormElement(parser.insertHTMLElement(token))
pop_current_node
)
TokenType.EOF => (block:
parser.processInHTMLContent(token, IN_BODY)
)
_ => (block:
parse_error
parser.fosterParenting = true
parser.processInHTMLContent(token, IN_BODY)
parser.fosterParenting = false
)
of IN_TABLE_TEXT:
match token:
'\0' => (block: parse_error)
TokenType.CHARACTER_ASCII => (block:
if token.c notin AsciiWhitespace:
parser.pendingTableCharsWhitespace = false
parser.pendingTableChars &= token.c
)
TokenType.CHARACTER => (block:
parser.pendingTableChars &= token.r
parser.pendingTableCharsWhitespace = false
)
_ => (block:
if not parser.pendingTableCharsWhitespace:
# I *think* this is effectively the same thing the specification wants...
parse_error
parser.fosterParenting = true
parser.reconstructActiveFormatting()
parser.insertCharacter(parser.pendingTableChars)
parser.framesetOk = false
parser.fosterParenting = false
else:
parser.insertCharacter(parser.pendingTableChars)
parser.insertionMode = parser.oldInsertionMode
reprocess token
)
of IN_CAPTION:
match token:
"</caption>" => (block:
if not parser.openElements.hasElementInTableScope(TAG_CAPTION):
parse_error
else:
parser.generateImpliedEndTags()
if parser.currentNode.tagType != TAG_CAPTION: parse_error
while parser.popElement().tagType != TAG_CAPTION: discard
parser.clearActiveFormattingTillMarker()
parser.insertionMode = IN_TABLE
)
("<caption>", "<col>", "<colgroup>", "<tbody>", "<td>", "<tfoot>",
"<th>", "<thead>", "<tr>", "</table>") => (block:
if not parser.openElements.hasElementInTableScope(TAG_CAPTION):
parse_error
else:
parser.generateImpliedEndTags()
if parser.currentNode.tagType != TAG_CAPTION: parse_error
parser.clearActiveFormattingTillMarker()
parser.insertionMode = IN_TABLE
reprocess token
)
("</body>", "</col>", "</colgroup>", "</html>", "</tbody>", "</td>",
"</tfoot>", "</th>", "</thead>", "</tr>") => (block: parse_error)
_ => (block: parser.processInHTMLContent(token, IN_BODY))
of IN_COLUMN_GROUP:
match token:
AsciiWhitespace => (block: parser.insertCharacter(token.c))
TokenType.COMMENT => (block: parser.insertComment(token))
TokenType.DOCTYPE => (block: parse_error)
"<html>" => (block: parser.processInHTMLContent(token, IN_BODY))
"<col>" => (block:
discard parser.insertHTMLElement(token)
pop_current_node
)
"</colgroup>" => (block:
if parser.currentNode.tagType != TAG_COLGROUP:
parse_error
else:
pop_current_node
parser.insertionMode = IN_TABLE
)
"</col>" => (block: parse_error)
("<template>", "</template>") => (block:
parser.processInHTMLContent(token, IN_HEAD)
)
TokenType.EOF => (block: parser.processInHTMLContent(token, IN_BODY))
_ => (block:
if parser.currentNode.tagType != TAG_COLGROUP:
parse_error
else:
pop_current_node
parser.insertionMode = IN_TABLE
reprocess token
)
of IN_TABLE_BODY:
template clear_the_stack_back_to_a_table_body_context() =
while parser.currentNode.tagType notin {TAG_TBODY, TAG_TFOOT, TAG_THEAD, TAG_TEMPLATE, TAG_HTML}:
pop_current_node
match token:
"<tr>" => (block:
clear_the_stack_back_to_a_table_body_context
discard parser.insertHTMLElement(token)
parser.insertionMode = IN_ROW
)
("<th>", "<td>") => (block:
parse_error
clear_the_stack_back_to_a_table_body_context
discard parser.insertHTMLElement(Token(t: START_TAG, tagtype: TAG_TR))
parser.insertionMode = IN_ROW
reprocess token
)
("</tbody>", "</tfoot>", "</thead>") => (block:
if not parser.openElements.hasElementInTableScope(token.tagtype):
parse_error
else:
clear_the_stack_back_to_a_table_body_context
pop_current_node
parser.insertionMode = IN_TABLE
)
("<caption>", "<col>", "<colgroup>", "<tbody>", "<tfoot>", "<thead>",
"</table>") => (block:
if not parser.openElements.hasElementInTableScope({TAG_TBODY, TAG_THEAD, TAG_TFOOT}):
parse_error
else:
clear_the_stack_back_to_a_table_body_context
pop_current_node
parser.insertionMode = IN_TABLE
reprocess token
)
("</body>", "</caption>", "</col>", "</colgroup>", "</html>", "</td>",
"</th>", "</tr>") => (block:
parse_error
)
_ => (block: parser.processInHTMLContent(token, IN_TABLE))
of IN_ROW:
template clear_the_stack_back_to_a_table_row_context() =
while parser.currentNode.tagType notin {TAG_TR, TAG_TEMPLATE, TAG_HTML}:
pop_current_node
match token:
("<th>", "<td>") => (block:
clear_the_stack_back_to_a_table_row_context
discard parser.insertHTMLElement(token)
parser.insertionMode = IN_CELL
parser.activeFormatting.add((nil, nil))
)
"</tr>" => (block:
if not parser.openElements.hasElementInTableScope(TAG_TR):
parse_error
else:
clear_the_stack_back_to_a_table_row_context
pop_current_node
parser.insertionMode = IN_TABLE_BODY
)
("<caption>", "<col>", "<colgroup>", "<tbody>", "<tfoot>", "<thead>",
"<tr>", "</table>") => (block:
if not parser.openElements.hasElementInTableScope(TAG_TR):
parse_error
else:
clear_the_stack_back_to_a_table_row_context
pop_current_node
parser.insertionMode = IN_TABLE_BODY
reprocess token
)
("</tbody>", "</tfoot>", "</thead>") => (block:
if not parser.openElements.hasElementInTableScope(token.tagtype):
parse_error
elif not parser.openElements.hasElementInTableScope(TAG_TR):
discard
else:
clear_the_stack_back_to_a_table_row_context
pop_current_node
parser.insertionMode = IN_BODY
reprocess token
)
("</body>", "</caption>", "</col>", "</colgroup>", "</html>", "</td>",
"</th>") => (block: parse_error)
_ => (block: parser.processInHTMLContent(token, IN_TABLE))
of IN_CELL:
template close_cell() =
parser.generateImpliedEndTags()
if parser.currentNode.tagType notin {TAG_TD, TAG_TH}: parse_error
while parser.popElement().tagType notin {TAG_TD, TAG_TH}: discard
parser.clearActiveFormattingTillMarker()
parser.insertionMode = IN_ROW
match token:
("</td>", "</th>") => (block:
if not parser.openElements.hasElementInTableScope(token.tagtype):
parse_error
else:
parser.generateImpliedEndTags()
if parser.currentNode.tagType != token.tagtype: parse_error
while parser.popElement().tagType != token.tagtype: discard
parser.clearActiveFormattingTillMarker()
parser.insertionMode = IN_ROW
)
("<caption>", "<col>", "<colgroup>", "<tbody>", "<td>", "<tfoot>",
"<thead>", "<tr>") => (block:
if not parser.openElements.hasElementInTableScope({TAG_TD, TAG_TH}):
parse_error
else:
close_cell
reprocess token
)
("</body>", "</caption>", "</col>", "</colgroup>",
"</html>") => (block: parse_error)
("</table>", "</tbody>", "</tfoot>", "</thead>", "</tr>") => (block:
if not parser.openElements.hasElementInTableScope(token.tagtype):
parse_error
else:
close_cell
reprocess token
)
_ => (block: parser.processInHTMLContent(token, IN_BODY))
of IN_SELECT:
match token:
'\0' => (block: parse_error)
TokenType.CHARACTER_ASCII => (block: parser.insertCharacter(token.c))
TokenType.CHARACTER => (block: parser.insertCharacter(token.r))
TokenType.DOCTYPE => (block: parse_error)
"<html>" => (block: parser.processInHTMLContent(token, IN_BODY))
"<option>" => (block:
if parser.currentNode.tagType == TAG_OPTION:
pop_current_node
discard parser.insertHTMLElement(token)
)
"<optgroup>" => (block:
if parser.currentNode.tagType == TAG_OPTION:
pop_current_node
if parser.currentNode.tagType == TAG_OPTGROUP:
pop_current_node
discard parser.insertHTMLElement(token)
)
"</optgroup>" => (block:
if parser.currentNode.tagType == TAG_OPTION:
if parser.openElements.len > 1 and parser.openElements[^2].tagType == TAG_OPTGROUP:
pop_current_node
if parser.currentNode.tagType == TAG_OPTGROUP:
pop_current_node
else:
parse_error
)
"</option>" => (block:
if parser.currentNode.tagType == TAG_OPTION:
pop_current_node
else:
parse_error
)
"</select>" => (block:
if not parser.openElements.hasElementInSelectScope(TAG_SELECT):
parse_error
else:
while parser.popElement().tagType != TAG_SELECT: discard
parser.resetInsertionMode()
)
"<select>" => (block:
parse_error
if parser.openElements.hasElementInSelectScope(TAG_SELECT):
while parser.popElement().tagType != TAG_SELECT: discard
parser.resetInsertionMode()
)
("<input>", "<keygen>", "<textarea>") => (block:
parse_error
if not parser.openElements.hasElementInSelectScope(TAG_SELECT):
discard
else:
while parser.popElement().tagType != TAG_SELECT: discard
parser.resetInsertionMode()
reprocess token
)
("<script>", "<template>", "</template>") => (block: parser.processInHTMLContent(token, IN_HEAD))
TokenType.EOF => (block: parser.processInHTMLContent(token, IN_BODY))
_ => (block: parse_error)
of IN_SELECT_IN_TABLE:
match token:
("<caption>", "<table>", "<tbody>", "<tfoot>", "<thead>", "<tr>", "<td>",
"<th>") => (block:
parse_error
while parser.popElement().tagType != TAG_SELECT: discard
parser.resetInsertionMode()
reprocess token
)
("</caption>", "</table>", "</tbody>", "</tfoot>", "</thead>", "</tr>",
"</td>", "</th>") => (block:
parse_error
if not parser.openElements.hasElementInTableScope(token.tagtype):
discard
else:
while parser.popElement().tagType != TAG_SELECT: discard
parser.resetInsertionMode()
reprocess token
)
_ => (block: parser.processInHTMLContent(token, IN_SELECT))
of IN_TEMPLATE:
match token:
(TokenType.CHARACTER_ASCII, TokenType.CHARACTER, TokenType.DOCTYPE) => (block:
parser.processInHTMLContent(token, IN_BODY)
)
("<base>", "<basefont>", "<bgsound>", "<link>", "<meta>", "<noframes>",
"<script>", "<style>", "<template>", "<title>", "</template>") => (block:
parser.processInHTMLContent(token, IN_HEAD)
)
("<caption>", "<colgroup>", "<tbody>", "<tfoot>", "<thead>") => (block:
discard parser.templateModes.pop()
parser.templateModes.add(IN_TABLE)
parser.insertionMode = IN_TABLE
reprocess token
)
"<col>" => (block:
discard parser.templateModes.pop()
parser.templateModes.add(IN_COLUMN_GROUP)
parser.insertionMode = IN_COLUMN_GROUP
reprocess token
)
"<tr>" => (block:
discard parser.templateModes.pop()
parser.templateModes.add(IN_TABLE_BODY)
parser.insertionMode = IN_TABLE_BODY
reprocess token
)
("<td>", "<th>") => (block:
discard parser.templateModes.pop()
parser.templateModes.add(IN_ROW)
parser.insertionMode = IN_ROW
reprocess token
)
TokenType.START_TAG => (block:
discard parser.templateModes.pop()
parser.templateModes.add(IN_BODY)
parser.insertionMode = IN_BODY
reprocess token
)
TokenType.END_TAG => (block: parse_error)
TokenType.EOF => (block:
if not parser.openElements.hasElement(TAG_TEMPLATE):
discard # stop
else:
parse_error
while parser.popElement().tagType != TAG_TEMPLATE: discard
parser.clearActiveFormattingTillMarker()
discard parser.templateModes.pop()
parser.resetInsertionMode()
reprocess token
)
of AFTER_BODY:
match token:
AsciiWhitespace => (block: parser.processInHTMLContent(token, IN_BODY))
TokenType.COMMENT => (block: parser.insertComment(token, last_child_of(parser.openElements[0])))
TokenType.DOCTYPE => (block: parse_error)
"<html>" => (block: parser.processInHTMLContent(token, IN_BODY))
"</html>" => (block:
if parser.fragment:
parse_error
else:
parser.insertionMode = AFTER_AFTER_BODY
)
TokenType.EOF => (block: discard) # stop
_ => (block:
parse_error
parser.insertionMode = IN_BODY
reprocess token
)
of IN_FRAMESET:
match token:
AsciiWhitespace => (block: parser.insertCharacter(token.c))
TokenType.COMMENT => (block: parser.insertComment(token))
TokenType.DOCTYPE => (block: parse_error)
"<html>" => (block: parser.processInHTMLContent(token, IN_BODY))
"<frameset>" => (block:
if parser.currentNode == parser.document.html:
parse_error
else:
pop_current_node
if not parser.fragment and parser.currentNode.tagType != TAG_FRAMESET:
parser.insertionMode = AFTER_FRAMESET
)
"<frame>" => (block:
discard parser.insertHTMLElement(token)
pop_current_node
)
"<noframes>" => (block: parser.processInHTMLContent(token, IN_HEAD))
TokenType.EOF => (block:
if parser.currentNode != parser.document.html: parse_error
# stop
)
_ => (block: parse_error)
of AFTER_FRAMESET:
match token:
AsciiWhitespace => (block: parser.insertCharacter(token.c))
TokenType.COMMENT => (block: parser.insertComment(token))
TokenType.DOCTYPE => (block: parse_error)
"<html>" => (block: parser.processInHTMLContent(token, IN_BODY))
"</html>" => (block: parser.insertionMode = AFTER_AFTER_FRAMESET)
"<noframes>" => (block: parser.processInHTMLContent(token, IN_HEAD))
TokenType.EOF => (block: discard) # stop
_ => (block: parse_error)
of AFTER_AFTER_BODY:
match token:
TokenType.COMMENT => (block: parser.insertComment(token, last_child_of(parser.document)))
(TokenType.DOCTYPE, AsciiWhitespace, "<html>") => (block: parser.processInHTMLContent(token, IN_BODY))
TokenType.EOF => (block: discard) # stop
_ => (block:
parse_error
parser.insertionMode = IN_BODY
reprocess token
)
of AFTER_AFTER_FRAMESET:
match token:
TokenType.COMMENT => (block: parser.insertComment(token, last_child_of(parser.document)))
(TokenType.DOCTYPE, AsciiWhitespace, "<html>") => (block: parser.processInHTMLContent(token, IN_BODY))
TokenType.EOF => (block: discard) # stop
"<noframes>" => (block: parser.processInHTMLContent(token, IN_HEAD))
_ => (block: parse_error)
proc processInForeignContent(parser: var HTML5Parser, token: Token) =
macro `=>`(v: typed, body: untyped): untyped =
quote do:
discard (`v`, proc() = `body`)
template script_end_tag() =
pop_current_node
#TODO document.write (?)
#TODO SVG
template any_other_end_tag() =
if parser.currentNode.localName != token.tagname: parse_error
for i in countdown(parser.openElements.high, 1):
let node = parser.openElements[i]
if node.localName == token.tagname:
while parser.popElement() != node: discard
break
if node.namespace == Namespace.HTML: break
parser.processInHTMLContent(token)
const CaseTable = {
"altglyph": "altGlyph",
"altglyphdef": "altGlyphDef",
"altglyphitem": "altGlyphItem",
"animatecolor": "animateColor",
"animatemotion": "animateMotion",
"animatetransform": "animateTransform",
"clippath": "clipPath",
"feblend": "feBlend",
"fecolormatrix": "feColorMatrix",
"fecomponenttransfer": "feComponentTransfer",
"fecomposite": "feComposite",
"feconvolvematrix": "feConvolveMatrix",
"fediffuselighting": "feDiffuseLighting",
"fedisplacementmap": "feDisplacementMap",
"fedistantlight": "feDistantLight",
"fedropshadow": "feDropShadow",
"feflood": "feFlood",
"fefunca": "feFuncA",
"fefuncb": "feFuncB",
"fefuncg": "feFuncG",
"fefuncr": "feFuncR",
"fegaussianblur": "feGaussianBlur",
"feimage": "feImage",
"femerge": "feMerge",
"femergenode": "feMergeNode",
"femorphology": "feMorphology",
"feoffset": "feOffset",
"fepointlight": "fePointLight",
"fespecularlighting": "feSpecularLighting",
"fespotlight": "feSpotLight",
"fetile": "feTile",
"feturbulence": "feTurbulence",
"foreignobject": "foreignObject",
"glyphref": "glyphRef",
"lineargradient": "linearGradient",
"radialgradient": "radialGradient",
"textpath": "textPath",
}.toTable()
match token:
'\0' => (block:
parse_error
parser.insertCharacter(Rune(0xFFFD))
)
AsciiWhitespace => (block: parser.insertCharacter(token.c))
TokenType.CHARACTER_ASCII => (block: parser.insertCharacter(token.c))
TokenType.CHARACTER => (block: parser.insertCharacter(token.r))
TokenType.DOCTYPE => (block: parse_error)
("<b>", "<big>", "<blockquote>", "<body>", "<br>", "<center>", "<code>",
"<dd>", "<div>", "<dl>", "<dt>", "<em>", "<embed>", "<h1>", "<h2>", "<h3>",
"<h4>", "<h5>", "<h6>", "<head>", "<hr>", "<i>", "<img>", "<li>",
"<listing>", "<menu>", "<meta>", "<nobr>", "<ol>", "<p>", "<pre>",
"<ruby>", "<s>", "<small>", "<span>", "<strong>", "<strike>", "<sub>",
"<sup>", "<table>", "<tt>", "<u>", "<ul>", "<var>") => (block:
parse_error
#NOTE MathML not implemented
while not (parser.currentNode.isHTMLIntegrationPoint() or parser.currentNode.inHTMLNamespace()):
pop_current_node
parser.processInHTMLContent(token)
)
TokenType.START_TAG => (block:
#NOTE MathML not implemented
if parser.adjustedCurrentNode.namespace == Namespace.SVG:
if token.tagname in CaseTable:
token.tagname = CaseTable[token.tagname]
adjustSVGAttributes(token)
#TODO adjust foreign attributes
let element = parser.insertForeignElement(token, parser.adjustedCurrentNode.namespace)
if token.selfclosing and element.inSVGNamespace():
script_end_tag
else:
pop_current_node
)
"</script>" => (block:
if parser.currentNode.namespace == Namespace.SVG and parser.currentNode.localName == "script": #TODO SVG
script_end_tag
else:
any_other_end_tag
)
TokenType.END_TAG => (block: any_other_end_tag)
proc constructTree(parser: var HTML5Parser): Document =
for token in parser.tokenizer.tokenize:
if parser.ignoreLF:
parser.ignoreLF = false
if token.t == CHARACTER_ASCII and token.c == '\n':
continue
if parser.openElements.len == 0 or
parser.adjustedCurrentNode.inHTMLNamespace() or
parser.adjustedCurrentNode.isHTMLIntegrationPoint() and token.t in {START_TAG, CHARACTER, CHARACTER_ASCII} or
token.t == EOF:
#NOTE MathML not implemented
parser.processInHTMLContent(token)
else:
parser.processInForeignContent(token)
if parser.needsreinterpret:
return nil
return parser.document
proc finishParsing(parser: var HTML5Parser) =
while parser.openElements.len > 0:
pop_current_node
while parser.document.scriptsToExecOnLoad.len > 0:
#TODO spin event loop
let script = parser.document.scriptsToExecOnLoad.popFirst()
script.execute()
#TODO events
proc parseHTML*(inputStream: Stream, cs = none(Charset), fallbackcs = CHARSET_UTF_8, window: Window = nil, url: URL = nil): (Document, Charset) =
var parser: HTML5Parser
var bom: string
if cs.isSome:
parser.charset = cs.get
parser.confidence = CONFIDENCE_CERTAIN
else:
# bom sniff
const u8bom = char(0xEF) & char(0xBB) & char(0xBF)
const bebom = char(0xFE) & char(0xFF)
const lebom = char(0xFF) & char(0xFE)
bom = inputStream.readStr(2)
if bom == bebom:
parser.charset = CHARSET_UTF_16_BE
parser.confidence = CONFIDENCE_CERTAIN
bom = ""
elif bom == lebom:
parser.charset = CHARSET_UTF_16_LE
parser.confidence = CONFIDENCE_CERTAIN
bom = ""
else:
bom &= inputStream.readChar()
if bom == u8bom:
parser.charset = CHARSET_UTF_8
parser.confidence = CONFIDENCE_CERTAIN
bom = ""
else:
parser.charset = fallbackcs
let decoder = newDecoderStream(inputStream, parser.charset)
for c in bom:
decoder.prepend(cast[uint32](c))
parser.document = newDocument()
parser.document.contentType = "text/html"
if window != nil:
parser.document.window = window
window.document = parser.document
parser.document.url = url
parser.tokenizer = newTokenizer(decoder)
let document = parser.constructTree()
parser.finishParsing()
return (document, parser.charset)
proc newDOMParser*(): DOMParser {.jsctor.} =
new(result)
proc parseFromString(parser: DOMParser, str: string, t: string): Document {.jserr, jsfunc.} =
case t
of "text/html":
let (res, _) = parseHTML(newStringStream(str))
return res
of "text/xml", "application/xml", "application/xhtml+xml", "image/svg+xml":
JS_ERR JS_InternalError, "XML parsing is not supported yet"
else:
JS_ERR JS_TypeError, "Invalid mime type"
proc addHTMLModule*(ctx: JSContext) =
ctx.registerType(DOMParser)
|