File: iphtmlparser.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (3091 lines) | stat: -rw-r--r-- 92,317 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
unit IpHtmlParser;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Graphics,
  ipConst, ipUtils, ipHtmlTypes, ipHtmlUtils, ipCSS, ipHtmlClasses, ipHtml;

type  
  TIpHtmlParser = class(TIpHtmlBasicParser)
  private
    FCharSP: Integer;
    FCharStack: array [0..7] of AnsiChar;
    FCharStream: TStream;
    FCurFrameSet: TIpHtmlNodeFRAMESET;
    FCurToken: TIpHtmlToken;
    FCurURL: string;
    FDocCharSet: String;
    FGlobalPos: Integer;
    FHasBOM: Boolean;
    FHasFrames: Boolean;
    FHaveToken: Boolean;
    FInBlock: Integer;
    FIndexPhrase: string;
    FInPre: Integer;
    FLastWasClose: Boolean;
    FLastWasSpace: Boolean;
    FLineNumber: Integer;
    FLineOffset: Integer;
    FListLevel: Integer;
    FOwner: TIpHtml;
    FParmBuf: PChar;
    FParmBufSize: Integer;
    FParmValueArray: array[TIpHtmlAttributesSet] of string;
    FTitleNode : TIpHtmlNodeTITLE;
    FTokenBuffer: TIpHtmlToken;
    FTokenStringBuf: PChar; {array[16383] of AnsiChar;}
    TBW: Integer;
    
    function GetFlagErrors: Boolean;
    
    procedure ClearParmValueArray;
    procedure ParmValueArrayAdd(const sName, sValue: string);
    
    procedure ReportError(const AErrorMsg: string);
    procedure ReportExpectedError(const AErrorMsg: string);
    procedure ReportExpectedToken(const AToken: TIpHtmlToken);
    
  protected
    function ColorFromString(S: string): TColor;
    procedure EnsureClosure(const EndToken: TIpHtmlToken; const EndTokens: TIpHtmlTokenSet);
    function GetChar: AnsiChar;
    function GetTokenString: string;
    function HtmlTokenListIndexOf(const TokenString: PAnsiChar): integer;
    function IsWhiteSpace: Boolean;
    function NextChar: AnsiChar;
    procedure NextNonBlankToken;
    procedure NextRealToken;
    procedure NextToken;
    procedure PutChar(Ch: AnsiChar);    
    procedure PutToken(AToken: TIpHtmlToken);
    procedure SkipTextTokens;
    
  protected
    // parser helper routines
    function ParseAlignment: TIpHtmlAlign;
    function ParseBoolean(const AttrNameSet: TIpHtmlAttributesSet): Boolean;
    function ParseBRClear: TIpHtmlBreakClear;
    function ParseButtonType: TIpHtmlButtonType;
    function ParseCellAlign(ADefault: TIpHtmlAlign): TIpHtmlAlign;
    procedure ParseCenter(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    function ParseDir: TIpHtmlDirection;
    function ParseFrameProp(ADefault: TIpHtmlFrameProp): TIpHtmlFrameProp;
    function ParseFrameScrollingProp: TIpHtmlFrameScrolling;
    function ParseHtmlInteger2(const AttrNameSet: TIpHtmlAttributesSet; 
      ADefault: Integer): TIpHtmlInteger;
    function ParseHyperLength(const AttrNameSet: TIpHtmlAttributesSet;
      const ADefault: string): TIpHtmlLength;
    function ParseHyperMultiLength(const AttrNameSet: TIpHtmlAttributesSet;
      const ADefault: string): TIpHtmlMultiLength;
    function ParseHyperMultiLengthList(const AttrNameSet: TIpHtmlAttributesSet;
      const ADefault: string): TIpHtmlMultiLengthList;
    procedure ParseIFrame(AParent: TIpHtmlNode);
    function ParseImageAlignment(ADefault: TIpHtmlImageAlign): TIpHtmlImageAlign;
    procedure ParseImg(AParent: TIpHtmlNode);
    function ParseInputType: TIpHtmlInputType;
    function ParseInteger(const AttrNameSet: TIpHtmlAttributesSet; 
      ADefault: Integer): Integer;
    function ParseMethod: TIpHtmlFormMethod;
    function ParseObjectValueType: TIpHtmlObjectValueType;
    function ParseOLStyle(ADefault: TIpHtmlOLStyle): TIpHtmlOLStyle;
    function ParsePixels(const AttrNameSet: TIpHtmlAttributesSet;
      const ADefault: string): TIpHtmlPixels;
    function ParseRelSize: TIpHtmlRelSize;
    function ParseRules(ADefault: TIpHtmlRules): TIpHtmlRules;
    function ParseShape: TIpHtmlMapShape;
    function ParseULStyle(ADefault: TIpHtmlULType): TIpHtmlULType;
    function ParseVAlignment: TIpHtmlVAlign;
    function ParseVAlignment2: TIpHtmlVAlignment2;
    function ParseVAlignment3: TIpHtmlVAlign3;
    
  protected
    // Methods for parsing html nodes
    procedure ParseAddress(AParent: TIpHtmlNode);
    procedure ParseAnchor(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseApplet(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseBase;
    procedure ParseBaseFont(AParent: TIpHtmlNode);
    procedure ParseBlink(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseBlock(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseBlockQuote(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseBody(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseBodyText(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseBR(AParent: TIpHtmlNode);
    procedure ParseColGroup(AParent: TIpHtmlNode);
    procedure ParseDefinitionList(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseDefinitionListItems(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseDel(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseDIV(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseFont(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseFontStyle(AParent: TIpHtmlNode; StartToken: TIpHtmlToken; 
      const EndTokens: TIpHtmlTokenSet);
    procedure ParseForm(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseFormFields(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseFrame(AParent: TIpHtmlNode);
    procedure ParseFrameSet(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseHead(AParent: TIpHtmlNode);
    procedure ParseHeader(AParent: TIpHtmlNode; EndToken: TIpHtmlToken; ASize: Integer);
    procedure ParseHeadItems(AParent: TIpHtmlNode);
    procedure ParseHR(AParent: TIpHtmlNode);
    procedure ParseHtml;
    procedure ParseInline(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseIns(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseIsIndex;
    procedure ParseLeft(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseLink(AParent: TIpHtmlNode);
    procedure ParseListItems(AParent: TIpHtmlNodeCore; EndToken: TIpHtmlToken; 
      const EndTokens: TIpHtmlTokenSet);
    procedure ParseMap(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseMeta(AParent: TIpHtmlNode);
    procedure ParseNOBR(AParent: TIpHtmlNode);
    procedure ParseNoFrames(AParent: TIpHtmlNode);
    procedure ParseNoScript(AParent: TIpHtmlNode);
    procedure ParseObject(AParent: TIpHtmlNode);
    procedure ParseOrderedList(AParent: TIpHtmlNode; EndToken: TIpHtmlToken; 
      const EndTokens: TIpHtmlTokenSet);
    procedure ParseParagraph(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParsePhraseElement(AParent: TIpHtmlNode; StartToken, EndToken: TIpHtmlToken; 
      const EndTokens: TIpHtmlTokenSet);
    procedure ParsePre(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseQ(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseRight(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseScript(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseSpan(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseStyle(AParent: TIpHtmlNode);
    procedure ParseStyleSheet(AParent: TIpHtmlNode; HRef: String);
    procedure ParseTable(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseTableBody(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseTableRow(AParent: TIpHtmlNode; const EndTokens : TIpHtmlTokenSet);
    procedure ParseTableRows(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseText(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
    procedure ParseTitle(AParent: TIpHtmlNode);
    procedure ParseUnorderedList(AParent: TIpHtmlNode; EndToken: TIpHtmlToken; const EndTokens: TIpHtmlTokenSet);

    property FlagErrors: Boolean read GetFlagErrors;
    
  public
    constructor Create(AOwner: TIpHtml; AStream: TStream);
    function Execute: Boolean; override;
    function FindAttribute(const AttrNameSet: TIpHtmlAttributesSet): string; override;
       
    property DocCharset: String read FDocCharset;
    property FrameSet: TIpHtmlNodeFRAMESET read FCurFrameSet;
    property HasFrames: Boolean read FHasFrames;
    property TitleNode: TIpHtmlNodeTITLE read FTitleNode;
  end;
  
implementation

uses
  LConvEncoding, LazUTF8, LazStringUtils, Translations,
  IpHtmlNodes;

{ TIpHtmlParser }

constructor TIpHtmlParser.Create(AOwner: TIpHtml; AStream: TStream);
begin
  inherited Create;
  FCharStream := AStream;
  FOwner := AOwner;
end;

procedure TIpHtmlParser.ClearParmValueArray;
var
  n: TIpHtmlAttributesSet;
begin
  for n := Low(FParmValueArray) to High(FParmValueArray) do
    FParmValueArray[n] := '';
//    SetLength(FParmValueArray[n], 0);
end;

function TIpHtmlParser.ColorFromString(S: String): TColor;
var
  err: String;
begin
  if TryColorFromString(S, Result, err) then
    //
  else
  begin
    ReportError(err);
    Result := clNone;
  end;
end;

procedure TIpHtmlParser.EnsureClosure(const EndToken: TIpHtmlToken;
  const EndTokens: TIpHtmlTokenSet);
begin
  if FCurToken = EndToken then
    NextToken
  else
  if FCurToken in EndTokens then
  else
    if FlagErrors then
      ReportExpectedToken(EndToken);
end;

function TIpHtmlParser.Execute: Boolean;
var
  ch1, ch2, ch3: AnsiChar;
  startPos: Int64;
begin
  Result := false;

  Getmem(FTokenStringBuf, FCharStream.Size * 4 + 65536);
  try
    FGlobalPos := 0;
    FLineNumber := 1;
    FLineOffset := 0;
    FCharSP := 0;
    FListLevel := 0;
    FDocCharset := '';
    FHasBOM := false;
    FHasFrames := false;
    startPos := FCharStream.Position;
    ch1 := GetChar;
    ch2 := GetChar;
    if (ch1 = #$FE) and (ch2 = #$FF) then begin
      FDocCharset := 'UCS-2BE';
      raise Exception.CreateFmt('%s document encoding not supported!', [FDocCharset]);
    end else
    if (ch1 = #$FF) and (ch2 = #$FE) then begin
      FDocCharset := 'UCS-2LE';
      raise Exception.CreateFmt('%s document encoding not supported!', [FDocCharset]);
    end else
    if (ch1 = #$EF) and (ch2=#$BB) then begin
      ch3 := GetChar;
      if ch3 = #$BF then begin
        FDocCharset := 'UTF-8';
        FHasBOM := true;
      end else begin
        PutChar(ch3);
        PutChar(ch2);
        PutChar(ch1);
      end;
    end else begin
      PutChar(ch2);
      PutChar(ch1);
    end;

    repeat
      NextToken;
    until FCurToken in [IpHtmlTagHtml, IpHtmlTagFRAMESET, IpHtmlTagEOF];
    
    if FCurToken = IpHtmlTagEOF then begin
      FCharStream.Position := startPos;
      FCharSP := 0;
      FListLevel := 0;
      repeat
        NextToken;
      until FCurToken <> IpHtmlTagText;
    end;
    if FCurToken = IpHtmlTagEOF then Exit;
    ParseHtml;
    Result := true;
  finally
    FreeMem(FTokenStringBuf);
    FTokenStringBuf := nil;
    if FParmBuf <> nil then begin
      FreeMem(FParmBuf);
      FParmBuf := nil;
      FParmBufSize := 0;
    end;
  end;
end;

function TIpHtmlParser.FindAttribute(const AttrNameSet: TIpHtmlAttributesSet): string;
begin
  Result := FParmValueArray[AttrNameSet];
end;

function TIpHtmlParser.GetChar: AnsiChar;
var
  trimming, done: Boolean;
begin
  trimming := False;
  repeat
    done := True;
    if (FCharSP > 0) then begin
      Dec(FCharSP);
      Result := FCharStack[FCharSP];
    end else begin
      Result := NextChar;
    end;
    if (FInPre = 0) and (FCurToken <> IpHtmlTagPRE) then begin
      if (Result <= ' ') and (Result > #0) then begin
        if (Result < ' ') and FLastWasClose then begin
          done := False;
          trimming := True;
        end else
          if trimming then
            done := False
          else
            if FLastWasSpace then
              Done := False
            else begin
              Result := ' ';
              FLastWasSpace := True;
            end;
      end else
        FLastWasSpace := False;
    end;
  until done;
  FLastWasClose := (Result = '>');
end;

function TIpHtmlParser.GetFlagErrors: Boolean;
begin
  Result := FOwner.FlagErrors;
end;

function TIpHtmlParser.GetTokenString: string;
begin
  FTokenStringBuf[TBW] := #0;
  Result := StrPas(FTokenStringBuf);
end;

function TIpHtmlParser.HtmlTokenListIndexOf(const TokenString: PAnsiChar): integer;
var
  vFirst: Integer;
  vLast: Integer;
  vPivot: Integer;
  vicmp: integer;
begin
  vFirst  := Low(IpHtmlTokens); //Sets the first item of the range
  vLast   := High(IpHtmlTokens); //Sets the last item of the range
  Result  := -1; //Initializes the Found flag (Not found yet)

  //If First > Last then the searched item doesn't exist
  //If the item is found the loop will stop
  while (vFirst <= vLast) do
  begin
    //Gets the middle of the selected range
    vPivot := (vFirst + vLast) div 2;
    //Compares the String in the middle with the searched one
    vicmp := strcomp(IpHtmlTokens[vPivot].pc, TokenString);
    if vicmp = 0 then
    begin
      Result  := vPivot;
      exit;
    end
    //If the Item in the middle has a bigger value than
    //the searched item, then select the first half
    else if vicmp > 0 then
      vLast := vPivot - 1    //else select the second half
    else
      vFirst := vPivot + 1;
  end;
end;

function TIpHtmlParser.IsWhiteSpace: Boolean;
var
  i : Integer;
begin
  Result := False;
  for i := 0 to TBW - 1 do
    if FTokenStringBuf[i] > ' ' then
      Exit;
  Result := True;
end;

function TIpHtmlParser.NextChar: AnsiChar;
begin
  Result := #0;
  if FCharStream.Read(Result, 1) = 0 then
    Result := #0
  else begin
    Inc(FGlobalPos);
    if Result = #10 then begin
      Inc(FLineNumber);
      FLineOffset := 0;
    end else
      Inc(FLineOffset);
  end;
end;

procedure TIpHtmlParser.NextNonBlankToken;
begin
  repeat
    NextToken;
  until (FCurToken <> IpHtmlTagText) or not IsWhiteSpace;
end;

procedure TIpHtmlParser.NextRealToken;
begin
  repeat
    NextToken;
  until FCurToken <> IpHtmlTagText;
end;

procedure TIpHtmlParser.NextToken;
var
  parmName: string;
  PBW: Integer;
  i: Integer;
  inValue, inQuote, inAttr: Boolean;
  seenEqual, seenQuotes: Boolean;
  ctl, done, endFound: Boolean;
  quoteChar: AnsiChar;
  ch: AnsiChar;

  procedure AddParmChar(const Ch: AnsiChar);
  begin
    if PBW >= FParmBufSize - 1 then begin
      Inc(FParmBufSize, 4096);
      ReallocMem(FParmBuf, FParmBufSize);
    end;
    FParmBuf[PBW] := Ch;
    Inc(PBW);
  end;

  function ParmString: string;
  begin
    if PBW = 0 then
      Result := ''
    else begin
      FParmBuf[PBW] := #0;
      Result := StrPas(FParmBuf);
      PBW := 0;
    end;
  end;

  procedure AddTokenChar(const Ch: AnsiChar);
  begin
    FTokenStringBuf[TBW] := Ch;
    Inc(TBW);
  end;

begin
  if FHaveToken then begin
    FCurToken := FTokenBuffer;
    FHaveToken := False;
    Exit;
  end;
  quoteChar := ' ';
  repeat
    TBW := 0;
    PBW := 0;
    ClearParmValueArray;
    ch := GetChar;
    if ch = #0 then begin
      FCurToken := IpHtmlTagEof;
      Exit;
    end;
    if ch = '<' then begin
      ch := GetChar;
      if ch = '!' then begin
        if GetChar = '-' then begin
          if GetChar <> '-' then
            if FlagErrors then
              ReportError(SHtmlDashExp);
          ch := GetChar;
          repeat
            while ch <> '-' do begin
              if ch = #0 then
                break;
              ch := GetChar;
            end;
            if (ch = #0) then
              break
            else begin
              ch := GetChar;
              if ch = #0 then
                break;
              if ch = '-' then begin
                ch := GetChar;
                while (ch = '-') do
                  ch := GetChar;
                while not (ch in [#0, '>']) do
                  ch := GetChar;
                break;
              end;
            end;
          until false;
          FCurToken := IpHtmlTagComment;
        end else begin
          ch := GetChar;
          while ch <> '>' do
            ch := GetChar;
          FCurToken := IpHtmlTagComment;
        end;
      end else begin
        while ch <> '>' do begin
          if ch <= ' ' then begin
            ch := ' ';
            break;
          end;
          if ch in [#33..#255] then
            AddTokenChar(UpCase(ch));
          ch := GetChar;
        end;
        if ch = ' ' then begin
          ch := GetChar;
          {list :== [attr]* ">"}
          {attr :== [" "]* attr-name [attr-value]}
          {attr-value :== [" "]* "=" [" "]* value}
          {value :== ['"']* string ['"']*}
          inAttr := False;
          inValue := False;
          inQuote := False;
          seenEqual := False;
          seenQuotes := False;
          parmName := '';
          PBW := 0;
          while True do begin
            case ch of
              #0 : break;
              #1..#31 :
                if inAttr then begin
                  inAttr := False;
                  parmName := ParmString;
                  seenEqual := False;
                end else
                if inValue then begin
                  if parmName <> '' then begin
                    ParmValueArrayAdd(UpperCase(parmName), ParmString);
                    parmName := '';
                  end;
                  inValue := False;
                  seenEqual := False;
                  seenQuotes := False;
                end;
              ' ', '/' :
                if inQuote then
                  AddParmChar(ch)
                else
                if inAttr then begin
                  inAttr := False;
                  parmName := ParmString;
                  seenEqual := False;
                end else
                if inValue then begin
                  if parmName <> '' then begin
                    ParmValueArrayAdd(UpperCase(parmName), ParmString);
                    parmName := '';
                  end;
                  inValue := False;
                  seenEqual := False;
                  seenQuotes := False;
                end;
              '''' :
                if inQuote then
                begin 
                  if quoteChar = '''' then
                    inQuote := False
                  else
                    AddParmChar('''');
                end else 
                begin
                  inQuote := True;
                  seenQuotes := True;
                  quoteChar := '''';
                end;
              '"' :
                if inQuote then
                begin
                  if quoteChar = '"' then
                    inQuote := False
                  else
                    AddParmChar('"')
                end else
                begin
                  inQuote := True;
                  seenQuotes := True;
                  quoteChar := '"';
                end;
              '<', '>' :
                begin
                  if inQuote then
                    AddParmChar(ch)
                  else begin
                    if inValue then begin
                      if parmName <> '' then begin
                        ParmValueArrayAdd(UpperCase(parmName), ParmString);
                        parmName := '';
                      end;
                    end;
                    break;
                  end;
                end;
              '=' :
                begin
                  seenEqual := True;
                  if inAttr then begin
                    parmName := ParmString;
                    inAttr := False;
                  end else
                    if inValue then
                      AddParmChar(ch)
                end;
              else
                if inAttr or inValue then
                  AddParmChar(ch)
                else
                  if seenEqual and (inQuote or not seenQuotes) then begin
                    inValue := True;
                    AddParmChar(ch);
                  end else begin
                    if (parmName <> '') and not seenQuotes then begin
                      parmName := UpperCase(parmName);
                      ParmValueArrayAdd(parmName, parmName);
                    end;
                    parmName := '';
                    AddParmChar(ch);
                    seenEqual := False;
                    seenQuotes := False;
                    inValue := False;
                    inAttr := True;
                  end;
            end;
            ch := GetChar;
          end;
          
          if inAttr then begin
            parmName := UpperCase(ParmString);
            if (parmName <> '') then begin
              ParmValueArrayAdd(parmName, parmName);
            end;
          end;
        end;

        { Check if this is a token of the form <tok/> }
        if (TBW > 0) and (FTokenStringBuf[TBW - 1] = '/') then begin
          {It is, set EndFound flag and convert to normal open token}
          endFound := True;
          Dec(TBW);
        end else
          endFound := False;
        FTokenStringBuf[TBW] := #0;
        FCurToken := IpHtmlTagUnknown;
        i := HtmlTokenListIndexOf(FTokenStringBuf);
        if i <> -1 then
          FCurToken := IpHtmlTokens[i].tk;

        {If the token was a single terminated token ( <tok/>
         as opposed to normal a <tok></tok> sequence), we fake
         it by pushing a close token to match the open token
         which was mangled above where EndFound was set.}

        if (FCurToken <> IpHtmlTagUnknown) and endFound then
          if succ(FCurToken) in IpEndTokenSet then
            PutToken(succ(FCurToken));
      end;
    end else begin
      FCurToken := IpHtmlTagText;
      repeat
        done := True;
        ctl := False;
        while ch <> '<' do begin
          case ch of
            #0 :
              break;
            #10, #13 :
              begin
                ctl := True;
                if FInPre > 0 then
                  AddTokenChar(ch);
              end
            else
              AddTokenChar(ch);
          end;
          ch := GetChar;
        end;
        
        if ch <> #0 then begin
          ch := GetChar;
          while (ch > #0) and (ch < ' ') do
            ch := GetChar;
          case ch of
            '/', '!', 'a'..'z','A'..'Z' :
              begin
                PutChar(ch);
                PutChar('<');
              end
            else
              begin
                AddTokenChar('<');
                AddTokenChar(Ch);
                done := False;
                ch := GetChar;
              end;
          end;
        end;
        
        if (FInPre = 0) and ctl and IsWhiteSpace then
          FCurToken := IpHtmlTagCOMMENT;
      until Done;
    end;
    
    // Eat script blocks that could confuse the parsing
    // example www.sqlite.org has javascript to write dynamic
    // content inside a table
    if FCurToken = IpHtmlTagSCRIPT then 
      ParseScript(FOwner.HtmlNode, []);
  until
    (FCurToken <> IpHtmlTagCOMMENT) and 
    ((FCurToken <> IpHtmlTagText) or (FInBlock > 0) or (FInPre > 0) or not IsWhiteSpace);
end;

procedure TIpHtmlParser.ParmValueArrayAdd(const sName, sValue: string);
var
  vFirst, vLast, vPivot: Integer;
begin
  vFirst := Ord(Low(TIpHtmlAttributesSet)); //Sets the first item of the range
  vLast  := Ord(High(TIpHtmlAttributesSet)); //Sets the last item of the range

  //If First > Last then the searched item doesn't exist
  //If the item is found the loop will stop
  while (vFirst <= vLast) do
  begin
    //Gets the middle of the selected range
    vPivot := (vFirst + vLast) div 2;
    //Compares the String in the middle with the searched one
    if TIpHtmlAttributesNames[TIpHtmlAttributesSet(vPivot)] = sName then
    begin
      FParmValueArray[TIpHtmlAttributesSet(vPivot)] := sValue;
      Exit;
    end
    //If the Item in the middle has a bigger value than
    //the searched item, then select the first half
    else if TIpHtmlAttributesNames[TIpHtmlAttributesSet(vPivot)] > sName then
      vLast := Pred(vPivot)//else select the second half
    else
      vFirst := Succ(vPivot);
  end;
end;
 
procedure TIpHtmlParser.ParseAddress(AParent: TIpHtmlNode);
var
  newPara: TIpHtmlNodeADDRESS;
begin
  newPara := TIpHtmlNodeADDRESS.Create(AParent);
  newPara.ParseBaseProps(FOwner);
  NextToken;
  ParseBodyText(newPara, [IpHtmlTagADDRESSend]);
  if FCurToken = IpHtmlTagADDRESSend then
    NextToken
  else
    if FlagErrors then
      ReportExpectedToken(IpHtmlTagADDRESSend);
end;

function TIpHtmlParser.ParseAlignment: TIpHtmlAlign;
begin
  Result := GetAlignmentForStr(FindAttribute(htmlAttrALIGN), haDefault); 
end;

procedure TIpHtmlParser.ParseAnchor(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curAnchor: TIpHtmlNodeA;
begin
  curAnchor := TIpHtmlNodeA.Create(AParent);
  FOwner.TabList.Add(curAnchor);
  with curAnchor do begin
    Name := FindAttribute(htmlAttrNAME);
    HRef := FindAttribute(htmlAttrHREF);
    Rel := FindAttribute(htmlAttrREL);
    Rev := FindAttribute(htmlAttrREV);
    Title := FindAttribute(htmlAttrTITLE);
    ParseBaseProps(FOwner);
    Shape := ParseShape;
    TabIndex := ParseInteger(htmlAttrTABINDEX, -1);
    Target := FindAttribute(htmlAttrTARGET);
  end;
  NextToken;
  ParseBodyText(curAnchor, EndTokens + [IpHtmlTagAend] - [IpHtmlTagA]);
  if FCurToken = IpHtmlTagAend then
    NextToken
  else
  if FCurToken = IpHtmlTagA then
  else
  if FCurToken in EndTokens then
  else
    if FlagErrors then
      ReportExpectedToken(IpHtmlTagAend);
  if (curAnchor.ChildCount = 0) and (CurAnchor.Name <> '') then
    TIpHtmlNodeText.Create(curAnchor).EscapedText := '&xxxxxx;';  //wp: ???
end;

procedure TIpHtmlParser.ParseApplet(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curApplet: TIpHtmlNodeAPPLET;
  curParam: TIpHtmlNodePARAM;
begin
  curApplet := TIpHtmlNodeAPPLET.Create(AParent);
  with curApplet do begin
    Codebase := FindAttribute(htmlAttrCODEBASE);
    Code := FindAttribute(htmlAttrCODE);
    Alt := FindAttribute(htmlAttrALT);
    Name := FindAttribute(htmlAttrNAME);
    Height := ParseInteger(htmlAttrHEIGHT, -1);
    Width := ParseHyperLength(htmlAttrWIDTH, '');
    Width.OnChange := @WidthChanged;
    Align := ParseImageAlignment(hiaBottom);
    HSpace := ParseInteger(htmlAttrHSPACE, 1);
    VSpace := ParseInteger(htmlAttrVSPACE, 1);
    Archive := FindAttribute(htmlAttrARCHIVE);
    ObjectCode := FindAttribute(htmlAttrOBJECT);
    Id := FindAttribute(htmlAttrID);
    ClassID := FindAttribute(htmlAttrCLASS);
    Title := FindAttribute(htmlAttrTITLE);
    Style := FindAttribute(htmlAttrSTYLE);
  end;
  NextToken;
  while not (FCurToken in EndTokens + [IpHtmlTagAPPLETend]) do begin
    case FCurToken of
      IpHtmlTagPARAM:
        begin
          curParam := TIpHtmlNodePARAM.Create(curApplet);
          with curParam do begin
            Name := FindAttribute(htmlAttrNAME);
            Value := FindAttribute(htmlAttrVALUE);
            Id := FindAttribute(htmlAttrID);
            ValueType := ParseObjectValueType;
          end;
          NextToken;
        end;
      else
        ParseText(curApplet, [IpHtmlTagAPPLETend, IpHtmlTagPARAM]);
    end;
  end;
  EnsureClosure(IpHtmlTagAPPLETend, EndTokens);
end;

procedure TIpHtmlParser.ParseBase;
begin
  NextToken;
end;

procedure TIpHtmlParser.ParseBaseFont(AParent: TIpHtmlNode);
var
  curBasefont: TIpHtmlNodeBASEFONT;
begin
  curBasefont := TIpHtmlNodeBASEFONT.Create(AParent);
//  if CurBasefont=nil then ;                                  // ???? What's this?????
  curBasefont.Size := ParseInteger(htmlAttrSIZE, 3);
  NextToken;
end;

procedure TIpHtmlParser.ParseBlink(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curBlink: TIpHtmlNodeBLINK;
begin
  curBlink := TIpHtmlNodeBLINK.Create(AParent);
  NextToken;
  ParseBodyText(curBlink, EndTokens + [IpHtmlTagBLINKend]);
  EnsureClosure(IpHtmlTagBLINKend, EndTokens);
end;

procedure TIpHtmlParser.ParseBlock(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
begin
  case FCurToken of
    IpHtmlTagH1: 
      ParseHeader(AParent, IpHtmlTagH1end, 1);
    IpHtmlTagH2: 
      ParseHeader(AParent, IpHtmlTagH2end, 2);
    IpHtmlTagH3: 
      ParseHeader(AParent, IpHtmlTagH3end, 3);
    IpHtmlTagH4: 
      ParseHeader(AParent, IpHtmlTagH4end, 4);
    IpHtmlTagH5: 
      ParseHeader(AParent, IpHtmlTagH5end, 5);
    IpHtmlTagH6: 
      ParseHeader(AParent, IpHtmlTagH6end, 6);
    {IpHtmlTagP: 
      ParseParagraph(AParent, EndTokens);} {moved to inline}
    IpHtmlTagDIR: 
      ParseUnorderedList(AParent, IpHtmlTagDIRend, EndTokens);
    IpHtmlTagMENU: 
      ParseUnorderedList(AParent, IpHtmlTagMENUend, EndTokens);
    IpHtmlTagUL: 
      ParseUnorderedList(AParent, IpHtmlTagULend, EndTokens);
    IpHtmlTagDL: 
      ParseDefinitionList(AParent, EndTokens);
    IpHtmlTagOL: 
      ParseOrderedList(AParent, IpHtmlTagOLend, EndTokens);
    IpHtmlTagPRE: 
      ParsePre(AParent, EndTokens);
    IpHtmlTagBLOCKQUOTE: 
      ParseBlockQuote(AParent, EndTokens);
    IpHtmlTagFORM: 
      ParseForm(AParent, EndTokens);
    IpHtmlTagTABLE: 
      ParseTable(AParent, EndTokens);
    IpHtmlTagIMG: 
      ParseIMG(AParent);
    IpHtmlTagOBJECT: 
      ParseObject(AParent);
    IpHtmlTagAPPLET: 
      ParseApplet(AParent, EndTokens);
    IpHtmlTagADDRESS: 
      ParseAddress(AParent);
    IpHtmlTagEof: 
      Exit;
    IpHtmlTagFRAMESET: 
      ParseFrameSet(AParent, EndTokens + [IpHtmlTagFRAMESETend]);
    IpHtmlTagUnknown:
      if FlagErrors then
        ReportError(SHtmlUnknownTok)
      else
        NextToken;
  end;
end;

procedure TIpHtmlParser.ParseBlockQuote(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  BQ: TIpHtmlNodeBLOCKQUOTE;
begin
  BQ := TIpHtmlNodeBLOCKQUOTE.Create(AParent);
  BQ.ParseBaseProps(FOwner);
  NextToken;
  ParseBodyText(BQ, EndTokens + [IpHtmlTagBLOCKQUOTEend]);
  EnsureClosure(IpHtmlTagBLOCKQUOTEend, EndTokens);
end;

procedure TIpHtmlParser.ParseBody(AParent: TIpHtmlNode;
  const EndTokens: TIpHtmlTokenSet);
begin
  if FCurToken = IpHtmlTagFRAMESET then begin
    ParseFrameSet(AParent, EndTokens);
    Exit;
  end;
  
  {lead token is optional}
  if FCurToken = IpHtmlTagBODY then begin
    TIpHtmlNodeBODY.Create(AParent);
    with FOwner.Body do begin
      BgColor := ColorFromString(FindAttribute(htmlAttrBGCOLOR));
      TextColor := ColorFromString(FindAttribute(htmlAttrTEXT));
      Link := ColorFromString(FindAttribute(htmlAttrLINK));
      VLink := ColorFromString(FindAttribute(htmlAttrVLINK));
      ALink := ColorFromString(FindAttribute(htmlAttrALINK));
      Background := FindAttribute(htmlAttrBACKGROUND);
      ParseBaseProps(FOwner);
      LoadAndApplyCSSProps;
    end;
    NextToken;
    ParseBodyText(FOwner.Body, EndTokens + [IpHtmlTagBODYend]);
    EnsureClosure(IpHtmlTagBODYend, EndTokens);
  end else begin
    ParseBodyText(AParent, EndTokens + [IpHtmlTagBODYend]);
    FOwner.FixMissingBodyTag;
    if FCurToken = IpHtmlTagBODYend then
      NextToken;
  end;
end;

procedure TIpHtmlParser.ParseBodyText(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
begin
  Inc(FInBlock);
  try
    while not (FCurToken in EndTokens) do begin
      case FCurToken of
        IpHtmlTagH1,
        IpHtmlTagH2,
        IpHtmlTagH3,
        IpHtmlTagH4,
        IpHtmlTagH5,
        IpHtmlTagH6,
        {IpHtmlTagP,}
        IpHtmlTagDIR,
        IpHtmlTagMENU,
        IpHtmlTagUL,
        IpHtmlTagDL,
        IpHtmlTagOL,
        IpHtmlTagPRE,
        IpHtmlTagBLOCKQUOTE,
        IpHtmlTagFORM,
        IpHtmlTagTABLE,
        IpHtmlTagIMG,
        IpHtmlTagOBJECT,
        IpHtmlTagAPPLET,
        IpHtmlTagADDRESS,
        IpHtmlTagFRAMESET :
          ParseBlock(AParent, EndTokens);
        IpHtmlTagBODY :
          begin
            if FOwner.Body = nil then begin
              TIpHtmlNodeBODY.Create(AParent);
              NextToken;
              ParseBodyText(FOwner.Body, EndTokens);
            end
            else
              ParseInline(AParent, EndTokens);
          end;
        IpHtmlTagEof :
          Exit;
        else
          ParseInline(AParent, EndTokens);
      end;
    end;
  finally
    Dec(FInBlock);
  end;
end;

function TIpHtmlParser.ParseBoolean(const AttrNameSet: TIpHtmlAttributesSet): Boolean;
begin
  Result := Length(FParmValueArray[AttrNameSet]) > 0;
end;

procedure TIpHtmlParser.ParseBR(AParent: TIpHtmlNode);
var
  br: TIpHtmlNodeBR;
begin
  br := TIpHtmlNodeBR.Create(AParent);
  br.Clear := ParseBRClear;
  br.Id := FindAttribute(htmlAttrID);
  br.ClassId :=FindAttribute(htmlAttrCLASS);
  br.Title := FindAttribute(htmlAttrTITLE);
  br.Style := FindAttribute(htmlAttrSTYLE);
  NextToken;
end;

function TIpHtmlParser.ParseBRClear: TIpHtmlBreakClear;
var
  S : string;
begin
  Result := hbcNone;
  S := UpperCase(FindAttribute(htmlAttrCLEAR));
  if Length(S) = 0 then 
    exit;
  
  case S[1] of
    'A','C': if (S = 'ALL') or (S = 'CLEAR') then Result := hbcAll;
    'L': if S = 'LEFT' then Result := hbcLeft;
    'R': if S = 'RIGHT' then Result := hbcRight;
    else
      if FlagErrors then
        ReportError(SHtmlInvAlign);
  end;
end;

function TIpHtmlParser.ParseButtonType: TIpHtmlButtonType;
const
  TIpHtmlButtonTypeNames : array[TIpHtmlButtonType] of string = (
    'SUBMIT', 'RESET', 'BUTTON'
  );
var
  S: string;
begin
  Result := hbtSubmit;
  S := UpperCase(FindAttribute(htmlAttrTYPE));
  if Length(S) > 0 then
  begin
    for Result := Low(TIpHtmlButtonType) to High(TIpHtmlButtonType) do
      if S = TIpHtmlButtonTypeNames[Result] then exit;
    if FlagErrors then
      ReportError(SHtmlInvType);
  end;
end;

function TIpHtmlParser.ParseCellAlign(ADefault: TIpHtmlAlign): TIpHtmlAlign;
begin
  Result := GetAlignmentForStr(FindAttribute(htmlAttrALIGN), ADefault);
end;

procedure TIpHtmlParser.ParseCenter(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curContainer: TIpHtmlNodeDIV;
begin
  curContainer := TIpHtmlNodeDIV.Create(AParent);
  with curContainer do
    Align := haCenter;
  NextToken;
  ParseBodyText(curContainer, EndTokens + [IpHtmlTagCENTERend]);
  EnsureClosure(IpHtmlTagCENTERend, EndTokens);
end;

procedure TIpHtmlParser.ParseColGroup(AParent: TIpHtmlNode);
var
  curColGroup: TIpHtmlNodeCOLGROUP;
  curCol: TIpHtmlNodeCOL;
begin
  while FCurToken = IpHtmlTagCOLGROUP do begin
    curColGroup := TIpHtmlNodeCOLGROUP.Create(AParent);
    with curColGroup do begin
      ParseBaseProps(FOwner);
      Span := ParseInteger(htmlAttrSPAN, 1);
      Width := ParseHyperMultiLength(htmlAttrWIDTH, '');
    end;
    NextToken;
    SkipTextTokens;
    while FCurToken = IpHtmlTagCOL do begin
      curCol := TIpHtmlNodeCOL.Create(curColGroup);
      with curCol do begin
        ParseBaseProps(FOwner);
        Span := ParseInteger(htmlAttrSPAN, 1);
        Width := ParseHyperMultiLength(htmlAttrWIDTH, '');
      end;
      NextToken;
      SkipTextTokens;
    end;
    if FCurToken = IpHtmlTagCOLGROUPend then
      NextToken;
  end;
end;

procedure TIpHtmlParser.ParseDefinitionList(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  newDL: TIpHtmlNodeDL;
begin
  newDL := TIpHtmlNodeDL.Create(AParent);
  newDL.ParseBaseProps(FOwner);
  newDL.Compact := ParseBoolean(htmlAttrCOMPACT);
  NextToken;
  ParseDefinitionListItems(newDL, EndTokens + [IpHtmlTagDLend]);
  EnsureClosure(IpHtmlTagDLend, EndTokens);
end;

procedure TIpHtmlParser.ParseDefinitionListItems(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curDT: TIpHtmlNodeDT;
  curDD: TIpHtmlNodeDD;
begin
  while not (FCurToken in EndTokens) do begin
    case FCurToken of
      IpHtmlTagDT :
        begin
          curDT := TIpHtmlNodeDT.Create(AParent);
          curDT.ParseBaseProps(FOwner);
          NextToken;
          ParseBodyText(curDT, [IpHtmlTagDD, IpHtmlTagDTend] + EndTokens);
          if FCurToken = IpHtmlTagDTend then
            NextToken;
        end;
      IpHtmlTagDD :
        begin
          curDD := TIpHtmlNodeDD.Create(AParent);
          curDD.ParseBaseProps(FOwner);
          NextToken;
          ParseBodyText(curDD, [IpHtmlTagDT, IpHtmlTagDDend] + EndTokens);
          if FCurToken = IpHtmlTagDDend then
            NextToken;
        end;
      else
        ParseBodyText(AParent, EndTokens + [IpHtmlTagDT, IpHtmlTagDD]);
    end;
  end;
end;

procedure TIpHtmlParser.ParseDel(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  BQ: TIpHtmlNodeDEL;
begin
  BQ:= TIpHtmlNodeDEL.Create(AParent);
  BQ.ParseBaseProps(FOwner);
  BQ.Cite := FindAttribute(htmlAttrCITE);
  BQ.Datetime := FindAttribute(htmlAttrDATETIME);
  NextToken;
  ParseBodyText(BQ, EndTokens + [IpHtmlTagDELend]);
  EnsureClosure(IpHtmlTagDELend, EndTokens);
end;

function TIpHtmlParser.ParseDir: TIpHtmlDirection;
var
  S : string;
begin
  Result := hdLTR;
  S := UpperCase(FindAttribute(htmlAttrDIR));
  if (S = '') then
    Result := hdNone
  else if (S = 'LTR') then
    Result := hdLTR
  else if (S = 'RTL') then
    Result := hdRTL
  else
    if FlagErrors then
      ReportError(SHtmlInvDir);
end;

procedure TIpHtmlParser.ParseDiv(AParent: TIpHtmlNode;
  const EndTokens: TIpHtmlTokenSet);
var
  curDIV: TIpHtmlNodeDIV;
begin
  curDIV := TIpHtmlNodeDIV.Create(AParent);
  with curDIV do begin
    Align := ParseAlignment;
    ParseBaseProps(FOwner);
  end;
  NextToken;
  ParseBodyText(curDIV, EndTokens + [IpHtmlTagDIVend]);
  EnsureClosure(IpHtmlTagDIVend, EndTokens);
end;

procedure TIpHtmlParser.ParseFont(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curFont: TIpHtmlNodeFONT;
begin
  curFont := TIpHtmlNodeFONT.Create(AParent);
  with curFont do begin
    Face := FindAttribute(htmlAttrFACE);
    Size.Free;
    Size := nil;
    Size := ParseRelSize{('+0')};
    Size.OnChange := @SizeChanged;
    Color := ColorFromString(FindAttribute(htmlAttrCOLOR));
    ParseBaseProps(FOwner);
  end;
  NextToken;
  ParseBodyText(curFont, EndTokens + [IpHtmlTagFONTend]);
  EnsureClosure(IpHtmlTagFONTend, EndTokens);
end;

procedure TIpHtmlParser.ParseFontStyle(AParent: TIpHtmlNode;
  StartToken: TIpHtmlToken; const EndTokens: TIpHtmlTokenSet);
var
  curStyle: TIpHtmlNodeFontStyle;
begin
  curStyle := TIpHtmlNodeFontStyle.Create(AParent);
  case StartToken of
    IpHtmlTagTT :
      curStyle.Style := hfsTT;
    IpHtmlTagI :
      curStyle.Style := hfsI;
    IpHtmlTagB :
      curStyle.Style := hfsB;
    IpHtmlTagU :
      curStyle.Style := hfsU;
    IpHtmlTagSTRIKE :
      curStyle.Style := hfsSTRIKE;
    IpHtmlTagS :
      curStyle.Style := hfsS;
    IpHtmlTagBIG :
      curStyle.Style := hfsBIG;
    IpHtmlTagSMALL :
      curStyle.Style := hfsSMALL;
    IpHtmlTagSUB :
      curStyle.Style := hfsSUB;
    IpHtmlTagSUP :
      curStyle.Style := hfsSUP;
  end;
  curStyle.ParseBaseProps(FOwner);
  NextToken;
  ParseBodyText(curStyle, EndTokens);
  EnsureClosure(succ(StartToken), EndTokens);
end;

procedure TIpHtmlParser.ParseForm(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  newForm : TIpHtmlNodeFORM;
begin
  newForm := TIpHtmlNodeFORM.Create(AParent);
  with newForm do begin
    Action := FindAttribute(htmlAttrACTION);
    Method := ParseMethod;
    Enctype := FindAttribute(htmlAttrENCTYPE);
    Name := FindAttribute(htmlAttrNAME);
    AcceptCharset := FindAttribute(htmlAttrACCEPT_CHARSET);
    Accept := FindAttribute(htmlAttrACCEPT);
    if Enctype = '' then
      Enctype := 'application/x-www-form-urlencoded';
    if AcceptCharset = '' then
      AcceptCharset := 'UNKNOWN';
    ParseBaseProps(FOwner);
  end;
  NextToken;
  ParseBodyText(newForm, EndTokens + [IpHtmlTagFORMend]);
  EnsureClosure(IpHtmlTagFORMend, EndTokens);
end;

procedure TIpHtmlParser.ParseFormFields(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curSelect: TIpHtmlNodeSELECT;
  curTextArea: TIpHtmlNodeTEXTAREA;
  curButton: TIpHtmlNodeBUTTON;
  curOptGroup: TIpHtmlNodeOPTGROUP;
  curLabel: TIpHtmlNodeLABEL;
  curFieldset: TIpHtmlNodeFIELDSET;
  curLegend: TIpHtmlNodeLEGEND;
  curOption: TIpHtmlNodeOPTION;
  curInput: TIpHtmlNodeINPUT;
begin
  while not (FCurToken in EndTokens) do begin
    case FCurToken of
      IpHtmlTagINPUT:
        begin
          curInput := TIpHtmlNodeINPUT.Create(AParent);
          FOwner.TabList.Add(curInput);
          with curInput do begin
            ParseBaseProps(FOwner);
            InputType := ParseInputType;
            Name := FindAttribute(htmlAttrNAME);
            Value := FindAttribute(htmlAttrVALUE);
            Checked := ParseBoolean(htmlAttrCHECKED);
            Size := ParseInteger(htmlAttrSIZE, -1);
            MaxLength := ParseInteger(htmlAttrMAXLENGTH, -1);
            Src := FindAttribute(htmlAttrSRC);
            Align := ParseImageAlignment(hiaBottom);
            Disabled := ParseBoolean(htmlAttrDISABLED);
            ReadOnly := ParseBoolean(htmlAttrREADONLY);
            Alt := FindAttribute(htmlAttrALT);
            TabIndex := ParseInteger(htmlAttrTABINDEX, -1);
          end;
          NextToken;
        end;
      IpHtmlTagBUTTON:
        begin
          curButton := TIpHtmlNodeBUTTON.Create(AParent);
          FOwner.TabList.Add(curButton);
          with curButton do begin
            ParseBaseProps(FOwner);
            ButtonType := ParseButtonType;
            Name := FindAttribute(htmlAttrNAME);
            Value := FindAttribute(htmlAttrVALUE);
            Disabled := ParseBoolean(htmlAttrDISABLED);
            TabIndex := ParseInteger(htmlAttrTABINDEX, -1);
          end;
          NextToken;
          ParseBodyText(curButton, EndTokens + [IpHtmlTagBUTTONend]);
          if FCurToken = IpHtmlTagBUTTONend then
            NextToken
          else
            if FlagErrors then
              ReportExpectedToken(IpHtmlTagBUTTONend);
        end;
      IpHtmlTagSELECT:
        begin
          curSelect := TIpHtmlNodeSELECT.Create(AParent);
          with curSelect do begin
            Name := FindAttribute(htmlAttrNAME);
            Size := ParseInteger(htmlAttrSIZE, -1);
            Width := ParseInteger(htmlAttrWIDTH, -1);
            ParseBaseProps(FOwner);
            Multiple := ParseBoolean(htmlAttrMULTIPLE);
            ComboBox := ParseBoolean(htmlAttrCOMBOBOX);
            Disabled := ParseBoolean(htmlAttrDISABLED);
            TabIndex := ParseInteger(htmlAttrTABINDEX, -1);
            Alt := FindAttribute(htmlAttrALT);
          end;
          NextNonBlankToken;
          repeat
            case FCurToken of
              IpHtmlTagOPTION :
                begin
                  curOption := TIpHtmlNodeOPTION.Create(curSelect);
                  with curOption do begin
                    ParseBaseProps(FOwner);
                    Selected := ParseBoolean(htmlAttrSELECTED);
                    Value := FindAttribute(htmlAttrVALUE);
                    Disabled := ParseBoolean(htmlAttrDISABLED);
                    OptionLabel := FindAttribute(htmlAttrLABEL);
                  end;
                  NextNonBlankToken;
                  ParseText(curOption, EndTokens + [IpHtmlTagSelectEND, IpHtmlTagOption, IpHtmlTagOPTIONend]);
                  if FCurToken = IpHtmlTagOPTIONend then
                    NextNonBlankToken;
                end;
              IpHtmlTagOPTGROUP :
                begin
                  curOptGroup := TIpHtmlNodeOPTGROUP.Create(curSelect);
                  with curOptGroup do begin
                    ParseBaseProps(FOwner);
                    Disabled := ParseBoolean(htmlAttrDISABLED);
                    GroupLabel := FindAttribute(htmlAttrLABEL);
                  end;
                  NextNonBlankToken;
                  while FCurToken = IpHtmlTagOPTION do begin
                    curOption := TIpHtmlNodeOPTION.Create(curOptGroup);
                    FOwner.TabList.Add(curOption);
                    with curOption do begin
                      ParseBaseProps(FOwner);
                      Selected := ParseBoolean(htmlAttrSELECTED);
                      Value := FindAttribute(htmlAttrVALUE);
                      Disabled := ParseBoolean(htmlAttrDISABLED);
                      OptionLabel := FindAttribute(htmlAttrLABEL);
                    end;
                    NextNonBlankToken;
                    ParseText(curOption, EndTokens + [IpHtmlTagSelectEND, IpHtmlTagOption, IpHtmlTagOPTIONend]);
                    if FCurToken = IpHtmlTagOPTIONend then
                      NextNonBlankToken;
                  end;
                  if FCurToken = IpHtmlTagOPTGROUPend then
                    NextNonBlankToken
                  else
                  if FCurToken = IpHtmlTagOPTGROUP then
                  else
                  if FCurToken = IpHtmlTagOPTION then
                  else
                  if FCurToken = IpHtmlTagSELECTend then
                  else
                    if FlagErrors then
                      ReportExpectedToken(IpHtmlTagOPTGROUPend);
                end;
              else
                break;
            end;
          until False;
          if FCurToken = IpHtmlTagSELECTend then
            NextNonBlankToken;
        end;
      IpHtmlTagTEXTAREA:
        begin
          curTextArea := TIpHtmlNodeTEXTAREA.Create(AParent);
          FOwner.TabList.Add(curTextArea);
          with curTextArea do begin
            Name := FindAttribute(htmlAttrNAME);
            Rows := ParseInteger(htmlAttrROWS, 20);
            Cols := ParseInteger(htmlAttrCOLS, 20);
            ParseBaseProps(FOwner);
            Disabled := ParseBoolean(htmlAttrDISABLED);
            ReadOnly := ParseBoolean(htmlAttrREADONLY);
            TabIndex := ParseInteger(htmlAttrTABINDEX, -1);
            Alt := FindAttribute(htmlAttrALT);
          end;
          NextToken;
          ParseText(curTextArea, [IpHtmlTagTEXTAREAend]);
          if FCurToken = IpHtmlTagTEXTAREAend then
            NextToken
          else
            if FlagErrors then
              ReportExpectedToken(IpHtmlTagTEXTAREAend);
        end;
      IpHtmlTagLABEL :
        begin
          curLabel := TIpHtmlNodeLABEL.Create(AParent);
          with curLabel do begin
            ParseBaseProps(FOwner);
            LabelFor := FindAttribute(htmlAttrLABEL);
          end;
          NextToken;
          ParseBodyText(curLabel, [IpHtmlTagLABELend]);
          if FCurToken = IpHtmlTagLABELend then
            NextToken
          else
            if FlagErrors then
              ReportExpectedToken(IpHtmlTagLABELend);
        end;
      IpHtmlTagFIELDSET :
        begin
          curFieldset := TIpHtmlNodeFIELDSET.Create(AParent);
          with curFieldset do
            ParseBaseProps(FOwner);
          NextToken;
          ParseFormFields(curFieldSet, EndTokens + [IpHtmlTagFIELDSETend]);
          if FCurToken = IpHtmlTagFIELDSETend then
            NextToken
          else
            if FlagErrors then
              ReportExpectedToken(IpHtmlTagFIELDSETend);
        end;
      IpHtmlTagLEGEND :
        begin
          curLegend := TIpHtmlNodeLEGEND.Create(AParent);
          with curLegend do begin
            ParseBaseProps(FOwner);
          end;
          NextToken;
          ParseBodyText(CurLegend, [IpHtmlTagLEGENDend]);
          if FCurToken = IpHtmlTagLEGENDend then
            NextToken
          else
            if FlagErrors then
              ReportExpectedToken(IpHtmlTagLEGENDend);
        end;
      else
        Exit;
    end;
  end;
end;

procedure TIpHtmlParser.ParseFrame(AParent: TIpHtmlNode);
var
  curFrame: TIpHtmlNodeFRAME;
begin
  curFrame := TIpHtmlNodeFRAME.Create(AParent);
  with curFrame do begin
    LongDesc := FindAttribute(htmlAttrLONGDESC);
    Name := FindAttribute(htmlAttrNAME);
    Src := FindAttribute(htmlAttrSRC);
    FrameBorder := ParseInteger(htmlAttrBORDER, 1);
    MarginWidth := ParseInteger(htmlAttrMARGINWIDTH, 1);
    MarginHeight := ParseInteger(htmlAttrMARGINHEIGHT, 1);
    NoResize := ParseBoolean(htmlAttrNORESIZE);
    Scrolling := ParseFrameScrollingProp;
    ParseBaseProps(FOwner);
  end;
  NextToken;
end;

function TIpHtmlParser.ParseFrameProp(ADefault: TIpHtmlFrameProp): TIpHtmlFrameProp;
var
  S: string;
begin
  Result := hfVoid;
  S := UpperCase(FindAttribute(htmlAttrFRAME));
  if Length(S) = 0 then
  begin
    Result := ADefault;
    exit;
  end;
  
  case S[1] of
    'A': if (S = 'ABOVE') then Result := hfAbove;
    'B': if S = 'BELOW' then Result := hfBelow
         else if S = 'BOX' then Result := hfBox
         else if S = 'BORDER' then Result := hfBorder;
    'H': if S = 'HSIDES' then Result := hfHSides;
    'L': if S = 'LHS' then Result := hfLhs;
    'R': if S = 'RHS' then Result := hfRhs;
    'V': if (S = 'VOID') then exit
         else if S = 'VSIDES' then
           Result := hfvSides;
    else
      if FlagErrors then
        ReportError(SHtmlInvFrame);
  end;
end;      

function TIpHtmlParser.ParseFrameScrollingProp: TIpHtmlFrameScrolling;
var
  S: string;
begin
  Result := hfsAuto;
  S := UpperCase(FindAttribute(htmlAttrSCROLLING));
  if (length(S) = 0) then exit;
  case S[1] of
    'A': if (S = 'AUTO') then exit;
    'N': if S = 'NO' then Result := hfsNo;
    'Y': if S = 'YES' then Result := hfsYes;
    else
      if FlagErrors then
        ReportError(SHtmlInvScroll);
  end;
end;

procedure TIpHtmlParser.ParseFrameSet(AParent: TIpHtmlNode;
  const EndTokens: TIpHtmlTokenSet);
begin
  FHasFrames := True;   
  while FCurToken = IpHtmlTagFRAMESET do begin
    FCurFrameSet := TIpHtmlNodeFRAMESET.Create(AParent);
    with FCurFrameSet do begin
      Rows := ParseHyperMultiLengthList(htmlAttrROWS, '100%');
      Cols := ParseHyperMultiLengthList(htmlAttrCOLS, '100%');
      Id := FindAttribute(htmlAttrID);
      ClassId := FindAttribute(htmlAttrCLASS);
      Title := FindAttribute(htmlAttrTITLE);
      Style := FindAttribute(htmlAttrSTYLE);
    end;
    NextToken;
    if FCurToken = IpHtmlTagFRAMESET then
      ParseFrameSet(FCurFrameSet, EndTokens + [IpHtmlTagFRAMESETend]);
    while FCurToken = IpHtmlTagFRAME do
      ParseFrame(FCurFrameSet);
    if FCurToken = IpHtmlTagNOFRAMES then
      ParseNOFRAMES(FCurFrameSet);
    if FCurToken = IpHtmlTagFRAMESETend then
      NextToken;
  end;
end;

procedure TIpHtmlParser.ParseHead(AParent: TIpHtmlNode);
begin
  {lead token is optional}
  if FCurToken = IpHtmlTagHEAD then begin
    NextToken;
    ParseHeadItems(TIpHtmlNodeHEAD.Create(AParent));
    if FCurToken = IpHtmlTagHEADend then
      NextToken;
  end;
  if SameText(FDocCharset, 'UTF-8') then  // clear for UTF-8 to avoid conversion
    FDocCharset := '';
end;

procedure TIpHtmlParser.ParseHeader(AParent: TIpHtmlNode; EndToken: TIpHtmlToken; 
  ASize: Integer);
var
  newHeader: TIpHtmlNodeHeader;
begin
  newHeader := TIpHtmlNodeHeader.Create(AParent);
  newHeader.ElementName := 'h' + IntToStr(ASize);
  newHeader.ParseBaseProps(FOwner);
  newHeader.Size := ASize;
  newHeader.Align := ParseAlignment;
  NextToken;
  ParseBodyText(newHeader, [EndToken]);
  if FCurToken = EndToken then
    NextToken
  else if FlagErrors then
    ReportExpectedToken(EndToken);
end;

procedure TIpHtmlParser.ParseHeadItems(AParent: TIpHtmlNode);
begin
  while not (FCurToken in [IpHtmlTagEOF, IpHtmlTagHEADend, IpHtmlTagFRAMESET, IpHtmlTagBODY]) do 
  begin
    case FCurToken of
      IpHtmlTagTITLE :
        ParseTitle(AParent);
      IpHtmlTagSTYLE :
        ParseStyle(AParent);
      IpHtmlTagSCRIPT :
        ParseScript(AParent, [IpHtmlTagEOF]);
      IpHtmlTagNOSCRIPT :
        ParseNoscript(AParent);
      IpHtmlTagISINDEX :
        ParseIsIndex;
      IpHtmlTagBASE :
        ParseBase;
      IpHtmlTagMETA :
        ParseMeta(AParent);
      IpHtmlTagLINK :
        ParseLink(AParent);
      else
        NextToken;    // unknown
    end;
  end;
end;

procedure TIpHtmlParser.ParseHR(AParent: TIpHtmlNode);
var
  newRule: TIpHtmlNodeHR;
begin
  newRule := TIpHtmlNodeHR.Create(AParent);
  with newRule do begin
    Align := ParseImageAlignment(hiaCenter);
    NoShade := ParseBoolean(htmlAttrNOSHADE);
    Size := ParseHtmlInteger2(htmlAttrSIZE, 1);
    Size.OnChange := @WidthChanged;
    Width := ParseHyperLength(htmlAttrWIDTH, '100%');
    Width.OnChange := @WidthChanged;
    Color := ColorFromString(FindAttribute(htmlAttrCOLOR));
    ParseBaseProps(FOwner);
  end;
  NextToken;
end;

procedure TIpHtmlParser.ParseHtml;
begin
  {lead token is optional}
  if FCurToken = IpHtmlTagHtml then begin
    FOwner.HtmlNode.Version := FindAttribute(htmlAttrVERSION);
    FOwner.HtmlNode.Lang := FindAttribute(htmlAttrLANG);
    FOwner.HtmlNode.Dir := ParseDir;
    NextToken;
    ParseHead(FOwner.HtmlNode); {may not be present}
    ParseBody(FOwner.HtmlNode, [IpHtmlTagHtmlend, IpHtmlTagEOF]); {may not be present}
    if FCurToken in [IpHtmlTagHtmlend, IpHtmlTagEOF] then
    else
      if FlagErrors then
        ReportExpectedToken(IpHtmlTagHtmlend);
    NextToken;
  end else begin
    ParseHead(FOwner.HtmlNode); {may not be present}
    ParseBody(FOwner.HtmlNode, [IpHtmlTagEof]); {may not be present}
  end;
end;

function TIpHtmlParser.ParseHyperLength(const AttrNameSet: TIpHtmlAttributesSet;
  const ADefault: string): TIpHtmlLength;
var
  S, units: string;
  n: Double;
  P, Err: Integer;
begin
  Result := TIpHtmlLength.Create;
  Result.LengthType := hlUndefined;
  S := FindAttribute(AttrNameSet);
  if Length(S) = 0 then
  begin
    if Length(aDefault) = 0 then 
      exit
    else 
      S := ADefault;
  end;
  P := CharPos('%', S);
  if P <> 0 then begin
    Result.LengthType := hlPercent;
    Delete(S, P, 1);
  end else
    Result.LengthType := hlAbsolute;
  // Remove non-numeric appendix
  units := '';
  for P := Length(S) downto 1 do
    if not (S[P] in ['0'..'9', '+', '-', '.']) then
      units := S[P] + units
    else begin
      SetLength(S, P);
      break;
    end;
  val(S, n, Err);
  if n < 0 then n := 0;
  Result.LengthValue := round(n);
  if (Err <> 0) or (Result.LengthValue < 0) then begin
    if FlagErrors then
      ReportError(SHtmlInvInt)
    else
      Result.LengthType := hlUndefined;
  end else
    if (Result.LengthType = hlPercent) and (Result.LengthValue > 100) then
      Result.LengthValue := 100;
end;

function TIpHtmlParser.ParseHyperMultiLength(const AttrNameSet: TIpHtmlAttributesSet;
  const ADefault: string): TIpHtmlMultiLength;
var
  S, units: string;
  n: Double;
  P, Err: Integer;
begin
  Result := TIpHtmlMultiLength.Create;
  Result.LengthType := hmlUndefined;
  S := FindAttribute(AttrNameSet);
  if Length(S) = 0 then
  begin
    if Length(ADefault) = 0 then 
      exit
    else 
      S := ADefault;
  end;
  P := CharPos('%', S);
  if P <> 0 then begin
    Result.LengthType := hmlPercent;
    Delete(S, P, 1);
  end else begin
    P := CharPos('*', S);
    if P <> 0 then begin
      Result.LengthType := hmlRelative;
      Delete(S, P, 1);
    end else
      Result.LengthType := hmlAbsolute;
  end;
  
  // Remove non-numeric appendix
  units := '';
  for P := Length(S) downto 1 do
    if not (S[P] in ['0'..'9', '+', '-', '.']) then
      units := S[P] + units
    else begin
      SetLength(S, P);
      break;
    end;

  val(s, n, Err);
  if n < 0 then n := 0;
  Result.LengthValue := round(n);
  if (Err <> 0) or (Result.LengthValue < 0) then begin
    if FlagErrors then
      ReportError(SHtmlInvInt)
    else
      Result.LengthType := hmlUndefined;
  end;
end;

function TIpHtmlParser.ParseHyperMultiLengthList(const AttrNameSet: TIpHtmlAttributesSet;
  const ADefault: string): TIpHtmlMultiLengthList;
var
  S, S2, units: string;
  B, E, P, Err: Integer;
  n: Double;
  NewEntry: TIpHtmlMultiLength;
begin
  Result := TIpHtmlMultiLengthList.Create;
  S := FindAttribute(AttrNameSet);
  if Length(S) = 0 then
  begin
    if length(ADefault) = 0 then 
      exit
    else 
      S := ADefault;
  end;
  B := 1;
  while B <= length(S) do begin
    E := B;
    repeat
      Inc(E);
    until (E > Length(S)) or (S[E] = ',');
    S2 := copy(S, B, E - B);
    newEntry := TIpHtmlMultiLength.Create;
    newEntry.LengthType := hmlUndefined;
    P := CharPos('%', S2);
    if P <> 0 then begin
      newEntry.LengthType := hmlPercent;
      Delete(S2, P, 1);
    end else begin
      P := CharPos('*', S2);
      if P <> 0 then begin
        newEntry.LengthType := hmlRelative;
        Delete(S2, P, 1);
      end else
        newEntry.LengthType := hmlAbsolute;
    end;
    if S2 = '' then
      newEntry.LengthValue := 0
    else begin
      // Remove non-numeric appendix
      units := '';
      for P := Length(S2) downto 1 do
        if not (S2[P] in ['0'..'9', '+', '-', '.']) then
          units := S2[P] + units
        else begin
          SetLength(S2, P);
          break;
        end;
      val(S2, n, Err);
      if n < 0 then n := 0;
      newEntry.LengthValue := round(n);
      if (Err <> 0) or (NewEntry.LengthValue < 0) then begin
        if FlagErrors then
          ReportError(SHtmlInvInt)
        else
          newEntry.LengthType := hmlUndefined;
      end;
    end;
    Result.AddEntry(newEntry);
    B := E + 1;
  end;
end;

procedure TIpHtmlParser.ParseIFrame(AParent: TIpHtmlNode);
var
  curFrame: TIpHtmlNodeIFRAME;
begin
  curFrame := TIpHtmlNodeIFRAME.Create(AParent);
  with curFrame do begin
    LongDesc := FindAttribute(htmlAttrLONGDESC);
    Name := FindAttribute(htmlAttrNAME);
    Src := FindAttribute(htmlAttrSRC);
    FrameBorder := ParseInteger(htmlAttrBORDER, 1);
    MarginWidth := ParseInteger(htmlAttrMARGINWIDTH, 1);
    MarginHeight := ParseInteger(htmlAttrMARGINHEIGHT, 1);
    Scrolling := ParseFrameScrollingProp;
    Align := ParseAlignment;
    Height := ParseHyperLength(htmlAttrHEIGHT, '');
    Height.OnChange := @WidthChanged;
    Width := ParseHyperLength(htmlAttrWIDTH, '');
    Width.OnChange := @WidthChanged;
    ParseBaseProps(FOwner);
  end;
  NextToken;
  ParseBodyText(curFrame, [IpHtmlTagIFRAMEend]);
  if FCurToken = IpHtmlTagIFRAMEend then
    NextToken;
end;

function TIpHtmlParser.ParseImageAlignment(ADefault: TIpHtmlImageAlign): TIpHtmlImageAlign;
const
  TIpHtmlImageAlignNames : array[TIpHtmlImageAlign] of string = (
    'TOP', 'MIDDLE', 'BOTTOM', 'LEFT', 'RIGHT', 'CENTER');
var
  S : string;
begin
  Result := aDefault;
  S := UpperCase(FindAttribute(htmlAttrALIGN));
  if Length(S) = 0 then exit;
  for Result := Low(TIpHtmlImageAlign) to High(TIpHtmlImageAlign) do
    if S = TIpHtmlImageAlignNames[Result] then 
      exit;
  
  if FlagErrors then
    ReportError(SHtmlInvAlign);
end;

procedure TIpHtmlParser.ParseImg(AParent: TIpHtmlNode);
var
  curIMG : TIpHtmlNodeIMG;
begin
  curIMG := TIpHtmlNodeIMG.Create(AParent);
  with curIMG do begin
    Src := FindAttribute(htmlAttrSRC);
    Alt := FindAttribute(htmlAttrALT);
    Align := ParseImageAlignment(hiaBottom);
    Height := ParsePixels(htmlAttrHEIGHT, '');
    Height.OnChange := @DimChanged;
    Width := ParseHyperLength(htmlAttrWIDTH, '');
    Width.OnChange := @DimChanged;
    Border := ParseInteger(htmlAttrBORDER, 0);
    HSpace := ParseInteger(htmlAttrHSPACE, 0);
    VSpace := ParseInteger(htmlAttrVSPACE, 0);
    UseMap := FindAttribute(htmlAttrUSEMAP);
    IsMap := ParseBoolean(htmlAttrISMAP);
    ParseBaseProps(FOwner);
    LongDesc := FindAttribute(htmlAttrLONGDESC);
    Name := FindAttribute(htmlAttrNAME);
  end;
  NextToken;
end;    

procedure TIpHtmlParser.ParseInline(AParent: TIpHtmlNode; const EndTokens: TIpHtmlTokenSet);
begin
  case FCurToken of
    IpHtmlTagP: 
      ParseParagraph(AParent, EndTokens); {moved from block}
    IpHtmlTagFont: 
      ParseFont(AParent, EndTokens);
    IpHtmlTagDIV: 
      ParseDiv(AParent, EndTokens);
    IpHtmlTagSPAN: 
      ParseSpan(AParent, EndTokens);
    IpHtmlTagLEFT: 
      ParseLeft(AParent, EndTokens);
    IpHtmlTagCENTER: 
      ParseCenter(AParent, EndTokens);
    IpHtmlTagRIGHT: 
      ParseRight(AParent, EndTokens);
    IpHtmlTagBLINK: 
      ParseBlink(AParent, EndTokens);
    IpHtmlTagQ: 
      ParseQ(AParent, EndTokens);
    IpHtmlTagHR: 
      ParseHR(AParent);
    IpHtmlTagTT, IpHtmlTagI, IpHtmlTagB, IpHtmlTagU, IpHtmlTagSTRIKE, IpHtmlTagS,
    IpHtmlTagBIG, IpHtmlTagSMALL, IpHtmlTagSUB, IpHtmlTagSUP :
      ParseFontStyle(AParent, FCurToken, EndTokens + [succ(FCurToken)]);
    IpHtmlTagEM, IpHtmlTagSTRONG, IpHtmlTagDFN, IpHtmlTagCODE,
    IpHtmlTagSAMP, IpHtmlTagKBD, IpHtmlTagVAR, IpHtmlTagCITE,
    IpHtmlTagABBR, IpHtmlTagACRONYM :
      ParsePhraseElement(AParent, FCurToken, succ(FCurToken), EndTokens);
    IpHtmlTagA: 
      ParseAnchor(AParent, EndTokens);
    IpHtmlTagBASEFONT: 
      ParseBaseFont(AParent);
    IpHtmlTagBR: 
      ParseBR(AParent);
    IpHtmlTagNOBR: 
      ParseNOBR(AParent);
    IpHtmlTagMAP: 
      ParseMAP(AParent, EndTokens);
    IpHtmlTagText:
      begin
        if FInPre > 0 then
          TIpHtmlNodeText.Create(AParent).ANSIText := GetTokenString
        else
          TIpHtmlNodeText.Create(AParent).EscapedText := GetTokenString;
        NextToken;
      end;
    IpHtmlTagINPUT,
    IpHtmlTagSELECT,
    IpHtmlTagButton,
    IpHtmlTagTEXTAREA:
      ParseFormFields(AParent, EndTokens);
    IpHtmlTagINS:
      ParseIns(AParent, EndTokens);
    IpHtmlTagDEL:
      ParseDel(AParent, EndTokens);
    IpHtmlTagIFRAME:
      ParseIFRAME(AParent);
    IpHtmlTagSCRIPT:
      ParseScript(AParent, EndTokens);
    IpHtmlTagNOSCRIPT:
      ParseNoscript(AParent);
    IpHtmlTagSTYLE:
      ParseStyle(AParent);
    else
      NextToken;
  end;
end;

function TIpHtmlParser.ParseInputType: TIpHtmlInputType;
const
  IpHtmlInputTypeNames: array[TIpHtmlInputType] of string = (
    'TEXT', 'PASSWORD', 'CHECKBOX', 'RADIO',
    'SUBMIT', 'RESET', 'FILE', 'HIDDEN', 'IMAGE', 'BUTTON'
  );
var
  S : string;
begin
  Result := hitText;
  S := UpperCase(FindAttribute(htmlAttrTYPE));
  if (Length(S) = 0) or (S = 'TEXTAREA') then
    //
  else
  begin
    for Result := Low(TIpHtmlInputType) to High(TIpHtmlInputType) do
      if S = IpHtmlInputTypeNames[Result] then exit;
    if FlagErrors then
      ReportError(SHtmlInvType);
  end;
end;

procedure TIpHtmlParser.ParseIns(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  BQ: TIpHtmlNodeINS;
begin
  BQ := TIpHtmlNodeINS.Create(AParent);
  BQ.ParseBaseProps(FOwner);
  BQ.Cite := FindAttribute(htmlAttrCITE);
  BQ.Datetime := FindAttribute(htmlAttrDATETIME);
  NextToken;
  ParseBodyText(BQ, EndTokens + [IpHtmlTagINSend]);
  EnsureClosure(IpHtmlTagINSend, EndTokens);
end;

function TIpHtmlParser.ParseInteger(const AttrNameSet: TIpHtmlAttributesSet; 
  ADefault: Integer): Integer;
var
  S: string;
  Err: Integer;
  AttrName: string;
begin
  AttrName := TIpHtmlAttributesNames[AttrNameSet];
  S := FindAttribute(AttrNameSet);
  if Length(S) = 0 then
    Result := ADefault
  else
  if CompareText(S, AttrName) = 0 then
    Result := 1
  else begin
    Val(S, Result, Err);
    if Err <> 0 then begin
      Result := ADefault;
      if FlagErrors then
        ReportError(SHtmlInvInt)
    end;
  end;
end;

function TIpHtmlParser.ParseHtmlInteger2(const AttrNameSet: TIpHtmlAttributesSet;
  ADefault: Integer): TIpHtmlInteger;
begin
  Result := TIpHtmlInteger.Create(ParseInteger(AttrNameSet, aDefault));
end;

procedure TIpHtmlParser.ParseIsIndex;
begin
  FIndexPhrase := FindAttribute(htmlAttrPROMPT);
  NextToken;
end;

procedure TIpHtmlParser.ParseLeft(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curContainer: TIpHtmlNodeDIV;
begin
  curContainer := TIpHtmlNodeDIV.Create(AParent);
  with curContainer do
    Align := haLeft;
  NextToken;
  ParseBodyText(curContainer, EndTokens + [IpHtmlTagLEFTend]);
  EnsureClosure(IpHtmlTagLEFTend, EndTokens);
end;

procedure TIpHtmlParser.ParseLink(AParent: TIpHtmlNode);
begin
  with TIpHtmlNodeLINK.Create(AParent) do begin
    HRef := FindAttribute(htmlAttrHREF);
    Rel := FindAttribute(htmlAttrREL);
    Rev := FindAttribute(htmlAttrREV);
    Title := FindAttribute(htmlAttrTITLE);
    Type_ := LowerCase(FindAttribute(htmlAttrTYPE));
    if (LowerCase(Rel) = 'stylesheet') and (Type_ = 'text/css') then
      ParseStyleSheet(AParent, Href);
    ParseBaseProps(FOwner);
  end;
  NextToken;
end;

procedure TIpHtmlParser.ParseListItems(AParent: TIpHtmlNodeCore;
  EndToken: TIpHtmlToken; const EndTokens: TIpHtmlTokenSet);
var
  newListItem: TIpHtmlNodeLI;
begin
  while not (FCurToken in EndTokens) do begin
    case FCurToken of
      IpHtmlTagLI :
        begin
          newListItem := TIpHtmlNodeLI.Create(AParent);
          newListItem.ParseBaseProps(FOwner);
          newListItem.ListType := ParseULStyle(ulUndefined);
          newListItem.Value := ParseInteger(htmlAttrVALUE, -1);
          newListItem.Compact := ParseBoolean(htmlAttrCOMPACT);
          NextToken;
          ParseBodyText(
            newListItem,
            EndTokens + [EndToken, IpHtmlTagLI, IpHtmlTagLIend] - [IpHtmlTagP, IpHtmlTagPend]
          );
          if FCurToken = IpHtmlTagLIend then
            NextToken;
          SkipTextTokens;
        end;
      else
        ParseBodyText(AParent, EndTokens + [EndToken, IpHtmlTagLI]);
    end;
  end;
end;

procedure TIpHtmlParser.ParseMap(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curMap: TIpHtmlNodeMAP;
begin
  curMap := TIpHtmlNodeMAP.Create(AParent);
  curMap.Name := FindAttribute(htmlAttrNAME);
  curMap.ParseBaseProps(FOwner);
  NextToken;
  while not (FCurToken in EndTokens + [IpHtmlTagMAPend]) do begin
    case FCurToken of
      IpHtmlTagAREA :
        begin
          with TIpHtmlNodeAREA.Create(curMap) do begin
            Shape := ParseShape;
            Coords := FindAttribute(htmlAttrCOORDS);
            HRef := FindAttribute(htmlAttrHREF);
            NoHRef := ParseBoolean(htmlAttrNOHREF);
            Alt := FindAttribute(htmlAttrALT);
            TabIndex := ParseInteger(htmlAttrTABINDEX, -1);
            Target := FindAttribute(htmlAttrTARGET);
            ParseBaseProps(FOwner);
          end;
          NextToken;
        end;
      else
        if FlagErrors then
          ReportExpectedError('</MAP> or <AREA>')
        else
          NextToken;
    end;
  end;
  EnsureClosure(IpHtmlTagMAPend, EndTokens);
end;

procedure TIpHtmlParser.ParseMeta(AParent: TIpHtmlNode);
var
  i,j: Integer;
begin
  with TIpHtmlNodeMETA.Create(AParent) do begin
    HttpEquiv := FindAttribute(htmlAttrHTTP_EQUIV);
    Name := FindAttribute(htmlAttrNAME);
    Content := FindAttribute(htmlAttrCONTENT);
    if not FHasBOM then begin
      if SameText(HttpEquiv, 'content-type') then begin
        j := PosI('charset=', Content);
        if j>0 then begin
          j := j+8;
          i := j;
          while (j <= Length(Content)) do begin
            if Content[j] in [' ',';','"',','] then
              break;
            inc(j);
          end;
          fDocCharset := copy(content, i, j-i);
        end;
      end
      else
        fDocCharset := FindAttribute(htmlAttrCHARSET);
      if LazStartsText('windows', fDocCharset) then
        fDocCharset := NormalizeEncoding(StringReplace(fDocCharset, 'windows', 'cp', [rfIgnoreCase]));
    end;
    Scheme := FindAttribute(htmlAttrSCHEME);
  end;
  NextToken;
end;

function TIpHtmlParser.ParseMethod: TIpHtmlFormMethod;
var
  S: string;
begin
  Result := hfmGet;
  S := UpperCase(FindAttribute(htmlAttrMETHOD));
  if (Length(S) = 0) or (S = 'GET') then
  else
  if S = 'POST' then
    Result := hfmPost
  else
    if FlagErrors then
      ReportError(SHtmlInvMethod);
end;

procedure TIpHtmlParser.ParseNOBR(AParent: TIpHtmlNode);
begin
  NextToken;
  ParseBodyText(TIpHtmlNodeNOBR.Create(AParent), [IpHtmlTagNOBRend]);
  if FCurToken = IpHtmlTagNOBRend then
    NextToken
  else
    if FlagErrors then
      ReportExpectedToken(IpHtmlTagNOBRend);
end;

procedure TIpHtmlParser.ParseNoFrames(AParent: TIpHtmlNode);
var
  curNoFrames: TIpHtmlNodeNOFRAMES;
begin
  curNoFrames := TIpHtmlNodeNOFRAMES.Create(AParent);
  NextToken;
  ParseBodyText(curNoFrames, [IpHtmlTagNOFRAMESend, IpHtmlTagFRAMESETend]);
  if FCurToken = IpHtmlTagNOFRAMESend then
    NextToken;
end;

procedure TIpHtmlParser.ParseNoScript(AParent: TIpHtmlNode);
var
  curScript: TIpHtmlNodeNOSCRIPT;
begin
  curScript := TIpHtmlNodeNOSCRIPT.Create(AParent);
  with curScript do begin
    ParseBaseProps(FOwner);
  end;
  NextToken;
  ParseBodyText(curScript, [IpHtmlTagNOSCRIPTend]);
  if FCurToken = IpHtmlTagNOSCRIPTend then
    NextToken
  else
    if FlagErrors then
      ReportExpectedToken(IpHtmlTagNOSCRIPTend);
end;

procedure TIpHtmlParser.ParseObject(AParent: TIpHtmlNode);
var
  curObject: TIpHtmlNodeOBJECT;
  curParam: TIpHtmlNodePARAM;
begin
  curObject := TIpHtmlNodeOBJECT.Create(AParent);
  with curOBJECT do begin
    ClassID := FindAttribute(htmlAttrCLASSID);
    Codebase := FindAttribute(htmlAttrCODEBASE);
    Data := FindAttribute(htmlAttrDATA);
    CodeType := FindAttribute(htmlAttrCODETYPE);
    Archive := FindAttribute(htmlAttrARCHIVE);
    Standby := FindAttribute(htmlAttrSTANDBY);
    Align := ParseImageAlignment(hiaBottom);
    Height := ParseInteger(htmlAttrHEIGHT, -1);
    Width := ParseHyperLength(htmlAttrWIDTH, '');
    Width.OnChange := @WidthChanged;
    Border := ParseInteger(htmlAttrBORDER, 0);
    HSpace := ParseInteger(htmlAttrHSPACE, 1);
    VSpace := ParseInteger(htmlAttrVSPACE, 1);
    UseMap := FindAttribute(htmlAttrUSEMAP);
    Declare := ParseBoolean(htmlAttrDECLARE);
    ParseBaseProps(FOwner);
    Name := FindAttribute(htmlAttrNAME);
  end;
  NextToken;
  while not (FCurToken = IpHtmlTagOBJECTend) do begin
    case FCurToken of
      IpHtmlTagPARAM :
        begin
          curParam := TIpHtmlNodePARAM.Create(curObject);
          with curParam do begin
            Name := FindAttribute(htmlAttrNAME);
            Value := FindAttribute(htmlAttrVALUE);
            Id := FindAttribute(htmlAttrID);
            ValueType := ParseObjectValueType;
          end;
          NextToken;
        end;
      else
        ParseText(curObject, [IpHtmlTagOBJECTend, IpHtmlTagPARAM]);
    end;
  end;
  if FCurToken = IpHtmlTagOBJECTend then
    NextToken
  else
    if FlagErrors then
      ReportExpectedToken(IpHtmlTagOBJECTend);
end;

function TIpHtmlParser.ParseObjectValueType: TIpHtmlObjectValueType;
var
  S: string;
begin
  Result := hovtData;
  S := UpperCase(FindAttribute(htmlAttrVALUETYPE));
  if Length(S) = 0 then 
    exit;
  case S[1] of
    'D': if S = 'DATA' then exit;
    'O': if S = 'OBJECT' then Result := hovtObject;
    'R': if S = 'REF' then Result := hovtRef;
    else
      if FlagErrors then
        ReportError(SHtmlInvValType);
  end;
end;

function TIpHtmlParser.ParseOLStyle(ADefault: TIpHtmlOLStyle): TIpHtmlOLStyle;
const
  TIpHtmlOLStyleNames : array[TIpHtmlOLStyle] of char = ('1', 'a', 'A', 'i', 'I');
var
  S : string;
begin
  Result := ADefault;
  S := FindAttribute(htmlAttrTYPE);
  if Length(S) > 0 then
  begin
    for Result := Low(TIpHtmlOLStyle) to High(TIpHtmlOLStyle) do
      if S = TIpHtmlOLStyleNames[Result] then exit;
    if FlagErrors then
      ReportError(SHtmlInvType);
  end;
end;

procedure TIpHtmlParser.ParseOrderedList(AParent: TIpHtmlNode;
  EndToken: TIpHtmlToken; const EndTokens: TIpHtmlTokenSet);
var
  newList: TIpHtmlNodeOL;
begin
  newList := TIpHtmlNodeOL.Create(AParent);
  newList.ParseBaseProps(FOwner);
  newList.Start := ParseInteger(htmlAttrSTART, 1);
  newList.Compact := ParseBoolean(htmlAttrCOMPACT);
  newList.Style := ParseOLStyle(olArabic);
  
  NextToken;
  
  Inc(FListLevel);
  ParseListItems(
    newList, 
    EndToken, 
    EndTokens + [EndToken] - [IpHtmlTagP, IpHtmlTagLI]
  );
  Dec(FListLevel);
  
  EnsureClosure(EndToken, EndTokens);
end;

procedure TIpHtmlParser.ParseParagraph(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  newPara: TIpHtmlNodeP;
begin
  newPara := TIpHtmlNodeP.Create(AParent);
  newPara.ParseBaseProps(FOwner);
  newPara.Align := ParseAlignment;
  NextToken;
  ParseBodyText(newPara, EndTokens + [IpHtmlTagPend, IpHtmlTagP, IpHtmltagTABLE]);
  if FCurToken = IpHtmlTagPend then
    NextToken
  else
  if FCurToken in (EndTokens + [IpHtmlTagP, IpHtmltagTABLE]) then
  else
    if FlagErrors then
      ReportExpectedToken(IpHtmlTagPend);
end;

procedure TIpHtmlParser.ParsePhraseElement(AParent: TIpHtmlNode;
  StartToken, EndToken: TIpHtmlToken; const EndTokens: TIpHtmlTokenSet);
var
  curPhrase: TIpHtmlNodePhrase;
begin
  curPhrase := TIpHtmlNodePhrase.Create(AParent);
  case StartToken of
    IpHtmlTagEM :
      curPhrase.Style := hpsEM;
    IpHtmlTagSTRONG :
      curPhrase.Style := hpsSTRONG;
    IpHtmlTagDFN :
      curPhrase.Style := hpsDFN;
    IpHtmlTagCODE :
      curPhrase.Style := hpsCODE;
    IpHtmlTagSAMP :
      curPhrase.Style := hpsSAMP;
    IpHtmlTagKBD :
      curPhrase.Style := hpsKBD;
    IpHtmlTagVAR :
      curPhrase.Style := hpsVAR;
    IpHtmlTagCITE :
      curPhrase.Style := hpsCITE;
    IpHtmlTagABBR :
      curPhrase.Style := hpsABBR;
    IpHtmlTagACRONYM :
      curPhrase.Style := hpsACRONYM;
  end;
  curPhrase.ParseBaseProps(FOwner);
  NextToken; // this can not be before previous line, as NextToken resets properties
  ParseBodyText(curPhrase, [EndToken] + EndTokens);
  if FCurToken = EndToken then
    NextToken
  else
  if FCurToken in EndTokens then
    //
  else
    if FlagErrors then
      ReportExpectedToken(EndToken);
end;

function TIpHtmlParser.ParsePixels(const AttrNameSet: TIpHtmlAttributesSet;
  const ADefault: string): TIpHtmlPixels;
var
  S: string;
  n, Err: Integer;
begin
  Result := TIpHtmlPixels.Create;
  S := FindAttribute(AttrNameSet);
  if (S = '') then
    S := ADefault;
  
  if S = '' then
    Result.PixelsType := hpUndefined
  else begin
    Result.PixelsType := hpAbsolute;
    val(S, n, Err);
    Result.Value := n;
    if (Err <> 0) or (Result.Value < 0) then begin
      if FlagErrors then
        ReportError(SHtmlInvInt)
      else
        Result.Value := 0;
    end;
  end;
end;

procedure TIpHtmlParser.ParsePre(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curContainer: TIpHtmlNodePRE;
begin
  curContainer := TIpHtmlNodePRE.Create(AParent);
  curContainer.ParseBaseProps(FOwner);
  
  Inc(FInPre);
  NextToken;
  ParseBodyText(curContainer, EndTokens + [IpHtmlTagPREend]);
  Dec(FInPre);
  
  EnsureClosure(IpHtmlTagPREend, EndTokens);
end;

procedure TIpHtmlParser.ParseQ(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  BQ: TIpHtmlNodeQ;
begin
  BQ:= TIpHtmlNodeQ.Create(AParent);
  BQ.ParseBaseProps(FOwner);
  NextToken;
  ParseBodyText(BQ, EndTokens + [IpHtmlTagQend]);
  EnsureClosure(IpHtmlTagQend, EndTokens);
end;               

function TIpHtmlParser.ParseRelSize: TIpHtmlRelSize;
var
  S: string;
  i, Err: Integer;
begin
  Result := TIpHtmlRelSize.Create;
  Result.SizeType := hrsUnspecified;
  S := FindAttribute(htmlAttrSIZE);
  if Length(S) = 0 then
    Exit; {S := Default;}
  
  Result.Value := 0;
  if (Length(S) > 1) and (S[1] = '+') then begin
    Result.SizeType := hrsRelative;
    Delete(S, 1, 1);
  end else
  if (Length(S) > 1) and (S[1] = '-') then begin
    Result.SizeType := hrsRelative;
  end else
    Result.SizeType := hrsAbsolute;
  
  Val(S, i, Err);
  Result.Value := i;
  if Err <> 0 then
    if FlagErrors then
      ReportError(SHtmlInvInt);
end;

procedure TIpHtmlParser.ParseRight(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curContainer: TIpHtmlNodeDIV;
begin
  curContainer := TIpHtmlNodeDIV.Create(AParent);
  with curContainer do
    Align := haRight;
  NextToken;
  ParseBodyText(curContainer, EndTokens + [IpHtmlTagRIGHTend]);
  EnsureClosure(IpHtmlTagRIGHTend, EndTokens);
end;

function TIpHtmlParser.ParseRules(ADefault: TIpHtmlRules): TIpHtmlRules;
var
  S: string;
begin
  Result := hrNone;
  S := UpperCase(FindAttribute(htmlAttrRULES));
  if Length(S) = 0 then
  begin
    Result := ADefault;
    exit;
  end;
  case S[1] of
    'A': if S = 'ALL' then Result := hrAll;
    'C': if S = 'COLS' then Result := hrCols;
    'G': if S = 'GROUPS' then Result := hrGroups;
    'N': if S = 'NONE' then exit;
    'R': if S = 'ROWS' then Result := hrRows;
    else
      if FlagErrors then
        ReportError(SHtmlInvRule);
  end;
end;

procedure TIpHtmlParser.ParseScript(AParent: TIpHtmlNode;
  const EndTokens: TIpHtmlTokenSet);
begin
  TIpHtmlNodeSCRIPT.Create(AParent);
  NextToken;
  if FCurToken <> IpHtmlTagScriptEnd then
    repeat
      NextToken;
    until (FCurToken = IpHtmlTagSCRIPTend) or (FCurToken in EndTokens);
  EnsureClosure(IpHtmlTagSCRIPTend, EndTokens);
end;

function TIpHtmlParser.ParseShape: TIpHtmlMapShape;
var
  S: string;
begin
  Result := hmsDefault;
  S := UpperCase(FindAttribute(htmlAttrSHAPE));
  if Length(S) = 0 then 
    exit;
  case S[1] of
    'C': if S = 'CIRCLE' then Result := hmsCircle;
    'D': if S = 'DEFAULT' then exit;
    'P': if (S = 'POLY') or (S = 'POLYGON') then Result := hmsPoly;
    'R': if (S = 'RECT') then Result := hmsRect;
    else if FlagErrors then ReportError(SHtmlInvShape);
  end;
end;

procedure TIpHtmlParser.ParseSpan(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curSPAN: TIpHtmlNodeSPAN;
begin
  curSPAN:= TIpHtmlNodeSPAN.Create(AParent);
  with curSPAN do begin
    Align := ParseAlignment;
    ParseBaseProps(FOwner);
  end;
  NextToken;
  ParseBodyText(curSPAN, EndTokens + [IpHtmlTagSPANend]);
  EnsureClosure(IpHtmlTagSPANend, EndTokens);
end;

procedure TIpHtmlParser.ParseStyle(AParent: TIpHtmlNode);
var
  curStyle: TIpHtmlNodeSTYLE;
begin
  curStyle := TIpHtmlNodeSTYLE.Create(AParent);
  with curStyle do begin
    Media := FindAttribute(htmlAttrMEDIA);
    Title := FindAttribute(htmlAttrTITLE);
    Type_ := FindAttribute(htmlAttrTYPE);
  end;
  NextToken;
  if FCurToken <> IpHtmlTagSTYLEend then begin
    if (FCurToken = IpHtmlTagText) and (AnsiCompareText(curStyle.Type_, 'text/css')=0) then
      ParseStyleSheet(curStyle, GetTokenString);
    ParseText(curStyle, [IpHtmlTagSTYLEend]);
  end;
  if FCurToken = IpHtmlTagSTYLEend then
    NextToken
  else
    if FlagErrors then
      ReportExpectedToken(IpHtmlTagSTYLEend);
end;

procedure TIpHtmlParser.ParseStyleSheet(AParent: TIpHtmlNode; HRef: String);
var
  styleStream: TStream;
begin
  //debugln(['TIpHtml.ParseStyleSheet ',href,' ',Parent is TIpHtmlNodeHEAD,' ',DbgSName(FDataProvider)]);
  styleStream := nil;
  
  if AParent is TIpHtmlNodeHEAD then begin
    if FOwner.DataProvider <> nil then begin
      Href := FOwner.DataProvider.BuildURL(FCurURL, HRef);
      styleStream := FOwner.DataProvider.DoGetStream(HRef);
    end;
  end else
  if AParent is TIpHtmlNodeSTYLE then
    styleStream := TStringStream.Create(Href);
    
  if styleStream <> nil then
    with TCSSReader.Create(styleStream, FOwner.CSS) do begin
      ParseCSS;
      Free;
      styleStream.Free;
    end;
end;

procedure TIpHtmlParser.ParseTABLE(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curTable: TIpHtmlNodeTABLE;
  curCaption: TIpHtmlNodeCAPTION;
begin
  curTable := TIpHtmlNodeTABLE.Create(AParent);
  with curTable do begin
    Align := ParseImageAlignment(hiaBottom);
    Width := ParseHyperLength(htmlAttrWIDTH, '');
    Width.OnChange := @WidthChanged;
    Border := ParseInteger(htmlAttrBORDER, 0);
    CellSpacing := ParseInteger(htmlAttrCELLSPACING, 2);
    CellPadding := ParseInteger(htmlAttrCELLPADDING, 2);
    BgColor := ColorFromString(FindAttribute(htmlAttrBGCOLOR));
    ParseBaseProps(FOwner);
    Summary := FindAttribute(htmlAttrSUMMARY);
    Frame := ParseFrameProp(Frame);
    Rules := ParseRules(Rules);
  end;

  repeat
    NextToken;
  until FCurToken in
     [IpHtmlTagCAPTION, IpHtmlTagCOLGROUP, IpHtmlTagTHEAD, IpHtmlTagTFOOT,
      IpHtmlTagTBODY, IpHtmlTagTR, IpHtmlTagTABLEend, IpHtmlTagEOF];

  if FCurToken = IpHtmlTagCAPTION then begin
    curCaption := TIpHtmlNodeCAPTION.Create(CurTable);
    curCaption.Align := ParseVAlignment2;
    curCaption.ParseBaseProps(FOwner);
    ParseBodyText(curCaption, [IpHtmlTagCAPTIONend, IpHtmlTagTABLEend, IpHtmlTagTBODY]);
    if FCurToken in EndTokens then
    else
    if FCurToken = IpHtmlTagCAPTIONend then
      NextToken
    else
      if FlagErrors then
        ReportExpectedToken(IpHtmlTagCAPTIONend)
      else begin
        while not (FCurToken in EndTokens + [IpHtmlTagCAPTIONend]) do
          NextToken;
        if FCurToken = IpHtmlTagCAPTIONend then
          NextToken;
      end;
    curTable.FCaption := curCaption;
  end;
  ParseColGroup(curTable);
  SkipTextTokens;
  ParseTableBody(
    curTable, 
    EndTokens + [IpHtmlTagTABLEend] - [IpHtmlTagTR, IpHtmlTagP, IpHtmlTagPend, IpHTMLTagCENTERend,
       IpHtmlTagLEFTend, IpHtmlTagRIGHTend, IpHtmlTagBLINKend, IpHtmlTagBLOCKQUOTEend]
  );
  SkipTextTokens;
  EnsureClosure(IpHtmlTagTABLEend, EndTokens);
end;

procedure TIpHtmlParser.ParseTableBody(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curHead: TIpHtmlNodeTHEAD;
  curFoot: TIpHtmlNodeTFOOT;
  curBody: TIpHtmlNodeTBODY;
begin
  if FCurToken = IpHtmlTagTHEAD then begin
    curHead := TIpHtmlNodeTHEAD.Create(AParent);
    curHead.ParseBaseProps(FOwner);
    curHead.Align := ParseCellAlign(haLeft);
    curHead.VAlign := ParseVAlignment3;
    NextToken;
    ParseTableRows(
      curHead,
      EndTokens + [IpHtmlTagTFOOT, IpHtmlTagTBODY, IpHtmlTagTHEADend] 
                - [IpHtmlTagTR, IpHtmlTagTH, IpHtmlTagTD]
    );
    if FCurToken = IpHtmlTagTHEADend then
      NextToken;
  end;
  if FCurToken = IpHtmlTagTFOOT then begin
    curFoot := TIpHtmlNodeTFOOT.Create(AParent);
    curFoot.ParseBaseProps(FOwner);
    curFoot.Align := ParseCellAlign(haLeft);
    curFoot.VAlign := ParseVAlignment3;
    NextToken;
    ParseTableRows(
      curFoot,
      EndTokens + [IpHtmlTagTBODY, IpHtmlTagTFOOTend] 
                - [IpHtmlTagTR, IpHtmlTagTH, IpHtmlTagTD]
    );
    if FCurToken = IpHtmlTagTFOOTend then
      NextToken;
  end;
  while not (FCurToken in EndTokens) do begin
    case FCurToken of
      IpHtmlTagTBODY :
        begin
          curBody := TIpHtmlNodeTBODY.Create(AParent);
          curBody.ParseBaseProps(FOwner);
          curBody.Align := ParseCellAlign(haLeft);
          curBody.VAlign := ParseVAlignment3;
          NextToken;
          ParseTableRows(
            curBody,
            EndTokens + [IpHtmlTagTBODYend] 
                      - [IpHtmlTagTR, IpHtmlTagTH, IpHtmlTagTD, IpHtmlTagTRend]
          );
          if FCurToken = IpHtmlTagTBODYend then
            NextToken;
        end;
      IpHtmlTagTR :
        begin
          curBody := TIpHtmlNodeTBODY.Create(AParent);
          ParseTableRows(
            curBody,
            EndTokens - [IpHtmlTagTR, IpHtmlTagTH, IpHtmlTagTD]
          );
        end;
      else
        Exit;
    end;
  end;
end;

procedure TIpHtmlParser.ParseTableRow(AParent: TIpHtmlNode; 
  const EndTokens : TIpHtmlTokenSet);
var
  curHeader: TIpHtmlNodeTH;
  curTableCell: TIpHtmlNodeTD;
begin
  while not (FCurToken in EndTokens) do begin
    case FCurToken of
      IpHtmlTagTH :
        begin
          curHeader := TIpHtmlNodeTH.Create(AParent);
          with curHeader do begin
            NoWrap := ParseBoolean(htmlAttrNOWRAP);
            RowSpan := ParseInteger(htmlAttrROWSPAN, 1);
            ColSpan := ParseInteger(htmlAttrCOLSPAN, 1);
            ParseBaseProps(FOwner);
            Align := ParseCellAlign(haCenter{haDefault});
            VAlign := ParseVAlignment3;
            Width := ParseHyperLength(htmlAttrWIDTH, '');
            Width.OnChange := @DimChanged;
            Height := ParsePixels(htmlAttrHEIGHT, '');
              {ParseInteger(htmlAttrHEIGHT, -1);}
            Height.OnChange := @DimChanged;
            BgColor := ColorFromString(FindAttribute(htmlAttrBGCOLOR));
          end;
          NextToken;
          ParseBodyText(
            curHeader,
            EndTokens + [IpHtmlTagTH, IpHtmlTagTHend, IpHtmlTagTD]
          );
          if FCurToken in [IpHtmlTagTHend, IpHtmlTagTDend] then
            NextRealToken;
        end;
      IpHtmlTagTD :
        begin
          curTableCell := TIpHtmlNodeTD.Create(AParent);
          with curTableCell do begin
            NoWrap := ParseBoolean(htmlAttrNOWRAP);
            RowSpan := ParseInteger(htmlAttrROWSPAN, 1);
            ColSpan := ParseInteger(htmlAttrCOLSPAN, 1);
            ParseBaseProps(FOwner);
            Align := ParseCellAlign(haDefault);
            VAlign := ParseVAlignment3;
            Width := ParseHyperLength(htmlAttrWIDTH, '');
            Width.OnChange := @DimChanged;
            Height := ParsePixels(htmlAttrHEIGHT, '');
              {ParseInteger(htmlAttrHEIGHT, -1);}
            Height.OnChange := @DimChanged;
            BgColor := ColorFromString(FindAttribute(htmlAttrBGCOLOR));
          end;
          NextToken;
          ParseBodyText(curTableCell, EndTokens + [IpHtmlTagTD, IpHtmlTagTDend]);
          if FCurToken = IpHtmlTagTDend then
            NextRealToken;
        end;
      else
        NextToken;
    end;
  end;
end;

procedure TIpHtmlParser.ParseTableRows(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);

  procedure FixupPercentages(CurRow: TIpHtmlNodeTR);
  var
    i, Pt, P0: Integer;
  begin
    Pt := 0;
    P0 := 0;
    for i := 0 to CurRow.ChildCount - 1 do
      if CurRow.ChildNode[i] is TIpHtmlNodeTableHeaderOrCell then
        case TIpHtmlNodeTableHeaderOrCell(CurRow.ChildNode[i]).Width.LengthType of
          hlUndefined :
            Inc(P0);
          hlPercent :
            Inc(Pt, TIpHtmlNodeTableHeaderOrCell(CurRow.ChildNode[i]).Width.LengthValue);
        end;
    if (Pt > 0) and (Pt < 100) and (P0 > 0) then begin
      Pt := (100 - Pt) div P0;
      for i := 0 to CurRow.ChildCount - 1 do
        if CurRow.ChildNode[i] is TIpHtmlNodeTableHeaderOrCell then
          with TIpHtmlNodeTableHeaderOrCell(CurRow.ChildNode[i]).Width do
            if LengthType = hlUndefined then begin
              LengthType := hlPercent;
              LengthValue := Pt;
            end;
    end;
  end;

var
  curRow: TIpHtmlNodeTR;
begin
  curRow := nil;
  while not (FCurToken in EndTokens) do
    case FCurToken of
      IpHtmlTagTR:
        begin
          if curRow <> nil then
            FixupPercentages(curRow);
          curRow := TIpHtmlNodeTR.Create(AParent);
          curRow.ParseBaseProps(FOwner);
          curRow.BgColor := ColorFromString(FindAttribute(htmlAttrBGCOLOR));
          curRow.Align := ParseAlignment;
          curRow.VAlign := ParseVAlignment;
          curRow.LoadAndApplyCSSProps;
          NextRealToken;
          ParseTableRow(
            curRow,
            EndTokens + [IpHtmlTagTRend, IpHtmlTagTR] - [IpHtmlTagTH, IpHtmlTagTD]
          );
          while FCurToken = IpHtmlTagTRend do
            NextToken;
        end;
      IpHtmlTagTH,
      IpHtmlTagTD:
        begin
          if curRow <> nil then
            FixupPercentages(CurRow);
          curRow := TIpHtmlNodeTR.Create(AParent);
          ParseTableRow(
            curRow,
            EndTokens + [IpHtmlTagTR] - [IpHtmlTagTH, IpHtmlTagTD]
          );
        end;
      else
        NextToken;
    end;
  
  if curRow <> nil then
    FixupPercentages(curRow);
end;

procedure TIpHtmlParser.ParseText(AParent: TIpHtmlNode; 
  const EndTokens: TIpHtmlTokenSet);
var
  curContainer: TIpHtmlNodeText;
begin
  while not (FCurToken in EndTokens) do begin
    case FCurToken of
      IpHtmlTagEof :
        Exit;
      IpHtmlTagText :
        begin
          curContainer := TIpHtmlNodeText.Create(AParent);
//          if curContainer=nil then ;
          if curContainer <> nil then
          begin
            curContainer.EscapedText := GetTokenString;
            NextToken;
          end;
        end;
      else
        NextToken;
    end;
  end;
end;

procedure TIpHtmlParser.ParseTitle(AParent: TIpHtmlNode);
var
  B: PAnsiChar;
begin
  FTitleNode := TIpHtmlNodeTITLE.Create(AParent);
  NextToken;
  if FCurToken = IpHtmlTagText then begin
    GetMem(B, Length(GetTokenString) + 1);
    try
      TrimFormatting(EscapeToAnsi(GetTokenString), B);
      FTitleNode.Title := B;
    finally
      FreeMem(B);
    end;
    NextToken;
  end;
  if FCurToken = IpHtmlTagTITLEend then
    NextToken
  else
    if FlagErrors then
      ReportExpectedToken(IpHtmlTagTITLEend);
end;

function TIpHtmlParser.ParseULStyle(ADefault: TIpHtmlULType): TIpHtmlULType;
var
  S: string;
begin
  Result := ADefault;
  S := UpperCase(FindAttribute(htmlAttrTYPE));
  if S <> '' then
    case S[1] of
      'C': if S = 'CIRCLE' then Result := ulCircle;
      'D': if S = 'DISC' then Result := ulDisc;
      'S': if S = 'SQUARE' then Result := ulSquare;
      else
        if FlagErrors then
          ReportError(SHtmlInvType);
    end;
end;

procedure TIpHtmlParser.ParseUnorderedList(AParent: TIpHtmlNode;
  EndToken: TIpHtmlToken; const EndTokens: TIpHtmlTokenSet);
var
  newList: TIpHtmlNodeList;
begin
  case Pred(EndToken) of
    IpHtmlTagDIR: 
      newList := TIpHtmlNodeDIR.Create(AParent);
    IpHtmlTagMENU: 
      newList := TIpHtmlNodeMENU.Create(AParent);
    else 
      {IpHtmlTagUL : }
      newList := TIpHtmlNodeUL.Create(AParent);
  end;
  newList.ParseBaseProps(FOwner); 
  case FListLevel of
    0 : newList.ListType := ParseULStyle(ulDisc);
    1 : newList.ListType := ParseULStyle(ulCircle);
    else
        newList.ListType := ParseULStyle(ulSquare);
  end;
  newList.Compact := ParseBoolean(htmlAttrCOMPACT);
    
  NextToken;
  
  Inc(FListLevel);
  ParseListItems(
    newList,
    EndToken, 
    EndTokens + [EndToken] - [IpHtmlTagP, IpHtmlTagLI]
  );
  Dec(FListLevel);
  
  EnsureClosure(EndToken, EndTokens);
end;

function TIpHtmlParser.ParseVAlignment: TIpHtmlVAlign;
var
  S: string;
begin
  Result := hvaMiddle;
  S := UpperCase(FindAttribute(htmlAttrVALIGN));
  if Length(S) = 0 then 
    exit;
  case S[1] of
    'B': if S = 'BOTTOM' then Result := hvaBottom;
    'C','M': if (S = 'MIDDLE') or (S = 'CENTER') then exit;
    'T': if S = 'TOP' then Result := hvaTop;
    else
      if FlagErrors then
        ReportError(SHtmlInvAlign);
  end;
end;

function TIpHtmlParser.ParseVAlignment2: TIpHtmlVAlignment2;
var
  S: string;
begin
  Result := hva2Top;
  S := UpperCase(FindAttribute(htmlAttrALIGN));
  if Length(S) = 0 then 
    exit;
  case S[1] of
    'B': if S = 'BOTTOM' then Result := hva2Bottom;
    'L': if S = 'LEFT' then Result := hva2Left;
    'R': if S = 'RIGHT' then Result := hva2Right;
    'T': if (S = 'TOP') then exit;
    else
      if FlagErrors then
        ReportError(SHtmlInvAlign);
  end;
end;

function TIpHtmlParser.ParseVAlignment3: TIpHtmlVAlign3;
var
  S : string;
begin
  Result := hva3Middle;
  S := UpperCase(FindAttribute(htmlAttrVALIGN));
  if Length(S) = 0 then
  begin
    Result := hva3Default;
    exit;
  end;
  
  case S[1] of
    'B': 
      if S = 'BOTTOM' then 
        Result := hva3Bottom
      else if S = 'BASELINE' then 
        Result := hva3Baseline;
    'C','M': 
      if (S = 'MIDDLE') or (S = 'CENTER') then 
        exit;
    'T': 
      if (S = 'TOP') then 
        Result := hva3Top;
    else
      if FlagErrors then
        ReportError(SHtmlInvAlign);
  end;
end;

procedure TIpHtmlParser.PutChar(Ch: AnsiChar);
begin
  if (FCharSP >= SizeOf(FCharStack)) then
    raise EIpHtmlException.Create(SHtmlCharStackOverfl);
  FCharStack[FCharSP] := Ch;
  Inc(FCharSP);
end;

procedure TIpHtmlParser.PutToken(AToken: TIpHtmlToken);
begin
  if FHaveToken then
    raise EIpHtmlException.Create(SHtmlTokenStackOverfl);
  FTokenBuffer := AToken;
  FHaveToken := True;
end;

procedure TIpHtmlParser.ReportError(const AErrorMsg: string);
begin
  raise Exception.CreateFmt(SHtmlLineError, [AErrorMsg, FLineNumber, FLineOffset]);
end;

procedure TIpHtmlParser.ReportExpectedError(const AErrorMsg: string);
begin
  ReportError(AErrorMsg + SHtmlExp);
end;

procedure TIpHtmlParser.ReportExpectedToken(const AToken: TIpHtmlToken);
var
  n: integer;
begin
  for n := Low(IpHtmlTokens) to High(IpHtmlTokens) do
    if IpHtmlTokens[n].tk = AToken then
    begin
      ReportExpectedError(IpHtmlTokens[n].pc);
      break;
    end;
end;

procedure TIpHtmlParser.SkipTextTokens;
begin
  while FCurToken = IpHtmlTagText do
    NextToken;
end;

end.