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 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092
|
/* lexer.c -- Lexer for html parser
(c) 1998-2008 (W3C) MIT, ERCIM, Keio University
See tidy.h for the copyright notice.
*/
/*
Given a file stream fp it returns a sequence of tokens.
GetToken(fp) gets the next token
UngetToken(fp) provides one level undo
The tags include an attribute list:
- linked list of attribute/value nodes
- each node has 2 NULL-terminated strings.
- entities are replaced in attribute values
white space is compacted if not in preformatted mode
If not in preformatted mode then leading white space
is discarded and subsequent white space sequences
compacted to single space characters.
If XmlTags is no then Tag names are folded to upper
case and attribute names to lower case.
Not yet done:
- Doctype subset and marked sections
*/
#include "tidy-int.h"
#include "lexer.h"
#include "parser.h"
#include "entities.h"
#include "streamio.h"
#include "message.h"
#include "tmbstr.h"
#include "clean.h"
#include "utf8.h"
#include "streamio.h"
#ifdef _MSC_VER
#include "sprtf.h"
#endif
#ifndef SPRTF
#define SPRTF printf
#endif
#if !defined(NDEBUG) && defined(_MSC_VER)
static Bool show_attrs = yes;
#define MX_TXT 5
static char buffer[MX_TXT+8]; /* NOTE extra for '...'\0 tail */
static tmbstr get_text_string(Lexer* lexer, Node *node)
{
uint len = node->end - node->start;
tmbstr cp = lexer->lexbuf + node->start;
tmbstr end = lexer->lexbuf + node->end;
uint i = 0;
buffer[0] = (char)0;
while (cp < end ) {
buffer[i++] = *cp;
cp++;
if (i >= MX_TXT)
break;
}
if (i < len) {
buffer[i++] = '.';
if (i < len) {
buffer[i++] = '.';
if (i < len) {
buffer[i++] = '.';
}
}
}
buffer[i] = 0;
return buffer;
}
static void Show_Node( TidyDocImpl* doc, const char *msg, Node *node )
{
Lexer* lexer = doc->lexer;
Bool lex = ((msg[0] == 'l')&&(msg[1] == 'e')) ? yes : no;
int line = ( doc->lexer ? doc->lexer->lines : 0 );
int col = ( doc->lexer ? doc->lexer->columns : 0 );
SPRTF("R=%d C=%d: ", line, col );
if (lexer && lexer->token && (lexer->token->type == TextNode)) {
if (show_attrs) {
uint len = node->end - node->start;
tmbstr cp = get_text_string( lexer, node );
SPRTF("Returning %s TextNode [%s]%u %s\n", msg, cp, len,
lex ? "lexer" : "stream");
} else {
SPRTF("Returning %s TextNode %p... %s\n", msg, node,
lex ? "lexer" : "stream");
}
} else {
if (show_attrs) {
AttVal* av;
tmbstr name = node->element ? node->element : "blank";
SPRTF("Returning %s node <%s", msg, name);
for (av = node->attributes; av; av = av->next) {
name = av->attribute;
if (name) {
SPRTF(" %s",name);
if (av->value) {
SPRTF("=\"%s\"", av->value);
}
}
}
SPRTF("> %s\n", lex ? "lexer" : "stream");
} else {
SPRTF("Returning %s node %p <%s>... %s\n", msg, node,
node->element ? node->element : "blank",
lex ? "lexer" : "stream");
}
}
}
#define GTDBG(a,b,c) Show_Node(a,b,c)
#else
#define GTDBG(a,b,c)
#endif
/* Forward references
*/
/* swallows closing '>' */
static AttVal *ParseAttrs( TidyDocImpl* doc, Bool *isempty );
static tmbstr ParseAttribute( TidyDocImpl* doc, Bool* isempty,
Node **asp, Node **php );
static tmbstr ParseValue( TidyDocImpl* doc, ctmbstr name, Bool foldCase,
Bool *isempty, int *pdelim );
static Node *ParseDocTypeDecl(TidyDocImpl* doc);
static void AddAttrToList( AttVal** list, AttVal* av );
/* used to classify characters for lexical purposes */
#define MAP(c) ((unsigned)c < 128 ? lexmap[(unsigned)c] : 0)
static uint lexmap[128];
#define IsValidXMLAttrName(name) TY_(IsValidXMLID)(name)
#define IsValidXMLElemName(name) TY_(IsValidXMLID)(name)
static struct _doctypes
{
uint score;
uint vers;
ctmbstr name;
ctmbstr fpi;
ctmbstr si;
} const W3C_Doctypes[] =
{
{ 2, HT20, "HTML 2.0", "-//IETF//DTD HTML 2.0//EN", NULL, },
{ 2, HT20, "HTML 2.0", "-//IETF//DTD HTML//EN", NULL, },
{ 2, HT20, "HTML 2.0", "-//W3C//DTD HTML 2.0//EN", NULL, },
{ 1, HT32, "HTML 3.2", "-//W3C//DTD HTML 3.2//EN", NULL, },
{ 1, HT32, "HTML 3.2", "-//W3C//DTD HTML 3.2 Final//EN", NULL, },
{ 1, HT32, "HTML 3.2", "-//W3C//DTD HTML 3.2 Draft//EN", NULL, },
{ 6, H40S, "HTML 4.0 Strict", "-//W3C//DTD HTML 4.0//EN", "http://www.w3.org/TR/REC-html40/strict.dtd" },
{ 8, H40T, "HTML 4.0 Transitional", "-//W3C//DTD HTML 4.0 Transitional//EN", "http://www.w3.org/TR/REC-html40/loose.dtd" },
{ 7, H40F, "HTML 4.0 Frameset", "-//W3C//DTD HTML 4.0 Frameset//EN", "http://www.w3.org/TR/REC-html40/frameset.dtd" },
{ 3, H41S, "HTML 4.01 Strict", "-//W3C//DTD HTML 4.01//EN", "http://www.w3.org/TR/html4/strict.dtd" },
{ 5, H41T, "HTML 4.01 Transitional", "-//W3C//DTD HTML 4.01 Transitional//EN", "http://www.w3.org/TR/html4/loose.dtd" },
{ 4, H41F, "HTML 4.01 Frameset", "-//W3C//DTD HTML 4.01 Frameset//EN", "http://www.w3.org/TR/html4/frameset.dtd" },
{ 9, X10S, "XHTML 1.0 Strict", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" },
{ 11, X10T, "XHTML 1.0 Transitional", "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" },
{ 10, X10F, "XHTML 1.0 Frameset", "-//W3C//DTD XHTML 1.0 Frameset//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd" },
{ 12, XH11, "XHTML 1.1", "-//W3C//DTD XHTML 1.1//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" },
{ 13, XB10, "XHTML Basic 1.0", "-//W3C//DTD XHTML Basic 1.0//EN", "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd" },
{ 20, HT50, "HTML5", NULL, NULL },
{ 21, XH50, "XHTML5", NULL, NULL },
/* reminder to add XHTML Print 1.0 support, see http://www.w3.org/TR/xhtml-print */
#if 0
{ 14, XP10, "XHTML Print 1.0", "-//W3C//DTD XHTML-Print 1.0//EN", "http://www.w3.org/MarkUp/DTD/xhtml-print10.dtd" },
{ 14, XP10, "XHTML Print 1.0", "-//PWG//DTD XHTML-Print 1.0//EN", "http://www.xhtml-print.org/xhtml-print/xhtml-print10.dtd" },
#endif
/* final entry */
{ 0, 0, NULL, NULL, NULL }
};
int TY_(HTMLVersion)(TidyDocImpl* doc)
{
uint i;
uint j = 0;
uint score = 0;
uint vers = doc->lexer->versions;
uint dtver = doc->lexer->doctype;
TidyDoctypeModes dtmode = (TidyDoctypeModes)cfg(doc, TidyDoctypeMode);
Bool xhtml = (cfgBool(doc, TidyXmlOut) || doc->lexer->isvoyager) &&
!cfgBool(doc, TidyHtmlOut);
Bool html4 = dtmode == TidyDoctypeStrict || dtmode == TidyDoctypeLoose || VERS_FROM40 & dtver;
if (xhtml && dtver == VERS_UNKNOWN) return XH50;
if (dtver == VERS_UNKNOWN) return HT50;
/* Issue #167 - if NOT XHTML, and doctype is default VERS_HTML5, then return HT50 */
if (!xhtml && (dtver == VERS_HTML5)) return HT50;
for (i = 0; W3C_Doctypes[i].name; ++i)
{
if ((xhtml && !(VERS_XHTML & W3C_Doctypes[i].vers)) ||
(html4 && !(VERS_FROM40 & W3C_Doctypes[i].vers)))
continue;
if (vers & W3C_Doctypes[i].vers &&
(W3C_Doctypes[i].score < score || !score))
{
score = W3C_Doctypes[i].score;
j = i;
}
}
if (score)
return W3C_Doctypes[j].vers;
return VERS_UNKNOWN;
}
static ctmbstr GetFPIFromVers(uint vers)
{
uint i;
for (i = 0; W3C_Doctypes[i].name; ++i)
if (W3C_Doctypes[i].vers == vers)
return W3C_Doctypes[i].fpi;
return NULL;
}
static ctmbstr GetSIFromVers(uint vers)
{
uint i;
for (i = 0; W3C_Doctypes[i].name; ++i)
if (W3C_Doctypes[i].vers == vers)
return W3C_Doctypes[i].si;
return NULL;
}
static ctmbstr GetNameFromVers(uint vers)
{
uint i;
for (i = 0; W3C_Doctypes[i].name; ++i)
if (W3C_Doctypes[i].vers == vers)
return W3C_Doctypes[i].name;
return NULL;
}
static uint GetVersFromFPI(ctmbstr fpi)
{
uint i;
for (i = 0; W3C_Doctypes[i].name; ++i)
if (W3C_Doctypes[i].fpi != NULL && TY_(tmbstrcasecmp)(W3C_Doctypes[i].fpi, fpi) == 0)
return W3C_Doctypes[i].vers;
return 0;
}
/* everything is allowed in proprietary version of HTML */
/* this is handled here rather than in the tag/attr dicts */
void TY_(ConstrainVersion)(TidyDocImpl* doc, uint vers)
{
doc->lexer->versions &= (vers | VERS_PROPRIETARY);
}
Bool TY_(IsWhite)(uint c)
{
uint map = MAP(c);
return (map & white)!=0;
}
Bool TY_(IsNewline)(uint c)
{
uint map = MAP(c);
return (map & newline)!=0;
}
Bool TY_(IsDigit)(uint c)
{
uint map;
map = MAP(c);
return (map & digit)!=0;
}
static Bool IsDigitHex(uint c)
{
uint map;
map = MAP(c);
return (map & digithex)!=0;
}
Bool TY_(IsLetter)(uint c)
{
uint map;
map = MAP(c);
return (map & letter)!=0;
}
Bool TY_(IsHTMLSpace)(uint c)
{
return c == 0x020 || c == 0x009 || c == 0x00a || c == 0x00c || c == 0x00d;
}
Bool TY_(IsNamechar)(uint c)
{
uint map = MAP(c);
return (map & namechar)!=0;
}
Bool TY_(IsXMLLetter)(uint c)
{
return ((c >= 0x41 && c <= 0x5a) ||
(c >= 0x61 && c <= 0x7a) ||
(c >= 0xc0 && c <= 0xd6) ||
(c >= 0xd8 && c <= 0xf6) ||
(c >= 0xf8 && c <= 0xff) ||
(c >= 0x100 && c <= 0x131) ||
(c >= 0x134 && c <= 0x13e) ||
(c >= 0x141 && c <= 0x148) ||
(c >= 0x14a && c <= 0x17e) ||
(c >= 0x180 && c <= 0x1c3) ||
(c >= 0x1cd && c <= 0x1f0) ||
(c >= 0x1f4 && c <= 0x1f5) ||
(c >= 0x1fa && c <= 0x217) ||
(c >= 0x250 && c <= 0x2a8) ||
(c >= 0x2bb && c <= 0x2c1) ||
c == 0x386 ||
(c >= 0x388 && c <= 0x38a) ||
c == 0x38c ||
(c >= 0x38e && c <= 0x3a1) ||
(c >= 0x3a3 && c <= 0x3ce) ||
(c >= 0x3d0 && c <= 0x3d6) ||
c == 0x3da ||
c == 0x3dc ||
c == 0x3de ||
c == 0x3e0 ||
(c >= 0x3e2 && c <= 0x3f3) ||
(c >= 0x401 && c <= 0x40c) ||
(c >= 0x40e && c <= 0x44f) ||
(c >= 0x451 && c <= 0x45c) ||
(c >= 0x45e && c <= 0x481) ||
(c >= 0x490 && c <= 0x4c4) ||
(c >= 0x4c7 && c <= 0x4c8) ||
(c >= 0x4cb && c <= 0x4cc) ||
(c >= 0x4d0 && c <= 0x4eb) ||
(c >= 0x4ee && c <= 0x4f5) ||
(c >= 0x4f8 && c <= 0x4f9) ||
(c >= 0x531 && c <= 0x556) ||
c == 0x559 ||
(c >= 0x561 && c <= 0x586) ||
(c >= 0x5d0 && c <= 0x5ea) ||
(c >= 0x5f0 && c <= 0x5f2) ||
(c >= 0x621 && c <= 0x63a) ||
(c >= 0x641 && c <= 0x64a) ||
(c >= 0x671 && c <= 0x6b7) ||
(c >= 0x6ba && c <= 0x6be) ||
(c >= 0x6c0 && c <= 0x6ce) ||
(c >= 0x6d0 && c <= 0x6d3) ||
c == 0x6d5 ||
(c >= 0x6e5 && c <= 0x6e6) ||
(c >= 0x905 && c <= 0x939) ||
c == 0x93d ||
(c >= 0x958 && c <= 0x961) ||
(c >= 0x985 && c <= 0x98c) ||
(c >= 0x98f && c <= 0x990) ||
(c >= 0x993 && c <= 0x9a8) ||
(c >= 0x9aa && c <= 0x9b0) ||
c == 0x9b2 ||
(c >= 0x9b6 && c <= 0x9b9) ||
(c >= 0x9dc && c <= 0x9dd) ||
(c >= 0x9df && c <= 0x9e1) ||
(c >= 0x9f0 && c <= 0x9f1) ||
(c >= 0xa05 && c <= 0xa0a) ||
(c >= 0xa0f && c <= 0xa10) ||
(c >= 0xa13 && c <= 0xa28) ||
(c >= 0xa2a && c <= 0xa30) ||
(c >= 0xa32 && c <= 0xa33) ||
(c >= 0xa35 && c <= 0xa36) ||
(c >= 0xa38 && c <= 0xa39) ||
(c >= 0xa59 && c <= 0xa5c) ||
c == 0xa5e ||
(c >= 0xa72 && c <= 0xa74) ||
(c >= 0xa85 && c <= 0xa8b) ||
c == 0xa8d ||
(c >= 0xa8f && c <= 0xa91) ||
(c >= 0xa93 && c <= 0xaa8) ||
(c >= 0xaaa && c <= 0xab0) ||
(c >= 0xab2 && c <= 0xab3) ||
(c >= 0xab5 && c <= 0xab9) ||
c == 0xabd ||
c == 0xae0 ||
(c >= 0xb05 && c <= 0xb0c) ||
(c >= 0xb0f && c <= 0xb10) ||
(c >= 0xb13 && c <= 0xb28) ||
(c >= 0xb2a && c <= 0xb30) ||
(c >= 0xb32 && c <= 0xb33) ||
(c >= 0xb36 && c <= 0xb39) ||
c == 0xb3d ||
(c >= 0xb5c && c <= 0xb5d) ||
(c >= 0xb5f && c <= 0xb61) ||
(c >= 0xb85 && c <= 0xb8a) ||
(c >= 0xb8e && c <= 0xb90) ||
(c >= 0xb92 && c <= 0xb95) ||
(c >= 0xb99 && c <= 0xb9a) ||
c == 0xb9c ||
(c >= 0xb9e && c <= 0xb9f) ||
(c >= 0xba3 && c <= 0xba4) ||
(c >= 0xba8 && c <= 0xbaa) ||
(c >= 0xbae && c <= 0xbb5) ||
(c >= 0xbb7 && c <= 0xbb9) ||
(c >= 0xc05 && c <= 0xc0c) ||
(c >= 0xc0e && c <= 0xc10) ||
(c >= 0xc12 && c <= 0xc28) ||
(c >= 0xc2a && c <= 0xc33) ||
(c >= 0xc35 && c <= 0xc39) ||
(c >= 0xc60 && c <= 0xc61) ||
(c >= 0xc85 && c <= 0xc8c) ||
(c >= 0xc8e && c <= 0xc90) ||
(c >= 0xc92 && c <= 0xca8) ||
(c >= 0xcaa && c <= 0xcb3) ||
(c >= 0xcb5 && c <= 0xcb9) ||
c == 0xcde ||
(c >= 0xce0 && c <= 0xce1) ||
(c >= 0xd05 && c <= 0xd0c) ||
(c >= 0xd0e && c <= 0xd10) ||
(c >= 0xd12 && c <= 0xd28) ||
(c >= 0xd2a && c <= 0xd39) ||
(c >= 0xd60 && c <= 0xd61) ||
(c >= 0xe01 && c <= 0xe2e) ||
c == 0xe30 ||
(c >= 0xe32 && c <= 0xe33) ||
(c >= 0xe40 && c <= 0xe45) ||
(c >= 0xe81 && c <= 0xe82) ||
c == 0xe84 ||
(c >= 0xe87 && c <= 0xe88) ||
c == 0xe8a ||
c == 0xe8d ||
(c >= 0xe94 && c <= 0xe97) ||
(c >= 0xe99 && c <= 0xe9f) ||
(c >= 0xea1 && c <= 0xea3) ||
c == 0xea5 ||
c == 0xea7 ||
(c >= 0xeaa && c <= 0xeab) ||
(c >= 0xead && c <= 0xeae) ||
c == 0xeb0 ||
(c >= 0xeb2 && c <= 0xeb3) ||
c == 0xebd ||
(c >= 0xec0 && c <= 0xec4) ||
(c >= 0xf40 && c <= 0xf47) ||
(c >= 0xf49 && c <= 0xf69) ||
(c >= 0x10a0 && c <= 0x10c5) ||
(c >= 0x10d0 && c <= 0x10f6) ||
c == 0x1100 ||
(c >= 0x1102 && c <= 0x1103) ||
(c >= 0x1105 && c <= 0x1107) ||
c == 0x1109 ||
(c >= 0x110b && c <= 0x110c) ||
(c >= 0x110e && c <= 0x1112) ||
c == 0x113c ||
c == 0x113e ||
c == 0x1140 ||
c == 0x114c ||
c == 0x114e ||
c == 0x1150 ||
(c >= 0x1154 && c <= 0x1155) ||
c == 0x1159 ||
(c >= 0x115f && c <= 0x1161) ||
c == 0x1163 ||
c == 0x1165 ||
c == 0x1167 ||
c == 0x1169 ||
(c >= 0x116d && c <= 0x116e) ||
(c >= 0x1172 && c <= 0x1173) ||
c == 0x1175 ||
c == 0x119e ||
c == 0x11a8 ||
c == 0x11ab ||
(c >= 0x11ae && c <= 0x11af) ||
(c >= 0x11b7 && c <= 0x11b8) ||
c == 0x11ba ||
(c >= 0x11bc && c <= 0x11c2) ||
c == 0x11eb ||
c == 0x11f0 ||
c == 0x11f9 ||
(c >= 0x1e00 && c <= 0x1e9b) ||
(c >= 0x1ea0 && c <= 0x1ef9) ||
(c >= 0x1f00 && c <= 0x1f15) ||
(c >= 0x1f18 && c <= 0x1f1d) ||
(c >= 0x1f20 && c <= 0x1f45) ||
(c >= 0x1f48 && c <= 0x1f4d) ||
(c >= 0x1f50 && c <= 0x1f57) ||
c == 0x1f59 ||
c == 0x1f5b ||
c == 0x1f5d ||
(c >= 0x1f5f && c <= 0x1f7d) ||
(c >= 0x1f80 && c <= 0x1fb4) ||
(c >= 0x1fb6 && c <= 0x1fbc) ||
c == 0x1fbe ||
(c >= 0x1fc2 && c <= 0x1fc4) ||
(c >= 0x1fc6 && c <= 0x1fcc) ||
(c >= 0x1fd0 && c <= 0x1fd3) ||
(c >= 0x1fd6 && c <= 0x1fdb) ||
(c >= 0x1fe0 && c <= 0x1fec) ||
(c >= 0x1ff2 && c <= 0x1ff4) ||
(c >= 0x1ff6 && c <= 0x1ffc) ||
c == 0x2126 ||
(c >= 0x212a && c <= 0x212b) ||
c == 0x212e ||
(c >= 0x2180 && c <= 0x2182) ||
(c >= 0x3041 && c <= 0x3094) ||
(c >= 0x30a1 && c <= 0x30fa) ||
(c >= 0x3105 && c <= 0x312c) ||
(c >= 0xac00 && c <= 0xd7a3) ||
(c >= 0x4e00 && c <= 0x9fa5) ||
c == 0x3007 ||
(c >= 0x3021 && c <= 0x3029) ||
(c >= 0x4e00 && c <= 0x9fa5) ||
c == 0x3007 ||
(c >= 0x3021 && c <= 0x3029));
}
Bool TY_(IsXMLNamechar)(uint c)
{
return (TY_(IsXMLLetter)(c) ||
c == '.' || c == '_' ||
c == ':' || c == '-' ||
(c >= 0x300 && c <= 0x345) ||
(c >= 0x360 && c <= 0x361) ||
(c >= 0x483 && c <= 0x486) ||
(c >= 0x591 && c <= 0x5a1) ||
(c >= 0x5a3 && c <= 0x5b9) ||
(c >= 0x5bb && c <= 0x5bd) ||
c == 0x5bf ||
(c >= 0x5c1 && c <= 0x5c2) ||
c == 0x5c4 ||
(c >= 0x64b && c <= 0x652) ||
c == 0x670 ||
(c >= 0x6d6 && c <= 0x6dc) ||
(c >= 0x6dd && c <= 0x6df) ||
(c >= 0x6e0 && c <= 0x6e4) ||
(c >= 0x6e7 && c <= 0x6e8) ||
(c >= 0x6ea && c <= 0x6ed) ||
(c >= 0x901 && c <= 0x903) ||
c == 0x93c ||
(c >= 0x93e && c <= 0x94c) ||
c == 0x94d ||
(c >= 0x951 && c <= 0x954) ||
(c >= 0x962 && c <= 0x963) ||
(c >= 0x981 && c <= 0x983) ||
c == 0x9bc ||
c == 0x9be ||
c == 0x9bf ||
(c >= 0x9c0 && c <= 0x9c4) ||
(c >= 0x9c7 && c <= 0x9c8) ||
(c >= 0x9cb && c <= 0x9cd) ||
c == 0x9d7 ||
(c >= 0x9e2 && c <= 0x9e3) ||
c == 0xa02 ||
c == 0xa3c ||
c == 0xa3e ||
c == 0xa3f ||
(c >= 0xa40 && c <= 0xa42) ||
(c >= 0xa47 && c <= 0xa48) ||
(c >= 0xa4b && c <= 0xa4d) ||
(c >= 0xa70 && c <= 0xa71) ||
(c >= 0xa81 && c <= 0xa83) ||
c == 0xabc ||
(c >= 0xabe && c <= 0xac5) ||
(c >= 0xac7 && c <= 0xac9) ||
(c >= 0xacb && c <= 0xacd) ||
(c >= 0xb01 && c <= 0xb03) ||
c == 0xb3c ||
(c >= 0xb3e && c <= 0xb43) ||
(c >= 0xb47 && c <= 0xb48) ||
(c >= 0xb4b && c <= 0xb4d) ||
(c >= 0xb56 && c <= 0xb57) ||
(c >= 0xb82 && c <= 0xb83) ||
(c >= 0xbbe && c <= 0xbc2) ||
(c >= 0xbc6 && c <= 0xbc8) ||
(c >= 0xbca && c <= 0xbcd) ||
c == 0xbd7 ||
(c >= 0xc01 && c <= 0xc03) ||
(c >= 0xc3e && c <= 0xc44) ||
(c >= 0xc46 && c <= 0xc48) ||
(c >= 0xc4a && c <= 0xc4d) ||
(c >= 0xc55 && c <= 0xc56) ||
(c >= 0xc82 && c <= 0xc83) ||
(c >= 0xcbe && c <= 0xcc4) ||
(c >= 0xcc6 && c <= 0xcc8) ||
(c >= 0xcca && c <= 0xccd) ||
(c >= 0xcd5 && c <= 0xcd6) ||
(c >= 0xd02 && c <= 0xd03) ||
(c >= 0xd3e && c <= 0xd43) ||
(c >= 0xd46 && c <= 0xd48) ||
(c >= 0xd4a && c <= 0xd4d) ||
c == 0xd57 ||
c == 0xe31 ||
(c >= 0xe34 && c <= 0xe3a) ||
(c >= 0xe47 && c <= 0xe4e) ||
c == 0xeb1 ||
(c >= 0xeb4 && c <= 0xeb9) ||
(c >= 0xebb && c <= 0xebc) ||
(c >= 0xec8 && c <= 0xecd) ||
(c >= 0xf18 && c <= 0xf19) ||
c == 0xf35 ||
c == 0xf37 ||
c == 0xf39 ||
c == 0xf3e ||
c == 0xf3f ||
(c >= 0xf71 && c <= 0xf84) ||
(c >= 0xf86 && c <= 0xf8b) ||
(c >= 0xf90 && c <= 0xf95) ||
c == 0xf97 ||
(c >= 0xf99 && c <= 0xfad) ||
(c >= 0xfb1 && c <= 0xfb7) ||
c == 0xfb9 ||
(c >= 0x20d0 && c <= 0x20dc) ||
c == 0x20e1 ||
(c >= 0x302a && c <= 0x302f) ||
c == 0x3099 ||
c == 0x309a ||
(c >= 0x30 && c <= 0x39) ||
(c >= 0x660 && c <= 0x669) ||
(c >= 0x6f0 && c <= 0x6f9) ||
(c >= 0x966 && c <= 0x96f) ||
(c >= 0x9e6 && c <= 0x9ef) ||
(c >= 0xa66 && c <= 0xa6f) ||
(c >= 0xae6 && c <= 0xaef) ||
(c >= 0xb66 && c <= 0xb6f) ||
(c >= 0xbe7 && c <= 0xbef) ||
(c >= 0xc66 && c <= 0xc6f) ||
(c >= 0xce6 && c <= 0xcef) ||
(c >= 0xd66 && c <= 0xd6f) ||
(c >= 0xe50 && c <= 0xe59) ||
(c >= 0xed0 && c <= 0xed9) ||
(c >= 0xf20 && c <= 0xf29) ||
c == 0xb7 ||
c == 0x2d0 ||
c == 0x2d1 ||
c == 0x387 ||
c == 0x640 ||
c == 0xe46 ||
c == 0xec6 ||
c == 0x3005 ||
(c >= 0x3031 && c <= 0x3035) ||
(c >= 0x309d && c <= 0x309e) ||
(c >= 0x30fc && c <= 0x30fe));
}
#if 0
Bool IsLower(uint c)
{
uint map = MAP(c);
return (map & lowercase)!=0;
}
#endif
Bool TY_(IsUpper)(uint c)
{
uint map = MAP(c);
return (map & uppercase)!=0;
}
uint TY_(ToLower)(uint c)
{
uint map = MAP(c);
if (map & uppercase)
c += 'a' - 'A';
return c;
}
uint TY_(ToUpper)(uint c)
{
uint map = MAP(c);
if (map & lowercase)
c += (uint) ('A' - 'a' );
return c;
}
#if 0
char FoldCase( TidyDocImpl* doc, tmbchar c, Bool tocaps )
{
if ( !cfgBool(doc, TidyXmlTags) )
{
if ( tocaps )
{
c = (tmbchar) ToUpper(c);
}
else /* force to lower case */
{
c = (tmbchar) ToLower(c);
}
}
return c;
}
#endif
/*
return last character in string
this is useful when trailing quotemark
is missing on an attribute
*/
static tmbchar LastChar( tmbstr str )
{
if ( str && *str )
{
int n = TY_(tmbstrlen)(str);
return str[n-1];
}
return 0;
}
/*
node->type is one of these:
#define TextNode 1
#define StartTag 2
#define EndTag 3
#define StartEndTag 4
*/
Lexer* TY_(NewLexer)( TidyDocImpl* doc )
{
Lexer* lexer = (Lexer*) TidyDocAlloc( doc, sizeof(Lexer) );
if ( lexer != NULL )
{
TidyClearMemory( lexer, sizeof(Lexer) );
lexer->allocator = doc->allocator;
lexer->lines = 1;
lexer->columns = 1;
lexer->state = LEX_CONTENT;
lexer->versions = (VERS_ALL|VERS_PROPRIETARY);
lexer->doctype = VERS_UNKNOWN;
lexer->root = &doc->root;
}
return lexer;
}
static Bool EndOfInput( TidyDocImpl* doc )
{
assert( doc->docIn != NULL );
return ( !doc->docIn->pushed && TY_(IsEOF)(doc->docIn) );
}
void TY_(FreeLexer)( TidyDocImpl* doc )
{
Lexer *lexer = doc->lexer;
if ( lexer )
{
TY_(FreeStyles)( doc );
/* See GetToken() */
if ( lexer->pushed || lexer->itoken )
{
if (lexer->pushed)
TY_(FreeNode)( doc, lexer->itoken );
TY_(FreeNode)( doc, lexer->token );
}
while ( lexer->istacksize > 0 )
TY_(PopInline)( doc, NULL );
TidyDocFree( doc, lexer->istack );
TidyDocFree( doc, lexer->lexbuf );
TidyDocFree( doc, lexer );
doc->lexer = NULL;
}
}
/* Lexer uses bigger memory chunks than pprint as
** it must hold the entire input document. not just
** the last line or three.
*/
static void AddByte( Lexer *lexer, tmbchar ch )
{
if ( lexer->lexsize + 2 >= lexer->lexlength )
{
tmbstr buf = NULL;
uint allocAmt = lexer->lexlength;
while ( lexer->lexsize + 2 >= allocAmt )
{
if ( allocAmt == 0 )
allocAmt = 8192;
else
allocAmt *= 2;
}
buf = (tmbstr) TidyRealloc( lexer->allocator, lexer->lexbuf, allocAmt );
if ( buf )
{
TidyClearMemory( buf + lexer->lexlength,
allocAmt - lexer->lexlength );
lexer->lexbuf = buf;
lexer->lexlength = allocAmt;
}
}
lexer->lexbuf[ lexer->lexsize++ ] = ch;
lexer->lexbuf[ lexer->lexsize ] = '\0'; /* debug */
}
static void ChangeChar( Lexer *lexer, tmbchar c )
{
if ( lexer->lexsize > 0 )
{
lexer->lexbuf[ lexer->lexsize-1 ] = c;
}
}
/* store character c as UTF-8 encoded byte stream */
void TY_(AddCharToLexer)( Lexer *lexer, uint c )
{
int i, err, count = 0;
tmbchar buf[10] = {0};
err = TY_(EncodeCharToUTF8Bytes)( c, buf, NULL, &count );
if (err)
{
#if 0 && defined(_DEBUG)
fprintf( stderr, "lexer UTF-8 encoding error for U+%x : ", c );
#endif
/* replacement character 0xFFFD encoded as UTF-8 */
buf[0] = (byte) 0xEF;
buf[1] = (byte) 0xBF;
buf[2] = (byte) 0xBD;
count = 3;
}
for ( i = 0; i < count; ++i )
AddByte( lexer, buf[i] );
}
static void AddStringToLexer( Lexer *lexer, ctmbstr str )
{
uint c;
/* Many (all?) compilers will sign-extend signed chars (the default) when
** converting them to unsigned integer values. We must cast our char to
** unsigned char before assigning it to prevent this from happening.
*/
while( 0 != (c = (unsigned char) *str++ ))
TY_(AddCharToLexer)( lexer, c );
}
static void SetLexerLocus( TidyDocImpl* doc, Lexer *lexer )
{
lexer->lines = doc->docIn->curline;
lexer->columns = doc->docIn->curcol;
}
/*
No longer attempts to insert missing ';' for unknown
enitities unless one was present already, since this
gives unexpected results.
For example: <a href="something.htm?foo&bar&fred">
was tidied to: <a href="something.htm?foo&bar;&fred;">
rather than: <a href="something.htm?foo&bar&fred">
My thanks for Maurice Buxton for spotting this.
Also Randy Waki pointed out the following case for the
04 Aug 00 version (bug #433012):
For example: <a href="something.htm?id=1&lang=en">
was tidied to: <a href="something.htm?id=1⟨=en">
rather than: <a href="something.htm?id=1&lang=en">
where "lang" is a known entity (#9001), but browsers would
misinterpret "⟨" because it had a value > 256.
So the case of an apparently known entity with a value > 256 and
missing a semicolon is handled specially.
"ParseEntity" is also a bit of a misnomer - it handles entities and
numeric character references. Invalid NCR's are now reported.
*/
static void ParseEntity( TidyDocImpl* doc, GetTokenMode mode )
{
typedef enum
{
ENT_default,
ENT_numdec,
ENT_numhex
} ENTState;
typedef Bool (*ENTfn)(uint);
const ENTfn entFn[] = {
TY_(IsNamechar),
TY_(IsDigit),
IsDigitHex
};
uint start;
ENTState entState = ENT_default;
uint charRead = 0;
Bool semicolon = no, found = no;
Bool isXml = cfgBool( doc, TidyXmlTags );
Bool preserveEntities = cfgBool( doc, TidyPreserveEntities );
uint c, ch, startcol, entver = 0;
Lexer* lexer = doc->lexer;
start = lexer->lexsize - 1; /* to start at "&" */
startcol = doc->docIn->curcol - 1;
while ( (c = TY_(ReadChar)(doc->docIn)) != EndOfStream )
{
if ( c == ';' )
{
semicolon = yes;
break;
}
++charRead;
if (charRead == 1 && c == '#')
{
#if SUPPORT_ASIAN_ENCODINGS
if ( !cfgBool(doc, TidyNCR) ||
cfg(doc, TidyInCharEncoding) == BIG5 ||
cfg(doc, TidyInCharEncoding) == SHIFTJIS )
{
TY_(UngetChar)('#', doc->docIn);
return;
}
#endif
TY_(AddCharToLexer)( lexer, c );
entState = ENT_numdec;
continue;
}
else if (charRead == 2 && entState == ENT_numdec
&& (c == 'x' || (!isXml && c == 'X')) )
{
TY_(AddCharToLexer)( lexer, c );
entState = ENT_numhex;
continue;
}
if ( entFn[entState](c) )
{
TY_(AddCharToLexer)( lexer, c );
continue;
}
/* otherwise put it back */
TY_(UngetChar)( c, doc->docIn );
break;
}
/* make sure entity is NULL terminated */
lexer->lexbuf[lexer->lexsize] = '\0';
/* Should contrain version to XML/XHTML if '
** is encountered. But this is not possible with
** Tidy's content model bit mask.
*/
if ( TY_(tmbstrcmp)(lexer->lexbuf+start, "&apos") == 0
&& !cfgBool(doc, TidyXmlOut)
&& !lexer->isvoyager
&& !cfgBool(doc, TidyXhtmlOut) )
TY_(ReportEntityError)( doc, APOS_UNDEFINED, lexer->lexbuf+start, 39 );
if (( mode == OtherNamespace ) && ( c == ';' ))
{
/* #130 MathML attr and entity fix! */
found = yes;
ch = 255;
entver = XH50|HT50;
preserveEntities = yes;
}
else
{
/* Lookup entity code and version
*/
found = TY_(EntityInfo)( lexer->lexbuf+start, isXml, &ch, &entver );
}
/* deal with unrecognized or invalid entities */
/* #433012 - fix by Randy Waki 17 Feb 01 */
/* report invalid NCR's - Terry Teague 01 Sep 01 */
if ( !found || (ch >= 128 && ch <= 159) || (ch >= 256 && c != ';') )
{
/* set error position just before offending character */
SetLexerLocus( doc, lexer );
lexer->columns = startcol;
if (lexer->lexsize > start + 1)
{
if (ch >= 128 && ch <= 159)
{
/* invalid numeric character reference */
uint c1 = 0;
int replaceMode = DISCARDED_CHAR;
if ( TY_(ReplacementCharEncoding) == WIN1252 )
c1 = TY_(DecodeWin1252)( ch );
else if ( TY_(ReplacementCharEncoding) == MACROMAN )
c1 = TY_(DecodeMacRoman)( ch );
if ( c1 )
replaceMode = REPLACED_CHAR;
if ( c != ';' ) /* issue warning if not terminated by ';' */
TY_(ReportEntityError)( doc, MISSING_SEMICOLON_NCR,
lexer->lexbuf+start, c );
TY_(ReportEncodingError)(doc, INVALID_NCR, ch, replaceMode == DISCARDED_CHAR);
if ( c1 )
{
/* make the replacement */
lexer->lexsize = start;
TY_(AddCharToLexer)( lexer, c1 );
semicolon = no;
}
else
{
/* discard */
lexer->lexsize = start;
semicolon = no;
}
}
else
TY_(ReportEntityError)( doc, UNKNOWN_ENTITY,
lexer->lexbuf+start, ch );
if (semicolon)
TY_(AddCharToLexer)( lexer, ';' );
}
else /* naked & */
TY_(ReportEntityError)( doc, UNESCAPED_AMPERSAND,
lexer->lexbuf+start, ch );
}
else
{
if ( c != ';' ) /* issue warning if not terminated by ';' */
{
/* set error position just before offending chararcter */
SetLexerLocus( doc, lexer );
lexer->columns = startcol;
TY_(ReportEntityError)( doc, MISSING_SEMICOLON, lexer->lexbuf+start, c );
}
if (preserveEntities)
TY_(AddCharToLexer)( lexer, ';' );
else
{
lexer->lexsize = start;
if ( ch == 160 && (mode == Preformatted) )
ch = ' ';
TY_(AddCharToLexer)( lexer, ch );
if ( ch == '&' && !cfgBool(doc, TidyQuoteAmpersand) )
AddStringToLexer( lexer, "amp;" );
}
/* Detect extended vs. basic entities */
TY_(ConstrainVersion)( doc, entver );
}
}
static tmbchar ParseTagName( TidyDocImpl* doc )
{
Lexer *lexer = doc->lexer;
uint c = lexer->lexbuf[ lexer->txtstart ];
Bool xml = cfgBool(doc, TidyXmlTags);
/* fold case of first character in buffer */
if (!xml && TY_(IsUpper)(c))
lexer->lexbuf[lexer->txtstart] = (tmbchar) TY_(ToLower)(c);
while ((c = TY_(ReadChar)(doc->docIn)) != EndOfStream)
{
if ((!xml && !TY_(IsNamechar)(c)) ||
(xml && !TY_(IsXMLNamechar)(c)))
break;
/* fold case of subsequent characters */
if (!xml && TY_(IsUpper)(c))
c = TY_(ToLower)(c);
TY_(AddCharToLexer)(lexer, c);
}
lexer->txtend = lexer->lexsize;
return (tmbchar) c;
}
/*
Used for elements and text nodes
element name is NULL for text nodes
start and end are offsets into lexbuf
which contains the textual content of
all elements in the parse tree.
parent and content allow traversal
of the parse tree in any direction.
attributes are represented as a linked
list of AttVal nodes which hold the
strings for attribute/value pairs.
*/
Node *TY_(NewNode)(TidyAllocator* allocator, Lexer *lexer)
{
Node* node = (Node*) TidyAlloc( allocator, sizeof(Node) );
TidyClearMemory( node, sizeof(Node) );
if ( lexer )
{
node->line = lexer->lines;
node->column = lexer->columns;
}
node->type = TextNode;
#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_ALLOCATION)
SPRTF("Allocated node %p\n", node );
#endif
return node;
}
/* used to clone heading nodes when split by an <HR> */
Node *TY_(CloneNode)( TidyDocImpl* doc, Node *element )
{
Lexer* lexer = doc->lexer;
Node *node = TY_(NewNode)( lexer->allocator, lexer );
node->start = lexer->lexsize;
node->end = lexer->lexsize;
if ( element )
{
node->parent = element->parent;
node->type = element->type;
node->closed = element->closed;
node->implicit = element->implicit;
node->tag = element->tag;
node->element = TY_(tmbstrdup)( doc->allocator, element->element );
node->attributes = TY_(DupAttrs)( doc, element->attributes );
}
return node;
}
/* free node's attributes */
void TY_(FreeAttrs)( TidyDocImpl* doc, Node *node )
{
while ( node->attributes )
{
AttVal *av = node->attributes;
if ( av->attribute )
{
if ( (attrIsID(av) || attrIsNAME(av)) &&
TY_(IsAnchorElement)(doc, node) )
{
TY_(RemoveAnchorByNode)( doc, av->value, node );
}
}
node->attributes = av->next;
TY_(FreeAttribute)( doc, av );
}
}
/* doesn't repair attribute list linkage */
void TY_(FreeAttribute)( TidyDocImpl* doc, AttVal *av )
{
TY_(FreeNode)( doc, av->asp );
TY_(FreeNode)( doc, av->php );
TidyDocFree( doc, av->attribute );
TidyDocFree( doc, av->value );
TidyDocFree( doc, av );
}
/* detach attribute from node
*/
void TY_(DetachAttribute)( Node *node, AttVal *attr )
{
AttVal *av, *prev = NULL;
for ( av = node->attributes; av; av = av->next )
{
if ( av == attr )
{
if ( prev )
prev->next = attr->next;
else
node->attributes = attr->next;
break;
}
prev = av;
}
}
/* detach attribute from node then free it
*/
void TY_(RemoveAttribute)( TidyDocImpl* doc, Node *node, AttVal *attr )
{
TY_(DetachAttribute)( node, attr );
TY_(FreeAttribute)( doc, attr );
}
/*
Free document nodes by iterating through peers and recursing
through children. Set next to NULL before calling TY_(FreeNode)()
to avoid freeing peer nodes. Doesn't patch up prev/next links.
*/
void TY_(FreeNode)( TidyDocImpl* doc, Node *node )
{
#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_ALLOCATION)
if (node) SPRTF("Free node %p\n", node );
#endif
/* this is no good ;=((
if (node && doc && doc->lexer) {
if (node == doc->lexer->token) {
doc->lexer->token = NULL; // TY_(NewNode)( doc->lexer->allocator, doc->lexer );
}
}
----------------- */
while ( node )
{
Node* next = node->next;
TY_(FreeAttrs)( doc, node );
TY_(FreeNode)( doc, node->content );
TidyDocFree( doc, node->element );
#ifdef TIDY_STORE_ORIGINAL_TEXT
if (node->otext)
TidyDocFree(doc, node->otext);
#endif
if (RootNode != node->type)
TidyDocFree( doc, node );
else
node->content = NULL;
node = next;
}
}
#ifdef TIDY_STORE_ORIGINAL_TEXT
void StoreOriginalTextInToken(TidyDocImpl* doc, Node* node, uint count)
{
if (!doc->storeText)
return;
if (count >= doc->docIn->otextlen)
return;
if (!doc->docIn->otextsize)
return;
if (count == 0)
{
node->otext = doc->docIn->otextbuf;
doc->docIn->otextbuf = NULL;
doc->docIn->otextlen = 0;
doc->docIn->otextsize = 0;
}
else
{
uint len = doc->docIn->otextlen;
tmbstr buf1 = (tmbstr)TidyDocAlloc(doc, len - count + 1);
tmbstr buf2 = (tmbstr)TidyDocAlloc(doc, count + 1);
uint i, j;
/* strncpy? */
for (i = 0; i < len - count; ++i)
buf1[i] = doc->docIn->otextbuf[i];
buf1[i] = 0;
for (j = 0; j + i < len; ++j)
buf2[j] = doc->docIn->otextbuf[j + i];
buf2[j] = 0;
TidyDocFree(doc, doc->docIn->otextbuf);
node->otext = buf1;
doc->docIn->otextbuf = buf2;
doc->docIn->otextlen = count;
doc->docIn->otextsize = count + 1;
}
}
#endif
Node* TY_(TextToken)( Lexer *lexer )
{
Node *node = TY_(NewNode)( lexer->allocator, lexer );
node->start = lexer->txtstart;
node->end = lexer->txtend;
return node;
}
/* used for creating preformatted text from Word2000 */
Node *TY_(NewLineNode)( Lexer *lexer )
{
Node *node = TY_(NewNode)( lexer->allocator, lexer );
node->start = lexer->lexsize;
TY_(AddCharToLexer)( lexer, (uint)'\n' );
node->end = lexer->lexsize;
return node;
}
/* used for adding a for Word2000 */
Node* TY_(NewLiteralTextNode)( Lexer *lexer, ctmbstr txt )
{
Node *node = TY_(NewNode)( lexer->allocator, lexer );
node->start = lexer->lexsize;
AddStringToLexer( lexer, txt );
node->end = lexer->lexsize;
return node;
}
static Node* TagToken( TidyDocImpl* doc, NodeType type )
{
Lexer* lexer = doc->lexer;
Node* node = TY_(NewNode)( lexer->allocator, lexer );
node->type = type;
node->element = TY_(tmbstrndup)( doc->allocator,
lexer->lexbuf + lexer->txtstart,
lexer->txtend - lexer->txtstart );
node->start = lexer->txtstart;
node->end = lexer->txtstart;
if ( type == StartTag || type == StartEndTag || type == EndTag )
TY_(FindTag)(doc, node);
return node;
}
static Node* NewToken(TidyDocImpl* doc, NodeType type)
{
Lexer* lexer = doc->lexer;
Node* node = TY_(NewNode)(lexer->allocator, lexer);
node->type = type;
node->start = lexer->txtstart;
node->end = lexer->txtend;
#ifdef TIDY_STORE_ORIGINAL_TEXT
StoreOriginalTextInToken(doc, node, 0);
#endif
return node;
}
#define CommentToken(doc) NewToken(doc, CommentTag)
#define DocTypeToken(doc) NewToken(doc, DocTypeTag)
#define PIToken(doc) NewToken(doc, ProcInsTag)
#define AspToken(doc) NewToken(doc, AspTag)
#define JsteToken(doc) NewToken(doc, JsteTag)
#define PhpToken(doc) NewToken(doc, PhpTag)
#define XmlDeclToken(doc) NewToken(doc, XmlDecl)
#define SectionToken(doc) NewToken(doc, SectionTag)
#define CDATAToken(doc) NewToken(doc, CDATATag)
void TY_(AddStringLiteral)( Lexer* lexer, ctmbstr str )
{
byte c;
while(0 != (c = *str++) )
TY_(AddCharToLexer)( lexer, c );
}
/*
void AddStringLiteralLen( Lexer* lexer, ctmbstr str, int len )
{
byte c;
int ix;
for ( ix=0; ix < len && (c = *str++); ++ix )
TY_(AddCharToLexer)(lexer, c);
}
*/
/* find doctype element */
Node *TY_(FindDocType)( TidyDocImpl* doc )
{
Node* node;
for ( node = (doc ? doc->root.content : NULL);
node && node->type != DocTypeTag;
node = node->next )
/**/;
return node;
}
/* find parent container element */
Node* TY_(FindContainer)( Node* node )
{
for ( node = (node ? node->parent : NULL);
node && TY_(nodeHasCM)(node, CM_INLINE);
node = node->parent )
/**/;
return node;
}
/* find html element */
Node *TY_(FindHTML)( TidyDocImpl* doc )
{
Node *node;
for ( node = (doc ? doc->root.content : NULL);
node && !nodeIsHTML(node);
node = node->next )
/**/;
return node;
}
/* find XML Declaration */
Node *TY_(FindXmlDecl)(TidyDocImpl* doc)
{
Node *node;
for ( node = (doc ? doc->root.content : NULL);
node && !(node->type == XmlDecl);
node = node->next )
/**/;
return node;
}
Node *TY_(FindHEAD)( TidyDocImpl* doc )
{
Node *node = TY_(FindHTML)( doc );
if ( node )
{
for ( node = node->content;
node && !nodeIsHEAD(node);
node = node->next )
/**/;
}
return node;
}
Node *TY_(FindTITLE)(TidyDocImpl* doc)
{
Node *node = TY_(FindHEAD)(doc);
if (node)
for (node = node->content;
node && !nodeIsTITLE(node);
node = node->next) {}
return node;
}
Node *TY_(FindBody)( TidyDocImpl* doc )
{
Node *node = ( doc ? doc->root.content : NULL );
while ( node && !nodeIsHTML(node) )
node = node->next;
if (node == NULL)
return NULL;
node = node->content;
while ( node && !nodeIsBODY(node) && !nodeIsFRAMESET(node) )
node = node->next;
if ( node && nodeIsFRAMESET(node) )
{
node = node->content;
while ( node && !nodeIsNOFRAMES(node) )
node = node->next;
if ( node )
{
node = node->content;
while ( node && !nodeIsBODY(node) )
node = node->next;
}
}
return node;
}
/* add meta element for Tidy */
Bool TY_(AddGenerator)( TidyDocImpl* doc )
{
AttVal *attval;
Node *node;
Node *head = TY_(FindHEAD)( doc );
tmbchar buf[256];
if (head)
{
#ifdef PLATFORM_NAME
TY_(tmbsnprintf)(buf, sizeof(buf), "HTML Tidy for HTML5 for "PLATFORM_NAME" version %s",
tidyLibraryVersion());
#else
TY_(tmbsnprintf)(buf, sizeof(buf), "HTML Tidy for HTML5 version %s", tidyLibraryVersion());
#endif
for ( node = head->content; node; node = node->next )
{
if ( nodeIsMETA(node) )
{
attval = TY_(AttrGetById)(node, TidyAttr_NAME);
if (AttrValueIs(attval, "generator"))
{
attval = TY_(AttrGetById)(node, TidyAttr_CONTENT);
if (AttrHasValue(attval) &&
TY_(tmbstrncasecmp)(attval->value, "HTML Tidy", 9) == 0)
{
/* update the existing content to reflect the */
/* actual version of Tidy currently being used */
TidyDocFree(doc, attval->value);
attval->value = TY_(tmbstrdup)(doc->allocator, buf);
return no;
}
}
}
}
if ( cfg(doc, TidyAccessibilityCheckLevel) == 0 )
{
node = TY_(InferredTag)(doc, TidyTag_META);
TY_(AddAttribute)( doc, node, "name", "generator" );
TY_(AddAttribute)( doc, node, "content", buf );
TY_(InsertNodeAtStart)( head, node );
return yes;
}
}
return no;
}
/*\ examine <!DOCTYPE ...> to identify version
* Issue #167 and #169
* If HTML5
* <!DOCTYPE html>
* <!DOCTYPE html SYSTEM "about:legacy-compat">
* else others
\*/
static uint FindGivenVersion( TidyDocImpl* doc, Node* doctype )
{
AttVal * fpi = TY_(GetAttrByName)(doctype, "PUBLIC");
uint vers;
if (!fpi || !fpi->value)
{
if (doctype->element && (TY_(tmbstrcmp)(doctype->element,"html") == 0))
{
return VERS_HTML5; /* TODO: do we need to check MORE? */
}
/* TODO: Consider warning, error message */
return VERS_UNKNOWN;
}
vers = GetVersFromFPI(fpi->value);
if (VERS_XHTML & vers)
{
TY_(SetOptionBool)(doc, TidyXmlOut, yes);
TY_(SetOptionBool)(doc, TidyXhtmlOut, yes);
doc->lexer->isvoyager = yes;
}
/* todo: add a warning if case does not match? */
TidyDocFree(doc, fpi->value);
fpi->value = TY_(tmbstrdup)(doc->allocator, GetFPIFromVers(vers));
return vers;
}
/* return guessed version */
uint TY_(ApparentVersion)( TidyDocImpl* doc )
{
if ((doc->lexer->doctype == XH11 ||
doc->lexer->doctype == XB10) &&
(doc->lexer->versions & doc->lexer->doctype))
return doc->lexer->doctype;
else
return TY_(HTMLVersion)(doc);
}
ctmbstr TY_(HTMLVersionNameFromCode)( uint vers, Bool ARG_UNUSED(isXhtml) )
{
ctmbstr name = GetNameFromVers(vers);
/* this test has moved to ReportMarkupVersion() in localize.c, for localization reasons */
/*
if (!name)
name = "HTML Proprietary";
*/
return name;
}
Bool TY_(WarnMissingSIInEmittedDocType)( TidyDocImpl* doc )
{
Bool isXhtml = doc->lexer->isvoyager;
Node* doctype;
/* Do not warn in XHTML mode */
if ( isXhtml )
return no;
/* Do not warn if emitted doctype is proprietary */
if ( TY_(HTMLVersionNameFromCode)(doc->lexer->versionEmitted, isXhtml ) == NULL )
return no;
/* Do not warn if no SI is possible */
if ( GetSIFromVers(doc->lexer->versionEmitted) == NULL )
return no;
if ( (doctype = TY_(FindDocType)( doc )) != NULL
&& TY_(GetAttrByName)(doctype, "SYSTEM") == NULL )
return yes;
return no;
}
/* Put DOCTYPE declaration between the
** <?xml version "1.0" ... ?> declaration, if any,
** and the <html> tag. Should also work for any comments,
** etc. that may precede the <html> tag.
*/
static Node* NewDocTypeNode( TidyDocImpl* doc )
{
Node* doctype = NULL;
Node* html = TY_(FindHTML)( doc );
if ( !html )
return NULL;
doctype = TY_(NewNode)( doc->allocator, NULL );
doctype->type = DocTypeTag;
TY_(InsertNodeBeforeElement)(html, doctype);
return doctype;
}
Bool TY_(SetXHTMLDocType)( TidyDocImpl* doc )
{
Lexer *lexer = doc->lexer;
Node *doctype = TY_(FindDocType)( doc );
TidyDoctypeModes dtmode = (TidyDoctypeModes)cfg(doc, TidyDoctypeMode);
ctmbstr pub = "PUBLIC";
ctmbstr sys = "SYSTEM";
lexer->versionEmitted = TY_(ApparentVersion)( doc );
if (dtmode == TidyDoctypeOmit)
{
if (doctype)
TY_(DiscardElement)(doc, doctype);
return yes;
}
if (dtmode == TidyDoctypeUser && !cfgStr(doc, TidyDoctype))
return no;
if (!doctype)
{
doctype = NewDocTypeNode(doc);
doctype->element = TY_(tmbstrdup)(doc->allocator, "html");
}
else
{
doctype->element = TY_(tmbstrtolower)(doctype->element);
}
switch(dtmode)
{
case TidyDoctypeHtml5:
/* HTML5 */
TY_(RepairAttrValue)(doc, doctype, pub, NULL);
TY_(RepairAttrValue)(doc, doctype, sys, NULL);
lexer->versionEmitted = XH50;
break;
case TidyDoctypeStrict:
/* XHTML 1.0 Strict */
TY_(RepairAttrValue)(doc, doctype, pub, GetFPIFromVers(X10S));
TY_(RepairAttrValue)(doc, doctype, sys, GetSIFromVers(X10S));
lexer->versionEmitted = X10S;
break;
case TidyDoctypeLoose:
/* XHTML 1.0 Transitional */
TY_(RepairAttrValue)(doc, doctype, pub, GetFPIFromVers(X10T));
TY_(RepairAttrValue)(doc, doctype, sys, GetSIFromVers(X10T));
lexer->versionEmitted = X10T;
break;
case TidyDoctypeUser:
/* user defined document type declaration */
TY_(RepairAttrValue)(doc, doctype, pub, cfgStr(doc, TidyDoctype));
TY_(RepairAttrValue)(doc, doctype, sys, "");
break;
case TidyDoctypeAuto:
if (lexer->doctype == VERS_UNKNOWN) {
lexer->versionEmitted = XH50;
return yes;
}
else if (lexer->versions & XH11 && lexer->doctype == XH11)
{
if (!TY_(GetAttrByName)(doctype, sys))
TY_(RepairAttrValue)(doc, doctype, sys, GetSIFromVers(XH11));
lexer->versionEmitted = XH11;
return yes;
}
else if (lexer->versions & XH11 && !(lexer->versions & VERS_HTML40))
{
TY_(RepairAttrValue)(doc, doctype, pub, GetFPIFromVers(XH11));
TY_(RepairAttrValue)(doc, doctype, sys, GetSIFromVers(XH11));
lexer->versionEmitted = XH11;
}
else if (lexer->versions & XB10 && lexer->doctype == XB10)
{
if (!TY_(GetAttrByName)(doctype, sys))
TY_(RepairAttrValue)(doc, doctype, sys, GetSIFromVers(XB10));
lexer->versionEmitted = XB10;
return yes;
}
else if (lexer->versions & VERS_HTML40_STRICT)
{
TY_(RepairAttrValue)(doc, doctype, pub, GetFPIFromVers(X10S));
TY_(RepairAttrValue)(doc, doctype, sys, GetSIFromVers(X10S));
lexer->versionEmitted = X10S;
}
else if (lexer->versions & VERS_FRAMESET)
{
TY_(RepairAttrValue)(doc, doctype, pub, GetFPIFromVers(X10F));
TY_(RepairAttrValue)(doc, doctype, sys, GetSIFromVers(X10F));
lexer->versionEmitted = X10F;
}
else if (lexer->versions & VERS_LOOSE)
{
TY_(RepairAttrValue)(doc, doctype, pub, GetFPIFromVers(X10T));
TY_(RepairAttrValue)(doc, doctype, sys, GetSIFromVers(X10T));
lexer->versionEmitted = X10T;
}
else
{
if (doctype)
TY_(DiscardElement)(doc, doctype);
return no;
}
break;
case TidyDoctypeOmit:
assert(0);
break;
}
return no;
}
/* fixup doctype if missing */
Bool TY_(FixDocType)( TidyDocImpl* doc )
{
Lexer* lexer = doc->lexer;
Node* doctype = TY_(FindDocType)( doc );
uint dtmode = cfg( doc, TidyDoctypeMode );
uint guessed = VERS_UNKNOWN;
Bool hadSI = no;
/* Issue #167 - found doctype, and doctype is default VERS_HTML5, set VERS_HTML5 and return yes */
if (doctype && (dtmode == TidyDoctypeAuto) &&
(lexer->doctype == VERS_HTML5) )
{
lexer->versionEmitted = lexer->doctype;
return yes;
}
if (dtmode == TidyDoctypeAuto &&
lexer->versions & lexer->doctype &&
!(VERS_XHTML & lexer->doctype && !lexer->isvoyager)
&& TY_(FindDocType)(doc))
{
lexer->versionEmitted = lexer->doctype;
return yes;
}
if (dtmode == TidyDoctypeOmit)
{
if (doctype)
TY_(DiscardElement)( doc, doctype );
lexer->versionEmitted = TY_(ApparentVersion)( doc );
return yes;
}
if (cfgBool(doc, TidyXmlOut))
return yes;
if (doctype)
hadSI = TY_(GetAttrByName)(doctype, "SYSTEM") != NULL;
if ((dtmode == TidyDoctypeStrict ||
dtmode == TidyDoctypeLoose) && doctype)
{
TY_(DiscardElement)(doc, doctype);
doctype = NULL;
}
switch (dtmode)
{
case TidyDoctypeHtml5:
guessed = HT50;
break;
case TidyDoctypeStrict:
guessed = H41S;
break;
case TidyDoctypeLoose:
guessed = H41T;
break;
case TidyDoctypeAuto:
guessed = TY_(HTMLVersion)(doc);
break;
}
lexer->versionEmitted = guessed;
if (guessed == VERS_UNKNOWN)
return no;
if (doctype)
{
doctype->element = TY_(tmbstrtolower)(doctype->element);
}
else
{
doctype = NewDocTypeNode(doc);
doctype->element = TY_(tmbstrdup)(doc->allocator, "html");
}
TY_(RepairAttrValue)(doc, doctype, "PUBLIC", GetFPIFromVers(guessed));
if (hadSI)
TY_(RepairAttrValue)(doc, doctype, "SYSTEM", GetSIFromVers(guessed));
return yes;
}
/* ensure XML document starts with <?xml version="1.0"?> */
/* add encoding attribute if not using ASCII or UTF-8 output */
Bool TY_(FixXmlDecl)( TidyDocImpl* doc )
{
Node* xml;
AttVal *version, *encoding;
Lexer*lexer = doc->lexer;
Node* root = &doc->root;
if ( root->content && root->content->type == XmlDecl )
{
xml = root->content;
}
else
{
xml = TY_(NewNode)(lexer->allocator, lexer);
xml->type = XmlDecl;
if ( root->content )
TY_(InsertNodeBeforeElement)(root->content, xml);
else
root->content = xml;
}
version = TY_(GetAttrByName)(xml, "version");
encoding = TY_(GetAttrByName)(xml, "encoding");
/*
We need to insert a check if declared encoding
and output encoding mismatch and fix the XML
declaration accordingly!!!
*/
if ( encoding == NULL && cfg(doc, TidyOutCharEncoding) != UTF8 )
{
ctmbstr enc = TY_(GetEncodingNameFromTidyId)(cfg(doc, TidyOutCharEncoding));
if ( enc )
TY_(AddAttribute)( doc, xml, "encoding", enc );
}
if ( version == NULL )
TY_(AddAttribute)( doc, xml, "version", "1.0" );
return yes;
}
Node* TY_(InferredTag)(TidyDocImpl* doc, TidyTagId id)
{
Lexer *lexer = doc->lexer;
Node *node = TY_(NewNode)( lexer->allocator, lexer );
const Dict* dict = TY_(LookupTagDef)(id);
assert( dict != NULL );
node->type = StartTag;
node->implicit = yes;
node->element = TY_(tmbstrdup)(doc->allocator, dict->name);
node->tag = dict;
node->start = lexer->txtstart;
node->end = lexer->txtend;
return node;
}
static Bool ExpectsContent(Node *node)
{
if (node->type != StartTag)
return no;
/* unknown element? */
if (node->tag == NULL)
return yes;
if (node->tag->model & CM_EMPTY)
return no;
return yes;
}
/*
create a text node for the contents of
a CDATA element like style or script
which ends with </foo> for some foo.
*/
typedef enum
{
CDATA_INTERMEDIATE,
CDATA_STARTTAG,
CDATA_ENDTAG
} CDATAState;
static Node *GetCDATA( TidyDocImpl* doc, Node *container )
{
Lexer* lexer = doc->lexer;
uint start = 0;
int nested = 0;
CDATAState state = CDATA_INTERMEDIATE;
uint i;
Bool isEmpty = yes;
Bool matches = no;
uint c;
Bool hasSrc = TY_(AttrGetById)(container, TidyAttr_SRC) != NULL;
SetLexerLocus( doc, lexer );
lexer->waswhite = no;
lexer->txtstart = lexer->txtend = lexer->lexsize;
/* seen start tag, look for matching end tag */
while ((c = TY_(ReadChar)(doc->docIn)) != EndOfStream)
{
TY_(AddCharToLexer)(lexer, c);
lexer->txtend = lexer->lexsize;
if (state == CDATA_INTERMEDIATE)
{
if (c != '<')
{
if (isEmpty && !TY_(IsWhite)(c))
isEmpty = no;
continue;
}
c = TY_(ReadChar)(doc->docIn);
if (TY_(IsLetter)(c))
{
/* <head><script src=foo><meta name=foo content=bar>*/
if (hasSrc && isEmpty && nodeIsSCRIPT(container))
{
/* ReportError(doc, container, NULL, MISSING_ENDTAG_FOR); */
lexer->lexsize = lexer->txtstart;
TY_(UngetChar)(c, doc->docIn);
TY_(UngetChar)('<', doc->docIn);
return NULL;
}
TY_(AddCharToLexer)(lexer, c);
start = lexer->lexsize - 1;
state = CDATA_STARTTAG;
}
else if (c == '/')
{
TY_(AddCharToLexer)(lexer, c);
c = TY_(ReadChar)(doc->docIn);
if (!TY_(IsLetter)(c))
{
TY_(UngetChar)(c, doc->docIn);
continue;
}
TY_(UngetChar)(c, doc->docIn);
start = lexer->lexsize;
state = CDATA_ENDTAG;
}
else if (c == '\\')
{
/* recognize document.write("<script><\/script>") */
TY_(AddCharToLexer)(lexer, c);
c = TY_(ReadChar)(doc->docIn);
if (c != '/')
{
TY_(UngetChar)(c, doc->docIn);
continue;
}
TY_(AddCharToLexer)(lexer, c);
c = TY_(ReadChar)(doc->docIn);
if (!TY_(IsLetter)(c))
{
TY_(UngetChar)(c, doc->docIn);
continue;
}
TY_(UngetChar)(c, doc->docIn);
start = lexer->lexsize;
state = CDATA_ENDTAG;
}
else
{
TY_(UngetChar)(c, doc->docIn);
}
}
/* '<' + Letter found */
else if (state == CDATA_STARTTAG)
{
if (TY_(IsLetter)(c))
continue;
matches = TY_(tmbstrncasecmp)(container->element, lexer->lexbuf + start,
TY_(tmbstrlen)(container->element)) == 0;
if (matches)
nested++;
state = CDATA_INTERMEDIATE;
}
/* '<' + '/' + Letter found */
else if (state == CDATA_ENDTAG)
{
if (TY_(IsLetter)(c))
continue;
matches = TY_(tmbstrncasecmp)(container->element, lexer->lexbuf + start,
TY_(tmbstrlen)(container->element)) == 0;
if (isEmpty && !matches)
{
/* ReportError(doc, container, NULL, MISSING_ENDTAG_FOR); */
for (i = lexer->lexsize - 1; i >= start; --i)
TY_(UngetChar)((uint)lexer->lexbuf[i], doc->docIn);
TY_(UngetChar)('/', doc->docIn);
TY_(UngetChar)('<', doc->docIn);
break;
}
if (matches && nested-- <= 0)
{
for (i = lexer->lexsize - 1; i >= start; --i)
TY_(UngetChar)((uint)lexer->lexbuf[i], doc->docIn);
TY_(UngetChar)('/', doc->docIn);
TY_(UngetChar)('<', doc->docIn);
lexer->lexsize -= (lexer->lexsize - start) + 2;
break;
}
else if (lexer->lexbuf[start - 2] != '\\')
{
/* if the end tag is not already escaped using backslash */
SetLexerLocus( doc, lexer );
lexer->columns -= 3;
TY_(ReportError)(doc, NULL, NULL, BAD_CDATA_CONTENT);
/* if javascript insert backslash before / */
if (TY_(IsJavaScript)(container))
{
for (i = lexer->lexsize; i > start-1; --i)
lexer->lexbuf[i] = lexer->lexbuf[i-1];
lexer->lexbuf[start-1] = '\\';
lexer->lexsize++;
}
}
state = CDATA_INTERMEDIATE;
}
}
if (isEmpty)
lexer->lexsize = lexer->txtstart = lexer->txtend;
else
lexer->txtend = lexer->lexsize;
if (c == EndOfStream)
TY_(ReportError)(doc, container, NULL, MISSING_ENDTAG_FOR );
/* this was disabled for some reason... */
#if 0
if (lexer->txtend > lexer->txtstart)
return TextToken(lexer);
else
return NULL;
#else
return TY_(TextToken)(lexer);
#endif
}
void TY_(UngetToken)( TidyDocImpl* doc )
{
doc->lexer->pushed = yes;
}
#ifdef TIDY_STORE_ORIGINAL_TEXT
#define CondReturnTextNode(doc, skip) \
if (lexer->txtend > lexer->txtstart) \
{ \
lexer->token = TY_(TextToken)(lexer); \
StoreOriginalTextInToken(doc, lexer->token, skip); \
return lexer->token; \
}
#else
#if !defined(NDEBUG) && defined(_MSC_VER)
#define CondReturnTextNode(doc, skip) \
if (lexer->txtend > lexer->txtstart) { \
Node *_node = TY_(TextToken)(lexer); \
lexer->token = _node; \
GTDBG(doc,"text_node",_node); \
return _node; \
}
#else
#define CondReturnTextNode(doc, skip) \
if (lexer->txtend > lexer->txtstart) \
{ \
lexer->token = TY_(TextToken)(lexer); \
return lexer->token; \
}
#endif
#endif
/*
modes for GetToken()
MixedContent -- for elements which don't accept PCDATA
Preformatted -- white space preserved as is
IgnoreMarkup -- for CDATA elements such as script, style
*/
static Node* GetTokenFromStream( TidyDocImpl* doc, GetTokenMode mode );
Node* TY_(GetToken)( TidyDocImpl* doc, GetTokenMode mode )
{
Node *node;
Lexer* lexer = doc->lexer;
if (lexer->pushed || lexer->itoken)
{
/* Deal with previously returned duplicate inline token */
if (lexer->itoken)
{
/* itoken rejected */
if (lexer->pushed)
{
lexer->pushed = no;
node = lexer->itoken;
GTDBG(doc,"lex-itoken", node);
return node;
}
/* itoken has been accepted */
lexer->itoken = NULL;
}
/* duplicate inlines in preference to pushed text nodes when appropriate */
lexer->pushed = no;
if (lexer->token->type != TextNode
|| !(lexer->insert || lexer->inode)) {
node = lexer->token;
GTDBG(doc,"lex-token", node);
return node;
}
lexer->itoken = TY_(InsertedToken)( doc );
node = lexer->itoken;
GTDBG(doc,"lex-inserted", node);
return node;
}
assert( !(lexer->pushed || lexer->itoken) );
/* at start of block elements, unclosed inline
elements are inserted into the token stream */
if (lexer->insert || lexer->inode) {
/*\ Issue #92: could fix by the following, but instead chose not to stack these 2
* if ( !(lexer->insert && (nodeIsINS(lexer->insert) || nodeIsDEL(lexer->insert))) ) {
\*/
lexer->token = TY_(InsertedToken)( doc );
node = lexer->token;
GTDBG(doc,"lex-inserted2", node);
return node;
}
if (mode == CdataContent)
{
assert( lexer->parent != NULL );
node = GetCDATA(doc, lexer->parent);
GTDBG(doc,"lex-cdata", node);
return node;
}
return GetTokenFromStream( doc, mode );
}
#if !defined(NDEBUG) && defined(_MSC_VER)
static void check_me(char *name)
{
SPRTF("Have node %s\n", name);
}
#endif
static Node* GetTokenFromStream( TidyDocImpl* doc, GetTokenMode mode )
{
Lexer* lexer = doc->lexer;
uint c, lexdump, badcomment = 0;
Bool isempty = no;
AttVal *attributes = NULL;
Node *node;
/* Lexer->token must be set on return. Nullify it for safety. */
lexer->token = NULL;
SetLexerLocus( doc, lexer );
lexer->waswhite = no;
lexer->txtstart = lexer->txtend = lexer->lexsize;
while ((c = TY_(ReadChar)(doc->docIn)) != EndOfStream)
{
if (lexer->insertspace)
{
TY_(AddCharToLexer)(lexer, ' ');
lexer->waswhite = yes;
lexer->insertspace = no;
}
if (c == 160 && (mode == Preformatted))
c = ' ';
TY_(AddCharToLexer)(lexer, c);
switch (lexer->state)
{
case LEX_CONTENT: /* element content */
/*
Discard white space if appropriate. Its cheaper
to do this here rather than in parser methods
for elements that don't have mixed content.
*/
if (TY_(IsWhite)(c) && (mode == IgnoreWhitespace)
&& lexer->lexsize == lexer->txtstart + 1)
{
--(lexer->lexsize);
lexer->waswhite = no;
SetLexerLocus( doc, lexer );
continue;
}
if (c == '<')
{
lexer->state = LEX_GT;
continue;
}
if (TY_(IsWhite)(c))
{
/* was previous character white? */
if (lexer->waswhite)
{
if (mode != Preformatted && mode != IgnoreMarkup)
{
--(lexer->lexsize);
SetLexerLocus( doc, lexer );
}
}
else /* prev character wasn't white */
{
lexer->waswhite = yes;
if (mode != Preformatted && mode != IgnoreMarkup && c != ' ')
ChangeChar(lexer, ' ');
}
continue;
}
else if (c == '&' && mode != IgnoreMarkup)
ParseEntity( doc, mode );
/* this is needed to avoid trimming trailing whitespace */
if (mode == IgnoreWhitespace)
mode = MixedContent;
lexer->waswhite = no;
continue;
case LEX_GT: /* < */
/* check for endtag */
if (c == '/')
{
if ((c = TY_(ReadChar)(doc->docIn)) == EndOfStream)
{
TY_(UngetChar)(c, doc->docIn);
continue;
}
TY_(AddCharToLexer)(lexer, c);
if (TY_(IsLetter)(c))
{
lexer->lexsize -= 3;
lexer->txtend = lexer->lexsize;
TY_(UngetChar)(c, doc->docIn);
lexer->state = LEX_ENDTAG;
lexer->lexbuf[lexer->lexsize] = '\0'; /* debug */
doc->docIn->curcol -= 2;
/* if some text before the </ return it now */
if (lexer->txtend > lexer->txtstart)
{
/* trim space character before end tag */
if (mode == IgnoreWhitespace && lexer->lexbuf[lexer->lexsize - 1] == ' ')
{
lexer->lexsize -= 1;
lexer->txtend = lexer->lexsize;
}
lexer->token = TY_(TextToken)(lexer);
#ifdef TIDY_STORE_ORIGINAL_TEXT
StoreOriginalTextInToken(doc, lexer->token, 3);
#endif
node = lexer->token;
GTDBG(doc,"text", node);
return node;
}
continue; /* no text so keep going */
}
/* otherwise treat as CDATA */
lexer->waswhite = no;
lexer->state = LEX_CONTENT;
continue;
}
if (mode == IgnoreMarkup)
{
/* otherwise treat as CDATA */
lexer->waswhite = no;
lexer->state = LEX_CONTENT;
continue;
}
/*
look out for comments, doctype or marked sections
this isn't quite right, but its getting there ...
*/
if (c == '!')
{
c = TY_(ReadChar)(doc->docIn);
if (c == '-')
{
c = TY_(ReadChar)(doc->docIn);
if (c == '-')
{
lexer->state = LEX_COMMENT; /* comment */
lexer->lexsize -= 2;
lexer->txtend = lexer->lexsize;
CondReturnTextNode(doc, 4)
lexer->txtstart = lexer->lexsize;
continue;
}
TY_(ReportError)(doc, NULL, NULL, MALFORMED_COMMENT );
}
else if (c == 'd' || c == 'D')
{
/* todo: check for complete "<!DOCTYPE" not just <!D */
uint skip = 0;
lexer->state = LEX_DOCTYPE; /* doctype */
lexer->lexsize -= 2;
lexer->txtend = lexer->lexsize;
mode = IgnoreWhitespace;
/* skip until white space or '>' */
for (;;)
{
c = TY_(ReadChar)(doc->docIn);
++skip;
if (c == EndOfStream || c == '>')
{
TY_(UngetChar)(c, doc->docIn);
break;
}
if (!TY_(IsWhite)(c))
continue;
/* and skip to end of whitespace */
for (;;)
{
c = TY_(ReadChar)(doc->docIn);
++skip;
if (c == EndOfStream || c == '>')
{
TY_(UngetChar)(c, doc->docIn);
break;
}
if (TY_(IsWhite)(c))
continue;
TY_(UngetChar)(c, doc->docIn);
break;
}
break;
}
CondReturnTextNode(doc, (skip + 3))
lexer->txtstart = lexer->lexsize;
continue;
}
else if (c == '[')
{
/* Word 2000 embeds <![if ...]> ... <![endif]> sequences */
lexer->lexsize -= 2;
lexer->state = LEX_SECTION;
lexer->txtend = lexer->lexsize;
CondReturnTextNode(doc, 2)
lexer->txtstart = lexer->lexsize;
continue;
}
/* else swallow characters up to and including next '>' */
while ((c = TY_(ReadChar)(doc->docIn)) != '>')
{
if (c == EndOfStream)
{
TY_(UngetChar)(c, doc->docIn);
break;
}
}
lexer->lexsize -= 2;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->state = LEX_CONTENT;
continue;
}
/*
processing instructions
*/
if (c == '?')
{
lexer->lexsize -= 2;
lexer->state = LEX_PROCINSTR;
lexer->txtend = lexer->lexsize;
CondReturnTextNode(doc, 2)
lexer->txtstart = lexer->lexsize;
continue;
}
/* Microsoft ASP's e.g. <% ... server-code ... %> */
if (c == '%')
{
lexer->lexsize -= 2;
lexer->state = LEX_ASP;
lexer->txtend = lexer->lexsize;
CondReturnTextNode(doc, 2)
lexer->txtstart = lexer->lexsize;
continue;
}
/* Netscapes JSTE e.g. <# ... server-code ... #> */
if (c == '#')
{
lexer->lexsize -= 2;
lexer->state = LEX_JSTE;
lexer->txtend = lexer->lexsize;
CondReturnTextNode(doc, 2)
lexer->txtstart = lexer->lexsize;
continue;
}
/* check for start tag */
if (TY_(IsLetter)(c))
{
TY_(UngetChar)(c, doc->docIn); /* push back letter */
TY_(UngetChar)('<', doc->docIn);
lexer->lexsize -= 2; /* discard "<" + letter */
lexer->txtend = lexer->lexsize;
lexer->state = LEX_STARTTAG; /* ready to read tag name */
CondReturnTextNode(doc, 2)
/* lexer->txtstart = lexer->lexsize; missing here? */
continue; /* no text so keep going */
}
/* fix for bug 762102 */
if (c == '&')
{
TY_(UngetChar)(c, doc->docIn);
--(lexer->lexsize);
}
/* otherwise treat as CDATA */
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
continue;
case LEX_ENDTAG: /* </letter */
lexer->txtstart = lexer->lexsize - 1;
doc->docIn->curcol += 2;
c = ParseTagName( doc );
lexer->token = TagToken( doc, EndTag ); /* create endtag token */
lexer->lexsize = lexer->txtend = lexer->txtstart;
/* skip to '>' */
while ( c != '>' && c != EndOfStream )
{
c = TY_(ReadChar)(doc->docIn);
}
if (c == EndOfStream)
{
TY_(FreeNode)( doc, lexer->token );
continue;
}
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
#ifdef TIDY_STORE_ORIGINAL_TEXT
StoreOriginalTextInToken(doc, lexer->token, 0); /* hmm... */
#endif
node = lexer->token;
GTDBG(doc,"endtag", node);
return node; /* the endtag token */
case LEX_STARTTAG: /* first letter of tagname */
c = TY_(ReadChar)(doc->docIn);
ChangeChar(lexer, (tmbchar)c);
lexer->txtstart = lexer->lexsize - 1; /* set txtstart to first letter */
c = ParseTagName( doc );
isempty = no;
attributes = NULL;
lexer->token = TagToken( doc, StartTag ); /* [i_a]2 'isempty' is always false, thanks to code 2 lines above */
/* parse attributes, consuming closing ">" */
if (c != '>')
{
if (c == '/')
TY_(UngetChar)(c, doc->docIn);
attributes = ParseAttrs( doc, &isempty );
}
if (isempty)
lexer->token->type = StartEndTag;
lexer->token->attributes = attributes;
lexer->lexsize = lexer->txtend = lexer->txtstart;
/* swallow newline following start tag */
/* special check needed for CRLF sequence */
/* this doesn't apply to empty elements */
/* nor to preformatted content that needs escaping */
if ((mode != Preformatted && ExpectsContent(lexer->token))
|| nodeIsBR(lexer->token) || nodeIsHR(lexer->token))
{
c = TY_(ReadChar)(doc->docIn);
if (c != '\n' && c != '\f')
TY_(UngetChar)(c, doc->docIn);
lexer->waswhite = yes; /* to swallow leading whitespace */
}
else
lexer->waswhite = no;
lexer->state = LEX_CONTENT;
if (lexer->token->tag == NULL)
{
if (mode != OtherNamespace) /* [i_a]2 only issue warning if NOT 'OtherNamespace', and tag null */
TY_(ReportFatal)( doc, NULL, lexer->token, UNKNOWN_ELEMENT );
}
else if ( !cfgBool(doc, TidyXmlTags) )
{
Node* curr = lexer->token;
TY_(ConstrainVersion)( doc, curr->tag->versions );
if ( curr->tag->versions & VERS_PROPRIETARY )
{
if ( !cfgBool(doc, TidyMakeClean) ||
( !nodeIsNOBR(curr) && !nodeIsWBR(curr) ) )
{
TY_(ReportError)(doc, NULL, curr, PROPRIETARY_ELEMENT );
if ( nodeIsLAYER(curr) )
doc->badLayout |= USING_LAYER;
else if ( nodeIsSPACER(curr) )
doc->badLayout |= USING_SPACER;
else if ( nodeIsNOBR(curr) )
doc->badLayout |= USING_NOBR;
}
}
TY_(RepairDuplicateAttributes)( doc, curr, no );
} else
TY_(RepairDuplicateAttributes)( doc, lexer->token, yes );
#ifdef TIDY_STORE_ORIGINAL_TEXT
StoreOriginalTextInToken(doc, lexer->token, 0);
#endif
node = lexer->token;
GTDBG(doc,"starttag", node);
return node; /* return start tag */
case LEX_COMMENT: /* seen <!-- so look for --> */
if (c != '-')
continue;
c = TY_(ReadChar)(doc->docIn);
TY_(AddCharToLexer)(lexer, c);
if (c != '-')
continue;
end_comment:
c = TY_(ReadChar)(doc->docIn);
if (c == '>')
{
if (badcomment)
TY_(ReportError)(doc, NULL, NULL, MALFORMED_COMMENT );
/* do not store closing -- in lexbuf */
lexer->lexsize -= 2;
lexer->txtend = lexer->lexsize;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
lexer->token = CommentToken(doc);
/* now look for a line break */
c = TY_(ReadChar)(doc->docIn);
if (c == '\n')
lexer->token->linebreak = yes;
else
TY_(UngetChar)(c, doc->docIn);
node = lexer->token;
GTDBG(doc,"comment", node);
return node;
}
/* note position of first such error in the comment */
if (!badcomment)
{
SetLexerLocus( doc, lexer );
lexer->columns -= 3;
}
badcomment++;
if ( cfgBool(doc, TidyFixComments) )
lexer->lexbuf[lexer->lexsize - 2] = '=';
/* if '-' then look for '>' to end the comment */
if (c == '-')
{
TY_(AddCharToLexer)(lexer, c);
goto end_comment;
}
/* otherwise continue to look for --> */
lexer->lexbuf[lexer->lexsize - 1] = '=';
/* http://tidy.sf.net/bug/1266647 */
TY_(AddCharToLexer)(lexer, c);
continue;
case LEX_DOCTYPE: /* seen <!d so look for '>' munging whitespace */
/* use ParseDocTypeDecl() to tokenize doctype declaration */
TY_(UngetChar)(c, doc->docIn);
lexer->lexsize -= 1;
lexer->token = ParseDocTypeDecl(doc);
lexer->txtend = lexer->lexsize;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
/* make a note of the version named by the 1st doctype */
if (lexer->doctype == VERS_UNKNOWN && lexer->token && !cfgBool(doc, TidyXmlTags))
{
lexer->doctype = FindGivenVersion(doc, lexer->token);
if (lexer->doctype != VERS_HTML5)
{
/*\
* Back to legacy HTML4 mode for -
* Issue #167 & #169 - TidyTag_A
* Issue #196 - TidyTag_CAPTION
* others?
\*/
TY_(AdjustTags)(doc); /* Dynamically modify the tags table */
}
}
node = lexer->token;
GTDBG(doc,"doctype", node);
return node;
case LEX_PROCINSTR: /* seen <? so look for '>' */
/* check for PHP preprocessor instructions <?php ... ?> */
if (lexer->lexsize - lexer->txtstart == 3)
{
if (TY_(tmbstrncmp)(lexer->lexbuf + lexer->txtstart, "php", 3) == 0)
{
lexer->state = LEX_PHP;
continue;
}
}
if (lexer->lexsize - lexer->txtstart == 4)
{
if (TY_(tmbstrncmp)(lexer->lexbuf + lexer->txtstart, "xml", 3) == 0 &&
TY_(IsWhite)(lexer->lexbuf[lexer->txtstart + 3]))
{
lexer->state = LEX_XMLDECL;
attributes = NULL;
continue;
}
}
if (cfgBool(doc, TidyXmlPIs) || lexer->isvoyager) /* insist on ?> as terminator */
{
if (c != '?')
continue;
/* now look for '>' */
c = TY_(ReadChar)(doc->docIn);
if (c == EndOfStream)
{
TY_(ReportError)(doc, NULL, NULL, UNEXPECTED_END_OF_FILE );
TY_(UngetChar)(c, doc->docIn);
continue;
}
TY_(AddCharToLexer)(lexer, c);
}
if (c != '>')
continue;
lexer->lexsize -= 1;
if (lexer->lexsize)
{
uint i;
Bool closed;
for (i = 0; i < lexer->lexsize - lexer->txtstart &&
!TY_(IsWhite)(lexer->lexbuf[i + lexer->txtstart]); ++i)
/**/;
closed = lexer->lexbuf[lexer->lexsize - 1] == '?';
if (closed)
lexer->lexsize -= 1;
lexer->txtstart += i;
lexer->txtend = lexer->lexsize;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->token = PIToken(doc);
lexer->token->closed = closed;
lexer->token->element = TY_(tmbstrndup)(doc->allocator,
lexer->lexbuf +
lexer->txtstart - i, i);
}
else
{
lexer->txtend = lexer->lexsize;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->token = PIToken(doc);
}
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
node = lexer->token;
GTDBG(doc,"procinstr", node);
return node;
case LEX_ASP: /* seen <% so look for "%>" */
if (c != '%')
continue;
/* now look for '>' */
c = TY_(ReadChar)(doc->docIn);
if (c != '>')
{
TY_(UngetChar)(c, doc->docIn);
continue;
}
lexer->lexsize -= 1;
lexer->txtend = lexer->lexsize;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
lexer->token = AspToken(doc);
node = lexer->token;
GTDBG(doc,"ASP", node);
return node; /* the endtag token */
case LEX_JSTE: /* seen <# so look for "#>" */
if (c != '#')
continue;
/* now look for '>' */
c = TY_(ReadChar)(doc->docIn);
if (c != '>')
{
TY_(UngetChar)(c, doc->docIn);
continue;
}
lexer->lexsize -= 1;
lexer->txtend = lexer->lexsize;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
lexer->token = JsteToken(doc);
node = lexer->token;
GTDBG(doc,"JSTE", node);
return node; /* the JSTE token */
case LEX_PHP: /* seen "<?php" so look for "?>" */
if (c != '?')
continue;
/* now look for '>' */
c = TY_(ReadChar)(doc->docIn);
if (c != '>')
{
TY_(UngetChar)(c, doc->docIn);
continue;
}
lexer->lexsize -= 1;
lexer->txtend = lexer->lexsize;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
lexer->token = PhpToken(doc);
node = lexer->token;
GTDBG(doc,"PHP", node);
return node; /* the PHP token */
case LEX_XMLDECL: /* seen "<?xml" so look for "?>" */
if (TY_(IsWhite)(c) && c != '?')
continue;
/* get pseudo-attribute */
if (c != '?')
{
tmbstr name;
Node *asp, *php;
AttVal *av = NULL;
int pdelim = 0;
isempty = no;
TY_(UngetChar)(c, doc->docIn);
name = ParseAttribute( doc, &isempty, &asp, &php );
if (!name)
{
/* fix for http://tidy.sf.net/bug/788031 */
lexer->lexsize -= 1;
lexer->txtend = lexer->txtstart;
lexer->lexbuf[lexer->txtend] = '\0';
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
lexer->token = XmlDeclToken(doc);
lexer->token->attributes = attributes;
node = lexer->token;
GTDBG(doc,"xml", node);
return node; /* the xml token */
}
av = TY_(NewAttribute)(doc);
av->attribute = name;
av->value = ParseValue( doc, name, yes, &isempty, &pdelim );
av->delim = pdelim;
av->dict = TY_(FindAttribute)( doc, av );
AddAttrToList( &attributes, av );
/* continue; */
}
/* now look for '>' */
c = TY_(ReadChar)(doc->docIn);
if (c != '>')
{
TY_(UngetChar)(c, doc->docIn);
continue;
}
lexer->lexsize -= 1;
lexer->txtend = lexer->txtstart;
lexer->lexbuf[lexer->txtend] = '\0';
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
lexer->token = XmlDeclToken(doc);
lexer->token->attributes = attributes;
node = lexer->token;
GTDBG(doc,"XML", node);
return node; /* the XML token */
case LEX_SECTION: /* seen "<![" so look for "]>" */
if (c == '[')
{
if (lexer->lexsize == (lexer->txtstart + 6) &&
TY_(tmbstrncmp)(lexer->lexbuf+lexer->txtstart, "CDATA[", 6) == 0)
{
lexer->state = LEX_CDATA;
lexer->lexsize -= 6;
continue;
}
}
if (c != ']')
continue;
/* now look for '>' */
c = TY_(ReadChar)(doc->docIn);
lexdump = 1;
if (c != '>')
{
/* Issue #153 - can also be ]'-->' */
if (c == '-')
{
c = TY_(ReadChar)(doc->docIn);
if (c == '-')
{
c = TY_(ReadChar)(doc->docIn);
if (c != '>')
{
TY_(UngetChar)(c, doc->docIn);
TY_(UngetChar)('-', doc->docIn);
TY_(UngetChar)('-', doc->docIn);
continue;
}
/* this failed!
TY_(AddCharToLexer)(lexer, '-'); TY_(AddCharToLexer)(lexer, '-'); lexdump = 0;
got output <![endif]--]> - needs furhter fix in pprint section output
*/
}
else
{
TY_(UngetChar)(c, doc->docIn);
TY_(UngetChar)('-', doc->docIn);
continue;
}
}
else
{
TY_(UngetChar)(c, doc->docIn);
continue;
}
}
lexer->lexsize -= lexdump;
lexer->txtend = lexer->lexsize;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
lexer->token = SectionToken(doc);
node = lexer->token;
GTDBG(doc,"SECTION", node);
return node; /* the SECTION token */
case LEX_CDATA: /* seen "<![CDATA[" so look for "]]>" */
if (c != ']')
continue;
/* now look for ']' */
c = TY_(ReadChar)(doc->docIn);
if (c != ']')
{
TY_(UngetChar)(c, doc->docIn);
continue;
}
/* now look for '>' */
c = TY_(ReadChar)(doc->docIn);
if (c != '>')
{
TY_(UngetChar)(c, doc->docIn);
TY_(UngetChar)(']', doc->docIn);
continue;
}
lexer->lexsize -= 1;
lexer->txtend = lexer->lexsize;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
lexer->token = CDATAToken(doc);
node = lexer->token;
GTDBG(doc,"CDATA", node);
return node; /* the CDATA token */
}
}
if (lexer->state == LEX_CONTENT) /* text string */
{
lexer->txtend = lexer->lexsize;
if (lexer->txtend > lexer->txtstart)
{
TY_(UngetChar)(c, doc->docIn);
if (lexer->lexbuf[lexer->lexsize - 1] == ' ')
{
lexer->lexsize -= 1;
lexer->txtend = lexer->lexsize;
}
lexer->token = TY_(TextToken)(lexer);
#ifdef TIDY_STORE_ORIGINAL_TEXT
StoreOriginalTextInToken(doc, lexer->token, 0); /* ? */
#endif
node = lexer->token;
GTDBG(doc,"textstring", node);
return node; /* the textstring token */
}
}
else if (lexer->state == LEX_COMMENT) /* comment */
{
if (c == EndOfStream)
TY_(ReportError)(doc, NULL, NULL, MALFORMED_COMMENT );
lexer->txtend = lexer->lexsize;
lexer->lexbuf[lexer->lexsize] = '\0';
lexer->state = LEX_CONTENT;
lexer->waswhite = no;
lexer->token = CommentToken(doc);
node = lexer->token;
GTDBG(doc,"COMMENT", node);
return node; /* the COMMENT token */
}
#if !defined(NDEBUG) && defined(_MSC_VER)
SPRTF("Returning NULL...\n");
#endif
return NULL;
}
static void MapStr( ctmbstr str, uint code )
{
while ( *str )
{
uint i = (byte) *str++;
lexmap[i] |= code;
}
}
void TY_(InitMap)(void)
{
MapStr("\r\n\f", newline|white);
MapStr(" \t", white);
MapStr("-.:_", namechar);
MapStr("0123456789", digit|digithex|namechar);
MapStr("abcdefghijklmnopqrstuvwxyz", lowercase|letter|namechar);
MapStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", uppercase|letter|namechar);
MapStr("abcdefABCDEF", digithex);
}
/*
parser for ASP within start tags
Some people use ASP for to customize attributes
Tidy isn't really well suited to dealing with ASP
This is a workaround for attributes, but won't
deal with the case where the ASP is used to tailor
the attribute value. Here is an example of a work
around for using ASP in attribute values:
href='<%=rsSchool.Fields("ID").Value%>'
where the ASP that generates the attribute value
is masked from Tidy by the quotemarks.
*/
static Node *ParseAsp( TidyDocImpl* doc )
{
Lexer* lexer = doc->lexer;
uint c;
Node *asp = NULL;
lexer->txtstart = lexer->lexsize;
for (;;)
{
if ((c = TY_(ReadChar)(doc->docIn)) == EndOfStream)
break;
TY_(AddCharToLexer)(lexer, c);
if (c != '%')
continue;
if ((c = TY_(ReadChar)(doc->docIn)) == EndOfStream)
break;
TY_(AddCharToLexer)(lexer, c);
if (c == '>')
{
lexer->lexsize -= 2;
break;
}
}
lexer->txtend = lexer->lexsize;
if (lexer->txtend > lexer->txtstart)
asp = AspToken(doc);
lexer->txtstart = lexer->txtend;
return asp;
}
/*
PHP is like ASP but is based upon XML
processing instructions, e.g. <?php ... ?>
*/
static Node *ParsePhp( TidyDocImpl* doc )
{
Lexer* lexer = doc->lexer;
uint c;
Node *php = NULL;
lexer->txtstart = lexer->lexsize;
for (;;)
{
if ((c = TY_(ReadChar)(doc->docIn)) == EndOfStream)
break;
TY_(AddCharToLexer)(lexer, c);
if (c != '?')
continue;
if ((c = TY_(ReadChar)(doc->docIn)) == EndOfStream)
break;
TY_(AddCharToLexer)(lexer, c);
if (c == '>')
{
lexer->lexsize -= 2;
break;
}
}
lexer->txtend = lexer->lexsize;
if (lexer->txtend > lexer->txtstart)
php = PhpToken(doc);
lexer->txtstart = lexer->txtend;
return php;
}
/* consumes the '>' terminating start tags */
static tmbstr ParseAttribute( TidyDocImpl* doc, Bool *isempty,
Node **asp, Node **php)
{
Lexer* lexer = doc->lexer;
int start, len = 0;
tmbstr attr = NULL;
uint c, lastc;
*asp = NULL; /* clear asp pointer */
*php = NULL; /* clear php pointer */
/* skip white space before the attribute */
for (;;)
{
c = TY_(ReadChar)( doc->docIn );
if (c == '/')
{
c = TY_(ReadChar)( doc->docIn );
if (c == '>')
{
*isempty = yes;
return NULL;
}
TY_(UngetChar)(c, doc->docIn);
c = '/';
break;
}
if (c == '>')
return NULL;
if (c =='<')
{
c = TY_(ReadChar)(doc->docIn);
if (c == '%')
{
*asp = ParseAsp( doc );
return NULL;
}
else if (c == '?')
{
*php = ParsePhp( doc );
return NULL;
}
TY_(UngetChar)(c, doc->docIn);
TY_(UngetChar)('<', doc->docIn);
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_GT );
return NULL;
}
if (c == '=')
{
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_EQUALSIGN );
continue;
}
if (c == '"' || c == '\'')
{
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_QUOTEMARK );
continue;
}
if (c == EndOfStream)
{
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_END_OF_FILE_ATTR );
TY_(UngetChar)(c, doc->docIn);
return NULL;
}
if (!TY_(IsWhite)(c))
break;
}
start = lexer->lexsize;
lastc = c;
for (;;)
{
/* but push back '=' for parseValue() */
if (c == '=' || c == '>')
{
TY_(UngetChar)(c, doc->docIn);
break;
}
if (c == '<' || c == EndOfStream)
{
TY_(UngetChar)(c, doc->docIn);
break;
}
if (lastc == '-' && (c == '"' || c == '\''))
{
lexer->lexsize--;
--len;
TY_(UngetChar)(c, doc->docIn);
break;
}
if (TY_(IsWhite)(c))
break;
/* what should be done about non-namechar characters? */
/* currently these are incorporated into the attr name */
if ( !cfgBool(doc, TidyXmlTags) && TY_(IsUpper)(c) )
c = TY_(ToLower)(c);
TY_(AddCharToLexer)( lexer, c );
lastc = c;
c = TY_(ReadChar)(doc->docIn);
}
/* handle attribute names with multibyte chars */
len = lexer->lexsize - start;
attr = (len > 0 ? TY_(tmbstrndup)(doc->allocator,
lexer->lexbuf+start, len) : NULL);
lexer->lexsize = start;
return attr;
}
/*
invoked when < is seen in place of attribute value
but terminates on whitespace if not ASP, PHP or Tango
this routine recognizes ' and " quoted strings
*/
static int ParseServerInstruction( TidyDocImpl* doc )
{
Lexer* lexer = doc->lexer;
uint c;
int delim = '"';
Bool isrule = no;
c = TY_(ReadChar)(doc->docIn);
TY_(AddCharToLexer)(lexer, c);
/* check for ASP, PHP or Tango */
if (c == '%' || c == '?' || c == '@')
isrule = yes;
for (;;)
{
c = TY_(ReadChar)(doc->docIn);
if (c == EndOfStream)
break;
if (c == '>')
{
if (isrule)
TY_(AddCharToLexer)(lexer, c);
else
TY_(UngetChar)(c, doc->docIn);
break;
}
/* if not recognized as ASP, PHP or Tango */
/* then also finish value on whitespace */
if (!isrule)
{
if (TY_(IsWhite)(c))
break;
}
TY_(AddCharToLexer)(lexer, c);
if (c == '"')
{
do
{
c = TY_(ReadChar)(doc->docIn);
if (c == EndOfStream) /* #427840 - fix by Terry Teague 30 Jun 01 */
{
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_END_OF_FILE_ATTR );
TY_(UngetChar)(c, doc->docIn);
return 0;
}
if (c == '>') /* #427840 - fix by Terry Teague 30 Jun 01 */
{
TY_(UngetChar)(c, doc->docIn);
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_GT );
return 0;
}
TY_(AddCharToLexer)(lexer, c);
}
while (c != '"');
delim = '\'';
continue;
}
if (c == '\'')
{
do
{
c = TY_(ReadChar)(doc->docIn);
if (c == EndOfStream) /* #427840 - fix by Terry Teague 30 Jun 01 */
{
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_END_OF_FILE_ATTR );
TY_(UngetChar)(c, doc->docIn);
return 0;
}
if (c == '>') /* #427840 - fix by Terry Teague 30 Jun 01 */
{
TY_(UngetChar)(c, doc->docIn);
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_GT );
return 0;
}
TY_(AddCharToLexer)(lexer, c);
}
while (c != '\'');
}
}
return delim;
}
/* values start with "=" or " = " etc. */
/* doesn't consume the ">" at end of start tag */
static tmbstr ParseValue( TidyDocImpl* doc, ctmbstr name,
Bool foldCase, Bool *isempty, int *pdelim)
{
Lexer* lexer = doc->lexer;
int len = 0, start;
Bool seen_gt = no;
Bool munge = yes;
uint c, lastc, delim, quotewarning;
tmbstr value;
delim = (tmbchar) 0;
*pdelim = '"';
/*
Henry Zrepa reports that some folk are using the
embed element with script attributes where newlines
are significant and must be preserved
*/
if ( cfgBool(doc, TidyLiteralAttribs) )
munge = no;
/* skip white space before the '=' */
for (;;)
{
c = TY_(ReadChar)(doc->docIn);
if (c == EndOfStream)
{
TY_(UngetChar)(c, doc->docIn);
break;
}
if (!TY_(IsWhite)(c))
break;
}
/*
c should be '=' if there is a value
other legal possibilities are white
space, '/' and '>'
*/
if (c != '=' && c != '"' && c != '\'')
{
TY_(UngetChar)(c, doc->docIn);
return NULL;
}
/* skip white space after '=' */
for (;;)
{
c = TY_(ReadChar)(doc->docIn);
if (c == EndOfStream)
{
TY_(UngetChar)(c, doc->docIn);
break;
}
if (!TY_(IsWhite)(c))
break;
}
/* check for quote marks */
if (c == '"' || c == '\'')
delim = c;
else if (c == '<')
{
start = lexer->lexsize;
TY_(AddCharToLexer)(lexer, c);
*pdelim = ParseServerInstruction( doc );
len = lexer->lexsize - start;
lexer->lexsize = start;
return (len > 0 ? TY_(tmbstrndup)(doc->allocator,
lexer->lexbuf+start, len) : NULL);
}
else
TY_(UngetChar)(c, doc->docIn);
/*
and read the value string
check for quote mark if needed
*/
quotewarning = 0;
start = lexer->lexsize;
c = '\0';
for (;;)
{
lastc = c; /* track last character */
c = TY_(ReadChar)(doc->docIn);
if (c == EndOfStream)
{
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_END_OF_FILE_ATTR );
TY_(UngetChar)(c, doc->docIn);
break;
}
if (delim == (tmbchar)0)
{
if (c == '>')
{
TY_(UngetChar)(c, doc->docIn);
break;
}
if (c == '"' || c == '\'')
{
uint q = c;
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_QUOTEMARK );
/* handle <input onclick=s("btn1")> and <a title=foo""">...</a> */
/* this doesn't handle <a title=foo"/> which browsers treat as */
/* 'foo"/' nor <a title=foo" /> which browser treat as 'foo"' */
c = TY_(ReadChar)(doc->docIn);
if (c == '>')
{
TY_(AddCharToLexer)(lexer, q);
TY_(UngetChar)(c, doc->docIn);
break;
}
else
{
TY_(UngetChar)(c, doc->docIn);
c = q;
}
}
if (c == '<')
{
TY_(UngetChar)(c, doc->docIn);
c = '>';
TY_(UngetChar)(c, doc->docIn);
TY_(ReportAttrError)( doc, lexer->token, NULL, UNEXPECTED_GT );
break;
}
/*
For cases like <br clear=all/> need to avoid treating /> as
part of the attribute value, however care is needed to avoid
so treating <a href=http://www.acme.com/> in this way, which
would map the <a> tag to <a href="http://www.acme.com"/>
*/
if (c == '/')
{
/* peek ahead in case of /> */
c = TY_(ReadChar)(doc->docIn);
if ( c == '>' && !TY_(IsUrl)(doc, name) )
{
*isempty = yes;
TY_(UngetChar)(c, doc->docIn);
break;
}
/* unget peeked character */
TY_(UngetChar)(c, doc->docIn);
c = '/';
}
}
else /* delim is '\'' or '"' */
{
if (c == delim)
break;
if (c == '\n' || c == '<' || c == '>')
++quotewarning;
if (c == '>')
seen_gt = yes;
}
if (c == '&')
{
TY_(AddCharToLexer)(lexer, c);
ParseEntity( doc, IgnoreWhitespace );
if (lexer->lexbuf[lexer->lexsize - 1] == '\n' && munge)
ChangeChar(lexer, ' ');
continue;
}
/*
kludge for JavaScript attribute values
with line continuations in string literals
*/
if (c == '\\')
{
c = TY_(ReadChar)(doc->docIn);
if (c != '\n')
{
TY_(UngetChar)(c, doc->docIn);
c = '\\';
}
}
if (TY_(IsWhite)(c))
{
if ( delim == 0 )
break;
if (munge)
{
/* discard line breaks in quoted URLs */
/* #438650 - fix by Randy Waki */
if ( c == '\n' && TY_(IsUrl)(doc, name) )
{
/* warn that we discard this newline */
TY_(ReportAttrError)( doc, lexer->token, NULL, NEWLINE_IN_URI);
continue;
}
c = ' ';
if (lastc == ' ')
{
if (TY_(IsUrl)(doc, name) )
TY_(ReportAttrError)( doc, lexer->token, NULL, WHITE_IN_URI);
continue;
}
}
}
else if (foldCase && TY_(IsUpper)(c))
c = TY_(ToLower)(c);
TY_(AddCharToLexer)(lexer, c);
}
if (quotewarning > 10 && seen_gt && munge)
{
/*
there is almost certainly a missing trailing quote mark
as we have see too many newlines, < or > characters.
an exception is made for Javascript attributes and the
javascript URL scheme which may legitimately include < and >,
and for attributes starting with "<xml " as generated by
Microsoft Office.
*/
if ( !TY_(IsScript)(doc, name) &&
!(TY_(IsUrl)(doc, name) && TY_(tmbstrncmp)(lexer->lexbuf+start, "javascript:", 11) == 0) &&
!(TY_(tmbstrncmp)(lexer->lexbuf+start, "<xml ", 5) == 0)
)
TY_(ReportFatal)( doc, NULL, NULL, SUSPECTED_MISSING_QUOTE );
}
len = lexer->lexsize - start;
lexer->lexsize = start;
if (len > 0 || delim)
{
/* ignore leading and trailing white space for all but title, alt, value */
/* and prompts attributes unless --literal-attributes is set to yes */
/* #994841 - Whitespace is removed from value attributes */
if (munge &&
TY_(tmbstrcasecmp)(name, "alt") &&
TY_(tmbstrcasecmp)(name, "title") &&
TY_(tmbstrcasecmp)(name, "value") &&
TY_(tmbstrcasecmp)(name, "prompt"))
{
while (TY_(IsWhite)(lexer->lexbuf[start+len-1]))
--len;
while (TY_(IsWhite)(lexer->lexbuf[start]) && start < len)
{
++start;
--len;
}
}
value = TY_(tmbstrndup)(doc->allocator, lexer->lexbuf + start, len);
}
else
value = NULL;
/* note delimiter if given */
*pdelim = (delim ? delim : '"');
return value;
}
/* attr must be non-NULL */
static Bool IsValidAttrName( ctmbstr attr )
{
uint i, c = attr[0];
/* first character should be a letter */
if (!TY_(IsLetter)(c))
return no;
/* remaining characters should be namechars */
for( i = 1; i < TY_(tmbstrlen)(attr); i++)
{
c = attr[i];
if (TY_(IsNamechar)(c))
continue;
return no;
}
return yes;
}
/* create a new attribute */
AttVal *TY_(NewAttribute)( TidyDocImpl* doc )
{
AttVal *av = (AttVal*) TidyDocAlloc( doc, sizeof(AttVal) );
TidyClearMemory( av, sizeof(AttVal) );
return av;
}
/* create a new attribute with given name and value */
AttVal* TY_(NewAttributeEx)( TidyDocImpl* doc, ctmbstr name, ctmbstr value,
int delim )
{
AttVal *av = TY_(NewAttribute)(doc);
av->attribute = TY_(tmbstrdup)(doc->allocator, name);
av->value = TY_(tmbstrdup)(doc->allocator, value);
av->delim = delim;
av->dict = TY_(FindAttribute)( doc, av );
return av;
}
static void AddAttrToList( AttVal** list, AttVal* av )
{
if ( *list == NULL )
*list = av;
else
{
AttVal* here = *list;
while ( here->next )
here = here->next;
here->next = av;
}
}
void TY_(InsertAttributeAtEnd)( Node *node, AttVal *av )
{
AddAttrToList(&node->attributes, av);
}
void TY_(InsertAttributeAtStart)( Node *node, AttVal *av )
{
av->next = node->attributes;
node->attributes = av;
}
/* swallows closing '>' */
static AttVal* ParseAttrs( TidyDocImpl* doc, Bool *isempty )
{
Lexer* lexer = doc->lexer;
AttVal *av, *list;
tmbstr value;
int delim;
Node *asp, *php;
list = NULL;
while ( !EndOfInput(doc) )
{
tmbstr attribute = ParseAttribute( doc, isempty, &asp, &php );
if (attribute == NULL)
{
/* check if attributes are created by ASP markup */
if (asp)
{
av = TY_(NewAttribute)(doc);
av->asp = asp;
AddAttrToList( &list, av );
continue;
}
/* check if attributes are created by PHP markup */
if (php)
{
av = TY_(NewAttribute)(doc);
av->php = php;
AddAttrToList( &list, av );
continue;
}
break;
}
value = ParseValue( doc, attribute, no, isempty, &delim );
if (attribute && (IsValidAttrName(attribute) ||
(cfgBool(doc, TidyXmlTags) && IsValidXMLAttrName(attribute))))
{
av = TY_(NewAttribute)(doc);
av->delim = delim;
av->attribute = attribute;
av->value = value;
av->dict = TY_(FindAttribute)( doc, av );
AddAttrToList( &list, av );
}
else
{
av = TY_(NewAttribute)(doc);
av->attribute = attribute;
av->value = value;
if (LastChar(attribute) == '"')
TY_(ReportAttrError)( doc, lexer->token, av, MISSING_QUOTEMARK);
else if (value == NULL)
TY_(ReportAttrError)(doc, lexer->token, av, MISSING_ATTR_VALUE);
else
TY_(ReportAttrError)(doc, lexer->token, av, INVALID_ATTRIBUTE);
TY_(FreeAttribute)( doc, av );
}
}
return list;
}
/*
Returns document type declarations like
<!DOCTYPE foo PUBLIC "fpi" "sysid">
<!DOCTYPE bar SYSTEM "sysid">
<!DOCTYPE baz [ <!ENTITY ouml "ö"> ]>
as
<foo PUBLIC="fpi" SYSTEM="sysid" />
<bar SYSTEM="sysid" />
<baz> <!ENTITY ouml "&#246"> </baz>
*/
static Node *ParseDocTypeDecl(TidyDocImpl* doc)
{
Lexer *lexer = doc->lexer;
int start = lexer->lexsize;
ParseDocTypeDeclState state = DT_DOCTYPENAME;
uint c;
uint delim = 0;
Bool hasfpi = yes;
Node* node = TY_(NewNode)(lexer->allocator, lexer);
node->type = DocTypeTag;
node->start = lexer->txtstart;
node->end = lexer->txtend;
lexer->waswhite = no;
/* todo: reset lexer->lexsize when appropriate to avoid wasting memory */
while ((c = TY_(ReadChar)(doc->docIn)) != EndOfStream)
{
/* convert newlines to spaces */
if (state != DT_INTSUBSET)
c = c == '\n' ? ' ' : c;
/* convert white-space sequences to single space character */
if (TY_(IsWhite)(c) && state != DT_INTSUBSET)
{
if (!lexer->waswhite)
{
TY_(AddCharToLexer)(lexer, c);
lexer->waswhite = yes;
}
else
{
/* discard space */
continue;
}
}
else
{
TY_(AddCharToLexer)(lexer, c);
lexer->waswhite = no;
}
switch(state)
{
case DT_INTERMEDIATE:
/* determine what's next */
if (TY_(ToUpper)(c) == 'P' || TY_(ToUpper)(c) == 'S')
{
start = lexer->lexsize - 1;
state = DT_PUBLICSYSTEM;
continue;
}
else if (c == '[')
{
start = lexer->lexsize;
state = DT_INTSUBSET;
continue;
}
else if (c == '\'' || c == '"')
{
start = lexer->lexsize;
delim = c;
state = DT_QUOTEDSTRING;
continue;
}
else if (c == '>')
{
AttVal* si;
node->end = --(lexer->lexsize);
si = TY_(GetAttrByName)(node, "SYSTEM");
if (si)
TY_(CheckUrl)(doc, node, si);
if (!node->element || !IsValidXMLElemName(node->element))
{
TY_(ReportError)(doc, NULL, NULL, MALFORMED_DOCTYPE);
TY_(FreeNode)(doc, node);
return NULL;
}
#ifdef TIDY_STORE_ORIGINAL_TEXT
StoreOriginalTextInToken(doc, node, 0);
#endif
return node;
}
else
{
/* error */
}
break;
case DT_DOCTYPENAME:
/* read document type name */
if (TY_(IsWhite)(c) || c == '>' || c == '[')
{
node->element = TY_(tmbstrndup)(doc->allocator,
lexer->lexbuf + start,
lexer->lexsize - start - 1);
if (c == '>' || c == '[')
{
--(lexer->lexsize);
TY_(UngetChar)(c, doc->docIn);
}
state = DT_INTERMEDIATE;
continue;
}
break;
case DT_PUBLICSYSTEM:
/* read PUBLIC/SYSTEM */
if (TY_(IsWhite)(c) || c == '>')
{
char *attname = TY_(tmbstrndup)(doc->allocator,
lexer->lexbuf + start,
lexer->lexsize - start - 1);
hasfpi = !(TY_(tmbstrcasecmp)(attname, "SYSTEM") == 0);
TidyDocFree(doc, attname);
/* todo: report an error if SYSTEM/PUBLIC not uppercase */
if (c == '>')
{
--(lexer->lexsize);
TY_(UngetChar)(c, doc->docIn);
}
state = DT_INTERMEDIATE;
continue;
}
break;
case DT_QUOTEDSTRING:
/* read quoted string */
if (c == delim)
{
char *value = TY_(tmbstrndup)(doc->allocator,
lexer->lexbuf + start,
lexer->lexsize - start - 1);
AttVal* att = TY_(AddAttribute)(doc, node, hasfpi ? "PUBLIC" : "SYSTEM", value);
TidyDocFree(doc, value);
att->delim = delim;
hasfpi = no;
state = DT_INTERMEDIATE;
delim = 0;
continue;
}
break;
case DT_INTSUBSET:
/* read internal subset */
if (c == ']')
{
Node* subset;
lexer->txtstart = start;
lexer->txtend = lexer->lexsize - 1;
subset = TY_(TextToken)(lexer);
TY_(InsertNodeAtEnd)(node, subset);
state = DT_INTERMEDIATE;
}
break;
}
}
/* document type declaration not finished */
TY_(ReportError)(doc, NULL, NULL, MALFORMED_DOCTYPE);
TY_(FreeNode)(doc, node);
return NULL;
}
/*
* local variables:
* mode: c
* indent-tabs-mode: nil
* c-basic-offset: 4
* eval: (c-set-offset 'substatement-open 0)
* end:
*/
|