summaryrefslogtreecommitdiffstats
path: root/configure
Commit message (Expand)AuthorAgeFilesLines
* Simplify bootstrap script source dir detectionBrad King2009-09-251-2/+2
* ENH: Configure script now just invokes bootstrap script.Brad King2003-06-231-1707/+2
* BUG: Rename Bootstrap directory to Bootstrap.cmk, so that on platforms such a...Andy Cedilnik2003-04-171-5/+5
* ENH: Centralized setting of CMake version number to top-level CMake listfile.Brad King2003-02-141-19/+43
* Improve bootstrapping on Unix, so that it bootstraps in the subdirectory. Thi...Andy Cedilnik2002-09-131-85/+111
* ERR: Went back to old-style test for -LANG:std on the SGI.Brad King2002-09-101-60/+24
* ERR: AC_SUBST replaced with AC_DEFINE. Needed to get configure script to cor...Brad King2002-09-101-22/+26
* BUG: Missing AC_SUBST commands added.Brad King2002-09-101-10/+18
* ENH: Improved configure test implementations by using AC_TRY_COMPILE.Brad King2002-09-061-78/+291
* try another flag for the dec compiler...Bill Hoffman2002-08-231-3/+3
* BUG: fix for OSF compiler to use ansi mode for streamsBill Hoffman2002-08-221-6/+18
* ERR: Ran autoconf on corresponding fixes to configure.in and cxxconfigure.in.Brad King2002-06-211-14/+12
* ENH: Re-ran autoconf to include changes to corresponding configure input. Th...Brad King2002-06-191-4/+23
* Revert to make it work againAndy Cedilnik2002-05-021-4/+4
* Improve bootstrap on UNIX. Now it bootstraps into a separate directory.Andy Cedilnik2002-05-021-4/+4
* ENH: handle spaces in paths for cygwin bootstrapKen Martin2002-04-181-25/+35
* ENH: do not use O2 g as default flagsBill Hoffman2002-02-141-23/+34
* check for LANG:stdBill Hoffman2001-12-191-5/+13
* -LANG:std should be default on sgi for ansi CXX FlagsBill Hoffman2001-12-191-13/+5
* remove template flags from cmake, no ptused, or -instance=staticBill Hoffman2001-11-071-29/+4
* ENH: pass prefix from configure into cmakeBill Hoffman2001-09-271-2/+2
* *** empty log message ***Bill Hoffman2001-08-271-3/+3
* ENH: clean up sgi build and use non-broken autoconfBill Hoffman2001-08-271-5/+8
* *** empty log message ***Bill Hoffman2001-08-271-2252/+772
* auto detect lang:std:Bill Hoffman2001-08-241-19/+18
* auto detect lang:std:Bill Hoffman2001-08-241-18/+19
* auto detect lang:std:Bill Hoffman2001-08-241-3/+3
* auto detect lang:std:Bill Hoffman2001-08-241-3/+3
* auto detect lang:std:Bill Hoffman2001-08-241-18/+44
* auto detect lang:std:Bill Hoffman2001-08-241-1/+1
* BUG: fix for SGI Native compilerBill Hoffman2001-08-241-764/+2254
* ENH: big change, only allow commands access to the cache via the cmMakefile c...Bill Hoffman2001-08-081-1/+20
* ENH: run make dependBill Hoffman2001-07-061-8/+4
* ENH: various fixes to allow bootstrap on sunos with CCBill Hoffman2001-06-281-0/+2
* ENH: fix install for cygwin, build cmake from configureBill Hoffman2001-06-271-0/+49
* better install supportKen Martin2001-06-211-94/+2
* use cxxflags in test buildsBill Hoffman2001-05-221-4/+3
* clean up for build on its ownBill Hoffman2001-05-211-1973/+23
* out of place cmakeKen Martin2001-05-211-0/+3382
='#n465'>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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>


#include <qtextdocument.h>
#include <qtextdocumentfragment.h>
#include <qtexttable.h>
#include <qtextlist.h>
#include <qdebug.h>
#include <private/qtextdocument_p.h>


#include <qtextcursor.h>

QT_FORWARD_DECLARE_CLASS(QTextDocument)

//TESTED_CLASS=
//TESTED_FILES=gui/text/qtextdocumentfragment.h gui/text/qtextdocumentfragment.cpp gui/text/qtexthtmlparser.cpp gui/text/qtexthtmlparser_p.h

class tst_QTextDocumentFragment : public QObject
{
    Q_OBJECT

public:
    tst_QTextDocumentFragment();
    ~tst_QTextDocumentFragment();

public slots:
    void init();
    void cleanup();
private slots:
    void listCopying();
    void listZeroCopying();
    void listCopying2();
    void tableCopying();
    void tableCopyingWithColSpans();
    void tableColSpanAndWidth();
    void tableImport();
    void tableImport2();
    void tableImport3();
    void tableImport4();
    void tableImport5();
    void textCopy();
    void copyWholeDocument();
    void title();
    void html_listIndents1();
    void html_listIndents2();
    void html_listIndents3();
    void html_listIndents4();
    void html_listIndents5();
    void html_listIndents6();
    void blockCharFormat();
    void blockCharFormatCopied();
    void initialBlock();
    void clone();
    void dontRemoveInitialBlockIfItHoldsObjectIndexedCharFormat();
    void dosLineFeed();
    void unorderedListEnumeration();
    void resetHasBlockAfterClosedBlockTags();
    void ignoreStyleTags();
    void hrefAnchor();
    void namedAnchorFragments();
    void namedAnchorFragments2();
    void namedAnchorFragments3();
    void dontInheritAlignmentInTables();
    void cellBlockCount();
    void cellBlockCount2();
    void emptyTable();
    void emptyTable2();
    void emptyTable3();
    void doubleRowClose();
    void mayNotHaveChildren();
    void inheritAlignment();
    void dontEmitEmptyNodeWhenEmptyTagIsFollowedByCloseTag();
    void toPlainText();
    void copyTableRow();
    void copyTableColumn();
    void copySubTable();
    void html_textDecoration();
    void html_infiniteLoop();
    void html_blockIndent();
    void html_listIndent();
    void html_whitespace();
    void html_whitespace_data();
    void html_qt3Whitespace();
    void html_qt3WhitespaceWithFragments();
    void html_qt3WhitespaceAfterTags();
    void html_listStart1();
    void html_listStart2();
    void html_cssMargin();
    void html_hexEntities();
    void html_decEntities();
    void html_thCentered();
    void orderedListNumbering();
    void html_blockAfterList();
    void html_subAndSuperScript();
    void html_cssColors();
    void obeyFragmentMarkersInImport();
    void whitespaceWithFragmentMarkers();
    void html_emptyParapgraphs1();
    void html_emptyParapgraphs2();
    void html_emptyParagraphs3();
    void html_emptyParagraphs4();
    void html_font();
    void html_fontSize();
    void html_fontSizeAdjustment();
    void html_cssFontSize();
    void html_cssShorthandFont();
    void html_bodyBgColor();
    void html_qtBgColor();
    void html_blockLevelDiv();
    void html_spanNesting();
    void html_nestedLists();
    void noSpecialCharactersInPlainText();
    void html_doNotInheritBackground();
    void html_inheritBackgroundToInlineElements();
    void html_doNotInheritBackgroundFromBlockElements();
    void html_nobr();
    void fromPlainText();
    void fromPlainText2();
    void html_closingImageTag();
    void html_emptyDocument();
    void html_closingTag();
    void html_anchorAroundImage();
    void html_floatBorder();
    void html_frameImport();
    void html_frameImport2();
    void html_dontAddMarginsAcrossTableCells();
    void html_dontMergeCenterBlocks();
    void html_tableCellBgColor();
    void html_tableCellBgColor2();
    void html_cellSkip();
    void nonZeroMarginOnImport();
    void html_charFormatPropertiesUnset();
    void html_headings();
    void html_quotedFontFamily();
    void html_spanBackgroundColor();
    void defaultFont();
    void html_brokenTitle_data();
    void html_brokenTitle();
    void html_blockVsInline();
    void html_tbody();
    void html_nestedTables();
    void html_rowSpans();
    void html_rowSpans2();
    void html_implicitParagraphs();
    void html_missingCloseTag();
    void html_anchorColor();
    void html_lastParagraphClosing();
    void html_tableHeaderBodyFootParent();
    void html_columnWidths();
    void html_bodyBackground();
    void html_tableCellBackground();
    void css_bodyBackground();
    void css_tableCellBackground();
    void css_fontWeight();
    void css_float();
    void css_textIndent();
    void css_inline();
    void css_external();
    void css_import();
    void css_selectors_data();
    void css_selectors();
    void css_nodeNameCaseInsensitivity();
    void css_textUnderlineStyle_data();
    void css_textUnderlineStyle();
    void css_textUnderlineStyleAndDecoration();
    void css_listStyleType();
    void css_linkPseudo();
    void css_pageBreaks();
    void css_cellPaddings();
    void universalSelectors_data();
    void universalSelectors();
    void screenMedia();
    void htmlResourceLoading();
    void someCaseInsensitiveAttributeValues();
    void backgroundImage();
    void dontMergePreAndNonPre();
    void leftMarginInsideHtml();
    void html_margins();
    void newlineInsidePreShouldBecomeNewParagraph();
    void invalidColspan();
    void html_brokenTableWithJustTr();
    void html_brokenTableWithJustTd();
    void html_preNewlineHandling_data();
    void html_preNewlineHandling();
    void html_br();
    void html_dl();
    void html_tableStrangeNewline();
    void html_tableStrangeNewline2();
    void html_tableStrangeNewline3();
    void html_caption();
    void html_windowsEntities();
    void html_eatenText();
    void html_hr();
    void html_hrMargins();
    void html_blockQuoteMargins();
    void html_definitionListMargins();
    void html_listMargins();
    void html_titleAttribute();
    void html_compressDivs();
    void completeToPlainText();
    void copyContents();
    void html_textAfterHr();
    void blockTagClosing();
    void isEmpty();
    void html_alignmentInheritance();
    void html_ignoreEmptyDivs();
    void html_dontInheritAlignmentForFloatingImages();
    void html_verticalImageAlignment();
    void html_verticalCellAlignment();
    void html_borderColor();
    void html_borderStyle();
    void html_borderWidth();
    void html_userState();
    void html_rootFrameProperties();
    void html_alignmentPropertySet();
    void html_appendList();
    void html_appendList2();
    void html_qt3RichtextWhitespaceMode();
    void html_brAfterHr();
    void html_unclosedHead();
    void html_entities();
    void html_entities_data();
    void html_ignore_script();
    void html_directionWithHtml();
    void html_directionWithRichText();
    void html_metaInBody();
    void html_importImageWithoutAspectRatio();
    void html_fromFirefox();

private:
    inline void setHtml(const QString &html)
    // don't take the shortcut in QTextDocument::setHtml
    { doc->clear(); QTextCursor(doc).insertFragment(QTextDocumentFragment::fromHtml(html)); }

    inline void appendHtml(const QString &html)
    {
        QTextCursor cursor(doc);
        cursor.movePosition(QTextCursor::End);
        cursor.insertHtml(html);
    }

    QTextDocument *doc;
    QTextCursor cursor;
};

tst_QTextDocumentFragment::tst_QTextDocumentFragment()
{
    QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
    img.save("foo.png");
}

tst_QTextDocumentFragment::~tst_QTextDocumentFragment()
{
    QFile::remove(QLatin1String("foo.png"));
}

void tst_QTextDocumentFragment::init()
{
    doc = new QTextDocument;
    cursor = QTextCursor(doc);
}

void tst_QTextDocumentFragment::cleanup()
{
    cursor = QTextCursor();
    delete doc;
    doc = 0;
}

#include <private/qtextdocument_p.h>
#include <qdebug.h>
static void dumpTable(const QTextDocumentPrivate *pt)
{
    qDebug() << "---dump----";
    qDebug() << "all text:" << pt->buffer();
    for (QTextDocumentPrivate::FragmentIterator it = pt->begin();
         !it.atEnd(); ++it) {
        qDebug() << "Fragment at text position" << it.position() << "; stringPosition" << it.value()->stringPosition << "; size" << it.value()->size_array[0] << "format :" << it.value()->format << "frag: " << it.n;
        qDebug() << "    text:" << pt->buffer().mid(it.value()->stringPosition, it.value()->size_array[0]);
    }
    qDebug() << "----begin block dump----";
    for (QTextBlock it = pt->blocksBegin(); it.isValid(); it = it.next())
        qDebug() << "block at" << it.position() << "with length" << it.length() << "block alignment" << it.blockFormat().alignment();
    qDebug() << "---dump----";
}
static void dumpTable(QTextDocument *doc) { dumpTable(doc->docHandle()); }

void tst_QTextDocumentFragment::listCopying()
{
    cursor.insertList(QTextListFormat::ListDecimal);

    QTextFormat originalBlockFormat = cursor.blockFormat();
    QVERIFY(originalBlockFormat.objectIndex() != -1);
    int originalListItemIdx = cursor.blockFormat().objectIndex();

    cursor.insertText("Hello World");

    QTextDocumentFragment fragment(doc);

    cursor.insertFragment(fragment);

    QVERIFY(cursor.currentList());
    QVERIFY(cursor.blockFormat() != originalBlockFormat);
    QVERIFY(cursor.blockFormat().objectIndex() != originalListItemIdx);
}

void tst_QTextDocumentFragment::listZeroCopying()
{
    // same testcase as above but using the zero-copying

    cursor.insertList(QTextListFormat::ListDecimal);

    QTextFormat originalBlockFormat = cursor.blockFormat();
    int originalListItemIdx = cursor.blockFormat().objectIndex();

    cursor.insertText("Hello World");

    QTextDocumentFragment fragment(doc);
    cursor.insertFragment(fragment);

    QVERIFY(cursor.currentList());
    QVERIFY(cursor.blockFormat() != originalBlockFormat);
    QVERIFY(cursor.blockFormat().objectIndex() != originalListItemIdx);
}

void tst_QTextDocumentFragment::listCopying2()
{
    cursor.insertList(QTextListFormat::ListDecimal);
    cursor.insertText("Hello World");

    cursor.insertList(QTextListFormat::ListDisc);
    cursor.insertText("Hello World");

    QTextDocumentFragment fragment(doc);

    cursor.insertFragment(fragment);

    cursor.movePosition(QTextCursor::Start);
    int listItemCount = 0;
    do {
	if (cursor.currentList())
	    listItemCount++;
    } while (cursor.movePosition(QTextCursor::NextBlock));

    QCOMPARE(listItemCount, 4);

    // we call this here because it used to cause a failing assertion in the
    // list manager.
    doc->undo();
}

void tst_QTextDocumentFragment::tableCopying()
{
    // this tests both, the fragment to use the direction insertion instead of using the
    // cursor, which might adjuts its position when inserting a table step by step, as well
    // as the pasiveness of the tablemanager.
    QTextDocumentFragment fragment;
    {
	QTextDocument doc;
	QTextCursor cursor(&doc);

	QTextTableFormat fmt;
	QTextTable *table = cursor.insertTable(2, 2, fmt);

	table->cellAt(0, 0).firstCursorPosition().insertText("First Cell");
	table->cellAt(0, 1).firstCursorPosition().insertText("Second Cell");
	table->cellAt(1, 0).firstCursorPosition().insertText("Third Cell");
	table->cellAt(1, 1).firstCursorPosition().insertText("Fourth Cell");

	fragment = QTextDocumentFragment(&doc);
    }
    {
	QTextDocument doc;
	QTextCursor cursor(&doc);

	cursor.insertText("FooBar");
	cursor.insertBlock();
	cursor.movePosition(QTextCursor::Left);

	cursor.insertFragment(fragment);
	cursor.movePosition(QTextCursor::Start);
	cursor.movePosition(QTextCursor::NextBlock);

	QTextTable *table = cursor.currentTable();
	QVERIFY(table);
	QCOMPARE(table->rows(), 2);
	QCOMPARE(table->columns(), 2);
    }
}

void tst_QTextDocumentFragment::tableCopyingWithColSpans()
{
    const char html[] = ""
"<table border>"
"  <tr>"
"    <td>First Cell"
"    <td>Second Cell"
"  </tr>"
"  <tr>"
"    <td colspan=\"2\">Third Cell"
"  </tr>"
"  <tr>"
"    <td>Fourth Cell"
"    <td>Fifth Cell"
"  </tr>"
"</table>";
    setHtml(html);

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QVERIFY(table->columns() == 2 && table->rows() == 3);

    cursor = table->cellAt(2, 0).lastCursorPosition();
    cursor.setPosition(table->cellAt(0, 0).firstPosition(), QTextCursor::KeepAnchor);
    QVERIFY(cursor.hasComplexSelection());

    int firstRow = 0, numRows = 0, firstCol = 0, numCols = 0;
    cursor.selectedTableCells(&firstRow, &numRows, &firstCol, &numCols);
    QCOMPARE(firstRow, 0);
    QCOMPARE(numRows, 3);
    QCOMPARE(firstCol, 0);
    QCOMPARE(numCols, 1);

    QTextDocumentFragment frag = cursor.selection();
    cleanup();
    init();
    cursor.insertFragment(frag);

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    table = cursor.currentTable();
    QVERIFY(table);
    QVERIFY(table->columns() == 1 && table->rows() == 3);
}

void tst_QTextDocumentFragment::tableColSpanAndWidth()
{
    const char html[] = ""
"<table border=\"0\">"
"  <tr>"
"    <td colspan=\"4\" width=\"400\">First Cell</td>"
"  </tr>"
"</table>";
    setHtml(html);

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QVERIFY(table->columns() == 4 && table->rows() == 1);
    // make sure its approx 400 and not a multiple due to the colspan
    QVERIFY(doc->size().width()> 398.);
    QVERIFY(doc->size().width() < 420.);
}

void tst_QTextDocumentFragment::tableImport()
{
    // used to cause a failing assertion, as HTMLImporter::closeTag was
    // called twice with the last node.
    QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml(QString::fromLatin1("<table><tr><td>Hey</td><td>Blah"));
    QVERIFY(!fragment.isEmpty());
}

void tst_QTextDocumentFragment::tableImport2()
{
    {
	const char html[] = ""
	    "<table>"
	    "<tr><td>First Cell</td><td>Second Cell</td></tr>"
	    "<tr><td>Third Cell</td><td>Fourth Cell</td></tr>"
	    "</table>";

	QTextDocument doc;
	QTextCursor cursor(&doc);
	cursor.insertFragment(QTextDocumentFragment::fromHtml(QByteArray::fromRawData(html, sizeof(html) / sizeof(html[0]))));

	cursor.movePosition(QTextCursor::Start);
	cursor.movePosition(QTextCursor::NextBlock);
	QTextTable *table = cursor.currentTable();
	QVERIFY(table);
	QCOMPARE(table->columns(), 2);
	QCOMPARE(table->rows(), 2);
    }
    {
	const char html[] = ""
	    "<table>"
	    "<tr><td>First Cell</td><td>Second Cell</td></tr>"
	    "<tr><td>Third Cell</td><td>"
	    "                           <table>"
	    "                           <tr><td>First Nested Cell</td><td>Second Nested Cell</td></tr>"
	    "                           <tr><td>Third Nested Cell</td><td>Fourth Nested Cell</td></tr>"
	    "                           <tr><td>Fifth Nested Cell</td><td>Sixth Nested Cell</td></tr>"
	    "                           </table></td></tr>"
	    "</table>";

	QTextDocument doc;
	QTextCursor cursor(&doc);
	cursor.insertFragment(QTextDocumentFragment::fromHtml(QByteArray::fromRawData(html, sizeof(html) / sizeof(html[0]))));

	cursor.movePosition(QTextCursor::Start);
	cursor.movePosition(QTextCursor::NextBlock);
	QTextTable *table = cursor.currentTable();
	QVERIFY(table);
	QCOMPARE(table->columns(), 2);
	QCOMPARE(table->rows(), 2);

        /*
	QTextCursor fourthCell = table->cellAt(1, 1).firstCursorPosition();
	fourthCell.movePosition(QTextCursor::NextBlock);
	table = fourthCell.currentTable();
	QVERIFY(table);
	QVERIFY(table != cursor.currentTable());
	QCOMPARE(table->columns(), 2);
	QCOMPARE(table->rows(), 3);
        */
    }
    {
	const char buggyHtml[] = ""
	    "<table>"
	    "<tr><td>First Cell<td>Second Cell"
	    "<tr><td>Third Cell<td>Fourth Cell"
	    "</table>";

	QTextDocument doc;
	QTextCursor cursor(&doc);
	cursor.insertFragment(QTextDocumentFragment::fromHtml(QByteArray::fromRawData(buggyHtml, sizeof(buggyHtml) / sizeof(buggyHtml[0]))));

	cursor.movePosition(QTextCursor::Start);
	cursor.movePosition(QTextCursor::NextBlock);
	QTextTable *table = cursor.currentTable();
	QVERIFY(table);
	QCOMPARE(table->columns(), 2);
	QCOMPARE(table->rows(), 2);
    }
    {
	const char buggyHtml[] = ""
	    "<table>"
	    "<tr><th>First Cell<th>Second Cell"
	    "<tr><td>Third Cell<td>Fourth Cell"
	    "</table>";

	QTextDocument doc;
	QTextCursor cursor(&doc);
	cursor.insertFragment(QTextDocumentFragment::fromHtml(QByteArray::fromRawData(buggyHtml, sizeof(buggyHtml) / sizeof(buggyHtml[0]))));

	cursor.movePosition(QTextCursor::Start);
	cursor.movePosition(QTextCursor::NextBlock);
	QTextTable *table = cursor.currentTable();
	QVERIFY(table);
	QCOMPARE(table->columns(), 2);
	QCOMPARE(table->rows(), 2);
    }

}

void tst_QTextDocumentFragment::tableImport3()
{
    // ### would be better to have tree tests for QTextHtmlParser
    // make sure the p is a child of the td. If not the following td
    // ends up outside the table, causing an assertion
    const char html[] = "<table><tr><td><p></p></td><td></td></tr></table>";
    QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml(QString::fromLatin1(html));
    QVERIFY(!fragment.isEmpty());
}

void tst_QTextDocumentFragment::tableImport4()
{
    const char html[] = "<table>"
        "<tr><td>blah</td></tr>"
        "<tr><td>blah</td><td>blah</td></tr>"
        "</table>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html)));
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(cursor.currentTable());
    QCOMPARE(cursor.currentTable()->columns(), 2);
}

void tst_QTextDocumentFragment::tableImport5()
{
    const char html[] = "<table>"
        "<tr>"
        " <td>Foo</td>"
        " <td>Bar</td>"
        " <td>Bleh</td>"
        "  <td>Blub</td>"
        "</tr>"
        "<tr>"
        "  <td>Ahh</td>"
        "  <td colspan=5>Gah</td>"
        "</tr>"
        "</table>";

    cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html)));
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(cursor.currentTable());
    QCOMPARE(cursor.currentTable()->rows(), 2);
    QCOMPARE(cursor.currentTable()->columns(), 6);
}

void tst_QTextDocumentFragment::textCopy()
{
    /* this test used to cause failing assertions in QTextDocumentFragment */
    /* copy&paste 'lo\bwor' */
    cursor.insertText("Hello");
    cursor.insertBlock();
    cursor.insertText("World");

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, 3);
    cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor);
    cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 3);

    QTextDocumentFragment fragment(cursor);
    QVERIFY(!fragment.isEmpty());
    cursor.insertFragment(fragment);
}

void tst_QTextDocumentFragment::copyWholeDocument()
{
    // used to cause the famous currentBlock.position() == pos + 1 failing assertion
    cursor.insertText("\nHey\nBlah\n");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);

    QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
    fmt.setBackground(Qt::blue);
    doc->rootFrame()->setFrameFormat(fmt);

    QTextDocumentFragment fragment(cursor);
    QVERIFY(true); // good if we reach this point :)

    cleanup();
    init();

    fmt.setBackground(Qt::red);
    doc->rootFrame()->setFrameFormat(fmt);

    cursor.insertFragment(fragment);

    QVERIFY(doc->rootFrame()->frameFormat().background().color() == Qt::red);
}

void tst_QTextDocumentFragment::title()
{
    doc->setHtml(QString::fromLatin1("<html><head><title>Test</title></head><body>Blah</body></html>"));
    QCOMPARE(doc->metaInformation(QTextDocument::DocumentTitle), QString::fromLatin1("Test"));
}

void tst_QTextDocumentFragment::html_listIndents1()
{
    const char html[] = "<ul><li>Hey</li><li>Hah</li></ul>";
    setHtml(QString::fromLatin1(html));
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextList *list = cursor.currentList();
    QVERIFY(list);
    QCOMPARE(list->format().indent(), 1);
    QCOMPARE(cursor.block().blockFormat().indent(), 0);
}

void tst_QTextDocumentFragment::html_listIndents2()
{
    const char html[] = "<ul><li>Hey<p>Hah</ul>";
    setHtml(QString::fromLatin1(html));
    cursor.movePosition(QTextCursor::Start);
    QTextList *list = cursor.currentList();
    QVERIFY(list);
    QCOMPARE(list->format().indent(), 1);
    QCOMPARE(cursor.block().blockFormat().indent(), 0);

    cursor.movePosition(QTextCursor::NextBlock);
    QCOMPARE(cursor.block().blockFormat().indent(), 1);
}

void tst_QTextDocumentFragment::html_listIndents3()
{
    const char html[] = "<ul><li><p>Hah</ul>";
    setHtml(QString::fromLatin1(html));
    cursor.movePosition(QTextCursor::Start);
    QTextList *list = cursor.currentList();
    QVERIFY(list);
    QCOMPARE(list->format().indent(), 1);
    QCOMPARE(cursor.block().blockFormat().indent(), 0);
}

void tst_QTextDocumentFragment::html_listIndents4()
{
    const char html[] = "<ul><li>Foo</ul><p>This should not have the same indent as Foo";
    setHtml(QString::fromLatin1(html));
    cursor.movePosition(QTextCursor::Start);
    QTextList *list = cursor.currentList();
    QVERIFY(list);
    QCOMPARE(list->format().indent(), 1);

    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(!cursor.currentList());
    QCOMPARE(cursor.blockFormat().indent(), 0);
}

void tst_QTextDocumentFragment::html_listIndents5()
{
    const char html[] = "<ul><li>Foo<p><li>Bar</li></ul>";
    setHtml(QString::fromLatin1(html));
    cursor.movePosition(QTextCursor::Start);
    QTextList *list = cursor.currentList();
    QVERIFY(list);
    QCOMPARE(list->format().indent(), 1);

    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(cursor.currentList() == list);
    QCOMPARE(cursor.blockFormat().indent(), 0);
}

void tst_QTextDocumentFragment::html_listIndents6()
{
    const char html[] = "<ul><li>Outer List<div class=\"testclass\"><ul><li>Nested Item 1</li></ul></div></li></ul>";
    setHtml(QString::fromLatin1(html));
    cursor.movePosition(QTextCursor::Start);
    QTextList *list = cursor.currentList();
    QVERIFY(list);
    QCOMPARE(list->format().indent(), 1);

    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(cursor.currentList() != list);
    list = cursor.currentList();
    QVERIFY(list);
    QCOMPARE(list->format().indent(), 2);

    QCOMPARE(cursor.blockFormat().indent(), 0);
}

void tst_QTextDocumentFragment::blockCharFormat()
{
    const char html[] = "<p style=\"font-style:italic\"><span style=\"font-style:normal\">Test</span></p>";
    setHtml(QString::fromLatin1(html));
    QVERIFY(doc->begin().charFormat().fontItalic());
}

void tst_QTextDocumentFragment::blockCharFormatCopied()
{
    QTextCharFormat fmt;
    fmt.setForeground(Qt::green);
    cursor.setBlockCharFormat(fmt);
    cursor.insertText("Test", QTextCharFormat());
    QTextDocumentFragment frag(doc);
    cleanup();
    init();
    cursor.insertFragment(frag);
    QVERIFY(cursor.blockCharFormat() == fmt);
}

void tst_QTextDocumentFragment::initialBlock()
{
    const char html[] = "<p>Test</p>";
    setHtml(QString::fromLatin1(html));
    QCOMPARE(doc->blockCount(), 1);
}

void tst_QTextDocumentFragment::clone()
{
    QTextBlockFormat mod;
    mod.setAlignment(Qt::AlignCenter);
    cursor.mergeBlockFormat(mod);
    cursor.insertText("Blah");
    QVERIFY(cursor.blockFormat().alignment() == Qt::AlignCenter);
    QTextDocumentFragment frag(doc);
    cleanup();
    init();
    cursor.insertFragment(frag);
    cursor.movePosition(QTextCursor::Start);
    QVERIFY(cursor.blockFormat().alignment() == Qt::AlignCenter);
}

void tst_QTextDocumentFragment::dontRemoveInitialBlockIfItHoldsObjectIndexedCharFormat()
{
    const char html[] = "<table><tr><td>cell one<td>cell two</tr><tr><td>cell three<td>cell four</tr></table>";
    QVERIFY(doc->begin().charFormat().objectIndex() == -1);
    setHtml(QString::fromLatin1(html));
    int cnt = 0;

    int objectIndexOfLast = -1;
    for (QTextBlock blk = doc->begin(); blk.isValid(); blk = blk.next()) {
        ++cnt;
        objectIndexOfLast = blk.charFormat().objectIndex();
    }
    //   beginning of frame for first cell
    // + beginning of frame for second cell
    // + beginning of frame for third cell
    // + beginning of frame for fourth cell
    // + end of frame
    // + initial block
    // ==> 6
    QCOMPARE(cnt, 6);
    QVERIFY(objectIndexOfLast != -1);
    QVERIFY(doc->begin().next().charFormat().objectIndex() != -1);
}

void tst_QTextDocumentFragment::dosLineFeed()
{
    const char html[] = "<pre>Test\r\n</pre>Bar";
    setHtml(QString::fromLatin1(html));
    QVERIFY(!doc->toPlainText().contains('\r'));
    QCOMPARE(doc->toPlainText(), QString("Test\nBar"));
}

void tst_QTextDocumentFragment::unorderedListEnumeration()
{
    const char html[] = "<ul><ul><ul><li>Blah</li></ul></ul>";
    setHtml(QString::fromLatin1(html));
    cursor.movePosition(QTextCursor::End);
    QVERIFY(cursor.currentList());
    QVERIFY(cursor.currentList()->format().style() == QTextListFormat::ListCircle);

    const char html2[] = "<ul><ul><ul type=disc><li>Blah</li></ul></ul>";
    setHtml(QString::fromLatin1(html2));
    cursor.movePosition(QTextCursor::End);
    QVERIFY(cursor.currentList());
    QVERIFY(cursor.currentList()->format().style() == QTextListFormat::ListDisc);

}

void tst_QTextDocumentFragment::resetHasBlockAfterClosedBlockTags()
{
    // when closing tags we have to make sure hasBlock in import() gets resetted
    const char html[] = "<body><table><tr><td><td><p></table><p></body>";
    setHtml(QString::fromLatin1(html));
    QVERIFY(!doc->isEmpty());
}

void tst_QTextDocumentFragment::ignoreStyleTags()
{
    const char html[] = "<body><style>Blah</style>Hello</body>";
    setHtml(QString::fromLatin1(html));
    QCOMPARE(doc->toPlainText(), QString("Hello"));
}

void tst_QTextDocumentFragment::hrefAnchor()
{
    {
        const char html[] = "<a href=\"test\">blah</a>";
        setHtml(QString::fromLatin1(html));
        QVERIFY(doc->begin().begin().fragment().charFormat().isAnchor());
        QCOMPARE(doc->begin().begin().fragment().charFormat().anchorHref(), QString::fromAscii("test"));
        QVERIFY(doc->begin().begin().fragment().charFormat().fontUnderline() == true);
    }

    {
        // only hyperlinks should have special formatting
        const char html[] = "<a>blah</a>";
        setHtml(QString::fromLatin1(html));
        QVERIFY(doc->begin().begin().fragment().charFormat().isAnchor());
        QVERIFY(doc->begin().begin().fragment().charFormat().fontUnderline() == false);
    }
}

void tst_QTextDocumentFragment::namedAnchorFragments()
{
    // named anchors should be 'invisible', but the fragment right after it should
    // hold the attribute
    const char html[] = "a<a name=\"test\" />blah";
    setHtml(QString::fromLatin1(html));

    QTextBlock firstBlock = doc->begin();
    QVERIFY(firstBlock.isValid());

    QTextBlock::Iterator it = firstBlock.begin();
    QVERIFY(!it.atEnd());

    // the 'a'
    QVERIFY(it.fragment().isValid());
    QCOMPARE(it.fragment().text(), QString::fromAscii("a"));
    QVERIFY(it.fragment().charFormat().isAnchor() == false);

    // the 'b' of 'blah' as separate fragment with the anchor attribute
    ++it;
    QVERIFY(it.fragment().isValid());
    QCOMPARE(it.fragment().text(), QString::fromAscii("b"));
    QVERIFY(it.fragment().charFormat().isAnchor());

    // the 'lah' of 'blah' as remainder
    ++it;
    QVERIFY(it.fragment().isValid());
    QVERIFY(it.fragment().text().startsWith("lah"));
    QVERIFY(it.fragment().charFormat().isAnchor() == false);
}

void tst_QTextDocumentFragment::namedAnchorFragments2()
{
    const char html[] = "<p>    <a name=\"foo\"> Hello";
    setHtml(QString::fromLatin1(html));

    QCOMPARE(doc->toPlainText(), QString("Hello"));

    QTextBlock::Iterator it = doc->begin().begin();
    QVERIFY(!it.atEnd());

    QCOMPARE(it.fragment().text(), QString::fromAscii("H"));
    QVERIFY(it.fragment().charFormat().isAnchor());

    ++it;

    QCOMPARE(it.fragment().text(), QString::fromAscii("ello"));
    QVERIFY(!it.fragment().charFormat().isAnchor());
}

void tst_QTextDocumentFragment::namedAnchorFragments3()
{
    setHtml("<a name=\"target\" /><a name=\"target2\"/><span>Text</span>");

    QCOMPARE(doc->toPlainText(), QString("Text"));

    QTextBlock::Iterator it = doc->begin().begin();
    QVERIFY(!it.atEnd());

    QCOMPARE(it.fragment().text(), QString::fromAscii("T"));
    QVERIFY(it.fragment().charFormat().isAnchor());
    QCOMPARE(it.fragment().charFormat().anchorName(), QString("target"));
    QStringList targets; targets << "target" << "target2";
    QCOMPARE(it.fragment().charFormat().anchorNames(), targets);

    ++it;

    QCOMPARE(it.fragment().text(), QString::fromAscii("ext"));
    QVERIFY(!it.fragment().charFormat().isAnchor());
}

void tst_QTextDocumentFragment::dontInheritAlignmentInTables()
{
    const char html[] = "<table align=center><tr><td>Hey</td></tr></table>";
    setHtml(QString::fromLatin1(html));

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(cursor.currentTable());
    QVERIFY(cursor.currentTable()->cellAt(0, 0).isValid());
    QVERIFY(cursor.currentTable()->cellAt(0, 0).firstCursorPosition().block().next().blockFormat().alignment() != Qt::AlignHCenter);
}

void tst_QTextDocumentFragment::cellBlockCount()
{
    const char html[] = "<table><tr><td>Hey</td></tr></table>";
    setHtml(QString::fromLatin1(html));

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(cursor.currentTable());

    QTextTableCell cell = cursor.currentTable()->cellAt(0, 0);
    QVERIFY(cell.isValid());

    int blockCount = 0;
    for (QTextFrame::iterator it = cell.begin(); !it.atEnd(); ++it) {
        QVERIFY(it.currentFrame() == 0);
        QVERIFY(it.currentBlock().isValid());
        ++blockCount;
    }
    QCOMPARE(blockCount, 1);
}

void tst_QTextDocumentFragment::cellBlockCount2()
{
    const char html[] = "<table><tr><td><p>Hey</p></td></tr></table>";
    setHtml(QString::fromLatin1(html));

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(cursor.currentTable());

    QTextTableCell cell = cursor.currentTable()->cellAt(0, 0);
    QVERIFY(cell.isValid());

    int blockCount = 0;
    for (QTextFrame::iterator it = cell.begin(); !it.atEnd(); ++it) {
        QVERIFY(it.currentFrame() == 0);
        QVERIFY(it.currentBlock().isValid());
        ++blockCount;
    }
    QCOMPARE(blockCount, 1);
}

void tst_QTextDocumentFragment::emptyTable()
{
    const char html[] = "<table></table>";
    setHtml(QString::fromLatin1(html));
    QVERIFY(true); // don't crash with a failing assertion
}

void tst_QTextDocumentFragment::emptyTable2()
{
    const char html[] = "<table></td></tr></table><p>blah</p>";
    setHtml(QString::fromLatin1(html));
    QVERIFY(true); // don't crash with a failing assertion
}

void tst_QTextDocumentFragment::emptyTable3()
{
    const char html[] = "<table><tr><td><table></table></td><td>Foobar</td></tr></table>";
    setHtml(QString::fromLatin1(html));

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 1);
    QCOMPARE(table->columns(), 2);
    QTextTableCell cell = table->cellAt(0, 0);
    QVERIFY(cell.isValid());
    QVERIFY(cell.firstPosition() == cell.lastPosition());
    cell = table->cellAt(0, 1);
    QTextCursor cursor = cell.firstCursorPosition();
    cursor.setPosition(cell.lastPosition(), QTextCursor::KeepAnchor);
    QCOMPARE(cursor.selectedText(), QString("Foobar"));
}

void tst_QTextDocumentFragment::doubleRowClose()
{
    const char html[] = "<table><tr><td>Blah</td></tr></tr><tr><td>Hm</td></tr></table>";
    setHtml(QString::fromLatin1(html));
    QVERIFY(true); // don't crash with a failing assertion
}

void tst_QTextDocumentFragment::mayNotHaveChildren()
{
    // make sure the Hey does not end up as tag text for the img tag
    const char html[] = "<img />Hey";
    setHtml(QString::fromLatin1(html));
    QCOMPARE(doc->toPlainText().mid(1), QString::fromAscii("Hey"));
}

void tst_QTextDocumentFragment::inheritAlignment()
{
    // make sure attributes from the body tag get inherited
    const char html[] = "<body align=right><p>Hey";
    setHtml(QString::fromLatin1(html));
    // html alignment is absolute
    QVERIFY(doc->begin().blockFormat().alignment() == Qt::Alignment(Qt::AlignRight|Qt::AlignAbsolute));
}

void tst_QTextDocumentFragment::dontEmitEmptyNodeWhenEmptyTagIsFollowedByCloseTag()
{
    // make sure the Hey does not end up as tag text for the img tag
    const char html[] = "<body align=right><p align=left>Blah<img></img><p>Hey";
    setHtml(QString::fromLatin1(html));
    QVERIFY(doc->begin().blockFormat().alignment() == Qt::Alignment(Qt::AlignLeft|Qt::AlignAbsolute));
    QVERIFY(doc->begin().next().blockFormat().alignment() == Qt::Alignment(Qt::AlignRight|Qt::AlignAbsolute));
}

void tst_QTextDocumentFragment::toPlainText()
{
    QString input = "Hello\nWorld";
    input += QChar::ParagraphSeparator;
    input += "Blah";
    doc->setPlainText(input);
    QCOMPARE(doc->blockCount(), 3);
}

void tst_QTextDocumentFragment::copyTableRow()
{
    QTextDocumentFragment frag;
    {
        QTextTable *table = cursor.insertTable(2, 2);
        table->cellAt(0, 0).firstCursorPosition().insertText("Blah");
        table->cellAt(0, 1).firstCursorPosition().insertText("Foo");
        table->cellAt(1, 0).firstCursorPosition().insertText("Bar");
        table->cellAt(1, 1).firstCursorPosition().insertText("Hah");

        // select second row
        cursor = table->cellAt(1, 1).firstCursorPosition();
        cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::KeepAnchor);

        QCOMPARE(table->cellAt(cursor.position()).row(), 1);
        QCOMPARE(table->cellAt(cursor.position()).column(), 0);
        QCOMPARE(table->cellAt(cursor.anchor()).row(), 1);
        QCOMPARE(table->cellAt(cursor.anchor()).column(), 1);

        frag = QTextDocumentFragment(cursor);
    }
    {
        QTextDocument doc2;
        cursor = QTextCursor(&doc2);
        cursor.insertFragment(frag);

        cursor.movePosition(QTextCursor::Start);
        cursor.movePosition(QTextCursor::NextBlock);
        QTextTable *table = cursor.currentTable();

        QVERIFY(table);
        QCOMPARE(table->columns(), 2);
        QCOMPARE(table->rows(), 1);

        QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("Bar"));
        QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Hah"));
    }
}

void tst_QTextDocumentFragment::copyTableColumn()
{
    QTextDocumentFragment frag;
    {
        QTextTable *table = cursor.insertTable(2, 2);
        table->cellAt(0, 0).firstCursorPosition().insertText("Blah");
        table->cellAt(0, 1).firstCursorPosition().insertText("Foo");
        table->cellAt(1, 0).firstCursorPosition().insertText("Bar");
        table->cellAt(1, 1).firstCursorPosition().insertText("Hah");

        // select second column
        cursor = table->cellAt(0, 1).firstCursorPosition();
        cursor.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor);

        QCOMPARE(table->cellAt(cursor.anchor()).row(), 0);
        QCOMPARE(table->cellAt(cursor.anchor()).column(), 1);
        QCOMPARE(table->cellAt(cursor.position()).row(), 1);
        QCOMPARE(table->cellAt(cursor.position()).column(), 1);

        frag = QTextDocumentFragment(cursor);
    }
    {
        QTextDocument doc2;
        cursor = QTextCursor(&doc2);
        cursor.insertFragment(frag);

        cursor.movePosition(QTextCursor::Start);
        cursor.movePosition(QTextCursor::NextBlock);
        QTextTable *table = cursor.currentTable();

        QVERIFY(table);
        QCOMPARE(table->columns(), 1);
        QCOMPARE(table->rows(), 2);

        QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("Foo"));
        QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Hah"));
    }
}

void tst_QTextDocumentFragment::copySubTable()
{
    QTextDocumentFragment frag;
    {
        QTextTableFormat fmt;
        QVector<QTextLength> constraints;
        constraints << QTextLength(QTextLength::PercentageLength, 16);
        constraints << QTextLength(QTextLength::PercentageLength, 28);
        constraints << QTextLength(QTextLength::PercentageLength, 28);
        constraints << QTextLength(QTextLength::PercentageLength, 28);
        fmt.setColumnWidthConstraints(constraints);

        QTextTable *table = cursor.insertTable(4, 4, fmt);
        for (int row = 0; row < 4; ++row)
            for (int col = 0; col < 4; ++col)
                table->cellAt(row, col).firstCursorPosition().insertText(QString("%1/%2").arg(row).arg(col));

        QCOMPARE(table->format().columnWidthConstraints().count(), table->columns());

        // select 2x2 subtable
        cursor = table->cellAt(1, 1).firstCursorPosition();
        cursor.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor);
        cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);

        QCOMPARE(table->cellAt(cursor.anchor()).row(), 1);
        QCOMPARE(table->cellAt(cursor.anchor()).column(), 1);
        QCOMPARE(table->cellAt(cursor.position()).row(), 2);
        QCOMPARE(table->cellAt(cursor.position()).column(), 2);

        frag = QTextDocumentFragment(cursor);
    }
    {
        QTextDocument doc2;
        cursor = QTextCursor(&doc2);
        cursor.insertFragment(frag);

        cursor.movePosition(QTextCursor::Start);
        cursor.movePosition(QTextCursor::NextBlock);
        QTextTable *table = cursor.currentTable();

        QVERIFY(table);
        QVERIFY(table->format().columnWidthConstraints().isEmpty());
        QCOMPARE(table->columns(), 2);
        QCOMPARE(table->rows(), 2);

        QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("1/1"));
        QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("1/2"));
        QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("2/1"));
        QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("2/2"));
    }
}

void tst_QTextDocumentFragment::html_textDecoration()
{
    const char html[] = "<span style='text-decoration: overline line-through underline'>Blah</span>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QByteArray::fromRawData(html, sizeof(html) / sizeof(html[0]))));

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().fontUnderline());
    QVERIFY(cursor.charFormat().fontOverline());
    QVERIFY(cursor.charFormat().fontStrikeOut());
}

void tst_QTextDocumentFragment::html_infiniteLoop()
{
    {
        // used to cause an infinite loop due to the lack of a space after the
        // tag name
        const char html[] = "<ahref=\"argl\">Link</a>";
        cursor.insertFragment(QTextDocumentFragment::fromHtml(html));
        QVERIFY(true);
    }

    {
        const char html[] = "<a href=\"\"a<";
        cursor.insertFragment(QTextDocumentFragment::fromHtml(html));
        QVERIFY(true);
    }
}

void tst_QTextDocumentFragment::html_blockIndent()
{
    const char html[] = "<p style=\"-qt-block-indent:3;\">Test</p>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));
    QCOMPARE(cursor.blockFormat().indent(), 3);
}

void tst_QTextDocumentFragment::html_listIndent()
{
    const char html[] = "<ul style=\"-qt-list-indent:4;\"><li>Blah</ul>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));
    QVERIFY(cursor.currentList());
    QCOMPARE(cursor.currentList()->format().indent(), 4);
}

void tst_QTextDocumentFragment::html_whitespace_data()
{
    QTest::addColumn<QString>("html");
    QTest::addColumn<QString>("expectedPlainText");

    QTest::newRow("1") << QString("<span>This is some test</span><span> with spaces between words</span>")
                       << QString("This is some test with spaces between words");

    QTest::newRow("2") << QString("<span>  </span><span>nowhitespacehereplease</span>")
                       << QString::fromLatin1("nowhitespacehereplease");

    QTest::newRow("3") << QString("<span style=\"white-space: pre;\">  white  space  here  </span>")
                       << QString::fromLatin1("  white  space  here  ");

    QTest::newRow("4") << QString("<span style=\"white-space: pre-wrap;\">  white  space  here  </span>")
                       << QString::fromLatin1("  white  space  here  ");

    QTest::newRow("5") << QString("<a href=\"One.html\">One</a> <a href=\"Two.html\">Two</a> <b>Three</b>\n"
                                  "<b>Four</b>")
                       << QString::fromLatin1("One Two Three Four");

    QTest::newRow("6") << QString("<p>Testing:     <b><i><u>BoldItalic</u></i></b> <i>Italic</i></p>")
                       << QString("Testing: BoldItalic Italic");

    QTest::newRow("7") << QString("<table><tr><td>Blah</td></tr></table> <table border><tr><td>Foo</td></tr></table>")
                       << QString("\nBlah\n\nFoo\n");

    QTest::newRow("8") << QString("<table><tr><td><i>Blah</i></td></tr></table> <i>Blub</i>")
                       << QString("\nBlah\nBlub");

    QTest::newRow("task116492") << QString("<p>a<font=\"Times\"> b </font>c</p>")
                                << QString("a b c");

    QTest::newRow("task121653") << QString("abc<b> def</b>")
                                << QString("abc def");

    QTest::newRow("task122650") << QString("<p>Foo</p>    Bar")
                                << QString("Foo\nBar");

    QTest::newRow("task122650-2") << QString("<p>Foo</p>  <p>  Bar")
                                  << QString("Foo \nBar");

    QTest::newRow("task122650-3") << QString("<html>Before<pre>\nTest</pre>")
                                  << QString("Before\nTest");

    QTest::newRow("br-with-whitespace") << QString("Foo<br>\nBlah")
                                        << QString("Foo\nBlah");

    QTest::newRow("collapse-p-with-newline") << QString("Foo<p>\n<p>\n<p>\n<p>\n<p>\n<p>\nBar")
            << QString("Foo\nBar");

    QTest::newRow("table") << QString("<table><tr><td>Blah</td></tr></table>\nTest")
                           << QString("\nBlah\nTest");

    QTest::newRow("table2") << QString("<table><tr><td><pre>\nTest\n</pre></td>\n </tr></table>")
                            << QString("\nTest\n");

    QTest::newRow("table3") << QString("<table><tr><td><pre>\nTest\n</pre> \n \n </td></tr></table>")
                            << QString("\nTest \n");
}

void tst_QTextDocumentFragment::html_whitespace()
{
    QFETCH(QString, html);
    QFETCH(QString, expectedPlainText);

    setHtml(html);

    QCOMPARE(doc->toPlainText(), expectedPlainText);
}

void tst_QTextDocumentFragment::html_qt3Whitespace()
{
    QString text = "This     text       has         some   whitespace"
                   "\n and \nnewlines that \n should be ignored\n\n";
    const QString html = QString("<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>")
                         + text
                         + QString("</body></html>");

    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));

    text.remove(QChar::fromLatin1('\n'));

    QCOMPARE(doc->toPlainText(), text);
}

void tst_QTextDocumentFragment::html_qt3WhitespaceWithFragments()
{
    QString text = "This     text       has         some   whitespace"
                   "\n and \nnewlines that \n should be ignored\n\n";
    const QString html = QString("<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"
                                 "blah blah<!--StartFragment--><span>")
                         + text
                         + QString("</span><!--EndFragment--></body></html>");

    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));

    text.remove(QChar::fromLatin1('\n'));

    QCOMPARE(doc->toPlainText(), text);
}

void tst_QTextDocumentFragment::html_qt3WhitespaceAfterTags()
{
    QString text = "    This     text       has         some   whitespace   ";
    const QString html = QString("<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body><span>")
                         + text
                         + QString("</span></body></html>");

    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));

    QCOMPARE(doc->toPlainText(), text);
}

void tst_QTextDocumentFragment::html_listStart1()
{
    // don't create a block for the <ul> element, even if there's some whitespace between
    // it and the <li>
    const char html[] = "<ul>        <li>list item</li><ul>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QByteArray::fromRawData(html, sizeof(html) / sizeof(html[0]))));

    QCOMPARE(doc->blockCount(), 1);
}

void tst_QTextDocumentFragment::html_listStart2()
{
    // unlike with html_listStart1 we want a block showing the 'buggy' text here
    const char html[] = "<ul>buggy, but text should appear<li>list item</li><ul>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QByteArray::fromRawData(html, sizeof(html) / sizeof(html[0]))));

    QCOMPARE(doc->blockCount(), 2);
}

void tst_QTextDocumentFragment::html_cssMargin()
{
    const char html[] = "<p style=\"margin-top: 1px; margin-bottom: 2px; margin-left: 3px; margin-right: 4px\">Test</p>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));
    const QTextBlockFormat fmt = cursor.blockFormat();
    QCOMPARE(fmt.topMargin(), qreal(1));
    QCOMPARE(fmt.bottomMargin(), qreal(2));
    QCOMPARE(fmt.leftMargin(), qreal(3));
    QCOMPARE(fmt.rightMargin(), qreal(4));
}

void tst_QTextDocumentFragment::html_hexEntities()
{
    const char html[] = "&#x00040;";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));
    QCOMPARE(doc->begin().begin().fragment().text(), QString("@"));
}

void tst_QTextDocumentFragment::html_decEntities()
{
    const char html[] = "&#64;";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));
    QCOMPARE(doc->begin().begin().fragment().text(), QString("@"));
}

void tst_QTextDocumentFragment::html_thCentered()
{
    const char html[] = "<table><tr><th>This should be centered</th></tr></table>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));

    cursor.movePosition(QTextCursor::PreviousBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);

    QVERIFY(table->cellAt(0, 0).begin().currentBlock().blockFormat().alignment() == Qt::AlignCenter);
}

void tst_QTextDocumentFragment::orderedListNumbering()
{
    // Supporter issue 45941 - make sure _two_ separate lists
    // are imported, which have their own numbering
    const char html[] = "<html><body>"
                        "<ol><li>elem 1</li></ol>"
                        "<ol><li>elem 1</li></ol>"
                        "</body></html>";

    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));

    int numberOfLists = 0;

    cursor.movePosition(QTextCursor::Start);
    QTextList *lastList = 0;
    do {
        QTextList *list = cursor.currentList();
        if (list && list != lastList) {
            lastList = list;
            ++numberOfLists;
        }
    } while (cursor.movePosition(QTextCursor::NextBlock));

    QCOMPARE(numberOfLists, 2);
}

void tst_QTextDocumentFragment::html_blockAfterList()
{
    const char html[] = "<ul><li>Foo</ul>This should be a separate paragraph and not be indented at the same level as Foo";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(html));

    cursor.movePosition(QTextCursor::Start);

    QVERIFY(cursor.currentList());
    QCOMPARE(cursor.currentList()->format().indent(), 1);

    QVERIFY(cursor.movePosition(QTextCursor::NextBlock));
    QVERIFY(!cursor.currentList());
    QCOMPARE(cursor.blockFormat().indent(), 0);
}

void tst_QTextDocumentFragment::html_subAndSuperScript()
{
    const char subHtml[] = "<sub>Subby</sub>";
    const char superHtml[] = "<sup>Super</sup>";
    const char subHtmlCss[] = "<span style=\"vertical-align: sub\">Subby</span>";
    const char superHtmlCss[] = "<span style=\"vertical-align: super\">Super</span>";
    const char alignmentInherited[] = "<sub><font face=\"Verdana\">Subby</font></sub>";

    setHtml(subHtml);
    QVERIFY(cursor.charFormat().verticalAlignment() == QTextCharFormat::AlignSubScript);

    setHtml(subHtmlCss);
    QVERIFY(cursor.charFormat().verticalAlignment() == QTextCharFormat::AlignSubScript);

    setHtml(superHtml);
    QVERIFY(cursor.charFormat().verticalAlignment() == QTextCharFormat::AlignSuperScript);

    setHtml(superHtmlCss);
    QVERIFY(cursor.charFormat().verticalAlignment() == QTextCharFormat::AlignSuperScript);

    setHtml(alignmentInherited);
    QVERIFY(cursor.charFormat().verticalAlignment() == QTextCharFormat::AlignSubScript);
}

void tst_QTextDocumentFragment::html_cssColors()
{
    const char color[] = "<span style=\"color:red\"><span style=\"color:blue\">Blue</span></span>";
    setHtml(color);
    QVERIFY(cursor.charFormat().foreground().color() == Qt::blue);

    const char rgbColor[] = "<span style=\"color:red\"><span style=\"color:rgb(0, 0, 255)\">Blue</span></span>";
    setHtml(rgbColor);
    QVERIFY(cursor.charFormat().foreground().color() == Qt::blue);
}

void tst_QTextDocumentFragment::obeyFragmentMarkersInImport()
{
    const char html[] = "This leading text should not appear<!--StartFragment--><span>Text</span><!--EndFragment-->This text at the end should not appear";
    setHtml(html);

    QCOMPARE(doc->toPlainText(), QString("Text"));
}

void tst_QTextDocumentFragment::whitespaceWithFragmentMarkers()
{
    QString text("    text with leading and trailing whitespace    ");
    const char html[] = "This leading text should not appear<!--StartFragment-->%1<!--EndFragment-->This text at the end should not appear";
    setHtml(QString::fromLatin1(html).arg(text));

    QString expected("text with leading and trailing whitespace ");
    QCOMPARE(doc->toPlainText(), expected);
}

void tst_QTextDocumentFragment::html_emptyParapgraphs1()
{
    const char html[] = "<p style=\"-qt-paragraph-type:empty;\">&nbsp;</p><p>Two paragraphs</p>";
    setHtml(html);

    QCOMPARE(doc->blockCount(), 2);
    QVERIFY(doc->begin().text().isEmpty());
    QCOMPARE(doc->begin().next().text(), QString("Two paragraphs"));
}

void tst_QTextDocumentFragment::html_emptyParapgraphs2()
{
    const char html[] = "<p style=\"margin-left:80px\"></p><p>One paragraph</p>";
    setHtml(html);

    QCOMPARE(doc->blockCount(), 1);
    QCOMPARE(cursor.blockFormat().leftMargin(), qreal(0));

    const char html2[] = "<p style=\"margin-left:80px\"></p>One paragraph";
    setHtml(html2);
    QCOMPARE(doc->blockCount(), 1);
    QCOMPARE(cursor.blockFormat().leftMargin(), qreal(0));

    const char html3[] = "<p style=\"margin-left:80px\">Foo</p><p></p>Two paragraphs";
    setHtml(html3);
    QCOMPARE(doc->blockCount(), 2);
    cursor = QTextCursor(doc);
    QCOMPARE(cursor.blockFormat().leftMargin(), qreal(80));
    QCOMPARE(cursor.block().next().blockFormat().leftMargin(), qreal(0));
}

void tst_QTextDocumentFragment::html_emptyParagraphs3()
{
    const char html[] = "<ul><p>Foo</p><p></p></ul><h4>Bar</h4>";

    setHtml(html);

    QCOMPARE(doc->blockCount(), 2);

    cursor = QTextCursor(doc);
    QCOMPARE(cursor.block().next().blockFormat().indent(), 0);
}

void tst_QTextDocumentFragment::html_emptyParagraphs4()
{
    const char html[] = "<p>foo</p><p style=\"page-break-before: always\"></p><p>bar</p>";
    setHtml(html);

    QTextBlock block = doc->begin();
    QVERIFY(block.isValid());
    QCOMPARE(block.text(), QString("foo"));
    block = block.next();
    QVERIFY(block.isValid());
    QTextBlockFormat bf = block.blockFormat();
    QVERIFY(bf.hasProperty(QTextFormat::PageBreakPolicy));
    QCOMPARE(bf.pageBreakPolicy(), QTextFormat::PageBreak_AlwaysBefore);
    QCOMPARE(block.text(), QString("bar"));

    const char html2[] = "<p>foo</p><p style=\"page-break-after: always\"></p><p>bar</p>";
    setHtml(html2);

    block = doc->begin();
    QVERIFY(block.isValid());
    QCOMPARE(block.text(), QString("foo"));
    block = block.next();
    QVERIFY(block.isValid());
    bf = block.blockFormat();
    QVERIFY(bf.hasProperty(QTextFormat::PageBreakPolicy));
    QCOMPARE(bf.pageBreakPolicy(), QTextFormat::PageBreak_AlwaysBefore); // after the empty line means it should appear for 'bar'
    QCOMPARE(block.text(), QString("bar"));
}

void tst_QTextDocumentFragment::html_font()
{
    const char html[] = "<font color=\"blue\"><p>Hah</p></font>";
    setHtml(html);

    QVERIFY(cursor.charFormat().foreground().color() == Qt::blue);
    QVERIFY(cursor.blockCharFormat().foreground().color() == Qt::blue);
}

void tst_QTextDocumentFragment::html_fontSize()
{
    const char html[] = "<font size=\"2\">Hah</font>";
    setHtml(html);

    QCOMPARE(cursor.charFormat().property(QTextFormat::FontSizeAdjustment).toInt(), -1);
}

void tst_QTextDocumentFragment::html_fontSizeAdjustment()
{
    const char html[] = "<font size=\"7\"><b>Hah</b></font>";
    setHtml(html);

    QCOMPARE(cursor.charFormat().property(QTextFormat::FontSizeAdjustment).toInt(), 4);
    QCOMPARE(cursor.charFormat().fontWeight(), int(QFont::Bold));
}

void tst_QTextDocumentFragment::html_cssFontSize()
{
    const char html[] = "<span style=\"font-size: 50pt\">Foo</span>";
    setHtml(html);

    QCOMPARE(cursor.charFormat().property(QTextFormat::FontPointSize).toInt(), 50);

    const char html2[] = "<span style=\"font-size: 50px\">Foo</span>";
    setHtml(html2);

    QCOMPARE(cursor.charFormat().property(QTextFormat::FontPixelSize).toInt(), 50);

    const char html3[] = "<span style=\"font-size: large\">Foo</span>";
    setHtml(html3);

    QCOMPARE(cursor.charFormat().property(QTextFormat::FontSizeAdjustment).toInt(), 1);
}

void tst_QTextDocumentFragment::html_cssShorthandFont()
{
    {
        const char html[] = "<span style=\"font: 50px sans-serif\">Foo</span>";
        setHtml(html);
        QCOMPARE(cursor.charFormat().property(QTextFormat::FontPixelSize).toInt(), 50);
        QCOMPARE(cursor.charFormat().property(QTextFormat::FontFamily).toString(), QString("sans-serif"));
    }
    {
        const char html[] = "<span style=\"font: 50pt sans-serif\">Foo</span>";
        setHtml(html);
        QCOMPARE(cursor.charFormat().property(QTextFormat::FontPointSize).toInt(), 50);
        QCOMPARE(cursor.charFormat().property(QTextFormat::FontFamily).toString(), QString("sans-serif"));
    }
    {
        const char html[] = "<span style='font:7.0pt \"Times New Roman\"'>Foo</span>";
        setHtml(html);
        QCOMPARE(cursor.charFormat().property(QTextFormat::FontPointSize).toInt(), 7);
        QCOMPARE(cursor.charFormat().property(QTextFormat::FontFamily).toString(), QString("Times New Roman"));
    }
    {
        const char html[] = "<span style='font:bold 7.0pt'>Foo</span>";
        setHtml(html);
        QCOMPARE(cursor.charFormat().property(QTextFormat::FontWeight).toInt(), int(QFont::Bold));
        QCOMPARE(cursor.charFormat().property(QTextFormat::FontPointSize).toInt(), 7);
    }
    {
        const char html[] = "<span style='font:bold italic 7.0pt'>Foo</span>";
        setHtml(html);
        QCOMPARE(cursor.charFormat().property(QTextFormat::FontWeight).toInt(), int(QFont::Bold));
        QCOMPARE(cursor.charFormat().property(QTextFormat::FontItalic).toBool(), true);
    }
}

void tst_QTextDocumentFragment::html_bodyBgColor()
{
    const char html[] = "<body bgcolor=\"blue\">Foo</body>";
    doc->setHtml(html);

    QVERIFY(doc->rootFrame()->frameFormat().background().color() == Qt::blue);
}

void tst_QTextDocumentFragment::html_qtBgColor()
{
    const char html[] = "<qt bgcolor=\"blue\">Foo</qt>";
    doc->setHtml(html);

    QVERIFY(doc->rootFrame()->frameFormat().background().color() == Qt::blue);
}

void tst_QTextDocumentFragment::html_bodyBackground()
{
    const char html[] = "<body background=\"foo.png\">Foo</body>";
    doc->setHtml(html);

    QVERIFY(doc->rootFrame()->frameFormat().background().style() == Qt::TexturePattern);
}

void tst_QTextDocumentFragment::html_tableCellBackground()
{
    const char html[] = "<body><table><tr><td background=\"foo.png\">Foo</td></tr></table></body>";
    doc->setHtml(html);

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);

    QTextTableCell cell = table->cellAt(0, 0);
    QVERIFY(cell.format().background().style() == Qt::TexturePattern);
}

void tst_QTextDocumentFragment::css_bodyBackground()
{
    const char html[] = "<body style=\"background-image:url('foo.png')\">Foo</body>";
    doc->setHtml(html);

    QVERIFY(doc->rootFrame()->frameFormat().background().style() == Qt::TexturePattern);
}

void tst_QTextDocumentFragment::css_tableCellBackground()
{
    const char html[] = "<body><table><tr><td style=\"background-image:url('foo.png')\">Foo</td></tr></table></body>";
    doc->setHtml(html);

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);

    QTextTableCell cell = table->cellAt(0, 0);
    QVERIFY(cell.format().background().style() == Qt::TexturePattern);
}

void tst_QTextDocumentFragment::css_cellPaddings()
{
    const char html[] = "<body><table><tr><td style=\"padding-left:1\">Foo</td>"
                        "<td style=\"padding-right:1\"></td><td style=\"padding-top:10\"></td>"
                        "<td style=\"padding-bottom:5\"></td><td style=\"padding:15\"></td></tr></table></body>";
    doc->setHtml(html);

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);

    QTextTableCell cell = table->cellAt(0, 0);
    QCOMPARE(cell.format().toTableCellFormat().leftPadding(), qreal(1));
    cell = table->cellAt(0, 1);
    QCOMPARE(cell.format().toTableCellFormat().rightPadding(), qreal(1));
    cell = table->cellAt(0, 2);
    QCOMPARE(cell.format().toTableCellFormat().topPadding(), qreal(10));
    cell = table->cellAt(0, 3);
    QCOMPARE(cell.format().toTableCellFormat().bottomPadding(), qreal(5));
    cell = table->cellAt(0, 4);
    QCOMPARE(cell.format().toTableCellFormat().leftPadding(), qreal(15));
    QCOMPARE(cell.format().toTableCellFormat().rightPadding(), qreal(15));
    QCOMPARE(cell.format().toTableCellFormat().topPadding(), qreal(15));
    QCOMPARE(cell.format().toTableCellFormat().bottomPadding(), qreal(15));
}

void tst_QTextDocumentFragment::html_blockLevelDiv()
{
    const char html[] = "<div align=right><b>Hello World";
    setHtml(html);

    QCOMPARE(doc->begin().blockFormat().alignment(), Qt::AlignRight|Qt::AlignAbsolute);
    QVERIFY(doc->begin().next() == doc->end());
}

void tst_QTextDocumentFragment::html_spanNesting()
{
    const char html[] = "<span style=\"color:black\">a<span style=\"color:red\">b<span style=\"color:black\">c</span></span>d</span>";
    setHtml(html);

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().foreground() == Qt::black);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().foreground() == Qt::red);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().foreground() == Qt::black);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().foreground() == Qt::black);
}

void tst_QTextDocumentFragment::html_nestedLists()
{
    const char html[] = "<p><ul><li>Foo<ul><li>In nested list</li></ul></li><li>Last item</li></ul></p>";
    setHtml(html);

    cursor.movePosition(QTextCursor::Start);
    QTextList *firstList = cursor.currentList();
    QVERIFY(firstList);
    QCOMPARE(firstList->format().indent(), 1);

    cursor.movePosition(QTextCursor::NextBlock);
    QTextList *secondList = cursor.currentList();
    QVERIFY(secondList);
    QVERIFY(secondList != firstList);
    QCOMPARE(cursor.currentList()->format().indent(), 2);

    cursor.movePosition(QTextCursor::NextBlock);
    QTextList *thirdList = cursor.currentList();
    QVERIFY(thirdList);
    QVERIFY(thirdList == firstList);
}

void tst_QTextDocumentFragment::noSpecialCharactersInPlainText()
{
    cursor.insertTable(2, 2);
    cursor.insertBlock();
    cursor.insertText(QString(QChar::LineSeparator));
    cursor.insertText(QString(QChar::Nbsp));

    QString plain = doc->toPlainText();
    QVERIFY(!plain.contains(QChar::ParagraphSeparator));
    QVERIFY(!plain.contains(QChar::Nbsp));
    QVERIFY(!plain.contains(QTextBeginningOfFrame));
    QVERIFY(!plain.contains(QTextEndOfFrame));
    QVERIFY(!plain.contains(QChar::LineSeparator));

    plain = QTextDocumentFragment(doc).toPlainText();
    QVERIFY(!plain.contains(QChar::ParagraphSeparator));
    QVERIFY(!plain.contains(QChar::Nbsp));
    QVERIFY(!plain.contains(QTextBeginningOfFrame));
    QVERIFY(!plain.contains(QTextEndOfFrame));
    QVERIFY(!plain.contains(QChar::LineSeparator));
}

void tst_QTextDocumentFragment::html_doNotInheritBackground()
{
    const char html[] = "<html><body bgcolor=\"blue\"><p>Blah</p></body></html>";
    doc->setHtml(html);

    for (QTextBlock block = doc->begin();
         block.isValid(); block = block.next()) {
        QVERIFY(block.blockFormat().hasProperty(QTextFormat::BackgroundBrush) == false);
    }

    QVERIFY(doc->rootFrame()->frameFormat().hasProperty(QTextFormat::BackgroundBrush));
    QVERIFY(doc->rootFrame()->frameFormat().background().color() == Qt::blue);
}

void tst_QTextDocumentFragment::html_inheritBackgroundToInlineElements()
{
    const char html[] = "<span style=\"background: blue\">Foo<span>Bar</span></span>";
    doc->setHtml(html);

    int fragmentCount = 0;

    QTextBlock block = doc->begin();
    for (QTextBlock::Iterator it = block.begin();
         !it.atEnd(); ++it, ++fragmentCount) {

        const QTextFragment fragment = it.fragment();
        if (fragmentCount == 0) {
            QCOMPARE(fragment.text(), QString("FooBar"));
            QVERIFY(fragment.charFormat().background().color() == Qt::blue);
        }
    }

    QCOMPARE(fragmentCount, 1);
}

void tst_QTextDocumentFragment::html_doNotInheritBackgroundFromBlockElements()
{
    const char html[] = "<p style=\"background: blue\"><span>Foo</span></span>";
    doc->setHtml(html);

    int fragmentCount = 0;

    QTextBlock block = doc->begin();
    for (QTextBlock::Iterator it = block.begin();
         !it.atEnd(); ++it, ++fragmentCount) {

        const QTextFragment fragment = it.fragment();
        if (fragmentCount == 0) {
            QCOMPARE(fragment.text(), QString("Foo"));
            QVERIFY(!fragment.charFormat().hasProperty(QTextFormat::BackgroundBrush));
        }
    }

    QCOMPARE(fragmentCount, 1);
}
void tst_QTextDocumentFragment::html_nobr()
{
    const QString input = "Blah Foo    Bar";
    const QString html = QString::fromLatin1("<html><body><p><nobr>") + input + QString::fromLatin1("</nobr></p></body></html>");
    setHtml(html);

    QString text = doc->begin().begin().fragment().text();
    QString expectedText = input;
    expectedText.replace(QRegExp("\\s+"), QString(QChar::Nbsp));
    QCOMPARE(text, expectedText);
}

void tst_QTextDocumentFragment::fromPlainText()
{
    QString plainText;
    plainText = "Hello\nWorld\r\nBlub";
    plainText += QChar::ParagraphSeparator;
    // TextEdit on OS 10 gives us OS 9 style linefeeds
    // when copy & pasteing multi-line plaintext.
    plainText += "OS9IsOldSchool\r";
    plainText += "Last Parag";

    doc->setPlainText(plainText);

    int blockCount = 0;
    for (QTextBlock block = doc->begin(); block.isValid(); block = block.next()) {
        QVERIFY(!block.text().contains(QLatin1Char('\n')));
        QVERIFY(!block.text().contains(QLatin1Char('\r')));
        QVERIFY(!block.text().contains(QChar::ParagraphSeparator));

        if (blockCount == 0)
            QCOMPARE(block.text(), QString("Hello"));
        else if (blockCount == 1)
            QCOMPARE(block.text(), QString("World"));
        else if (blockCount == 2)
            QCOMPARE(block.text(), QString("Blub"));
        else if (blockCount == 3)
            QCOMPARE(block.text(), QString("OS9IsOldSchool"));
        else if (blockCount == 4)
            QCOMPARE(block.text(), QString("Last Parag"));


        ++blockCount;
    }

    QCOMPARE(blockCount, 5);
}

void tst_QTextDocumentFragment::fromPlainText2()
{
    doc->setPlainText("Hello World");
    QCOMPARE(QTextDocumentFragment(doc).toPlainText(), doc->toPlainText());
}

void tst_QTextDocumentFragment::html_closingImageTag()
{
    const char html[] = "<span style=\"font-size: 10pt\"><span style=\"font-size: 40pt\">Blah<img src=\"blah\"></img>Foo</span></span>";
    setHtml(html);

    int fragmentCount = 0;

    QTextBlock block = doc->begin();
    for (QTextBlock::Iterator it = block.begin();
         !it.atEnd(); ++it, ++fragmentCount) {

        const QTextFragment fragment = it.fragment();
        if (fragmentCount == 0) {
            QCOMPARE(fragment.text(), QString("Blah"));
            QCOMPARE(fragment.charFormat().fontPointSize(), qreal(40));
        } else if (fragmentCount == 1) {
            QCOMPARE(fragment.text(), QString(QChar::ObjectReplacementCharacter));
        } else if (fragmentCount == 2) {
            QCOMPARE(fragment.text(), QString("Foo"));
            QCOMPARE(fragment.charFormat().fontPointSize(), qreal(40));
        }
    }

    QCOMPARE(fragmentCount, 3);
}

void tst_QTextDocumentFragment::html_emptyDocument()
{
    const char html[] = "<html><body><p style=\"-qt-paragraph-type:empty;\"></p></body></html>";
    setHtml(html);
    QCOMPARE(doc->blockCount(), 1);
}

void tst_QTextDocumentFragment::html_closingTag()
{
    const char html[] = "<i />text";
    setHtml(html);

    QVERIFY(!cursor.charFormat().fontItalic());
}

void tst_QTextDocumentFragment::html_anchorAroundImage()
{
    const char html[] = "<a href=\"http://www.troll.no\"><img src=test.png></a>";
    setHtml(html);

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QTextImageFormat fmt = cursor.charFormat().toImageFormat();
    QCOMPARE(fmt.name(), QString("test.png"));
    QVERIFY(fmt.isAnchor());
    QCOMPARE(fmt.anchorHref(), QString("http://www.troll.no"));
}

void tst_QTextDocumentFragment::html_floatBorder()
{
    const char html[] = "<table border=1.2><tr><td>Foo";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html)));
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(cursor.currentTable());
    QCOMPARE(cursor.currentTable()->format().border(), qreal(1.2));
}

void tst_QTextDocumentFragment::html_frameImport()
{
    QTextFrameFormat ffmt;
    ffmt.setBorder(1);
    ffmt.setPosition(QTextFrameFormat::FloatRight);
    ffmt.setMargin(2);
    ffmt.setWidth(100);
    ffmt.setHeight(50);
    ffmt.setBackground(QColor("#00ff00"));
    cursor.insertFrame(ffmt);
    cursor.insertText("Hello World");

    QTextDocumentFragment frag(doc);
    cleanup();
    init();
    frag = QTextDocumentFragment::fromHtml(frag.toHtml());
    cursor.insertFragment(frag);

    QList<QTextFrame *> childFrames = doc->rootFrame()->childFrames();
    QVERIFY(childFrames.count() == 1);
    QTextFrame *frame = childFrames.first();
    QCOMPARE(frame->frameFormat().margin(), ffmt.margin());
    QCOMPARE(frame->frameFormat().border(), ffmt.border());
}

void tst_QTextDocumentFragment::html_frameImport2()
{
    QTextFrameFormat ffmt;
    ffmt.setBorder(1);
    ffmt.setPosition(QTextFrameFormat::FloatRight);
    ffmt.setLeftMargin(200);
    ffmt.setTopMargin(100);
    ffmt.setBottomMargin(50);
    ffmt.setRightMargin(250);
    ffmt.setWidth(100);
    ffmt.setHeight(50);
    ffmt.setBackground(QColor("#00ff00"));
    cursor.insertFrame(ffmt);
    cursor.insertText("Hello World");

    QTextDocumentFragment frag(doc);
    cleanup();
    init();
    frag = QTextDocumentFragment::fromHtml(frag.toHtml());
    cursor.insertFragment(frag);

    QList<QTextFrame *> childFrames = doc->rootFrame()->childFrames();
    QVERIFY(childFrames.count() == 1);
    QTextFrame *frame = childFrames.first();
    QCOMPARE(frame->frameFormat().topMargin(), ffmt.topMargin());
    QCOMPARE(frame->frameFormat().bottomMargin(), ffmt.bottomMargin());
    QCOMPARE(frame->frameFormat().leftMargin(), ffmt.leftMargin());
    QCOMPARE(frame->frameFormat().rightMargin(), ffmt.rightMargin());
    QCOMPARE(frame->frameFormat().border(), ffmt.border());
}

void tst_QTextDocumentFragment::html_dontAddMarginsAcrossTableCells()
{
    const char html[] = "<table style=\"margin-left: 100px;\"><tr><td><p style=\"margin-left:50px;\">Foo</p></td></tr></table>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html)));

    QList<QTextFrame *> childFrames = doc->rootFrame()->childFrames();
    QVERIFY(childFrames.count() == 1);
    QTextFrame *frame = childFrames.first();
    cursor = frame->firstCursorPosition();
    QCOMPARE(cursor.blockFormat().leftMargin(), qreal(50.0));
}

void tst_QTextDocumentFragment::html_dontMergeCenterBlocks()
{
    const char html[] = "<center>This should be centered</center>And this should not be centered anymore";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html)));

    QCOMPARE(doc->blockCount(), 2);
    QTextBlock blk = doc->begin();
    QVERIFY(blk.blockFormat().alignment() == Qt::AlignCenter);
    blk = blk.next();
    QVERIFY(blk.blockFormat().alignment() != Qt::AlignCenter);
}

void tst_QTextDocumentFragment::html_tableCellBgColor()
{
    const char html[] = "<table><tr><td bgcolor=\"blue\">Test<p>Second Parag</p></td></tr></table>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html)));

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);

    QTextTableCell cell = table->cellAt(0, 0);
    QVERIFY(cell.format().background().color() == Qt::blue);
}

void tst_QTextDocumentFragment::html_tableCellBgColor2()
{
    const char html[] = "<table><tr><td bgcolor=\"blue\"><table><tr><td>Blah</td></tr></table></td></tr></table>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html)));

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);

    QTextTableCell cell = table->cellAt(0, 0);
    QVERIFY(cell.format().background().color() == Qt::blue);

    QTextFrame::Iterator it = cell.begin();
    QVERIFY(!it.atEnd());
    QVERIFY(it.currentFrame() == 0);
    QVERIFY(it.currentBlock().isValid());

    ++it;
    QVERIFY(!it.atEnd());
    QVERIFY(it.currentFrame() != 0);
    QVERIFY(!it.currentBlock().isValid());

    ++it;
    QVERIFY(!it.atEnd());
    QVERIFY(it.currentFrame() == 0);
    QVERIFY(it.currentBlock().isValid());
    QVERIFY(it.currentBlock().blockFormat().background() == QBrush(Qt::NoBrush));

    ++it;
    QVERIFY(it.atEnd());
}

void tst_QTextDocumentFragment::html_cellSkip()
{
    const char html[] = ""
"<table border>"
"  <tr>"
"    <td>First Cell</td>"
"  </tr>"
"  <tr>"
"    <td>Second Cell</td>"
"    <td>Third Cell</td>"
"  </tr>"
"</table>";

    setHtml(html);
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QVERIFY(table->columns() == 2 && table->rows() == 2);

    QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First Cell"));
    QVERIFY(table->cellAt(0, 1).firstCursorPosition().block().text().isEmpty());
    QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Second Cell"));
    QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Third Cell"));
}

void tst_QTextDocumentFragment::nonZeroMarginOnImport()
{
    // specify bgcolor so that the html import creates a root frame format
    setHtml("<html><body bgcolor=\"#00ff00\"><b>Hello World</b></body></html>");
    QVERIFY(doc->rootFrame()->frameFormat().margin() > 0.0);
}

void tst_QTextDocumentFragment::html_charFormatPropertiesUnset()
{
    setHtml("Hello World");
    QVERIFY(doc->begin().begin().fragment().charFormat().properties().isEmpty());
}

void tst_QTextDocumentFragment::html_headings()
{
    setHtml("<h1>foo</h1>bar");
    QCOMPARE(doc->blockCount(), 2);
}

void tst_QTextDocumentFragment::html_quotedFontFamily()
{
    setHtml("<div style=\"font-family: 'Foo Bar';\">Test</div>");
    QCOMPARE(doc->begin().begin().fragment().charFormat().fontFamily(), QString("Foo Bar"));

    setHtml("<div style='font-family: \"Foo Bar\";'>Test</div>");
    QCOMPARE(doc->begin().begin().fragment().charFormat().fontFamily(), QString("Foo Bar"));
}

void tst_QTextDocumentFragment::defaultFont()
{
    QFont f;
    f.setFamily("Courier New");
    f.setBold(true);
    f.setItalic(true);
    f.setStrikeOut(true); // set here but deliberately ignored for the html export
    f.setPointSize(100);
    doc->setDefaultFont(f);
    doc->setPlainText("Hello World");
    const QString html = doc->toHtml();
    QLatin1String str("<body style=\" font-family:'Courier New'; font-size:100pt; font-weight:600; font-style:italic;\">");
    QVERIFY(html.contains(str));
}

void tst_QTextDocumentFragment::html_spanBackgroundColor()
{
    setHtml("<span style=\"background-color: blue\">Foo</span>");
    QVERIFY(doc->begin().begin().fragment().charFormat().background().color() == QColor(Qt::blue));
}

void tst_QTextDocumentFragment::html_brokenTitle_data()
{
    QTest::addColumn<QString>("html");
    QTest::addColumn<QString>("expectedBody");
    QTest::addColumn<QString>("expectedTitle");

    QTest::newRow("brokentitle") << QString("<html><head><title>Foo<b>bar</b></title></head><body>Blah</body></html>")
                                 << QString("Blah") << QString("Foo");
    QTest::newRow("brokentitle2") << QString("<html><head><title>Foo<font color=red>i</font>t<font color=red>i</font>Blub</title></head><body>Blah</body></html>")
                                 << QString("Blah") << QString("Foo");
    QTest::newRow("entities") << QString("<html><head><title>Foo&lt;bar</title></head><body>Blah</body></html>")
                              << QString("Blah") << QString("Foo<bar");
    QTest::newRow("unclosedtitle") << QString("<html><head><title>Foo</head><body>Blah</body></html>")
                                   << QString("Blah") << QString("Foo");
}

void tst_QTextDocumentFragment::html_brokenTitle()
{
    QFETCH(QString, html);
    QFETCH(QString, expectedBody);
    QFETCH(QString, expectedTitle);
    doc->setHtml(html);
    QCOMPARE(doc->toPlainText(), expectedBody);
    QCOMPARE(doc->metaInformation(QTextDocument::DocumentTitle), expectedTitle);
}

void tst_QTextDocumentFragment::html_blockVsInline()
{
    {
        setHtml("<html><body><div><b>Foo<div>Bar");
        QVERIFY(cursor.charFormat().fontWeight() == QFont::Bold);
        QVERIFY(cursor.blockCharFormat().fontWeight() == QFont::Bold);
    }
    {
        setHtml("<html><body><p><b>Foo<p>Bar");
        QVERIFY(cursor.charFormat().fontWeight() != QFont::Bold);
        QVERIFY(cursor.blockCharFormat().fontWeight() != QFont::Bold);
    }
    {
        setHtml("<html><body><b><center>Foo</center></b>");
        QVERIFY(cursor.charFormat().fontWeight() == QFont::Bold);
        QVERIFY(cursor.blockCharFormat().fontWeight() == QFont::Bold);
    }
    {
        setHtml("<html><body><b><p>Foo");
        QVERIFY(cursor.charFormat().fontWeight() == QFont::Bold);
        QVERIFY(cursor.blockCharFormat().fontWeight() == QFont::Bold);
    }
    {
        setHtml("<html><body><b><p>Foo<p>Bar");
        QVERIFY(cursor.charFormat().fontWeight() == QFont::Bold);
        QVERIFY(cursor.blockCharFormat().fontWeight() == QFont::Bold);
    }
    {
        setHtml("<div><b>Foo<div>Bar");
        QVERIFY(cursor.charFormat().fontWeight() == QFont::Bold);
        QVERIFY(cursor.blockCharFormat().fontWeight() == QFont::Bold);
    }
    {
        setHtml("<p><b>Foo<p>Bar");
        QVERIFY(cursor.charFormat().fontWeight() != QFont::Bold);
        QVERIFY(cursor.blockCharFormat().fontWeight() != QFont::Bold);
    }
    {
        setHtml("<b><center>Foo</center></b>");
        QVERIFY(cursor.charFormat().fontWeight() == QFont::Bold);
        QVERIFY(cursor.blockCharFormat().fontWeight() == QFont::Bold);
    }
    {
        setHtml("<b><p>Foo");
        QVERIFY(cursor.charFormat().fontWeight() == QFont::Bold);
        QVERIFY(cursor.blockCharFormat().fontWeight() == QFont::Bold);
    }
    {
        setHtml("<b><p>Foo<p>Bar");
        QVERIFY(cursor.charFormat().fontWeight() == QFont::Bold);
        QVERIFY(cursor.blockCharFormat().fontWeight() == QFont::Bold);
    }
}

void tst_QTextDocumentFragment::html_tbody()
{
    setHtml("<table><thead><tr><td>First Cell</td></tr></thead><tbody><tr><td>Second Cell</td></tr></tbody></table>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->columns(), 1);
    QCOMPARE(table->rows(), 2);
    QCOMPARE(table->format().headerRowCount(), 1);
    QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First Cell"));
    QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Second Cell"));
}

void tst_QTextDocumentFragment::html_nestedTables()
{
    setHtml("<table>"
            "  <tr><td>"
            ""
            "    <table>"
            "      <tr><td>Hello</td></tr>"
            "    </table>"
            ""
            "    <table>"
            "      <tr><td>World</td></tr>"
            "    </table>"
            ""
            "  </td></tr>"
            "</table>"
           );

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 1);
    QCOMPARE(table->columns(), 1);

    cursor = table->cellAt(0, 0).firstCursorPosition();
    cursor.movePosition(QTextCursor::NextBlock);

    QTextTable *firstNestedTable = cursor.currentTable();
    QVERIFY(firstNestedTable);
    QVERIFY(firstNestedTable->parentFrame() == table);
    QCOMPARE(firstNestedTable->rows(), 1);
    QCOMPARE(firstNestedTable->columns(), 1);
    QCOMPARE(firstNestedTable->cellAt(0, 0).firstCursorPosition().block().text(), QString("Hello"));

    while (cursor.currentTable() == firstNestedTable
           && cursor.movePosition(QTextCursor::NextBlock))
        ;

    QVERIFY(!cursor.isNull());
    QVERIFY(cursor.currentTable() == table);

    cursor.movePosition(QTextCursor::NextBlock);

    QTextTable *secondNestedTable = cursor.currentTable();
    QVERIFY(secondNestedTable);
    QVERIFY(secondNestedTable->parentFrame() == table);
    QCOMPARE(secondNestedTable->rows(), 1);
    QCOMPARE(secondNestedTable->columns(), 1);
    QCOMPARE(secondNestedTable->cellAt(0, 0).firstCursorPosition().block().text(), QString("World"));
}

void tst_QTextDocumentFragment::html_rowSpans()
{
    setHtml(""
            "<table border=\"1\" width=\"100%\">"
            "  <tr>"
            "    <td rowspan=\"2\">blah</td>"
            "    <td rowspan=\"2\">foo</td>"
            "  </tr>"
            "  <tr></tr>"
            "  <tr>"
            "    <td rowspan=\"2\">blubb</td>"
            "    <td rowspan=\"2\">baz</td>"
            "  </tr>"
            "  <tr></tr>"
            "</table>");

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 4);
    QCOMPARE(table->columns(), 2);

    QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("blah"));
    QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("foo"));

    QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("blah"));
    QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("foo"));

    QCOMPARE(table->cellAt(2, 0).firstCursorPosition().block().text(), QString("blubb"));
    QCOMPARE(table->cellAt(2, 1).firstCursorPosition().block().text(), QString("baz"));

    QCOMPARE(table->cellAt(3, 0).firstCursorPosition().block().text(), QString("blubb"));
    QCOMPARE(table->cellAt(3, 1).firstCursorPosition().block().text(), QString("baz"));
}

void tst_QTextDocumentFragment::html_rowSpans2()
{
    setHtml(""
            "<html><body>"
            "<table border=\"1\">"
            "<tr>"
            "<td>Row 1 col 1</td>"
            "</tr>"
            "<tr>"
            "<td rowspan=\"3\">Row 2 col 1, rowspan 3</td>"
            "<td>Row 2 col 2</td>"
            "</tr>"
            "<tr>"
            "<td rowspan=\"2\">Row 3 col 2, rowspan 2</td>"
            "</tr>"
            "<tr>"
            "</tr>"
            "</table>"
            "</body></html>");

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 4);
    QCOMPARE(table->columns(), 2);
    QCOMPARE(table->cellAt(0, 1).rowSpan(), 1);
    QCOMPARE(table->cellAt(1, 0).rowSpan(), 3);
    QCOMPARE(table->cellAt(2, 1).rowSpan(), 2);
}

void tst_QTextDocumentFragment::html_implicitParagraphs()
{
    setHtml("<p>foo</p>bar");
    QCOMPARE(doc->blockCount(), 2);
}

void tst_QTextDocumentFragment::html_missingCloseTag()
{
    setHtml("<font color=\"red\"><span style=\"color:blue\">blue</span></span>&nbsp;red</font>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().foreground().color() == Qt::blue);
    cursor.movePosition(QTextCursor::NextWord);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().foreground().color() == Qt::red);
}

void tst_QTextDocumentFragment::html_anchorColor()
{
    setHtml("<span style=\"color: red;\">Red</span>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().foreground().color() == Qt::red);

    setHtml("<span style=\"color: red;\"><a href=\"http://www.kde.org/\">Blue</a></span>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().foreground().color() == QApplication::palette().link().color());

    setHtml("<span style=\"color: red;\"><a href=\"http://www.kde.org/\" style=\"color: yellow;\">Green</a></span>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().foreground().color() == Qt::yellow);
}

void tst_QTextDocumentFragment::html_lastParagraphClosing()
{
    setHtml("<p>Foo<b>Bar</b>Baz");
    QCOMPARE(doc->blockCount(), 1);
}

void tst_QTextDocumentFragment::html_tableHeaderBodyFootParent()
{
    // don't get confused by strange tags, keep tbody/thead/tfoot children of
    // the table tag
    setHtml("<table><col><col><col><tbody><tr><td>Hey</td></tr></tbody></table>");

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->columns(), 1);
    QCOMPARE(table->rows(), 1);
    QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("Hey"));

    setHtml("<table><col><col><col><thead><tr><td>Hey</td></tr></thead></table>");

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->columns(), 1);
    QCOMPARE(table->rows(), 1);
    QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("Hey"));

    setHtml("<table><col><col><col><tfoot><tr><td>Hey</td></tr></tfoot></table>");

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->columns(), 1);
    QCOMPARE(table->rows(), 1);
    QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("Hey"));
}

void tst_QTextDocumentFragment::html_columnWidths()
{
    setHtml("<table>"
            " <tr>"
            "   <td colspan=\"2\">Foo</td>"
            " </tr>"
            " <tr>"
            "   <td>Bar</td>"
            "   <td width=\"1%\">Baz</td>"
            " </tr>"
            "</table>");

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->columns(), 2);
    QCOMPARE(table->rows(), 2);
    QTextTableFormat fmt = table->format();

    const QVector<QTextLength> columnWidths = fmt.columnWidthConstraints();
    QCOMPARE(columnWidths.count(), 2);
    QVERIFY(columnWidths.at(0).type() == QTextLength::VariableLength);
    QVERIFY(columnWidths.at(1).type() == QTextLength::PercentageLength);
    QVERIFY(columnWidths.at(1).rawValue() == 1);
}

void tst_QTextDocumentFragment::css_fontWeight()
{
    setHtml("<p style=\"font-weight:bold\">blah</p>");
    QVERIFY(doc->begin().charFormat().fontWeight() == QFont::Bold);
    setHtml("<p style=\"font-weight:600\">blah</p>");
    QVERIFY(doc->begin().charFormat().fontWeight() == QFont::Bold);

}

void tst_QTextDocumentFragment::css_float()
{
    setHtml("<img src=\"foo\" style=\"float: right\">");
    QTextCharFormat fmt = doc->begin().begin().fragment().charFormat();
    QVERIFY(fmt.isImageFormat());
    QTextObject *o = doc->objectForFormat(fmt);
    QVERIFY(o);
    QTextFormat f = o->format();
    QVERIFY(f.isFrameFormat());
    QVERIFY(f.toFrameFormat().position() == QTextFrameFormat::FloatRight);

    setHtml("<img src=\"foo\" align=right>");
    fmt = doc->begin().begin().fragment().charFormat();
    QVERIFY(fmt.isImageFormat());
    o = doc->objectForFormat(fmt);
    QVERIFY(o);
    f = o->format();
    QVERIFY(f.isFrameFormat());
    QVERIFY(f.toFrameFormat().position() == QTextFrameFormat::FloatRight);

    setHtml("<img src=\"foo\" align=left>");
    fmt = doc->begin().begin().fragment().charFormat();
    QVERIFY(fmt.isImageFormat());
    o = doc->objectForFormat(fmt);
    QVERIFY(o);
    f = o->format();
    QVERIFY(f.isFrameFormat());
    QVERIFY(f.toFrameFormat().position() == QTextFrameFormat::FloatLeft);
}

void tst_QTextDocumentFragment::css_textIndent()
{
    setHtml("<p style=\"text-indent: 42px\">foo</p>");
    QTextBlockFormat fmt = doc->begin().blockFormat();
    QCOMPARE(fmt.textIndent(), qreal(42));
}

void tst_QTextDocumentFragment::css_inline()
{
    setHtml(""
            "<style>"
            " p { background-color: green;}"
            "</style>"
            "<p>test</p>"
            );
    QTextBlockFormat fmt = doc->begin().blockFormat();
    QVERIFY(fmt.background().color() == QColor("green"));
}

void tst_QTextDocumentFragment::css_external()
{
    doc->addResource(QTextDocument::StyleSheetResource, QUrl("test.css"), QString("p { background-color: green; }"));
    doc->setHtml(""
            "<link href=\"test.css\" type=\"text/css\" />"
            "<p>test</p>"
            );
    QTextBlockFormat fmt = doc->begin().blockFormat();
    QVERIFY(fmt.background().color() == QColor("green"));
}

void tst_QTextDocumentFragment::css_import()
{
    doc->addResource(QTextDocument::StyleSheetResource, QUrl("test.css"), QString("@import \"other.css\";"));
    doc->addResource(QTextDocument::StyleSheetResource, QUrl("other.css"), QString("@import url(\"other2.css\");"));
    doc->addResource(QTextDocument::StyleSheetResource, QUrl("other2.css"), QString("p { background-color: green; }"));
    doc->setHtml(""
            "<link href=\"test.css\" type=\"text/css\" />"
            "<p>test</p>"
            );
    QTextBlockFormat fmt = doc->begin().blockFormat();
    QVERIFY(fmt.background().color() == QColor("green"));

    doc->setHtml(""
            "<style>@import \"test.css\" screen;</style>"
            "<p>test</p>"
            );
    fmt = doc->begin().blockFormat();
    QVERIFY(fmt.background().color() == QColor("green"));
}

void tst_QTextDocumentFragment::css_selectors_data()
{
    QTest::addColumn<bool>("match");
    QTest::addColumn<QString>("selector");
    QTest::addColumn<QString>("attributes");

    QTest::newRow("plain") << true << QString() << QString();

    QTest::newRow("class") << true << QString(".foo") << QString("class=foo");
    QTest::newRow("notclass") << false << QString(".foo") << QString("class=bar");

    QTest::newRow("attrset") << true << QString("[justset]") << QString("justset");
    QTest::newRow("notattrset") << false << QString("[justset]") << QString("otherattribute");

    QTest::newRow("attrmatch") << true << QString("[foo=bar]") << QString("foo=bar");
    QTest::newRow("noattrmatch") << false << QString("[foo=bar]") << QString("foo=xyz");

    QTest::newRow("contains") << true << QString("[foo~=bar]") << QString("foo=\"baz bleh bar\"");
    QTest::newRow("notcontains") << false << QString("[foo~=bar]") << QString("foo=\"test\"");

    QTest::newRow("beingswith") << true << QString("[foo|=bar]") << QString("foo=\"bar-bleh\"");
    QTest::newRow("notbeingswith") << false << QString("[foo|=bar]") << QString("foo=\"bleh-bar\"");

    QTest::newRow("attr2") << true << QString("[bar=foo]") << QString("bleh=bar bar=foo");
}

void tst_QTextDocumentFragment::css_selectors()
{
    QFETCH(bool, match);
    QFETCH(QString, selector);
    QFETCH(QString, attributes);

    QString html = QString(""
            "<style>"
            " p { background-color: green }"
            " p%1 { background-color: red }"
            "</style>"
            "<p %2>test</p>"
            ).arg(selector).arg(attributes);
    setHtml(html);

    QTextBlockFormat fmt = doc->begin().blockFormat();
    if (match)
        QVERIFY(fmt.background().color() == QColor("red"));
    else
        QVERIFY(fmt.background().color() == QColor("green"));
}

void tst_QTextDocumentFragment::css_nodeNameCaseInsensitivity()
{
    setHtml("<style>"
            "P { background-color: green }"
            "</style>"
            "<p>test</p>");
    QTextBlockFormat fmt = doc->begin().blockFormat();
    QVERIFY(fmt.background().color() == QColor("green"));
}

void tst_QTextDocumentFragment::css_textUnderlineStyle_data()
{
    QTest::addColumn<QString>("styleName");
    QTest::addColumn<int>("expectedStyle");

    QTest::newRow("none") << QString("none") << int(QTextCharFormat::NoUnderline);
    QTest::newRow("solid") << QString("solid") << int(QTextCharFormat::SingleUnderline);
    QTest::newRow("dash") << QString("dashed") << int(QTextCharFormat::DashUnderline);
    QTest::newRow("dot") << QString("dotted") << int(QTextCharFormat::DotLine);
    QTest::newRow("dashdot") << QString("dot-dash") << int(QTextCharFormat::DashDotLine);
    QTest::newRow("dashdotdot") << QString("dot-dot-dash") << int(QTextCharFormat::DashDotDotLine);
    QTest::newRow("wave") << QString("wave") << int(QTextCharFormat::WaveUnderline);
}

void tst_QTextDocumentFragment::css_textUnderlineStyle()
{
    QFETCH(QString, styleName);
    QFETCH(int, expectedStyle);

    QString html = QString::fromLatin1("<span style=\"text-underline-style: %1\">Blah</span>").arg(styleName);
    doc->setHtml(html);

    QTextFragment fragment = doc->begin().begin().fragment();
    QVERIFY(fragment.isValid());
    QCOMPARE(int(fragment.charFormat().underlineStyle()), expectedStyle);
}

void tst_QTextDocumentFragment::css_textUnderlineStyleAndDecoration()
{
    doc->setHtml("<span style=\"text-decoration: overline; text-underline-style: solid\">Test</span>");

    QTextFragment fragment = doc->begin().begin().fragment();
    QVERIFY(fragment.isValid());
    QVERIFY(fragment.charFormat().underlineStyle() == QTextCharFormat::SingleUnderline);
    QVERIFY(fragment.charFormat().fontOverline());

    doc->setHtml("<span style=\"text-underline-style: solid; text-decoration: overline\">Test</span>");

    fragment = doc->begin().begin().fragment();
    QVERIFY(fragment.isValid());
    QVERIFY(fragment.charFormat().underlineStyle() == QTextCharFormat::SingleUnderline);
    QVERIFY(fragment.charFormat().fontOverline());
}

void tst_QTextDocumentFragment::css_listStyleType()
{
    doc->setHtml("<ol style=\"list-style-type: disc\"><li>Blah</li></ol>");
    cursor.movePosition(QTextCursor::End);
    QVERIFY(cursor.currentList());
    QVERIFY(cursor.currentList()->format().style() == QTextListFormat::ListDisc);

    doc->setHtml("<ul style=\"list-style-type: square\"><li>Blah</li></ul>");
    cursor.movePosition(QTextCursor::End);
    QVERIFY(cursor.currentList());
    QVERIFY(cursor.currentList()->format().style() == QTextListFormat::ListSquare);

    doc->setHtml("<ul style=\"list-style-type: circle\"><li>Blah</li></ul>");
    cursor.movePosition(QTextCursor::End);
    QVERIFY(cursor.currentList());
    QVERIFY(cursor.currentList()->format().style() == QTextListFormat::ListCircle);

    doc->setHtml("<ul style=\"list-style-type: decimal\"><li>Blah</li></ul>");
    cursor.movePosition(QTextCursor::End);
    QVERIFY(cursor.currentList());
    QVERIFY(cursor.currentList()->format().style() == QTextListFormat::ListDecimal);

    doc->setHtml("<ul style=\"list-style-type: lower-alpha\"><li>Blah</li></ul>");
    cursor.movePosition(QTextCursor::End);
    QVERIFY(cursor.currentList());
    QVERIFY(cursor.currentList()->format().style() == QTextListFormat::ListLowerAlpha);

    doc->setHtml("<ul style=\"list-style-type: upper-alpha\"><li>Blah</li></ul>");
    cursor.movePosition(QTextCursor::End);
    QVERIFY(cursor.currentList());
    QVERIFY(cursor.currentList()->format().style() == QTextListFormat::ListUpperAlpha);

    // ignore the unsupported list-style-position inside the list-style shorthand property
    doc->setHtml("<ul style=\"list-style: outside decimal\"><li>Blah</li></ul>");
    cursor.movePosition(QTextCursor::End);
    QVERIFY(cursor.currentList());
    QVERIFY(cursor.currentList()->format().style() == QTextListFormat::ListDecimal);
}

void tst_QTextDocumentFragment::css_linkPseudo()
{
    doc->setHtml("<a href=\"foobar\">Blah</a>");
    QVERIFY(doc->begin().begin().fragment().charFormat().fontUnderline());

    doc->setHtml("<style>a { text-decoration: none; }</style><a href=\"foobar\">Blah</a>");
    QVERIFY(!doc->begin().begin().fragment().charFormat().fontUnderline());

    doc->setHtml("<style>a:link { text-decoration: none; }</style><a href=\"foobar\">Blah</a>");
    QVERIFY(!doc->begin().begin().fragment().charFormat().fontUnderline());
}

void tst_QTextDocumentFragment::css_pageBreaks()
{
    doc->setHtml("<p>Foo</p>");
    QVERIFY(doc->begin().blockFormat().pageBreakPolicy() == QTextFormat::PageBreak_Auto);

    doc->setHtml("<p style=\" page-break-before:always;\">Foo</p>");
    QVERIFY(doc->begin().blockFormat().pageBreakPolicy() == QTextFormat::PageBreak_AlwaysBefore);

    doc->setHtml("<p style=\" page-break-after:always;\">Foo</p>");
    QVERIFY(doc->begin().blockFormat().pageBreakPolicy() == QTextFormat::PageBreak_AlwaysAfter);

    doc->setHtml("<p style=\" page-break-before:always; page-break-after:always;\">Foo</p>");
    QVERIFY(doc->begin().blockFormat().pageBreakPolicy() == (QTextFormat::PageBreak_AlwaysAfter | QTextFormat::PageBreak_AlwaysBefore));
}

void tst_QTextDocumentFragment::universalSelectors_data()
{
    QTest::addColumn<bool>("match");
    QTest::addColumn<QString>("selector");
    QTest::addColumn<QString>("attributes");

    QTest::newRow("1") << true << QString("*") << QString();
    QTest::newRow("2") << false << QString() << QString(); // invalid totally empty selector

    QTest::newRow("3") << false << QString("*[foo=bar]") << QString("foo=bleh");
    QTest::newRow("4") << true << QString("*[foo=bar]") << QString("foo=bar");

    QTest::newRow("5") << false << QString("[foo=bar]") << QString("foo=bleh");
    QTest::newRow("6") << true << QString("[foo=bar]") << QString("foo=bar");

    QTest::newRow("7") << true << QString(".charfmt1") << QString("class=charfmt1");
}

void tst_QTextDocumentFragment::universalSelectors()
{
    QFETCH(bool, match);
    QFETCH(QString, selector);
    QFETCH(QString, attributes);

    QString html = QString(""
            "<style>"
            "%1 { background-color: green }"
            "</style>"
            "<p %2>test</p>"
            ).arg(selector).arg(attributes);

    setHtml(html);

    QTextBlockFormat fmt = doc->begin().blockFormat();
    if (match)
        QVERIFY(fmt.background().color() == QColor("green"));
    else
        QVERIFY(!fmt.hasProperty(QTextFormat::BackgroundBrush));
}

void tst_QTextDocumentFragment::screenMedia()
{
    setHtml("<style>"
            "@media screen {"
            "p { background-color: green }"
            "}"
            "</style>"
            "<p>test</p>"
            "");
    QTextBlockFormat fmt = doc->begin().blockFormat();
    QVERIFY(fmt.background().color() == QColor("green"));

    setHtml("<style>"
            "@media foobar {"
            "p { background-color: green }"
            "}"
            "</style>"
            "<p>test</p>"
            "");
    fmt = doc->begin().blockFormat();
    QVERIFY(fmt.background().color() != QColor("green"));

    setHtml("<style>"
            "@media sCrEeN {"
            "p { background-color: green }"
            "}"
            "</style>"
            "<p>test</p>"
            "");
    fmt = doc->begin().blockFormat();
    QVERIFY(fmt.background().color() == QColor("green"));
}

void tst_QTextDocumentFragment::htmlResourceLoading()
{
    const QString html("<link href=\"test.css\" type=\"text/css\" />"
                                   "<p>test</p>");

    QTextDocument tmp;
    tmp.addResource(QTextDocument::StyleSheetResource, QUrl("test.css"), QString("p { background-color: green; }"));
    QTextDocumentFragment frag = QTextDocumentFragment::fromHtml(html, &tmp);
    doc->clear();
    QTextCursor(doc).insertFragment(frag);
    QTextBlockFormat fmt = doc->begin().blockFormat();
    QVERIFY(fmt.background().color() == QColor("green"));
}

void tst_QTextDocumentFragment::someCaseInsensitiveAttributeValues()
{
    const char html1[] = "<ul type=sQUarE><li>Blah</li></ul>";
    setHtml(QString::fromLatin1(html1));
    cursor.movePosition(QTextCursor::End);
    QVERIFY(cursor.currentList());
    QVERIFY(cursor.currentList()->format().style() == QTextListFormat::ListSquare);

    const char html2[] = "<div align=ceNTeR><b>Hello World";
    setHtml(html2);

    QCOMPARE(doc->begin().blockFormat().alignment(), Qt::AlignHCenter);

    const char html3[] = "<p dir=rTL><b>Hello World";
    setHtml(html3);

    QCOMPARE(doc->begin().blockFormat().layoutDirection(), Qt::RightToLeft);
}

class TestDocument : public QTextDocument
{
public:
    inline TestDocument() {}

    QPixmap testPixmap;

    virtual QVariant loadResource(int type, const QUrl &name) {
        if (name.toString() == QLatin1String("testPixmap")) {
            return testPixmap;
        }
        return QTextDocument::loadResource(type, name);
    }
};

void tst_QTextDocumentFragment::backgroundImage()
{
    TestDocument doc;
    doc.testPixmap = QPixmap(100, 100);
    doc.testPixmap.fill(Qt::blue);
    doc.setHtml("<p style=\"background-image: url(testPixmap)\">Hello</p>");
    QBrush bg = doc.begin().blockFormat().background();
    QVERIFY(bg.style() == Qt::TexturePattern);
    QVERIFY(bg.texture().serialNumber() == doc.testPixmap.serialNumber());
}

void tst_QTextDocumentFragment::dontMergePreAndNonPre()
{
    doc->setHtml("<pre>Pre text</pre>Text that should be wrapped");
    QCOMPARE(doc->blockCount(), 2);
    QCOMPARE(doc->begin().text(), QString("Pre text"));
    QCOMPARE(doc->begin().next().text(), QString("Text that should be wrapped"));
}

void tst_QTextDocumentFragment::leftMarginInsideHtml()
{
    doc->setHtml("<html><dl><dd>Blah");
    QCOMPARE(doc->blockCount(), 1);
    QVERIFY(doc->begin().blockFormat().leftMargin() > 0);
}

void tst_QTextDocumentFragment::html_margins()
{
    doc->setHtml("<p style=\"margin-left: 42px\">Test");
    QCOMPARE(doc->blockCount(), 1);
    QCOMPARE(doc->begin().blockFormat().topMargin(), 12.);
    QCOMPARE(doc->begin().blockFormat().bottomMargin(), 12.);
    QCOMPARE(doc->begin().blockFormat().leftMargin(), 42.);
}

void tst_QTextDocumentFragment::newlineInsidePreShouldBecomeNewParagraph()
{
    // rationale: we used to map newlines inside <pre> to QChar::LineSeparator, but
    // if you display a lot of text inside pre it all ended up inside one single paragraph,
    // which doesn't scale very well with our text engine. Paragraphs spanning thousands of
    // lines are not a common use-case otherwise.

    doc->setHtml("<pre>Foo\nBar</pre>");
    QCOMPARE(doc->blockCount(), 2);
    QTextBlock block = doc->begin();
    QCOMPARE(block.blockFormat().topMargin(), qreal(12));
    QVERIFY(qIsNull(block.blockFormat().bottomMargin()));

    block = block.next();

    QVERIFY(qIsNull(block.blockFormat().topMargin()));
    QCOMPARE(block.blockFormat().bottomMargin(), qreal(12));

    doc->setHtml("<pre style=\"margin-top: 32px; margin-bottom: 45px; margin-left: 50px\">Foo\nBar</pre>");
    QCOMPARE(doc->blockCount(), 2);
    block = doc->begin();
    QCOMPARE(block.blockFormat().topMargin(), qreal(32));
    QVERIFY(qIsNull(block.blockFormat().bottomMargin()));
    QCOMPARE(block.blockFormat().leftMargin(), qreal(50));

    block = block.next();

    QVERIFY(qIsNull(block.blockFormat().topMargin()));
    QCOMPARE(block.blockFormat().bottomMargin(), qreal(45));
    QCOMPARE(block.blockFormat().leftMargin(), qreal(50));

}

void tst_QTextDocumentFragment::invalidColspan()
{
    doc->setHtml("<table><tr rowspan=-1><td colspan=-1>Blah</td></tr></table>");

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->columns(), 1);
    QCOMPARE(table->rows(), 1);
}

void tst_QTextDocumentFragment::html_brokenTableWithJustTr()
{
    doc->setHtml("<tr><td>First Cell</td><tr><td>Second Cell");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 2);
    QCOMPARE(table->columns(), 1);
    QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First Cell"));
    QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Second Cell"));

    doc->setHtml(""
        "<col width=286 style='mso-width-source:userset;mso-width-alt:10459;width:215pt'>"
        "<col width=64 span=3 style='width:48pt'>"
        "<tr height=17 style='height:12.75pt'>"
        "<td height=17 width=286 style='height:12.75pt;width:215pt'>1a</td>"
        "<td width=64 style='width:48pt'>1b</td>"
        "<td width=64 style='width:48pt'>1c</td>"
        "<td width=64 style='width:48pt'>1d</td>"
        "</tr>"
        "<tr height=17 style='height:12.75pt'>"
        "<td height=17 style='height:12.75pt'>|2a</td>"
        "<td>2b</td>"
        "<td>2c</td>"
        "<td>2d</td>"
        "</tr>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 2);
    QCOMPARE(table->columns(), 4);
}

void tst_QTextDocumentFragment::html_brokenTableWithJustTd()
{
    doc->setHtml("<td>First Cell</td><td>Second Cell");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 1);
    QCOMPARE(table->columns(), 2);
    QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First Cell"));
    QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Second Cell"));

    doc->setHtml("<td height=17 width=286 style='height:12.75pt;width:215pt'>1a</td>"
                 "<td width=64 style='width:48pt'>1b</td>"
                 "<td width=64 style='width:48pt'>1c</td>"
                 "<td width=64 style='width:48pt'>1d</td>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 1);
    QCOMPARE(table->columns(), 4);
}

void tst_QTextDocumentFragment::html_preNewlineHandling_data()
{
    QTest::addColumn<QString>("html");
    QTest::addColumn<QString>("expectedPlainText");

    QTest::newRow("pre1") << QString("Foo<pre>Bar")
                          << QString("Foo\nBar");
    QTest::newRow("pre2") << QString("Foo<pre>\nBar")
                          << QString("Foo\nBar");
    QTest::newRow("pre2") << QString("Foo<pre>\n\nBar")
                          << QString("Foo\n\nBar");
    QTest::newRow("pre4") << QString("<html>Foo<pre>\nBar")
                          << QString("Foo\nBar");
    QTest::newRow("pre5") << QString("<pre>Foo\n</pre>\nBar")
                          << QString("Foo\nBar");
    QTest::newRow("pre6") << QString("<pre>Foo<b>Bar</b>Blah\n</pre>\nMooh")
                          << QString("FooBarBlah\nMooh");
    QTest::newRow("pre7") << QString("<pre>\nPara1\n</pre>\n<pre>\nPara2\n</pre>")
                          << QString("Para1\nPara2");
}

void tst_QTextDocumentFragment::html_preNewlineHandling()
{
    QFETCH(QString, html);

    doc->setHtml(html);
    QTEST(doc->toPlainText(), "expectedPlainText");
}

void tst_QTextDocumentFragment::html_br()
{
    doc->setHtml("Foo<br><br><br>Blah");
    QCOMPARE(doc->toPlainText(), QString("Foo\n\n\nBlah"));
}

void tst_QTextDocumentFragment::html_dl()
{
    doc->setHtml("<dl><dt>term<dd>data</dl>Text afterwards");
    QCOMPARE(doc->toPlainText(), QString("term\ndata\nText afterwards"));
}

void tst_QTextDocumentFragment::html_tableStrangeNewline()
{
    doc->setHtml("<table><tr><td>Foo</td></tr>\n</table>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 1);
    QCOMPARE(table->columns(), 1);
    const QTextTableCell cell = table->cellAt(0, 0);
    QCOMPARE(cell.firstCursorPosition().block().text(), QString("Foo"));
    QVERIFY(cell.firstCursorPosition().block() == cell.lastCursorPosition().block());
}

void tst_QTextDocumentFragment::html_tableStrangeNewline2()
{
    doc->setHtml("<table><tr><td>Foo</td></tr><tr>\n<td/></tr></table>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 2);
    QCOMPARE(table->columns(), 1);
    const QTextTableCell cell = table->cellAt(0, 0);
    QCOMPARE(cell.firstCursorPosition().block().text(), QString("Foo"));
    QVERIFY(cell.firstCursorPosition().block() == cell.lastCursorPosition().block());
}

void tst_QTextDocumentFragment::html_tableStrangeNewline3()
{
    doc->setHtml("<table border>"
                 "<tr>"
                 "<td>"
                 "<ul>"
                 "<li>Meh</li>"
                 "</ul>"
                 "</td>"
                 "<td>\n"
                 "<ul>"
                 "<li>Foo</li>"
                 "</ul>"
                 "</td>"
                 "</tr>"
                 "</table>");

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 1);
    QCOMPARE(table->columns(), 2);

    QTextTableCell cell = table->cellAt(0, 0);
    QCOMPARE(cell.firstCursorPosition().block().text(), QString("Meh"));
    QVERIFY(cell.firstCursorPosition().block() == cell.lastCursorPosition().block());

    cell = table->cellAt(0, 1);
    QCOMPARE(cell.firstCursorPosition().block().text(), QString("Foo"));
    QVERIFY(cell.firstCursorPosition().block() == cell.lastCursorPosition().block());
}

void tst_QTextDocumentFragment::html_caption()
{
    doc->setHtml("<table border align=center>"
                 "<caption>This <b>   is a</b> Caption!</caption>"
                 "<tr><td>Blah</td></tr>"
                 "</table>");

    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);

    QCOMPARE(cursor.block().text(), QString("This is a Caption!"));
    QVERIFY(cursor.blockFormat().alignment() == Qt::AlignHCenter);

    cursor.movePosition(QTextCursor::NextBlock);
    QTextTable *table = cursor.currentTable();
    QVERIFY(table);
    QCOMPARE(table->rows(), 1);
    QCOMPARE(table->columns(), 1);

    QTextTableCell cell = table->cellAt(0, 0);
    QCOMPARE(cell.firstCursorPosition().block().text(), QString("Blah"));
}

static const uint windowsLatin1ExtendedCharacters[0xA0 - 0x80] = {
    0x20ac, // 0x80
    0x0081, // 0x81 direct mapping
    0x201a, // 0x82
    0x0192, // 0x83
    0x201e, // 0x84
    0x2026, // 0x85
    0x2020, // 0x86
    0x2021, // 0x87
    0x02C6, // 0x88
    0x2030, // 0x89
    0x0160, // 0x8A
    0x2039, // 0x8B
    0x0152, // 0x8C
    0x008D, // 0x8D direct mapping
    0x017D, // 0x8E
    0x008F, // 0x8F directmapping
    0x0090, // 0x90 directmapping
    0x2018, // 0x91
    0x2019, // 0x92
    0x201C, // 0x93
    0X201D, // 0x94
    0x2022, // 0x95
    0x2013, // 0x96
    0x2014, // 0x97
    0x02DC, // 0x98
    0x2122, // 0x99
    0x0161, // 0x9A
    0x203A, // 0x9B
    0x0153, // 0x9C
    0x009D, // 0x9D direct mapping
    0x017E, // 0x9E
    0x0178  // 0x9F
};

void tst_QTextDocumentFragment::html_windowsEntities()
{
    for (uint i = 0; i < sizeof(windowsLatin1ExtendedCharacters)/sizeof(windowsLatin1ExtendedCharacters[0]); ++i) {
        QString html = QString::number(i + 0x80);
        html.prepend("<p>&#");
        html.append(";");
        doc->setHtml(html);
        QCOMPARE(doc->toPlainText(), QString(QChar(windowsLatin1ExtendedCharacters[i])));
    }
}

void tst_QTextDocumentFragment::html_eatenText()
{
    doc->setHtml("<h1>Test1</h1>\nTest2<h1>Test3</h1>");
    cursor.movePosition(QTextCursor::Start);
    QCOMPARE(cursor.block().text(), QString("Test1"));
    cursor.movePosition(QTextCursor::NextBlock);
    QCOMPARE(cursor.block().text(), QString("Test2"));
    cursor.movePosition(QTextCursor::NextBlock);
    QCOMPARE(cursor.block().text(), QString("Test3"));
}

void tst_QTextDocumentFragment::html_hr()
{
    doc->setHtml("<hr />");
    QCOMPARE(doc->blockCount(), 1);
    QVERIFY(doc->begin().blockFormat().hasProperty(QTextFormat::BlockTrailingHorizontalRulerWidth));
}

void tst_QTextDocumentFragment::html_hrMargins()
{
    doc->setHtml("<p>Test<hr/>Blah");
    QCOMPARE(doc->blockCount(), 3);

    cursor.movePosition(QTextCursor::Start);
    QTextBlock block = cursor.block();
    QCOMPARE(block.text(), QString("Test"));
    QVERIFY(block.blockFormat().bottomMargin() <= qreal(12.));
    QTextBlock first = block;

    cursor.movePosition(QTextCursor::NextBlock);
    block = cursor.block();
    QTextBlock hr = block;
    QVERIFY(qMax(first.blockFormat().bottomMargin(), block.blockFormat().topMargin()) > 0);

    cursor.movePosition(QTextCursor::NextBlock);
    block = cursor.block();

    QCOMPARE(block.text(), QString("Blah"));
}

void tst_QTextDocumentFragment::html_blockQuoteMargins()
{
    doc->setHtml("<blockquote>Bar</blockquote>");
    QCOMPARE(doc->blockCount(), 1);
    cursor.movePosition(QTextCursor::Start);
    QTextBlock block = cursor.block();
    QCOMPARE(block.text(), QString("Bar"));
    QCOMPARE(block.blockFormat().leftMargin(), qreal(40.));
    QCOMPARE(block.blockFormat().rightMargin(), qreal(40.));
    QCOMPARE(block.blockFormat().topMargin(), qreal(12.));
    QCOMPARE(block.blockFormat().bottomMargin(), qreal(12.));
}

void tst_QTextDocumentFragment::html_definitionListMargins()
{
    doc->setHtml("Foo<dl><dt>tag<dd>data</dl>Bar");
    QCOMPARE(doc->blockCount(), 4);

    cursor.movePosition(QTextCursor::Start);
    QTextBlock block = cursor.block();
    QCOMPARE(block.text(), QString("Foo"));

    block = block.next();
    QCOMPARE(block.text(), QString("tag"));
    QCOMPARE(block.blockFormat().topMargin(), qreal(8.));

    block = block.next();
    QCOMPARE(block.text(), QString("data"));
    QCOMPARE(block.blockFormat().bottomMargin(), qreal(8.));

    block = block.next();
    QCOMPARE(block.text(), QString("Bar"));
}

void tst_QTextDocumentFragment::html_listMargins()
{
    doc->setHtml("Foo<ol><li>First<li>Second</ol>Bar");
    QCOMPARE(doc->blockCount(), 4);

    cursor.movePosition(QTextCursor::Start);
    QTextBlock block = cursor.block();
    QCOMPARE(block.text(), QString("Foo"));

    block = block.next();
    QCOMPARE(block.text(), QString("First"));
    QCOMPARE(block.blockFormat().topMargin(), qreal(12.));

    block = block.next();
    QCOMPARE(block.text(), QString("Second"));
    QCOMPARE(block.blockFormat().bottomMargin(), qreal(12.));

    block = block.next();
    QCOMPARE(block.text(), QString("Bar"));
}

void tst_QTextDocumentFragment::html_titleAttribute()
{
    doc->setHtml("<span title=\"this is my title\">Test</span>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QCOMPARE(cursor.charFormat().toolTip(), QString("this is my title"));
}

void tst_QTextDocumentFragment::html_compressDivs()
{
    doc->setHtml("<p/><div/><div/><div/><div/>Test");
    QCOMPARE(doc->blockCount(), 1);
    QCOMPARE(doc->begin().text(), QString("Test"));
}

void tst_QTextDocumentFragment::completeToPlainText()
{
    doc->setPlainText("Hello\nWorld");
    QCOMPARE(doc->toPlainText(), QString("Hello\nWorld"));
    QTextDocumentFragment fragment(doc);
    QCOMPARE(fragment.toPlainText(), QString("Hello\nWorld"));
}

void tst_QTextDocumentFragment::copyContents()
{
    doc->setPlainText("Hello");
    QFont f;
    doc->setDefaultFont(f);
    QTextFragment fragment = doc->begin().begin().fragment();
    QCOMPARE(fragment.text(), QString("Hello"));
    QCOMPARE(fragment.charFormat().font().pointSize(), f.pointSize());

    QTextDocumentFragment frag(doc);
    doc->clear();
    f.setPointSize(48);
    doc->setDefaultFont(f);
    QTextCursor(doc).insertFragment(QTextDocumentFragment::fromHtml(frag.toHtml()));
    fragment = doc->begin().begin().fragment();
    QCOMPARE(fragment.text(), QString("Hello"));
    QCOMPARE(fragment.charFormat().font().pointSize(), f.pointSize());
}

void tst_QTextDocumentFragment::html_textAfterHr()
{
    doc->setHtml("<hr><nobr><b>After the centered text</b></nobr>");
    QCOMPARE(doc->blockCount(), 2);
    QTextBlock block = doc->begin();
    QVERIFY(block.text().isEmpty());
    QVERIFY(block.blockFormat().hasProperty(QTextFormat::BlockTrailingHorizontalRulerWidth));
    block = block.next();

    QString txt("After the centered text");
    txt.replace(QLatin1Char(' '), QChar::Nbsp);
    QCOMPARE(block.text(), txt);
    QVERIFY(!block.blockFormat().hasProperty(QTextFormat::BlockTrailingHorizontalRulerWidth));
}

void tst_QTextDocumentFragment::blockTagClosing()
{
    doc->setHtml("<p>foo<p>bar<span>baz</span>");
    QCOMPARE(doc->blockCount(), 2);
    QTextBlock block = doc->begin();
    QCOMPARE(block.text(), QString("foo"));
    block = block.next();
    QCOMPARE(block.text(), QString("barbaz"));
}

void tst_QTextDocumentFragment::isEmpty()
{
    QTextDocumentFragment frag;
    QVERIFY(frag.isEmpty());
    frag = QTextDocumentFragment::fromHtml("test");
    QVERIFY(!frag.isEmpty());
    frag = QTextDocumentFragment::fromHtml("<hr />");
    QVERIFY(!frag.isEmpty());
}

void tst_QTextDocumentFragment::html_alignmentInheritance()
{
    doc->setHtml("<center>Centered text<hr></center><b>After the centered text</b>");
    QCOMPARE(doc->blockCount(), 3);
    QTextBlock block = doc->begin();
    QVERIFY(block.blockFormat().alignment() & Qt::AlignHCenter);
    block = block.next();
    QVERIFY(block.blockFormat().alignment() & Qt::AlignHCenter);
    block = block.next();
    QVERIFY(!(block.blockFormat().alignment() & Qt::AlignHCenter));
}

void tst_QTextDocumentFragment::html_ignoreEmptyDivs()
{
    doc->setHtml("<p><div/><b>Foo</b>");
    QCOMPARE(doc->blockCount(), 1);
    QCOMPARE(doc->begin().text(), QString("Foo"));
}

void tst_QTextDocumentFragment::html_dontInheritAlignmentForFloatingImages()
{
    doc->setHtml("<p align=right><img align=unknownignored src=\"foo\" /></p>");
    QTextCharFormat fmt = doc->begin().begin().fragment().charFormat();
    QVERIFY(fmt.isImageFormat());
    QTextObject *o = doc->objectForFormat(fmt);
    QVERIFY(o);
    QTextFormat f = o->format();
    QVERIFY(f.isFrameFormat());
    QVERIFY(f.toFrameFormat().position() == QTextFrameFormat::InFlow);
}

void tst_QTextDocumentFragment::html_verticalImageAlignment()
{
    doc->setHtml("<img src=\"foo\"/>");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().isImageFormat());
    QTextImageFormat fmt = cursor.charFormat().toImageFormat();
    QVERIFY(fmt.verticalAlignment() == QTextCharFormat::AlignNormal);

    doc->setHtml("<img src=\"foo\" align=middle />");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().isImageFormat());
    fmt = cursor.charFormat().toImageFormat();
    QVERIFY(fmt.verticalAlignment() == QTextCharFormat::AlignMiddle);

    doc->setHtml("<img src=\"foo\" style=\"vertical-align: middle\" />");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().isImageFormat());
    fmt = cursor.charFormat().toImageFormat();
    QVERIFY(fmt.verticalAlignment() == QTextCharFormat::AlignMiddle);

    doc->setHtml("<img src=\"foo\" align=top />");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().isImageFormat());
    fmt = cursor.charFormat().toImageFormat();
    QVERIFY(fmt.verticalAlignment() == QTextCharFormat::AlignTop);

    doc->setHtml("<img src=\"foo\" style=\"vertical-align: top\" />");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().isImageFormat());
    fmt = cursor.charFormat().toImageFormat();
    QVERIFY(fmt.verticalAlignment() == QTextCharFormat::AlignTop);
}

void tst_QTextDocumentFragment::html_verticalCellAlignment()
{
    const char *alt[] =
    {
        // vertical-align property
        "<table>"
        "<tr>"
        "<td style=\"vertical-align: middle\"></td>"
        "<td style=\"vertical-align: top\"></td>"
        "<td style=\"vertical-align: bottom\"></td>"
        "</tr>"
        "</table>",
        // valign property
        "<table>"
        "<tr>"
        "<td valign=\"middle\"></td>"
        "<td valign=\"top\"></td>"
        "<td valign=\"bottom\"></td>"
        "</tr>"
        "</table>",
        // test td override of tr property
        "<table>"
        "<tr valign=\"bottom\">"
        "<td valign=\"middle\"></td>"
        "<td valign=\"top\"></td>"
        "<td></td>"
        "</tr>"
        "</table>"
    };

    const int numTestCases = sizeof(alt) / sizeof(*alt);
    for (int i = 0; i < numTestCases; ++i) {
        doc->setHtml(alt[i]);

        QTextTable *table = qobject_cast<QTextTable *>(doc->rootFrame()->childFrames().at(0));
        QVERIFY(table);

        QCOMPARE(table->cellAt(0, 0).format().verticalAlignment(), QTextCharFormat::AlignMiddle);
        QCOMPARE(table->cellAt(0, 1).format().verticalAlignment(), QTextCharFormat::AlignTop);
        QCOMPARE(table->cellAt(0, 2).format().verticalAlignment(), QTextCharFormat::AlignBottom);
    }
}

void tst_QTextDocumentFragment::html_borderColor()
{
    const char html[] = "<table border=1 style=\"border-color:#0000ff;\"><tr><td>Foo</td></tr></table>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html)));
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(cursor.currentTable());
    QCOMPARE(cursor.currentTable()->format().borderStyle(), QTextFrameFormat::BorderStyle_Outset);
    QCOMPARE(cursor.currentTable()->format().borderBrush(), QBrush(QColor("#0000ff")));
}

void tst_QTextDocumentFragment::html_borderStyle()
{
    const char html[] = "<table border=1 style=\"border-style:solid;\"><tr><td>Foo</td></tr></table>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html)));
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextBlock);
    QVERIFY(cursor.currentTable());
    QCOMPARE(cursor.currentTable()->format().borderStyle(), QTextFrameFormat::BorderStyle_Solid);
    QCOMPARE(cursor.currentTable()->format().borderBrush(), QBrush(Qt::darkGray));
}

void tst_QTextDocumentFragment::html_borderWidth()
{
    const char *html[2] = { "<table style=\"border-width:2;\"><tr><td>Foo</td></tr></table>",
                            "<table style=\"border-width:2px;\"><tr><td>Foo</td></tr></table>" };

    for (int i = 0; i < 2; ++i) {
        cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html[i])));
        cursor.movePosition(QTextCursor::Start);
        cursor.movePosition(QTextCursor::NextBlock);
        QVERIFY(cursor.currentTable());
        QCOMPARE(cursor.currentTable()->format().border(), qreal(2));
    }
}

void tst_QTextDocumentFragment::html_userState()
{
    const char html[] = "<p style=\"-qt-user-state:42;\">A</p><p style=\"-qt-user-state:0;\">B</p><p>C</p>";
    cursor.insertFragment(QTextDocumentFragment::fromHtml(QString::fromLatin1(html)));
    QTextBlock block = doc->begin();
    QCOMPARE(block.userState(), 42);
    QCOMPARE(block.next().userState(), 0);
    QCOMPARE(block.next().next().userState(), -1);
}

void tst_QTextDocumentFragment::html_rootFrameProperties()
{
    const char html[] = "<table border=1 style=\"-qt-table-type:root; margin-top:10px;\"><tr><td>Foo</tr></td>";
    doc->setHtml(html);

    QCOMPARE(doc->rootFrame()->childFrames().size(), 0);

    QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
    QCOMPARE(fmt.topMargin(), qreal(10));
    QCOMPARE(fmt.bottomMargin(), qreal(0));
    QCOMPARE(fmt.leftMargin(), qreal(0));
    QCOMPARE(fmt.rightMargin(), qreal(0));
    QCOMPARE(fmt.border(), qreal(1));

    QString normalFrameHtml = QLatin1String(html);
    normalFrameHtml.replace(QLatin1String("root"), QLatin1String("frame"));

    doc->setHtml(normalFrameHtml);
    QCOMPARE(doc->rootFrame()->childFrames().size(), 1);
}

void tst_QTextDocumentFragment::html_appendList()
{
    appendHtml("<p>foo</p>");
    appendHtml("<ul><li>Line 1</li><li>Line 2</li></ul>");

    QCOMPARE(doc->blockCount(), 3);
    QVERIFY(doc->begin().next().textList() != 0);
}

void tst_QTextDocumentFragment::html_appendList2()
{
    appendHtml("1");
    appendHtml("<ul><li><img src=\"/foo/bar\" /></li></ul>");

    QCOMPARE(doc->blockCount(), 2);
    QVERIFY(doc->begin().next().textList() != 0);
}

void tst_QTextDocumentFragment::html_alignmentPropertySet()
{
    const char html[] = "<p>Test</p>";
    setHtml(QString::fromLatin1(html));
    QVERIFY(!doc->begin().blockFormat().hasProperty(QTextFormat::BlockAlignment));
}

void tst_QTextDocumentFragment::html_qt3RichtextWhitespaceMode()
{
    setHtml(QString::fromLatin1("<html><head><meta name=\"qrichtext\" content=\"1\" /></head><p>   line with whitespace</p><p>    another line with whitespace</p></body></html>"));
    QCOMPARE(doc->blockCount(), 2);

    QTextBlock block = doc->begin();
    QVERIFY(block.text().startsWith("   "));

    block = block.next();
    QVERIFY(block.text().startsWith("   "));
}

void tst_QTextDocumentFragment::html_brAfterHr()
{
    setHtml(QString::fromLatin1("Text A<br><hr><br>Text B<hr>"));

    QCOMPARE(doc->blockCount(), 4);

    QTextBlock block = doc->begin();
    QCOMPARE(block.text(), QString("Text A") + QChar(QChar::LineSeparator));

    block = block.next();
    QVERIFY(block.text().isEmpty());

    block = block.next();
    QCOMPARE(block.text(), QChar(QChar::LineSeparator) + QString("Text B"));

    block = block.next();
    QVERIFY(block.text().isEmpty());
}

void tst_QTextDocumentFragment::html_unclosedHead()
{
    doc->setHtml(QString::fromLatin1("<html><head><title>Test</title><body>Blah</body></html>"));
    QCOMPARE(doc->metaInformation(QTextDocument::DocumentTitle), QString::fromLatin1("Test"));
    QCOMPARE(doc->toPlainText(), QString::fromLatin1("Blah"));
}

// duplicated from qtexthtmlparser.cpp
#define MAX_ENTITY 258
static const struct { const char *name; quint16 code; } entities[MAX_ENTITY]= {
    { "AElig", 0x00c6 },
    { "Aacute", 0x00c1 },
    { "Acirc", 0x00c2 },
    { "Agrave", 0x00c0 },
    { "Alpha", 0x0391 },
    { "AMP", 38 },
    { "Aring", 0x00c5 },
    { "Atilde", 0x00c3 },
    { "Auml", 0x00c4 },
    { "Beta", 0x0392 },
    { "Ccedil", 0x00c7 },
    { "Chi", 0x03a7 },
    { "Dagger", 0x2021 },
    { "Delta", 0x0394 },
    { "ETH", 0x00d0 },
    { "Eacute", 0x00c9 },
    { "Ecirc", 0x00ca },
    { "Egrave", 0x00c8 },
    { "Epsilon", 0x0395 },
    { "Eta", 0x0397 },
    { "Euml", 0x00cb },
    { "Gamma", 0x0393 },
    { "GT", 62 },
    { "Iacute", 0x00cd },
    { "Icirc", 0x00ce },
    { "Igrave", 0x00cc },
    { "Iota", 0x0399 },
    { "Iuml", 0x00cf },
    { "Kappa", 0x039a },
    { "Lambda", 0x039b },
    { "LT", 60 },
    { "Mu", 0x039c },
    { "Ntilde", 0x00d1 },
    { "Nu", 0x039d },
    { "OElig", 0x0152 },
    { "Oacute", 0x00d3 },
    { "Ocirc", 0x00d4 },
    { "Ograve", 0x00d2 },
    { "Omega", 0x03a9 },
    { "Omicron", 0x039f },
    { "Oslash", 0x00d8 },
    { "Otilde", 0x00d5 },
    { "Ouml", 0x00d6 },
    { "Phi", 0x03a6 },
    { "Pi", 0x03a0 },
    { "Prime", 0x2033 },
    { "Psi", 0x03a8 },
    { "QUOT", 34 },
    { "Rho", 0x03a1 },
    { "Scaron", 0x0160 },
    { "Sigma", 0x03a3 },
    { "THORN", 0x00de },
    { "Tau", 0x03a4 },
    { "Theta", 0x0398 },
    { "Uacute", 0x00da },
    { "Ucirc", 0x00db },
    { "Ugrave", 0x00d9 },
    { "Upsilon", 0x03a5 },
    { "Uuml", 0x00dc },
    { "Xi", 0x039e },
    { "Yacute", 0x00dd },
    { "Yuml", 0x0178 },
    { "Zeta", 0x0396 },
    { "aacute", 0x00e1 },
    { "acirc", 0x00e2 },
    { "acute", 0x00b4 },
    { "aelig", 0x00e6 },
    { "agrave", 0x00e0 },
    { "alefsym", 0x2135 },
    { "alpha", 0x03b1 },
    { "amp", 38 },
    { "and", 0x22a5 },
    { "ang", 0x2220 },
    { "apos", 0x0027 },
    { "aring", 0x00e5 },
    { "asymp", 0x2248 },
    { "atilde", 0x00e3 },
    { "auml", 0x00e4 },
    { "bdquo", 0x201e },
    { "beta", 0x03b2 },
    { "brvbar", 0x00a6 },
    { "bull", 0x2022 },
    { "cap", 0x2229 },
    { "ccedil", 0x00e7 },
    { "cedil", 0x00b8 },
    { "cent", 0x00a2 },
    { "chi", 0x03c7 },
    { "circ", 0x02c6 },
    { "clubs", 0x2663 },
    { "cong", 0x2245 },
    { "copy", 0x00a9 },
    { "crarr", 0x21b5 },
    { "cup", 0x222a },
    { "curren", 0x00a4 },
    { "dArr", 0x21d3 },
    { "dagger", 0x2020 },
    { "darr", 0x2193 },
    { "deg", 0x00b0 },
    { "delta", 0x03b4 },
    { "diams", 0x2666 },
    { "divide", 0x00f7 },
    { "eacute", 0x00e9 },
    { "ecirc", 0x00ea },
    { "egrave", 0x00e8 },
    { "empty", 0x2205 },
    { "emsp", 0x2003 },
    { "ensp", 0x2002 },
    { "epsilon", 0x03b5 },
    { "equiv", 0x2261 },
    { "eta", 0x03b7 },
    { "eth", 0x00f0 },
    { "euml", 0x00eb },
    { "euro", 0x20ac },
    { "exist", 0x2203 },
    { "fnof", 0x0192 },
    { "forall", 0x2200 },
    { "frac12", 0x00bd },
    { "frac14", 0x00bc },
    { "frac34", 0x00be },
    { "frasl", 0x2044 },
    { "gamma", 0x03b3 },
    { "ge", 0x2265 },
    { "gt", 62 },
    { "hArr", 0x21d4 },
    { "harr", 0x2194 },
    { "hearts", 0x2665 },
    { "hellip", 0x2026 },
    { "iacute", 0x00ed },
    { "icirc", 0x00ee },
    { "iexcl", 0x00a1 },
    { "igrave", 0x00ec },
    { "image", 0x2111 },
    { "infin", 0x221e },
    { "int", 0x222b },
    { "iota", 0x03b9 },
    { "iquest", 0x00bf },
    { "isin", 0x2208 },
    { "iuml", 0x00ef },
    { "kappa", 0x03ba },
    { "lArr", 0x21d0 },
    { "lambda", 0x03bb },
    { "lang", 0x2329 },
    { "laquo", 0x00ab },
    { "larr", 0x2190 },
    { "lceil", 0x2308 },
    { "ldquo", 0x201c },
    { "le", 0x2264 },
    { "lfloor", 0x230a },
    { "lowast", 0x2217 },
    { "loz", 0x25ca },
    { "lrm", 0x200e },
    { "lsaquo", 0x2039 },
    { "lsquo", 0x2018 },
    { "lt", 60 },
    { "macr", 0x00af },
    { "mdash", 0x2014 },
    { "micro", 0x00b5 },
    { "middot", 0x00b7 },
    { "minus", 0x2212 },
    { "mu", 0x03bc },
    { "nabla", 0x2207 },
    { "nbsp", 0x00a0 },
    { "ndash", 0x2013 },
    { "ne", 0x2260 },
    { "ni", 0x220b },
    { "not", 0x00ac },
    { "notin", 0x2209 },
    { "nsub", 0x2284 },
    { "ntilde", 0x00f1 },
    { "nu", 0x03bd },
    { "oacute", 0x00f3 },
    { "ocirc", 0x00f4 },
    { "oelig", 0x0153 },
    { "ograve", 0x00f2 },
    { "oline", 0x203e },
    { "omega", 0x03c9 },
    { "omicron", 0x03bf },
    { "oplus", 0x2295 },
    { "or", 0x22a6 },
    { "ordf", 0x00aa },
    { "ordm", 0x00ba },
    { "oslash", 0x00f8 },
    { "otilde", 0x00f5 },
    { "otimes", 0x2297 },
    { "ouml", 0x00f6 },
    { "para", 0x00b6 },
    { "part", 0x2202 },
    { "percnt", 0x0025 },
    { "permil", 0x2030 },
    { "perp", 0x22a5 },
    { "phi", 0x03c6 },
    { "pi", 0x03c0 },
    { "piv", 0x03d6 },
    { "plusmn", 0x00b1 },
    { "pound", 0x00a3 },
    { "prime", 0x2032 },
    { "prod", 0x220f },
    { "prop", 0x221d },
    { "psi", 0x03c8 },
    { "quot", 34 },
    { "rArr", 0x21d2 },
    { "radic", 0x221a },
    { "rang", 0x232a },
    { "raquo", 0x00bb },
    { "rarr", 0x2192 },
    { "rceil", 0x2309 },
    { "rdquo", 0x201d },
    { "real", 0x211c },
    { "reg", 0x00ae },
    { "rfloor", 0x230b },
    { "rho", 0x03c1 },
    { "rlm", 0x200f },
    { "rsaquo", 0x203a },
    { "rsquo", 0x2019 },
    { "sbquo", 0x201a },
    { "scaron", 0x0161 },
    { "sdot", 0x22c5 },
    { "sect", 0x00a7 },
    { "shy", 0x00ad },
    { "sigma", 0x03c3 },
    { "sigmaf", 0x03c2 },
    { "sim", 0x223c },
    { "spades", 0x2660 },
    { "sub", 0x2282 },
    { "sube", 0x2286 },
    { "sum", 0x2211 },
    { "sup1", 0x00b9 },
    { "sup2", 0x00b2 },
    { "sup3", 0x00b3 },
    { "sup", 0x2283 },
    { "supe", 0x2287 },
    { "szlig", 0x00df },
    { "tau", 0x03c4 },
    { "there4", 0x2234 },
    { "theta", 0x03b8 },
    { "thetasym", 0x03d1 },
    { "thinsp", 0x2009 },
    { "thorn", 0x00fe },
    { "tilde", 0x02dc },
    { "times", 0x00d7 },
    { "trade", 0x2122 },
    { "uArr", 0x21d1 },
    { "uacute", 0x00fa },
    { "uarr", 0x2191 },
    { "ucirc", 0x00fb },
    { "ugrave", 0x00f9 },
    { "uml", 0x00a8 },
    { "upsih", 0x03d2 },
    { "upsilon", 0x03c5 },
    { "uuml", 0x00fc },
    { "weierp", 0x2118 },
    { "xi", 0x03be },
    { "yacute", 0x00fd },
    { "yen", 0x00a5 },
    { "yuml", 0x00ff },
    { "zeta", 0x03b6 },
    { "zwj", 0x200d },
    { "zwnj", 0x200c }
};

void tst_QTextDocumentFragment::html_entities_data()
{
    QTest::addColumn<QString>("html");
    QTest::addColumn<quint16>("code");

    for (int i = 0; i < MAX_ENTITY; ++i) {
        QTest::newRow(entities[i].name) << QString("<pre>&") + QString::fromLatin1(entities[i].name) + QString(";</pre>")
                                        << entities[i].code;
    }
}

void tst_QTextDocumentFragment::html_entities()
{
    QFETCH(QString, html);
    QFETCH(quint16, code);

    setHtml(html);
    QCOMPARE(doc->blockCount(), 1);
    QString txt = doc->begin().text();
    QCOMPARE(txt.length(), 1);
    QCOMPARE(txt.at(0).unicode(), code);
}

void tst_QTextDocumentFragment::html_ignore_script()
{
    doc->setHtml(QString::fromLatin1("<html><script>Test</script><body>Blah</body></html>"));
    QCOMPARE(doc->toPlainText(), QString("Blah"));
}

void tst_QTextDocumentFragment::html_directionWithHtml()
{
    doc->setHtml(QString::fromLatin1("<html><body><p>Test<p dir=rtl>RTL<p dir=ltr>LTR"));
    QCOMPARE(doc->blockCount(), 3);

    QTextBlock block = doc->firstBlock();
    QVERIFY(block.blockFormat().hasProperty(QTextFormat::LayoutDirection));
    QVERIFY(block.blockFormat().layoutDirection() == Qt::LeftToRight); // HTML default

    block = block.next();
    QVERIFY(block.blockFormat().hasProperty(QTextFormat::LayoutDirection));
    QVERIFY(block.blockFormat().layoutDirection() == Qt::RightToLeft);

    block = block.next();
    QVERIFY(block.blockFormat().hasProperty(QTextFormat::LayoutDirection));
    QVERIFY(block.blockFormat().layoutDirection() == Qt::LeftToRight);
}

void tst_QTextDocumentFragment::html_directionWithRichText()
{
    doc->setHtml(QString::fromLatin1("<p>Test<p dir=rtl>RTL<p dir=ltr>LTR"));
    QCOMPARE(doc->blockCount(), 3);

    QTextBlock block = doc->firstBlock();
    QVERIFY(!block.blockFormat().hasProperty(QTextFormat::LayoutDirection));

    block = block.next();
    QVERIFY(block.blockFormat().hasProperty(QTextFormat::LayoutDirection));
    QVERIFY(block.blockFormat().layoutDirection() == Qt::RightToLeft);

    block = block.next();
    QVERIFY(block.blockFormat().hasProperty(QTextFormat::LayoutDirection));
    QVERIFY(block.blockFormat().layoutDirection() == Qt::LeftToRight);
}

void tst_QTextDocumentFragment::html_metaInBody()
{
    setHtml("<body>Hello<meta>World</body>");
    QCOMPARE(doc->toPlainText(), QString("HelloWorld"));
}

void tst_QTextDocumentFragment::html_importImageWithoutAspectRatio()
{
    doc->setHtml("<img src=\"foo\" width=\"100%\" height=\"43\">");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().isImageFormat());
    QTextImageFormat fmt = cursor.charFormat().toImageFormat();
    // qDebug() << fmt.width() << fmt.height();
    QVERIFY (fmt.hasProperty(QTextFormat::ImageWidth));
    QCOMPARE (fmt.height(), 43.);

    doc->setHtml("<img src=\"foo\" height=\"43\">");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().isImageFormat());
    fmt = cursor.charFormat().toImageFormat();
    QVERIFY (! fmt.hasProperty(QTextFormat::ImageWidth));
    QCOMPARE (fmt.height(), 43.);

    doc->setHtml("<img src=\"foo\" width=\"200\">");
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::NextCharacter);
    QVERIFY(cursor.charFormat().isImageFormat());
    fmt = cursor.charFormat().toImageFormat();
    QVERIFY (! fmt.hasProperty(QTextFormat::ImageHeight));
    QCOMPARE (fmt.width(), 200.);
}

void tst_QTextDocumentFragment::html_fromFirefox()
{
    // if you have a html loaded in firefox like <html>Test\nText</html>  then selecting all and copying will
    // result in the following text on the clipboard (for text/html)
    doc->setHtml(QString::fromLatin1("<!--StartFragment-->Test\nText\n\n<!--EndFragment-->"));
    QCOMPARE(doc->toPlainText(), QString::fromLatin1("Test Text "));
}

QTEST_MAIN(tst_QTextDocumentFragment)
#include "tst_qtextdocumentfragment.moc"