1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
|
<!DOCTYPE HTML>
<html lang="en-GB-hixie">
<head>
<title>HTML 5</title>
<link href="/style/specification" type="text/css" rel="stylesheet">
<link href="/images/icon" rel="icon">
<style type="text/css">
h4 + .element { margin-top: -2.5em; padding-top: 2em; }
h4 + p + .element { margin-top: -5em; padding-top: 4em; }
.element { background: #EEFFEE; color: black; margin: 0 0 1em -1em; padding: 0 1em 0.25em 0.75em; border-left: solid #99FF99 0.25em; -padding: 0; /* that last decl is for IE6. Try removing it, it's hilarious! */ }
.proposal { border: blue solid; padding: 1em; }
table.matrix, table.matrix td { border: none; text-align: right; }
table.matrix { margin-left: 2em; }
</style>
<link href="section-tokenisation.html#nav-bar" rel="prev" title="8.2.3. Tokenisation"><link href="index.html#contents" rel="index" title="Table of contents"><link href="section-namespaces.html#nav-bar" rel="next" title="8.3. Namespaces"></head><body class="draft"><div class="head">
<p><a href="http://www.whatwg.org/" class="logo" rel="home"><img src="/images/logo" alt="WHATWG"></a></p>
<h1 id="html-5">HTML 5</h1>
<h2 id="working" class="no-num no-toc">Working Draft — 12 June 2007</h2></div><nav id="nav-bar"><a href="section-tokenisation.html#nav-bar">< 8.2.3. Tokenisation</a> – <a href="index.html#contents">Table of contents</a> – <a href="section-namespaces.html#nav-bar">8.3. Namespaces ></a></nav><h4 id="tree-construction"><span class="secno">8.2.4. </span><dfn id="tree-construction0">Tree construction</dfn></h4>
<p>The input to the tree construction stage is a sequence of tokens from
the <a href="section-tokenisation.html#tokenisation0">tokenisation</a> stage. The tree construction
stage is associated with a DOM <code>Document</code> object when a parser
is created. The "output" of this stage consists of dynamically modifying
or extending that document's DOM tree.
</p><p>Tree construction passes through several phases. Initially, UAs must act
according to the steps described as being those of <a href="#the-initial0">the initial phase</a>.
</p><p>This specification does not define when an interactive user agent has to
render the <code>Document</code> available to the user, or when it has to
begin accepting user input.
</p><p>When the steps below require the UA to <dfn id="append">append a
character</dfn> to a node, the UA must collect it and all subsequent
consecutive characters that would be appended to that node, and insert one
<code>Text</code> node whose data is the concatenation of all those
characters.
</p><p id="mutation-during-parsing">DOM mutation events must not fire for changes
caused by the UA parsing the document. (Conceptually, the parser is not
mutating the DOM, it is constructing it.) This includes the parsing of any
content inserted using <code title="dom-document-write-HTML"><a href="section-dynamic.html#document.write0">document.write()</a></code> and <code title="dom-document-writeln"><a href="section-dynamic.html#document.writeln">document.writeln()</a></code> calls.<!--
XXX xref -->
<a href="#refsDOM3EVENTS">[DOM3EVENTS]</a></p>
<!-- XXX
what abotu innerHTML? -->
<p class="note">Not all of the tag names mentioned below are conformant tag
names in this specification; many are included to handle legacy content.
They still form part of the algorithm that implementations are required to
implement to claim conformance.
</p><p class="note">The algorithm described below places no limit on the depth of
the DOM tree generated, or on the length of tag names, attribute names,
attribute values, text nodes, etc. While implementators are encouraged to
avoid arbitrary limits, it is recognised that <a href="section-conformance.html#hardwareLimitations">practical concerns</a> will likely force user
agents to impose nesting depths.
</p><h5 id="the-initial"><span class="secno">8.2.4.1. </span><dfn id="the-initial0">The initial phase</dfn></h5>
<p>Initially, the tree construction stage must handle each token emitted
from the <a href="section-tokenisation.html#tokenisation0">tokenisation</a> stage as follows:
</p><dl class="switch">
<dt>A DOCTYPE token that is marked as being in error
</dt><dt>A comment token
</dt><dt>A start tag token
</dt><dt>An end tag token
</dt><dt>A character token that is not one of one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM
FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dt>An end-of-file token
</dt><dd>
<p>This specification does not define how to handle this case. In
particular, user agents may ignore the entirety of this specification
altogether for such documents, and instead invoke special parse modes
with a greater emphasis on backwards compatibility.</p>
<div class="note">
<p>Browsers in particular have generally used DOCTYPE-based sniffing to
invoke an "alternative conformance mode" known as <em>quirks mode</em>
on certain documents. In this mode, emphasis is put on legacy
compatibility rather than on standards compliance. This specification
takes no position on this behaviour; documents without DOCTYPEs or with
DOCTYPEs that do not conform to the syntax allowed by this
specification are considered to be out of scope of this specification.</p>
</div>
<div class="big-issue">
<p>As far as parsing goes, the quirks I know of are:</p>
<ul>
<li>Comment parsing is different.
</li><li>The following is considered one script block (!):
<pre><script><!-- document.write('</script>'); --></script></pre>
</li><li><code title=""></br></code> and <code title=""></p></code> do
magical things.
</li><li><code><a href="section-prose.html#p">p</a></code> can contain <code><a href="section-tabular.html#table">table</a></code>
</li><li>Safari and IE have special parsing rules for <% ... %> (even
in standards mode, though clearly this should be quirks-only).
</li></ul>
<p>Maybe we should just adopt all those and be done with it. One parsing
mode to rule them all. Or legitimise/codify the quirks mode parsing in
some way.</p>
<p>Would be interesting to do a search to see how many pages hit each of
the above.</p>
<!-- biased by page rank? --></div>
</dd><dt>A DOCTYPE token marked as being correct
</dt><dd>
<p>Append a <code>DocumentType</code> node to the <code>Document</code>
node, with the <code title="">name</code> attribute set to the name
given in the DOCTYPE token (which will be "HTML"), and the other
attributes specific to <code>DocumentType</code> objects set to null,
empty lists, or the empty string as appropriate.</p>
<p>Then, switch to <a href="#the-root1">the root element phase</a> of the
tree construction stage.</p>
<!-- XXX should set doctype on the Document object, too, unless
spec is defined to already point to it if you append -->
</dd><dt>A character token that <em>is</em> one of one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM
FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p><a href="#append" title="append a character">Append that character</a>
to the <code>Document</code> node.</p>
</dd></dl>
<h5 id="the-root0"><span class="secno">8.2.4.2. </span><dfn id="the-root1">The
root element phase</dfn></h5>
<p>After <a href="#the-initial0">the initial phase</a>, as each token is
emitted from the <a href="section-tokenisation.html#tokenisation0">tokenisation</a> stage, it must
be processed as described in this section.
</p><dl class="switch">
<dt>A DOCTYPE token
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <code>Document</code> object
with the <code title="">data</code> attribute set to the data given in
the comment token.</p>
</dd><dt>A character token that is one of one of U+0009 CHARACTER TABULATION,
U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p><a href="#append" title="append a character">Append that character</a>
to the <code>Document</code> node.</p>
</dd><dt>A character token that is <em>not</em> one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM
FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dt>A start tag token
</dt><dt>An end tag token
</dt><dt>An end-of-file token
</dt><dd>
<p>Create an <code><a href="section-elements.html#htmlelement">HTMLElement</a></code> node
with the tag name <code><a href="section-the-root.html#html">html</a></code>, in the <a href="section-namespaces.html#html-namespace0">HTML namespace</a>. Append it to the
<code>Document</code> object. Switch to <a href="#the-main0">the main
phase</a> and reprocess the current token.</p>
<p class="big-issue">Should probably make end tags be ignored, so that
"</head><!-- --><html>" puts the comment befor the root node
(or should we?)</p>
</dd></dl>
<p>The root element can end up being removed from the <code>Document</code>
object, e.g. by scripts; nothing in particular happens in such cases,
content continues being appended to the nodes as described in the next
section.
</p><h5 id="the-main"><span class="secno">8.2.4.3. </span><dfn id="the-main0">The
main phase</dfn></h5>
<p>After <a href="#the-root1">the root element phase</a>, each token
emitted from the <a href="section-tokenisation.html#tokenisation0">tokenisation</a> stage must be
processed as described in <em>this</em> section. This is by far the most
involved part of parsing an HTML document.
</p><p>The tree construction stage in this phase has several pieces of state: a
<a href="#stack">stack of open elements</a>, a <a href="#list-of4">list of
active formatting elements</a>, a <a href="#head-element"><code title="">head</code> element pointer</a>, a <a href="#form-element"><code title="">form</code> element pointer</a>, and an <a href="#insertion0">insertion mode</a>.
</p><p class="big-issue">We could just fold insertion modes and phases into one
concept (and duplicate the two rules common to all insertion modes into
all of them).
</p><h6 id="the-stack"><span class="secno">8.2.4.3.1. </span>The stack of open
elements</h6>
<p>Initially the <dfn id="stack">stack of open elements</dfn> contains just
the <code><a href="section-the-root.html#html">html</a></code> root element node created in the
<a href="#the-root1" title="the root element phase">last phase</a> before
switching to <em>this</em> phase (or, in the <a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>, the <code><a href="section-the-root.html#html">html</a></code> element created to represent the element
whose <code title="dom-innerHTML-HTML"><a href="section-dynamic.html#innerhtml0">innerHTML</a></code> attribute is being set). That's
the topmost node of the stack. It never gets popped off the stack. (This
stack grows downwards.)
</p><p>The <dfn id="current4">current node</dfn> is the bottommost node in this
stack.
</p><p>Elements in the stack fall into the following categories:
</p><dl>
<dt><dfn id="special">Special</dfn>
</dt><dd>
<p>The following HTML elements have varying levels of special parsing
rules: <code><a href="section-sections.html#address">address</a></code>, <code><a href="section-the-canvas.html#area">area</a></code>, <code><a href="section-document.html#base">base</a></code>,
<code>basefont</code>, <code>bgsound</code>, <code><a href="section-sections.html#blockquote">blockquote</a></code>, <code><a href="section-sections.html#body0">body</a></code>, <code><a href="section-prose.html#br">br</a></code>,
<code>center</code>, <code><a href="section-tabular.html#col">col</a></code>, <code><a href="section-tabular.html#colgroup">colgroup</a></code>, <code><a href="section-lists0.html#dd">dd</a></code>,
<code>dir</code>, <code><a href="section-miscellaneous.html#div">div</a></code>, <code><a href="section-lists0.html#dl">dl</a></code>, <code><a href="section-lists0.html#dt">dt</a></code>, <code><a href="section-embedded.html#embed">embed</a></code>, <code>fieldset</code>,
<code>form</code>, <code>frame</code>, <code>frameset</code>, <code><a href="section-sections.html#h1">h1</a></code>, <code><a href="section-sections.html#h2">h2</a></code>, <code><a href="section-sections.html#h3">h3</a></code>, <code><a href="section-sections.html#h4">h4</a></code>, <code><a href="section-sections.html#h5">h5</a></code>, <code><a href="section-sections.html#h6">h6</a></code>, <code><a href="section-document.html#head">head</a></code>, <code><a href="section-prose.html#hr">hr</a></code>,
<code><a href="section-embedded.html#iframe">iframe</a></code>,
<code>image</code><!-- XXX ? this isn't an element that can end up
on the stack-->,
<code><a href="section-embedded.html#img">img</a></code>, <code>input</code>,
<code>isindex</code>, <code><a href="section-lists0.html#li">li</a></code>, <code><a href="section-document.html#link">link</a></code>, <code>listing</code>, <code><a href="section-the-command.html#menu">menu</a></code>, <code><a href="section-document.html#meta0">meta</a></code>,
<code>noembed</code>, <code>noframes</code>, <code><a href="section-scripting0.html#noscript">noscript</a></code>, <code><a href="section-lists0.html#ol">ol</a></code>,
<code>optgroup</code>, <code>option</code>, <code><a href="section-prose.html#p">p</a></code>, <code><a href="section-embedded.html#param">param</a></code>,
<code>plaintext</code>, <code><a href="section-preformatted.html#pre">pre</a></code>, <code><a href="section-scripting0.html#script0">script</a></code>, <code>select</code>,
<code>spacer</code>, <code><a href="section-document.html#style">style</a></code>, <code><a href="section-tabular.html#tbody">tbody</a></code>, <code>textarea</code>, <code><a href="section-tabular.html#tfoot0">tfoot</a></code>, <code><a href="section-tabular.html#thead0">thead</a></code>, <code><a href="section-document.html#title1">title</a></code>, <code><a href="section-tabular.html#tr">tr</a></code>,
<code><a href="section-lists0.html#ul">ul</a></code>, and <code>wbr</code>.
</p></dd><dt><dfn id="scoping">Scoping</dfn>
</dt><dd>
<p>The following HTML elements introduce new <a href="#have-an" title="has an element in scope">scopes</a> for various parts of the
parsing: <code>button</code>, <code><a href="section-tabular.html#caption0">caption</a></code>, <code><a href="section-the-root.html#html">html</a></code>, <code>marquee</code>, <code><a href="section-embedded.html#object">object</a></code>, <code><a href="section-tabular.html#table">table</a></code>, <code><a href="section-tabular.html#td">td</a></code> and
<code><a href="section-tabular.html#th">th</a></code>.
</p></dd><dt><dfn id="formatting">Formatting</dfn>
</dt><dd>
<p>The following HTML elements are those that end up in the <a href="#list-of4">list of active formatting elements</a>: <code><a href="section-phrase.html#a">a</a></code>, <code><a href="section-phrase.html#b">b</a></code>,
<code>big</code>, <code><a href="section-phrase.html#em">em</a></code>, <code><a href="section-presentational.html#font">font</a></code>, <code><a href="section-phrase.html#i">i</a></code>,
<code>nobr</code>, <code>s</code>, <code><a href="section-phrase.html#small">small</a></code>, <code>strike</code>, <code><a href="section-phrase.html#strong">strong</a></code>, <code>tt</code>, and <code>u</code>.
</p></dd><dt><dfn id="phrasing">Phrasing</dfn>
</dt><dd>
<p>All other elements found while parsing an HTML document.
</p></dd></dl>
<p class="big-issue">Still need to add these new elements to the lists:
<code><a href="section-scripting0.html#event-source">event-source</a></code>, <code><a href="section-sections.html#section">section</a></code>, <code><a href="section-sections.html#nav">nav</a></code>,
<code><a href="section-sections.html#article">article</a></code>, <code><a href="section-sections.html#aside">aside</a></code>, <code><a href="section-sections.html#header">header</a></code>,
<code><a href="section-sections.html#footer">footer</a></code>, <code><a href="section-interactive.html#datagrid0">datagrid</a></code>, <code><a href="section-the-command.html#command0">command</a></code>
</p><p>The <a href="#stack">stack of open elements</a> is said to <dfn id="have-an" title="has an element in scope">have an element in scope</dfn>
or <dfn id="have-an0" title="has an element in table scope">have an element
in <em>table scope</em></dfn> when the following algorithm terminates in a
match state:
</p><ol>
<li>
<p>Initialise <var title="">node</var> to be the <a href="#current4">current node</a> (the bottommost node of the stack).
</p></li><li>
<p>If <var title="">node</var> is the target node, terminate in a match
state.
</p></li><li>
<p>Otherwise, if <var title="">node</var> is a <code><a href="section-tabular.html#table">table</a></code> element, terminate in a failure state.
</p></li><li>
<p>Otherwise, if the algorithm is the "has an element in scope" variant
(rather than the "has an element in table scope" variant), and <var title="">node</var> is one of the following, terminate in a failure
state:</p>
<ul class="brief">
<li><code><a href="section-tabular.html#caption0">caption</a></code>
</li><li><code><a href="section-tabular.html#td">td</a></code>
</li><li><code><a href="section-tabular.html#th">th</a></code>
</li><li><code>button</code>
</li><li><code>marquee</code>
</li><li><code><a href="section-embedded.html#object">object</a></code>
</li></ul>
</li><li>
<p>Otherwise, if <var title="">node</var> is an <code><a href="section-the-root.html#html">html</a></code> element, terminate in a failure state.
(This can only happen if the <var title="">node</var> is the topmost
node of the <a href="#stack">stack of open elements</a>, and prevents
the next step from being invoked if there are no more elements in the
stack.)
</p></li><li>
<p>Otherwise, set <var title="">node</var> to the previous entry in the
<a href="#stack">stack of open elements</a> and return to step 2. (This
will never fail, since the loop will always terminate in the previous
step if the top of the stack is reached.)
</p></li></ol>
<p>Nothing happens if at any time any of the elements in the <a href="#stack">stack of open elements</a> are moved to a new location in,
or removed from, the <code>Document</code> tree. In particular, the stack
is not changed in this situation. This can cause, amongst other strange
effects, content to be appended to nodes that are no longer in the DOM.
</p><p class="note">In some cases (namely, when <a href="#adoptionAgency">closing
misnested formatting elements</a>), the stack is manipulated in a
random-access fashion.
</p><h6 id="the-list"><span class="secno">8.2.4.3.2. </span>The list of active
formatting elements</h6>
<p>Initially the <dfn id="list-of4">list of active formatting elements</dfn>
is empty. It is used to handle mis-nested <a href="#formatting" title="formatting">formatting element tags</a>.
</p><p>The list contains elements in the <a href="#formatting">formatting</a>
category, and scope markers. The scope markers are inserted when entering
buttons, <code><a href="section-embedded.html#object">object</a></code> elements, marquees,
table cells, and table captions, and are used to prevent formatting from
"leaking" into tables, buttons, <code><a href="section-embedded.html#object">object</a></code>
elements, and marquees.
</p><p>When the steps below require the UA to <dfn id="reconstruct">reconstruct
the active formatting elements</dfn>, the UA must perform the following
steps:
</p><ol>
<li>If there are no entries in the <a href="#list-of4">list of active
formatting elements</a>, then there is nothing to reconstruct; stop this
algorithm.
</li><li>If the last (most recently added) entry in the <a href="#list-of4">list of active formatting elements</a> is a marker, or
if it is an element that is in the <a href="#stack">stack of open
elements</a>, then there is nothing to reconstruct; stop this algorithm.
</li><li>Let <var title="">entry</var> be the last (most recently added)
element in the <a href="#list-of4">list of active formatting
elements</a>.
</li><li>If there are no entries before <var title="">entry</var> in the <a href="#list-of4">list of active formatting elements</a>, then jump to
step 8.
</li><li>Let <var title="">entry</var> be the entry one earlier than <var title="">entry</var> in the <a href="#list-of4">list of active formatting
elements</a>.
</li><li>If <var title="">entry</var> is neither a marker nor an element that
is also in the <a href="#stack">stack of open elements</a>, go to step 4.
</li><li>Let <var title="">entry</var> be the element one later than <var title="">entry</var> in the <a href="#list-of4">list of active formatting
elements</a>.
</li><li>Perform a shallow clone of the element <var title="">entry</var> to
obtain <var title="">clone</var>. <a href="#refsDOM3CORE">[DOM3CORE]</a>
</li><li>Append <var title="">clone</var> to the <a href="#current4">current
node</a> and push it onto the <a href="#stack">stack of open elements</a>
so that it is the new <a href="#current4">current node</a>.
</li><li>Replace the entry for <var title="">entry</var> in the list with an
entry for <var title="">clone</var>.
</li><li>If the entry for <var title="">clone</var> in the <a href="#list-of4">list of active formatting elements</a> is not the last
entry in the list, return to step 7.
</li></ol>
<p>This has the effect of reopening all the formatting elements that were
opened in the current body, cell, or caption (whichever is youngest) that
haven't been explicitly closed.
</p><p class="note">The way this specification is written, the <a href="#list-of4">list of active formatting elements</a> always consists of
elements in chronological order with the least recently added element
first and the most recently added element last (except for while steps 8
to 11 of the above algorithm are being executed, of course).
</p><p>When the steps below require the UA to <dfn id="clear0">clear the list of
active formatting elements up to the last marker</dfn>, the UA must
perform the following steps:
</p><ol>
<li>Let <var title="">entry</var> be the last (most recently added) entry
in the <a href="#list-of4">list of active formatting elements</a>.
</li><li>Remove <var title="">entry</var> from the <a href="#list-of4">list of
active formatting elements</a>.
</li><li>If <var title="">entry</var> was a marker, then stop the algorithm at
this point. The list has been cleared up to the last marker.
</li><li>Go to step 1.
</li></ol>
<h6 id="creating"><span class="secno">8.2.4.3.3. </span>Creating and inserting
HTML elements</h6>
<p>When the steps below require the UA to <dfn id="create" title="create an
element for the token">create an element for a token</dfn>, the UA must
create a node implementing the interface appropriate for the element type
corresponding to the tag name of the token (as given in the section of
this specification that defines that element, e.g. for an <code><a href="section-phrase.html#a">a</a></code> element it would be the <code><a href="section-phrase.html#htmlanchorelement">HTMLAnchorElement</a></code> interface), with
the tag name being the name of that element, with the node being in the <a href="section-namespaces.html#html-namespace0">HTML namespace</a>, and with the attributes on the
node being those given in the given token.
</p><p>When the steps below require the UA to <dfn id="insert">insert an HTML
element</dfn> for a token, the UA must first <a href="#create">create an
element for the token</a>, and then append this node to the <a href="#current4">current node</a>, and push it onto the <a href="#stack">stack of open elements</a> so that it is the new <a href="#current4">current node</a>.
</p><p>The steps below may also require that the UA insert an HTML element in a
particular place, in which case the UA must <a href="#create">create an
element for the token</a> and then insert or append the new node in the
location specified. (This happens in particular during the parsing of
tables with invalid content.)
</p><p>The interface appropriate for an element that is not defined in this
specification is <code><a href="section-elements.html#htmlelement">HTMLElement</a></code>.
</p><h6 id="closing"><span class="secno">8.2.4.3.4. </span>Closing elements that
have implied end tags</h6>
<p>When the steps below require the UA to <dfn id="generate">generate implied
end tags</dfn>, then, if the <a href="#current4">current node</a> is a
<code><a href="section-lists0.html#dd">dd</a></code> element, a <code><a href="section-lists0.html#dt">dt</a></code> element, an <code><a href="section-lists0.html#li">li</a></code>
element, a <code><a href="section-prose.html#p">p</a></code> element, a <code><a href="section-tabular.html#td">td</a></code> element, a <code><a href="section-tabular.html#th">th</a></code>
element, or a <code><a href="section-tabular.html#tr">tr</a></code> element, the UA must act
as if an end tag with the respective tag name had been seen and then <a href="#generate">generate implied end tags</a> again.
</p><p>The step that requires the UA to generate implied end tags but lists an
element to exclude from the process, then the UA must perform the above
steps as if that element was not in the above list.
</p><h6 id="the-element"><span class="secno">8.2.4.3.5. </span>The element pointers</h6>
<p>Initially the <dfn id="head-element"><code title="">head</code> element
pointer</dfn> and the <dfn id="form-element"><code title="">form</code>
element pointer</dfn> are both null.
</p><p>Once a <code><a href="section-document.html#head">head</a></code> element has been parsed
(whether implicitly or explicitly) the <a href="#head-element"><code title="">head</code> element pointer</a> gets set to point to this node.
</p><p>The <a href="#form-element"><code title="">form</code> element
pointer</a> points to the last <code>form</code> element that was opened
and whose end tag has not yet been seen. It is used to make form controls
associate with forms in the face of dramatically bad markup, for
historical reasons.
</p><h6 id="the-insertion"><span class="secno">8.2.4.3.6. </span>The insertion mode</h6>
<p>Initially the <dfn id="insertion0">insertion mode</dfn> is "<a href="#before2" title="insertion mode: before head">before head</a>". It
can change to "<a href="#in-head" title="insertion mode: in head">in
head</a>", "<a href="#after1" title="insertion mode: after head">after
head</a>", "<a href="#in-body" title="insertion mode: in body">in
body</a>", "<a href="#in-table" title="insertion mode: in table">in
table</a>", "<a href="#in-caption" title="insertion mode: in caption">in
caption</a>", "<a href="#in-column" title="insertion mode: in column
group">in column group</a>", "<a href="#in-table0" title="insertion mode:
in table body">in table body</a>", "<a href="#in-row" title="insertion
mode: in row">in row</a>", "<a href="#in-cell" title="insertion mode: in
cell">in cell</a>", "<a href="#in-select" title="insertion mode: in
select">in select</a>", "<a href="#after2" title="insertion mode: after
body">after body</a>", "<a href="#in-frameset" title="insertion mode: in
frameset">in frameset</a>", and "<a href="#after3" title="insertion mode:
after frameset">after frameset</a>" during the course of the parsing, as
described below. It affects how certain tokens are processed.
</p><p>If the tree construction stage is switched from <a href="#the-main0">the
main phase</a> to <a href="#the-trailing0">the trailing end phase</a> and
back again, the various pieces of state are not reset; the UA must act as
if the state was maintained.
</p><p>When the steps below require the UA to <dfn id="reset">reset the insertion
mode appropriately</dfn>, it means the UA must follow these steps:
</p><ol>
<li>Let <var title="">last</var> be false.
</li><li>Let <var title="">node</var> be the last node in the <a href="#stack">stack of open elements</a>.
</li><li>If <var title="">node</var> is the first node in the stack of open
elements, then set <var title="">last</var> to true. If the element whose
<code title="dom-innerHTML-HTML"><a href="section-dynamic.html#innerhtml0">innerHTML</a></code>
attribute is being set is neither a <code><a href="section-tabular.html#td">td</a></code>
element nor a <code><a href="section-tabular.html#th">th</a></code> element, then set <var title="">node</var> to the element whose <code title="dom-innerHTML-HTML"><a href="section-dynamic.html#innerhtml0">innerHTML</a></code>
attribute is being set. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code>
case</a>)
</li><li>If <var title="">node</var> is a <code>select</code> element, then
switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-select" title="insertion mode: in select">in select</a>" and
abort these steps. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code>
case</a>)
</li><li>If <var title="">node</var> is a <code><a href="section-tabular.html#td">td</a></code> or
<code><a href="section-tabular.html#th">th</a></code> element, then switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-cell" title="insertion mode: in cell">in cell</a>" and abort these steps.
</li><li>If <var title="">node</var> is a <code><a href="section-tabular.html#tr">tr</a></code>
element, then switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-row" title="insertion mode: in row">in row</a>" and abort these
steps.
</li><li>If <var title="">node</var> is a <code><a href="section-tabular.html#tbody">tbody</a></code>, <code><a href="section-tabular.html#thead0">thead</a></code>,
or <code><a href="section-tabular.html#tfoot0">tfoot</a></code> element, then switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-table0" title="insertion mode: in table body">in table body</a>" and abort these
steps.
</li><li>If <var title="">node</var> is a <code><a href="section-tabular.html#caption0">caption</a></code> element, then switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-caption" title="insertion mode: in caption">in caption</a>" and abort these steps.
</li><li>If <var title="">node</var> is a <code><a href="section-tabular.html#colgroup">colgroup</a></code> element, then switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-column" title="insertion mode: in column group">in column group</a>" and abort
these steps. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)
</li><li>If <var title="">node</var> is a <code><a href="section-tabular.html#table">table</a></code> element, then switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-table" title="insertion mode: in table">in table</a>" and abort these steps.
</li><li>If <var title="">node</var> is a <code><a href="section-document.html#head">head</a></code>
element, then switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-body" title="insertion mode: in body">in body</a>" ("<a href="#in-body" title="insertion mode: in body">in body</a>"! <em> not
"<a href="#in-head" title="insertion mode: in head">in head</a>"</em>!)
and abort these steps. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code>
case</a>)
</li><li>If <var title="">node</var> is a <code><a href="section-sections.html#body0">body</a></code> element, then switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-body" title="insertion mode: in body">in body</a>" and abort these steps.
</li><li>If <var title="">node</var> is a <code>frameset</code> element, then
switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-frameset" title="insertion mode: in frameset">in frameset</a>"
and abort these steps. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code>
case</a>)
</li><li>If <var title="">node</var> is an <code><a href="section-the-root.html#html">html</a></code> element, then: if the <a href="#head-element"><code title="">head</code> element pointer</a> is
null, switch the <a href="#insertion0">insertion mode</a> to "<a href="#before2" title="insertion mode: before head">before head</a>",
otherwise, switch the <a href="#insertion0">insertion mode</a> to "<a href="#after1" title="insertion mode: after head">after head</a>". In
either case, abort these steps. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</li>
<!-- XXX can the head element pointer ever be
non-null when we're going through these steps? -->
<li>If <var title="">last</var> is true, then set the <a href="#insertion0">insertion mode</a> to "<a href="#in-body" title="insertion mode: in body">in body</a>" and abort these steps. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)
</li><li>Let <var title="">node</var> now be the node before <var title="">node</var> in the <a href="#stack">stack of open elements</a>.
</li><li>Return to step 3.
</li></ol>
<!--When you don't have to handle innerHTML, you can use this
simplified explanation instead:
<ol>
<li><p>If the <span>stack of open elements</span> <span title="has
an element in table scope">has a <code>td</code> or <code>th</code>
element in table scope</span>, then switch the <span>insertion
mode</span> to "<span title="insertion mode: in cell">in
cell</span>".</p></li>
<li><p>Otherwise, if the <span>stack of open elements</span> <span
title="has an element in table scope">has a <code>tr</code> element
in table scope</span>, then switch the <span>insertion mode</span>
to "<span title="insertion mode: in row">in row</span>".</p></li>
<li><p>Otherwise, if the <span>stack of open elements</span> <span
title="has an element in table scope">has a <code>tbody</code>,
<code>tfoot</code>, or <code>thead</code> element in table
scope</span>, then switch the <span>insertion mode</span> to "<span
title="insertion mode: in table body">in table
body</span>".</p></li>
<li><p>Otherwise, if the <span>stack of open elements</span> <span
title="has an element in table scope">has a <code>caption</code>
element in table scope</span>, then switch the <span>insertion
mode</span> to "<span title="insertion mode: in caption">in
caption</span>".</p></li>
( you can't reach this point with a colgroup element on the
stack )
<li><p>Otherwise, if the <span>stack of open elements</span> <span
title="has an element in table scope">has a <code>table</code>
element in table scope</span>, then switch the <span>insertion
mode</span> to "<span title="insertion mode: in table">in
table</span>".</p></li>
<li><p>Otherwise, switch the <span>insertion mode</span> to "<span
title="insertion mode: in body">in body</span>".</p></li>
</ol>
-->
<h6 id="how-to0"><span class="secno">8.2.4.3.7. </span>How to handle tokens in
the main phase</h6>
<p>Tokens in the main phase must be handled as follows:
</p><dl class="switch">
<dt>A DOCTYPE token
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>A start tag token with the tag name "html"
</dt><dd>
<p>If this start tag token was not the first start tag token, then it is
a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>For each attribute on the token, check to see if the attribute is
already present on the top element of the <a href="#stack">stack of open
elements</a>. If it is not, add the attribute and its corresponding
value to that element.</p>
</dd><dt>An end-of-file token
</dt><dd>
<p><a href="#generate">Generate implied end tags.</a></p>
<p>If there are more than two nodes on the <a href="#stack">stack of open
elements</a>, or if there are two nodes but the second node is not a
<code><a href="section-sections.html#body0">body</a></code> node, this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>Otherwise, if the parser was originally created in order to handle the
setting of an element's <code title="dom-innerHTML-HTML"><a href="section-dynamic.html#innerhtml0">innerHTML</a></code> attribute, and there's more than
one element in the <a href="#stack">stack of open elements</a>, and the
second node on the <a href="#stack">stack of open elements</a> is not a
<code><a href="section-sections.html#body0">body</a></code> node, then this is a <a href="section-parsing.html#parse">parse error</a>. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p><a href="#stops">Stop parsing.</a></p>
<p class="big-issue">This fails because it doesn't imply HEAD and BODY
tags. We should probably expand out the insertion modes and merge them
with phases and then put the three things here into each insertion mode
instead of trying to factor them out so carefully.</p>
</dd><dt>Anything else
</dt><dd>
<p>Depends on the <a href="#insertion0">insertion mode</a>:</p>
<dl class="switch">
<dt>If the <a href="#insertion0">insertion mode</a> is "<dfn id="before2" title="insertion mode: before head">before head</dfn>"
</dt><dd>
<p>Handle the token as follows:</p>
<dl class="switch">
<dt>A character token that is one of one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p><a href="#append" title="append a character">Append the
character</a> to the <a href="#current4">current node</a>.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <a href="#current4">current node</a> with the <code title="">data</code> attribute set to the data given in the comment
token.</p>
</dd><dt>A start tag token with the tag name "head"
</dt><dd>
<p><a href="#create">Create an element for the token</a>.</p>
<p>Set the <a href="#head-element"><code title="">head</code> element
pointer</a> to this new element node.</p>
<p>Append the new element to the <a href="#current4">current node</a>
and push it onto the <a href="#stack">stack of open elements</a>.</p>
<p>Change the <a href="#insertion0">insertion mode</a> to "<a href="#in-head" title="insertion mode: in head">in head</a>".</p>
</dd><dt>A start tag token whose tag name is one of: "base", "link",
"meta", "script", "style", "title"
</dt><dd>
<p>Act as if a start tag token with the tag name "head" and no
attributes had been seen, then reprocess the current token.</p>
<p class="note">This will result in a <code><a href="section-document.html#head">head</a></code> element being generated, and with the
current token being reprocessed in the "<a href="#in-head" title="insertion mode: in head">in head</a>" <a href="#insertion0">insertion mode</a>.</p>
</dd><dt>An end tag with the tag name "html"
</dt><dd>
<p>Act as if a start tag token with the tag name "head" and no
attributes had been seen, then reprocess the current token.</p>
</dd><dt>Any other end tag
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>A character token that is <em>not</em> one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dt>Any other start tag token
</dt><dd>
<p>Act as if a start tag token with the tag name "head" and no
attributes had been seen, then reprocess the current token.</p>
<p class="note">This will result in an empty <code><a href="section-document.html#head">head</a></code> element being generated, with the
current token being reprocessed in the "<a href="#after1" title="insertion mode: after head">after head</a>" <a href="#insertion0">insertion mode</a>.</p>
</dd></dl>
</dd><dt id="parsing-main-inhead">If the <a href="#insertion0">insertion
mode</a> is "<dfn id="in-head" title="insertion mode: in head">in
head</dfn>"
</dt><dd>
<p>Handle the token as follows.</p>
<p class="note">The rules for handling "title", "style", and "script"
start tags are similar, but not identical.</p>
<p class="note">It is possible for the <a href="#tree-construction0">tree
construction</a> stage's <a href="#the-main0" title="the main
phase">main phase</a> to be in the "<a href="#in-head" title="insertion mode: in head">in head</a>" <a href="#insertion0">insertion mode</a> without the <a href="#current4">current node</a> being a <code><a href="section-document.html#head">head</a></code> element, e.g. if a <code><a href="section-document.html#head">head</a></code> end tag is immediately followed by a
<code><a href="section-document.html#meta0">meta</a></code> start tag.</p>
<dl class="switch">
<dt>A character token that is one of one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p><a href="#append" title="append a character">Append the
character</a> to the <a href="#current4">current node</a>.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <a href="#current4">current node</a> with the <code title="">data</code> attribute set to the data given in the comment
token.</p>
</dd><dt>A start tag with the tag name "title"
</dt><dd>
<p><a href="#create">Create an element for the token</a>.</p>
<p>Append the new element to the node pointed to by the <a href="#head-element"><code title="">head</code> element pointer</a>,
or, if that is null (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code>
case</a>), to the <a href="#current4">current node</a>.</p>
<p>Switch the tokeniser's <a href="section-tokenisation.html#content2">content model flag</a>
to the RCDATA state.</p>
<p>Then, collect all the character tokens that the tokeniser returns
until it returns a token that is not a character token.</p>
<p>If this process resulted in a collection of character tokens,
append a single <code>Text</code> node to the <code><a href="section-document.html#title1">title</a></code> element node whose contents is the
concatenation of all those tokens' characters.</p>
<p>The tokeniser's <a href="section-tokenisation.html#content2">content model flag</a> will
have switched back to the PCDATA state.</p>
<p>If the next token is an end tag token with the tag name "title",
ignore it. Otherwise, this is a <a href="section-parsing.html#parse">parse error</a>.</p>
</dd><dt>A start tag with the tag name "style"
</dt><dd>
<p><a href="#create">Create an element for the token</a>.</p>
<p>Append the new element to the <a href="#current4">current
node</a>, unless the <a href="#insertion0">insertion mode</a> is "<a href="#in-head" title="insertion mode: in head">in head</a>" and the
<a href="#head-element"><code title="">head</code> element
pointer</a> is not null, in which case append it to the node pointed
to by the <a href="#head-element"><code title="">head</code> element
pointer</a>. <!--
<head></head><style><body> should put the style block in the
head, and does so by switching back to in head, but the head
isn't the current node at that point (comments should go
between the head and the body) -->.</p>
<p>Switch the tokeniser's <a href="section-tokenisation.html#content2">content model flag</a>
to the CDATA state.</p>
<p>Then, collect all the character tokens that the tokeniser returns
until it returns a token that is not a character token, or until it
stops tokenising.</p>
<p>If this process resulted in a collection of character tokens,
append a single <code>Text</code> node to the <code><a href="section-document.html#style">style</a></code> element node whose contents is the
concatenation of all those tokens' characters.</p>
<p>The tokeniser's <a href="section-tokenisation.html#content2">content model flag</a> will
have switched back to the PCDATA state.</p>
<p>If the next token is an end tag token with the tag name "style",
ignore it. Otherwise, this is a <a href="section-parsing.html#parse">parse error</a>.</p>
</dd><dt id="scriptTag">A start tag with the tag name "script"
</dt><dd>
<p><a href="#create">Create an element for the token</a>.</p>
<p>Mark the element as being <a href="section-scripting0.html#parser-inserted">"parser-inserted"</a>. This ensures that, if
the script is external, any <code title="dom-document-write-HTML"><a href="section-dynamic.html#document.write0">document.write()</a></code> calls in the
script will execute in-line, instead of blowing the document away,
as would happen in most other cases.</p>
<p>Switch the tokeniser's <a href="section-tokenisation.html#content2">content model flag</a>
to the CDATA state.</p>
<p>Then, collect all the character tokens that the tokeniser returns
until it returns a token that is not a character token, or until it
stops tokenising.</p>
<p>If this process resulted in a collection of character tokens,
append a single <code>Text</code> node to the <code><a href="section-scripting0.html#script0">script</a></code> element node whose contents is the
concatenation of all those tokens' characters.</p>
<p>The tokeniser's <a href="section-tokenisation.html#content2">content model flag</a> will
have switched back to the PCDATA state.</p>
<p>If the next token is not an end tag token with the tag name
"script", then this is a <a href="section-parsing.html#parse">parse error</a>; mark the
<code><a href="section-scripting0.html#script0">script</a></code> element as <a href="section-scripting0.html#already">"already executed"</a>. Otherwise, the token is the
<code><a href="section-scripting0.html#script0">script</a></code> element's end tag, so
ignore it.</p>
<p>If the parser was originally created in order to handle the
setting of a node's <code title="dom-innerHTML-HTML"><a href="section-dynamic.html#innerhtml0">innerHTML</a></code> attribute, then mark the
<code><a href="section-scripting0.html#script0">script</a></code> element as <a href="section-scripting0.html#already">"already executed"</a>, and skip the rest of the
processing described for this token (including the part below where
"<a href="section-scripting0.html#the-script" title="the script that will execute as soon
as the parser resumes">scripts that will execute as soon as the
parser resumes</a>" are executed). (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p class="note">Marking the <code><a href="section-scripting0.html#script0">script</a></code>
element as "already executed" prevents it from executing when it is
inserted into the document a few paragraphs below. Scripts missing
their end tags and scripts that were inserted using <code title="dom-innerHTML-HTML"><a href="section-dynamic.html#innerhtml0">innerHTML</a></code>
aren't executed.</p>
<p>Let the <var title="">old insertion point</var> have the same
value as the current <a href="section-parsing.html#insertion">insertion point</a>. Let
the <a href="section-parsing.html#insertion">insertion point</a> be just before the <a href="section-parsing.html#next-input">next input character</a>.</p>
<p>Append the new element to the <a href="#current4">current
node</a>, unless the <a href="#insertion0">insertion mode</a> is "<a href="#in-head" title="insertion mode: in head">in head</a>" and the
<a href="#head-element"><code title="">head</code> element
pointer</a> is not null, in which case append it to the node pointed
to by the <a href="#head-element"><code title="">head</code> element
pointer</a>. <!--
<head></head><script><body> should put the script in the head,
and does so by switching back to in head, but the head isn't
the current node at that point (comments should go between the
head and the body) -->
<a href="section-scripting0.html#running0" title="running a script">Special processing
occurs when a <code>script</code> element is inserted into a
document</a> that might cause some script to execute, which might
cause <a href="section-dynamic.html#document.write0" title="dom-document-write-HTML">new
characters to be inserted into the tokeniser</a>.</p>
<p>Let the <a href="section-parsing.html#insertion">insertion point</a> have the value of
the <var title="">old insertion point</var>. (In other words,
restore the <a href="section-parsing.html#insertion">insertion point</a> to the value it
had before the previous paragraph. This value might be the
"undefined" value.)</p>
<p id="scriptTagParserResumes">At this stage, if there is <a href="section-scripting0.html#the-script" title="the script that will execute as soon as
the parser resumes">a script that will execute as soon as the parser
resumes</a>, then:</p>
<dl class="switch">
<dt>If the tree construction stage is <a href="section-parsing.html#nestedParsing">being
called reentrantly</a>, say from a call to <code title="dom-document-write-HTML"><a href="section-dynamic.html#document.write0">document.write()</a></code>:
</dt><dd>
<p>Abort the processing of any nested invokations of the tokeniser,
yielding control back to the caller. (Tokenisation will resume
when the caller returns to the "outer" tree construction stage.)
</p></dd><dt>Otherwise:
</dt><dd>
<p>Follow these steps:</p>
<ol>
<li>
<p>Let <var title="">the script</var> be <a href="section-scripting0.html#the-script">the script that will execute as soon as the
parser resumes</a>. There is no longer <a href="section-scripting0.html#the-script" title="the script that will execute as soon as the parser
resumes">a script that will execute as soon as the parser
resumes</a>.
</p></li><li>
<p><a href="section-terminology.html#pause">Pause</a> until the script has
<span>completed loading</span><!-- XXX xref -->.
</p></li><li>
<p>Let the <a href="section-parsing.html#insertion">insertion point</a> be just
before the <a href="section-parsing.html#next-input">next input character</a>.
</p></li><li>
<p><a href="section-scripting0.html#executing0" title="executing a script block">Execute
the script</a>.
</p></li><li>
<p>Let the <a href="section-parsing.html#insertion">insertion point</a> be undefined
again.
</p></li><li>
<p>If there is once again <a href="section-scripting0.html#the-script" title="the script
that will execute as soon as the parser resumes">a script that
will execute as soon as the parser resumes</a>, then repeat
these steps from step 1.
</p></li></ol>
</dd></dl>
</dd><dt>A start tag with the tag name "base", "link", or "meta"
</dt><dd>
<p><a href="#create">Create an element for the token</a>.</p>
<p>Append the new element to the node pointed to by the <a href="#head-element"><code title="">head</code> element pointer</a>,
or, if that is null (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code>
case</a>), to the <a href="#current4">current node</a>.</p>
</dd><dt>An end tag with the tag name "head"
</dt><dd>
<p>If the <a href="#current4">current node</a> is a <code><a href="section-document.html#head">head</a></code> element, pop the <a href="#current4">current node</a> off the <a href="#stack">stack of
open elements</a>. Otherwise, this is a <a href="section-parsing.html#parse">parse
error</a>.</p>
<!-- might happen if you see two </head>s
and something in between the two sends you from "after head"
back to "in head" -->
<p>Change the <a href="#insertion0">insertion mode</a> to "<a href="#after1" title="insertion mode: after head">after head</a>".</p>
</dd><dt>An end tag with the tag name "html"
</dt><dd>
<p>Act as described in the "anything else" entry below.</p>
</dd><dt>A start tag with the tag name "head"
</dt><dt>Any other end tag
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>Anything else
</dt><dd>
<p>If the <a href="#current4">current node</a> is a <code><a href="section-document.html#head">head</a></code> element, act as if an end tag token
with the tag name "head" had been seen.</p>
<p>Otherwise, change the <a href="#insertion0">insertion mode</a> to
"<a href="#after1" title="insertion mode: after head">after
head</a>".</p>
<p>Then, reprocess the current token.</p>
<p class="big-issue">In certain UAs, <a href="https://bugzilla.mozilla.org/attachment.cgi?id=180157&action=view">some
elements</a> don't trigger the "in body" mode straight away, but
instead get put into the head. Do we want to copy that?</p>
</dd></dl>
</dd><dt>If the <a href="#insertion0">insertion mode</a> is "<dfn id="after1" title="insertion mode: after head">after head</dfn>"
</dt><dd>
<p>Handle the token as follows:</p>
<dl class="switch">
<dt>A character token that is one of one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p><a href="#append" title="append a character">Append the
character</a> to the <a href="#current4">current node</a>.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <a href="#current4">current node</a> with the <code title="">data</code> attribute set to the data given in the comment
token.</p>
</dd><dt>A start tag token with the tag name "body"
</dt><dd>
<p><a href="#insert" title="insert an HTML element">Insert a
<code>body</code> element</a> for the token.</p>
<p>Change the <a href="#insertion0">insertion mode</a> to "<a href="#in-body" title="insertion mode: in body">in body</a>".</p>
</dd><dt>A start tag token with the tag name "frameset"
</dt><dd>
<p><a href="#insert" title="insert an HTML element">Insert a
<code>frameset</code> element</a> for the token.</p>
<p>Change the <a href="#insertion0">insertion mode</a> to "<a href="#in-frameset" title="insertion mode: in frameset">in
frameset</a>".</p>
</dd><dt>A start tag token whose tag name is one of: "base", "link",
"meta", "script", "style", "title"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Switch the <a href="#insertion0">insertion mode</a> back to "<a href="#in-head" title="insertion mode: in head">in head</a>" and reprocess the
token.</p>
</dd><dt>Anything else
</dt><dd>
<p>Act as if a start tag token with the tag name "body" and no
attributes had been seen, and then reprocess the current token.</p>
</dd></dl>
</dd><dt id="parsing-main-inbody">If the <a href="#insertion0">insertion
mode</a> is "<dfn id="in-body" title="insertion mode: in body">in
body</dfn>"
</dt><dd>
<p>Handle the token as follows:</p>
<dl class="switch">
<dt>A character token
</dt><dd>
<p><a href="#reconstruct">Reconstruct the active formatting
elements</a>, if any.</p>
<p><a href="#append" title="append a character">Append the token's
character</a> to the <a href="#current4">current node</a>.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <a href="#current4">current node</a> with the <code title="">data</code> attribute set to the data given in the comment
token.</p>
</dd><dt>A start tag token whose tag name is one of: "script", "style"
</dt><dd>
<p>Process the token as if the <a href="#insertion0">insertion
mode</a> had been "<a href="#in-head" title="insertion mode: in
head">in head</a>".</p>
</dd><dt>A start tag token whose tag name is one of: "base", "link",
"meta", "title"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Process the token as if the <a href="#insertion0">insertion mode</a> had been "<a href="#in-head" title="insertion mode: in head">in head</a>".</p>
</dd><dt>A start tag token with the tag name "body"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>.</p>
<p>If the second element on the <a href="#stack">stack of open
elements</a> is not a <code><a href="section-sections.html#body0">body</a></code>
element, or, if the <a href="#stack">stack of open elements</a> has
only one node on it, then ignore the token. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise, for each attribute on the token, check to see if the
attribute is already present on the <code><a href="section-sections.html#body0">body</a></code> element (the second element) on the <a href="#stack">stack of open elements</a>. If it is not, add the
attribute and its corresponding value to that element.</p>
</dd><dt>An end tag with the tag name "body"
</dt><dd>
<p>If the second element in the <a href="#stack">stack of open
elements</a> is not a <code><a href="section-sections.html#body0">body</a></code>
element, this is a <a href="section-parsing.html#parse">parse error</a>. Ignore the
token. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise:</p>
<p class="big-issue">this needs to handle closing of implied elements,
but without closing them</p>
<p>If the <a href="#current4">current node</a> is not the <code><a href="section-sections.html#body0">body</a></code> element, then this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>Change the <a href="#insertion0">insertion mode</a> to "<a href="#after2" title="insertion mode: after body">after body</a>".</p>
</dd><dt>An end tag with the tag name "html"
</dt><dd>
<p>Act as if an end tag with tag name "body" had been seen, then, if
that token wasn't ignored, reprocess the current token.</p>
<p class="note">The fake end tag token here can only be ignored in the
<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>.</p>
</dd><dt>A start tag whose tag name is one of: "address", "blockquote",
"center", "dir", "div", "dl", "fieldset", "listing", "menu", "ol",
"p", "ul"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then act as if an end tag with the tag name
<code><a href="section-prose.html#p">p</a></code> had been seen.</p>
<p><a href="#insert" title="insert an html element">Insert an HTML
element</a> for the token.</p>
</dd><dt>A start tag whose tag name is "pre"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then act as if an end tag with the tag name
<code><a href="section-prose.html#p">p</a></code> had been seen.</p>
<p><a href="#insert" title="insert an html element">Insert an HTML
element</a> for the token.</p>
<p>If the next token is a U+000A LINE FEED (LF) character token, then
ignore that token and move on to the next one. (Newlines at the
start of <code><a href="section-preformatted.html#pre">pre</a></code> blocks are ignored as
an authoring convenience.)</p>
</dd><dt>A start tag whose tag name is "form"
</dt><dd>
<p>If the <a href="#form-element"><code title="form">form</code>
element pointer</a> is not null, ignore the token with a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>Otherwise:</p>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then act as if an end tag with the tag name
<code><a href="section-prose.html#p">p</a></code> had been seen.</p>
<p><a href="#insert" title="insert an html Element">Insert an HTML
element</a> for the token, and set the <code title="form">form</code>
element pointer to point to the element created.</p>
</dd><dt>A start tag whose tag name is "li"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then act as if an end tag with the tag name
<code><a href="section-prose.html#p">p</a></code> had been seen.</p>
<p>Run the following algorithm:</p>
<ol>
<li>
<p>Initialise <var title="">node</var> to be the <a href="#current4">current node</a> (the bottommost node of the
stack).
</p></li><li>
<p>If <var title="">node</var> is an <code><a href="section-lists0.html#li">li</a></code> element, then pop all the nodes from the
<a href="#current4">current node</a> up to <var title="">node</var>, including <var title="">node</var>, then stop
this algorithm. If more than one node is popped, then this is a <a href="section-parsing.html#parse">parse error</a>.
</p></li><li>
<p>If <var title="">node</var> is not in the <a href="#formatting">formatting</a> category, and is not in the <a href="#phrasing">phrasing</a> category, and is not an <code><a href="section-sections.html#address">address</a></code> or <code><a href="section-miscellaneous.html#div">div</a></code> element, then stop this algorithm.
</p></li>
<!-- an element <foo> is in this
list if the following markup:
<!DOCTYPE html><body><ol><li><foo><li>
...results in the second <li> not being (in any way) a
descendant of the first <li>, or if <foo> is a formatting
element that gets reopened later. -->
<li>
<p>Otherwise, set <var title="">node</var> to the previous entry in
the <a href="#stack">stack of open elements</a> and return to step
2.
</p></li></ol>
<p>Finally, <a href="#insert" title="insert an html element">insert
an <code>li</code> element</a>.</p>
</dd><dt>A start tag whose tag name is "dd" or "dt"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then act as if an end tag with the tag name
<code><a href="section-prose.html#p">p</a></code> had been seen.</p>
<p>Run the following algorithm:</p>
<ol>
<li>
<p>Initialise <var title="">node</var> to be the <a href="#current4">current node</a> (the bottommost node of the
stack).
</p></li><li>
<p>If <var title="">node</var> is a <code><a href="section-lists0.html#dd">dd</a></code> or <code><a href="section-lists0.html#dt">dt</a></code>
element, then pop all the nodes from the <a href="#current4">current node</a> up to <var title="">node</var>,
including <var title="">node</var>, then stop this algorithm. If
more than one node is popped, then this is a <a href="section-parsing.html#parse">parse error</a>.
</p></li><li>
<p>If <var title="">node</var> is not in the <a href="#formatting">formatting</a> category, and is not in the <a href="#phrasing">phrasing</a> category, and is not an <code><a href="section-sections.html#address">address</a></code> or <code><a href="section-miscellaneous.html#div">div</a></code> element, then stop this algorithm.
</p></li>
<!-- an element <foo> is in this
list if the following markup:
<!DOCTYPE html><body><ol><dt><foo><dt>
...results in the second <li> not being (in any way) a
descendant of the first <li>, or if <foo> is a formatting
element that gets reopened later. -->
<li>
<p>Otherwise, set <var title="">node</var> to the previous entry in
the <a href="#stack">stack of open elements</a> and return to step
2.
</p></li></ol>
<p>Finally, <a href="#insert" title="insert an html element">insert
an HTML element</a> with the same tag name as the token's.</p>
</dd><dt>A start tag token whose tag name is "plaintext"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then act as if an end tag with the tag name
<code><a href="section-prose.html#p">p</a></code> had been seen.</p>
<p><a href="#insert" title="insert an html element">Insert an HTML
element</a> for the token.</p>
<p>Switch the <a href="section-tokenisation.html#content2">content model flag</a> to the
PLAINTEXT state.</p>
<p class="note">Once a start tag with the tag name "plaintext" has been
seen, that will be the last token ever seen other than character
tokens (and the end-of-file token), because there is no way to
switch the <a href="section-tokenisation.html#content2">content model flag</a> out of the
PLAINTEXT state.</p>
</dd><dt>An end tag whose tag name is one of: "address", "blockquote",
"center", "dir", "div", "dl", "fieldset", "listing", "menu", "ol",
"pre", "ul"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an">has an element in scope</a> with the same tag name
as that of the token, then <a href="#generate">generate implied end
tags</a>.</p>
<p>Now, if the <a href="#current4">current node</a> is not an element
with the same tag name as that of the token, then this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an">has an element in scope</a> with the same tag name
as that of the token, then pop elements from this stack until an
element with that tag name has been popped from the stack.</p>
<!-- XXX quirk (except for in certain cases?):
<p>Otherwise, act as if a start tag with the tag name given in
the token had been seen, then reprocess the current token.</p>
-->
</dd><dt>An end tag whose tag name is "form"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an">has an element in scope</a> with the same tag name
as that of the token, then <a href="#generate">generate implied end
tags</a>.</p>
<p>Now, if the <a href="#current4">current node</a> is not an element
with the same tag name as that of the token, then this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>Otherwise, if the <a href="#current4">current node</a> is an
element with the same tag name as that of the token pop that element
from the stack.</p>
<p>In any case, set the <a href="#form-element"><code title="">form</code> element pointer</a> to null.</p>
</dd><dt>An end tag whose tag name is "p"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then <a href="#generate">generate implied end
tags</a>, except for <code><a href="section-prose.html#p">p</a></code> elements.</p>
<p>If the <a href="#current4">current node</a> is not a <code><a href="section-prose.html#p">p</a></code> element, then this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then pop elements from this stack until the
stack no longer <a href="#have-an" title="has an element in
scope">has a <code>p</code> element in scope</a>.</p>
<!-- XXX quirk:
<p>Otherwise, act as if a start tag with the tag name
<code>p</code> had been seen, then reprocess the current
token.</p>
-->
</dd><dt>An end tag whose tag name is "dd", "dt", or "li"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an">has an element in scope</a> whose tag name matches
the tag name of the token, then <a href="#generate">generate implied
end tags</a>, except for elements with the same tag name as the
token.</p>
<p>If the <a href="#current4">current node</a> is not an element with
the same tag name as the token, then this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an">has an element in scope</a> whose tag name matches
the tag name of the token, then pop elements from this stack until
an element with that tag name has been popped from the stack.</p>
</dd><dt>A start tag whose tag name is one of: "h1", "h2", "h3", "h4",
"h5", "h6"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then act as if an end tag with the tag name
<code><a href="section-prose.html#p">p</a></code> had been seen.</p>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has in scope</a> an
element whose tag name is one of "h1", "h2", "h3", "h4", "h5", or
"h6", then this is a <a href="section-parsing.html#parse">parse error</a>; pop elements
from the stack until an element with one of those tag names has been
popped from the stack.</p>
<p><a href="#insert" title="insert an html element">Insert an HTML
element</a> for the token.</p>
</dd><dt>An end tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5",
"h6"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has in scope</a> an
element whose tag name is one of "h1", "h2", "h3", "h4", "h5", or
"h6", then <a href="#generate">generate implied end tags</a>.</p>
<p>Now, if the <a href="#current4">current node</a> is not an element
with the same tag name as that of the token, then this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has in scope</a> an
element whose tag name is one of "h1", "h2", "h3", "h4", "h5", or
"h6", then pop elements from the stack until an element with one of
those tag names has been popped from the stack.</p>
<!-- XXX quirk:
<p>Otherwise, act as if a start tag with the tag name given in
the token had been seen, then reprocess the current token.</p>
-->
</dd>
<!-- ADOPTION AGENCY ELEMENTS
Mozilla-only: bdo blink del ins sub sup q
Safari-only: code dfn kbd nobr samp var wbr
Both: a b big em font i s small strike strong tt u -->
<dt>A start tag whose tag name is "a"
</dt><dd>
<p>If the <a href="#list-of4">list of active formatting elements</a>
contains an element whose tag name is "a" between the end of the
list and the last marker on the list (or the start of the list if
there is no marker on the list), then this is a <a href="section-parsing.html#parse">parse error</a>; act as if an end tag with the tag
name "a" had been seen, then remove that element from the <a href="#list-of4">list of active formatting elements</a> and the <a href="#stack">stack of open elements</a> if the end tag didn't
already remove it (it might not have if the element is not <a href="#have-an0" title="has an element in table scope">in table
scope</a>).</p>
<p class="example">In the non-conforming stream
<code><a href="a">a<table><a href="b">b</table>x</code>,
the first <code><a href="section-phrase.html#a">a</a></code> element would be closed
upon seeing the second one, and the "x" character would be inside a
link to "b", not to "a". This is despite the fact that the outer
<code><a href="section-phrase.html#a">a</a></code> element is not in table scope
(meaning that a regular <code></a></code> end tag at the start of
the table wouldn't close the outer <code><a href="section-phrase.html#a">a</a></code>
element).</p>
<p><a href="#reconstruct">Reconstruct the active formatting
elements</a>, if any.</p>
<p><a href="#insert" title="insert an html element">Insert an HTML
element</a> for the token. Add that element to the <a href="#list-of4">list of active formatting elements</a>.</p>
</dd><dt>A start tag whose tag name is one of: "b", "big", "em", "font",
"i", "nobr", "s", "small", "strike", "strong", "tt", "u"
</dt><dd>
<p><a href="#reconstruct">Reconstruct the active formatting
elements</a>, if any.</p>
<p><a href="#insert" title="insert an html element">Insert an HTML
element</a> for the token. Add that element to the <a href="#list-of4">list of active formatting elements</a>.</p>
</dd><dt id="adoptionAgency">An end tag whose tag name is one of: "a", "b",
"big", "em", "font", "i", "nobr", "s", "small", "strike", "strong",
"tt", "u"
</dt><dd>
<p>Follow these steps:</p>
<ol>
<li>
<p>Let the <var title="">formatting element</var> be the last
element in the <a href="#list-of4">list of active formatting
elements</a> that:</p>
<ul>
<li>is between the end of the list and the last scope marker in
the list, if any, or the start of the list otherwise, and
</li><li>has the same tag name as the token.
</li></ul>
<p>If there is no such node, or, if that node is also in the <a href="#stack">stack of open elements</a> but the element is not <a href="#have-an" title="has an element in scope">in scope</a>, then
this is a <a href="section-parsing.html#parse">parse error</a>. Abort these steps. The
token is ignored.</p>
<p>Otherwise, if there is such a node, but that node is not in the
<a href="#stack">stack of open elements</a>, then this is a <a href="section-parsing.html#parse">parse error</a>; remove the element from the list,
and abort these steps.</p>
<p>Otherwise, there is a <var title="">formatting element</var> and
that element is in <a href="#stack" title="stack of open
elements">the stack</a> and is <a href="#have-an" title="has an
element in scope">in scope</a>. If the element is not the <a href="#current4">current node</a>, this is a <a href="section-parsing.html#parse">parse error</a>. In any case, proceed with the
algorithm as written in the following steps.</p>
</li><li>
<p>Let the <var title="">furthest block</var> be the topmost node
in the <a href="#stack">stack of open elements</a> that is lower
in the stack than the <var title="">formatting element</var>, and
is not an element in the <a href="#phrasing">phrasing</a> or <a href="#formatting">formatting</a> categories. There might not be
one.
</p></li><li>
<p>If there is no <var title="">furthest block</var>, then the UA
must skip the subsequent steps and instead just pop all the nodes
from the bottom of the <a href="#stack">stack of open
elements</a>, from the <a href="#current4">current node</a> up to
the <var title="">formatting element</var>, and remove the <var title="">formatting element</var> from the <a href="#list-of4">list of active formatting elements</a>.
</p></li><li>
<p>Let the <var title="">common ancestor</var> be the element
immediately above the <var title="">formatting element</var> in
the <a href="#stack">stack of open elements</a>.
</p></li><li>
<p>If the <var title="">furthest block</var> has a parent node,
then remove the <var title="">furthest block</var> from its parent
node.
</p></li><li>
<p>Let a bookmark note the position of the <var title="">formatting
element</var> in the <a href="#list-of4">list of active formatting
elements</a> relative to the elements on either side of it in the
list.
</p></li><li>
<p>Let <var title="">node</var> and <var title="">last node</var>
be the <var title="">furthest block</var>. Follow these steps:</p>
<ol>
<li>Let <var title="">node</var> be the element immediately prior
to <var title="">node</var> in the <a href="#stack">stack of open
elements</a>.
</li><li>If <var title="">node</var> is not in the <a href="#list-of4">list of active formatting elements</a>, then
remove <var title="">node</var> from the <a href="#stack">stack
of open elements</a> and then go back to step 1.
</li><li>Otherwise, if <var title="">node</var> is the <var title="">formatting element</var>, then go to the next step in
the overall algorithm.
</li><li>Otherwise, if <var title="">last node</var> is the <var title="">furthest block</var>, then move the aforementioned
bookmark to be immediately after the <var title="">node</var> in
the <a href="#list-of4">list of active formatting elements</a>.
</li><li>If <var title="">node</var> has any children, perform a
shallow clone of <var title="">node</var>, replace the entry for
<var title="">node</var> in the <a href="#list-of4">list of
active formatting elements</a> with an entry for the clone,
replace the entry for <var title="">node</var> in the <a href="#stack">stack of open elements</a> with an entry for the
clone, and let <var title="">node</var> be the clone.
</li><li>Insert <var title="">last node</var> into <var title="">node</var>, first removing it from its previous parent
node if any.
</li><li>Let <var title="">last node</var> be <var title="">node</var>.
</li><li>Return to step 1 of this inner set of steps.
</li></ol>
</li><li>
<p>Insert whatever <var title="">last node</var> ended up being in
the previous step into the <var title="">common ancestor</var>
node, first removing it from its previous parent node if any.
</p></li><li>
<p>Perform a shallow clone of the <var title="">formatting
element</var>.
</p></li><li>
<p>Take all of the child nodes of the <var title="">furthest
block</var> and append them to the clone created in the last step.
</p></li><li>
<p>Append that clone to the <var title="">furthest block</var>.
</p></li><li>
<p>Remove the <var title="">formatting element</var> from the <a href="#list-of4">list of active formatting elements</a>, and
insert the clone into the <a href="#list-of4">list of active
formatting elements</a> at the position of the aforementioned
bookmark.
</p></li><li>
<p>Remove the <var title="">formatting element</var> from the <a href="#stack">stack of open elements</a>, and insert the clone
into the <a href="#stack">stack of open elements</a> immediately
after (i.e. in a more deeply nested position than) the position of
the <var title="">furthest block</var> in that stack.
</p></li><li>
<p>Jump back to step 1 in this series of steps.
</p></li></ol>
<p class="note">The way these steps are defined, only elements in the
<a href="#formatting">formatting</a> category ever get cloned by
this algorithm.</p>
<!--XXX
<div class="example">
<p class="big-issue">Need an example.</p>
</div>
-->
<p class="note">Because of the way this algorithm causes elements to
change parents, it has been dubbed the "adoption agency algorithm"
(in contrast with other possibly algorithms for dealing with
misnested content, which included the "incest algorithm", the
"secret affair algorithm", and the "Heisenberg algorithm").</p>
</dd><dt>A start tag token whose tag name is "button"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a
<code>button</code> element in scope</a>, then this is a <a href="section-parsing.html#parse">parse error</a>; act as if an end tag with the tag
name "button" had been seen, then reprocess the token.</p>
<p>Otherwise:</p>
<p><a href="#reconstruct">Reconstruct the active formatting
elements</a>, if any.</p>
<p><a href="#insert">Insert an HTML element</a> for the token.</p>
<p>Insert a marker at the end of the <a href="#list-of4">list of
active formatting elements</a>.</p>
</dd><dt>A start tag token whose tag name is one of: "marquee", "object"
</dt><dd>
<p><a href="#reconstruct">Reconstruct the active formatting
elements</a>, if any.</p>
<p><a href="#insert">Insert an HTML element</a> for the token.</p>
<p>Insert a marker at the end of the <a href="#list-of4">list of
active formatting elements</a>.</p>
</dd><dt>An end tag token whose tag name is one of: "button", "marquee",
"object"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has in scope</a> an
element whose tag name is the same as the tag name of the token,
then <a href="#generate">generate implied end tags</a>.</p>
<p>Now, if the <a href="#current4">current node</a> is not an element
with the same tag name as the token, then this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>Now, if the <a href="#stack">stack of open elements</a> <a href="#have-an">has an element in scope</a> whose tag name matches
the tag name of the token, then pop elements from the stack until
that element has been popped from the stack, and <a href="#clear0">clear the list of active formatting elements up to
the last marker</a>.</p>
</dd><dt>A start tag token whose tag name is "xmp"
</dt><dd>
<p><a href="#reconstruct">Reconstruct the active formatting
elements</a>, if any.</p>
<p><a href="#insert">Insert an HTML element</a> for the token.</p>
<p>Switch the <a href="section-tokenisation.html#content2">content model flag</a> to the CDATA
state.</p>
</dd><dt>A start tag whose tag name is "table"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then act as if an end tag with the tag name
<code><a href="section-prose.html#p">p</a></code> had been seen.</p>
<!-- XXX quirks: don't do this -->
<p><a href="#insert">Insert an HTML element</a> for the token.</p>
<p>Change the <a href="#insertion0">insertion mode</a> to "<a href="#in-table" title="insertion mode: in table">in table</a>".</p>
</dd><dt>A start tag whose tag name is one of: "area", "basefont",
"bgsound", "br", "embed", "img", "param", "spacer", "wbr"
</dt><dd>
<p><a href="#reconstruct">Reconstruct the active formatting
elements</a>, if any.</p>
<p><a href="#insert" title="insert an html element">Insert an HTML
element</a> for the token. Immediately pop the <a href="#current4">current node</a> off the <a href="#stack">stack of
open elements</a>.</p>
</dd><dt>A start tag whose tag name is "hr"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an" title="has an element in scope">has a <code>p</code>
element in scope</a>, then act as if an end tag with the tag name
<code><a href="section-prose.html#p">p</a></code> had been seen.</p>
<!-- XXX quirks: don't do this -->
<p><a href="#insert" title="insert an html element">Insert an HTML
element</a> for the token. Immediately pop the <a href="#current4">current node</a> off the <a href="#stack">stack of
open elements</a>.</p>
</dd><dt>A start tag whose tag name is "image"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Change the token's tag name to
"img" and reprocess it. (Don't ask.)</p>
<!-- As of
2005-12, studies showed that around 0.2% of pages used the
<image> element. -->
</dd><dt>A start tag whose tag name is "input"
</dt><dd>
<p><a href="#reconstruct">Reconstruct the active formatting
elements</a>, if any.</p>
<p><a href="#insert" title="insert an html element">Insert an
<code>input</code> element</a> for the token.</p>
<p>If the <a href="#form-element"><code title="">form</code> element
pointer</a> is not null, then <span>associate</span><!--XXX
xref! -->
the <code>input</code> element with the <code>form</code> element
pointed to by the <a href="#form-element"><code title="">form</code>
element pointer</a>.</p>
<p>Pop that <code>input</code> element off the <a href="#stack">stack
of open elements</a>.</p>
</dd><dt id="isindex">A start tag whose tag name is "isindex"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>.</p>
<p>If the <a href="#form-element"><code title="">form</code> element
pointer</a> is not null, then ignore the token.</p>
<p>Otherwise:</p>
<p>Act as if a start tag token with the tag name "form" had been
seen.</p>
<p>Act as if a start tag token with the tag name "hr" had been seen.</p>
<p>Act as if a start tag token with the tag name "p" had been seen.</p>
<p>Act as if a start tag token with the tag name "label" had been
seen.</p>
<p>Act as if a stream of character tokens had been seen (see below
for what they should say).</p>
<p>Act as if a start tag token with the tag name "input" had been
seen, with all the attributes from the "isindex" token, except with
the "name" attribute set to the value "isindex" (ignoring any
explicit "name" attribute).</p>
<p>Act as if a stream of character tokens had been seen (see below
for what they should say).</p>
<p>Act as if an end tag token with the tag name "label" had been
seen.</p>
<p>Act as if an end tag token with the tag name "p" had been seen.</p>
<p>Act as if a start tag token with the tag name "hr" had been seen.</p>
<p>Act as if an end tag token with the tag name "form" had been seen.</p>
<p>The two streams of character tokens together should, together with
the <code>input</code> element, express the equivalent of "This is a
searchable index. Insert your search keywords here: (input field)"
in the user's preferred language.</p>
<p class="big-issue"> Then need to specify that if the form submission
causes just a single form control, whose name is "isindex", to be
submitted, then we submit just the value part, not the "isindex="
part.</p>
</dd>
<!-- XXX keygen support; don't forget form element pointer!
<dt>A start tag whose tag name is "keygen"</dt>
<dd>
...
</dd>
-->
<dt>A start tag whose tag name is "textarea"
</dt><dd>
<p><a href="#create">Create an element for the token</a>.</p>
<p>If the <a href="#form-element"><code title="">form</code> element
pointer</a> is not null, then <span>associate</span><!--XXX
xref! -->
the <code>textarea</code> element with the <code>form</code> element
pointed to by the <a href="#form-element"><code title="">form</code>
element pointer</a>.</p>
<p>Append the new element to the <a href="#current4">current
node</a>.</p>
<p>Switch the tokeniser's <a href="section-tokenisation.html#content2">content model flag</a>
to the RCDATA state.</p>
<p>If the next token is a U+000A LINE FEED (LF) character token, then
ignore that token and move on to the next one. (Newlines at the
start of <code>textarea</code> elements are ignored as an authoring
convenience.)</p>
<p>Then, collect all the character tokens that the tokeniser returns
until it returns a token that is not a character token, or until it
stops tokenising.</p>
<p>If this process resulted in a collection of character tokens,
append a single <code>Text</code> node, whose contents is the
concatenation of all those tokens' characters, to the new element
node.</p>
<p>The tokeniser's <a href="section-tokenisation.html#content2">content model flag</a> will
have switched back to the PCDATA state.</p>
<p>If the next token is an end tag token with the tag name
"textarea", ignore it. Otherwise, this is a <a href="section-parsing.html#parse">parse
error</a>.</p>
</dd><dt>A start tag whose tag name is one of: "iframe", "noembed",
"noframes"
</dt><dt>A start tag whose tag name is "noscript", if <a href="section-scripting.html#scripting2">scripting is enabled</a>:
</dt><dd>
<p><a href="#create">Create an element for the token</a>.</p>
<p>For "iframe" tags, the node must be an <code><a href="section-embedded.html#htmliframeelement">HTMLIFrameElement</a></code> object, for
the other tags it must be an <code><a href="section-elements.html#htmlelement">HTMLElement</a></code> object.</p>
<p>Append the new element to the <a href="#current4">current
node</a>.</p>
<p>Switch the tokeniser's <a href="section-tokenisation.html#content2">content model flag</a>
to the CDATA state.</p>
<p>Then, collect all the character tokens that the tokeniser returns
until it returns a token that is not a character token, or until it
stops tokenising.</p>
<p>If this process resulted in a collection of character tokens,
append a single <code>Text</code> node, whose contents is the
concatenation of all those tokens' characters, to the new element
node.</p>
<p>The tokeniser's <a href="section-tokenisation.html#content2">content model flag</a> will
have switched back to the PCDATA state.</p>
<p>If the next token is an end tag token with the same tag name as
the start tag token, ignore it. Otherwise, this is a <a href="section-parsing.html#parse">parse error</a>.</p>
</dd><dt>A start tag whose tag name is "select"
</dt><dd>
<p><a href="#reconstruct">Reconstruct the active formatting
elements</a>, if any.</p>
<p><a href="#insert">Insert an HTML element</a> for the token.</p>
<p>Change the <a href="#insertion0">insertion mode</a> to "<a href="#in-select" title="insertion mode: in select">in select</a>".</p>
</dd>
<!-- XXX quirks:
<dt>An end tag whose tag name is "br"</dt>
<dd>
<p>Act as if a start tag token with the tag name "br" had been
seen. Ignore the end tag token.</p>
</dd>
-->
<dt>A start or end tag whose tag name is one of: "caption", "col",
"colgroup", "frame", "frameset", "head", "option", "optgroup",
"tbody", "td", "tfoot", "th", "thead", "tr"
</dt><dt>An end tag whose tag name is one of: "area", "basefont",
"bgsound", <!--XXX quirks: remove br-->"br", "embed", "hr", "iframe",
"image", "img", "input", "isindex", "noembed", "noframes", "param",
"select", "spacer", "table", "textarea", "wbr"</dt>
<!-- add keygen if we add the start tag -->
<dt>An end tag whose tag name is "noscript", if <a href="section-scripting.html#scripting2">scripting is enabled</a>:
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>A start or end tag whose tag name is one of: "event-source",
"section", "nav", "article", "aside", "header", "footer", "datagrid",
"command"
</dt><dd> <!-- XXXX -->
<p class="big-issue">Work in progress!</p>
</dd><dt>A start tag token not covered by the previous entries
</dt><dd>
<p><a href="#reconstruct">Reconstruct the active formatting
elements</a>, if any.</p>
<p><a href="#insert">Insert an HTML element</a> for the token.</p>
<p class="note">This element will be a <a href="#phrasing">phrasing</a>
element.</p>
<!--
Put the following into the MathML namespace if parsed:
math, mrow, mfrac, msqrt, mroot, mstyle, merror, mpadded,
mphantom, mfenced, menclose, msub, msup, msubsup, munder,
mover, munderover, mmultiscripts, mtable, mlabeledtr, mtr,
mtd, maction
-->
</dd><dt>An end tag token not covered by the previous entries
</dt><dd>
<p>Run the following algorithm:</p>
<ol>
<li>
<p>Initialise <var title="">node</var> to be the <a href="#current4">current node</a> (the bottommost node of the
stack).
</p></li><li>
<p>If <var title="">node</var> has the same tag name as the end tag
token, then:</p>
<ol>
<li>
<p><a href="#generate">Generate implied end tags</a>.
</p></li><li>
<p>If the tag name of the end tag token does not match the tag
name of the <a href="#current4">current node</a>, this is a <a href="section-parsing.html#parse">parse error</a>.
</p></li><li>
<p>Pop all the nodes from the <a href="#current4">current
node</a> up to <var title="">node</var>, including <var title="">node</var>, then stop this algorithm.
</p></li></ol>
</li><li>
<p>Otherwise, if <var title="">node</var> is in neither the <a href="#formatting">formatting</a> category nor the <a href="#phrasing">phrasing</a> category, then this is a <a href="section-parsing.html#parse">parse error</a>. Stop this algorithm. The end tag
token is ignored.
</p></li><li>
<p>Set <var title="">node</var> to the previous entry in the <a href="#stack">stack of open elements</a>.
</p></li><li>
<p>Return to step 2.
</p></li></ol>
</dd></dl>
</dd><dt id="parsing-main-intable">If the <a href="#insertion0">insertion
mode</a> is "<dfn id="in-table" title="insertion mode: in table">in
table</dfn>"
</dt><dd>
<dl class="switch">
<dt>A character token that is one of one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p><a href="#append" title="append a character">Append the
character</a> to the <a href="#current4">current node</a>.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <a href="#current4">current node</a> with the <code title="">data</code> attribute set to the data given in the comment
token.</p>
</dd><dt>A start tag whose tag name is "caption"
</dt><dd>
<p><a href="#clear1">Clear the stack back to a table context</a>.
(See below.)</p>
<p>Insert a marker at the end of the <a href="#list-of4">list of
active formatting elements</a>.</p>
<p><a href="#insert">Insert an HTML element</a> for the token, then
switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-caption" title="insertion mode: in caption">in
caption</a>".</p>
</dd><dt>A start tag whose tag name is "colgroup"
</dt><dd>
<p><a href="#clear1">Clear the stack back to a table context</a>.
(See below.)</p>
<p><a href="#insert">Insert an HTML element</a> for the token, then
switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-column" title="insertion mode: in column group">in column
group</a>".</p>
</dd><dt>A start tag whose tag name is "col"
</dt><dd>
<p>Act as if a start tag token with the tag name "colgroup" had been
seen, then reprocess the current token.</p>
</dd><dt>A start tag whose tag name is one of: "tbody", "tfoot", "thead"
</dt><dd>
<p><a href="#clear1">Clear the stack back to a table context</a>.
(See below.)</p>
<p><a href="#insert">Insert an HTML element</a> for the token, then
switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-table0" title="insertion mode: in table body">in table
body</a>".</p>
</dd><dt>A start tag whose tag name is one of: "td", "th", "tr"
</dt><dd>
<p>Act as if a start tag token with the tag name "tbody" had been
seen, then reprocess the current token.</p>
</dd><dt>A start tag whose tag name is "table"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Act as if an end tag token with
the tag name "table" had been seen, then, if that token wasn't
ignored, reprocess the current token.</p>
<p class="note">The fake end tag token here can only be ignored in the
<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>.</p>
</dd><dt>An end tag whose tag name is "table"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> does not <a href="#have-an0" title="has an element in table scope">have an
element in table scope</a> with the same tag name as the token, this
is a <a href="section-parsing.html#parse">parse error</a>. Ignore the token. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise:</p>
<p><a href="#generate">Generate implied end tags</a>.</p>
<p>Now, if the <a href="#current4">current node</a> is not a <code><a href="section-tabular.html#table">table</a></code> element, then this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>Pop elements from this stack until a <code><a href="section-tabular.html#table">table</a></code> element has been popped from the
stack.</p>
<p><a href="#reset">Reset the insertion mode appropriately</a>.</p>
</dd><dt>An end tag whose tag name is one of: "body", "caption", "col",
"colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>Anything else
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Process the token as if the <a href="#insertion0">insertion mode</a> was "<a href="#in-body" title="insertion mode: in body">in body</a>", with the following
exception:</p>
<p>If the <a href="#current4">current node</a> is a <code><a href="section-tabular.html#table">table</a></code>, <code><a href="section-tabular.html#tbody">tbody</a></code>, <code><a href="section-tabular.html#tfoot0">tfoot</a></code>, <code><a href="section-tabular.html#thead0">thead</a></code>, or <code><a href="section-tabular.html#tr">tr</a></code> element, then, whenever a node would be
inserted into the <a href="#current4">current node</a>, it must
instead be inserted into the <em><a href="#foster">foster parent
element</a></em>.</p>
<p>The <dfn id="foster">foster parent element</dfn> is the parent
element of the last <code><a href="section-tabular.html#table">table</a></code> element
in the <a href="#stack">stack of open elements</a>, if there is a
<code><a href="section-tabular.html#table">table</a></code> element and it has such a
parent element. If there is no <code><a href="section-tabular.html#table">table</a></code> element in the <a href="#stack">stack
of open elements</a> (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code>
case</a>), then the <em><a href="#foster">foster parent
element</a></em> is the first element in the <a href="#stack">stack
of open elements</a> (the <code><a href="section-the-root.html#html">html</a></code>
element). Otherwise, if there is a <code><a href="section-tabular.html#table">table</a></code> element in the <a href="#stack">stack
of open elements</a>, but the last <code><a href="section-tabular.html#table">table</a></code> element in the <a href="#stack">stack
of open elements</a> has no parent, or its parent node is not an
element, then the <em><a href="#foster">foster parent
element</a></em> is the element before the last <code><a href="section-tabular.html#table">table</a></code> element in the <a href="#stack">stack
of open elements</a>.</p>
<p>If the <em><a href="#foster">foster parent element</a></em> is the
parent element of the last <code><a href="section-tabular.html#table">table</a></code>
element in the <a href="#stack">stack of open elements</a>, then the
new node must be inserted immediately <em>before</em> the last
<code><a href="section-tabular.html#table">table</a></code> element in the <a href="#stack">stack of open elements</a> in the <a href="#foster">foster parent element</a>; otherwise, the new node
must be <em>appended</em> to the <a href="#foster">foster parent
element</a>.</p>
</dd></dl>
<p>When the steps above require the UA to <dfn id="clear1">clear the
stack back to a table context</dfn>, it means that the UA must, while
the <a href="#current4">current node</a> is not a <code><a href="section-tabular.html#table">table</a></code> element or an <code><a href="section-the-root.html#html">html</a></code> element, pop elements from the <a href="#stack">stack of open elements</a>. If this causes any elements
to be popped from the stack, then this is a <a href="section-parsing.html#parse">parse
error</a>.</p>
<p class="note">The <a href="#current4">current node</a> being an
<code><a href="section-the-root.html#html">html</a></code> element after this process is an
<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>.</p>
</dd><dt id="parsing-main-incaption">If the <a href="#insertion0">insertion
mode</a> is "<dfn id="in-caption" title="insertion mode: in caption">in
caption</dfn>"
</dt><dd>
<dl class="switch">
<dt>An end tag whose tag name is "caption"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> does not <a href="#have-an0" title="has an element in table scope">have an
element in table scope</a> with the same tag name as the token, this
is a <a href="section-parsing.html#parse">parse error</a>. Ignore the token. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise:</p>
<p><a href="#generate">Generate implied end tags</a>.</p>
<p>Now, if the <a href="#current4">current node</a> is not a <code><a href="section-tabular.html#caption0">caption</a></code> element, then this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>Pop elements from this stack until a <code><a href="section-tabular.html#caption0">caption</a></code> element has been popped from the
stack.</p>
<p><a href="#clear0">Clear the list of active formatting elements up
to the last marker</a>.</p>
<p>Switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-table" title="insertion mode: in table">in table</a>".</p>
</dd><dt>A start tag whose tag name is one of: "caption", "col",
"colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"
</dt><dt>An end tag whose tag name is "table"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Act as if an end tag with the
tag name "caption" had been seen, then, if that token wasn't
ignored, reprocess the current token.</p>
<p class="note">The fake end tag token here can only be ignored in the
<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>.</p>
</dd><dt>An end tag whose tag name is one of: "body", "col", "colgroup",
"html", "tbody", "td", "tfoot", "th", "thead", "tr"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>Anything else
</dt><dd>
<p>Process the token as if the <a href="#insertion0">insertion
mode</a> was "<a href="#in-body" title="insertion mode: in body">in
body</a>".</p>
</dd></dl>
</dd><dt id="parsing-main-incolgroup">If the <a href="#insertion0">insertion
mode</a> is "<dfn id="in-column" title="insertion mode: in column
group">in column group</dfn>"
</dt><dd>
<dl class="switch">
<dt>A character token that is one of one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p><a href="#append" title="append a character">Append the
character</a> to the <a href="#current4">current node</a>.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <a href="#current4">current node</a> with the <code title="">data</code> attribute set to the data given in the comment
token.</p>
</dd><dt>A start tag whose tag name is "col"
</dt><dd>
<p><a href="#insert" title="insert an HTML element">Insert a
<code>col</code> element</a> for the token. Immediately pop the <a href="#current4">current node</a> off the <a href="#stack">stack of
open elements</a>.</p>
</dd><dt>An end tag whose tag name is "colgroup"
</dt><dd>
<p>If the <a href="#current4">current node</a> is the root <code><a href="section-the-root.html#html">html</a></code> element, then this is a <a href="section-parsing.html#parse">parse error</a>, ignore the token. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise, pop the <a href="#current4">current node</a> (which
will be a <code><a href="section-tabular.html#colgroup">colgroup</a></code> element)
from the <a href="#stack">stack of open elements</a>. Switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-table" title="insertion mode: in table">in table</a>".</p>
</dd><dt>An end tag whose tag name is "col"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>Anything else
</dt><dd>
<p>Act as if an end tag with the tag name "colgroup" had been seen,
and then, if that token wasn't ignored, reprocess the current token.</p>
<p class="note">The fake end tag token here can only be ignored in the
<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>.</p>
</dd></dl>
</dd><dt id="parsing-main-intbody">If the <a href="#insertion0">insertion
mode</a> is "<dfn id="in-table0" title="insertion mode: in table body">in
table body</dfn>"
</dt><dd>
<dl class="switch">
<dt>A start tag whose tag name is "tr"
</dt><dd>
<p><a href="#clear2">Clear the stack back to a table body
context</a>. (See below.)</p>
<p><a href="#insert" title="insert an HTML element">Insert a
<code>tr</code> element</a> for the token, then switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-row" title="insertion mode: in row">in row</a>".</p>
</dd><dt>A start tag whose tag name is one of: "th", "td"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Act as if a start tag with the
tag name "tr" had been seen, then reprocess the current token.</p>
</dd><dt>An end tag whose tag name is one of: "tbody", "tfoot", "thead"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> does not <a href="#have-an0" title="has an element in table scope">have an
element in table scope</a> with the same tag name as the token, this
is a <a href="section-parsing.html#parse">parse error</a>. Ignore the token.</p>
<p>Otherwise:</p>
<p><a href="#clear2">Clear the stack back to a table body
context</a>. (See below.)</p>
<p>Pop the <a href="#current4">current node</a> from the <a href="#stack">stack of open elements</a>. Switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-table" title="insertion mode: in table">in table</a>".</p>
</dd><dt>A start tag whose tag name is one of: "caption", "col",
"colgroup", "tbody", "tfoot", "thead"
</dt><dt>An end tag whose tag name is "table"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> does not <a href="#have-an0" title="has an element in table scope">have a
<code>tbody</code>, <code>thead</code>, or <code>tfoot</code>
element in table scope</a>, this is a <a href="section-parsing.html#parse">parse
error</a>. Ignore the token. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise:</p>
<p><a href="#clear2">Clear the stack back to a table body
context</a>. (See below.)</p>
<p>Act as if an end tag with the same tag name as the <a href="#current4">current node</a> ("tbody", "tfoot", or "thead") had
been seen, then reprocess the current token.</p>
</dd><dt>An end tag whose tag name is one of: "body", "caption", "col",
"colgroup", "html", "td", "th", "tr"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>Anything else
</dt><dd>
<p>Process the token as if the <a href="#insertion0">insertion
mode</a> was "<a href="#in-table" title="insertion mode: in
table">in table</a>".</p>
</dd></dl>
<p>When the steps above require the UA to <dfn id="clear2">clear the
stack back to a table body context</dfn>, it means that the UA must,
while the <a href="#current4">current node</a> is not a <code><a href="section-tabular.html#tbody">tbody</a></code>, <code><a href="section-tabular.html#tfoot0">tfoot</a></code>, <code><a href="section-tabular.html#thead0">thead</a></code>, or <code><a href="section-the-root.html#html">html</a></code> element, pop elements from the <a href="#stack">stack of open elements</a>. If this causes any elements
to be popped from the stack, then this is a <a href="section-parsing.html#parse">parse
error</a>.</p>
<p class="note">The <a href="#current4">current node</a> being an
<code><a href="section-the-root.html#html">html</a></code> element after this process is an
<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>.</p>
</dd><dt id="parsing-main-intr">If the <a href="#insertion0">insertion mode</a>
is "<dfn id="in-row" title="insertion mode: in row">in row</dfn>"
</dt><dd>
<dl class="switch">
<dt>A start tag whose tag name is one of: "th", "td"
</dt><dd>
<p><a href="#clear3">Clear the stack back to a table row context</a>.
(See below.)</p>
<p><a href="#insert" title="insert an HTML element">Insert an HTML
element</a> for the token, then switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-cell" title="insertion mode: in cell">in cell</a>".</p>
<p>Insert a marker at the end of the <a href="#list-of4">list of
active formatting elements</a>.</p>
</dd><dt>An end tag whose tag name is "tr"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> does not <a href="#have-an0" title="has an element in table scope">have an
element in table scope</a> with the same tag name as the token, this
is a <a href="section-parsing.html#parse">parse error</a>. Ignore the token. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise:</p>
<p><a href="#clear3">Clear the stack back to a table row context</a>.
(See below.)</p>
<p>Pop the <a href="#current4">current node</a> (which will be a
<code><a href="section-tabular.html#tr">tr</a></code> element) from the <a href="#stack">stack of open elements</a>. Switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-table0" title="insertion mode: in table body">in table body</a>".</p>
</dd><dt>A start tag whose tag name is one of: "caption", "col",
"colgroup", "tbody", "tfoot", "thead", "tr"
</dt><dt>An end tag whose tag name is "table"
</dt><dd>
<p>Act as if an end tag with the tag name "tr" had been seen, then,
if that token wasn't ignored, reprocess the current token.</p>
<p class="note">The fake end tag token here can only be ignored in the
<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>.</p>
</dd><dt>An end tag whose tag name is one of: "tbody", "tfoot", "thead"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> does not <a href="#have-an0" title="has an element in table scope">have an
element in table scope</a> with the same tag name as the token, this
is a <a href="section-parsing.html#parse">parse error</a>. Ignore the token.</p>
<p>Otherwise, act as if an end tag with the tag name "tr" had been
seen, then reprocess the current token.</p>
</dd><dt>An end tag whose tag name is one of: "body", "caption", "col",
"colgroup", "html", "td", "th"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>Anything else
</dt><dd>
<p>Process the token as if the <a href="#insertion0">insertion
mode</a> was "<a href="#in-table" title="insertion mode: in
table">in table</a>".</p>
</dd></dl>
<p>When the steps above require the UA to <dfn id="clear3">clear the
stack back to a table row context</dfn>, it means that the UA must,
while the <a href="#current4">current node</a> is not a <code><a href="section-tabular.html#tr">tr</a></code> element or an <code><a href="section-the-root.html#html">html</a></code> element, pop elements from the <a href="#stack">stack of open elements</a>. If this causes any elements
to be popped from the stack, then this is a <a href="section-parsing.html#parse">parse
error</a>.</p>
<p class="note">The <a href="#current4">current node</a> being an
<code><a href="section-the-root.html#html">html</a></code> element after this process is an
<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>.</p>
</dd><dt id="parsing-main-intd">If the <a href="#insertion0">insertion mode</a>
is "<dfn id="in-cell" title="insertion mode: in cell">in cell</dfn>"
</dt><dd>
<dl class="switch">
<dt>An end tag whose tag name is one of: "td", "th"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> does not <a href="#have-an0" title="has an element in table scope">have an
element in table scope</a> with the same tag name as that of the
token, then this is a <a href="section-parsing.html#parse">parse error</a> and the token
must be ignored.</p>
<p>Otherwise:</p>
<p><a href="#generate">Generate implied end tags</a>, except for
elements with the same tag name as the token.</p>
<p>Now, if the <a href="#current4">current node</a> is not an element
with the same tag name as the token, then this is a <a href="section-parsing.html#parse">parse error</a>.</p>
<p>Pop elements from this stack until an element with the same tag
name as the token has been popped from the stack.</p>
<p><a href="#clear0">Clear the list of active formatting elements up
to the last marker</a>.</p>
<p>Switch the <a href="#insertion0">insertion mode</a> to "<a href="#in-row" title="insertion mode: in row">in row</a>". (The <a href="#current4">current node</a> will be a <code><a href="section-tabular.html#tr">tr</a></code> element at this point.)</p>
</dd><dt>A start tag whose tag name is one of: "caption", "col",
"colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> does
<em>not</em> <a href="#have-an0" title="has an element in table
scope">have a <code>td</code> or <code>th</code> element in table
scope</a>, then this is a <a href="section-parsing.html#parse">parse error</a>; ignore
the token. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise, <a href="#close2">close the cell</a> (see below) and
reprocess the current token.</p>
</dd><dt>An end tag whose tag name is one of: "body", "caption", "col",
"colgroup", "html"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>An end tag whose tag name is one of: "table", "tbody", "tfoot",
"thead", "tr"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> does not <a href="#have-an0" title="has an element in table scope">have an
element in table scope</a> with the same tag name as that of the
token (which can only happen for "tbody", "tfoot" and "thead", or,
in the <a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>), then
this is a <a href="section-parsing.html#parse">parse error</a> and the token must be
ignored.</p>
<p>Otherwise, <a href="#close2">close the cell</a> (see below) and
reprocess the current token.</p>
</dd><dt>Anything else
</dt><dd>
<p>Process the token as if the <a href="#insertion0">insertion
mode</a> was "<a href="#in-body" title="insertion mode: in body">in
body</a>".</p>
</dd></dl>
<p>Where the steps above say to <dfn id="close2">close the cell</dfn>,
they mean to follow the following algorithm:</p>
<ol>
<li>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an0" title="has an element in table scope">has a
<code>td</code> element in table scope</a>, then act as if an end
tag token with the tag name "td" had been seen.
</p></li><li>
<p>Otherwise, the <a href="#stack">stack of open elements</a> will <a href="#have-an0" title="has an element in table scope">have a
<code>th</code> element in table scope</a>; act as if an end tag
token with the tag name "th" had been seen.
</p></li></ol>
<p class="note">The <a href="#stack">stack of open elements</a> cannot
have both a <code><a href="section-tabular.html#td">td</a></code> and a <code><a href="section-tabular.html#th">th</a></code> element <a href="#have-an0" title="has an
element in table scope">in table scope</a> at the same time, nor can
it have neither when the <a href="#insertion0">insertion mode</a> is
"<a href="#in-cell" title="insertion mode: in cell">in cell</a>".</p>
</dd><dt id="parsing-main-inselect">If the <a href="#insertion0">insertion
mode</a> is "<dfn id="in-select" title="insertion mode: in select">in
select</dfn>"
</dt><dd>
<p>Handle the token as follows:</p>
<dl class="switch">
<dt>A character token
</dt><dd>
<p><a href="#append" title="append a character">Append the token's
character</a> to the <a href="#current4">current node</a>.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <a href="#current4">current node</a> with the <code title="">data</code> attribute set to the data given in the comment
token.</p>
</dd><dt>A start tag token whose tag name is "option"
</dt><dd>
<p>If the <a href="#current4">current node</a> is an
<code>option</code> element, act as if an end tag with the tag name
"option" had been seen.</p>
<p><a href="#insert">Insert an HTML element</a> for the token.</p>
</dd><dt>A start tag token whose tag name is "optgroup"
</dt><dd>
<p>If the <a href="#current4">current node</a> is an
<code>option</code> element, act as if an end tag with the tag name
"option" had been seen.</p>
<p>If the <a href="#current4">current node</a> is an
<code>optgroup</code> element, act as if an end tag with the tag
name "optgroup" had been seen.</p>
<p><a href="#insert">Insert an HTML element</a> for the token.</p>
</dd><dt>An end tag token whose tag name is "optgroup"
</dt><dd>
<p>First, if the <a href="#current4">current node</a> is an
<code>option</code> element, and the node immediately before it in
the <a href="#stack">stack of open elements</a> is an
<code>optgroup</code> element, then act as if an end tag with the
tag name "option" had been seen.</p>
<p>If the <a href="#current4">current node</a> is an
<code>optgroup</code> element, then pop that node from the <a href="#stack">stack of open elements</a>. Otherwise, this is a <a href="section-parsing.html#parse">parse error</a>, ignore the token.</p>
</dd><dt>An end tag token whose tag name is "option"
</dt><dd>
<p>If the <a href="#current4">current node</a> is an
<code>option</code> element, then pop that node from the <a href="#stack">stack of open elements</a>. Otherwise, this is a <a href="section-parsing.html#parse">parse error</a>, ignore the token.</p>
</dd><dt>An end tag whose tag name is "select"
</dt><dd>
<p>If the <a href="#stack">stack of open elements</a> does not <a href="#have-an0" title="has an element in table scope">have an
element in table scope</a> with the same tag name as the token, this
is a <a href="section-parsing.html#parse">parse error</a>. Ignore the token. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise:</p>
<p>Pop elements from the <a href="#stack">stack of open elements</a>
until a <code>select</code> element has been popped from the stack.</p>
<p><a href="#reset">Reset the insertion mode appropriately</a>.</p>
</dd><dt>A start tag whose tag name is "select"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Act as if the token had been an
end tag with the tag name "select" instead.</p>
</dd><dt>An end tag whose tag name is one of: "caption", "table", "tbody",
"tfoot", "thead", "tr", "td", "th"
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>.</p>
<p>If the <a href="#stack">stack of open elements</a> <a href="#have-an0">has an element in table scope</a> with the same tag
name as that of the token, then act as if an end tag with the tag
name "select" had been seen, and reprocess the token. Otherwise,
ignore the token.</p>
</dd><dt>Anything else
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd></dl>
</dd><dt id="parsing-main-afterbody">If the <a href="#insertion0">insertion
mode</a> is "<dfn id="after2" title="insertion mode: after body">after
body</dfn>"
</dt><dd>
<p>Handle the token as follows:</p>
<dl class="switch">
<dt>A character token that is one of one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p>Process the token as it would be processed if the <a href="#insertion0">insertion mode</a> was "<a href="#in-body" title="insertion mode: in body">in body</a>".</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the first element in the <a href="#stack">stack of open elements</a> (the <code><a href="section-the-root.html#html">html</a></code> element), with the <code title="">data</code> attribute set to the data given in the comment
token.</p>
</dd><dt>An end tag with the tag name "html"
</dt><dd>
<p>If the parser was originally created in order to handle the
setting of <em>an element</em>'s <code title="dom-innerHTML-HTML"><a href="section-dynamic.html#innerhtml0">innerHTML</a></code> attribute, this is a <a href="section-parsing.html#parse">parse error</a>; ignore the token. (The element will
be an <code><a href="section-the-root.html#html">html</a></code> element in this case.)
(<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise, switch to <a href="#the-trailing0">the trailing end
phase</a>.</p>
</dd><dt>Anything else
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Set the <a href="#insertion0">insertion mode</a> to "<a href="#in-body" title="insertion mode: in body">in body</a>" and reprocess the
token.</p>
</dd></dl>
</dd><dt id="parsing-main-inframeset">If the <a href="#insertion0">insertion
mode</a> is "<dfn id="in-frameset" title="insertion mode: in frameset">in
frameset</dfn>"
</dt><dd>
<p>Handle the token as follows:</p>
<dl class="switch">
<dt>A character token that is one of one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p><a href="#append" title="append a character">Append the
character</a> to the <a href="#current4">current node</a>.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <a href="#current4">current node</a> with the <code title="">data</code> attribute set to the data given in the comment
token.</p>
</dd><dt>A start tag with the tag name "frameset"
</dt><dd>
<p><a href="#insert" title="Insert an HTML element">Insert a
<code>frameset</code> element</a> for the token.</p>
</dd><dt>An end tag with the tag name "frameset"
</dt><dd>
<p>If the <a href="#current4">current node</a> is the root <code><a href="section-the-root.html#html">html</a></code> element, then this is a <a href="section-parsing.html#parse">parse error</a>; ignore the token. (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>)</p>
<p>Otherwise, pop the <a href="#current4">current node</a> from the
<a href="#stack">stack of open elements</a>.</p>
<p>If the parser was <em>not</em> originally created in order to
handle the setting of an element's <code title="dom-innerHTML-HTML"><a href="section-dynamic.html#innerhtml0">innerHTML</a></code> attribute (<a href="section-dynamic.html#innerhtml1"><code>innerHTML</code> case</a>), and the <a href="#current4">current node</a> is no longer a
<code>frameset</code> element, then change the <a href="#insertion0">insertion mode</a> to "<a href="#after3" title="insertion mode: after frameset">after frameset</a>".</p>
</dd><dt>A start tag with the tag name "frame"
</dt><dd>
<p><a href="#insert">Insert an HTML element</a> for the token.
Immediately pop the <a href="#current4">current node</a> off the <a href="#stack">stack of open elements</a>.</p>
</dd><dt>A start tag with the tag name "noframes"
</dt><dd>
<p>Process the token as if the <a href="#insertion0">insertion
mode</a> had been "<a href="#in-body" title="insertion mode: in
body">in body</a>".</p>
</dd><dt>Anything else
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd></dl>
</dd><dt id="parsing-main-afterframeset">If the <a href="#insertion0">insertion
mode</a> is "<dfn id="after3" title="insertion mode: after
frameset">after frameset</dfn>"
</dt><dd>
<p>Handle the token as follows:</p>
<dl class="switch">
<dt>A character token that is one of one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
FORM FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p><a href="#append" title="append a character">Append the
character</a> to the <a href="#current4">current node</a>.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <a href="#current4">current node</a> with the <code title="">data</code> attribute set to the data given in the comment
token.</p>
</dd><dt>An end tag with the tag name "html"
</dt><dd>
<p>Switch to <a href="#the-trailing0">the trailing end phase</a>.</p>
</dd><dt>A start tag with the tag name "noframes"
</dt><dd>
<p>Process the token as if the <a href="#insertion0">insertion
mode</a> had been "<a href="#in-body" title="insertion mode: in
body">in body</a>".</p>
</dd><dt>Anything else
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd></dl>
</dd></dl>
</dd></dl>
<p class="big-issue">This doesn't handle UAs that don't support frames, or
that do support frames but want to show the NOFRAMES content. Supporting
the former is easy; supporting the latter is harder.
</p><h5 id="the-trailing"><span class="secno">8.2.4.4. </span><dfn id="the-trailing0">The trailing end phase</dfn></h5>
<p>After <a href="#the-main0">the main phase</a>, as each token is emitted
from the <a href="section-tokenisation.html#tokenisation0">tokenisation</a> stage, it must be
processed as described in this section.
</p><dl class="switch">
<dt>A DOCTYPE token
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Ignore the token.</p>
</dd><dt>A comment token
</dt><dd>
<p>Append a <code>Comment</code> node to the <code>Document</code> object
with the <code title="">data</code> attribute set to the data given in
the comment token.</p>
</dd><dt>A character token that is one of one of U+0009 CHARACTER TABULATION,
U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dd>
<p>Process the token as it would be processed in <a href="#the-main0">the
main phase</a>.</p>
</dd><dt>A character token that is <em>not</em> one of U+0009 CHARACTER
TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM
FEED (FF), U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
</dt><dt>A start tag token
</dt><dt>An end tag token
</dt><dd>
<p><a href="section-parsing.html#parse">Parse error</a>. Switch back to <a href="#the-main0">the main phase</a> and reprocess the token.</p>
</dd><dt>An end-of-file token
</dt><dd>
<p><a href="#stops">Stop parsing</a>.</p>
</dd></dl>
<h4 id="the-end"><span class="secno">8.2.5. </span>The End</h4>
<p>Once the user agent <dfn id="stops" title="stop parsing">stops
parsing</dfn> the document, the user agent must follow the steps in this
section.
</p><p>First, <!--the user agent must <span title="fire a DOMContentLoaded
event">fire a <code
title="event-DOMContentLoaded">DOMContentLoaded</code> event</span>
at <span>the <code>body</code> element</span>.</p>
<p>Then, -->the
rules for <a href="section-scripting0.html#when-a">when a script completes loading</a> start
applying (script execution is no longer managed by the parser).
</p><p>If any of the scripts in the <a href="section-scripting0.html#list-of1">list of scripts that
will execute as soon as possible</a> have <span>completed
loading</span><!-- XXX xref -->, or if the <a href="section-scripting0.html#list-of0">list of
scripts that will execute asynchronously</a> is not empty and the first
script in that list has <span>completed loading</span><!-- XXX xref
-->,
then the user agent must act as if those scripts just completed loading,
following the rules given for that in the <code><a href="section-scripting0.html#script0">script</a></code> element definition.
</p><p>Then, if the <a href="section-scripting0.html#list-of">list of scripts that will execute when
the document has finished parsing</a> is not empty, and the first item in
this list has already <span>completed loading</span><!--XXX
xref -->,
then the user agent must act as if that script just finished loading.
</p><p>By this point, there will be no scripts that have loaded but have not
yet been executed.
</p><p>The user agent must then <a href="section-scripting.html#firing2">fire a simple event</a>
called <code title="event-DOMContentLoaded">DOMContentLoaded</code> at the
<code>Document</code>.
</p><p>Once everything that <dfn id="delays" title="delay the load event">delays
the load event</dfn> has completed, the user agent must <a href="section-scripting.html#firing4" title="fire a load event">fire a <code title="event-load">load</code>
event</a> at <a href="section-dom-tree.html#the-body0">the <code>body</code> element</a>.</p>
<!-- XXX make sure things "delay the load event" -->
<!--XXX need to handle
http://lxr.mozilla.org/mozilla/source/parser/htmlparser/src/CNavDTD.cpp#2354
2354 // Don't open transient styles if it makes the stack deep, bug 58917.
-->
<!--XXX
http://lxr.mozilla.org/mozilla/source/parser/htmlparser/src/nsHTMLTokenizer.cpp#749
-->
<!--
see also CTextToken::ConsumeCharacterData() for CDATA parsing?
1212 1 Here's a tricky case from bug 22596: <h5><li><h5>
1213 How do we know that the 2nd <h5> should close the <LI> rather than nest inside the <LI>?
1214 (Afterall, the <h5> is a legal child of the <LI>).
1215
1216 The way you know is that there is no root between the two, so the <h5> binds more
1217 tightly to the 1st <h5> than to the <LI>.
1218 2. Also, bug 6148 shows this case: <SPAN><DIV><SPAN>
1219 From this case we learned not to execute this logic if the parent is a block.
1220
1221 3. Fix for 26583
1222 Ex. <A href=foo.html><B>foo<A href-bar.html>bar</A></B></A> <- A legal HTML
1223 In the above example clicking on "foo" or "bar" should link to
1224 foo.html or bar.html respectively. That is, the inner <A> should be informed
1225 about the presence of an open <A> above <B>..so that the inner <A> can close out
1226 the outer <A>. The following code does it for us.
1227
1228 4. Fix for 27865 [ similer to 22596 ]. Ex: <DL><DD><LI>one<DD><LI>two
- http://lxr.mozilla.org/mozilla/source/parser/htmlparser/src/CNavDTD.cpp#1211
815 // Here's a problem. If theTag is legal in here, we don't move it
816 // out. So if we're moving stuff out of here, the parent of theTag
817 // gets closed at this point. But some things are legal
818 // _everywhere_ and hence would effectively close out misplaced
819 // content in tables. This is undesirable, so treat them as
820 // illegal here so they'll be shipped out with their parents and
821 // siblings. See bug 40855 for an explanation (that bug was for
822 // comments, but the same issues arise with whitespace, newlines,
823 // noscript, etc). Script is special, though. Shipping it out
824 // breaks document.write stuff. See bug 243064.
- http://lxr.mozilla.org/mozilla/source/parser/htmlparser/src/CNavDTD.cpp#825
1326 /**************************************************************************************
1327 *
1328 * Now a little code to deal with bug #49687 (crash when layout stack gets too deep)
1329 * I've also opened this up to any container (not just inlines): re bug 55095
1330 * Improved to handle bug 55980 (infinite loop caused when DEPTH is exceeded and
1331 * </P> is encountered by itself (<P>) is continuously produced.
1332 *
1333 **************************************************************************************/
1912 // Oh boy!! we found a "stray" tag. Nav4.x and IE introduce line break in
1913 // such cases. So, let's simulate that effect for compatibility.
1914 // Ex. <html><body>Hello</P>There</body></html>
http://lxr.mozilla.org/mozilla/source/parser/htmlparser/src/CNavDTD.cpp#1912
http://lxr.mozilla.org/seamonkey/search?string=nested
/parser/htmlparser/src/CNavDTD.cpp, line 791 - * 2. <CENTER><DL><DT><A><CENTER> allow nested <CENTER>
/parser/htmlparser/src/CNavDTD.cpp, line 792 - * 3. <TABLE><TR><TD><TABLE>... allow nested <TABLE>
/parser/htmlparser/src/CNavDTD.cpp, line 2562 - // Discard nested forms - bug 72639
/parser/htmlparser/src/nsElementTable.cpp, line 1453 - * 2. <CENTER><DL><DT><A><CENTER> allow nested <CENTER>
/parser/htmlparser/src/nsElementTable.cpp, line 1454 - * 3. <TABLE><TR><TD><TABLE>... allow nested <TABLE>
/parser/htmlparser/src/nsElementTable.cpp, line 1901 - // Ex: <H1><LI><H1><LI>. Inner LI has the potential of getting nested
-->
<script src="http://status.whatwg.org/annotate-web-apps.js" type="text/javascript"></script></body></html>
|