/usr/share/php/dompdf/lib/html5lib/Tokenizer.php is in php-dompdf 0.6.1+dfsg-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?php
/*
Copyright 2007 Jeroen van der Meer <http://jero.net/>
Copyright 2008 Edward Z. Yang <http://htmlpurifier.org/>
Copyright 2009 Geoffrey Sneddon <http://gsnedders.com/>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Some conventions:
// /* */ indicates verbatim text from the HTML 5 specification
// // indicates regular comments
// all flags are in hyphenated form
class HTML5_Tokenizer {
/**
* Points to an InputStream object.
*/
protected $stream;
/**
* Tree builder that the tokenizer emits token to.
*/
private $tree;
/**
* Current content model we are parsing as.
*/
protected $content_model;
/**
* Current token that is being built, but not yet emitted. Also
* is the last token emitted, if applicable.
*/
protected $token;
// These are constants describing the content model
const PCDATA = 0;
const RCDATA = 1;
const CDATA = 2;
const PLAINTEXT = 3;
// These are constants describing tokens
// XXX should probably be moved somewhere else, probably the
// HTML5 class.
const DOCTYPE = 0;
const STARTTAG = 1;
const ENDTAG = 2;
const COMMENT = 3;
const CHARACTER = 4;
const SPACECHARACTER = 5;
const EOF = 6;
const PARSEERROR = 7;
// These are constants representing bunches of characters.
const ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const UPPER_ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const LOWER_ALPHA = 'abcdefghijklmnopqrstuvwxyz';
const DIGIT = '0123456789';
const HEX = '0123456789ABCDEFabcdef';
const WHITESPACE = "\t\n\x0c ";
/**
* @param $data Data to parse
*/
public function __construct($data, $builder = null) {
$this->stream = new HTML5_InputStream($data);
if (!$builder) $this->tree = new HTML5_TreeBuilder;
else $this->tree = $builder;
$this->content_model = self::PCDATA;
}
public function parseFragment($context = null) {
$this->tree->setupContext($context);
if ($this->tree->content_model) {
$this->content_model = $this->tree->content_model;
$this->tree->content_model = null;
}
$this->parse();
}
// XXX maybe convert this into an iterator? regardless, this function
// and the save function should go into a Parser facade of some sort
/**
* Performs the actual parsing of the document.
*/
public function parse() {
// Current state
$state = 'data';
// This is used to avoid having to have look-behind in the data state.
$lastFourChars = '';
/**
* Escape flag as specified by the HTML5 specification: "used to
* control the behavior of the tokeniser. It is either true or
* false, and initially must be set to the false state."
*/
$escape = false;
//echo "\n\n";
while($state !== null) {
/*echo $state . ' ';
switch ($this->content_model) {
case self::PCDATA: echo 'PCDATA'; break;
case self::RCDATA: echo 'RCDATA'; break;
case self::CDATA: echo 'CDATA'; break;
case self::PLAINTEXT: echo 'PLAINTEXT'; break;
}
if ($escape) echo " escape";
echo "\n";*/
switch($state) {
case 'data':
/* Consume the next input character */
$char = $this->stream->char();
$lastFourChars .= $char;
if (strlen($lastFourChars) > 4) $lastFourChars = substr($lastFourChars, -4);
// see below for meaning
$hyp_cond =
!$escape &&
(
$this->content_model === self::RCDATA ||
$this->content_model === self::CDATA
);
$amp_cond =
!$escape &&
(
$this->content_model === self::PCDATA ||
$this->content_model === self::RCDATA
);
$lt_cond =
$this->content_model === self::PCDATA ||
(
(
$this->content_model === self::RCDATA ||
$this->content_model === self::CDATA
) &&
!$escape
);
$gt_cond =
$escape &&
(
$this->content_model === self::RCDATA ||
$this->content_model === self::CDATA
);
if($char === '&' && $amp_cond) {
/* U+0026 AMPERSAND (&)
When the content model flag is set to one of the PCDATA or RCDATA
states and the escape flag is false: switch to the
character reference data state. Otherwise: treat it as per
the "anything else" entry below. */
$state = 'character reference data';
} elseif(
$char === '-' &&
$hyp_cond &&
$lastFourChars === '<!--'
) {
/*
U+002D HYPHEN-MINUS (-)
If the content model flag is set to either the RCDATA state or
the CDATA state, and the escape flag is false, and there are at
least three characters before this one in the input stream, and the
last four characters in the input stream, including this one, are
U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+002D HYPHEN-MINUS,
and U+002D HYPHEN-MINUS ("<!--"), then set the escape flag to true. */
$escape = true;
/* In any case, emit the input character as a character token. Stay
in the data state. */
$this->emitToken(array(
'type' => self::CHARACTER,
'data' => '-'
));
// We do the "any case" part as part of "anything else".
/* U+003C LESS-THAN SIGN (<) */
} elseif($char === '<' && $lt_cond) {
/* When the content model flag is set to the PCDATA state: switch
to the tag open state.
When the content model flag is set to either the RCDATA state or
the CDATA state and the escape flag is false: switch to the tag
open state.
Otherwise: treat it as per the "anything else" entry below. */
$state = 'tag open';
/* U+003E GREATER-THAN SIGN (>) */
} elseif(
$char === '>' &&
$gt_cond &&
substr($lastFourChars, 1) === '-->'
) {
/* If the content model flag is set to either the RCDATA state or
the CDATA state, and the escape flag is true, and the last three
characters in the input stream including this one are U+002D
HYPHEN-MINUS, U+002D HYPHEN-MINUS, U+003E GREATER-THAN SIGN ("-->"),
set the escape flag to false. */
$escape = false;
/* In any case, emit the input character as a character token.
Stay in the data state. */
$this->emitToken(array(
'type' => self::CHARACTER,
'data' => '>'
));
// We do the "any case" part as part of "anything else".
} elseif($char === false) {
/* EOF
Emit an end-of-file token. */
$state = null;
$this->tree->emitToken(array(
'type' => self::EOF
));
} elseif($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
// Directly after emitting a token you switch back to the "data
// state". At that point spaceCharacters are important so they are
// emitted separately.
$chars = $this->stream->charsWhile(self::WHITESPACE);
$this->emitToken(array(
'type' => self::SPACECHARACTER,
'data' => $char . $chars
));
$lastFourChars .= $chars;
if (strlen($lastFourChars) > 4) $lastFourChars = substr($lastFourChars, -4);
} else {
/* Anything else
THIS IS AN OPTIMIZATION: Get as many character that
otherwise would also be treated as a character token and emit it
as a single character token. Stay in the data state. */
$mask = '';
if ($hyp_cond) $mask .= '-';
if ($amp_cond) $mask .= '&';
if ($lt_cond) $mask .= '<';
if ($gt_cond) $mask .= '>';
if ($mask === '') {
$chars = $this->stream->remainingChars();
} else {
$chars = $this->stream->charsUntil($mask);
}
$this->emitToken(array(
'type' => self::CHARACTER,
'data' => $char . $chars
));
$lastFourChars .= $chars;
if (strlen($lastFourChars) > 4) $lastFourChars = substr($lastFourChars, -4);
$state = 'data';
}
break;
case 'character reference data':
/* (This cannot happen if the content model flag
is set to the CDATA state.) */
/* Attempt to consume a character reference, with no
additional allowed character. */
$entity = $this->consumeCharacterReference();
/* If nothing is returned, emit a U+0026 AMPERSAND
character token. Otherwise, emit the character token that
was returned. */
// This is all done when consuming the character reference.
$this->emitToken(array(
'type' => self::CHARACTER,
'data' => $entity
));
/* Finally, switch to the data state. */
$state = 'data';
break;
case 'tag open':
$char = $this->stream->char();
switch($this->content_model) {
case self::RCDATA:
case self::CDATA:
/* Consume the next input character. If it is a
U+002F SOLIDUS (/) character, switch to the close
tag open state. Otherwise, emit a U+003C LESS-THAN
SIGN character token and reconsume the current input
character in the data state. */
// We consumed above.
if($char === '/') {
$state = 'close tag open';
} else {
$this->emitToken(array(
'type' => self::CHARACTER,
'data' => '<'
));
$this->stream->unget();
$state = 'data';
}
break;
case self::PCDATA:
/* If the content model flag is set to the PCDATA state
Consume the next input character: */
// We consumed above.
if($char === '!') {
/* U+0021 EXCLAMATION MARK (!)
Switch to the markup declaration open state. */
$state = 'markup declaration open';
} elseif($char === '/') {
/* U+002F SOLIDUS (/)
Switch to the close tag open state. */
$state = 'close tag open';
} elseif('A' <= $char && $char <= 'Z') {
/* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
Create a new start tag token, set its tag name to the lowercase
version of the input character (add 0x0020 to the character's code
point), then switch to the tag name state. (Don't emit the token
yet; further details will be filled in before it is emitted.) */
$this->token = array(
'name' => strtolower($char),
'type' => self::STARTTAG,
'attr' => array()
);
$state = 'tag name';
} elseif('a' <= $char && $char <= 'z') {
/* U+0061 LATIN SMALL LETTER A through to U+007A LATIN SMALL LETTER Z
Create a new start tag token, set its tag name to the input
character, then switch to the tag name state. (Don't emit
the token yet; further details will be filled in before it
is emitted.) */
$this->token = array(
'name' => $char,
'type' => self::STARTTAG,
'attr' => array()
);
$state = 'tag name';
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Parse error. Emit a U+003C LESS-THAN SIGN character token and a
U+003E GREATER-THAN SIGN character token. Switch to the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-tag-name-but-got-right-bracket'
));
$this->emitToken(array(
'type' => self::CHARACTER,
'data' => '<>'
));
$state = 'data';
} elseif($char === '?') {
/* U+003F QUESTION MARK (?)
Parse error. Switch to the bogus comment state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-tag-name-but-got-question-mark'
));
$this->token = array(
'data' => '?',
'type' => self::COMMENT
);
$state = 'bogus comment';
} else {
/* Anything else
Parse error. Emit a U+003C LESS-THAN SIGN character token and
reconsume the current input character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-tag-name'
));
$this->emitToken(array(
'type' => self::CHARACTER,
'data' => '<'
));
$state = 'data';
$this->stream->unget();
}
break;
}
break;
case 'close tag open':
if (
$this->content_model === self::RCDATA ||
$this->content_model === self::CDATA
) {
/* If the content model flag is set to the RCDATA or CDATA
states... */
$name = strtolower($this->stream->charsWhile(self::ALPHA));
$following = $this->stream->char();
$this->stream->unget();
if (
!$this->token ||
$this->token['name'] !== $name ||
$this->token['name'] === $name && !in_array($following, array("\x09", "\x0A", "\x0C", "\x20", "\x3E", "\x2F", false))
) {
/* if no start tag token has ever been emitted by this instance
of the tokenizer (fragment case), or, if the next few
characters do not match the tag name of the last start tag
token emitted (compared in an ASCII case-insensitive manner),
or if they do but they are not immediately followed by one of
the following characters:
* U+0009 CHARACTER TABULATION
* U+000A LINE FEED (LF)
* U+000C FORM FEED (FF)
* U+0020 SPACE
* U+003E GREATER-THAN SIGN (>)
* U+002F SOLIDUS (/)
* EOF
...then emit a U+003C LESS-THAN SIGN character token, a
U+002F SOLIDUS character token, and switch to the data
state to process the next input character. */
// XXX: Probably ought to replace in_array with $following === x ||...
// We also need to emit $name now we've consumed that, as we
// know it'll just be emitted as a character token.
$this->emitToken(array(
'type' => self::CHARACTER,
'data' => '</' . $name
));
$state = 'data';
} else {
// This matches what would happen if we actually did the
// otherwise below (but we can't because we've consumed too
// much).
// Start the end tag token with the name we already have.
$this->token = array(
'name' => $name,
'type' => self::ENDTAG
);
// Change to tag name state.
$state = 'tag name';
}
} elseif ($this->content_model === self::PCDATA) {
/* Otherwise, if the content model flag is set to the PCDATA
state [...]: */
$char = $this->stream->char();
if ('A' <= $char && $char <= 'Z') {
/* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
Create a new end tag token, set its tag name to the lowercase version
of the input character (add 0x0020 to the character's code point), then
switch to the tag name state. (Don't emit the token yet; further details
will be filled in before it is emitted.) */
$this->token = array(
'name' => strtolower($char),
'type' => self::ENDTAG
);
$state = 'tag name';
} elseif ('a' <= $char && $char <= 'z') {
/* U+0061 LATIN SMALL LETTER A through to U+007A LATIN SMALL LETTER Z
Create a new end tag token, set its tag name to the
input character, then switch to the tag name state.
(Don't emit the token yet; further details will be
filled in before it is emitted.) */
$this->token = array(
'name' => $char,
'type' => self::ENDTAG
);
$state = 'tag name';
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Parse error. Switch to the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-closing-tag-but-got-right-bracket'
));
$state = 'data';
} elseif($char === false) {
/* EOF
Parse error. Emit a U+003C LESS-THAN SIGN character token and a U+002F
SOLIDUS character token. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-closing-tag-but-got-eof'
));
$this->emitToken(array(
'type' => self::CHARACTER,
'data' => '</'
));
$this->stream->unget();
$state = 'data';
} else {
/* Parse error. Switch to the bogus comment state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-closing-tag-but-got-char'
));
$this->token = array(
'data' => $char,
'type' => self::COMMENT
);
$state = 'bogus comment';
}
}
break;
case 'tag name':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Switch to the before attribute name state. */
$state = 'before attribute name';
} elseif($char === '/') {
/* U+002F SOLIDUS (/)
Switch to the self-closing start tag state. */
$state = 'self-closing start tag';
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the current tag token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif('A' <= $char && $char <= 'Z') {
/* U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
Append the lowercase version of the current input
character (add 0x0020 to the character's code point) to
the current tag token's tag name. Stay in the tag name state. */
$chars = $this->stream->charsWhile(self::UPPER_ALPHA);
$this->token['name'] .= strtolower($char . $chars);
$state = 'tag name';
} elseif($char === false) {
/* EOF
Parse error. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-tag-name'
));
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append the current input character to the current tag token's tag name.
Stay in the tag name state. */
$chars = $this->stream->charsUntil("\t\n\x0C />" . self::UPPER_ALPHA);
$this->token['name'] .= $char . $chars;
$state = 'tag name';
}
break;
case 'before attribute name':
/* Consume the next input character: */
$char = $this->stream->char();
// this conditional is optimized, check bottom
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Stay in the before attribute name state. */
$state = 'before attribute name';
} elseif($char === '/') {
/* U+002F SOLIDUS (/)
Switch to the self-closing start tag state. */
$state = 'self-closing start tag';
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the current tag token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif('A' <= $char && $char <= 'Z') {
/* U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
Start a new attribute in the current tag token. Set that
attribute's name to the lowercase version of the current
input character (add 0x0020 to the character's code
point), and its value to the empty string. Switch to the
attribute name state.*/
$this->token['attr'][] = array(
'name' => strtolower($char),
'value' => ''
);
$state = 'attribute name';
} elseif($char === false) {
/* EOF
Parse error. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-attribute-name-but-got-eof'
));
$this->stream->unget();
$state = 'data';
} else {
/* U+0022 QUOTATION MARK (")
U+0027 APOSTROPHE (')
U+003C LESS-THAN SIGN (<)
U+003D EQUALS SIGN (=)
Parse error. Treat it as per the "anything else" entry
below. */
if($char === '"' || $char === "'" || $char === '<' || $char === '=') {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'invalid-character-in-attribute-name'
));
}
/* Anything else
Start a new attribute in the current tag token. Set that attribute's
name to the current input character, and its value to the empty string.
Switch to the attribute name state. */
$this->token['attr'][] = array(
'name' => $char,
'value' => ''
);
$state = 'attribute name';
}
break;
case 'attribute name':
// Consume the next input character:
$char = $this->stream->char();
// this conditional is optimized, check bottom
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Switch to the after attribute name state. */
$state = 'after attribute name';
} elseif($char === '/') {
/* U+002F SOLIDUS (/)
Switch to the self-closing start tag state. */
$state = 'self-closing start tag';
} elseif($char === '=') {
/* U+003D EQUALS SIGN (=)
Switch to the before attribute value state. */
$state = 'before attribute value';
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the current tag token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif('A' <= $char && $char <= 'Z') {
/* U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
Append the lowercase version of the current input
character (add 0x0020 to the character's code point) to
the current attribute's name. Stay in the attribute name
state. */
$chars = $this->stream->charsWhile(self::UPPER_ALPHA);
$last = count($this->token['attr']) - 1;
$this->token['attr'][$last]['name'] .= strtolower($char . $chars);
$state = 'attribute name';
} elseif($char === false) {
/* EOF
Parse error. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-attribute-name'
));
$this->stream->unget();
$state = 'data';
} else {
/* U+0022 QUOTATION MARK (")
U+0027 APOSTROPHE (')
U+003C LESS-THAN SIGN (<)
Parse error. Treat it as per the "anything else"
entry below. */
if($char === '"' || $char === "'" || $char === '<') {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'invalid-character-in-attribute-name'
));
}
/* Anything else
Append the current input character to the current attribute's name.
Stay in the attribute name state. */
$chars = $this->stream->charsUntil("\t\n\x0C /=>\"'" . self::UPPER_ALPHA);
$last = count($this->token['attr']) - 1;
$this->token['attr'][$last]['name'] .= $char . $chars;
$state = 'attribute name';
}
/* When the user agent leaves the attribute name state
(and before emitting the tag token, if appropriate), the
complete attribute's name must be compared to the other
attributes on the same token; if there is already an
attribute on the token with the exact same name, then this
is a parse error and the new attribute must be dropped, along
with the value that gets associated with it (if any). */
// this might be implemented in the emitToken method
break;
case 'after attribute name':
// Consume the next input character:
$char = $this->stream->char();
// this is an optimized conditional, check the bottom
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Stay in the after attribute name state. */
$state = 'after attribute name';
} elseif($char === '/') {
/* U+002F SOLIDUS (/)
Switch to the self-closing start tag state. */
$state = 'self-closing start tag';
} elseif($char === '=') {
/* U+003D EQUALS SIGN (=)
Switch to the before attribute value state. */
$state = 'before attribute value';
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the current tag token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif('A' <= $char && $char <= 'Z') {
/* U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
Start a new attribute in the current tag token. Set that
attribute's name to the lowercase version of the current
input character (add 0x0020 to the character's code
point), and its value to the empty string. Switch to the
attribute name state. */
$this->token['attr'][] = array(
'name' => strtolower($char),
'value' => ''
);
$state = 'attribute name';
} elseif($char === false) {
/* EOF
Parse error. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-end-of-tag-but-got-eof'
));
$this->stream->unget();
$state = 'data';
} else {
/* U+0022 QUOTATION MARK (")
U+0027 APOSTROPHE (')
U+003C LESS-THAN SIGN(<)
Parse error. Treat it as per the "anything else"
entry below. */
if($char === '"' || $char === "'" || $char === "<") {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'invalid-character-after-attribute-name'
));
}
/* Anything else
Start a new attribute in the current tag token. Set that attribute's
name to the current input character, and its value to the empty string.
Switch to the attribute name state. */
$this->token['attr'][] = array(
'name' => $char,
'value' => ''
);
$state = 'attribute name';
}
break;
case 'before attribute value':
// Consume the next input character:
$char = $this->stream->char();
// this is an optimized conditional
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Stay in the before attribute value state. */
$state = 'before attribute value';
} elseif($char === '"') {
/* U+0022 QUOTATION MARK (")
Switch to the attribute value (double-quoted) state. */
$state = 'attribute value (double-quoted)';
} elseif($char === '&') {
/* U+0026 AMPERSAND (&)
Switch to the attribute value (unquoted) state and reconsume
this input character. */
$this->stream->unget();
$state = 'attribute value (unquoted)';
} elseif($char === '\'') {
/* U+0027 APOSTROPHE (')
Switch to the attribute value (single-quoted) state. */
$state = 'attribute value (single-quoted)';
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Parse error. Emit the current tag token. Switch to the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-attribute-value-but-got-right-bracket'
));
$this->emitToken($this->token);
$state = 'data';
} elseif($char === false) {
/* EOF
Parse error. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-attribute-value-but-got-eof'
));
$this->stream->unget();
$state = 'data';
} else {
/* U+003D EQUALS SIGN (=)
* U+003C LESS-THAN SIGN (<)
Parse error. Treat it as per the "anything else" entry below. */
if($char === '=' || $char === '<') {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'equals-in-unquoted-attribute-value'
));
}
/* Anything else
Append the current input character to the current attribute's value.
Switch to the attribute value (unquoted) state. */
$last = count($this->token['attr']) - 1;
$this->token['attr'][$last]['value'] .= $char;
$state = 'attribute value (unquoted)';
}
break;
case 'attribute value (double-quoted)':
// Consume the next input character:
$char = $this->stream->char();
if($char === '"') {
/* U+0022 QUOTATION MARK (")
Switch to the after attribute value (quoted) state. */
$state = 'after attribute value (quoted)';
} elseif($char === '&') {
/* U+0026 AMPERSAND (&)
Switch to the character reference in attribute value
state, with the additional allowed character
being U+0022 QUOTATION MARK ("). */
$this->characterReferenceInAttributeValue('"');
} elseif($char === false) {
/* EOF
Parse error. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-attribute-value-double-quote'
));
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append the current input character to the current attribute's value.
Stay in the attribute value (double-quoted) state. */
$chars = $this->stream->charsUntil('"&');
$last = count($this->token['attr']) - 1;
$this->token['attr'][$last]['value'] .= $char . $chars;
$state = 'attribute value (double-quoted)';
}
break;
case 'attribute value (single-quoted)':
// Consume the next input character:
$char = $this->stream->char();
if($char === "'") {
/* U+0022 QUOTATION MARK (')
Switch to the after attribute value state. */
$state = 'after attribute value (quoted)';
} elseif($char === '&') {
/* U+0026 AMPERSAND (&)
Switch to the entity in attribute value state. */
$this->characterReferenceInAttributeValue("'");
} elseif($char === false) {
/* EOF
Parse error. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-attribute-value-single-quote'
));
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append the current input character to the current attribute's value.
Stay in the attribute value (single-quoted) state. */
$chars = $this->stream->charsUntil("'&");
$last = count($this->token['attr']) - 1;
$this->token['attr'][$last]['value'] .= $char . $chars;
$state = 'attribute value (single-quoted)';
}
break;
case 'attribute value (unquoted)':
// Consume the next input character:
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Switch to the before attribute name state. */
$state = 'before attribute name';
} elseif($char === '&') {
/* U+0026 AMPERSAND (&)
Switch to the entity in attribute value state, with the
additional allowed character being U+003E
GREATER-THAN SIGN (>). */
$this->characterReferenceInAttributeValue('>');
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the current tag token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* EOF
Parse error. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-attribute-value-no-quotes'
));
$this->stream->unget();
$state = 'data';
} else {
/* U+0022 QUOTATION MARK (")
U+0027 APOSTROPHE (')
U+003C LESS-THAN SIGN (<)
U+003D EQUALS SIGN (=)
Parse error. Treat it as per the "anything else"
entry below. */
if($char === '"' || $char === "'" || $char === '=' || $char == '<') {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-character-in-unquoted-attribute-value'
));
}
/* Anything else
Append the current input character to the current attribute's value.
Stay in the attribute value (unquoted) state. */
$chars = $this->stream->charsUntil("\t\n\x0c &>\"'=");
$last = count($this->token['attr']) - 1;
$this->token['attr'][$last]['value'] .= $char . $chars;
$state = 'attribute value (unquoted)';
}
break;
case 'after attribute value (quoted)':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Switch to the before attribute name state. */
$state = 'before attribute name';
} elseif ($char === '/') {
/* U+002F SOLIDUS (/)
Switch to the self-closing start tag state. */
$state = 'self-closing start tag';
} elseif ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the current tag token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* EOF
Parse error. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-EOF-after-attribute-value'
));
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Parse error. Reconsume the character in the before attribute
name state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-character-after-attribute-value'
));
$this->stream->unget();
$state = 'before attribute name';
}
break;
case 'self-closing start tag':
/* Consume the next input character: */
$char = $this->stream->char();
if ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Set the self-closing flag of the current tag token.
Emit the current tag token. Switch to the data state. */
// not sure if this is the name we want
$this->token['self-closing'] = true;
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* EOF
Parse error. Reconsume the EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-eof-after-self-closing'
));
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Parse error. Reconsume the character in the before attribute name state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-character-after-self-closing'
));
$this->stream->unget();
$state = 'before attribute name';
}
break;
case 'bogus comment':
/* (This can only happen if the content model flag is set to the PCDATA state.) */
/* Consume every character up to the first U+003E GREATER-THAN SIGN
character (>) or the end of the file (EOF), whichever comes first. Emit
a comment token whose data is the concatenation of all the characters
starting from and including the character that caused the state machine
to switch into the bogus comment state, up to and including the last
consumed character before the U+003E character, if any, or up to the
end of the file otherwise. (If the comment was started by the end of
the file (EOF), the token is empty.) */
$this->token['data'] .= (string) $this->stream->charsUntil('>');
$this->stream->char();
$this->emitToken($this->token);
/* Switch to the data state. */
$state = 'data';
break;
case 'markup declaration open':
// Consume for below
$hyphens = $this->stream->charsWhile('-', 2);
if ($hyphens === '-') {
$this->stream->unget();
}
if ($hyphens !== '--') {
$alpha = $this->stream->charsWhile(self::ALPHA, 7);
}
/* If the next two characters are both U+002D HYPHEN-MINUS (-)
characters, consume those two characters, create a comment token whose
data is the empty string, and switch to the comment state. */
if($hyphens === '--') {
$state = 'comment start';
$this->token = array(
'data' => '',
'type' => self::COMMENT
);
/* Otherwise if the next seven characters are a case-insensitive match
for the word "DOCTYPE", then consume those characters and switch to the
DOCTYPE state. */
} elseif(strtoupper($alpha) === 'DOCTYPE') {
$state = 'DOCTYPE';
// XXX not implemented
/* Otherwise, if the insertion mode is "in foreign content"
and the current node is not an element in the HTML namespace
and the next seven characters are an ASCII case-sensitive
match for the string "[CDATA[" (the five uppercase letters
"CDATA" with a U+005B LEFT SQUARE BRACKET character before
and after), then consume those characters and switch to the
CDATA section state (which is unrelated to the content model
flag's CDATA state). */
/* Otherwise, is is a parse error. Switch to the bogus comment state.
The next character that is consumed, if any, is the first character
that will be in the comment. */
} else {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-dashes-or-doctype'
));
$this->token = array(
'data' => (string) $alpha,
'type' => self::COMMENT
);
$state = 'bogus comment';
}
break;
case 'comment start':
/* Consume the next input character: */
$char = $this->stream->char();
if ($char === '-') {
/* U+002D HYPHEN-MINUS (-)
Switch to the comment start dash state. */
$state = 'comment start dash';
} elseif ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Parse error. Emit the comment token. Switch to the
data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'incorrect-comment'
));
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* EOF
Parse error. Emit the comment token. Reconsume the
EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-comment'
));
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append the input character to the comment token's
data. Switch to the comment state. */
$this->token['data'] .= $char;
$state = 'comment';
}
break;
case 'comment start dash':
/* Consume the next input character: */
$char = $this->stream->char();
if ($char === '-') {
/* U+002D HYPHEN-MINUS (-)
Switch to the comment end state */
$state = 'comment end';
} elseif ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Parse error. Emit the comment token. Switch to the
data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'incorrect-comment'
));
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* Parse error. Emit the comment token. Reconsume the
EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-comment'
));
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
$this->token['data'] .= '-' . $char;
$state = 'comment';
}
break;
case 'comment':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === '-') {
/* U+002D HYPHEN-MINUS (-)
Switch to the comment end dash state */
$state = 'comment end dash';
} elseif($char === false) {
/* EOF
Parse error. Emit the comment token. Reconsume the EOF character
in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-comment'
));
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append the input character to the comment token's data. Stay in
the comment state. */
$chars = $this->stream->charsUntil('-');
$this->token['data'] .= $char . $chars;
}
break;
case 'comment end dash':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === '-') {
/* U+002D HYPHEN-MINUS (-)
Switch to the comment end state */
$state = 'comment end';
} elseif($char === false) {
/* EOF
Parse error. Emit the comment token. Reconsume the EOF character
in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-comment-end-dash'
));
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append a U+002D HYPHEN-MINUS (-) character and the input
character to the comment token's data. Switch to the comment state. */
$this->token['data'] .= '-'.$char;
$state = 'comment';
}
break;
case 'comment end':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the comment token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif($char === '-') {
/* U+002D HYPHEN-MINUS (-)
Parse error. Append a U+002D HYPHEN-MINUS (-) character
to the comment token's data. Stay in the comment end
state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-dash-after-double-dash-in-comment'
));
$this->token['data'] .= '-';
} elseif($char === "\t" || $char === "\n" || $char === "\x0a" || $char === ' ') {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-space-after-double-dash-in-comment'
));
$this->token['data'] .= '--' . $char;
$state = 'comment end space';
} elseif($char === '!') {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-bang-after-double-dash-in-comment'
));
$state = 'comment end bang';
} elseif($char === false) {
/* EOF
Parse error. Emit the comment token. Reconsume the
EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-comment-double-dash'
));
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Parse error. Append two U+002D HYPHEN-MINUS (-)
characters and the input character to the comment token's
data. Switch to the comment state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-char-in-comment'
));
$this->token['data'] .= '--'.$char;
$state = 'comment';
}
break;
case 'comment end bang':
$char = $this->stream->char();
if ($char === '>') {
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === "-") {
$this->token['data'] .= '--!';
$state = 'comment end dash';
} elseif ($char === false) {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-comment-end-bang'
));
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
$this->token['data'] .= '--!' . $char;
$state = 'comment';
}
break;
case 'comment end space':
$char = $this->stream->char();
if ($char === '>') {
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === '-') {
$state = 'comment end dash';
} elseif ($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
$this->token['data'] .= $char;
} elseif ($char === false) {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-eof-in-comment-end-space',
));
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
$this->token['data'] .= $char;
$state = 'comment';
}
break;
case 'DOCTYPE':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Switch to the before DOCTYPE name state. */
$state = 'before DOCTYPE name';
} elseif($char === false) {
/* EOF
Parse error. Create a new DOCTYPE token. Set its
force-quirks flag to on. Emit the token. Reconsume the
EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'need-space-after-doctype-but-got-eof'
));
$this->emitToken(array(
'name' => '',
'type' => self::DOCTYPE,
'force-quirks' => true,
'error' => true
));
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Parse error. Reconsume the current character in the
before DOCTYPE name state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'need-space-after-doctype'
));
$this->stream->unget();
$state = 'before DOCTYPE name';
}
break;
case 'before DOCTYPE name':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Stay in the before DOCTYPE name state. */
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Parse error. Create a new DOCTYPE token. Set its
force-quirks flag to on. Emit the token. Switch to the
data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-doctype-name-but-got-right-bracket'
));
$this->emitToken(array(
'name' => '',
'type' => self::DOCTYPE,
'force-quirks' => true,
'error' => true
));
$state = 'data';
} elseif('A' <= $char && $char <= 'Z') {
/* U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
Create a new DOCTYPE token. Set the token's name to the
lowercase version of the input character (add 0x0020 to
the character's code point). Switch to the DOCTYPE name
state. */
$this->token = array(
'name' => strtolower($char),
'type' => self::DOCTYPE,
'error' => true
);
$state = 'DOCTYPE name';
} elseif($char === false) {
/* EOF
Parse error. Create a new DOCTYPE token. Set its
force-quirks flag to on. Emit the token. Reconsume the
EOF character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-doctype-name-but-got-eof'
));
$this->emitToken(array(
'name' => '',
'type' => self::DOCTYPE,
'force-quirks' => true,
'error' => true
));
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Create a new DOCTYPE token. Set the token's name to the
current input character. Switch to the DOCTYPE name state. */
$this->token = array(
'name' => $char,
'type' => self::DOCTYPE,
'error' => true
);
$state = 'DOCTYPE name';
}
break;
case 'DOCTYPE name':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Switch to the after DOCTYPE name state. */
$state = 'after DOCTYPE name';
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the current DOCTYPE token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif('A' <= $char && $char <= 'Z') {
/* U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
Append the lowercase version of the input character
(add 0x0020 to the character's code point) to the current
DOCTYPE token's name. Stay in the DOCTYPE name state. */
$this->token['name'] .= strtolower($char);
} elseif($char === false) {
/* EOF
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Reconsume the EOF
character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-doctype-name'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append the current input character to the current
DOCTYPE token's name. Stay in the DOCTYPE name state. */
$this->token['name'] .= $char;
}
// XXX this is probably some sort of quirks mode designation,
// check tree-builder to be sure. In general 'error' needs
// to be specc'ified, this probably means removing it at the end
$this->token['error'] = ($this->token['name'] === 'HTML')
? false
: true;
break;
case 'after DOCTYPE name':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Stay in the after DOCTYPE name state. */
} elseif($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the current DOCTYPE token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif($char === false) {
/* EOF
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Reconsume the EOF
character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else */
$nextSix = strtoupper($char . $this->stream->charsWhile(self::ALPHA, 5));
if ($nextSix === 'PUBLIC') {
/* If the next six characters are an ASCII
case-insensitive match for the word "PUBLIC", then
consume those characters and switch to the before
DOCTYPE public identifier state. */
$state = 'before DOCTYPE public identifier';
} elseif ($nextSix === 'SYSTEM') {
/* Otherwise, if the next six characters are an ASCII
case-insensitive match for the word "SYSTEM", then
consume those characters and switch to the before
DOCTYPE system identifier state. */
$state = 'before DOCTYPE system identifier';
} else {
/* Otherwise, this is the parse error. Set the DOCTYPE
token's force-quirks flag to on. Switch to the bogus
DOCTYPE state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-space-or-right-bracket-in-doctype'
));
$this->token['force-quirks'] = true;
$this->token['error'] = true;
$state = 'bogus DOCTYPE';
}
}
break;
case 'before DOCTYPE public identifier':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Stay in the before DOCTYPE public identifier state. */
} elseif ($char === '"') {
/* U+0022 QUOTATION MARK (")
Set the DOCTYPE token's public identifier to the empty
string (not missing), then switch to the DOCTYPE public
identifier (double-quoted) state. */
$this->token['public'] = '';
$state = 'DOCTYPE public identifier (double-quoted)';
} elseif ($char === "'") {
/* U+0027 APOSTROPHE (')
Set the DOCTYPE token's public identifier to the empty
string (not missing), then switch to the DOCTYPE public
identifier (single-quoted) state. */
$this->token['public'] = '';
$state = 'DOCTYPE public identifier (single-quoted)';
} elseif ($char === '>') {
/* Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Switch to the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-end-of-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* Parse error. Set the DOCTYPE token's force-quirks
flag to on. Emit that DOCTYPE token. Reconsume the EOF
character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Parse error. Set the DOCTYPE token's force-quirks flag
to on. Switch to the bogus DOCTYPE state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-char-in-doctype'
));
$this->token['force-quirks'] = true;
$state = 'bogus DOCTYPE';
}
break;
case 'DOCTYPE public identifier (double-quoted)':
/* Consume the next input character: */
$char = $this->stream->char();
if ($char === '"') {
/* U+0022 QUOTATION MARK (")
Switch to the after DOCTYPE public identifier state. */
$state = 'after DOCTYPE public identifier';
} elseif ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Switch to the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-end-of-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* EOF
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Reconsume the EOF
character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append the current input character to the current
DOCTYPE token's public identifier. Stay in the DOCTYPE
public identifier (double-quoted) state. */
$this->token['public'] .= $char;
}
break;
case 'DOCTYPE public identifier (single-quoted)':
/* Consume the next input character: */
$char = $this->stream->char();
if ($char === "'") {
/* U+0027 APOSTROPHE (')
Switch to the after DOCTYPE public identifier state. */
$state = 'after DOCTYPE public identifier';
} elseif ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Switch to the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-end-of-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* EOF
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Reconsume the EOF
character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append the current input character to the current
DOCTYPE token's public identifier. Stay in the DOCTYPE
public identifier (double-quoted) state. */
$this->token['public'] .= $char;
}
break;
case 'after DOCTYPE public identifier':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Stay in the after DOCTYPE public identifier state. */
} elseif ($char === '"') {
/* U+0022 QUOTATION MARK (")
Set the DOCTYPE token's system identifier to the
empty string (not missing), then switch to the DOCTYPE
system identifier (double-quoted) state. */
$this->token['system'] = '';
$state = 'DOCTYPE system identifier (double-quoted)';
} elseif ($char === "'") {
/* U+0027 APOSTROPHE (')
Set the DOCTYPE token's system identifier to the
empty string (not missing), then switch to the DOCTYPE
system identifier (single-quoted) state. */
$this->token['system'] = '';
$state = 'DOCTYPE system identifier (single-quoted)';
} elseif ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the current DOCTYPE token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* Parse error. Set the DOCTYPE token's force-quirks
flag to on. Emit that DOCTYPE token. Reconsume the EOF
character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Switch to the bogus DOCTYPE state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-char-in-doctype'
));
$this->token['force-quirks'] = true;
$state = 'bogus DOCTYPE';
}
break;
case 'before DOCTYPE system identifier':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Stay in the before DOCTYPE system identifier state. */
} elseif ($char === '"') {
/* U+0022 QUOTATION MARK (")
Set the DOCTYPE token's system identifier to the empty
string (not missing), then switch to the DOCTYPE system
identifier (double-quoted) state. */
$this->token['system'] = '';
$state = 'DOCTYPE system identifier (double-quoted)';
} elseif ($char === "'") {
/* U+0027 APOSTROPHE (')
Set the DOCTYPE token's system identifier to the empty
string (not missing), then switch to the DOCTYPE system
identifier (single-quoted) state. */
$this->token['system'] = '';
$state = 'DOCTYPE system identifier (single-quoted)';
} elseif ($char === '>') {
/* Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Switch to the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-char-in-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* Parse error. Set the DOCTYPE token's force-quirks
flag to on. Emit that DOCTYPE token. Reconsume the EOF
character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Parse error. Set the DOCTYPE token's force-quirks flag
to on. Switch to the bogus DOCTYPE state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-char-in-doctype'
));
$this->token['force-quirks'] = true;
$state = 'bogus DOCTYPE';
}
break;
case 'DOCTYPE system identifier (double-quoted)':
/* Consume the next input character: */
$char = $this->stream->char();
if ($char === '"') {
/* U+0022 QUOTATION MARK (")
Switch to the after DOCTYPE system identifier state. */
$state = 'after DOCTYPE system identifier';
} elseif ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Switch to the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-end-of-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* EOF
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Reconsume the EOF
character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append the current input character to the current
DOCTYPE token's system identifier. Stay in the DOCTYPE
system identifier (double-quoted) state. */
$this->token['system'] .= $char;
}
break;
case 'DOCTYPE system identifier (single-quoted)':
/* Consume the next input character: */
$char = $this->stream->char();
if ($char === "'") {
/* U+0027 APOSTROPHE (')
Switch to the after DOCTYPE system identifier state. */
$state = 'after DOCTYPE system identifier';
} elseif ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Switch to the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-end-of-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* EOF
Parse error. Set the DOCTYPE token's force-quirks flag
to on. Emit that DOCTYPE token. Reconsume the EOF
character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Append the current input character to the current
DOCTYPE token's system identifier. Stay in the DOCTYPE
system identifier (double-quoted) state. */
$this->token['system'] .= $char;
}
break;
case 'after DOCTYPE system identifier':
/* Consume the next input character: */
$char = $this->stream->char();
if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
Stay in the after DOCTYPE system identifier state. */
} elseif ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the current DOCTYPE token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif ($char === false) {
/* Parse error. Set the DOCTYPE token's force-quirks
flag to on. Emit that DOCTYPE token. Reconsume the EOF
character in the data state. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'eof-in-doctype'
));
$this->token['force-quirks'] = true;
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Parse error. Switch to the bogus DOCTYPE state.
(This does not set the DOCTYPE token's force-quirks
flag to on.) */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'unexpected-char-in-doctype'
));
$state = 'bogus DOCTYPE';
}
break;
case 'bogus DOCTYPE':
/* Consume the next input character: */
$char = $this->stream->char();
if ($char === '>') {
/* U+003E GREATER-THAN SIGN (>)
Emit the DOCTYPE token. Switch to the data state. */
$this->emitToken($this->token);
$state = 'data';
} elseif($char === false) {
/* EOF
Emit the DOCTYPE token. Reconsume the EOF character in
the data state. */
$this->emitToken($this->token);
$this->stream->unget();
$state = 'data';
} else {
/* Anything else
Stay in the bogus DOCTYPE state. */
}
break;
// case 'cdataSection':
}
}
}
/**
* Returns a serialized representation of the tree.
*/
public function save() {
return $this->tree->save();
}
/**
* @return HTML5_TreeBuilder The tree
*/
public function getTree() {
return $this->tree;
}
/**
* Returns the input stream.
*/
public function stream() {
return $this->stream;
}
private function consumeCharacterReference($allowed = false, $inattr = false) {
// This goes quite far against spec, and is far closer to the Python
// impl., mainly because we don't do the large unconsuming the spec
// requires.
// All consumed characters.
$chars = $this->stream->char();
/* This section defines how to consume a character
reference. This definition is used when parsing character
references in text and in attributes.
The behavior depends on the identity of the next character
(the one immediately after the U+0026 AMPERSAND character): */
if (
$chars[0] === "\x09" ||
$chars[0] === "\x0A" ||
$chars[0] === "\x0C" ||
$chars[0] === "\x20" ||
$chars[0] === '<' ||
$chars[0] === '&' ||
$chars === false ||
$chars[0] === $allowed
) {
/* U+0009 CHARACTER TABULATION
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+0020 SPACE
U+003C LESS-THAN SIGN
U+0026 AMPERSAND
EOF
The additional allowed character, if there is one
Not a character reference. No characters are consumed,
and nothing is returned. (This is not an error, either.) */
// We already consumed, so unconsume.
$this->stream->unget();
return '&';
} elseif ($chars[0] === '#') {
/* Consume the U+0023 NUMBER SIGN. */
// Um, yeah, we already did that.
/* The behavior further depends on the character after
the U+0023 NUMBER SIGN: */
$chars .= $this->stream->char();
if (isset($chars[1]) && ($chars[1] === 'x' || $chars[1] === 'X')) {
/* U+0078 LATIN SMALL LETTER X
U+0058 LATIN CAPITAL LETTER X */
/* Consume the X. */
// Um, yeah, we already did that.
/* Follow the steps below, but using the range of
characters U+0030 DIGIT ZERO through to U+0039 DIGIT
NINE, U+0061 LATIN SMALL LETTER A through to U+0066
LATIN SMALL LETTER F, and U+0041 LATIN CAPITAL LETTER
A, through to U+0046 LATIN CAPITAL LETTER F (in other
words, 0123456789, ABCDEF, abcdef). */
$char_class = self::HEX;
/* When it comes to interpreting the
number, interpret it as a hexadecimal number. */
$hex = true;
} else {
/* Anything else */
// Unconsume because we shouldn't have consumed this.
$chars = $chars[0];
$this->stream->unget();
/* Follow the steps below, but using the range of
characters U+0030 DIGIT ZERO through to U+0039 DIGIT
NINE (i.e. just 0123456789). */
$char_class = self::DIGIT;
/* When it comes to interpreting the number,
interpret it as a decimal number. */
$hex = false;
}
/* Consume as many characters as match the range of characters given above. */
$consumed = $this->stream->charsWhile($char_class);
if ($consumed === '' || $consumed === false) {
/* If no characters match the range, then don't consume
any characters (and unconsume the U+0023 NUMBER SIGN
character and, if appropriate, the X character). This
is a parse error; nothing is returned. */
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-numeric-entity'
));
return '&' . $chars;
} else {
/* Otherwise, if the next character is a U+003B SEMICOLON,
consume that too. If it isn't, there is a parse error. */
if ($this->stream->char() !== ';') {
$this->stream->unget();
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'numeric-entity-without-semicolon'
));
}
/* If one or more characters match the range, then take
them all and interpret the string of characters as a number
(either hexadecimal or decimal as appropriate). */
$codepoint = $hex ? hexdec($consumed) : (int) $consumed;
/* If that number is one of the numbers in the first column
of the following table, then this is a parse error. Find the
row with that number in the first column, and return a
character token for the Unicode character given in the
second column of that row. */
$new_codepoint = HTML5_Data::getRealCodepoint($codepoint);
if ($new_codepoint) {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'illegal-windows-1252-entity'
));
return HTML5_Data::utf8chr($new_codepoint);
} else {
/* Otherwise, if the number is greater than 0x10FFFF, then
* this is a parse error. Return a U+FFFD REPLACEMENT
* CHARACTER. */
if ($codepoint > 0x10FFFF) {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'overlong-character-entity' // XXX probably not correct
));
return "\xEF\xBF\xBD";
}
/* Otherwise, return a character token for the Unicode
* character whose code point is that number. If the
* number is in the range 0x0001 to 0x0008, 0x000E to
* 0x001F, 0x007F to 0x009F, 0xD800 to 0xDFFF, 0xFDD0 to
* 0xFDEF, or is one of 0x000B, 0xFFFE, 0xFFFF, 0x1FFFE,
* 0x1FFFF, 0x2FFFE, 0x2FFFF, 0x3FFFE, 0x3FFFF, 0x4FFFE,
* 0x4FFFF, 0x5FFFE, 0x5FFFF, 0x6FFFE, 0x6FFFF, 0x7FFFE,
* 0x7FFFF, 0x8FFFE, 0x8FFFF, 0x9FFFE, 0x9FFFF, 0xAFFFE,
* 0xAFFFF, 0xBFFFE, 0xBFFFF, 0xCFFFE, 0xCFFFF, 0xDFFFE,
* 0xDFFFF, 0xEFFFE, 0xEFFFF, 0xFFFFE, 0xFFFFF, 0x10FFFE,
* or 0x10FFFF, then this is a parse error. */
// && has higher precedence than ||
if (
$codepoint >= 0x0000 && $codepoint <= 0x0008 ||
$codepoint === 0x000B ||
$codepoint >= 0x000E && $codepoint <= 0x001F ||
$codepoint >= 0x007F && $codepoint <= 0x009F ||
$codepoint >= 0xD800 && $codepoint <= 0xDFFF ||
$codepoint >= 0xFDD0 && $codepoint <= 0xFDEF ||
($codepoint & 0xFFFE) === 0xFFFE ||
$codepoint == 0x10FFFF || $codepoint == 0x10FFFE
) {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'illegal-codepoint-for-numeric-entity'
));
}
return HTML5_Data::utf8chr($codepoint);
}
}
} else {
/* Anything else */
/* Consume the maximum number of characters possible,
with the consumed characters matching one of the
identifiers in the first column of the named character
references table (in a case-sensitive manner). */
// What we actually do here is consume as much as we can while it
// matches the start of one of the identifiers in the first column.
$refs = HTML5_Data::getNamedCharacterReferences();
// Get the longest string which is the start of an identifier
// ($chars) as well as the longest identifier which matches ($id)
// and its codepoint ($codepoint).
$codepoint = false;
$char = $chars;
while ($char !== false && isset($refs[$char])) {
$refs = $refs[$char];
if (isset($refs['codepoint'])) {
$id = $chars;
$codepoint = $refs['codepoint'];
}
$chars .= $char = $this->stream->char();
}
// Unconsume the one character we just took which caused the while
// statement to fail. This could be anything and could cause state
// changes (as if it matches the while loop it must be
// alphanumeric so we can just concat it to whatever we get later).
$this->stream->unget();
if ($char !== false) {
$chars = substr($chars, 0, -1);
}
/* If no match can be made, then this is a parse error.
No characters are consumed, and nothing is returned. */
if (!$codepoint) {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'expected-named-entity'
));
return '&' . $chars;
}
/* If the last character matched is not a U+003B SEMICOLON
(;), there is a parse error. */
$semicolon = true;
if (substr($id, -1) !== ';') {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'named-entity-without-semicolon'
));
$semicolon = false;
}
/* If the character reference is being consumed as part of
an attribute, and the last character matched is not a
U+003B SEMICOLON (;), and the next character is in the
range U+0030 DIGIT ZERO to U+0039 DIGIT NINE, U+0041
LATIN CAPITAL LETTER A to U+005A LATIN CAPITAL LETTER Z,
or U+0061 LATIN SMALL LETTER A to U+007A LATIN SMALL LETTER Z,
then, for historical reasons, all the characters that were
matched after the U+0026 AMPERSAND (&) must be unconsumed,
and nothing is returned. */
if ($inattr && !$semicolon) {
// The next character is either the next character in $chars or in the stream.
if (strlen($chars) > strlen($id)) {
$next = substr($chars, strlen($id), 1);
} else {
$next = $this->stream->char();
$this->stream->unget();
}
if (
'0' <= $next && $next <= '9' ||
'A' <= $next && $next <= 'Z' ||
'a' <= $next && $next <= 'z'
) {
return '&' . $chars;
}
}
/* Otherwise, return a character token for the character
corresponding to the character reference name (as given
by the second column of the named character references table). */
return HTML5_Data::utf8chr($codepoint) . substr($chars, strlen($id));
}
}
private function characterReferenceInAttributeValue($allowed = false) {
/* Attempt to consume a character reference. */
$entity = $this->consumeCharacterReference($allowed, true);
/* If nothing is returned, append a U+0026 AMPERSAND
character to the current attribute's value.
Otherwise, append the returned character token to the
current attribute's value. */
$char = (!$entity)
? '&'
: $entity;
$last = count($this->token['attr']) - 1;
$this->token['attr'][$last]['value'] .= $char;
/* Finally, switch back to the attribute value state that you
were in when were switched into this state. */
}
/**
* Emits a token, passing it on to the tree builder.
*/
protected function emitToken($token, $checkStream = true, $dry = false) {
if ($checkStream) {
// Emit errors from input stream.
while ($this->stream->errors) {
$this->emitToken(array_shift($this->stream->errors), false);
}
}
if($token['type'] === self::ENDTAG && !empty($token['attr'])) {
for ($i = 0; $i < count($token['attr']); $i++) {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'attributes-in-end-tag'
));
}
}
if($token['type'] === self::ENDTAG && !empty($token['self-closing'])) {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'self-closing-flag-on-end-tag',
));
}
if($token['type'] === self::STARTTAG) {
// This could be changed to actually pass the tree-builder a hash
$hash = array();
foreach ($token['attr'] as $keypair) {
if (isset($hash[$keypair['name']])) {
$this->emitToken(array(
'type' => self::PARSEERROR,
'data' => 'duplicate-attribute',
));
} else {
$hash[$keypair['name']] = $keypair['value'];
}
}
}
if(!$dry) {
// the current structure of attributes is not a terribly good one
$this->tree->emitToken($token);
}
if(!$dry && is_int($this->tree->content_model)) {
$this->content_model = $this->tree->content_model;
$this->tree->content_model = null;
} elseif($token['type'] === self::ENDTAG) {
$this->content_model = self::PCDATA;
}
}
}
|