File: cssmatcher.c

package info (click to toggle)
libgtkhtml2 2.11.0-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,740 kB
  • ctags: 3,830
  • sloc: ansic: 33,179; sh: 8,414; makefile: 597
file content (2848 lines) | stat: -rw-r--r-- 79,004 bytes parent folder | download
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
   Copyright (C) 2000 CodeFactory AB
   Copyright (C) 2000 Jonas Borgstr\366m <jonas@codefactory.se>
   Copyright (C) 2000 Anders Carlsson <andersca@codefactory.se>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>

#include "libgtkhtml/dom/core/dom-element.h"
#include "libgtkhtml/document/htmldocument.h"
#include "libgtkhtml/layout/htmlstyle.h"
#include "libgtkhtml/util/htmlstream.h"
#include "libgtkhtml/util/htmlglobalatoms.h"

#include "cssdebug.h"
#include "cssmatcher.h"
#include "cssparser.h"
#include "html.css.h"

static CssStylesheet *default_stylesheet = NULL;

typedef struct _CssDeclarationListEntry CssDeclarationListEntry;

struct _CssDeclarationListEntry {
	gint spec;
	gint type;
	CssDeclaration *decl;
};

enum {
	CSS_STYLESHEET_DEFAULT,
	CSS_STYLESHEET_USER,
	CSS_STYLESHEET_AUTHOR,
	CSS_STYLESHEET_STYLEDECL
};

enum {
	CSS_STYLESHEET_PSEUDO_NONE = 0,
	CSS_STYLESHEET_PSEUDO_HOVER = (1 << 0),
	CSS_STYLESHEET_PSEUDO_ACTIVE = (1 << 1),
	CSS_STYLESHEET_PSEUDO_FOCUS = (1 << 2),
	CSS_STYLESHEET_PSEUDO_BEFORE = (1 << 3),
	CSS_STYLESHEET_PSEUDO_AFTER = (1 << 4)
};

gint current_pseudos, total_pseudos;

static gboolean
css_matcher_match_simple_selector (CssSimpleSelector *simple, xmlNode *node, HtmlAtom *pseudo)
{
	gchar *str, *href;
	gint element_name = -1, element_id = -1; 
	gint i;
	gboolean matched;
	
	/* Don't try to match NULL nodes and nodes that aren't elements */
	if (!node || node->type != XML_ELEMENT_NODE)
		return FALSE;

	element_name = html_atom_list_get_atom (html_atom_list, node->name);
	
	/* Look at the element name */
	if (!simple->is_star && simple->element_name != element_name)
		return FALSE;

	str = xmlGetProp (node, "id");
	if (str) {
		element_id = html_atom_list_get_atom (html_atom_list, str);
		xmlFree (str);
	}

	for (i = 0;i < simple->n_tail; i++) {
		CssTail *tail = &simple->tail[i];

		if (tail->type == CSS_TAIL_ID_SEL &&
		    tail->t.id_sel.id != element_id)
			return FALSE;
		else if (tail->type == CSS_TAIL_CLASS_SEL) {
			gchar *element_class = xmlGetProp (node, "class");

			matched = FALSE;
			
			if (element_class) {
				const gchar *beg, *end, *next, *matchstr;
				gint matchlen;
				
				matchstr = html_atom_list_get_string (html_atom_list, tail->t.class_sel.class);
				matchlen = strlen (matchstr);

				for (beg = element_class; beg != NULL; beg = next) {
					end = strchr (beg, ' ');
					if (end == NULL) {
						end = beg + strlen (beg);
						next = NULL;
					}
					else
						next = end + 1;
					if (matchlen == end - beg && strncasecmp (matchstr, beg, matchlen) == 0)
						matched = TRUE;
				}

				xmlFree (element_class);
			}

			if (!matched) {
				return FALSE; 
			}
		}
		else if (tail->type == CSS_TAIL_ATTR_SEL) {
			gchar *str;

			str = xmlGetProp (node, html_atom_list_get_string (html_atom_list, tail->t.attr_sel.att));

			if (!str) {
				return FALSE;
			}

			/*
			 * We don't test for CSS_MATCH_EMPTY, since we've already returned false if the property
			 * doesn't exist.
			 */

			if (tail->t.attr_sel.match == CSS_MATCH_EQ) {
				gchar *val = NULL;

				if (tail->t.attr_sel.val.type == CSS_ATTR_VAL_IDENT) {
					val = html_atom_list_get_string (html_atom_list, tail->t.attr_sel.val.a.id);
				}
				else if (tail->t.attr_sel.val.type == CSS_ATTR_VAL_STRING) {
					val = tail->t.attr_sel.val.a.str;
				}

				if (strcasecmp (val, str) != 0) {
					xmlFree (str);
					return FALSE;
				}
			}
			else if (tail->t.attr_sel.match == CSS_MATCH_INCLUDES) {

				const gchar *beg, *end, *next, *matchstr = NULL;
				gint matchlen;

				matched = FALSE;

				if (tail->t.attr_sel.val.type == CSS_ATTR_VAL_IDENT) {
					matchstr = html_atom_list_get_string (html_atom_list, tail->t.attr_sel.val.a.id);
				}
				else if (tail->t.attr_sel.val.type == CSS_ATTR_VAL_STRING) {
					matchstr = tail->t.attr_sel.val.a.str;
				}

				matchlen = strlen (matchstr);

				for (beg = str; beg != NULL; beg = next) {
					end = strchr (beg, ' ');
					if (end == NULL) {
						end = beg + strlen (beg);
						next = NULL;
					}
					else
						next = end + 1;
					if (matchlen == end - beg && memcmp (matchstr, beg, matchlen) == 0)
						matched = TRUE;
				}
				
				if (!matched) {
					xmlFree (str);
					return FALSE; 
				}
			}
			else if (tail->t.attr_sel.match == CSS_MATCH_DASHMATCH) {
				const gchar *end, *matchstr = NULL;
				gint matchlen;


				if (tail->t.attr_sel.val.type == CSS_ATTR_VAL_IDENT) {
					matchstr = html_atom_list_get_string (html_atom_list, tail->t.attr_sel.val.a.id);
				}
				else if (tail->t.attr_sel.val.type == CSS_ATTR_VAL_STRING) {
					matchstr = tail->t.attr_sel.val.a.str;
				}
				matchlen = strlen (matchstr);

				end = strchr (str, '-');
				if ((end == NULL && strlen (str) != matchlen) ||
				    end - str != matchlen) {
					xmlFree (str);
					return FALSE;
				}
				if (memcmp (matchstr, str, matchlen) != 0) {
					xmlFree (str);
					return FALSE;
				}
			}
			xmlFree (str);
		}
		else if (tail->type == CSS_TAIL_PSEUDO_SEL) {
			switch (tail->t.pseudo_sel.name) {
			case HTML_ATOM_LINK:
				
				break;
				
				/*  This should only match for links that are unvisited, but
				    we don't have any mekanism for knowing this so we
				    match all links / jb */
				if ((href = xmlGetProp (node, "href")))
					xmlFree (href);
				else
					return FALSE;
				break;

			case HTML_ATOM_HOVER:
				current_pseudos |= CSS_STYLESHEET_PSEUDO_HOVER;
				break;

			case HTML_ATOM_ACTIVE:
				current_pseudos |= CSS_STYLESHEET_PSEUDO_ACTIVE;
				break;

			case HTML_ATOM_FOCUS:
				current_pseudos |= CSS_STYLESHEET_PSEUDO_FOCUS;
				break;

			case HTML_ATOM_BEFORE:
				current_pseudos |= CSS_STYLESHEET_PSEUDO_BEFORE;
				break;

			case HTML_ATOM_AFTER:
				current_pseudos |= CSS_STYLESHEET_PSEUDO_AFTER;
				break;
				
			case HTML_ATOM_FIRST_CHILD:
				while (node->prev && node->prev->type != XML_ELEMENT_NODE)
					node = node->prev;
				
				if (node->prev != NULL)
					return FALSE;
				break;
				
			default: 
				matched = FALSE;
				
				if (!pseudo)
					return FALSE;
				
				for (i = 0; pseudo[i]; i++) {
					if (tail->t.pseudo_sel.name == pseudo[i])
						matched = TRUE;
				}
				
				if (!matched)
					return FALSE;
			}
		}
	}

	/* Since we haven't returned FALSE anywhere we must have a selector match */
	return TRUE;
}

static gboolean
css_matcher_match_selector (CssSelector *sel, xmlNode *node, HtmlAtom *pseudo)
{
	CssSimpleSelector *simple = sel->simple [sel->n_simple - 1];
	CssCombinator comb;
	gboolean matched;
	gint i;

	current_pseudos = CSS_STYLESHEET_PSEUDO_NONE;

	if (!css_matcher_match_simple_selector (simple, node, pseudo))
		return FALSE;

	for (i = sel->n_simple - 1; i > 0; i --) {
		simple = sel->simple [i - 1];
		comb = sel->comb [i - 1];

		/* Child selector */
		if (comb == CSS_COMBINATOR_GT) {
			node = node->parent;

			if (!css_matcher_match_simple_selector (simple, node, pseudo))
				return FALSE;
		}
		/* Adjacent sibling selector */
		else if (comb == CSS_COMBINATOR_PLUS) {

			node = node->prev;
			
			while (node && node->type != XML_ELEMENT_NODE)
				node = node->prev;

			if (!css_matcher_match_simple_selector (simple, node, pseudo))
				return FALSE;
		}
		/* Descendant selector */
		else if (comb == CSS_COMBINATOR_EMPTY) {
			matched = FALSE;

			while (node) {

				node = node->parent;
				

				if (css_matcher_match_simple_selector (simple, node, pseudo)) {
					matched = TRUE;

					while (node->parent && css_matcher_match_simple_selector (simple, node->parent, pseudo) &&
					       !simple->is_star)
						node = node->parent;
					break;
				}
				
			}
			if (!matched)
				return FALSE;
			
		}
		else if (comb == CSS_COMBINATOR_TILDE) {
			matched = FALSE;

			while (node) {
				node = node->prev;

				if (css_matcher_match_simple_selector (simple, node, pseudo)) {
					matched = TRUE;

					while (node->prev && css_matcher_match_simple_selector (simple, node->prev, pseudo))
						node = node->prev;
					break;
				}
			}

			if (!matched)
				return FALSE;
		}
	}

	total_pseudos |= current_pseudos;
	
	/* FIXME: This is inefficient */
	if (current_pseudos & CSS_STYLESHEET_PSEUDO_HOVER) {
		matched = FALSE;
		for (i = 0; pseudo && pseudo[i]; i++) {
			if (pseudo[i] == HTML_ATOM_HOVER)
				matched = TRUE;
		}

		if (!matched)
			return FALSE;
	}

	/* FIXME: This is inefficient */
	if (current_pseudos & CSS_STYLESHEET_PSEUDO_ACTIVE) {
		matched = FALSE;
		for (i = 0; pseudo && pseudo[i]; i++) {
			if (pseudo[i] == HTML_ATOM_ACTIVE)
				matched = TRUE;
		}

		if (!matched)
			return FALSE;
	}

	/* FIXME: This is inefficient */
	if (current_pseudos & CSS_STYLESHEET_PSEUDO_FOCUS) {
		matched = FALSE;
		for (i = 0; pseudo && pseudo[i]; i++) {
			if (pseudo[i] == HTML_ATOM_FOCUS)
				matched = TRUE;
		}

		if (!matched)
			return FALSE;
	}

	
	/* FIXME: This is inefficient */
	if (current_pseudos & CSS_STYLESHEET_PSEUDO_BEFORE) {
		matched = FALSE;
		for (i = 0; pseudo && pseudo[i]; i++) {
			if (pseudo[i] == HTML_ATOM_BEFORE)
				matched = TRUE;
		}

		if (!matched)
			return FALSE;
	}


	/* FIXME: This is inefficient */
	if (current_pseudos & CSS_STYLESHEET_PSEUDO_AFTER) {
		matched = FALSE;
		for (i = 0; pseudo && pseudo[i]; i++) {
			if (pseudo[i] == HTML_ATOM_AFTER)
				matched = TRUE;
		}

		if (!matched)
			return FALSE;
	}

	return TRUE;
}

static gint
css_declaration_list_sorter (gconstpointer p1, gconstpointer p2)
{
	CssDeclarationListEntry *entry1 = (CssDeclarationListEntry *)p1;
	CssDeclarationListEntry *entry2 = (CssDeclarationListEntry *)p2;

	if (entry1->type > entry2->type)
		return 1;
	else if (entry1->type < entry2->type)
		return -1;
	else {
		if (entry1->decl->important && !entry2->decl->important)
			return 1;
		else if (!entry1->decl->important && entry2->decl->important)
			return -1;
		else {
	  		if (entry1->spec > entry2->spec)
				return 1;
			else if (entry1->spec < entry2->spec)
				return -1;
			else
				return 1;
		}
	}
}

static gboolean
css_parse_border_width (HtmlFontSpecification *old_font, CssValue *val, gint *width)
{
	HtmlLength length;
	
	if (val->value_type == CSS_IDENT) {
		switch (val->v.atom) {
		case HTML_ATOM_THIN:
			*width = HTML_BORDER_WIDTH_THIN;
			break;
		case HTML_ATOM_MEDIUM:
			*width = HTML_BORDER_WIDTH_MEDIUM;
			break;
		case HTML_ATOM_THICK:
			*width = HTML_BORDER_WIDTH_THICK;
			break;
		default:
			return FALSE;
		}

		return TRUE;
	}
	else if (html_length_from_css_value (old_font, val, &length)) {
		*width = html_length_get_value (&length, 0);
		return TRUE;
	}
	
	return FALSE;
}

static gboolean
css_parse_color (CssValue *val, HtmlColor *color)
{
	HtmlColor *tmp_color = NULL;
	gchar *str = css_value_to_string (val);

	if (str) {
		tmp_color = html_color_new_from_name (str);
		g_free (str);
	}

	if (!tmp_color)
		return FALSE;
	
	if (color) {
		color->refcount = tmp_color->refcount;
		color->red = tmp_color->red;
		color->green = tmp_color->green;
		color->blue = tmp_color->blue;
		color->transparent = tmp_color->transparent;
	}

	html_color_destroy (tmp_color);

	return TRUE;
}

static gboolean
css_parse_border_style (CssValue *val, HtmlBorderStyleType *style)
{
	switch (val->v.atom) {
	case HTML_ATOM_HIDDEN:
		*style = HTML_BORDER_STYLE_HIDDEN;
		return TRUE;
	case HTML_ATOM_DOTTED:
		*style = HTML_BORDER_STYLE_DOTTED;
		return TRUE;
	case HTML_ATOM_DASHED:
		*style = HTML_BORDER_STYLE_DASHED;
		return TRUE;
	case HTML_ATOM_SOLID:
		*style = HTML_BORDER_STYLE_SOLID;
		return TRUE;
	case HTML_ATOM_DOUBLE:
		*style = HTML_BORDER_STYLE_DOUBLE;
		return TRUE;
	case HTML_ATOM_GROOVE:
		*style = HTML_BORDER_STYLE_GROOVE;
		return TRUE;
	case HTML_ATOM_RIDGE:
		*style = HTML_BORDER_STYLE_RIDGE;
		return TRUE;
	case HTML_ATOM_INSET:
		*style = HTML_BORDER_STYLE_INSET;
		return TRUE;
	case HTML_ATOM_OUTSET:
		*style = HTML_BORDER_STYLE_OUTSET;
		return TRUE;
	default:
		return FALSE;
	}
}

static gint
length_to_pixels (const CssValue *val)
{
	if (val->value_type == CSS_PX)
		return val->v.d;
	return 0;
}

static gboolean
handle_background_repeat (HtmlDocument *document, HtmlStyle *style, HtmlStyle *parent_style, CssValue *val)
{
	switch (val->v.atom) {
	case HTML_ATOM_INHERIT:
		html_style_set_background_repeat (style, parent_style->background->repeat);
		break;
	case HTML_ATOM_REPEAT:
		html_style_set_background_repeat (style, HTML_BACKGROUND_REPEAT_REPEAT);
		break;
	case HTML_ATOM_REPEAT_X:
		html_style_set_background_repeat (style, HTML_BACKGROUND_REPEAT_REPEAT_X);
		break;
	case HTML_ATOM_REPEAT_Y:
		html_style_set_background_repeat (style, HTML_BACKGROUND_REPEAT_REPEAT_Y);
		break;
	case HTML_ATOM_NO_REPEAT:
		html_style_set_background_repeat (style, HTML_BACKGROUND_REPEAT_NO_REPEAT);
		break;
	case HTML_ATOM_SCALE:
		html_style_set_background_repeat (style, HTML_BACKGROUND_REPEAT_SCALE);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}

static gboolean
handle_background_image (HtmlDocument *document, HtmlStyle *style, CssValue *val)
{
	if (val->value_type == CSS_FUNCTION &&
	    val->v.function->name == HTML_ATOM_URL &&
	    val->v.function->args) {
		
		HtmlImage *image = NULL;
		gchar *str = css_value_to_string (val->v.function->args);

		if (str) {
			image = html_image_factory_get_image (document->image_factory, val->v.function->args->v.s);
			g_free (str);
		}
		if (image) {
			html_style_set_background_image (style, image);
			g_object_unref (G_OBJECT(image));
			return TRUE;
		}
	} 
	return FALSE;
}

void
css_matcher_apply_rule (HtmlDocument *document, HtmlStyle *style, HtmlStyle *parent_style, HtmlFontSpecification *old_font, CssDeclaration *decl)
{
	gint prop = decl->property;
	CssValue *val = decl->expr;
	HtmlBorderStyleType border_style;
	gint border_width;
	HtmlColor color;
	
	if (val->v.atom == HTML_ATOM_INHERIT && !parent_style)
		return;

	/* FIXME: once (if) we have a static atom table we'll change this to switch instead */

	switch (prop) {
	case HTML_ATOM_DISPLAY:
		/* FIXME: use getter functions for this, so we can check against type */

		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			style->display = parent_style->display;
			break;
		case HTML_ATOM_BLOCK:
			style->display = HTML_DISPLAY_BLOCK;
			break;
		case HTML_ATOM_INLINE:
			style->display = HTML_DISPLAY_INLINE;
			break;
		case HTML_ATOM_NONE:
			style->display = HTML_DISPLAY_NONE;
			break;
		case HTML_ATOM_TABLE:
			style->display = HTML_DISPLAY_TABLE;
			break;
		case HTML_ATOM_INLINE_TABLE:
			style->display = HTML_DISPLAY_INLINE_TABLE;
			break;
		case HTML_ATOM_TABLE_ROW:
			style->display = HTML_DISPLAY_TABLE_ROW;
			break;
		case HTML_ATOM_TABLE_CELL:
			style->display = HTML_DISPLAY_TABLE_CELL;
			break;
		case HTML_ATOM_TABLE_CAPTION:
			style->display = HTML_DISPLAY_TABLE_CAPTION;
			break;
		case HTML_ATOM_TABLE_HEADER_GROUP:
			style->display = HTML_DISPLAY_TABLE_HEADER_GROUP;
			break;
		case HTML_ATOM_TABLE_ROW_GROUP:
			style->display = HTML_DISPLAY_TABLE_ROW_GROUP;
			break;
		case HTML_ATOM_TABLE_FOOTER_GROUP:
			style->display = HTML_DISPLAY_TABLE_FOOTER_GROUP;
			break;
		case HTML_ATOM_LIST_ITEM:
			style->display = HTML_DISPLAY_LIST_ITEM;
			break;
		}
		break;

	case HTML_ATOM_CONTENT:

		if (val->v.atom == HTML_ATOM_INHERIT) {
			if (parent_style->content)
				style->content = g_strdup (parent_style->content);
			break;
		}
		else if (val->value_type == CSS_STRING) {
			style->content = g_strdup (val->v.s);
		}
		break;

	case HTML_ATOM_FLOAT:

		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			style->Float = parent_style->Float;
			break;
		case HTML_ATOM_NONE:
			style->Float = HTML_FLOAT_NONE;
			break;
		case HTML_ATOM_LEFT:
			style->Float = HTML_FLOAT_LEFT;
			break;
		case HTML_ATOM_RIGHT:
			style->Float = HTML_FLOAT_RIGHT;
			break;
		case HTML_ATOM_CENTER:
			style->Float = HTML_FLOAT_CENTER;
			break;
		}
		break;

	case HTML_ATOM_TABLE_LAYOUT:
		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			style->table_layout = parent_style->table_layout;
			break;
		case HTML_ATOM_AUTO:
			style->table_layout = HTML_TABLE_LAYOUT_AUTO;
			break;
		case HTML_ATOM_FIXED:
			style->table_layout = HTML_TABLE_LAYOUT_FIXED;
			break;
		}
		break;

	case HTML_ATOM_BACKGROUND_REPEAT:
		handle_background_repeat (document, style, parent_style, val);
		break;

	case HTML_ATOM_LIST_STYLE_TYPE:
		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			html_style_set_list_style_type (style, parent_style->inherited->list_style_type);
			break;
		case HTML_ATOM_DISC:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_DISC);
			break;
		case HTML_ATOM_CIRCLE:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_CIRCLE);
			break;
		case HTML_ATOM_SQUARE:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_SQUARE);
			break;
		case HTML_ATOM_DECIMAL:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_DECIMAL);
			break;
		case HTML_ATOM_DECIMAL_LEADING_ZERO:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_DECIMAL_LEADING_ZERO);
			break;
		case HTML_ATOM_LOWER_ROMAN:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_LOWER_ROMAN);
			break;
		case HTML_ATOM_UPPER_ROMAN:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_UPPER_ROMAN);
			break;
		case HTML_ATOM_LOWER_GREEK:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_LOWER_GREEK);
			break;
		case HTML_ATOM_LOWER_ALPHA:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_LOWER_ALPHA);
			break;
		case HTML_ATOM_LOWER_LATIN:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_LOWER_LATIN);
			break;
		case HTML_ATOM_UPPER_ALPHA:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_UPPER_ALPHA);
			break;
		case HTML_ATOM_UPPER_LATIN:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_UPPER_LATIN);
			break;
		case HTML_ATOM_HEBREW:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_HEBREW);
			break;
		case HTML_ATOM_ARMENIAN:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_ARMENIAN);
			break;
		case HTML_ATOM_GEORGIAN:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_GEORGIAN);
			break;
		case HTML_ATOM_CJK_IDEOGRAPHIC:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_CJK_IDEOGRAPHIC);
			break;
		case HTML_ATOM_HIRAGANA:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_HIRAGANA);
			break;
		case HTML_ATOM_KATAKANA:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_KATAKANA);
			break;
		case HTML_ATOM_HIRAGANA_IROHA:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_HIRAGANA_IROHA);
			break;
		case HTML_ATOM_KATAKANA_IROHA:
			html_style_set_list_style_type (style, HTML_LIST_STYLE_TYPE_KATAKANA_IROHA);
			break;
		}
		break;

	case HTML_ATOM_CLEAR:
		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			style->clear = parent_style->clear;
			break;
		case HTML_ATOM_NONE:
			style->clear = HTML_CLEAR_NONE;
			break;
		case HTML_ATOM_LEFT:
			style->clear = HTML_CLEAR_LEFT;
			break;
		case HTML_ATOM_RIGHT:
			style->clear = HTML_CLEAR_RIGHT;
			break;
		case HTML_ATOM_BOTH:
			style->clear = HTML_CLEAR_BOTH;
			break;
		}
		break;

	case HTML_ATOM_POSITION:
		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			style->position = parent_style->position;
			break;
		case HTML_ATOM_FIXED:
			style->position = HTML_POSITION_FIXED;
			break;
		case HTML_ATOM_STATIC:
			style->position = HTML_POSITION_STATIC;
			break;
		case HTML_ATOM_ABSOLUTE:
			style->position = HTML_POSITION_ABSOLUTE;
			break;
		case HTML_ATOM_RELATIVE:
			style->position = HTML_POSITION_RELATIVE;
			break;
		}
		break;

	case HTML_ATOM_TOP: {
		HtmlLength length;
		
		if (html_length_from_css_value (old_font, val, &length))
			html_style_set_position_top (style, &length);
		break;
	}

	case HTML_ATOM_RIGHT: {
		HtmlLength length;
		
		if (html_length_from_css_value (old_font, val, &length))
			html_style_set_position_right (style, &length);
		break;
	}

	case HTML_ATOM_BOTTOM: {
		HtmlLength length;
		
		if (html_length_from_css_value (old_font, val, &length))
			html_style_set_position_bottom (style, &length);
		break;
	}

	case HTML_ATOM_LEFT: {
		HtmlLength length;
		
		if (html_length_from_css_value (old_font, val, &length))
			html_style_set_position_left (style, &length);
		break;
	}

	case HTML_ATOM_CAPTION_SIDE: {
		HtmlCaptionSideType type = HTML_CAPTION_SIDE_TOP;
		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			type = parent_style->inherited->caption_side;
			break;
		case HTML_ATOM_TOP:
			type = HTML_CAPTION_SIDE_TOP;
			break;
		case HTML_ATOM_RIGHT:
			type = HTML_CAPTION_SIDE_RIGHT;
			break;
		case HTML_ATOM_BOTTOM:
			type = HTML_CAPTION_SIDE_BOTTOM;
			break;
		case HTML_ATOM_LEFT:
			type = HTML_CAPTION_SIDE_LEFT;
			break;
		}
		html_style_set_caption_side (style, type);
		break;
	}

	case HTML_ATOM_VISIBILITY:

		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			style->visibility = parent_style->visibility;
			break;
		case HTML_ATOM_VISIBLE:
			style->visibility = HTML_VISIBILITY_VISIBLE;
			break;
		case HTML_ATOM_HIDDEN:
			style->visibility = HTML_VISIBILITY_HIDDEN;
			break;
		case HTML_ATOM_COLLAPSE:
			style->visibility = HTML_VISIBILITY_COLLAPSE;
			break;
		}
		break;

	case HTML_ATOM_WHITE_SPACE:

		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			html_style_set_white_space (style, parent_style->inherited->white_space);
			break;
		case HTML_ATOM_NORMAL:
			html_style_set_white_space (style, HTML_WHITE_SPACE_NORMAL);
			break;
		case HTML_ATOM_PRE:
			html_style_set_white_space (style, HTML_WHITE_SPACE_PRE);
			break;
		case HTML_ATOM_NOWRAP:
			html_style_set_white_space (style, HTML_WHITE_SPACE_NOWRAP);
			break;
		}
		break;

	case HTML_ATOM_BORDER_SPACING: { /* FIXME: implement inherit */
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;
			gint horiz, vert;
			
			/* See how many items we have */
			switch (css_value_list_get_length (val)) {
			case 2:
				horiz = length_to_pixels (entry->value);
				entry = entry->next;
				vert = length_to_pixels (entry->value);
				html_style_set_border_spacing (style, horiz, vert);
				break;
			default:
				/* Do nothing */
				break;
			}
		}
		else {
			gint spacing = length_to_pixels (val);
			html_style_set_border_spacing (style, spacing, spacing);
		}
		break;
	}
	
	case HTML_ATOM_WIDTH: {
		HtmlLength width;
		
		if (val->v.atom == HTML_ATOM_INHERIT) {
			html_length_set_value (&width, 100, HTML_LENGTH_PERCENT);
			html_style_set_width (style, &width);
		}
		else if (html_length_from_css_value (old_font, val, &width))
			html_style_set_width (style, &width);

		break;
	}

	case HTML_ATOM_HEIGHT: {
		HtmlLength height;

		if (val->v.atom == HTML_ATOM_INHERIT) {
			html_length_set_value (&height, 100, HTML_LENGTH_PERCENT);
			html_style_set_height (style, &height);
		}
		else if (html_length_from_css_value (old_font, val, &height))
			html_style_set_height (style, &height);

		break;
	}

	case HTML_ATOM_MIN_WIDTH: {
		HtmlLength min_width;

		if (val->v.atom == HTML_ATOM_INHERIT)
			html_style_set_min_width (style, &parent_style->box->min_width);
		else if (html_length_from_css_value (old_font, val, &min_width))
			html_style_set_min_width (style, &min_width);
		break;
	}

	case HTML_ATOM_MAX_WIDTH: {
		HtmlLength max_width;

		if (val->v.atom == HTML_ATOM_INHERIT)
			html_style_set_max_width (style, &parent_style->box->max_width);
		else if (html_length_from_css_value (old_font, val, &max_width))
			html_style_set_max_width (style, &max_width);
		break;
	}

	case HTML_ATOM_MAX_HEIGHT: {
		HtmlLength max_height;

		if (val->v.atom == HTML_ATOM_INHERIT)
			html_style_set_max_height (style, &parent_style->box->max_height);
		if (html_length_from_css_value (old_font, val, &max_height))
			html_style_set_max_height (style, &max_height);
		break;
	}

	case HTML_ATOM_MIN_HEIGHT: {
		HtmlLength min_height;

		if (val->v.atom == HTML_ATOM_INHERIT)
			html_style_set_min_height (style, &parent_style->box->min_height);
		if (html_length_from_css_value (old_font, val, &min_height))
			html_style_set_min_height (style, &min_height);
		break;
	}

	case HTML_ATOM_COLOR:
		if (val->v.atom == HTML_ATOM_INHERIT)
			html_style_set_color (style, parent_style->inherited->color);
		else if (css_parse_color (val, &color)) {
			html_style_set_color (style, &color);
		}
		break;
	
	case HTML_ATOM_MARGIN: {
		if (val->value_type == CSS_VALUE_LIST) {
			HtmlLength margin;
			CssValueEntry *entry = val->v.entry;

			/* First, check that all specified values are correct */
			while (entry) {
				if (!html_length_from_css_value (old_font, entry->value, &margin))
					return;

				entry = entry->next;
			}

			entry = val->v.entry;
			
			/* See how many items we have */
			switch (css_value_list_get_length (val)) {
			case 2:
				html_length_from_css_value (old_font, entry->value, &margin);
				html_style_set_margin_top (style, &margin);
				html_style_set_margin_bottom (style, &margin);
				entry = entry->next;
				
				html_length_from_css_value (old_font, entry->value, &margin);
				html_style_set_margin_left (style, &margin);
				html_style_set_margin_right (style, &margin);
				break;
			case 3:
				html_length_from_css_value (old_font, entry->value, &margin);
				html_style_set_margin_top (style, &margin);
				entry = entry->next;
				
				html_length_from_css_value (old_font, entry->value, &margin);
				html_style_set_margin_right (style, &margin);
				html_style_set_margin_left (style, &margin);
				entry = entry->next;
				
				html_length_from_css_value (old_font, entry->value, &margin);
				html_style_set_margin_bottom (style, &margin);
				break;
			case 4:
				html_length_from_css_value (old_font, entry->value, &margin);
				html_style_set_margin_top (style, &margin);
				entry = entry->next;

				html_length_from_css_value (old_font, entry->value, &margin);
				html_style_set_margin_right (style, &margin);
				entry = entry->next;

				html_length_from_css_value (old_font, entry->value, &margin);
				html_style_set_margin_bottom (style, &margin);
				entry = entry->next;

				html_length_from_css_value (old_font, entry->value, &margin);
				html_style_set_margin_left (style, &margin);
				break;
			default:
				/* Do nothing */
				break;
			}
		}
		else {
			HtmlLength margin;

			/* Only one item, so we'll set margins entirely */
			if (html_length_from_css_value (old_font, val, &margin)) {
				
				html_style_set_margin_left (style, &margin);
				html_style_set_margin_right (style, &margin);
				html_style_set_margin_top (style, &margin);
				html_style_set_margin_bottom (style, &margin);
			}
		}
		
		break;
	}
	
	case HTML_ATOM_MARGIN_LEFT: {
		HtmlLength margin_left;

		if (html_length_from_css_value (old_font, val, &margin_left))
			html_style_set_margin_left (style, &margin_left);

		break;
	}
	case HTML_ATOM_MARGIN_RIGHT: {
		HtmlLength margin_right;

		if (html_length_from_css_value (old_font, val, &margin_right))
			html_style_set_margin_right (style, &margin_right);

		break;
	}
	
	case HTML_ATOM_MARGIN_TOP: {
		HtmlLength margin_top;

		if (html_length_from_css_value (old_font, val, &margin_top))
			html_style_set_margin_top (style, &margin_top);

		break;
	}
	
	case HTML_ATOM_MARGIN_BOTTOM: {
		HtmlLength margin_bottom;

		if (html_length_from_css_value (old_font, val, &margin_bottom))
			html_style_set_margin_bottom (style, &margin_bottom);

		break;
	}

	case HTML_ATOM_PADDING: {
		if (val->value_type == CSS_VALUE_LIST) {
			HtmlLength padding;
			CssValueEntry *entry = val->v.entry;

			/* First, check that all specified values are correct */
			while (entry) {
				if (!html_length_from_css_value (old_font, entry->value, &padding))
					return;

				entry = entry->next;
			}

			entry = val->v.entry;
			
			/* See how many items we have */
			switch (css_value_list_get_length (val)) {
			case 2:
				html_length_from_css_value (old_font, entry->value, &padding);
				html_style_set_padding_top (style, &padding);
				html_style_set_padding_bottom (style, &padding);
				entry = entry->next;
				
				html_length_from_css_value (old_font, entry->value, &padding);
				html_style_set_padding_left (style, &padding);
				html_style_set_padding_right (style, &padding);
				break;
			case 3:
				html_length_from_css_value (old_font, entry->value, &padding);
				html_style_set_padding_top (style, &padding);
				entry = entry->next;
				
				html_length_from_css_value (old_font, entry->value, &padding);
				html_style_set_padding_right (style, &padding);
				html_style_set_padding_left (style, &padding);
				entry = entry->next;
				
				html_length_from_css_value (old_font, entry->value, &padding);
				html_style_set_padding_bottom (style, &padding);
				
				break;
			case 4:
				html_length_from_css_value (old_font, entry->value, &padding);
				html_style_set_padding_top (style, &padding);
				entry = entry->next;

				html_length_from_css_value (old_font, entry->value, &padding);
				html_style_set_padding_right (style, &padding);
				entry = entry->next;

				html_length_from_css_value (old_font, entry->value, &padding);
				html_style_set_padding_bottom (style, &padding);
				entry = entry->next;

				html_length_from_css_value (old_font, entry->value, &padding);
				html_style_set_padding_left (style, &padding);
				break;
			default:
				/* Do nothing */
				break;
			}
		}
		else {
			HtmlLength padding;

			/* Only one item, so we'll set paddings entirely */
			if (html_length_from_css_value (old_font, val, &padding)) {
				
				html_style_set_padding_left (style, &padding);
				html_style_set_padding_right (style, &padding);
				html_style_set_padding_top (style, &padding);
				html_style_set_padding_bottom (style, &padding);
			}
		}

		break;
	}

	case HTML_ATOM_PADDING_LEFT: {
		HtmlLength padding_left;

		if (html_length_from_css_value (old_font, val, &padding_left))
			html_style_set_padding_left (style, &padding_left);

		break;
	}

	case HTML_ATOM_PADDING_RIGHT: {
		HtmlLength padding_right;

		if (html_length_from_css_value (old_font, val, &padding_right))
			html_style_set_padding_right (style, &padding_right);

		break;
	}

	case HTML_ATOM_PADDING_TOP: {
		HtmlLength padding_top;

		if (html_length_from_css_value (old_font, val, &padding_top))
			html_style_set_padding_top (style, &padding_top);
		
		break;
	}
	
	case HTML_ATOM_PADDING_BOTTOM: {
		HtmlLength padding_bottom;

		if (html_length_from_css_value (old_font, val, &padding_bottom))
			html_style_set_padding_bottom (style, &padding_bottom);
	}
	break;
	/* FIXME: this should support more than just colors, but it is better that nothing */
	case HTML_ATOM_BACKGROUND:

		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;

			/* First, check that all specified values are correct */
			while (entry) {
				if (handle_background_repeat (document, style, parent_style, entry->value)) {
				}
				else if (css_parse_color (entry->value, &color))
					html_style_set_background_color (style, &color);
				else if (entry->value->value_type == CSS_FUNCTION)
					handle_background_image (document, style, entry->value);

				entry = entry->next;
			}
		}
		if (handle_background_repeat (document, style, parent_style, val)) {
		}
		else if (val->value_type == CSS_FUNCTION)
			handle_background_image (document, style, val);
		else if (css_parse_color (val, &color))
			html_style_set_background_color (style, &color);

		break;

	case HTML_ATOM_BACKGROUND_IMAGE:
		
		if (val->v.atom == HTML_ATOM_INHERIT)
			html_style_set_background_image (style, parent_style->background->image);
		else
			handle_background_image (document, style, val);
		break;

	case HTML_ATOM_BACKGROUND_COLOR:
		
		if (val->v.atom == HTML_ATOM_INHERIT)
			html_style_set_background_color (style, &parent_style->background->color);
		else if (css_parse_color (val, &color))
			html_style_set_background_color (style, &color);
		break;

	case HTML_ATOM_DIRECTION:
		
		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			html_style_set_direction (style, parent_style->inherited->direction);
			break;
		case HTML_ATOM_LTR:
			html_style_set_direction (style, HTML_DIRECTION_LTR);
			break;
		case HTML_ATOM_RTL:
			html_style_set_direction (style, HTML_DIRECTION_RTL);
			break;
		}
		break;

	case HTML_ATOM_TEXT_ALIGN: {
		HtmlTextAlignType type = HTML_TEXT_ALIGN_DEFAULT;
		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			type = parent_style->inherited->text_align;
			break;
		case HTML_ATOM_LEFT:
			type = HTML_TEXT_ALIGN_LEFT;
			break;
		case HTML_ATOM_RIGHT:
			type = HTML_TEXT_ALIGN_RIGHT;
			break;
		case HTML_ATOM_CENTER:
			type = HTML_TEXT_ALIGN_CENTER;
			break;
		case HTML_ATOM_JUSTIFY:
			type = HTML_TEXT_ALIGN_JUSTIFY;
			break;
		default:
			g_message ("Unrecognized text-align attribute value");
			break;
		};
		html_style_set_text_align (style, type);
		break;
	}

	case HTML_ATOM_VERTICAL_ALIGN:

		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			style->vertical_align = parent_style->vertical_align;
			break;
		case HTML_ATOM_TOP:
			style->vertical_align = HTML_VERTICAL_ALIGN_TOP;
			break;
		case HTML_ATOM_BOTTOM:
			style->vertical_align = HTML_VERTICAL_ALIGN_BOTTOM;
			break;
		case HTML_ATOM_BASELINE:
			style->vertical_align = HTML_VERTICAL_ALIGN_BASELINE;
			break;
		case HTML_ATOM_MIDDLE:
			style->vertical_align = HTML_VERTICAL_ALIGN_MIDDLE;
			break;
		case HTML_ATOM_SUB:
			style->vertical_align = HTML_VERTICAL_ALIGN_SUB;
			break;
		case HTML_ATOM_SUPER:
			style->vertical_align = HTML_VERTICAL_ALIGN_SUPER;
			break;
		case HTML_ATOM_TEXT_TOP:
			style->vertical_align = HTML_VERTICAL_ALIGN_TEXT_TOP;
			break;
		case HTML_ATOM_TEXT_BOTTOM:
			style->vertical_align = HTML_VERTICAL_ALIGN_TEXT_BOTTOM;
			break;
		}
		/* FIXME: support Length and percentage / jb */
		break;

	
	case HTML_ATOM_UNICODE_BIDI: 
		
		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			style->unicode_bidi = parent_style->unicode_bidi;
			break;
		case HTML_ATOM_NORMAL:
			style->unicode_bidi = HTML_UNICODE_BIDI_NORMAL;
			break;
		case HTML_ATOM_EMBED:
			style->unicode_bidi = HTML_UNICODE_BIDI_EMBED;
			break;
		case HTML_ATOM_BIDI_OVERRIDE:
			style->unicode_bidi = HTML_UNICODE_BIDI_OVERRIDE;
			break;
		}
		
		break;
		
	case HTML_ATOM_FONT_FAMILY:
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry;
			gint len = 0;
			gchar *str;
			/* First figure out the length of all the entries */
			entry = val->v.entry;
			while (entry) {
				CssValue *value = entry->value;
				gchar *value_str = css_value_to_string (value);
				len += strlen (value_str);
				if (value_str)
					g_free (value_str);
				/* Add space for the ',' character */
				if (entry->next)
					len++;
				entry = entry->next;
			}
			/* Allocate a large enough buffer */
			str = g_new (gchar, len + 1);
			str[0] = 0;
			/* Build the family list string. Format: "x,y,z" */
			entry = val->v.entry;
			while (entry) {
				CssValue *value = entry->value;
				gchar *value_str = css_value_to_string (value);
				strcat (str, value_str);
				if (value_str)
					g_free (value_str);
				if (entry->next)
					strcat (str, ",");
				entry = entry->next;
			}
			html_style_set_font_family (style, str);
			g_free (str);
		}
		else {
			gchar *value_str = css_value_to_string (val);
			if (value_str) {
				html_style_set_font_family (style, value_str);
				g_free (value_str);
			}
		}
		break;

	case HTML_ATOM_TEXT_DECORATION:
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry;
			
			entry = val->v.entry;

			while (entry) {
				CssValue *value = entry->value;

				switch (value->v.atom) {
				case HTML_ATOM_NONE:
					html_style_set_text_decoration (style, HTML_FONT_DECORATION_NONE);
					break;
				case HTML_ATOM_UNDERLINE:
					html_style_set_text_decoration (style, HTML_FONT_DECORATION_UNDERLINE);
					break;
				case HTML_ATOM_OVERLINE:
					html_style_set_text_decoration (style, HTML_FONT_DECORATION_OVERLINE);
					break;
				case HTML_ATOM_LINE_THROUGH:
					html_style_set_text_decoration (style, HTML_FONT_DECORATION_LINETHROUGH);
					break;
				case HTML_ATOM_BLINK:
					style->blink = TRUE;
				}
				entry = entry->next;
			}
		}
		else if (val->value_type == CSS_IDENT) {

			switch (val->v.atom) {
			case HTML_ATOM_INHERIT:
				html_style_set_text_decoration (style, parent_style->inherited->font_spec->decoration);
				break;
			case HTML_ATOM_NONE:
				html_style_set_text_decoration (style, HTML_FONT_DECORATION_NONE);
				break;
			case HTML_ATOM_UNDERLINE:
				html_style_set_text_decoration (style, HTML_FONT_DECORATION_UNDERLINE);
				break;
			case HTML_ATOM_OVERLINE:
				html_style_set_text_decoration (style, HTML_FONT_DECORATION_OVERLINE);
				break;
			case HTML_ATOM_LINE_THROUGH:
				html_style_set_text_decoration (style, HTML_FONT_DECORATION_LINETHROUGH);
				break;
			case HTML_ATOM_BLINK:
				style->blink = TRUE;
			}
		}
		break;

	case HTML_ATOM_FONT_VARIANT:
		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			html_style_set_font_variant (style, parent_style->inherited->font_spec->variant);
			break;
		case HTML_ATOM_SMALL_CAPS:
			html_style_set_font_variant (style, HTML_FONT_VARIANT_SMALL_CAPS);
			break;
		}
		break;

	case HTML_ATOM_FONT_STYLE:

		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			html_style_set_font_style (style, parent_style->inherited->font_spec->style);
			break;
		case HTML_ATOM_NORMAL:
			html_style_set_font_style (style, HTML_FONT_STYLE_NORMAL);
			break;
		case HTML_ATOM_ITALIC:
			html_style_set_font_style (style, HTML_FONT_STYLE_ITALIC);
			break;
		case HTML_ATOM_OBLIQUE:
			break;
		}
		break;
		
	case HTML_ATOM_FONT_WEIGHT:

		if (val->value_type == CSS_NUMBER) {
			if (val->v.d == 100)
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_100);
			else if (val->v.d == 200)
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_200);
			else if (val->v.d == 300)
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_300);
			else if (val->v.d == 400)
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_400);
			else if (val->v.d == 500)
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_500);
			else if (val->v.d == 600)
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_600);
			else if (val->v.d == 700)
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_700);
			else if (val->v.d == 800)
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_800);
			else if (val->v.d == 900)
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_900);
		}
		else {
			switch (val->v.atom) {
			case HTML_ATOM_INHERIT:
				html_style_set_font_weight (style, parent_style->inherited->font_spec->weight);
				break;
			case HTML_ATOM_NORMAL:
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_NORMAL);
				break;
			case HTML_ATOM_BOLD:
				html_style_set_font_weight (style, HTML_FONT_WEIGHT_BOLD);
				break;
			case HTML_ATOM_BOLDER:
				html_style_set_font_weight_bolder (style);
				break;
			case HTML_ATOM_LIGHTER:
				html_style_set_font_weight_lighter (style);
				break;
			};
		}
	case HTML_ATOM_FONT_STRETCH:

		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			html_style_set_font_stretch (style, parent_style->inherited->font_spec->stretch);
			break;
		case HTML_ATOM_ULTRA_CONDENSED:
			html_style_set_font_stretch (style, HTML_FONT_STRETCH_ULTRA_CONDENSED);
			break;
		case HTML_ATOM_EXTRA_CONDENSED:
			html_style_set_font_stretch (style, HTML_FONT_STRETCH_EXTRA_CONDENSED);
			break;
		case HTML_ATOM_CONDENSED:
			html_style_set_font_stretch (style, HTML_FONT_STRETCH_CONDENSED);
			break;
		case HTML_ATOM_SEMI_CONDENSED:
			html_style_set_font_stretch (style, HTML_FONT_STRETCH_SEMI_CONDENSED);
			break;
		case HTML_ATOM_SEMI_EXPANDED:
			html_style_set_font_stretch (style, HTML_FONT_STRETCH_SEMI_EXPANDED);
			break;
		case HTML_ATOM_EXPANDED:
			html_style_set_font_stretch (style, HTML_FONT_STRETCH_EXPANDED);
			break;
		case HTML_ATOM_EXTRA_EXPANDED:
			html_style_set_font_stretch (style, HTML_FONT_STRETCH_EXTRA_EXPANDED);
			break;
		case HTML_ATOM_ULTRA_EXPANDED:
			html_style_set_font_stretch (style, HTML_FONT_STRETCH_ULTRA_EXPANDED);
			break;
		}
		
	case HTML_ATOM_FONT_SIZE:
		html_style_set_font_size (style, old_font, val);
		break;

	case HTML_ATOM_LINE_HEIGHT:
		html_style_set_line_height (style, old_font, val);
		break;

	case HTML_ATOM_OUTLINE_COLOR:
		if (val->v.atom == HTML_ATOM_INVERT)
			html_style_set_outline_color (style, NULL);
		else if (css_parse_color (val, &color))
			html_style_set_outline_color (style, &color);
		break;
		
	case HTML_ATOM_BORDER_TOP_COLOR:
		if (css_parse_color (val, &color))
			html_style_set_border_top_color (style, &color);
		break;
		
	case HTML_ATOM_BORDER_BOTTOM_COLOR:
		if (css_parse_color (val, &color))
			html_style_set_border_bottom_color (style, &color);
		break;
		
	case HTML_ATOM_BORDER_LEFT_COLOR:
		if (css_parse_color (val, &color))
			html_style_set_border_left_color (style, &color);
		break;
		
	case HTML_ATOM_BORDER_RIGHT_COLOR:
		if (css_parse_color (val, &color))
			html_style_set_border_right_color (style, &color);
		break;
		
	case HTML_ATOM_BORDER_COLOR: 
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;
			
			/* First, check that all specified values are correct */
			while (entry) {
				if (!css_parse_color (entry->value, NULL))
				    return;

				entry = entry->next;
			}

			entry = val->v.entry;
			
			/* Apply values */
			switch (css_value_list_get_length (val)) {
			case 2:
				css_parse_color (entry->value, &color);
				html_style_set_border_top_color (style, &color);
				html_style_set_border_bottom_color (style, &color);
				
				entry = entry->next;
				css_parse_color (entry->value, &color);
				html_style_set_border_left_color (style, &color);
				html_style_set_border_right_color (style, &color);

				break;
			case 3:
				css_parse_color (entry->value, &color);
				html_style_set_border_top_color (style, &color);
				entry = entry->next;
				
				css_parse_color (entry->value, &color);
				html_style_set_border_right_color (style, &color);
				html_style_set_border_left_color (style, &color);
				entry = entry->next;

				css_parse_color (entry->value, &color);
				html_style_set_border_bottom_color (style, &color);
				break;

			case 4:
				css_parse_color (entry->value, &color);
				html_style_set_border_top_color (style, &color);
				entry = entry->next;

				css_parse_color (entry->value, &color);
				html_style_set_border_right_color (style, &color);
				entry = entry->next;

				css_parse_color (entry->value, &color);
				html_style_set_border_bottom_color (style, &color);
				entry = entry->next;
				
				css_parse_color (entry->value, &color);
				html_style_set_border_left_color (style, &color);
				break;
			default:
				/* Do nothing */
				break;
			}
		}
		else {
			/* Only one item, so we'll set all the border styles */
			if (css_parse_color (val, &color)) {
				html_style_set_border_top_color (style, &color);
				html_style_set_border_right_color (style, &color);
				html_style_set_border_bottom_color (style, &color);
				html_style_set_border_left_color (style, &color);
			}
		}   
		break;
		
	case HTML_ATOM_BORDER_STYLE: 
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;
			
			/* First, check that all specified values are correct */
			while (entry) {
				if (!css_parse_border_style (entry->value, &border_style))
				    return;

				entry = entry->next;
			}

			entry = val->v.entry;
			
			/* Apply values */
			switch (css_value_list_get_length (val)) {
			case 2:
				css_parse_border_style (entry->value, &border_style);
				html_style_set_border_top_style (style, border_style);
				html_style_set_border_bottom_style (style, border_style);
				
				entry = entry->next;
				css_parse_border_style (entry->value, &border_style);
				html_style_set_border_left_style (style, border_style);
				html_style_set_border_right_style (style, border_style);

				break;
			case 3:
				css_parse_border_style (entry->value, &border_style);
				html_style_set_border_top_style (style, border_style);
				entry = entry->next;

				css_parse_border_style (entry->value, &border_style);
				html_style_set_border_right_style (style, border_style);
				html_style_set_border_left_style (style, border_style);
				entry = entry->next;
				
				css_parse_border_style (entry->value, &border_style);
				html_style_set_border_bottom_style (style, border_style);
				break;

			case 4:
				css_parse_border_style (entry->value, &border_style);
				html_style_set_border_top_style (style, border_style);
				entry = entry->next;

				css_parse_border_style (entry->value, &border_style);
				html_style_set_border_right_style (style, border_style);
				entry = entry->next;

				css_parse_border_style (entry->value, &border_style);
				html_style_set_border_bottom_style (style, border_style);
				entry = entry->next;
				
				css_parse_border_style (entry->value, &border_style);
				html_style_set_border_left_style (style, border_style);
				break;
			default:
				/* Do nothing */
				break;
			}
		}
		else {
			/* Only one item, so we'll set all the border styles */
			if (css_parse_border_style (val, &border_style)) {
				html_style_set_border_top_style (style, border_style);
				html_style_set_border_right_style (style, border_style);
				html_style_set_border_bottom_style (style, border_style);
				html_style_set_border_left_style (style, border_style);
			}
		}   
		break;

	case HTML_ATOM_BORDER_WIDTH:
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;
			
			/* First, check that all specified values are correct */
			while (entry) {
				if (!css_parse_border_width (old_font, entry->value, &border_width))
				    return;

				entry = entry->next;
			}

			entry = val->v.entry;
			
			/* Apply values */
			switch (css_value_list_get_length (val)) {
			case 2:
				css_parse_border_width (old_font, entry->value, &border_width);
				html_style_set_border_top_width (style, border_width);
				html_style_set_border_bottom_width (style, border_width);
				
				entry = entry->next;
				css_parse_border_width (old_font, entry->value, &border_width);
				html_style_set_border_left_width (style, border_width);
				html_style_set_border_right_width (style, border_width);

				break;
			case 3:
				css_parse_border_width (old_font, entry->value, &border_width);
				html_style_set_border_top_width (style, border_width);
				entry = entry->next;

				css_parse_border_width (old_font, entry->value, &border_width);
				html_style_set_border_right_width (style, border_width);
				html_style_set_border_left_width (style, border_width);
				entry = entry->next;
				
				css_parse_border_width (old_font, entry->value, &border_width);
				html_style_set_border_bottom_width (style, border_width);
				break;

			case 4:
				css_parse_border_width (old_font, entry->value, &border_width);
				html_style_set_border_top_width (style, border_width);
				entry = entry->next;

				css_parse_border_width (old_font, entry->value, &border_width);
				html_style_set_border_right_width (style, border_width);
				entry = entry->next;

				css_parse_border_width (old_font, entry->value, &border_width);
				html_style_set_border_bottom_width (style, border_width);
				entry = entry->next;
				
				css_parse_border_width (old_font, entry->value, &border_width);
				html_style_set_border_left_width (style, border_width);
				break;
			default:
				/* Do nothing */
				break;
			}
		}
		else {

			if (val->v.atom == HTML_ATOM_INHERIT) {
				html_style_set_border_top_width (style, parent_style->border->top.width);
				html_style_set_border_left_width (style, parent_style->border->left.width);
				html_style_set_border_right_width (style, parent_style->border->right.width);
				html_style_set_border_bottom_width (style, parent_style->border->bottom.width);
			}
			/* Only one item, so we'll set all the border styles */
			else if (css_parse_border_width (old_font, val, &border_width)) {
				html_style_set_border_top_width (style, border_width);
				html_style_set_border_right_width (style, border_width);
				html_style_set_border_bottom_width (style, border_width);
				html_style_set_border_left_width (style, border_width);
			}
		}   

		break;
		
	case HTML_ATOM_OUTLINE_WIDTH:
		if (css_parse_border_width (old_font, val, &border_width))
			html_style_set_outline_width (style, border_width);
		break;

	case HTML_ATOM_BORDER_TOP_WIDTH:
		if (css_parse_border_width (old_font, val, &border_width))
			html_style_set_border_top_width (style, border_width);
		break;

	case HTML_ATOM_BORDER_BOTTOM_WIDTH:
		if (css_parse_border_width (old_font, val, &border_width))
			html_style_set_border_bottom_width (style, border_width);
		break;
		
	case HTML_ATOM_BORDER_LEFT_WIDTH:
		if (css_parse_border_width (old_font, val, &border_width))
			html_style_set_border_left_width (style, border_width);
		break;

	case HTML_ATOM_BORDER_RIGHT_WIDTH:
		if (css_parse_border_width (old_font, val, &border_width))
			html_style_set_border_right_width (style, border_width);
		break;
		
	case HTML_ATOM_OUTLINE_STYLE:

		if (css_parse_border_style (val, &border_style))
			html_style_set_outline_style (style, border_style);
		break;
		
	case HTML_ATOM_BORDER_TOP_STYLE:

		if (css_parse_border_style (val, &border_style))
			html_style_set_border_top_style (style, border_style);
		break;
		
	case HTML_ATOM_BORDER_BOTTOM_STYLE:

		if (css_parse_border_style (val, &border_style))
			html_style_set_border_bottom_style (style, border_style);
		break;
		
	case HTML_ATOM_BORDER_LEFT_STYLE:

		if (css_parse_border_style (val, &border_style))
			html_style_set_border_left_style (style, border_style);
		break;
		
	case HTML_ATOM_BORDER_RIGHT_STYLE:

		if (css_parse_border_style (val, &border_style))
			html_style_set_border_right_style (style, border_style);
		break;

	case HTML_ATOM_OUTLINE:
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;
			gboolean width_spec = FALSE, style_spec = FALSE, color_spec = FALSE, invert = FALSE;
			
			if (css_value_list_get_length (val) > 3)
				return;

			/* Go through the list and check that all values are valid */
			while (entry) {
				if (!css_parse_border_width (old_font, entry->value, &border_width) &&
				    !css_parse_border_style (entry->value, &border_style) &&
				    !css_parse_color (entry->value, NULL) &&
				    entry->value->v.atom != HTML_ATOM_INVERT)
					return;
				
				entry = entry->next;
			}
			border_width = HTML_BORDER_WIDTH_MEDIUM;
			border_style = HTML_BORDER_STYLE_NONE;
			color.red = 0;
			color.green = 0;
			color.blue = 0;
			entry = val->v.entry;
			
			while (entry) {
				if (entry->value->v.atom == HTML_ATOM_INVERT)
					invert = TRUE;
				else if (css_parse_border_width (old_font, entry->value, &border_width)) {
					if (width_spec)
						return;
					else
						width_spec = TRUE;
				}
				else if (css_parse_border_style (entry->value, &border_style)) {
					if (style_spec)
						return;
					else
						style_spec = TRUE;
				}
				else if (css_parse_color (entry->value, &color)) {
					if (color_spec)
						return;
					else
						color_spec = TRUE;
				}
				entry = entry->next;
			}

			html_style_set_outline_width (style, border_width);
			html_style_set_outline_style (style, border_style);
			if (invert)
				html_style_set_outline_color (style, NULL);
			else
				html_style_set_outline_color (style, &color);
					 
		}
		else {
			if (css_parse_border_width (old_font, val, &border_width))
				html_style_set_outline_width (style, border_width);
			else if (css_parse_border_style (val, &border_style))
				html_style_set_outline_style (style, border_style);
			else if (val->v.atom == HTML_ATOM_INVERT)
				 html_style_set_outline_color (style, NULL);
			else if (css_parse_color (val, &color))
				 html_style_set_outline_color (style, &color);
		}
		break;

	case HTML_ATOM_BORDER_TOP:
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;
			gboolean width_spec = FALSE, style_spec = FALSE, color_spec = FALSE;
			
			if (css_value_list_get_length (val) > 3)
				return;

			/* Go through the list and check that all values are valid */
			while (entry) {
				if (!css_parse_border_width (old_font, entry->value, &border_width) &&
				    !css_parse_border_style (entry->value, &border_style) &&
				    !css_parse_color (entry->value, NULL))
					return;
				
				entry = entry->next;
			}
			border_width = HTML_BORDER_WIDTH_MEDIUM;
			border_style = HTML_BORDER_STYLE_NONE;
			color.red = 0;
			color.green = 0;
			color.blue = 0;
			entry = val->v.entry;
			
			while (entry) {
				if (css_parse_border_width (old_font, entry->value, &border_width)) {
					if (width_spec)
						return;
					else
						width_spec = TRUE;
				}
				else if (css_parse_border_style (entry->value, &border_style)) {
					if (style_spec)
						return;
					else
						style_spec = TRUE;
				}
				else if (css_parse_color (entry->value, &color)) {
					if (color_spec)
						return;
					else
						color_spec = TRUE;
				}
				
				entry = entry->next;
			}

			html_style_set_border_top_width (style, border_width);
			html_style_set_border_top_style (style, border_style);
			html_style_set_border_top_color (style, &color);
					 
		}
		else {
			if (css_parse_border_width (old_font, val, &border_width))
				html_style_set_border_top_width (style, border_width);
			else if (css_parse_border_style (val, &border_style))
				html_style_set_border_top_style (style, border_style);
			else if (css_parse_color (val, &color))
				 html_style_set_border_top_color (style, &color);
		}
		break;

	case HTML_ATOM_BORDER_RIGHT:
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;
			gboolean width_spec = FALSE, style_spec = FALSE, color_spec = FALSE;
			
			if (css_value_list_get_length (val) > 3)
				return;

			/* Go through the list and check that all values are valid */
			while (entry) {
				if (!css_parse_border_width (old_font, entry->value, &border_width) &&
				    !css_parse_border_style (entry->value, &border_style) &&
				    !css_parse_color (entry->value, NULL))
					return;
				
				entry = entry->next;
			}
			border_width = HTML_BORDER_WIDTH_MEDIUM;
			border_style = HTML_BORDER_STYLE_NONE;
			color.red = 0;
			color.green = 0;
			color.blue = 0;
			entry = val->v.entry;
			
			while (entry) {
				if (css_parse_border_width (old_font, entry->value, &border_width)) {
					if (width_spec)
						return;
					else
						width_spec = TRUE;
				}
				else if (css_parse_border_style (entry->value, &border_style)) {
					if (style_spec)
						return;
					else
						style_spec = TRUE;
				}
				else if (css_parse_color (entry->value, &color)) {
					if (color_spec)
						return;
					else
						color_spec = TRUE;
				}
				
				entry = entry->next;
			}

			html_style_set_border_right_width (style, border_width);
			html_style_set_border_right_style (style, border_style);
			html_style_set_border_right_color (style, &color);
					 
		}
		else {
			if (css_parse_border_width (old_font, val, &border_width))
				html_style_set_border_right_width (style, border_width);
			else if (css_parse_border_style (val, &border_style))
				html_style_set_border_right_style (style, border_style);
			else if (css_parse_color (val, &color))
				 html_style_set_border_right_color (style, &color);
		}
		break;

	case HTML_ATOM_BORDER_BOTTOM:

		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;
			gboolean width_spec = FALSE, style_spec = FALSE, color_spec = FALSE;
			
			if (css_value_list_get_length (val) > 3)
				return;

			/* Go through the list and check that all values are valid */
			while (entry) {
				if (!css_parse_border_width (old_font, entry->value, &border_width) &&
				    !css_parse_border_style (entry->value, &border_style) &&
				    !css_parse_color (entry->value, NULL))
					return;
				
				entry = entry->next;
			}
			border_width = HTML_BORDER_WIDTH_MEDIUM;
			border_style = HTML_BORDER_STYLE_NONE;
			color.red = 0;
			color.green = 0;
			color.blue = 0;
			entry = val->v.entry;
			
			while (entry) {
				if (css_parse_border_width (old_font, entry->value, &border_width)) {
					if (width_spec)
						return;
					else
						width_spec = TRUE;
				}
				else if (css_parse_border_style (entry->value, &border_style)) {
					if (style_spec)
						return;
					else
						style_spec = TRUE;
				}
				else if (css_parse_color (entry->value, &color)) {
					if (color_spec)
						return;
					else
						color_spec = TRUE;
				}
				
				entry = entry->next;
			}

			html_style_set_border_bottom_width (style, border_width);
			html_style_set_border_bottom_style (style, border_style);
			html_style_set_border_bottom_color (style, &color);
					 
		}
		else {
			if (css_parse_border_width (old_font, val, &border_width))
				html_style_set_border_bottom_width (style, border_width);
			else if (css_parse_border_style (val, &border_style))
				html_style_set_border_bottom_style (style, border_style);
			else if (css_parse_color (val, &color))
				 html_style_set_border_bottom_color (style, &color);
		}
		break;

	case HTML_ATOM_BORDER_LEFT:
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;
			gboolean width_spec = FALSE, style_spec = FALSE, color_spec = FALSE;
			
			if (css_value_list_get_length (val) > 3)
				return;

			/* Go through the list and check that all values are valid */
			while (entry) {
				if (!css_parse_border_width (old_font, entry->value, &border_width) &&
				    !css_parse_border_style (entry->value, &border_style) &&
				    !css_parse_color (entry->value, NULL))
					return;
				
				entry = entry->next;
			}
			border_width = HTML_BORDER_WIDTH_MEDIUM;
			border_style = HTML_BORDER_STYLE_NONE;
			color.red = 0;
			color.green = 0;
			color.blue = 0;
			entry = val->v.entry;
			
			while (entry) {
				if (css_parse_border_width (old_font, entry->value, &border_width)) {
					if (width_spec)
						return;
					else
						width_spec = TRUE;
				}
				else if (css_parse_border_style (entry->value, &border_style)) {
					if (style_spec)
						return;
					else
						style_spec = TRUE;
				}
				else if (css_parse_color (entry->value, &color)) {
					if (color_spec)
						return;
					else
						color_spec = TRUE;
				}
				
				entry = entry->next;
			}

			html_style_set_border_left_width (style, border_width);
			html_style_set_border_left_style (style, border_style);
			html_style_set_border_left_color (style, &color);
					 
		}
		else {
			if (css_parse_border_width (old_font, val, &border_width))
				html_style_set_border_left_width (style, border_width);
			else if (css_parse_border_style (val, &border_style))
				html_style_set_border_left_style (style, border_style);
			else if (css_parse_color (val, &color))
				 html_style_set_border_left_color (style, &color);
		}
		break;
		

	case HTML_ATOM_BORDER:
		if (val->value_type == CSS_VALUE_LIST) {
			CssValueEntry *entry = val->v.entry;
			gboolean width_spec = FALSE, style_spec = FALSE, color_spec = FALSE;
			
			if (css_value_list_get_length (val) > 3)
				return;
 
			/* Go through the list and check that all values are valid */
			while (entry) {
				if (!css_parse_border_width (old_font, entry->value, &border_width) &&
				    !css_parse_border_style (entry->value, &border_style) &&
				    !css_parse_color (entry->value, NULL))
					return;
				
				entry = entry->next;
			}
			border_width = HTML_BORDER_WIDTH_MEDIUM;
			border_style = HTML_BORDER_STYLE_NONE;
			color.red = 0;
			color.green = 0;
			color.blue = 0;
			entry = val->v.entry;
			
			while (entry) {
				if (css_parse_border_width (old_font, entry->value, &border_width)) {
					if (width_spec)
						return;
					else
						width_spec = TRUE;
				}
				else if (css_parse_border_style (entry->value, &border_style)) {
					if (style_spec)
						return;
					else
						style_spec = TRUE;
				}
				else if (css_parse_color (entry->value, &color)) {
					if (color_spec)
						return;
					else
						color_spec = TRUE;
				}
				
				entry = entry->next;
			}

			html_style_set_border_top_width (style, border_width);
			html_style_set_border_right_width (style, border_width);
			html_style_set_border_bottom_width (style, border_width);
			html_style_set_border_left_width (style, border_width);

			html_style_set_border_top_style (style, border_style);
			html_style_set_border_right_style (style, border_style);
			html_style_set_border_bottom_style (style, border_style);
			html_style_set_border_left_style (style, border_style);
			
			html_style_set_border_top_color (style, &color);
			html_style_set_border_right_color (style, &color);
			html_style_set_border_bottom_color (style, &color);
			html_style_set_border_left_color (style, &color);
					 
		}
		else {
			if (css_parse_border_width (old_font, val, &border_width)) {
				html_style_set_border_top_width (style, border_width);
				html_style_set_border_right_width (style, border_width);
				html_style_set_border_bottom_width (style, border_width);
				html_style_set_border_left_width (style, border_width);
			}
			else if (css_parse_border_style (val, &border_style)) {
				html_style_set_border_top_style (style, border_style);
				html_style_set_border_right_style (style, border_style);
				html_style_set_border_bottom_style (style, border_style);
				html_style_set_border_left_style (style, border_style);
			}
			else if (css_parse_color (val, &color)) {
				html_style_set_border_top_color (style, &color);
				html_style_set_border_right_color (style, &color);
				html_style_set_border_bottom_color (style, &color);
				html_style_set_border_left_color (style, &color);
			}
		}
		break;
	case HTML_ATOM_CURSOR:

		switch (val->v.atom) {
		case HTML_ATOM_INHERIT:
			html_style_set_cursor (style, parent_style->inherited->cursor);
			break;
		case HTML_ATOM_AUTO:
			html_style_set_cursor (style, HTML_CURSOR_AUTO);
			break;
		case HTML_ATOM_CROSSHAIR:
			html_style_set_cursor (style, HTML_CURSOR_CROSSHAIR);
			break;
		case HTML_ATOM_DEFAULT:
			html_style_set_cursor (style, HTML_CURSOR_DEFAULT);
			break;
		case HTML_ATOM_POINTER:
			html_style_set_cursor (style, HTML_CURSOR_POINTER);
			break;
		case HTML_ATOM_MOVE:
			html_style_set_cursor (style, HTML_CURSOR_MOVE);
			break;
		case HTML_ATOM_E_RESIZE:
			html_style_set_cursor (style, HTML_CURSOR_E_RESIZE);
			break;
		case HTML_ATOM_NE_RESIZE:
			html_style_set_cursor (style, HTML_CURSOR_NE_RESIZE);
			break;
		case HTML_ATOM_NW_RESIZE:
			html_style_set_cursor (style, HTML_CURSOR_NW_RESIZE);
			break;
		case HTML_ATOM_N_RESIZE:
			html_style_set_cursor (style, HTML_CURSOR_N_RESIZE);
			break;
		case HTML_ATOM_SE_RESIZE:
			html_style_set_cursor (style, HTML_CURSOR_SE_RESIZE);
			break;
		case HTML_ATOM_SW_RESIZE:
			html_style_set_cursor (style, HTML_CURSOR_SW_RESIZE);
			break;
		case HTML_ATOM_S_RESIZE:
			html_style_set_cursor (style, HTML_CURSOR_S_RESIZE);
			break;
		case HTML_ATOM_W_RESIZE:
			html_style_set_cursor (style, HTML_CURSOR_W_RESIZE);
			break;
		case HTML_ATOM_TEXT:
			html_style_set_cursor (style, HTML_CURSOR_TEXT);
			break;
		case HTML_ATOM_WAIT:
			html_style_set_cursor (style, HTML_CURSOR_WAIT);
			break;
		case HTML_ATOM_HELP:
			html_style_set_cursor (style, HTML_CURSOR_HELP);
			break;
		default:
			break;
		}

		break;
		
	default:
		g_print ("Unhandled property: %d %s\n", prop, html_atom_list_get_string (html_atom_list, prop));
	}
}

#if 0
static void
css_matcher_sheet_stream_write (HtmlStream *stream, const gchar *buffer, guint size, CssFetcherContext *context)
{
	if (!context->str)
		context->str = g_string_new_len (buffer, size);
	else
		g_string_append_len (context->str, buffer, size);
}

static void
css_matcher_sheet_stream_close (HtmlStream *stream, CssFetcherContext *context)
{
	CssStylesheet *ss;

	if (html_stream_get_written (stream) != 0) {
		ss = css_parser_parse_stylesheet (context->str->str, context->str->len);

		context->stat->s.import_rule.fetched = TRUE;
	
		if (ss) {
			context->stat->s.import_rule.sheet = ss;
		}

		g_string_free (context->str, TRUE);

		html_document_restyle_views (context->doc);
	}

	g_free (context);
}

#endif

static void
css_matcher_apply_stylesheet (HtmlDocument *doc, CssStylesheet *ss, xmlNode *node, GList **declaration_list, gint type, HtmlAtom *pseudo)
{
	GSList *list = ss->stat;

	for (list = ss->stat; list; list = list->next) {
		CssStatement *stat = list->data;
		gint j;

		if (stat->type == CSS_IMPORT_RULE) {
			if (stat->s.import_rule.fetched) {
				if (stat->s.import_rule.sheet) {
					css_matcher_apply_stylesheet (doc, stat->s.import_rule.sheet, node, declaration_list, type, pseudo);
				}
			}
			else if (!stat->s.import_rule.fetching) {
#if 0
				HtmlStream *stream;
				CssFetcherContext *context = g_new0 (CssFetcherContext, 1);
					
				/* FIXME: Can we assume this? */
				gchar *str = g_strndup (stat->s.import_rule.url->v.s->buffer,
							stat->s.import_rule.url->v.s->len);
				
				stream = html_stream_new ((HtmlStreamWriteFunc)css_matcher_sheet_stream_write,
							  (HtmlStreamCloseFunc)css_matcher_sheet_stream_close,
							  context);
				
				context->stat = stat;
				context->doc = doc;
				
				stat->s.import_rule.fetching = TRUE;
				
				gtk_signal_emit_by_name (GTK_OBJECT (doc), "url_requested", str, stream);
				
				g_free (str);
#endif			
			}
		}
		
		/* FIXME: We need to support more than just rulesets here */
		if (stat->type != CSS_RULESET)
			continue;
		
		for (j = 0; j < stat->s.ruleset->n_sel; j++) {
			CssSelector *sel = stat->s.ruleset->sel[j];
			
			if (css_matcher_match_selector (sel, node, pseudo)) {
				int i;
				
				for (i = 0; i < stat->s.ruleset->n_decl; i++) {
					CssDeclaration *decl = stat->s.ruleset->decl[i];
					CssDeclarationListEntry *entry = g_new (CssDeclarationListEntry, 1);
					
					entry->spec = sel->a * 1000000 + sel->b * 1000 + sel->c;
					entry->type = type;
					entry->decl = g_new (CssDeclaration, 1);
					entry->decl->property = decl->property;
					entry->decl->expr = css_value_ref (decl->expr);
					entry->decl->important = decl->important;
					*declaration_list = g_list_insert_sorted (*declaration_list, entry, css_declaration_list_sorter);
				}
			}
		}
	}
}

static void
css_matcher_validate_style (HtmlStyle *style)
{
	/* Do This is according to the CSS2 specification section 9.7 */
	if (style->position == HTML_POSITION_ABSOLUTE ||
	    style->position == HTML_POSITION_FIXED) {

		style->display = HTML_DISPLAY_BLOCK;
		style->Float   = HTML_FLOAT_NONE;
	}
	else if (style->Float != HTML_FLOAT_NONE) {

		style->display = HTML_DISPLAY_BLOCK;
	}
}

static void
free_decl_entry (CssDeclarationListEntry *entry, gpointer user_data)
{
	css_value_unref (entry->decl->expr);
	g_free (entry->decl);
	g_free (entry);
}

static void
css_matcher_html_to_css (HtmlDocument *document, HtmlStyle *style, xmlNode *n)
{
	gchar *str;
	
	if (n->type != XML_ELEMENT_NODE)
		return;


	if (strcasecmp (n->name, "td") == 0 ||
	    strcasecmp (n->name, "th") == 0) {
		DomNode *node = dom_Node_mkref (n);
		
		gint level = 4;

		node = dom_Node__get_parentNode (node);
		
		while (level-- && node) {
			/* FIXME: Check if border is a valid number */
			if (node->style && node->style->display == HTML_DISPLAY_TABLE &&
			    dom_Element_hasAttribute (DOM_ELEMENT (node), "border")) {
				gchar *str = dom_Element_getAttribute (DOM_ELEMENT (node), "border");

				str = g_strchug (str);
				
				if (atoi (str) > 0) {
					html_style_set_border_top_width (style, HTML_BORDER_WIDTH_THIN);
					html_style_set_border_right_width (style, HTML_BORDER_WIDTH_THIN);
					html_style_set_border_bottom_width (style, HTML_BORDER_WIDTH_THIN);
					html_style_set_border_left_width (style, HTML_BORDER_WIDTH_THIN);
					
					html_style_set_border_top_style (style, HTML_BORDER_STYLE_INSET);
					html_style_set_border_left_style (style, HTML_BORDER_STYLE_INSET);
					html_style_set_border_right_style (style, HTML_BORDER_STYLE_INSET);
					html_style_set_border_bottom_style (style, HTML_BORDER_STYLE_INSET);
				}
				xmlFree (str);

				break;
			}
			node = dom_Node__get_parentNode (node);
		}
	}

	if (n->properties == NULL)
		return;
	
	if (strcasecmp (n->name, "table") == 0) {

		if ((str = xmlGetProp (n, "border"))) {
			gint border_width;
			if (*str == 0)
				border_width = 1;
			else
				border_width = atoi (str);
			
			html_style_set_border_top_width (style, border_width);
			html_style_set_border_right_width (style, border_width);
			html_style_set_border_bottom_width (style, border_width);
			html_style_set_border_left_width (style, border_width);
			
			html_style_set_border_top_style (style, HTML_BORDER_STYLE_OUTSET);
			html_style_set_border_left_style (style, HTML_BORDER_STYLE_OUTSET);
			html_style_set_border_right_style (style, HTML_BORDER_STYLE_OUTSET);
			html_style_set_border_bottom_style (style, HTML_BORDER_STYLE_OUTSET);
			
			xmlFree (str);
		}
	}
	if (strcasecmp (n->name, "img") == 0 || 
	    strcasecmp (n->name, "applet") == 0 ||
	    strcasecmp (n->name, "object") == 0) {
		if ((str = xmlGetProp (n, "hspace"))) {
			HtmlLength length;
			html_length_set_value (&length, atoi (str), HTML_LENGTH_FIXED);
			html_style_set_padding_left (style, &length);
			html_style_set_padding_right (style, &length);
			xmlFree (str);
		}
		if ((str = xmlGetProp (n, "vspace"))) {
			HtmlLength length;
			html_length_set_value (&length, atoi (str), HTML_LENGTH_FIXED);
			html_style_set_padding_top (style, &length);
			html_style_set_padding_bottom (style, &length);
			xmlFree (str);
		}
	}
	if (strcasecmp (n->name, "body") == 0) {
		if ((str = xmlGetProp (n, "text"))) {
			HtmlColor *color = html_color_new_from_name (str);
			if (color) {
				html_style_set_color (style, color);
				html_color_destroy (color);
			}
			xmlFree (str);
		}
	}
	else if (strcasecmp (n->name, "font") == 0) {
		if ((str = xmlGetProp (n, "family"))) {		
			html_style_set_font_family (style, str);
			xmlFree (str);
		}
		if ((str = xmlGetProp (n, "size"))) {		

			gint font_size;

			/* Relative size */
			if (strchr (str, '+') || strchr (str, '-')) {

				gint value = atoi (str);
				font_size = html_font_specification_get_html_size (style->inherited->font_spec) + value;
			}
			/* Absolute size */
			else
				font_size = atoi (str);

			html_style_set_font_size_html (style, font_size);

			xmlFree (str);
		}
	}
	
	/* convert align="left|right|center|justify" to "text-align:x"
	   * Match against tr, td, th, thead, tfoot, tbody, div, p, h1, h2, h3, h4, h5, h6 */
	else if ((strcasecmp ("tr", n->name) == 0) || 
		 (strcasecmp ("td", n->name) == 0) || 
		 (strcasecmp ("th", n->name) == 0) || 
		 (strcasecmp ("thead", n->name) == 0) || 
		 (strcasecmp ("tbody", n->name) == 0) || 
		 (strcasecmp ("tfoot", n->name) == 0) || 
		 (strcasecmp ("div", n->name) == 0) || 
		 (strcasecmp ("p", n->name) == 0) || 
		 (strlen (n->name) == 2 && tolower(n->name[0]) == 'h' && strchr ("123456", n->name[1]))) {

		if ((str = xmlGetProp (n, "align"))) {
			if (strcasecmp (str, "left") == 0)
				html_style_set_text_align (style, HTML_TEXT_ALIGN_LEFT);
			else if (strcasecmp (str, "right") == 0)
				html_style_set_text_align (style, HTML_TEXT_ALIGN_RIGHT);
			else if (strcasecmp (str, "center") == 0)
				html_style_set_text_align (style, HTML_TEXT_ALIGN_CENTER);
			else if (strcasecmp (str, "justify") == 0)
				html_style_set_text_align (style, HTML_TEXT_ALIGN_JUSTIFY);
			xmlFree (str);
		}
	}
	if ((str = xmlGetProp (n, "color"))) {
		HtmlColor *color = html_color_new_from_name (str);
		if (color) {
			html_style_set_color (style, color);
			html_color_destroy (color);
		}
		xmlFree (str);
	}
	if ((str = xmlGetProp (n, "bgcolor"))) {
		HtmlColor *color = html_color_new_from_name (str);
		if (color) {
			html_style_set_background_color (style, color);
			html_color_destroy (color);
		}
		xmlFree (str);
	}
	if ((str = xmlGetProp (n, "background"))) {
		HtmlImage *image;

		image = html_image_factory_get_image (document->image_factory, str);

		html_style_set_background_image (style, image);
		g_object_unref (G_OBJECT(image));
		xmlFree (str);
	}
	if ((str = xmlGetProp (n, "width"))) {
		HtmlLength length;

		str = g_strchug (str);

		if (strchr (str, '%'))
			html_length_set_value (&length, atoi (str), HTML_LENGTH_PERCENT);
		else if (g_ascii_isdigit (*str))
			html_length_set_value (&length, atoi (str), HTML_LENGTH_FIXED);

		html_style_set_width (style, &length);

		xmlFree (str);
	}
	if ((str = xmlGetProp (n, "height"))) {
		HtmlLength length;

		str = g_strchug (str);

		if (strchr (str, '%'))
			html_length_set_value (&length, atoi (str), HTML_LENGTH_PERCENT);
		else if (g_ascii_isdigit (*str))
			html_length_set_value (&length, atoi (str), HTML_LENGTH_FIXED);

		html_style_set_height (style, &length);

		xmlFree (str);
	}
}

static void
css_matcher_html_to_css_after (HtmlStyle *style, xmlNode *n) 
{
	gchar *str;

	if (n->type != XML_ELEMENT_NODE)
		return;

	if (n->properties == NULL)
		return;

	if (strcasecmp ("body", n->name) == 0) {
		if ((str = xmlGetProp (n, "leftmargin"))) {
			HtmlLength length;
			html_length_set_value (&length, atoi (str), HTML_LENGTH_FIXED);
			html_style_set_margin_left (style, &length);
			xmlFree (str);
		}
		if ((str = xmlGetProp (n, "rightmargin"))) {
			HtmlLength length;
			html_length_set_value (&length, atoi (str), HTML_LENGTH_FIXED);
			html_style_set_margin_right (style, &length);
			xmlFree (str);
		}
		if ((str = xmlGetProp (n, "topmargin"))) {
			HtmlLength length;
			html_length_set_value (&length, atoi (str), HTML_LENGTH_FIXED);
			html_style_set_margin_top (style, &length);
			xmlFree (str);
		}
		if ((str = xmlGetProp (n, "bottommargin"))) {
			HtmlLength length;
			html_length_set_value (&length, atoi (str), HTML_LENGTH_FIXED);
			html_style_set_margin_bottom (style, &length);
			xmlFree (str);
		}
		if ((str = xmlGetProp (n, "marginwidth"))) {
			HtmlLength length;
			html_length_set_value (&length, atoi (str), HTML_LENGTH_FIXED);
			html_style_set_margin_left (style, &length);
			html_style_set_margin_right (style, &length);
			xmlFree (str);
		}
		if ((str = xmlGetProp (n, "marginheight"))) {
			HtmlLength length;
			html_length_set_value (&length, atoi (str), HTML_LENGTH_FIXED);
			html_style_set_margin_top (style, &length);
			html_style_set_margin_bottom (style, &length);
			xmlFree (str);
		}
	}
	if (strcasecmp ("table", n->name) == 0) {
		if ((str = xmlGetProp (n, "align"))) {
			if (strcasecmp (str, "left") == 0)
				style->Float = HTML_FLOAT_LEFT;
			else if (strcasecmp (str, "right") == 0)
			style->Float = HTML_FLOAT_RIGHT;
			xmlFree (str);
		}
		if ((str = xmlGetProp (n, "cellspacing"))) {
			gint spacing = atoi (str);
			html_style_set_border_spacing (style, spacing, spacing);
			xmlFree (str);
		}
	}
}

HtmlStyle *
css_matcher_get_style (HtmlDocument *doc, HtmlStyle *parent_style, xmlNode *node, HtmlAtom *pseudo)
{
	/* We have to send the old font specification separately because all the em and ex values are based on the
	 * old value not the new */
	HtmlFontSpecification *font_spec = parent_style ? parent_style->inherited->font_spec : NULL;
	xmlChar *prop;
	GList *declaration_list = NULL;
	GList *temp;
	GSList *ss;
	HtmlStyle *style;

	total_pseudos = CSS_STYLESHEET_PSEUDO_NONE;
	
	style = html_style_new (parent_style);

	/* First, convert known HTML properties into style properties */
	css_matcher_html_to_css (doc, style, node);
	
	if (!default_stylesheet) {
		default_stylesheet = css_parser_parse_stylesheet (html_css, strlen (html_css));
	}

	css_matcher_apply_stylesheet (doc, default_stylesheet, node, &declaration_list, CSS_STYLESHEET_DEFAULT, pseudo);
	
	ss = doc->stylesheets;

	while (ss) {
		CssStylesheet *sheet = ss->data;

		css_matcher_apply_stylesheet (doc, sheet, node, &declaration_list, CSS_STYLESHEET_AUTHOR, pseudo);

		ss = ss->next;
	}
		
	/* FIXME: Should we look at namespaces here, yes, but probably global namespaces */
	prop = xmlGetProp (node, "style");
	
	if (prop) {
		CssRuleset *rs = css_parser_parse_style_attr (prop, strlen (prop));
		gint i;
		
		if (rs) {
			for (i = 0; i < rs->n_decl; i++) {
				CssDeclarationListEntry *entry = g_new (CssDeclarationListEntry, 1);
				CssDeclaration *decl = rs->decl[i];

				entry->type = CSS_STYLESHEET_STYLEDECL;
				entry->decl = g_new (CssDeclaration, 1);
				entry->decl->property = decl->property;
				entry->decl->expr = css_value_ref (decl->expr);
				entry->decl->important = decl->important;
				entry->spec = 0;

				declaration_list = g_list_insert_sorted (declaration_list, entry, css_declaration_list_sorter);
			}
			css_ruleset_destroy (rs);
		}
		xmlFree (prop);
	}

	temp = declaration_list;
	while (declaration_list) {
		CssDeclarationListEntry *entry = declaration_list->data;

		css_matcher_apply_rule (doc, style, parent_style, font_spec, entry->decl);

		declaration_list = declaration_list->next;
	}
	declaration_list = temp;
	g_list_foreach (declaration_list, (GFunc) free_decl_entry, NULL);
	g_list_free (declaration_list);

	if (style->unicode_bidi == HTML_UNICODE_BIDI_EMBED) {
		
		if (parent_style && style->inherited->direction != parent_style->inherited->direction)
			html_style_set_bidi_level (style, style->inherited->bidi_level + 1);
		else if (parent_style == NULL && style->inherited->direction == HTML_DIRECTION_RTL)
			html_style_set_bidi_level (style, HTML_DIRECTION_RTL);
	}

	css_matcher_validate_style (style);
	css_matcher_html_to_css_after (style, node);

	if (total_pseudos & CSS_STYLESHEET_PSEUDO_HOVER)
		style->has_hover_style = TRUE;
	if (total_pseudos & CSS_STYLESHEET_PSEUDO_ACTIVE)
		style->has_active_style = TRUE;
	if (total_pseudos & CSS_STYLESHEET_PSEUDO_FOCUS)
		style->has_focus_style = TRUE;
	if (total_pseudos & CSS_STYLESHEET_PSEUDO_BEFORE)
		style->has_before_style = TRUE;
	if (total_pseudos & CSS_STYLESHEET_PSEUDO_AFTER)
		style->has_after_style = TRUE;
	
	return style;
}