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
|
/*
* This file is part of the DOM implementation for KDE.
*
* Copyright (C) 2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
* Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
// #define CSS_DEBUG
// #define TOKEN_DEBUG
#define YYDEBUG 0
#include <kdebug.h>
#include <tdeglobal.h>
#include <kurl.h>
#include "cssparser.h"
#include "css_valueimpl.h"
#include "css_ruleimpl.h"
#include "css_stylesheetimpl.h"
#include "cssproperties.h"
#include "cssvalues.h"
#include "misc/helper.h"
#include "csshelper.h"
using namespace DOM;
#include <stdlib.h>
#include <assert.h>
// used to promote background: left to left center
#define BACKGROUND_SKIP_CENTER( num ) \
if ( !pos_ok[ num ] && expected != 1 ) { \
pos_ok[num] = true; \
pos[num] = 0; \
skip_next = false; \
}
ValueList::~ValueList()
{
unsigned numValues = m_values.size();
for (unsigned i = 0; i < numValues; i++)
if (m_values[i].unit == Value::Function)
delete m_values[i].function;
}
namespace {
class ShorthandScope {
public:
ShorthandScope(CSSParser* parser, int propId) : m_parser(parser)
{
if (!(m_parser->m_inParseShorthand++))
m_parser->m_currentShorthand = propId;
}
~ShorthandScope()
{
if (!(--m_parser->m_inParseShorthand))
m_parser->m_currentShorthand = 0;
}
private:
CSSParser* m_parser;
};
}
using namespace DOM;
#if YYDEBUG > 0
extern int cssyydebug;
#endif
extern int cssyyparse( void * parser );
CSSParser *CSSParser::currentParser = 0;
CSSParser::CSSParser( bool strictParsing )
{
#ifdef CSS_DEBUG
kdDebug( 6080 ) << "CSSParser::CSSParser this=" << this << endl;
#endif
strict = strictParsing;
parsedProperties = (CSSProperty **) malloc( 32 * sizeof( CSSProperty * ) );
numParsedProperties = 0;
maxParsedProperties = 32;
data = 0;
valueList = 0;
rule = 0;
id = 0;
important = false;
nonCSSHint = false;
m_inParseShorthand = 0;
m_currentShorthand = 0;
m_implicitShorthand = false;
yy_start = 1;
#if YYDEBUG > 0
cssyydebug = 1;
#endif
}
CSSParser::~CSSParser()
{
if ( numParsedProperties )
clearProperties();
free( parsedProperties );
delete valueList;
#ifdef CSS_DEBUG
kdDebug( 6080 ) << "CSSParser::~CSSParser this=" << this << endl;
#endif
free( data );
}
unsigned int CSSParser::defaultNamespace()
{
if (styleElement && styleElement->isCSSStyleSheet())
return static_cast<CSSStyleSheetImpl*>(styleElement)->defaultNamespace();
else
return anyNamespace;
}
void CSSParser::runParser(int length)
{
data[length-1] = 0;
data[length-2] = 0;
data[length-3] = ' ';
yyTok = -1;
block_nesting = 0;
yy_hold_char = 0;
yyleng = 0;
yytext = yy_c_buf_p = data;
yy_hold_char = *yy_c_buf_p;
CSSParser *old = currentParser;
currentParser = this;
cssyyparse( this );
currentParser = old;
}
void CSSParser::parseSheet( CSSStyleSheetImpl *sheet, const DOMString &string )
{
styleElement = sheet;
int length = string.length() + 3;
data = (unsigned short *)malloc( length *sizeof( unsigned short ) );
memcpy( data, string.unicode(), string.length()*sizeof( unsigned short) );
#ifdef CSS_DEBUG
kdDebug( 6080 ) << ">>>>>>> start parsing style sheet" << endl;
#endif
runParser(length);
#ifdef CSS_DEBUG
kdDebug( 6080 ) << "<<<<<<< done parsing style sheet" << endl;
#endif
delete rule;
rule = 0;
}
CSSRuleImpl *CSSParser::parseRule( DOM::CSSStyleSheetImpl *sheet, const DOM::DOMString &string )
{
styleElement = sheet;
const char tdehtml_rule[] = "@-khtml-rule{";
int length = string.length() + 4 + strlen(tdehtml_rule);
assert( !data );
data = (unsigned short *)malloc( length *sizeof( unsigned short ) );
for ( unsigned int i = 0; i < strlen(tdehtml_rule); i++ )
data[i] = tdehtml_rule[i];
memcpy( data + strlen( tdehtml_rule ), string.unicode(), string.length()*sizeof( unsigned short) );
// tqDebug("parse string = '%s'", TQConstString( (const TQChar *)data, length ).string().latin1() );
data[length-4] = '}';
runParser(length);
CSSRuleImpl *result = rule;
rule = 0;
return result;
}
bool CSSParser::parseValue( DOM::CSSStyleDeclarationImpl *declaration, int _id, const DOM::DOMString &string,
bool _important, bool _nonCSSHint )
{
#ifdef CSS_DEBUG
kdDebug( 6080 ) << "CSSParser::parseValue: id=" << _id << " important=" << _important
<< " nonCSSHint=" << _nonCSSHint << " value='" << string.string() << "'" << endl;
#endif
styleElement = declaration->stylesheet();
const char tdehtml_value[] = "@-khtml-value{";
int length = string.length() + 4 + strlen(tdehtml_value);
assert( !data );
data = (unsigned short *)malloc( length *sizeof( unsigned short ) );
for ( unsigned int i = 0; i < strlen(tdehtml_value); i++ )
data[i] = tdehtml_value[i];
memcpy( data + strlen( tdehtml_value ), string.unicode(), string.length()*sizeof( unsigned short) );
data[length-4] = '}';
// tqDebug("parse string = '%s'", TQConstString( (const TQChar *)data, length ).string().latin1() );
id = _id;
important = _important;
nonCSSHint = _nonCSSHint;
runParser(length);
delete rule;
rule = 0;
bool ok = false;
if ( numParsedProperties ) {
ok = true;
for ( int i = 0; i < numParsedProperties; i++ ) {
declaration->removeProperty(parsedProperties[i]->m_id, nonCSSHint);
declaration->values()->append( parsedProperties[i] );
}
numParsedProperties = 0;
}
return ok;
}
bool CSSParser::parseDeclaration( DOM::CSSStyleDeclarationImpl *declaration, const DOM::DOMString &string,
bool _nonCSSHint )
{
#ifdef CSS_DEBUG
kdDebug( 6080 ) << "CSSParser::parseDeclaration: nonCSSHint=" << nonCSSHint
<< " value='" << string.string() << "'" << endl;
#endif
styleElement = declaration->stylesheet();
const char tdehtml_decls[] = "@-khtml-decls{";
int length = string.length() + 4 + strlen(tdehtml_decls);
assert( !data );
data = (unsigned short *)malloc( length *sizeof( unsigned short ) );
for ( unsigned int i = 0; i < strlen(tdehtml_decls); i++ )
data[i] = tdehtml_decls[i];
memcpy( data + strlen( tdehtml_decls ), string.unicode(), string.length()*sizeof( unsigned short) );
data[length-4] = '}';
nonCSSHint = _nonCSSHint;
runParser(length);
delete rule;
rule = 0;
bool ok = false;
if ( numParsedProperties ) {
ok = true;
for ( int i = 0; i < numParsedProperties; i++ ) {
declaration->removeProperty(parsedProperties[i]->m_id, false);
declaration->values()->append( parsedProperties[i] );
}
numParsedProperties = 0;
}
return ok;
}
void CSSParser::addProperty( int propId, CSSValueImpl *value, bool important )
{
CSSProperty *prop = new CSSProperty;
prop->m_id = propId;
prop->setValue( value );
prop->m_important = important;
prop->nonCSSHint = nonCSSHint;
if ( numParsedProperties >= maxParsedProperties ) {
maxParsedProperties += 32;
parsedProperties = (CSSProperty **) realloc( parsedProperties,
maxParsedProperties*sizeof( CSSProperty * ) );
}
parsedProperties[numParsedProperties++] = prop;
}
CSSStyleDeclarationImpl *CSSParser::createStyleDeclaration( CSSStyleRuleImpl *rule )
{
TQPtrList<CSSProperty> *propList = new TQPtrList<CSSProperty>;
propList->setAutoDelete( true );
for ( int i = 0; i < numParsedProperties; i++ )
propList->append( parsedProperties[i] );
numParsedProperties = 0;
return new CSSStyleDeclarationImpl(rule, propList);
}
void CSSParser::clearProperties()
{
for ( int i = 0; i < numParsedProperties; i++ )
delete parsedProperties[i];
numParsedProperties = 0;
}
DOM::DocumentImpl *CSSParser::document() const
{
const StyleBaseImpl* root = styleElement;
DocumentImpl *doc = 0;
while (root->parent())
root = root->parent();
if (root->isCSSStyleSheet())
doc = static_cast<const CSSStyleSheetImpl*>(root)->doc();
return doc;
}
// defines units allowed for a certain property, used in parseUnit
enum Units
{
FUnknown = 0x0000,
FInteger = 0x0001,
FNumber = 0x0002, // Real Numbers
FPercent = 0x0004,
FLength = 0x0008,
FAngle = 0x0010,
FTime = 0x0020,
FFrequency = 0x0040,
FRelative = 0x0100,
FNonNeg = 0x0200
};
static bool validUnit( Value *value, int unitflags, bool strict )
{
if ( unitflags & FNonNeg && value->fValue < 0 )
return false;
bool b = false;
switch( value->unit ) {
case CSSPrimitiveValue::CSS_NUMBER:
b = (unitflags & FNumber);
if ( !b && ( (unitflags & FLength) && (value->fValue == 0 || !strict ) ) ) {
value->unit = CSSPrimitiveValue::CSS_PX;
b = true;
}
if (!b && (unitflags & FInteger) && value->isInt)
b = true;
break;
case CSSPrimitiveValue::CSS_PERCENTAGE:
b = (unitflags & FPercent);
break;
case Value::Q_EMS:
case CSSPrimitiveValue::CSS_EMS:
case CSSPrimitiveValue::CSS_EXS:
case CSSPrimitiveValue::CSS_PX:
case CSSPrimitiveValue::CSS_CM:
case CSSPrimitiveValue::CSS_MM:
case CSSPrimitiveValue::CSS_IN:
case CSSPrimitiveValue::CSS_PT:
case CSSPrimitiveValue::CSS_PC:
b = (unitflags & FLength);
break;
case CSSPrimitiveValue::CSS_MS:
case CSSPrimitiveValue::CSS_S:
b = (unitflags & FTime);
break;
case CSSPrimitiveValue::CSS_DEG:
case CSSPrimitiveValue::CSS_RAD:
case CSSPrimitiveValue::CSS_GRAD:
case CSSPrimitiveValue::CSS_HZ:
case CSSPrimitiveValue::CSS_KHZ:
case CSSPrimitiveValue::CSS_DIMENSION:
default:
break;
}
return b;
}
bool CSSParser::parseValue( int propId, bool important )
{
if ( !valueList ) return false;
Value *value = valueList->current();
if ( !value )
return false;
int id = value->id;
int num = inShorthand() ? 1 : valueList->size();
if ( id == CSS_VAL_INHERIT ) {
if (num != 1)
return false;
addProperty( propId, new CSSInheritedValueImpl(), important );
return true;
} else if (id == CSS_VAL_INITIAL ) {
if (num != 1)
return false;
addProperty(propId, new CSSInitialValueImpl(), important);
return true;
}
bool valid_primitive = false;
CSSValueImpl *parsedValue = 0;
switch(propId) {
/* The comment to the left defines all valid value of this properties as defined
* in CSS 2, Appendix F. Property index
*/
/* All the CSS properties are not supported by the renderer at the moment.
* Note that all the CSS2 Aural properties are only checked, if CSS_AURAL is defined
* (see parseAuralValues). As we don't support them at all this seems reasonable.
*/
case CSS_PROP_SIZE: // <length>{1,2} | auto | portrait | landscape | inherit
// case CSS_PROP_PAGE: // <identifier> | auto // ### CHECK
// ### To be done
if (id)
valid_primitive = true;
break;
case CSS_PROP_UNICODE_BIDI: // normal | embed | bidi-override | inherit
if ( id == CSS_VAL_NORMAL ||
id == CSS_VAL_EMBED ||
id == CSS_VAL_BIDI_OVERRIDE )
valid_primitive = true;
break;
case CSS_PROP_POSITION: // static | relative | absolute | fixed | inherit
if ( id == CSS_VAL_STATIC ||
id == CSS_VAL_RELATIVE ||
id == CSS_VAL_ABSOLUTE ||
id == CSS_VAL_FIXED )
valid_primitive = true;
break;
case CSS_PROP_PAGE_BREAK_AFTER: // auto | always | avoid | left | right | inherit
case CSS_PROP_PAGE_BREAK_BEFORE: // auto | always | avoid | left | right | inherit
if ( id == CSS_VAL_AUTO ||
id == CSS_VAL_ALWAYS ||
id == CSS_VAL_AVOID ||
id == CSS_VAL_LEFT ||
id == CSS_VAL_RIGHT )
valid_primitive = true;
break;
case CSS_PROP_PAGE_BREAK_INSIDE: // avoid | auto | inherit
if ( id == CSS_VAL_AUTO ||
id == CSS_VAL_AVOID )
valid_primitive = true;
break;
case CSS_PROP_EMPTY_CELLS: // show | hide | inherit
if ( id == CSS_VAL_SHOW ||
id == CSS_VAL_HIDE )
valid_primitive = true;
break;
case CSS_PROP_QUOTES: // [<string> <string>]+ | none | inherit
if (id == CSS_VAL_NONE) {
valid_primitive = true;
} else {
QuotesValueImpl *quotes = new QuotesValueImpl;
bool is_valid = true;
TQString open, close;
Value *val=valueList->current();
while (val) {
if (val->unit == CSSPrimitiveValue::CSS_STRING)
open = qString(val->string);
else {
is_valid = false;
break;
}
valueList->next();
val=valueList->current();
if (val && val->unit == CSSPrimitiveValue::CSS_STRING)
close = qString(val->string);
else {
is_valid = false;
break;
}
quotes->addLevel(open, close);
valueList->next();
val=valueList->current();
}
if (is_valid)
parsedValue = quotes;
else
delete quotes;
}
break;
case CSS_PROP_CONTENT: // normal | none | inherit |
// [ <string> | <uri> | <counter> | attr(X) | open-quote | close-quote | no-open-quote | no-close-quote ]+
if ( id == CSS_VAL_NORMAL || id == CSS_VAL_NONE)
valid_primitive = true;
else
return parseContent( propId, important );
break;
case CSS_PROP_WHITE_SPACE: // normal | pre | nowrap | pre-wrap | pre-line | inherit
if ( id == CSS_VAL_NORMAL ||
id == CSS_VAL_PRE ||
id == CSS_VAL_PRE_WRAP ||
id == CSS_VAL_PRE_LINE ||
id == CSS_VAL_NOWRAP )
valid_primitive = true;
break;
case CSS_PROP_CLIP: // <shape> | auto | inherit
if ( id == CSS_VAL_AUTO )
valid_primitive = true;
else if ( value->unit == Value::Function )
return parseShape( propId, important );
break;
/* Start of supported CSS properties with validation. This is needed for parseShortHand to work
* correctly and allows optimization in tdehtml::applyRule(..)
*/
case CSS_PROP_CAPTION_SIDE: // top | bottom | left | right | inherit
// Left and right were deprecated in CSS 2.1 and never supported by TDEHTML
if ( /* id == CSS_VAL_LEFT || id == CSS_VAL_RIGHT || */
id == CSS_VAL_TOP || id == CSS_VAL_BOTTOM)
valid_primitive = true;
break;
case CSS_PROP_BORDER_COLLAPSE: // collapse | separate | inherit
if ( id == CSS_VAL_COLLAPSE || id == CSS_VAL_SEPARATE )
valid_primitive = true;
break;
case CSS_PROP_VISIBILITY: // visible | hidden | collapse | inherit
if (id == CSS_VAL_VISIBLE || id == CSS_VAL_HIDDEN || id == CSS_VAL_COLLAPSE)
valid_primitive = true;
break;
case CSS_PROP_OVERFLOW: // visible | hidden | scroll | auto | marquee | inherit
case CSS_PROP_OVERFLOW_X:
case CSS_PROP_OVERFLOW_Y:
if (id == CSS_VAL_VISIBLE || id == CSS_VAL_HIDDEN || id == CSS_VAL_SCROLL || id == CSS_VAL_AUTO ||
id == CSS_VAL_MARQUEE)
valid_primitive = true;
break;
case CSS_PROP_LIST_STYLE_POSITION: // inside | outside | inherit
if ( id == CSS_VAL_INSIDE || id == CSS_VAL_OUTSIDE )
valid_primitive = true;
break;
case CSS_PROP_LIST_STYLE_TYPE:
// disc | circle | square | decimal | decimal-leading-zero | lower-roman |
// upper-roman | lower-greek | lower-alpha | lower-latin | upper-alpha |
// upper-latin | hebrew | armenian | georgian | cjk-ideographic | hiragana |
// katakana | hiragana-iroha | katakana-iroha | none | inherit
if ((id >= CSS_VAL_DISC && id <= CSS_VAL__TDEHTML_CLOSE_QUOTE) || id == CSS_VAL_NONE)
valid_primitive = true;
break;
case CSS_PROP_DISPLAY:
// inline | block | list-item | run-in | inline-block | -khtml-ruler | table |
// inline-table | table-row-group | table-header-group | table-footer-group | table-row |
// table-column-group | table-column | table-cell | table-caption | none | inherit
if ((id >= CSS_VAL_INLINE && id <= CSS_VAL_TABLE_CAPTION) || id == CSS_VAL_NONE)
valid_primitive = true;
break;
case CSS_PROP_DIRECTION: // ltr | rtl | inherit
if ( id == CSS_VAL_LTR || id == CSS_VAL_RTL )
valid_primitive = true;
break;
case CSS_PROP_TEXT_TRANSFORM: // capitalize | uppercase | lowercase | none | inherit
if ((id >= CSS_VAL_CAPITALIZE && id <= CSS_VAL_LOWERCASE) || id == CSS_VAL_NONE)
valid_primitive = true;
break;
case CSS_PROP_FLOAT: // left | right | none | tdehtml_left | tdehtml_right | inherit + center for buggy CSS
if ( id == CSS_VAL_LEFT || id == CSS_VAL_RIGHT || id == CSS_VAL__TDEHTML_LEFT ||
id == CSS_VAL__TDEHTML_RIGHT ||id == CSS_VAL_NONE || id == CSS_VAL_CENTER)
valid_primitive = true;
break;
case CSS_PROP_CLEAR: // none | left | right | both | inherit
if ( id == CSS_VAL_NONE || id == CSS_VAL_LEFT ||
id == CSS_VAL_RIGHT|| id == CSS_VAL_BOTH)
valid_primitive = true;
break;
case CSS_PROP_TEXT_ALIGN:
// left | right | center | justify | tdehtml_left | tdehtml_right | tdehtml_center | <string> | inherit
if ( ( id >= CSS_VAL__TDEHTML_AUTO && id <= CSS_VAL__TDEHTML_CENTER ) ||
value->unit == CSSPrimitiveValue::CSS_STRING )
valid_primitive = true;
break;
case CSS_PROP_OUTLINE_STYLE: // <border-style> | inherit
case CSS_PROP_BORDER_TOP_STYLE: //// <border-style> | inherit
case CSS_PROP_BORDER_RIGHT_STYLE: // Defined as: none | hidden | dotted | dashed |
case CSS_PROP_BORDER_BOTTOM_STYLE: // solid | double | groove | ridge | inset | outset | -khtml-native
case CSS_PROP_BORDER_LEFT_STYLE: ////
if (id >= CSS_VAL__TDEHTML_NATIVE && id <= CSS_VAL_DOUBLE)
valid_primitive = true;
break;
case CSS_PROP_FONT_WEIGHT: // normal | bold | bolder | lighter | 100 | 200 | 300 | 400 |
// 500 | 600 | 700 | 800 | 900 | inherit
if (id >= CSS_VAL_NORMAL && id <= CSS_VAL_900) {
// Allready correct id
valid_primitive = true;
} else if ( validUnit( value, FInteger|FNonNeg, false ) ) {
int weight = (int)value->fValue;
if ( (weight % 100) )
break;
weight /= 100;
if ( weight >= 1 && weight <= 9 ) {
id = CSS_VAL_100 + weight - 1;
valid_primitive = true;
}
}
break;
case CSS_PROP_BORDER_SPACING:
{
const int properties[2] = { CSS_PROP__TDEHTML_BORDER_HORIZONTAL_SPACING,
CSS_PROP__TDEHTML_BORDER_VERTICAL_SPACING };
if (num == 1) {
ShorthandScope scope(this, CSS_PROP_BORDER_SPACING);
if (!parseValue(properties[0], important)) return false;
CSSValueImpl* value = parsedProperties[numParsedProperties-1]->value();
addProperty(properties[1], value, important);
return true;
}
else if (num == 2) {
ShorthandScope scope(this, CSS_PROP_BORDER_SPACING);
if (!parseValue(properties[0], important)) return false;
if (!parseValue(properties[1], important)) return false;
return true;
}
return false;
}
case CSS_PROP__TDEHTML_BORDER_HORIZONTAL_SPACING:
case CSS_PROP__TDEHTML_BORDER_VERTICAL_SPACING:
valid_primitive = validUnit(value, FLength|FNonNeg, strict&(!nonCSSHint));
break;
case CSS_PROP_SCROLLBAR_FACE_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_SHADOW_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_HIGHLIGHT_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_3DLIGHT_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_DARKSHADOW_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_TRACK_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_ARROW_COLOR: // IE5.5
case CSS_PROP_SCROLLBAR_BASE_COLOR: // IE5.5
if ( strict )
break;
/* nobreak */
case CSS_PROP_OUTLINE_COLOR: // <color> | invert | inherit
// outline has "invert" as additional keyword.
if ( propId == CSS_PROP_OUTLINE_COLOR && id == CSS_VAL_INVERT ) {
valid_primitive = true;
break;
}
/* nobreak */
case CSS_PROP_BACKGROUND_COLOR: // <color> | inherit
case CSS_PROP_BORDER_TOP_COLOR: // <color> | inherit
case CSS_PROP_BORDER_RIGHT_COLOR: // <color> | inherit
case CSS_PROP_BORDER_BOTTOM_COLOR: // <color> | inherit
case CSS_PROP_BORDER_LEFT_COLOR: // <color> | inherit
case CSS_PROP_COLOR: // <color> | inherit
if ( id == CSS_VAL__TDEHTML_TEXT || id == CSS_VAL_MENU ||
(id >= CSS_VAL_AQUA && id <= CSS_VAL_WINDOWTEXT ) ||
id == CSS_VAL_TRANSPARENT ||
(id >= CSS_VAL_GREY && id < CSS_VAL__TDEHTML_TEXT && (nonCSSHint|!strict) ) ) {
valid_primitive = true;
} else {
parsedValue = parseColor();
if ( parsedValue )
valueList->next();
}
break;
case CSS_PROP_CURSOR:
// [ auto | crosshair | default | pointer | progress | move | e-resize | ne-resize |
// nw-resize | n-resize | se-resize | sw-resize | s-resize | w-resize | text |
// wait | help ] ] | inherit
// MSIE 5 compatibility :/
if ( !strict && id == CSS_VAL_HAND ) {
id = CSS_VAL_POINTER;
valid_primitive = true;
} else if ( id >= CSS_VAL_AUTO && id <= CSS_VAL_HELP )
valid_primitive = true;
break;
case CSS_PROP_BACKGROUND_ATTACHMENT:
case CSS_PROP__TDEHTML_BACKGROUND_CLIP:
case CSS_PROP_BACKGROUND_IMAGE:
case CSS_PROP__TDEHTML_BACKGROUND_ORIGIN:
case CSS_PROP_BACKGROUND_POSITION:
case CSS_PROP_BACKGROUND_POSITION_X:
case CSS_PROP_BACKGROUND_POSITION_Y:
case CSS_PROP__TDEHTML_BACKGROUND_SIZE:
case CSS_PROP_BACKGROUND_REPEAT: {
CSSValueImpl *val1 = 0, *val2 = 0;
int propId1, propId2;
if (parseBackgroundProperty(propId, propId1, propId2, val1, val2)) {
addProperty(propId1, val1, important);
if (val2)
addProperty(propId2, val2, important);
return true;
}
return false;
}
case CSS_PROP_LIST_STYLE_IMAGE: // <uri> | none | inherit
if (id == CSS_VAL_NONE) {
parsedValue = new CSSImageValueImpl();
valueList->next();
}
else if (value->unit == CSSPrimitiveValue::CSS_URI ) {
// ### allow string in non strict mode?
DOMString uri = tdehtml::parseURL( domString( value->string ) );
if (!uri.isEmpty()) {
parsedValue = new CSSImageValueImpl(
DOMString(KURL( styleElement->baseURL(), uri.string()).url()),
styleElement );
valueList->next();
}
}
break;
case CSS_PROP_OUTLINE_WIDTH: // <border-width> | inherit
case CSS_PROP_BORDER_TOP_WIDTH: //// <border-width> | inherit
case CSS_PROP_BORDER_RIGHT_WIDTH: // Which is defined as
case CSS_PROP_BORDER_BOTTOM_WIDTH: // thin | medium | thick | <length>
case CSS_PROP_BORDER_LEFT_WIDTH: ////
if (id == CSS_VAL_THIN || id == CSS_VAL_MEDIUM || id == CSS_VAL_THICK)
valid_primitive = true;
else
valid_primitive = ( validUnit( value, FLength, strict&(!nonCSSHint) ) );
break;
case CSS_PROP_LETTER_SPACING: // normal | <length> | inherit
case CSS_PROP_WORD_SPACING: // normal | <length> | inherit
if ( id == CSS_VAL_NORMAL )
valid_primitive = true;
else
valid_primitive = validUnit( value, FLength, strict&(!nonCSSHint) );
break;
case CSS_PROP_TEXT_INDENT: // <length> | <percentage> | inherit
valid_primitive = ( !id && validUnit( value, FLength|FPercent, strict&(!nonCSSHint) ) );
break;
case CSS_PROP_PADDING_TOP: // <length> | <percentage> | inherit
case CSS_PROP_PADDING_RIGHT: // <padding-width> | inherit
case CSS_PROP_PADDING_BOTTOM: // Which is defined as
case CSS_PROP_PADDING_LEFT: // <length> | <percentage>
case CSS_PROP__TDEHTML_PADDING_START:
valid_primitive = ( !id && validUnit( value, FLength|FPercent|FNonNeg, strict&(!nonCSSHint) ) );
break;
case CSS_PROP_MAX_HEIGHT: // <length> | <percentage> | none | inherit
case CSS_PROP_MAX_WIDTH: // <length> | <percentage> | none | inherit
if ( id == CSS_VAL_NONE ) {
valid_primitive = true;
break;
}
/* nobreak */
case CSS_PROP_MIN_HEIGHT: // <length> | <percentage> | inherit
case CSS_PROP_MIN_WIDTH: // <length> | <percentage> | inherit
valid_primitive = ( !id && validUnit( value, FLength|FPercent|FNonNeg, strict&(!nonCSSHint) ) );
break;
case CSS_PROP_FONT_SIZE:
// <absolute-size> | <relative-size> | <length> | <percentage> | inherit
if (id >= CSS_VAL_XX_SMALL && id <= CSS_VAL_LARGER)
valid_primitive = true;
else
valid_primitive = ( validUnit( value, FLength|FPercent, strict&(!nonCSSHint) ) );
break;
case CSS_PROP_FONT_STYLE: // normal | italic | oblique | inherit
if ( id == CSS_VAL_NORMAL || id == CSS_VAL_ITALIC || id == CSS_VAL_OBLIQUE)
valid_primitive = true;
break;
case CSS_PROP_FONT_VARIANT: // normal | small-caps | inherit
if ( id == CSS_VAL_NORMAL || id == CSS_VAL_SMALL_CAPS)
valid_primitive = true;
break;
case CSS_PROP_VERTICAL_ALIGN:
// baseline | sub | super | top | text-top | middle | bottom | text-bottom |
// <percentage> | <length> | inherit
if ( id >= CSS_VAL_BASELINE && id <= CSS_VAL__TDEHTML_BASELINE_MIDDLE )
valid_primitive = true;
else
valid_primitive = ( !id && validUnit( value, FLength|FPercent, strict&(!nonCSSHint) ) );
break;
case CSS_PROP_HEIGHT: // <length> | <percentage> | auto | inherit
case CSS_PROP_WIDTH: // <length> | <percentage> | auto | inherit
if ( id == CSS_VAL_AUTO )
valid_primitive = true;
else
// ### handle multilength case where we allow relative units
valid_primitive = ( !id && validUnit( value, FLength|FPercent|FNonNeg, strict&(!nonCSSHint) ) );
break;
case CSS_PROP_BOTTOM: // <length> | <percentage> | auto | inherit
case CSS_PROP_LEFT: // <length> | <percentage> | auto | inherit
case CSS_PROP_RIGHT: // <length> | <percentage> | auto | inherit
case CSS_PROP_TOP: // <length> | <percentage> | auto | inherit
case CSS_PROP_MARGIN_TOP: //// <margin-width> | inherit
case CSS_PROP_MARGIN_RIGHT: // Which is defined as
case CSS_PROP_MARGIN_BOTTOM: // <length> | <percentage> | auto | inherit
case CSS_PROP_MARGIN_LEFT: ////
case CSS_PROP__TDEHTML_MARGIN_START:
if ( id == CSS_VAL_AUTO )
valid_primitive = true;
else
valid_primitive = ( !id && validUnit( value, FLength|FPercent, strict&(!nonCSSHint) ) );
break;
case CSS_PROP_Z_INDEX: // auto | <integer> | inherit
// tqDebug("parsing z-index: id=%d, fValue=%f", id, value->fValue );
if ( id == CSS_VAL_AUTO ) {
valid_primitive = true;
break;
}
/* nobreak */
case CSS_PROP_ORPHANS: // <integer> | inherit
case CSS_PROP_WIDOWS: // <integer> | inherit
// ### not supported later on
valid_primitive = ( !id && validUnit( value, FInteger, false ) );
break;
case CSS_PROP_LINE_HEIGHT: // normal | <number> | <length> | <percentage> | inherit
if ( id == CSS_VAL_NORMAL )
valid_primitive = true;
else
valid_primitive = ( !id && validUnit( value, FNumber|FLength|FPercent, strict&(!nonCSSHint) ) );
break;
case CSS_PROP_COUNTER_INCREMENT: // [ <identifier> <integer>? ]+ | none | inherit
if ( id == CSS_VAL_NONE )
valid_primitive = true;
else
return parseCounter(propId, true, important);
break;
case CSS_PROP_COUNTER_RESET: // [ <identifier> <integer>? ]+ | none | inherit
if ( id == CSS_VAL_NONE )
valid_primitive = true;
else
return parseCounter(propId, false, important);
break;
case CSS_PROP_FONT_FAMILY:
// [[ <family-name> | <generic-family> ],]* [<family-name> | <generic-family>] | inherit
{
parsedValue = parseFontFamily();
break;
}
case CSS_PROP_TEXT_DECORATION:
// none | [ underline || overline || line-through || blink ] | inherit
if (id == CSS_VAL_NONE) {
valid_primitive = true;
} else {
CSSValueListImpl *list = new CSSValueListImpl;
bool is_valid = true;
while( is_valid && value ) {
switch ( value->id ) {
case CSS_VAL_BLINK:
break;
case CSS_VAL_UNDERLINE:
case CSS_VAL_OVERLINE:
case CSS_VAL_LINE_THROUGH:
list->append( new CSSPrimitiveValueImpl( value->id ) );
break;
default:
is_valid = false;
}
value = valueList->next();
}
//kdDebug( 6080 ) << "got " << list->length() << "d decorations" << endl;
if(list->length() && is_valid) {
parsedValue = list;
valueList->next();
} else {
delete list;
}
}
break;
case CSS_PROP_TABLE_LAYOUT: // auto | fixed | inherit
if ( id == CSS_VAL_AUTO || id == CSS_VAL_FIXED )
valid_primitive = true;
break;
case CSS_PROP__TDEHTML_FLOW_MODE:
if ( id == CSS_VAL__TDEHTML_NORMAL || id == CSS_VAL__TDEHTML_AROUND_FLOATS )
valid_primitive = true;
break;
/* CSS3 properties */
case CSS_PROP_BOX_SIZING: // border-box | content-box | inherit
if ( id == CSS_VAL_BORDER_BOX || id == CSS_VAL_CONTENT_BOX )
valid_primitive = true;
break;
case CSS_PROP_OUTLINE_OFFSET:
valid_primitive = validUnit(value, FLength, strict);
break;
case CSS_PROP_TEXT_SHADOW: // CSS2 property, dropped in CSS2.1, back in CSS3, so treat as CSS3
if (id == CSS_VAL_NONE)
valid_primitive = true;
else
return parseShadow(propId, important);
break;
case CSS_PROP_OPACITY:
valid_primitive = validUnit(value, FNumber, strict);
break;
case CSS_PROP__TDEHTML_USER_INPUT: // none | enabled | disabled | inherit
if ( id == CSS_VAL_NONE || id == CSS_VAL_ENABLED || id == CSS_VAL_DISABLED )
valid_primitive = true;
// kdDebug(6080) << "CSS_PROP__TDEHTML_USER_INPUT: " << valid_primitive << endl;
break;
case CSS_PROP__TDEHTML_MARQUEE: {
const int properties[5] = { CSS_PROP__TDEHTML_MARQUEE_DIRECTION, CSS_PROP__TDEHTML_MARQUEE_INCREMENT,
CSS_PROP__TDEHTML_MARQUEE_REPETITION,
CSS_PROP__TDEHTML_MARQUEE_STYLE, CSS_PROP__TDEHTML_MARQUEE_SPEED };
return parseShortHand(propId, properties, 5, important);
}
case CSS_PROP__TDEHTML_MARQUEE_DIRECTION:
if (id == CSS_VAL_FORWARDS || id == CSS_VAL_BACKWARDS || id == CSS_VAL_AHEAD ||
id == CSS_VAL_REVERSE || id == CSS_VAL_LEFT || id == CSS_VAL_RIGHT || id == CSS_VAL_DOWN ||
id == CSS_VAL_UP || id == CSS_VAL_AUTO)
valid_primitive = true;
break;
case CSS_PROP__TDEHTML_MARQUEE_INCREMENT:
if (id == CSS_VAL_SMALL || id == CSS_VAL_LARGE || id == CSS_VAL_MEDIUM)
valid_primitive = true;
else
valid_primitive = validUnit(value, FLength|FPercent, strict&(!nonCSSHint));
break;
case CSS_PROP__TDEHTML_MARQUEE_STYLE:
if (id == CSS_VAL_NONE || id == CSS_VAL_SLIDE || id == CSS_VAL_SCROLL || id == CSS_VAL_ALTERNATE ||
id == CSS_VAL_UNFURL)
valid_primitive = true;
break;
case CSS_PROP__TDEHTML_MARQUEE_REPETITION:
if (id == CSS_VAL_INFINITE)
valid_primitive = true;
else
valid_primitive = validUnit(value, FInteger|FNonNeg, strict&(!nonCSSHint));
break;
case CSS_PROP__TDEHTML_MARQUEE_SPEED:
if (id == CSS_VAL_NORMAL || id == CSS_VAL_SLOW || id == CSS_VAL_FAST)
valid_primitive = true;
else
valid_primitive = validUnit(value, FTime|FInteger|FNonNeg, strict&(!nonCSSHint));
break;
case CSS_PROP_TEXT_OVERFLOW: // clip | ellipsis
if (id == CSS_VAL_CLIP || id == CSS_VAL_ELLIPSIS)
valid_primitive = true;
break;
// End of CSS3 properties
/* shorthand properties */
case CSS_PROP_BACKGROUND:
// ['background-color' || 'background-image' ||'background-repeat' ||
// 'background-attachment' || 'background-position'] | inherit
return parseBackgroundShorthand(important);
case CSS_PROP_BORDER:
// [ 'border-width' || 'border-style' || <color> ] | inherit
{
const int properties[3] = { CSS_PROP_BORDER_WIDTH, CSS_PROP_BORDER_STYLE,
CSS_PROP_BORDER_COLOR };
return parseShortHand(propId, properties, 3, important);
}
case CSS_PROP_BORDER_TOP:
// [ 'border-top-width' || 'border-style' || <color> ] | inherit
{
const int properties[3] = { CSS_PROP_BORDER_TOP_WIDTH, CSS_PROP_BORDER_TOP_STYLE,
CSS_PROP_BORDER_TOP_COLOR};
return parseShortHand(propId, properties, 3, important);
}
case CSS_PROP_BORDER_RIGHT:
// [ 'border-right-width' || 'border-style' || <color> ] | inherit
{
const int properties[3] = { CSS_PROP_BORDER_RIGHT_WIDTH, CSS_PROP_BORDER_RIGHT_STYLE,
CSS_PROP_BORDER_RIGHT_COLOR };
return parseShortHand(propId, properties, 3, important);
}
case CSS_PROP_BORDER_BOTTOM:
// [ 'border-bottom-width' || 'border-style' || <color> ] | inherit
{
const int properties[3] = { CSS_PROP_BORDER_BOTTOM_WIDTH, CSS_PROP_BORDER_BOTTOM_STYLE,
CSS_PROP_BORDER_BOTTOM_COLOR };
return parseShortHand(propId, properties, 3, important);
}
case CSS_PROP_BORDER_LEFT:
// [ 'border-left-width' || 'border-style' || <color> ] | inherit
{
const int properties[3] = { CSS_PROP_BORDER_LEFT_WIDTH, CSS_PROP_BORDER_LEFT_STYLE,
CSS_PROP_BORDER_LEFT_COLOR };
return parseShortHand(propId, properties, 3, important);
}
case CSS_PROP_OUTLINE:
// [ 'outline-color' || 'outline-style' || 'outline-width' ] | inherit
{
const int properties[3] = { CSS_PROP_OUTLINE_WIDTH, CSS_PROP_OUTLINE_STYLE,
CSS_PROP_OUTLINE_COLOR };
return parseShortHand(propId, properties, 3, important);
}
case CSS_PROP_BORDER_COLOR:
// <color>{1,4} | inherit
{
const int properties[4] = { CSS_PROP_BORDER_TOP_COLOR, CSS_PROP_BORDER_RIGHT_COLOR,
CSS_PROP_BORDER_BOTTOM_COLOR, CSS_PROP_BORDER_LEFT_COLOR };
return parse4Values(propId, properties, important);
}
case CSS_PROP_BORDER_WIDTH:
// <border-width>{1,4} | inherit
{
const int properties[4] = { CSS_PROP_BORDER_TOP_WIDTH, CSS_PROP_BORDER_RIGHT_WIDTH,
CSS_PROP_BORDER_BOTTOM_WIDTH, CSS_PROP_BORDER_LEFT_WIDTH };
return parse4Values(propId, properties, important);
}
case CSS_PROP_BORDER_STYLE:
// <border-style>{1,4} | inherit
{
const int properties[4] = { CSS_PROP_BORDER_TOP_STYLE, CSS_PROP_BORDER_RIGHT_STYLE,
CSS_PROP_BORDER_BOTTOM_STYLE, CSS_PROP_BORDER_LEFT_STYLE };
return parse4Values(propId, properties, important);
}
case CSS_PROP_MARGIN:
// <margin-width>{1,4} | inherit
{
const int properties[4] = { CSS_PROP_MARGIN_TOP, CSS_PROP_MARGIN_RIGHT,
CSS_PROP_MARGIN_BOTTOM, CSS_PROP_MARGIN_LEFT };
return parse4Values(propId, properties, important);
}
case CSS_PROP_PADDING:
// <padding-width>{1,4} | inherit
{
const int properties[4] = { CSS_PROP_PADDING_TOP, CSS_PROP_PADDING_RIGHT,
CSS_PROP_PADDING_BOTTOM, CSS_PROP_PADDING_LEFT };
return parse4Values(propId, properties, important);
}
case CSS_PROP_FONT:
// [ [ 'font-style' || 'font-variant' || 'font-weight' ]? 'font-size' [ / 'line-height' ]?
// 'font-family' ] | caption | icon | menu | message-box | small-caption | status-bar | inherit
if ( id >= CSS_VAL_CAPTION && id <= CSS_VAL_STATUS_BAR )
valid_primitive = true;
else
return parseFont(important);
case CSS_PROP_LIST_STYLE:
{
const int properties[3] = { CSS_PROP_LIST_STYLE_TYPE, CSS_PROP_LIST_STYLE_POSITION,
CSS_PROP_LIST_STYLE_IMAGE };
return parseShortHand(propId, properties, 3, important);
}
case CSS_PROP_WORD_WRAP:
{
// normal | break-word
if ( id == CSS_VAL_NORMAL || id == CSS_VAL_BREAK_WORD )
valid_primitive = true;
break;
}
default:
// #ifdef CSS_DEBUG
// kdDebug( 6080 ) << "illegal or CSS2 Aural property: " << val << endl;
// #endif
break;
}
if ( valid_primitive ) {
if ( id != 0 ) {
parsedValue = new CSSPrimitiveValueImpl( id );
} else if ( value->unit == CSSPrimitiveValue::CSS_STRING )
parsedValue = new CSSPrimitiveValueImpl( domString( value->string ),
(CSSPrimitiveValue::UnitTypes) value->unit );
else if ( value->unit >= CSSPrimitiveValue::CSS_NUMBER &&
value->unit <= CSSPrimitiveValue::CSS_KHZ ) {
parsedValue = new CSSPrimitiveValueImpl( value->fValue,
(CSSPrimitiveValue::UnitTypes) value->unit );
} else if ( value->unit >= Value::Q_EMS ) {
parsedValue = new CSSQuirkPrimitiveValueImpl( value->fValue, CSSPrimitiveValue::CSS_EMS );
}
valueList->next();
}
if ( parsedValue ) {
if (!valueList->current() || inShorthand()) {
addProperty( propId, parsedValue, important );
return true;
}
delete parsedValue;
}
return false;
}
void CSSParser::addBackgroundValue(CSSValueImpl*& lval, CSSValueImpl* rval)
{
if (lval) {
if (lval->isValueList())
static_cast<CSSValueListImpl*>(lval)->append(rval);
else {
CSSValueImpl* oldVal = lval;
CSSValueListImpl* list = new CSSValueListImpl();
lval = list;
list->append(oldVal);
list->append(rval);
}
}
else
lval = rval;
}
bool CSSParser::parseBackgroundShorthand(bool important)
{
// Position must come before color in this array because a plain old "0" is a legal color
// in quirks mode but it's usually the X coordinate of a position.
// FIXME: Add CSS_PROP__TDEHTML_BACKGROUND_SIZE to the shorthand.
const int numProperties = 7;
const int properties[numProperties] = { CSS_PROP_BACKGROUND_IMAGE, CSS_PROP_BACKGROUND_REPEAT,
CSS_PROP_BACKGROUND_ATTACHMENT, CSS_PROP_BACKGROUND_POSITION, CSS_PROP__TDEHTML_BACKGROUND_CLIP,
CSS_PROP__TDEHTML_BACKGROUND_ORIGIN, CSS_PROP_BACKGROUND_COLOR };
ShorthandScope scope(this, CSS_PROP_BACKGROUND);
bool parsedProperty[numProperties] = { false }; // compiler will repeat false as necessary
CSSValueImpl* values[numProperties] = { 0 }; // compiler will repeat 0 as necessary
CSSValueImpl* positionYValue = 0;
int i;
while (valueList->current()) {
Value* val = valueList->current();
if (val->unit == Value::Operator && val->iValue == ',') {
// We hit the end. Fill in all remaining values with the initial value.
valueList->next();
for (i = 0; i < numProperties; ++i) {
if (properties[i] == CSS_PROP_BACKGROUND_COLOR && parsedProperty[i])
// Color is not allowed except as the last item in a list. Reject the entire
// property.
goto fail;
if (!parsedProperty[i] && properties[i] != CSS_PROP_BACKGROUND_COLOR) {
addBackgroundValue(values[i], new CSSInitialValueImpl());
if (properties[i] == CSS_PROP_BACKGROUND_POSITION)
addBackgroundValue(positionYValue, new CSSInitialValueImpl());
}
parsedProperty[i] = false;
}
if (!valueList->current())
break;
}
bool found = false;
for (i = 0; !found && i < numProperties; ++i) {
if (!parsedProperty[i]) {
CSSValueImpl *val1 = 0, *val2 = 0;
int propId1, propId2;
if (parseBackgroundProperty(properties[i], propId1, propId2, val1, val2)) {
parsedProperty[i] = found = true;
addBackgroundValue(values[i], val1);
if (properties[i] == CSS_PROP_BACKGROUND_POSITION)
addBackgroundValue(positionYValue, val2);
}
}
}
// if we didn't find at least one match, this is an
// invalid shorthand and we have to ignore it
if (!found)
goto fail;
}
// Fill in any remaining properties with the initial value.
for (i = 0; i < numProperties; ++i) {
if (!parsedProperty[i]) {
addBackgroundValue(values[i], new CSSInitialValueImpl());
if (properties[i] == CSS_PROP_BACKGROUND_POSITION)
addBackgroundValue(positionYValue, new CSSInitialValueImpl());
}
}
// Now add all of the properties we found.
for (i = 0; i < numProperties; i++) {
if (properties[i] == CSS_PROP_BACKGROUND_POSITION) {
addProperty(CSS_PROP_BACKGROUND_POSITION_X, values[i], important);
addProperty(CSS_PROP_BACKGROUND_POSITION_Y, positionYValue, important);
}
else
addProperty(properties[i], values[i], important);
}
return true;
fail:
for (int k = 0; k < numProperties; k++)
delete values[k];
delete positionYValue;
return false;
}
bool CSSParser::parseShortHand(int propId, const int *properties, int numProperties, bool important )
{
/* We try to match as many properties as possible
* We setup an array of booleans to mark which property has been found,
* and we try to search for properties until it makes no longer any sense
*/
ShorthandScope scope(this, propId);
bool found = false;
bool fnd[6]; //Trust me ;)
for( int i = 0; i < numProperties; i++ )
fnd[i] = false;
while ( valueList->current() ) {
found = false;
for (int propIndex = 0; !found && propIndex < numProperties; ++propIndex) {
if (!fnd[propIndex]) {
if ( parseValue( properties[propIndex], important ) ) {
fnd[propIndex] = found = true;
}
}
}
// if we didn't find at least one match, this is an
// invalid shorthand and we have to ignore it
if (!found)
return false;
}
// Fill in any remaining properties with the initial value.
m_implicitShorthand = true;
for (int i = 0; i < numProperties; ++i) {
if (!fnd[i])
addProperty(properties[i], new CSSInitialValueImpl(), important);
}
m_implicitShorthand = false;
return true;
}
bool CSSParser::parse4Values(int propId, const int *properties, bool important )
{
/* From the CSS 2 specs, 8.3
* If there is only one value, it applies to all sides. If there are two values, the top and
* bottom margins are set to the first value and the right and left margins are set to the second.
* If there are three values, the top is set to the first value, the left and right are set to the
* second, and the bottom is set to the third. If there are four values, they apply to the top,
* right, bottom, and left, respectively.
*/
int num = inShorthand() ? 1 : valueList->size();
//tqDebug("parse4Values: num=%d %d", num, valueList->numValues );
ShorthandScope scope(this, propId);
// the order is top, right, bottom, left
switch (num) {
case 1: {
if (!parseValue(properties[0], important))
return false;
CSSValueImpl *value = parsedProperties[numParsedProperties-1]->value();
m_implicitShorthand = true;
addProperty(properties[1], value, important);
addProperty(properties[2], value, important);
addProperty(properties[3], value, important);
m_implicitShorthand = false;
break;
}
case 2: {
if (!parseValue(properties[0], important) || !parseValue(properties[1], important))
return false;
CSSValueImpl *value = parsedProperties[numParsedProperties-2]->value();
m_implicitShorthand = true;
addProperty(properties[2], value, important);
value = parsedProperties[numParsedProperties-2]->value();
addProperty(properties[3], value, important);
m_implicitShorthand = false;
break;
}
case 3: {
if (!parseValue(properties[0], important) || !parseValue(properties[1], important) || !parseValue(properties[2], important))
return false;
CSSValueImpl *value = parsedProperties[numParsedProperties-2]->value();
m_implicitShorthand = true;
addProperty(properties[3], value, important);
m_implicitShorthand = false;
break;
}
case 4: {
if (!parseValue(properties[0], important) || !parseValue(properties[1], important) ||
!parseValue(properties[2], important) || !parseValue(properties[3], important))
return false;
break;
}
default: {
return false;
}
}
return true;
}
// [ <string> | <uri> | <counter> | attr(X) | open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit
// in CSS 2.1 this got somewhat reduced:
// [ <string> | attr(X) | open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit
bool CSSParser::parseContent( int propId, bool important )
{
CSSValueListImpl* values = new CSSValueListImpl();
bool isValid = true;
Value *val;
CSSValueImpl *parsedValue = 0;
while ( (val = valueList->current()) ) {
parsedValue = 0;
if ( val->unit == CSSPrimitiveValue::CSS_URI ) {
// url
DOMString value = tdehtml::parseURL(domString(val->string));
parsedValue = new CSSImageValueImpl(
DOMString(KURL( styleElement->baseURL(), value.string()).url() ), styleElement );
#ifdef CSS_DEBUG
kdDebug( 6080 ) << "content, url=" << value.string() << " base=" << styleElement->baseURL().url( ) << endl;
#endif
} else if ( val->unit == Value::Function ) {
// attr( X ) | counter( X [,Y] ) | counters( X, Y, [,Z] )
ValueList *args = val->function->args;
TQString fname = qString( val->function->name ).lower();
if (!args) return false;
if (fname == "attr(") {
if ( args->size() != 1)
return false;
Value *a = args->current();
if (a->unit != CSSPrimitiveValue::CSS_IDENT) {
isValid=false;
break;
}
if (qString(a->string)[0] == '-') {
isValid=false;
break;
}
parsedValue = new CSSPrimitiveValueImpl(domString(a->string), CSSPrimitiveValue::CSS_ATTR);
}
else
if (fname == "counter(") {
parsedValue = parseCounterContent(args, false);
if (!parsedValue) return false;
} else
if (fname == "counters(") {
parsedValue = parseCounterContent(args, true);
if (!parsedValue) return false;
}
else
return false;
} else if ( val->unit == CSSPrimitiveValue::CSS_IDENT ) {
// open-quote | close-quote | no-open-quote | no-close-quote
if ( val->id == CSS_VAL_OPEN_QUOTE ||
val->id == CSS_VAL_CLOSE_QUOTE ||
val->id == CSS_VAL_NO_OPEN_QUOTE ||
val->id == CSS_VAL_NO_CLOSE_QUOTE ) {
parsedValue = new CSSPrimitiveValueImpl(val->id);
}
} else if ( val->unit == CSSPrimitiveValue::CSS_STRING ) {
parsedValue = new CSSPrimitiveValueImpl(domString(val->string), CSSPrimitiveValue::CSS_STRING);
}
if (parsedValue)
values->append(parsedValue);
else {
isValid = false;
break;
}
valueList->next();
}
if ( isValid && values->length() ) {
addProperty( propId, values, important );
valueList->next();
return true;
}
delete values; // also frees any content by deref
return false;
}
CSSValueImpl* CSSParser::parseCounterContent(ValueList *args, bool counters)
{
if (counters || (args->size() != 1 && args->size() != 3))
if (!counters || (args->size() != 3 && args->size() != 5))
return 0;
CounterImpl *counter = new CounterImpl;
Value *i = args->current();
if (i->unit != CSSPrimitiveValue::CSS_IDENT) goto invalid;
if (qString(i->string)[0] == '-') goto invalid;
counter->m_identifier = domString(i->string);
if (counters) {
i = args->next();
if (i->unit != Value::Operator || i->iValue != ',') goto invalid;
i = args->next();
if (i->unit != CSSPrimitiveValue::CSS_STRING) goto invalid;
counter->m_separator = domString(i->string);
}
counter->m_listStyle = CSS_VAL_DECIMAL - CSS_VAL_DISC;
i = args->next();
if (i) {
if (i->unit != Value::Operator || i->iValue != ',') goto invalid;
i = args->next();
if (i->unit != CSSPrimitiveValue::CSS_IDENT) goto invalid;
if (i->id < CSS_VAL_DISC || i->id > CSS_VAL__TDEHTML_CLOSE_QUOTE) goto invalid;
counter->m_listStyle = i->id - CSS_VAL_DISC;
}
return new CSSPrimitiveValueImpl(counter);
invalid:
delete counter;
return 0;
}
CSSValueImpl* CSSParser::parseBackgroundColor()
{
int id = valueList->current()->id;
if (id == CSS_VAL__TDEHTML_TEXT || id == CSS_VAL_TRANSPARENT ||
(id >= CSS_VAL_AQUA && id <= CSS_VAL_WINDOWTEXT) || id == CSS_VAL_MENU ||
(id >= CSS_VAL_GREY && id < CSS_VAL__TDEHTML_TEXT && !strict))
return new CSSPrimitiveValueImpl(id);
return parseColor();
}
CSSValueImpl* CSSParser::parseBackgroundImage()
{
if (valueList->current()->id == CSS_VAL_NONE)
return new CSSImageValueImpl();
if (valueList->current()->unit == CSSPrimitiveValue::CSS_URI) {
DOMString uri = tdehtml::parseURL(domString(valueList->current()->string));
if (!uri.isEmpty())
return new CSSImageValueImpl(DOMString(KURL(styleElement->baseURL(), uri.string()).url()),
styleElement);
}
return 0;
}
CSSValueImpl* CSSParser::parseBackgroundPositionXY(bool& xFound, bool& yFound)
{
int id = valueList->current()->id;
if (id == CSS_VAL_LEFT || id == CSS_VAL_TOP || id == CSS_VAL_RIGHT || id == CSS_VAL_BOTTOM || id == CSS_VAL_CENTER) {
int percent = 0;
if (id == CSS_VAL_LEFT || id == CSS_VAL_RIGHT) {
if (xFound)
return 0;
xFound = true;
if (id == CSS_VAL_RIGHT)
percent = 100;
}
else if (id == CSS_VAL_TOP || id == CSS_VAL_BOTTOM) {
if (yFound)
return 0;
yFound = true;
if (id == CSS_VAL_BOTTOM)
percent = 100;
}
else if (id == CSS_VAL_CENTER)
// Center is ambiguous, so we're not sure which position we've found yet, an x or a y.
percent = 50;
return new CSSPrimitiveValueImpl(percent, CSSPrimitiveValue::CSS_PERCENTAGE);
}
if (validUnit(valueList->current(), FPercent|FLength, strict))
return new CSSPrimitiveValueImpl(valueList->current()->fValue,
(CSSPrimitiveValue::UnitTypes)valueList->current()->unit);
return 0;
}
void CSSParser::parseBackgroundPosition(CSSValueImpl*& value1, CSSValueImpl*& value2)
{
value1 = value2 = 0;
Value* value = valueList->current();
// Parse the first value. We're just making sure that it is one of the valid keywords or a percentage/length.
bool value1IsX = false, value1IsY = false;
value1 = parseBackgroundPositionXY(value1IsX, value1IsY);
if (!value1)
return;
// It only takes one value for background-position to be correctly parsed if it was specified in a shorthand (since we
// can assume that any other values belong to the rest of the shorthand). If we're not parsing a shorthand, though, the
// value was explicitly specified for our property.
value = valueList->next();
// First check for the comma. If so, we are finished parsing this value or value pair.
if (value && value->unit == Value::Operator && value->iValue == ',')
value = 0;
bool value2IsX = false, value2IsY = false;
if (value) {
value2 = parseBackgroundPositionXY(value2IsX, value2IsY);
if (value2)
valueList->next();
else {
if (!inShorthand()) {
delete value1;
value1 = 0;
return;
}
}
}
if (!value2)
// Only one value was specified. If that value was not a keyword, then it sets the x position, and the y position
// is simply 50%. This is our default.
// For keywords, the keyword was either an x-keyword (left/right), a y-keyword (top/bottom), or an ambiguous keyword (center).
// For left/right/center, the default of 50% in the y is still correct.
value2 = new CSSPrimitiveValueImpl(50, CSSPrimitiveValue::CSS_PERCENTAGE);
if (value1IsY || value2IsX) {
// Swap our two values.
CSSValueImpl* val = value2;
value2 = value1;
value1 = val;
}
}
CSSValueImpl* CSSParser::parseBackgroundSize()
{
Value* value = valueList->current();
CSSPrimitiveValueImpl* parsedValue1;
if (value->id == CSS_VAL_AUTO)
parsedValue1 = new CSSPrimitiveValueImpl(0, CSSPrimitiveValue::CSS_UNKNOWN);
else {
if (!validUnit(value, FLength|FPercent, strict))
return 0;
parsedValue1 = new CSSPrimitiveValueImpl(value->fValue, (CSSPrimitiveValue::UnitTypes)value->unit);
}
CSSPrimitiveValueImpl* parsedValue2 = parsedValue1;
if ((value = valueList->next())) {
if (value->id == CSS_VAL_AUTO)
parsedValue2 = new CSSPrimitiveValueImpl(0, CSSPrimitiveValue::CSS_UNKNOWN);
else {
if (!validUnit(value, FLength|FPercent, strict)) {
delete parsedValue1;
return 0;
}
parsedValue2 = new CSSPrimitiveValueImpl(value->fValue, (CSSPrimitiveValue::UnitTypes)value->unit);
}
}
PairImpl* pair = new PairImpl(parsedValue1, parsedValue2);
return new CSSPrimitiveValueImpl(pair);
}
bool CSSParser::parseBackgroundProperty(int propId, int& propId1, int& propId2,
CSSValueImpl*& retValue1, CSSValueImpl*& retValue2)
{
#ifdef CSS_DEBUG
kdDebug(6080) << "parseBackgroundProperty()" << endl;
kdDebug(6080) << "LOOKING FOR: " << getPropertyName(propId).string() << endl;
#endif
CSSValueListImpl *values = 0, *values2 = 0;
Value* val;
CSSValueImpl *value = 0, *value2 = 0;
bool allowComma = false;
retValue1 = retValue2 = 0;
propId1 = propId;
propId2 = propId;
if (propId == CSS_PROP_BACKGROUND_POSITION) {
propId1 = CSS_PROP_BACKGROUND_POSITION_X;
propId2 = CSS_PROP_BACKGROUND_POSITION_Y;
}
while ((val = valueList->current())) {
CSSValueImpl *currValue = 0, *currValue2 = 0;
if (allowComma) {
if (val->unit != Value::Operator || val->iValue != ',')
goto failed;
valueList->next();
allowComma = false;
}
else {
switch (propId) {
case CSS_PROP_BACKGROUND_ATTACHMENT:
if (val->id == CSS_VAL_SCROLL || val->id == CSS_VAL_FIXED) {
currValue = new CSSPrimitiveValueImpl(val->id);
valueList->next();
}
break;
case CSS_PROP_BACKGROUND_COLOR:
currValue = parseBackgroundColor();
if (currValue)
valueList->next();
break;
case CSS_PROP_BACKGROUND_IMAGE:
currValue = parseBackgroundImage();
if (currValue)
valueList->next();
break;
case CSS_PROP__TDEHTML_BACKGROUND_CLIP:
case CSS_PROP__TDEHTML_BACKGROUND_ORIGIN:
if (val->id == CSS_VAL_BORDER || val->id == CSS_VAL_PADDING || val->id == CSS_VAL_CONTENT) {
currValue = new CSSPrimitiveValueImpl(val->id);
valueList->next();
}
break;
case CSS_PROP_BACKGROUND_POSITION:
parseBackgroundPosition(currValue, currValue2);
// unlike the other functions, parseBackgroundPosition advances the valueList pointer
break;
case CSS_PROP_BACKGROUND_POSITION_X: {
bool xFound = false, yFound = true;
currValue = parseBackgroundPositionXY(xFound, yFound);
if (currValue)
valueList->next();
break;
}
case CSS_PROP_BACKGROUND_POSITION_Y: {
bool xFound = true, yFound = false;
currValue = parseBackgroundPositionXY(xFound, yFound);
if (currValue)
valueList->next();
break;
}
case CSS_PROP_BACKGROUND_REPEAT:
if (val->id >= CSS_VAL_REPEAT && val->id <= CSS_VAL_NO_REPEAT) {
currValue = new CSSPrimitiveValueImpl(val->id);
valueList->next();
}
break;
case CSS_PROP__TDEHTML_BACKGROUND_SIZE:
currValue = parseBackgroundSize();
if (currValue)
valueList->next();
break;
}
if (!currValue)
goto failed;
if (value && !values) {
values = new CSSValueListImpl();
values->append(value);
value = 0;
}
if (value2 && !values2) {
values2 = new CSSValueListImpl();
values2->append(value2);
value2 = 0;
}
if (values)
values->append(currValue);
else
value = currValue;
if (currValue2) {
if (values2)
values2->append(currValue2);
else
value2 = currValue2;
}
allowComma = true;
}
// When parsing the 'background' shorthand property, we let it handle building up the lists for all
// properties.
if (inShorthand())
break;
}
if (values && values->length()) {
retValue1 = values;
if (values2 && values2->length())
retValue2 = values2;
return true;
}
if (value) {
retValue1 = value;
retValue2 = value2;
return true;
}
failed:
delete values; delete values2;
delete value; delete value2;
return false;
}
bool CSSParser::parseShape( int propId, bool important )
{
Value *value = valueList->current();
ValueList *args = value->function->args;
TQString fname = qString( value->function->name ).lower();
//tqDebug( "parseShape: fname: %d", fname.latin1() );
if ( fname != "rect(" || !args )
return false;
// rect( t, r, b, l ) || rect( t r b l )
if ( args->size() != 4 && args->size() != 7 )
return false;
RectImpl *rect = new RectImpl();
bool valid = true;
int i = 0;
Value *a = args->current();
while ( a ) {
valid = validUnit( a, FLength, strict );
if ( !valid )
break;
CSSPrimitiveValueImpl *length =
new CSSPrimitiveValueImpl( a->fValue, (CSSPrimitiveValue::UnitTypes) a->unit );
if ( i == 0 )
rect->setTop( length );
else if ( i == 1 )
rect->setRight( length );
else if ( i == 2 )
rect->setBottom( length );
else
rect->setLeft( length );
a = args->next();
if ( a && args->size() == 7 ) {
if ( a->unit == Value::Operator && a->iValue == ',' ) {
a = args->next();
} else {
valid = false;
break;
}
}
i++;
}
if ( valid ) {
addProperty( propId, new CSSPrimitiveValueImpl( rect ), important );
valueList->next();
return true;
}
delete rect;
return false;
}
// [ 'font-style' || 'font-variant' || 'font-weight' ]? 'font-size' [ / 'line-height' ]? 'font-family'
bool CSSParser::parseFont( bool important )
{
// kdDebug(6080) << "parsing font property current=" << valueList->currentValue << endl;
bool valid = true;
Value *value = valueList->current();
CSSValueListImpl* family = 0;
CSSPrimitiveValueImpl *style = 0, *variant = 0, *weight = 0, *size = 0, *lineHeight = 0;
// optional font-style, font-variant and font-weight
while ( value ) {
// kdDebug( 6080 ) << "got value " << value->id << " / " << (value->unit == CSSPrimitiveValue::CSS_STRING ||
// value->unit == CSSPrimitiveValue::CSS_IDENT ? qString( value->string ) : TQString::null )
// << endl;
int id = value->id;
if ( id ) {
if ( id == CSS_VAL_NORMAL ) {
// do nothing, it's the initial value for all three
}
/*
else if ( id == CSS_VAL_INHERIT ) {
// set all non set ones to inherit
// This is not that simple as the inherit could also apply to the following font-size.
// very ahrd to tell without looking ahead.
inherit = true;
} */
else if ( id == CSS_VAL_ITALIC || id == CSS_VAL_OBLIQUE ) {
if ( style )
goto invalid;
style = new CSSPrimitiveValueImpl( id );
} else if ( id == CSS_VAL_SMALL_CAPS ) {
if ( variant )
goto invalid;
variant = new CSSPrimitiveValueImpl( id );
} else if ( id >= CSS_VAL_BOLD && id <= CSS_VAL_LIGHTER ) {
if ( weight )
goto invalid;
weight = new CSSPrimitiveValueImpl( id );
} else {
valid = false;
}
} else if ( !weight && validUnit( value, FInteger|FNonNeg, true ) ) {
int w = (int)value->fValue;
int val = 0;
if ( w == 100 )
val = CSS_VAL_100;
else if ( w == 200 )
val = CSS_VAL_200;
else if ( w == 300 )
val = CSS_VAL_300;
else if ( w == 400 )
val = CSS_VAL_400;
else if ( w == 500 )
val = CSS_VAL_500;
else if ( w == 600 )
val = CSS_VAL_600;
else if ( w == 700 )
val = CSS_VAL_700;
else if ( w == 800 )
val = CSS_VAL_800;
else if ( w == 900 )
val = CSS_VAL_900;
if ( val )
weight = new CSSPrimitiveValueImpl( val );
else
valid = false;
} else {
valid = false;
}
if ( !valid )
break;
value = valueList->next();
}
if ( !value )
goto invalid;
// set undefined values to default
if ( !style )
style = new CSSPrimitiveValueImpl( CSS_VAL_NORMAL );
if ( !variant )
variant = new CSSPrimitiveValueImpl( CSS_VAL_NORMAL );
if ( !weight )
weight = new CSSPrimitiveValueImpl( CSS_VAL_NORMAL );
// kdDebug( 6080 ) << " got style, variant and weight current=" << valueList->currentValue << endl;
// now a font size _must_ come
// <absolute-size> | <relative-size> | <length> | <percentage> | inherit
if ( value->id >= CSS_VAL_XX_SMALL && value->id <= CSS_VAL_LARGER )
size = new CSSPrimitiveValueImpl( value->id );
else if ( validUnit( value, FLength|FPercent, strict ) ) {
size = new CSSPrimitiveValueImpl( value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit );
}
value = valueList->next();
if ( !size || !value )
goto invalid;
// kdDebug( 6080 ) << " got size" << endl;
if ( value->unit == Value::Operator && value->iValue == '/' ) {
// line-height
value = valueList->next();
if ( !value )
goto invalid;
if ( value->id == CSS_VAL_NORMAL ) {
// default value, nothing to do
} else if ( validUnit( value, FNumber|FLength|FPercent, strict ) ) {
lineHeight = new CSSPrimitiveValueImpl( value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit );
} else {
goto invalid;
}
value = valueList->next();
if ( !value )
goto invalid;
}
if ( !lineHeight )
lineHeight = new CSSPrimitiveValueImpl( CSS_VAL_NORMAL );
// kdDebug( 6080 ) << " got line height current=" << valueList->currentValue << endl;
// font family must come now
family = parseFontFamily();
if ( valueList->current() || !family )
goto invalid;
//kdDebug( 6080 ) << " got family, parsing ok!" << endl;
addProperty( CSS_PROP_FONT_FAMILY, family, important );
addProperty( CSS_PROP_FONT_STYLE, style, important );
addProperty( CSS_PROP_FONT_VARIANT, variant, important );
addProperty( CSS_PROP_FONT_WEIGHT, weight, important );
addProperty( CSS_PROP_FONT_SIZE, size, important );
addProperty( CSS_PROP_LINE_HEIGHT, lineHeight, important );
return true;
invalid:
//kdDebug(6080) << " -> invalid" << endl;
delete family;
delete style;
delete variant;
delete weight;
delete size;
delete lineHeight;
return false;
}
CSSValueListImpl *CSSParser::parseFontFamily()
{
// kdDebug( 6080 ) << "CSSParser::parseFontFamily current=" << valueList->currentValue << endl;
CSSValueListImpl *list = new CSSValueListImpl;
Value *value = valueList->current();
TQString currFace;
while ( value ) {
// kdDebug( 6080 ) << "got value " << value->id << " / "
// << (value->unit == CSSPrimitiveValue::CSS_STRING ||
// value->unit == CSSPrimitiveValue::CSS_IDENT ? qString( value->string ) : TQString::null )
// << endl;
Value* nextValue = valueList->next();
bool nextValBreaksFont = !nextValue ||
(nextValue->unit == Value::Operator && nextValue->iValue == ',');
bool nextValIsFontName = nextValue &&
((nextValue->id >= CSS_VAL_SERIF && nextValue->id <= CSS_VAL_MONOSPACE) ||
(nextValue->unit == CSSPrimitiveValue::CSS_STRING ||
nextValue->unit == CSSPrimitiveValue::CSS_IDENT));
if (value->id >= CSS_VAL_SERIF && value->id <= CSS_VAL_MONOSPACE) {
if (!currFace.isNull()) {
currFace += ' ';
currFace += qString(value->string);
}
else if (nextValBreaksFont || !nextValIsFontName) {
if ( !currFace.isNull() ) {
list->append( new FontFamilyValueImpl( currFace ) );
currFace = TQString::null;
}
list->append(new CSSPrimitiveValueImpl(value->id));
}
else {
currFace = qString( value->string );
}
}
else if (value->unit == CSSPrimitiveValue::CSS_STRING) {
// Strings never share in a family name.
currFace = TQString::null;
list->append(new FontFamilyValueImpl(qString( value->string) ) );
}
else if (value->unit == CSSPrimitiveValue::CSS_IDENT) {
if (!currFace.isNull()) {
currFace += ' ';
currFace += qString(value->string);
}
else if (nextValBreaksFont || !nextValIsFontName) {
if ( !currFace.isNull() ) {
list->append( new FontFamilyValueImpl( currFace ) );
currFace = TQString::null;
}
list->append(new FontFamilyValueImpl( qString( value->string ) ) );
}
else {
currFace = qString( value->string);
}
}
else {
//kdDebug( 6080 ) << "invalid family part" << endl;
break;
}
if (!nextValue)
break;
if (nextValBreaksFont) {
value = valueList->next();
if ( !currFace.isNull() )
list->append( new FontFamilyValueImpl( currFace ) );
currFace = TQString::null;
}
else if (nextValIsFontName)
value = nextValue;
else
break;
}
if ( !currFace.isNull() )
list->append( new FontFamilyValueImpl( currFace ) );
if ( !list->length() ) {
delete list;
list = 0;
}
return list;
}
bool CSSParser::parseColorParameters(Value* value, int* colorArray, bool parseAlpha)
{
ValueList* args = value->function->args;
Value* v = args->current();
// Get the first value
if (!validUnit(v, FInteger | FPercent, true))
return false;
colorArray[0] = static_cast<int>(v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? 256.0 / 100.0 : 1.0));
for (int i = 1; i < 3; i++) {
v = args->next();
if (v->unit != Value::Operator && v->iValue != ',')
return false;
v = args->next();
if (!validUnit(v, FInteger | FPercent, true))
return false;
colorArray[i] = static_cast<int>(v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? 256.0 / 100.0 : 1.0));
}
if (parseAlpha) {
v = args->next();
if (v->unit != Value::Operator && v->iValue != ',')
return false;
v = args->next();
if (!validUnit(v, FNumber, true))
return false;
colorArray[3] = static_cast<int>(kMax(0.0, kMin(1.0, v->fValue)) * 255);
}
return true;
}
// CSS3 specification defines the format of a HSL color as
// hsl(<number>, <percent>, <percent>)
// and with alpha, the format is
// hsla(<number>, <percent>, <percent>, <number>)
// The first value, HUE, is in an angle with a value between 0 and 360
bool CSSParser::parseHSLParameters(Value* value, double* colorArray, bool parseAlpha)
{
ValueList* args = value->function->args;
Value* v = args->current();
// Get the first value
if (!validUnit(v, FInteger, true))
return false;
// normalize the Hue value and change it to be between 0 and 1.0
colorArray[0] = (((static_cast<int>(v->fValue) % 360) + 360) % 360) / 360.0;
for (int i = 1; i < 3; i++) {
v = args->next();
if (v->unit != Value::Operator && v->iValue != ',')
return false;
v = args->next();
if (!validUnit(v, FPercent, true))
return false;
colorArray[i] = kMax(0.0, kMin(100.0, v->fValue)) / 100.0; // needs to be value between 0 and 1.0
}
if (parseAlpha) {
v = args->next();
if (v->unit != Value::Operator && v->iValue != ',')
return false;
v = args->next();
if (!validUnit(v, FNumber, true))
return false;
colorArray[3] = kMax(0.0, kMin(1.0, v->fValue));
}
return true;
}
static bool parseColor(int unit, const TQString &name, QRgb& rgb)
{
int len = name.length();
if ( !len )
return false;
bool ok;
if ( len == 3 || len == 6 ) {
int val = name.toInt(&ok, 16);
if ( ok ) {
if (len == 6) {
rgb = (0xff << 24) | val;
return true;
}
else if ( len == 3 ) {
// #abc converts to #aabbcc according to the specs
rgb = (0xff << 24) |
(val&0xf00)<<12 | (val&0xf00)<<8 |
(val&0xf0)<<8 | (val&0xf0)<<4 |
(val&0xf)<<4 | (val&0xf);
return true;
}
}
}
if ( unit == CSSPrimitiveValue::CSS_IDENT ) {
// try a little harder
TQColor tc;
tc.setNamedColor(name.lower());
if ( tc.isValid() ) {
rgb = tc.rgb();
return true;
}
}
return false;
}
CSSPrimitiveValueImpl *CSSParser::parseColor()
{
return parseColorFromValue(valueList->current());
}
CSSPrimitiveValueImpl *CSSParser::parseColorFromValue(Value* value)
{
QRgb c = tdehtml::transparentColor;
if ( !strict && value->unit == CSSPrimitiveValue::CSS_NUMBER &&
value->fValue >= 0. && value->fValue < 1000000. ) {
TQString str;
str.sprintf( "%06d", (int)(value->fValue+.5) );
if ( !::parseColor( value->unit, str, c ) )
return 0;
}
else if (value->unit == CSSPrimitiveValue::CSS_RGBCOLOR ||
value->unit == CSSPrimitiveValue::CSS_IDENT ||
(!strict && value->unit == CSSPrimitiveValue::CSS_DIMENSION)) {
if ( !::parseColor( value->unit, qString( value->string ), c) )
return 0;
}
else if ( value->unit == Value::Function &&
value->function->args != 0 &&
value->function->args->size() == 5 /* rgb + two commas */ &&
qString( value->function->name ).lower() == "rgb(" ) {
int colorValues[3];
if (!parseColorParameters(value, colorValues, false))
return 0;
colorValues[0] = kMax( 0, kMin( 255, colorValues[0] ) );
colorValues[1] = kMax( 0, kMin( 255, colorValues[1] ) );
colorValues[2] = kMax( 0, kMin( 255, colorValues[2] ) );
c = tqRgb(colorValues[0], colorValues[1], colorValues[2]);
} else if (value->unit == Value::Function &&
value->function->args != 0 &&
value->function->args->size() == 7 /* rgba + three commas */ &&
domString(value->function->name).lower() == "rgba(") {
int colorValues[4];
if (!parseColorParameters(value, colorValues, true))
return 0;
colorValues[0] = kMax( 0, kMin( 255, colorValues[0] ) );
colorValues[1] = kMax( 0, kMin( 255, colorValues[1] ) );
colorValues[2] = kMax( 0, kMin( 255, colorValues[2] ) );
c = tqRgba(colorValues[0], colorValues[1], colorValues[2], colorValues[3]);
} else if (value->unit == Value::Function &&
value->function->args != 0 &&
value->function->args->size() == 5 /* hsl + two commas */ &&
domString(value->function->name).lower() == "hsl(") {
double colorValues[3];
if (!parseHSLParameters(value, colorValues, false))
return 0;
c = tdehtml::tqRgbaFromHsla(colorValues[0], colorValues[1], colorValues[2], 1.0);
} else if (value->unit == Value::Function &&
value->function->args != 0 &&
value->function->args->size() == 7 /* hsla + three commas */ &&
domString(value->function->name).lower() == "hsla(") {
double colorValues[4];
if (!parseHSLParameters(value, colorValues, true))
return 0;
c = tdehtml::tqRgbaFromHsla(colorValues[0], colorValues[1], colorValues[2], colorValues[3]);
}
else
return 0;
return new CSSPrimitiveValueImpl(c);
}
// This class tracks parsing state for shadow values. If it goes out of scope (e.g., due to an early return)
// without the allowBreak bit being set, then it will clean up all of the objects and destroy them.
struct ShadowParseContext {
ShadowParseContext()
:values(0), x(0), y(0), blur(0), color(0),
allowX(true), allowY(false), allowBlur(false), allowColor(true),
allowBreak(true)
{}
~ShadowParseContext() {
if (!allowBreak) {
delete values;
delete x;
delete y;
delete blur;
delete color;
}
}
bool allowLength() { return allowX || allowY || allowBlur; }
bool failed() { return allowBreak = false; }
void commitValue() {
// Handle the ,, case gracefully by doing nothing.
if (x || y || blur || color) {
if (!values)
values = new CSSValueListImpl();
// Construct the current shadow value and add it to the list.
values->append(new ShadowValueImpl(x, y, blur, color));
}
// Now reset for the next shadow value.
x = y = blur = color = 0;
allowX = allowColor = allowBreak = true;
allowY = allowBlur = false;
}
void commitLength(Value* v) {
CSSPrimitiveValueImpl* val = new CSSPrimitiveValueImpl(v->fValue,
(CSSPrimitiveValue::UnitTypes)v->unit);
if (allowX) {
x = val;
allowX = false; allowY = true; allowColor = false; allowBreak = false;
}
else if (allowY) {
y = val;
allowY = false; allowBlur = true; allowColor = true; allowBreak = true;
}
else if (allowBlur) {
blur = val;
allowBlur = false;
}
else
delete val;
}
void commitColor(CSSPrimitiveValueImpl* val) {
color = val;
allowColor = false;
if (allowX)
allowBreak = false;
else
allowBlur = false;
}
CSSValueListImpl* values;
CSSPrimitiveValueImpl* x;
CSSPrimitiveValueImpl* y;
CSSPrimitiveValueImpl* blur;
CSSPrimitiveValueImpl* color;
bool allowX;
bool allowY;
bool allowBlur;
bool allowColor;
bool allowBreak;
};
bool CSSParser::parseShadow(int propId, bool important)
{
ShadowParseContext context;
Value* val;
while ((val = valueList->current())) {
// Check for a comma break first.
if (val->unit == Value::Operator) {
if (val->iValue != ',' || !context.allowBreak)
// Other operators aren't legal or we aren't done with the current shadow
// value. Treat as invalid.
return context.failed();
// The value is good. Commit it.
context.commitValue();
}
// Check to see if we're a length.
else if (validUnit(val, FLength, true)) {
// We required a length and didn't get one. Invalid.
if (!context.allowLength())
return context.failed();
// A length is allowed here. Construct the value and add it.
context.commitLength(val);
}
else {
// The only other type of value that's ok is a color value.
CSSPrimitiveValueImpl* parsedColor = 0;
bool isColor = (val->id >= CSS_VAL_AQUA && val->id <= CSS_VAL_WINDOWTEXT || val->id == CSS_VAL_MENU ||
(val->id >= CSS_VAL_GREY && val->id <= CSS_VAL__TDEHTML_TEXT && !strict));
if (!context.allowColor)
return context.failed();
if (isColor)
parsedColor = new CSSPrimitiveValueImpl(val->id);
if (!parsedColor)
// It's not built-in. Try to parse it as a color.
parsedColor = parseColorFromValue(val);
if (!parsedColor)
return context.failed();
context.commitColor(parsedColor);
}
valueList->next();
}
if (context.allowBreak) {
context.commitValue();
if (context.values->length()) {
addProperty(propId, context.values, important);
valueList->next();
return true;
}
}
return context.failed();
}
bool CSSParser::parseCounter(int propId, bool increment, bool important)
{
enum { ID, VAL, COMMA } state = ID;
CSSValueListImpl *list = new CSSValueListImpl;
DOMString c;
Value* val;
while (true) {
val = valueList->current();
switch (state) {
// Commas are not allowed according to the standard, but Opera allows them and being the only
// other browser with counter support we need to match their behavior to work with current use
case COMMA:
state = ID;
if (val && val->unit == Value::Operator && val->iValue == ',') {
valueList->next();
continue;
}
// no break
case ID:
if (val && val->unit == CSSPrimitiveValue::CSS_IDENT) {
c = qString(val->string);
state = VAL;
valueList->next();
continue;
}
break;
case VAL: {
short i = 0;
if (val && val->unit == CSSPrimitiveValue::CSS_NUMBER) {
i = (short)val->fValue;
valueList->next();
} else
i = (increment) ? 1 : 0;
CounterActImpl *cv = new CounterActImpl(c,i);
list->append(cv);
state = COMMA;
continue;
}
}
break;
}
if(list->length() > 0) {
addProperty( propId, list, important );
return true;
}
delete list;
return false;
}
static inline int yyerror( const char *str ) {
// assert( 0 );
#ifdef CSS_DEBUG
kdDebug( 6080 ) << "CSS parse error " << str << endl;
#else
Q_UNUSED( str );
#endif
return 1;
}
#define END 0
#include "parser.h"
int DOM::CSSParser::lex( void *_yylval )
{
YYSTYPE *yylval = (YYSTYPE *)_yylval;
int token = lex();
int length;
unsigned short *t = text( &length );
#ifdef TOKEN_DEBUG
tqDebug("CSSTokenizer: got token %d: '%s'", token, token == END ? "" : TQString( (TQChar *)t, length ).latin1() );
#endif
switch( token ) {
case '{':
block_nesting++;
break;
case '}':
if ( block_nesting )
block_nesting--;
break;
case END:
if ( block_nesting ) {
block_nesting--;
return '}';
}
break;
case S:
case SGML_CD:
case INCLUDES:
case DASHMATCH:
break;
case URI:
case STRING:
case IDENT:
case NTH:
case HASH:
case DIMEN:
case UNICODERANGE:
case NOTFUNCTION:
case FUNCTION:
yylval->string.string = t;
yylval->string.length = length;
break;
case IMPORT_SYM:
case PAGE_SYM:
case MEDIA_SYM:
case FONT_FACE_SYM:
case CHARSET_SYM:
case NAMESPACE_SYM:
case IMPORTANT_SYM:
break;
case QEMS:
length--;
case GRADS:
length--;
case DEGS:
case RADS:
case KHERZ:
length--;
case MSECS:
case HERZ:
case EMS:
case EXS:
case PXS:
case CMS:
case MMS:
case INS:
case PTS:
case PCS:
length--;
case SECS:
case PERCENTAGE:
length--;
case FLOAT:
case INTEGER:
yylval->val = TQString( (TQChar *)t, length ).toDouble();
//tqDebug("value = %s, converted=%.2f", TQString( (TQChar *)t, length ).latin1(), yylval->val );
break;
default:
break;
}
return token;
}
static inline int toHex( char c ) {
if ( '0' <= c && c <= '9' )
return c - '0';
if ( 'a' <= c && c <= 'f' )
return c - 'a' + 10;
if ( 'A' <= c && c<= 'F' )
return c - 'A' + 10;
return 0;
}
unsigned short *DOM::CSSParser::text(int *length)
{
unsigned short *start = yytext;
int l = yyleng;
switch( yyTok ) {
case STRING:
l--;
/* nobreak */
case HASH:
start++;
l--;
break;
case URI:
// "url("{w}{string}{w}")"
// "url("{w}{url}{w}")"
// strip "url(" and ")"
start += 4;
l -= 5;
// strip {w}
while ( l &&
(*start == ' ' || *start == '\t' || *start == '\r' ||
*start == '\n' || *start == '\f' ) ) {
start++; l--;
}
if ( *start == '"' || *start == '\'' ) {
start++; l--;
}
while ( l &&
(start[l-1] == ' ' || start[l-1] == '\t' || start[l-1] == '\r' ||
start[l-1] == '\n' || start[l-1] == '\f' ) ) {
l--;
}
if ( l && (start[l-1] == '\"' || start[l-1] == '\'' ) )
l--;
default:
break;
}
// process escapes
unsigned short *out = start;
unsigned short *escape = 0;
for ( int i = 0; i < l; i++ ) {
unsigned short *current = start+i;
if ( escape == current - 1 ) {
if ( ( *current >= '0' && *current <= '9' ) ||
( *current >= 'a' && *current <= 'f' ) ||
( *current >= 'A' && *current <= 'F' ) )
continue;
if ( yyTok == STRING &&
( *current == '\n' || *current == '\r' || *current == '\f' ) ) {
// ### handle \r\n case
if ( *current != '\r' )
escape = 0;
continue;
}
// in all other cases copy the char to output
// ###
*out++ = *current;
escape = 0;
continue;
}
if ( escape == current - 2 && yyTok == STRING &&
*(current-1) == '\r' && *current == '\n' ) {
escape = 0;
continue;
}
if ( escape > current - 7 &&
( ( *current >= '0' && *current <= '9' ) ||
( *current >= 'a' && *current <= 'f' ) ||
( *current >= 'A' && *current <= 'F' ) ) )
continue;
if ( escape ) {
// add escaped char
int uc = 0;
escape++;
while ( escape < current ) {
// tqDebug("toHex( %c = %x", (char)*escape, toHex( *escape ) );
uc *= 16;
uc += toHex( *escape );
escape++;
}
// tqDebug(" converting escape: string='%s', value=0x%x", TQString( (TQChar *)e, current-e ).latin1(), uc );
// can't handle chars outside ucs2
if ( uc > 0xffff )
uc = 0xfffd;
*(out++) = (unsigned short)uc;
escape = 0;
if ( *current == ' ' ||
*current == '\t' ||
*current == '\r' ||
*current == '\n' ||
*current == '\f' )
continue;
}
if ( !escape && *current == '\\' ) {
escape = current;
continue;
}
*(out++) = *current;
}
if ( escape ) {
// add escaped char
int uc = 0;
escape++;
while ( escape < start+l ) {
// tqDebug("toHex( %c = %x", (char)*escape, toHex( *escape ) );
uc *= 16;
uc += toHex( *escape );
escape++;
}
// tqDebug(" converting escape: string='%s', value=0x%x", TQString( (TQChar *)e, current-e ).latin1(), uc );
// can't handle chars outside ucs2
if ( uc > 0xffff )
uc = 0xfffd;
*(out++) = (unsigned short)uc;
}
*length = out - start;
return start;
}
#define YY_DECL int DOM::CSSParser::lex()
#define yyconst const
typedef int yy_state_type;
typedef unsigned int YY_CHAR;
// this line makes sure we treat all Unicode chars correctly.
#define YY_SC_TO_UI(c) (c > 0xff ? 0xff : c)
#define YY_DO_BEFORE_ACTION \
yytext = yy_bp; \
yyleng = (int) (yy_cp - yy_bp); \
yy_hold_char = *yy_cp; \
*yy_cp = 0; \
yy_c_buf_p = yy_cp;
#define YY_BREAK break;
#define ECHO tqDebug( "%s", TQString( (TQChar *)yytext, yyleng ).latin1() )
#define YY_RULE_SETUP
#define INITIAL 0
#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1)
#define YY_START ((yy_start - 1) / 2)
#define yyterminate() yyTok = END; return yyTok
#define YY_FATAL_ERROR(a) tqFatal(a)
#define BEGIN yy_start = 1 + 2 *
#define COMMENT 1
#include "tokenizer.cpp"
|