summaryrefslogtreecommitdiff
path: root/css/css.c
blob: 9287096f381e0999217940e8f33b7867e2da2075 (plain)
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
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
 */

/** \file
 * CSS handling (implementation).
 *
 * See CSS 2.1 chapter 5 for the terms used here.
 *
 * CSS style sheets are stored as a hash table mapping selectors to styles.
 * Selectors are hashed by the <em>type selector</em> of the last <em>simple
 * selector</em> in the selector. The <em>universal selector</em> is hashed to
 * chain 0.
 *
 * A <em>simple selector</em> is a struct css_selector with type
 * CSS_SELECTOR_ELEMENT. The data field is the <em>type selector</em>, or 0 for
 * the <em>universal selector</em>. Any <em>attribute selectors</em>, <em>ID
 * selectors</em>, or <em>pseudo-classes</em> form a linked list of
 * css_selector hanging from detail.
 *
 * A <em>selector</em> is a linked list by the combiner field of these simple
 * selectors, in reverse order that they appear in the concrete syntax. The last
 * simple selector is first, then the previous one is linked at combiner and has
 * relationship comb to the last, etc.
 *
 * Selectors are then linked in each hash chain by next, in order of increasing
 * specificity.
 *
 * As an example, the stylesheet
 * \code
 *   th { [1] }
 *   div#id1 > h4.class1 { [2] }
 *   center * { [3] }                                                  \endcode
 *
 * would result in a struct css_stylesheet (content::data.css.css) like this
 * \dot
 *   digraph example {
 *     node [shape=record, fontname=Helvetica, fontsize=9];
 *     edge [fontname=Helvetica, fontsize=9];
 *     css -> n0 [label="rule[0]"];
 *     css -> n2 [label="rule[29]"];
 *
 *     n0 [label="css_selector\ntype CSS_SELECTOR_ELEMENT\ldata 0\lcomb CSS_COMB_ANCESTOR\lspecificity 2\l"];
 *     n0 -> n1 [label="combiner"];
 *     n0 -> n0style [label="style"]; n0style [label="[3]"];
 *
 *     n1 [label="css_selector\ntype CSS_SELECTOR_ELEMENT\ldata \"center\"\lcomb CSS_COMB_NONE\lspecificity 1\l"];
 *
 *     n2 [label="css_selector\ntype CSS_SELECTOR_ELEMENT\ldata \"th\"\lcomb CSS_COMB_NONE\lspecificity 1\l"];
 *     n2 -> n3 [label="next"];
 *     n2 -> n2style [label="style"]; n2style [label="[1]"];
 *
 *     n3 [label="css_selector\ntype CSS_SELECTOR_ELEMENT\ldata \"h4\"\lcomb CSS_COMB_PARENT\lspecificity 0x10102\l"];
 *     n3 -> n4 [label="detail"];
 *     n3 -> n5 [label="combiner"];
 *     n3 -> n3style [label="style"]; n3style [label="[2]"];
 *
 *     n4 [label="css_selector\ntype CSS_SELECTOR_CLASS\ldata \"class1\"\lcomb CSS_COMB_NONE\lspecificity 0x100\l"];
 *
 *     n5 [label="css_selector\ntype CSS_SELECTOR_ELEMENT\ldata \"div\"\lcomb CSS_COMB_NONE\lspecificity 0x10001\l"];
 *     n5 -> n6 [label="detail"];
 *
 *     n6 [label="css_selector\ntype CSS_SELECTOR_ID\ldata \"#id1\"\lcomb CSS_COMB_NONE\lspecificity 0x10000\l"];
 *   }
 * \enddot
 *
 * (any fields not shown are 0). In this example the first two rules happen to
 * have hashed to the same value.
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#define CSS_INTERNALS
#undef NDEBUG
#include "netsurf/utils/config.h"
#include "netsurf/content/content.h"
#include "netsurf/content/fetch.h"
#include "netsurf/content/fetchcache.h"
#include "netsurf/css/css.h"
#include "netsurf/css/parser.h"
#ifdef riscos
#include "netsurf/desktop/gui.h"
#endif
#include "netsurf/utils/log.h"
#include "netsurf/utils/messages.h"
#include "netsurf/utils/url.h"
#include "netsurf/utils/utils.h"


static void css_atimport_callback(content_msg msg, struct content *css,
		void *p1, void *p2, union content_msg_data data);
static bool css_match_rule(struct css_selector *rule, xmlNode *element);
static bool css_match_detail(const struct css_selector *detail,
		xmlNode *element);
static void css_dump_length(const struct css_length * const length);
static void css_dump_selector(const struct css_selector *r);

/** Default style for a document. These are the 'Initial values' from the
 *  spec. */
const struct css_style css_base_style = {
	0xffffff,
	CSS_BACKGROUND_ATTACHMENT_SCROLL,
	{ CSS_BACKGROUND_IMAGE_NONE, 0 },
	{ { CSS_BACKGROUND_POSITION_PERCENT, { 0.0 } },
	  { CSS_BACKGROUND_POSITION_PERCENT, { 0.0 } } },
	CSS_BACKGROUND_REPEAT_REPEAT,
	{ { 0x000000, { CSS_BORDER_WIDTH_LENGTH,
	    { 2, CSS_UNIT_PX } }, CSS_BORDER_STYLE_NONE },
	  { 0x000000, { CSS_BORDER_WIDTH_LENGTH,
	    { 2, CSS_UNIT_PX } }, CSS_BORDER_STYLE_NONE },
	  { 0x000000, { CSS_BORDER_WIDTH_LENGTH,
	    { 2, CSS_UNIT_PX } }, CSS_BORDER_STYLE_NONE },
	  { 0x000000, { CSS_BORDER_WIDTH_LENGTH,
	    { 2, CSS_UNIT_PX } }, CSS_BORDER_STYLE_NONE } },
	CSS_CLEAR_NONE,
	0x000000,
	CSS_CURSOR_AUTO,
	CSS_DISPLAY_BLOCK,
	CSS_FLOAT_NONE,
	{ CSS_FONT_SIZE_LENGTH, { { 10, CSS_UNIT_PT } } },
	CSS_FONT_FAMILY_SANS_SERIF,
	CSS_FONT_WEIGHT_NORMAL,
	CSS_FONT_STYLE_NORMAL,
	CSS_FONT_VARIANT_NORMAL,
	{ CSS_HEIGHT_AUTO, { 1, CSS_UNIT_EM } },
	{ CSS_LINE_HEIGHT_ABSOLUTE, { 1.3 } },
	{ { CSS_MARGIN_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_MARGIN_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_MARGIN_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_MARGIN_LENGTH, { { 0, CSS_UNIT_PX } } } },
	CSS_OVERFLOW_VISIBLE,
	{ { CSS_PADDING_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_PADDING_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_PADDING_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_PADDING_LENGTH, { { 0, CSS_UNIT_PX } } } },
	CSS_TEXT_ALIGN_LEFT,
	CSS_TEXT_DECORATION_NONE,
	{ CSS_TEXT_INDENT_LENGTH, { { 0, CSS_UNIT_EM } } },
	CSS_TEXT_TRANSFORM_NONE,
	CSS_VISIBILITY_VISIBLE,
	{ CSS_WIDTH_AUTO, { { 1, CSS_UNIT_EM } } },
	CSS_WHITE_SPACE_NORMAL
};

/** Style with no values set. */
const struct css_style css_empty_style = {
	CSS_COLOR_INHERIT,
	CSS_BACKGROUND_ATTACHMENT_INHERIT,
	{ CSS_BACKGROUND_IMAGE_INHERIT, 0 },
	{ { CSS_BACKGROUND_POSITION_INHERIT, { 0.0 } },
	  { CSS_BACKGROUND_POSITION_INHERIT, { 0.0 } } },
	CSS_BACKGROUND_REPEAT_INHERIT,
	{ { CSS_COLOR_INHERIT, { CSS_BORDER_WIDTH_INHERIT,
	    { 0, CSS_UNIT_PX } }, CSS_BORDER_STYLE_INHERIT },
	  { CSS_COLOR_INHERIT, { CSS_BORDER_WIDTH_INHERIT,
	    { 0, CSS_UNIT_PX } }, CSS_BORDER_STYLE_INHERIT },
	  { CSS_COLOR_INHERIT, { CSS_BORDER_WIDTH_INHERIT,
	    { 0, CSS_UNIT_PX } }, CSS_BORDER_STYLE_INHERIT },
	  { CSS_COLOR_INHERIT, { CSS_BORDER_WIDTH_INHERIT,
	    { 0, CSS_UNIT_PX } }, CSS_BORDER_STYLE_INHERIT } },
	CSS_CLEAR_INHERIT,
	CSS_COLOR_INHERIT,
	CSS_CURSOR_INHERIT,
	CSS_DISPLAY_INHERIT,
	CSS_FLOAT_INHERIT,
	{ CSS_FONT_SIZE_INHERIT, { { 1, CSS_UNIT_EM } } },
	CSS_FONT_FAMILY_INHERIT,
	CSS_FONT_WEIGHT_INHERIT,
	CSS_FONT_STYLE_INHERIT,
	CSS_FONT_VARIANT_INHERIT,
	{ CSS_HEIGHT_INHERIT, { 1, CSS_UNIT_EM } },
	{ CSS_LINE_HEIGHT_INHERIT, { 1.3 } },
	{ { CSS_MARGIN_INHERIT, { { 0, CSS_UNIT_PX } } },
	  { CSS_MARGIN_INHERIT, { { 0, CSS_UNIT_PX } } },
	  { CSS_MARGIN_INHERIT, { { 0, CSS_UNIT_PX } } },
	  { CSS_MARGIN_INHERIT, { { 0, CSS_UNIT_PX } } } },
	CSS_OVERFLOW_INHERIT,
	{ { CSS_PADDING_INHERIT, { { 0, CSS_UNIT_PX } } },
	  { CSS_PADDING_INHERIT, { { 0, CSS_UNIT_PX } } },
	  { CSS_PADDING_INHERIT, { { 0, CSS_UNIT_PX } } },
	  { CSS_PADDING_INHERIT, { { 0, CSS_UNIT_PX } } } },
	CSS_TEXT_ALIGN_INHERIT,
	CSS_TEXT_DECORATION_INHERIT,
	{ CSS_TEXT_INDENT_INHERIT, { { 0, CSS_UNIT_EM } } },
	CSS_TEXT_TRANSFORM_INHERIT,
	CSS_VISIBILITY_INHERIT,
	{ CSS_WIDTH_INHERIT, { { 1, CSS_UNIT_EM } } },
	CSS_WHITE_SPACE_INHERIT
};

/** Default style for an element. These should be INHERIT if 'Inherited' is yes,
 *  and the 'Initial value' otherwise. */
const struct css_style css_blank_style = {
	TRANSPARENT,
	CSS_BACKGROUND_ATTACHMENT_SCROLL,
	{ CSS_BACKGROUND_IMAGE_NONE, 0 },
	{ { CSS_BACKGROUND_POSITION_PERCENT, { 0.0 } },
	  { CSS_BACKGROUND_POSITION_PERCENT, { 0.0 } } },
	CSS_BACKGROUND_REPEAT_REPEAT,
	{ { 0x000000, { CSS_BORDER_WIDTH_LENGTH,
	    { 2, CSS_UNIT_PX } }, CSS_BORDER_STYLE_NONE },
	  { 0x000000, { CSS_BORDER_WIDTH_LENGTH,
	    { 2, CSS_UNIT_PX } }, CSS_BORDER_STYLE_NONE },
	  { 0x000000, { CSS_BORDER_WIDTH_LENGTH,
	    { 2, CSS_UNIT_PX } }, CSS_BORDER_STYLE_NONE },
	  { 0x000000, { CSS_BORDER_WIDTH_LENGTH,
	    { 2, CSS_UNIT_PX } }, CSS_BORDER_STYLE_NONE } },
	CSS_CLEAR_NONE,
	CSS_COLOR_INHERIT,
	CSS_CURSOR_INHERIT,
	CSS_DISPLAY_INLINE,
	CSS_FLOAT_NONE,
	{ CSS_FONT_SIZE_INHERIT, { { 1, CSS_UNIT_EM } } },
	CSS_FONT_FAMILY_INHERIT,
	CSS_FONT_WEIGHT_INHERIT,
	CSS_FONT_STYLE_INHERIT,
	CSS_FONT_VARIANT_INHERIT,
	{ CSS_HEIGHT_AUTO, { 1, CSS_UNIT_EM } },
	{ CSS_LINE_HEIGHT_INHERIT, { 1.3 } },
	{ { CSS_MARGIN_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_MARGIN_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_MARGIN_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_MARGIN_LENGTH, { { 0, CSS_UNIT_PX } } } },
	CSS_OVERFLOW_VISIBLE,
	{ { CSS_PADDING_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_PADDING_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_PADDING_LENGTH, { { 0, CSS_UNIT_PX } } },
	  { CSS_PADDING_LENGTH, { { 0, CSS_UNIT_PX } } } },
	CSS_TEXT_ALIGN_INHERIT,
	CSS_TEXT_DECORATION_INHERIT,
	{ CSS_TEXT_INDENT_INHERIT, { { 0, CSS_UNIT_EM } } },
	CSS_TEXT_TRANSFORM_INHERIT,
	CSS_VISIBILITY_INHERIT,
	{ CSS_WIDTH_AUTO, { { 1, CSS_UNIT_EM } } },
	CSS_WHITE_SPACE_INHERIT
};


/**
 * Convert a CONTENT_CSS for use.
 */

bool css_convert(struct content *c, int width, int height)
{
	unsigned char *source_data;
	unsigned char *current, *end, *token_text;
	unsigned int i;
	int token;
	void *parser;
	struct css_parser_params param = {false, c, 0, false, false};
	struct css_parser_token token_data;
	union content_msg_data msg_data;

	c->data.css.css = malloc(sizeof *c->data.css.css);
	parser = css_parser_Alloc(malloc);
	source_data = realloc(c->source_data, c->source_size + 10);

	if (!c->data.css.css || !parser || !source_data) {
		free(c->data.css.css);
		css_parser_Free(parser, free);

		msg_data.error = messages_get("NoMemory");
		content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
		warn_user("NoMemory", 0);
		return false;
	}

	for (i = 0; i != HASH_SIZE; i++)
		c->data.css.css->rule[i] = 0;
	c->data.css.import_count = 0;
	c->data.css.import_url = 0;
	c->data.css.import_content = 0;
	c->active = 0;
	c->source_data = source_data;

	for (i = 0; i != 10; i++)
		source_data[c->source_size + i] = 0;

	current = source_data;
	end = source_data + c->source_size;
	while (current < end && (token = css_tokenise(&current, end + 10,
			&token_text))) {
		token_data.text = token_text;
		token_data.length = current - token_text;
		css_parser_(parser, token, token_data, &param);
		if (param.syntax_error) {
			LOG(("syntax error near offset %i (%s)",
					token_text - source_data,
					c->url));
			param.syntax_error = false;
		} else if (param.memory_error) {
			LOG(("out of memory"));
			break;
		}
	}

	css_parser_(parser, 0, token_data, &param);
	css_parser_Free(parser, free);

	if (param.memory_error) {
		msg_data.error = messages_get("NoMemory");
		content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
		warn_user("NoMemory", 0);
		return false;
	}

	/*css_dump_stylesheet(c->data.css.css);*/

	/* complete fetch of any imported stylesheets */
	while (c->active != 0) {
		LOG(("importing %i from '%s'", c->active, c->url));
		fetch_poll();
		gui_multitask();
	}

	c->status = CONTENT_STATUS_DONE;
	return true;
}


/**
 * Destroy a CONTENT_CSS and free all resources it owns.
 */

void css_destroy(struct content *c)
{
	unsigned int i;
	struct css_selector *r;

	for (i = 0; i != HASH_SIZE; i++) {
		for (r = c->data.css.css->rule[i]; r != 0; r = r->next) {
		        if (r->style->background_image.uri != NULL)
		                free(r->style->background_image.uri);
			free(r->style);
		}
		css_free_selector(c->data.css.css->rule[i]);
	}
	free(c->data.css.css);

	/* imported stylesheets */
	for (i = 0; i != c->data.css.import_count; i++)
		if (c->data.css.import_content[i] != 0) {
			free(c->data.css.import_url[i]);
			content_remove_user(c->data.css.import_content[i],
					css_atimport_callback, c, (void*)i);
		}
	free(c->data.css.import_url);
	free(c->data.css.import_content);
}


/**
 * Create a new struct css_node.
 *
 * \param  stylesheet  content of type CONTENT_CSS
 * \param  type  type of node
 * \param  data  string for data, not copied
 * \param  data_length  length of data
 * \return  allocated node, or 0 if memory exhausted
 *
 * Used by the parser.
 */

struct css_node * css_new_node(struct content *stylesheet,
                css_node_type type,
		const char *data, unsigned int data_length)
{
	struct css_node *node = malloc(sizeof *node);
	if (!node)
		return 0;
	node->type = type;
	node->data = data;
	node->data_length = data_length;
	node->value = 0;
	node->next = 0;
	node->comb = CSS_COMB_NONE;
	node->style = 0;
	node->specificity = 0;
	node->stylesheet = stylesheet;
	return node;
}


/**
 * Free a struct css_node recursively.
 *
 * \param  node  css_node to free
 *
 * Used by the parser.
 */

void css_free_node(struct css_node *node)
{
	if (!node)
		return;
	if (node->value)
		css_free_node(node->value);
	if (node->next)
		css_free_node(node->next);
	free(node);
}


/**
 * Create a new struct css_selector.
 *
 * \param  type  type of selector
 * \param  data  string for data, not copied
 * \param  data_length  length of data
 * \return  allocated selector, or 0 if memory exhausted
 */

struct css_selector * css_new_selector(css_selector_type type,
		const char *data, unsigned int data_length)
{
	struct css_selector *node = malloc(sizeof *node);
	if (!node)
		return 0;
	node->type = type;
	node->data = data;
	node->data_length = data_length;
	node->data2 = 0;
	node->detail = 0;
	node->combiner = 0;
	node->next = 0;
	node->comb = CSS_COMB_NONE;
	node->style = 0;
	node->specificity = 0;
	return node;
}


/**
 * Free a struct css_selector recursively.
 *
 * \param  node  css_selector to free
 */

void css_free_selector(struct css_selector *node)
{
	if (!node)
		return;
	if (node->detail)
		css_free_selector(node->detail);
	if (node->combiner)
		css_free_selector(node->combiner);
	if (node->next)
		css_free_selector(node->next);
	free(node);
}


/**
 * Process an \@import rule.
 */

void css_atimport(struct content *c, struct css_node *node)
{
	const char *s;
	char *t, *url, *url1;
	bool string = false, screen = true;
	unsigned int i;
	char **import_url;
	struct content **import_content;

	LOG(("@import rule"));

	import_url = realloc(c->data.css.import_url,
			(c->data.css.import_count + 1) *
			sizeof(*c->data.css.import_url));
	if (!import_url) {
		/** \todo report to user */
		return;
	}
	c->data.css.import_url = import_url;

	import_content = realloc(c->data.css.import_content,
			(c->data.css.import_count + 1) *
			sizeof(*c->data.css.import_content));
	if (!import_content)  {
		/** \todo report to user */
		return;
	}
	c->data.css.import_content = import_content;

	/* uri(...) or "..." */
	switch (node->type) {
		case CSS_NODE_URI:
			LOG(("URI '%.*s'", node->data_length, node->data));
			for (s = node->data + 4;
					*s == ' ' || *s == '\t' || *s == '\r' ||
					*s == '\n' || *s == '\f';
					s++)
				;
			if (*s == '\'' || *s == '"') {
				string = true;
				s++;
			}
			url = strndup(s, node->data_length - (s - node->data));
			if (!url) {
				/** \todo report to user */
				return;
			}
			for (t = url + strlen(url) - 2;
					*t == ' ' || *t == '\t' || *t == '\r' ||
					*t == '\n' || *t == '\f';
					t--)
				;
			if (string)
				*t = 0;
			else
				*(t + 1) = 0;
			break;
		case CSS_NODE_STRING:
			LOG(("STRING '%.*s'", node->data_length, node->data));
			url = strndup(node->data, node->data_length);
			if (!url) {
				/** \todo report to user */
				return;
			}
			break;
		default:
			return;
	}

	/* media not specified, 'screen', or 'all' */
	for (node = node->next; node != 0; node = node->next) {
		screen = false;
		if (node->type != CSS_NODE_IDENT) {
			free(url);
			return;
		}
		LOG(("medium '%s'", node->data));
		if ((node->data_length == 6 &&
				strncmp(node->data, "screen", 6) == 0) ||
				(node->data_length == 3 &&
				strncmp(node->data, "all", 3) == 0)) {
			screen = true;
			break;
		}
		node = node->next;
		if (node == 0 || node->type != CSS_NODE_COMMA) {
			free(url);
			return;
		}
	}
	if (!screen) {
		free(url);
		return;
	}

	url1 = url_join(url, c->url);
	if (!url1) {
		free(url);
		return;
	}

	/* start the fetch */
	c->data.css.import_count++;
	i = c->data.css.import_count - 1;
	c->data.css.import_url[i] = url1;
	c->data.css.import_content[i] = fetchcache(c->data.css.import_url[i],
			css_atimport_callback, c, (void *) i,
			c->width, c->height, true, 0, 0, false);
	if (c->data.css.import_content[i]) {
		c->active++;
		fetchcache_go(c->data.css.import_content[i], c->url,
				css_atimport_callback, c, (void *) i,
				0, 0, false);
	}

	free(url);
}


/**
 * Fetchcache callback for imported stylesheets.
 */

void css_atimport_callback(content_msg msg, struct content *css,
		void *p1, void *p2, union content_msg_data data)
{
	struct content *c = p1;
	unsigned int i = (unsigned int) p2;
	switch (msg) {
		case CONTENT_MSG_LOADING:
			if (css->type != CONTENT_CSS) {
				content_remove_user(css, css_atimport_callback, c, (void*)i);
				c->data.css.import_content[i] = 0;
				c->active--;
				content_add_error(c, "NotCSS", 0);
			}
			break;

		case CONTENT_MSG_READY:
			break;

		case CONTENT_MSG_DONE:
			LOG(("got imported stylesheet '%s'", css->url));
			/*css_dump_stylesheet(css->data.css);*/
			c->active--;
			break;

		case CONTENT_MSG_ERROR:
			c->data.css.import_content[i] = 0;
			c->active--;
			content_add_error(c, "?", 0);
			break;

		case CONTENT_MSG_STATUS:
			break;

		case CONTENT_MSG_REDIRECT:
			c->active--;
			free(c->data.css.import_url[i]);
			c->data.css.import_url[i] = strdup(data.redirect);
			if (!c->data.css.import_url[i]) {
				/** \todo report to user */
				/* c->error = 1; */
				return;
			}
			c->data.css.import_content[i] = fetchcache(
					c->data.css.import_url[i],
					css_atimport_callback, c, (void *) i,
					css->width, css->height, true, 0, 0,
					false);
			if (c->data.css.import_content[i]) {
				c->active++;
				fetchcache_go(c->data.css.import_content[i],
						c->url, css_atimport_callback,
						c, (void *) i,
						0, 0, false);
			}
			break;

		default:
			assert(0);
	}
}


/**
 * Find the style which applies to an element.
 *
 * \param  css      content of type CONTENT_CSS
 * \param  element  element in xml tree to match
 * \param  style    style to update
 *
 * The style is updated with any rules that match the element.
 */

void css_get_style(struct content *css, xmlNode *element,
		struct css_style *style)
{
	struct css_stylesheet *stylesheet = css->data.css.css;
	struct css_selector *rule;
	unsigned int hash, i;

	/* imported stylesheets */
	for (i = 0; i != css->data.css.import_count; i++)
		if (css->data.css.import_content[i] != 0)
			css_get_style(css->data.css.import_content[i],
					element, style);

	/* match rules which end with the same element */
	hash = css_hash((const char *) element->name,
			strlen((const char *) element->name));
	for (rule = stylesheet->rule[hash]; rule; rule = rule->next)
		if (css_match_rule(rule, element))
			css_merge(style, rule->style);

	/* match rules which apply to all elements */
	for (rule = stylesheet->rule[0]; rule; rule = rule->next)
		if (css_match_rule(rule, element))
			css_merge(style, rule->style);
}


/**
 * Determine if a rule applies to an element.
 */

bool css_match_rule(struct css_selector *rule, xmlNode *element)
{
	struct css_selector *detail;
	xmlNode *anc, *prev;

	assert(element->type == XML_ELEMENT_NODE);

	if (rule->data && (rule->data_length !=
			strlen((const char *) element->name) ||
			strncasecmp(rule->data, (const char *) element->name,
				rule->data_length) != 0))
		return false;

	for (detail = rule->detail; detail; detail = detail->next) {
		if (!css_match_detail(detail, element))
			return false;
	}

	if (!rule->combiner)
		return true;

	switch (rule->comb) {
		case CSS_COMB_ANCESTOR:
			for (anc = element->parent; anc; anc = anc->parent)
				if (anc->type == XML_ELEMENT_NODE &&
						css_match_rule(rule->combiner, anc))
					return true;
			break;

		case CSS_COMB_PRECEDED:
			for (prev = element->prev;
					prev && prev->type != XML_ELEMENT_NODE;
					prev = prev->prev)
				;
			if (!prev)
				return false;
			return css_match_rule(rule->combiner, prev);
			break;

		case CSS_COMB_PARENT:
			for (anc = element->parent;
					anc && anc->type != XML_ELEMENT_NODE;
					anc = anc->parent)
				;
			if (!anc)
				return false;
			return css_match_rule(rule->combiner, anc);
			break;

		default:
			assert(0);
	}

	return false;
}


/**
 * Determine if a selector detail matches an element.
 *
 * \param  detail   a css_selector of type other than CSS_SELECTOR_ELEMENT
 * \param  element  element in xml tree to match
 * \return  true if the selector matches the element
 */

bool css_match_detail(const struct css_selector *detail,
		xmlNode *element)
{
	bool match = false;
	char *s = 0;
	char *space, *word;
	unsigned int length;

	switch (detail->type) {
		case CSS_SELECTOR_ID:
			s = (char *) xmlGetProp(element,
					(const xmlChar *) "id");
			if (s && strlen(s) == detail->data_length &&
					strncasecmp(detail->data, s,
					detail->data_length) == 0)
				match = true;
			break;

		case CSS_SELECTOR_CLASS:
			s = (char *) xmlGetProp(element,
					(const xmlChar *) "class");
			if (!s)
				break;
			word = s;
			do {
				space = strchr(word, ' ');
				if (space)
					length = space - word;
				else
					length = strlen(word);
				if (length == detail->data_length &&
		   				strncasecmp(word, detail->data,
						length) == 0) {
					match = true;
					break;
				}
				word = space + 1;
			} while (space);
			break;

		case CSS_SELECTOR_ATTRIB:
			/* matches if an attribute is present */
			word = strndup(detail->data, detail->data_length);
			if (!word) {
				/** \todo report to user */
				return false;
			}
			s = (char *) xmlGetProp(element,
					(const xmlChar *) word);
			free(word);
			if (s)
				match = true;
			break;

		case CSS_SELECTOR_ATTRIB_EQ:
			/* matches if an attribute has a certain value*/
			word = strndup(detail->data, detail->data_length);
			if (!word) {
				/** \todo report to user */
				return false;
			}
			s = (char *) xmlGetProp(element,
					(const xmlChar *) word);
			free(word);
			if (s && strlen(s) == detail->data2_length &&
					strncasecmp(detail->data2, s,
					detail->data2_length) == 0)
				match = true;
			break;

		case CSS_SELECTOR_ATTRIB_INC:
			/* matches if one of the space separated words
			 * in the attribute is equal */
			word = strndup(detail->data,
					detail->data_length);
			if (!word) {
				/** \todo report to user */
				return false;
			}
			s = (char *) xmlGetProp(element,
					(const xmlChar *) word);
			free(word);
			if (!s)
				break;
			word = s;
			do {
				space = strchr(word, ' ');
				if (space)
					length = space - word;
				else
					length = strlen(word);
				if (length == detail->data2_length &&
						strncasecmp(word, detail->data2,
						length) == 0) {
					match = true;
					break;
				}
				word = space + 1;
			} while (space);
			break;

		case CSS_SELECTOR_ATTRIB_DM:
			/* matches if a prefix up to a hyphen matches */
			word = strndup(detail->data,
					detail->data_length);
			if (!word) {
				/** \todo report to user */
				return false;
			}
			s = (char *) xmlGetProp(element,
					(const xmlChar *) word);
			free(word);
			if (!s)
				break;
			length = detail->data2_length;
			if (strncasecmp(detail->data2, s, length) == 0 &&
					(s[length] == '-' || s[length] == 0))
				match = true;
			break;

		case CSS_SELECTOR_PSEUDO:
			break;

		default:
			assert(0);
	}

	if (s)
		xmlFree(s);

	return match;
}


/**
 * Parse a stand-alone CSS property list.
 *
 * \param  c      parent content
 * \param  style  css_style to update
 * \param  str    property list, as found in HTML style attributes
 */

void css_parse_property_list(struct content *c, struct css_style * style,
                            char * str)
{
	unsigned char *source_data;
	unsigned char *current, *end, *token_text;
	size_t length;
	unsigned int i;
	int token;
	void *parser;
	struct css_parser_params param = {true, c, 0, false, false};
	struct css_parser_token token_data;
	const struct css_parser_token token_start = { "{", 1 };
	const struct css_parser_token token_end = { "}", 1 };

	length = strlen(str);

	parser = css_parser_Alloc(malloc);
	source_data = malloc(length + 10);

	if (!parser || !source_data) {
		free(parser);
		css_parser_Free(parser, free);
		return;
	}

	strcpy(source_data, str);
	for (i = 0; i != 10; i++)
		source_data[length + i] = 0;

	css_parser_(parser, LBRACE, token_start, &param);

	current = source_data;
	end = source_data + strlen(str);
	while (current < end && (token = css_tokenise(&current, end + 10,
			&token_text))) {
		token_data.text = token_text;
		token_data.length = current - token_text;
		css_parser_(parser, token, token_data, &param);
		if (param.syntax_error) {
			LOG(("syntax error near offset %i",
					token_text - source_data));
			param.syntax_error = false;
		} else if (param.memory_error) {
			LOG(("out of memory"));
			break;
		}
	}
	css_parser_(parser, RBRACE, token_end, &param);
	css_parser_(parser, 0, token_data, &param);

	css_parser_Free(parser, free);

	if (param.memory_error) {
		css_free_node(param.declaration);
		return;
	}

	css_add_declarations(style, param.declaration);

	css_free_node(param.declaration);

	free(source_data);
}


/**
 * Dump a css_style to stderr in CSS-like syntax.
 */

void css_dump_style(const struct css_style * const style)
{
	unsigned int i;
	fprintf(stderr, "{ ");

#define DUMP_COLOR(z, s) \
	if (style->z != css_empty_style.z) {				\
		if (style->z == TRANSPARENT)				\
			fprintf(stderr, s ": transparent; ");		\
		else if (style->z == CSS_COLOR_NONE)			\
			fprintf(stderr, s ": none; ");			\
		else							\
			fprintf(stderr, s ": #%.6lx; ", style->z);	\
	}

#define DUMP_KEYWORD(z, s, n) \
	if (style->z != css_empty_style.z)				\
		fprintf(stderr, s ": %s; ", n[style->z]);

	DUMP_COLOR(background_color, "background-color");
	if (style->background_attachment !=
			css_empty_style.background_attachment ||
			style->background_image.type !=
			css_empty_style.background_image.type ||
			style->background_position.horz.pos !=
			css_empty_style.background_position.horz.pos ||
			style->background_position.vert.pos !=
			css_empty_style.background_position.vert.pos ||
			style->background_repeat !=
			css_empty_style.background_repeat) {
		fprintf(stderr, "background: ");
		switch (style->background_image.type) {
			case CSS_BACKGROUND_IMAGE_NONE:
				fprintf(stderr, "none");
				break;
			case CSS_BACKGROUND_IMAGE_INHERIT:
				fprintf(stderr, "inherit");
				break;
			case CSS_BACKGROUND_IMAGE_URI:
				fprintf(stderr, "(%p) \"%s\"",
						style->background_image.uri,
						style->background_image.uri);
				break;
			default:
				fprintf(stderr, "UNKNOWN");
				break;
		}
		fprintf(stderr, " %s %s ",
				css_background_repeat_name[
						style->background_repeat],
				css_background_attachment_name[
						style->background_attachment]);
		switch (style->background_position.horz.pos) {
			case CSS_BACKGROUND_POSITION_LENGTH:
				css_dump_length(&style->background_position.
						horz.value.length);
				break;
			case CSS_BACKGROUND_POSITION_PERCENT:
				fprintf(stderr, "%g%%",
						style->background_position.
						horz.value.percent);
				break;
			case CSS_BACKGROUND_POSITION_INHERIT:
				fprintf(stderr, "inherit");
				break;
			default:
				fprintf(stderr, "UNKNOWN");
				break;
		}
		fprintf(stderr, " ");
		switch (style->background_position.vert.pos) {
			case CSS_BACKGROUND_POSITION_LENGTH:
				css_dump_length(&style->background_position.
						vert.value.length);
				break;
			case CSS_BACKGROUND_POSITION_PERCENT:
				fprintf(stderr, "%g%%",
						style->background_position.
						vert.value.percent);
				break;
			case CSS_BACKGROUND_POSITION_INHERIT:
				fprintf(stderr, "inherit");
				break;
			default:
				fprintf(stderr, "UNKNOWN");
				break;
		}
		fprintf(stderr, "; ");
	}
	DUMP_KEYWORD(clear, "clear", css_clear_name);
	DUMP_COLOR(color, "color");
	DUMP_KEYWORD(cursor, "cursor", css_cursor_name);
	DUMP_KEYWORD(display, "display", css_display_name);
	DUMP_KEYWORD(float_, "float", css_float_name);

	if (style->font_style != css_empty_style.font_style ||
			style->font_weight != css_empty_style.font_weight ||
			style->font_size.size !=
					css_empty_style.font_size.size ||
			style->line_height.size !=
					css_empty_style.line_height.size ||
			style->font_family != css_empty_style.font_family ||
			style->font_variant != css_empty_style.font_variant) {
		fprintf(stderr, "font: %s %s ",
				css_font_style_name[style->font_style],
				css_font_weight_name[style->font_weight]);
		switch (style->font_size.size) {
			case CSS_FONT_SIZE_ABSOLUTE:
				fprintf(stderr, "[%g]",
					style->font_size.value.absolute);
				break;
			case CSS_FONT_SIZE_LENGTH:
				css_dump_length(&style->font_size.value.length);
				break;
			case CSS_FONT_SIZE_PERCENT:
				fprintf(stderr, "%g%%",
					style->font_size.value.percent);
				break;
			case CSS_FONT_SIZE_INHERIT:
				fprintf(stderr, "inherit");
				break;
			default:
				fprintf(stderr, "UNKNOWN");
				break;
		}
		fprintf(stderr, "/");
		switch (style->line_height.size) {
			case CSS_LINE_HEIGHT_ABSOLUTE:
				fprintf(stderr, "[%g]",
					style->line_height.value.absolute);
					break;
			case CSS_LINE_HEIGHT_LENGTH:
				css_dump_length(&style->line_height.value.length);
				break;
			case CSS_LINE_HEIGHT_PERCENT:
				fprintf(stderr, "%g%%",
					style->line_height.value.percent);
					break;
			case CSS_LINE_HEIGHT_INHERIT:
				fprintf(stderr, "inherit");
				break;
			default:
				fprintf(stderr, "UNKNOWN");
				break;
		}
		fprintf(stderr, " %s",
				css_font_family_name[style->font_family]);
		fprintf(stderr, " %s",
				css_font_variant_name[style->font_variant]);
		fprintf(stderr, "; ");
	}

	if (style->height.height != css_empty_style.height.height) {
		fprintf(stderr, "height: ");
		switch (style->height.height) {
			case CSS_HEIGHT_AUTO:
				fprintf(stderr, "auto"); break;
			case CSS_HEIGHT_LENGTH:
				css_dump_length(&style->height.length); break;
			default:
				fprintf(stderr, "UNKNOWN"); break;
		}
		fprintf(stderr, "; ");
	}

	if (style->margin[0].margin != css_empty_style.margin[0].margin ||
			style->margin[1].margin != css_empty_style.margin[1].margin ||
			style->margin[2].margin != css_empty_style.margin[2].margin ||
			style->margin[3].margin != css_empty_style.margin[3].margin) {
		fprintf(stderr, "margin:");
		for (i = 0; i != 4; i++) {
			switch (style->margin[i].margin) {
				case CSS_MARGIN_INHERIT:
					fprintf(stderr, " inherit");
					break;
				case CSS_MARGIN_LENGTH:
					fprintf(stderr, " ");
					css_dump_length(&style->margin[i].value.length);
					break;
				case CSS_MARGIN_PERCENT:
					fprintf(stderr, " %g%%",
						style->margin[i].value.percent);
					break;
				case CSS_MARGIN_AUTO:
					fprintf(stderr, " auto");
					break;
				default:
					fprintf(stderr, "UNKNOWN");
					break;
			}
		}
		fprintf(stderr, "; ");
	}

	DUMP_KEYWORD(overflow, "overflow", css_overflow_name);

	if (style->padding[0].padding != css_empty_style.padding[0].padding ||
			style->padding[1].padding != css_empty_style.padding[1].padding ||
			style->padding[2].padding != css_empty_style.padding[2].padding ||
			style->padding[3].padding != css_empty_style.padding[3].padding) {
		fprintf(stderr, "padding:");
		for (i = 0; i != 4; i++) {
			switch (style->padding[i].padding) {
				case CSS_PADDING_INHERIT:
					fprintf(stderr, " inherit");
					break;
				case CSS_PADDING_LENGTH:
					fprintf(stderr, " ");
					css_dump_length(&style->padding[i].value.length);
					break;
				case CSS_PADDING_PERCENT:
					fprintf(stderr, " %g%%",
						style->padding[i].value.percent);
					break;
				default:
					fprintf(stderr, "UNKNOWN");
					break;
			}
		}
		fprintf(stderr, "; ");
	}

	DUMP_KEYWORD(text_align, "text-align", css_text_align_name);

	if (style->text_decoration != css_empty_style.text_decoration) {
		fprintf(stderr, "text-decoration:");
		if (style->text_decoration == CSS_TEXT_DECORATION_NONE)
			fprintf(stderr, " none");
		if (style->text_decoration == CSS_TEXT_DECORATION_INHERIT)
			fprintf(stderr, " inherit");
		if (style->text_decoration & CSS_TEXT_DECORATION_UNDERLINE)
			fprintf(stderr, " underline");
		if (style->text_decoration & CSS_TEXT_DECORATION_OVERLINE)
			fprintf(stderr, " overline");
		if (style->text_decoration & CSS_TEXT_DECORATION_LINE_THROUGH)
			fprintf(stderr, " line-through");
		if (style->text_decoration & CSS_TEXT_DECORATION_BLINK)
			fprintf(stderr, " blink");
		fprintf(stderr, "; ");
	}

	if (style->text_indent.size != css_empty_style.text_indent.size) {
		fprintf(stderr, "text-indent: ");
		switch (style->text_indent.size) {
			case CSS_TEXT_INDENT_LENGTH:
				css_dump_length(&style->text_indent.value.length);
				break;
			case CSS_TEXT_INDENT_PERCENT:
				fprintf(stderr, "%g%%",
					style->text_indent.value.percent);
				break;
			case CSS_TEXT_INDENT_INHERIT:
				fprintf(stderr, "inherit");
				break;
			default:
				fprintf(stderr, "UNKNOWN");
				break;
		}
		fprintf(stderr, "; ");
	}

	DUMP_KEYWORD(text_transform, "text-transform", css_text_transform_name);
	DUMP_KEYWORD(visibility, "visibility", css_visibility_name);

	if (style->width.width != css_empty_style.width.width) {
		fprintf(stderr, "width: ");
		switch (style->width.width) {
			case CSS_WIDTH_INHERIT:
				fprintf(stderr, "inherit");
				break;
			case CSS_WIDTH_AUTO:
				fprintf(stderr, "auto");
				break;
			case CSS_WIDTH_LENGTH:
				css_dump_length(&style->width.value.length);
				break;
			case CSS_WIDTH_PERCENT:
				fprintf(stderr, "%g%%",
						style->width.value.percent);
				break;
			default:
				fprintf(stderr, "UNKNOWN");
				break;
		}
		fprintf(stderr, "; ");
	}

	DUMP_KEYWORD(white_space, "white-space", css_white_space_name);

	fprintf(stderr, "}");
}


/**
 * Dump a css_length to stderr.
 */

void css_dump_length(const struct css_length * const length)
{
	fprintf(stderr, "%g%s", length->value, css_unit_name[length->unit]);
}


/**
 * Dump a complete css_stylesheet to stderr in CSS syntax.
 */

void css_dump_stylesheet(const struct css_stylesheet * stylesheet)
{
	unsigned int i;
	struct css_selector *r;
	for (i = 0; i != HASH_SIZE; i++) {
		/*fprintf(stderr, "hash %i:\n", i);*/
		for (r = stylesheet->rule[i]; r != 0; r = r->next) {
			css_dump_selector(r);
			fprintf(stderr, " <%lx> ", r->specificity);
			css_dump_style(r->style);
			fprintf(stderr, "\n");
		}
	}
}


/**
 * Dump a css_selector to stderr in CSS syntax.
 */

void css_dump_selector(const struct css_selector *r)
{
	struct css_selector *m;

	if (r->combiner)
		css_dump_selector(r->combiner);

	switch (r->comb) {
		case CSS_COMB_NONE:     break;
		case CSS_COMB_ANCESTOR: fprintf(stderr, " "); break;
		case CSS_COMB_PARENT:   fprintf(stderr, " > "); break;
		case CSS_COMB_PRECEDED: fprintf(stderr, " + "); break;
	}

	if (r->data)
		fprintf(stderr, "%.*s", r->data_length, r->data);
	else
		fprintf(stderr, "*");

	for (m = r->detail; m; m = m->next) {
		switch (m->type) {
			case CSS_SELECTOR_ID:
				fprintf(stderr, "#%.*s",
						m->data_length, m->data);
				break;
			case CSS_SELECTOR_CLASS:
				fprintf(stderr, ".%.*s",
						m->data_length,	m->data);
				break;
			case CSS_SELECTOR_ATTRIB:
				fprintf(stderr, "[%.*s]",
						m->data_length, m->data);
				break;
			case CSS_SELECTOR_ATTRIB_EQ:
				fprintf(stderr, "[%.*s=%.*s]",
						m->data_length,	m->data,
						m->data2_length, m->data2);
				break;
			case CSS_SELECTOR_ATTRIB_INC:
				fprintf(stderr, "[%.*s~=%.*s]",
						m->data_length, m->data,
						m->data2_length, m->data2);
				break;
			case CSS_SELECTOR_ATTRIB_DM:
				fprintf(stderr, "[%.*s|=%.*s]",
						m->data_length, m->data,
						m->data2_length, m->data2);
				break;
			case CSS_SELECTOR_PSEUDO:
				fprintf(stderr, ":%.*s",
						m->data_length, m->data);
				break;
			default:
				fprintf(stderr, "(unexpected detail)");
		}
	}
}


/**
 * Cascade styles.
 *
 * \param  style  css_style to modify
 * \param  apply  css_style to cascade onto style
 *
 * Attributes which have the value 'inherit' in apply are unchanged in style.
 * Other attributes are copied to style, calculating percentages relative to
 * style where applicable.
 */

void css_cascade(struct css_style * const style,
		const struct css_style * const apply)
{
	unsigned int i;
	float f;

	/* text-decoration: approximate CSS 2.1 by inheriting into inline elements */
	if (apply->text_decoration != CSS_TEXT_DECORATION_INHERIT)
		style->text_decoration = apply->text_decoration;
/*	if (style->display == CSS_DISPLAY_INLINE && apply->display != CSS_DISPLAY_INLINE)
		style->text_decoration = CSS_TEXT_DECORATION_NONE;*/
        if (apply->background_attachment != CSS_BACKGROUND_ATTACHMENT_INHERIT)
                style->background_attachment = apply->background_attachment;
	if (apply->background_color != CSS_COLOR_INHERIT)
		style->background_color = apply->background_color;
	if (apply->background_image.type != CSS_BACKGROUND_IMAGE_INHERIT)
		style->background_image = apply->background_image;
	if (apply->background_repeat != CSS_BACKGROUND_REPEAT_INHERIT)
		style->background_repeat = apply->background_repeat;
	if (apply->clear != CSS_CLEAR_INHERIT)
		style->clear = apply->clear;
	if (apply->color != CSS_COLOR_INHERIT)
		style->color = apply->color;
	if (apply->cursor != CSS_CURSOR_INHERIT)
		style->cursor = apply->cursor;
	if (apply->display != CSS_DISPLAY_INHERIT)
		style->display = apply->display;
	if (apply->float_ != CSS_FLOAT_INHERIT)
		style->float_ = apply->float_;
	if (apply->font_family != CSS_FONT_FAMILY_INHERIT)
	        style->font_family = apply->font_family;
	if (apply->font_style != CSS_FONT_STYLE_INHERIT)
		style->font_style = apply->font_style;
	if (apply->font_weight != CSS_FONT_WEIGHT_INHERIT)
		style->font_weight = apply->font_weight;
        if (apply->font_variant != CSS_FONT_VARIANT_INHERIT)
		style->font_variant = apply->font_variant;
	if (apply->height.height != CSS_HEIGHT_INHERIT)
		style->height = apply->height;
	if (apply->line_height.size != CSS_LINE_HEIGHT_INHERIT)
		style->line_height = apply->line_height;
	if (apply->overflow != CSS_OVERFLOW_INHERIT)
		style->overflow = apply->overflow;
	if (apply->text_align != CSS_TEXT_ALIGN_INHERIT)
		style->text_align = apply->text_align;
	if (apply->text_indent.size != CSS_TEXT_INDENT_INHERIT)
	        style->text_indent = apply->text_indent;
	if (apply->text_transform != CSS_TEXT_TRANSFORM_INHERIT)
		style->text_transform = apply->text_transform;
	if (apply->visibility != CSS_VISIBILITY_INHERIT)
		style->visibility = apply->visibility;
	if (apply->width.width != CSS_WIDTH_INHERIT)
		style->width = apply->width;
	if (apply->white_space != CSS_WHITE_SPACE_INHERIT)
		style->white_space = apply->white_space;

        /* background-position */
        if (apply->background_position.horz.pos != CSS_BACKGROUND_POSITION_INHERIT) {
                style->background_position.horz = apply->background_position.horz;
        }
        if (apply->background_position.vert.pos != CSS_BACKGROUND_POSITION_INHERIT) {
                style->background_position.vert = apply->background_position.vert;
        }

	/* font-size */
	f = apply->font_size.value.percent / 100;
	switch (apply->font_size.size) {
		case CSS_FONT_SIZE_ABSOLUTE:
			style->font_size = apply->font_size;
			break;
		case CSS_FONT_SIZE_LENGTH:
			switch (apply->font_size.value.length.unit) {
				case CSS_UNIT_EM:
					f = apply->font_size.value.length.value;
					break;
				case CSS_UNIT_EX:
					f = apply->font_size.value.length.value * 0.6 /*?*/;
					break;
				default:
					style->font_size = apply->font_size;
			}
			if ((apply->font_size.value.length.unit != CSS_UNIT_EM) &&
			    (apply->font_size.value.length.unit != CSS_UNIT_EX))
				break;
			/* drop through if EM or EX */
		case CSS_FONT_SIZE_PERCENT:
			switch (style->font_size.size) {
				case CSS_FONT_SIZE_ABSOLUTE:
					style->font_size.value.absolute *= f;
					break;
				case CSS_FONT_SIZE_LENGTH:
					style->font_size.value.length.value *= f;
					break;
				default:
					die("attempting percentage of unknown font-size");
			}
			break;
		case CSS_FONT_SIZE_INHERIT:
		default:                     /* leave unchanged */
			break;
	}

	for (i = 0; i != 4; i++) {
		if (apply->border[i].color != CSS_COLOR_INHERIT)
			style->border[i].color = apply->border[i].color;
		if (apply->border[i].width.width != CSS_BORDER_WIDTH_INHERIT)
			style->border[i].width = apply->border[i].width;
		if (apply->border[i].style != CSS_BORDER_STYLE_INHERIT)
			style->border[i].style = apply->border[i].style;

		if (apply->margin[i].margin != CSS_MARGIN_INHERIT)
			style->margin[i] = apply->margin[i];

		if (apply->padding[i].padding != CSS_PADDING_INHERIT)
			style->padding[i] = apply->padding[i];
	}
}


/**
 * Merge styles.
 *
 * \param  style  css_style to modify
 * \param  apply  css_style to merge onto style
 *
 * Attributes which have the value 'inherit' in apply are unchanged in style.
 * Other attributes are copied to style, overwriting it.
 */

void css_merge(struct css_style * const style,
		const struct css_style * const apply)
{
	unsigned int i;

        if (apply->background_attachment != CSS_BACKGROUND_ATTACHMENT_INHERIT)
                style->background_attachment = apply->background_attachment;
	if (apply->background_color != CSS_COLOR_INHERIT)
		style->background_color = apply->background_color;
	if (apply->background_image.type != CSS_BACKGROUND_IMAGE_INHERIT)
		style->background_image = apply->background_image;
	if (apply->background_repeat != CSS_BACKGROUND_REPEAT_INHERIT)
		style->background_repeat = apply->background_repeat;
	if (apply->clear != CSS_CLEAR_INHERIT)
		style->clear = apply->clear;
	if (apply->color != CSS_COLOR_INHERIT)
		style->color = apply->color;
	if (apply->cursor != CSS_CURSOR_INHERIT)
		style->cursor = apply->cursor;
	if (apply->display != CSS_DISPLAY_INHERIT)
		style->display = apply->display;
	if (apply->float_ != CSS_FLOAT_INHERIT)
		style->float_ = apply->float_;
	if (apply->font_family != CSS_FONT_FAMILY_INHERIT)
	        style->font_family = apply->font_family;
	if (apply->font_size.size != CSS_FONT_SIZE_INHERIT)
		style->font_size = apply->font_size;
	if (apply->font_style != CSS_FONT_STYLE_INHERIT)
		style->font_style = apply->font_style;
	if (apply->font_weight != CSS_FONT_WEIGHT_INHERIT)
		style->font_weight = apply->font_weight;
	if (apply->font_variant != CSS_FONT_VARIANT_INHERIT)
		style->font_variant = apply->font_variant;
	if (apply->height.height != CSS_HEIGHT_INHERIT)
		style->height = apply->height;
	if (apply->line_height.size != CSS_LINE_HEIGHT_INHERIT)
		style->line_height = apply->line_height;
	if (apply->overflow != CSS_OVERFLOW_INHERIT)
		style->overflow = apply->overflow;
	if (apply->text_align != CSS_TEXT_ALIGN_INHERIT)
		style->text_align = apply->text_align;
	if (apply->text_decoration != CSS_TEXT_DECORATION_INHERIT)
		style->text_decoration = apply->text_decoration;
	if (apply->text_indent.size != CSS_TEXT_INDENT_INHERIT)
	        style->text_indent = apply->text_indent;
	if (apply->text_transform != CSS_TEXT_TRANSFORM_INHERIT)
		style->text_transform = apply->text_transform;
	if (apply->visibility != CSS_VISIBILITY_INHERIT)
		style->visibility = apply->visibility;
	if (apply->width.width != CSS_WIDTH_INHERIT)
		style->width = apply->width;
	if (apply->white_space != CSS_WHITE_SPACE_INHERIT)
		style->white_space = apply->white_space;

	/* background-position */
        if (apply->background_position.horz.pos != CSS_BACKGROUND_POSITION_INHERIT) {
                style->background_position.horz = apply->background_position.horz;
        }
        if (apply->background_position.vert.pos != CSS_BACKGROUND_POSITION_INHERIT) {
                style->background_position.vert = apply->background_position.vert;
        }

	for (i = 0; i != 4; i++) {
		if (apply->border[i].color != CSS_COLOR_INHERIT)
			style->border[i].color = apply->border[i].color;
		if (apply->border[i].width.width != CSS_BORDER_WIDTH_INHERIT)
			style->border[i].width = apply->border[i].width;
		if (apply->border[i].style != CSS_BORDER_STYLE_INHERIT)
			style->border[i].style = apply->border[i].style;

		if (apply->margin[i].margin != CSS_MARGIN_INHERIT)
			style->margin[i] = apply->margin[i];

		if (apply->padding[i].padding != CSS_PADDING_INHERIT)
			style->padding[i] = apply->padding[i];
	}
}


/**
 * Calculate a hash for an element name.
 *
 * The hash is case-insensitive.
 */

unsigned int css_hash(const char *s, int length)
{
	int i;
	unsigned int z = 0;
	if (s == 0)
		return 0;
	for (i = 0; i != length; i++)
		z += s[i] & 0x1f;  /* lower 5 bits, case insensitive */
	return (z % (HASH_SIZE - 1)) + 1;
}


/**
 * Convert a struct css_length to pixels.
 */

float len(struct css_length * length, struct css_style * style)
{
	assert(!((length->unit == CSS_UNIT_EM || length->unit == CSS_UNIT_EX) && style == 0));
	switch (length->unit) {
		case CSS_UNIT_EM: return length->value * len(&style->font_size.value.length, 0);
		case CSS_UNIT_EX: return length->value * len(&style->font_size.value.length, 0) * 0.6;
		case CSS_UNIT_PX: return length->value;
		case CSS_UNIT_IN: return length->value * 90.0;
		case CSS_UNIT_CM: return length->value * 35.0;
		case CSS_UNIT_MM: return length->value * 3.5;
		case CSS_UNIT_PT: return length->value * 90.0 / 72.0;
		case CSS_UNIT_PC: return length->value * 90.0 / 6.0;
		default: break;
	}
	return 0;
}


#ifdef DEBUG

int main()
{
	const char data[] = "h1 { blah: foo; display: block; }"
		"h1.c1 h2#id1 + h3, h4 h5.c2#id2 { size: 100mm; color: red }"
		"p { background-color: #123; clear: left; color: #ff0000; display: block;"
		"float: left; font-size: 150%; height: blah; line-height: 100;"
		"text-align: left right; width: 90%;}";
	struct content c;
	css_create(&c);
	css_process_data(&c, data, 24);
	css_process_data(&c, data + 24, sizeof(data) - 25);
	css_convert(&c, 100, 100);
	return 0;
}

#endif