summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/libcroco/cr-sel-eng.c
blob: 8ed485dca60fdf9f2d5153e63ed7cba124348ce0 (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
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
/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */

/*
 * This file is part of The Croco Library
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 
 * General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * See  COPYRIGHTS file for copyright information.
 */

#include <string.h>
#include "cr-sel-eng.h"
#include "cr-node-iface.h"

/**
 *@CRSelEng:
 *
 *The definition of the  #CRSelEng class.
 *The #CRSelEng is actually the "Selection Engine"
 *class. This is highly experimental for at the moment and
 *its api is very likely to change in a near future.
 */

#define PRIVATE(a_this) (a_this)->priv

struct CRPseudoClassSelHandlerEntry {
        guchar *name;
        enum CRPseudoType type;
        CRPseudoClassSelectorHandler handler;
};

struct _CRSelEngPriv {
        /*not used yet */
        gboolean case_sensitive;

        CRNodeIface const *node_iface;
        CRStyleSheet *sheet;
        /**
         *where to store the next statement
         *to be visited so that we can remember
         *it from one method call to another.
         */
        CRStatement *cur_stmt;
        GList *pcs_handlers;
        gint pcs_handlers_size;
} ;

static gboolean class_add_sel_matches_node (CRAdditionalSel * a_add_sel,
                                            CRNodeIface const * a_node_iface, CRXMLNodePtr a_node);

static gboolean id_add_sel_matches_node (CRAdditionalSel * a_add_sel,
                                         CRNodeIface const * a_node_iface, CRXMLNodePtr a_node);

static gboolean attr_add_sel_matches_node (CRAdditionalSel * a_add_sel,
                                           CRNodeIface const * a_node_iface, CRXMLNodePtr a_node);

static enum CRStatus sel_matches_node_real (CRSelEng * a_this,
                                            CRSimpleSel * a_sel,
                                            CRXMLNodePtr a_node,
                                            gboolean * a_result,
                                            gboolean a_eval_sel_list_from_end,
                                            gboolean a_recurse);

static enum CRStatus cr_sel_eng_get_matched_rulesets_real (CRSelEng * a_this,
                                                           CRStyleSheet *
                                                           a_stylesheet,
                                                           CRXMLNodePtr a_node,
                                                           CRStatement ***
                                                           a_rulesets,
                                                           gulong * a_len,
                                                           gulong * a_capacity);

static enum CRStatus put_css_properties_in_props_list (CRPropList ** a_props,
                                                       CRStatement *
                                                       a_ruleset);

static gboolean pseudo_class_add_sel_matches_node (CRSelEng * a_this,
                                                   CRAdditionalSel * a_add_sel,
                                                   CRXMLNodePtr a_node);

static gboolean empty_pseudo_class_handler (CRSelEng * a_this,
                                            CRAdditionalSel * a_sel,
                                            CRXMLNodePtr a_node);

static gboolean root_pseudo_class_handler (CRSelEng * a_this,
                                           CRAdditionalSel * a_sel,
                                           CRXMLNodePtr a_node);

static gboolean lang_pseudo_class_handler (CRSelEng * a_this,
                                           CRAdditionalSel * a_sel,
                                           CRXMLNodePtr a_node);

static gboolean only_child_pseudo_class_handler (CRSelEng * a_this,
                                                 CRAdditionalSel * a_sel,
                                                 CRXMLNodePtr a_node);

static gboolean first_child_pseudo_class_handler (CRSelEng * a_this,
                                                  CRAdditionalSel * a_sel,
                                                  CRXMLNodePtr a_node);

static gboolean first_of_type_pseudo_class_handler (CRSelEng * a_this,
                                                    CRAdditionalSel * a_sel,
                                                    CRXMLNodePtr a_node);

static gboolean last_child_pseudo_class_handler (CRSelEng * a_this,
                                                 CRAdditionalSel * a_sel,
                                                 CRXMLNodePtr a_node);

static gboolean last_of_type_pseudo_class_handler (CRSelEng * a_this,
                                                   CRAdditionalSel * a_sel,
                                                   CRXMLNodePtr a_node);

static gboolean nth_child_pseudo_class_handler (CRSelEng * a_this,
                                                CRAdditionalSel * a_sel,
                                                CRXMLNodePtr a_node);

static gboolean nth_of_type_pseudo_class_handler (CRSelEng * a_this,
                                                  CRAdditionalSel * a_sel,
                                                  CRXMLNodePtr a_node);

static gboolean nth_last_child_pseudo_class_handler (CRSelEng * a_this,
                                                     CRAdditionalSel * a_sel,
                                                     CRXMLNodePtr a_node);

static gboolean nth_last_of_type_pseudo_class_handler (CRSelEng * a_this,
                                                       CRAdditionalSel * a_sel,
                                                       CRXMLNodePtr a_node);

static CRXMLNodePtr get_next_element_node (CRNodeIface const * a_node_iface, CRXMLNodePtr a_node);

static CRXMLNodePtr get_first_child_element_node (CRNodeIface const * a_node_iface, CRXMLNodePtr a_node);

static CRXMLNodePtr get_prev_element_node (CRNodeIface const * a_node_iface, CRXMLNodePtr a_node);

static CRXMLNodePtr get_next_parent_element_node (CRNodeIface const * a_node_iface, CRXMLNodePtr a_node);

static CRArguments get_arguments_from_function (CRAdditionalSel * a_sel);

void
cr_sel_eng_set_node_iface (CRSelEng *const a_this, CRNodeIface const *const a_node_iface)
{
        /* Allow NULL: the caller may be just ensuring that the previous node_iface
           value doesn't get used until next cr_sel_eng_set_node_iface call. */
        PRIVATE(a_this)->node_iface = a_node_iface;
}

/* Quick strcmp.  Test only for == 0 or != 0, not < 0 or > 0.  */
#define strqcmp(str,lit,lit_len) \
  (strlen (str) != (lit_len) || memcmp (str, lit, lit_len))

static gboolean
root_pseudo_class_handler (CRSelEng *const a_this,
                           CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr parent = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "root")
            || a_sel->content.pseudo->type != IDENT_PSEUDO) {
                cr_utils_trace_info ("This handler is for :root only");
                return FALSE;
        }

        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);

        // libxml apears to set the parent of the root element to an
        // element of type 'xml'.
        return (parent == NULL || !strcmp(node_iface->getLocalName(parent),"xml") );
}

static gboolean
empty_pseudo_class_handler (CRSelEng *const a_this,
                            CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr cur_node = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "empty")
            || a_sel->content.pseudo->type != IDENT_PSEUDO) {
                cr_utils_trace_info ("This handler is for :empty only");
                return FALSE;
        }
        node_iface = PRIVATE(a_this)->node_iface;

        cur_node = node_iface->getFirstChild (a_node);

        return (cur_node == NULL);
}

static gboolean
lang_pseudo_class_handler (CRSelEng *const a_this,
                           CRAdditionalSel * a_sel, CRXMLNodePtr a_node)
{
        CRNodeIface const *node_iface;
        CRXMLNodePtr node = a_node;
        gboolean result = FALSE;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        node_iface = PRIVATE(a_this)->node_iface;

        /* "xml:lang" needed for SVG */
        if ( (strqcmp (a_sel->content.pseudo->name->stryng->str, "lang", 4 ) &&
              (strqcmp (a_sel->content.pseudo->name->stryng->str, "xml:lang", 8 ) ) )
            || a_sel->content.pseudo->type != FUNCTION_PSEUDO) {
                cr_utils_trace_info ("This handler is for :lang only");
                return FALSE;
        }
        /*lang code should exist and be at least of length 2 */
        if (!a_sel->content.pseudo->term
            || a_sel->content.pseudo->term->type != TERM_IDENT
            || !a_sel->content.pseudo->term->content.str->stryng
            || a_sel->content.pseudo->term->content.str->stryng->len < 2)
                return FALSE;
        for (; node; node = get_next_parent_element_node (node_iface, node)) {
                char *val = node_iface->getProp (node, "lang");
                if (!val) val = node_iface->getProp (node, "xml:lang");
                if (val) {
                        if (!strcasecmp(val, a_sel->content.pseudo->term->content.str->stryng->str)) {
                                result = TRUE;
                                break;
                        }
                        node_iface->freePropVal (val);
                        val = NULL;
                }
        }

        return result;
}

static gboolean
only_child_pseudo_class_handler (CRSelEng *const a_this,
                                  CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr parent = NULL;
        CRXMLNodePtr cur_node = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "only-child")
            || a_sel->content.pseudo->type != IDENT_PSEUDO) {
                cr_utils_trace_info ("This handler is for :only-child only");
                return FALSE;
        }
        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);
        if (!parent)
                return FALSE;

        cur_node = get_first_child_element_node (node_iface, parent);
        return (cur_node == a_node &&
                !get_next_element_node(node_iface, cur_node) );
}

static gboolean
only_of_type_pseudo_class_handler (CRSelEng *const a_this,
                                  CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr parent = NULL;
        CRXMLNodePtr cur_node = NULL;
        int m = 0;
        int child = 0;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "only-of-type")
            || a_sel->content.pseudo->type != IDENT_PSEUDO) {
                cr_utils_trace_info ("This handler is for :only-of-type selector only");
                return FALSE;
        }
        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);
        if (!parent)
                return FALSE;

        cur_node = get_first_child_element_node (node_iface, parent);

        while (cur_node) {
                if (!strcmp(node_iface->getLocalName(cur_node), a_sel->content.pseudo->sel_name->stryng->str)) {
                        ++m;
                }
                if (cur_node == a_node) {
                        child = m;
                }
                cur_node = get_next_element_node (node_iface, cur_node);
        }
        return (child == m && child == 1);
}

static gboolean
first_child_pseudo_class_handler (CRSelEng *const a_this,
                                  CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr node = NULL;
        CRXMLNodePtr parent = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "first-child")
            || a_sel->content.pseudo->type != IDENT_PSEUDO) {
                cr_utils_trace_info ("This handler is for :first-child only");
                return FALSE;
        }
        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);
        if (!parent)
                return FALSE;
        node = get_first_child_element_node (node_iface, parent);
        return (node == a_node);
}

static gboolean
first_of_type_pseudo_class_handler (CRSelEng *const a_this,
                                  CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr parent = NULL;

        // Count which child no. of type
        CRXMLNodePtr cur_node = NULL;
        int child = 0;
        int found = FALSE;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "first-of-type")
            || a_sel->content.pseudo->type != IDENT_PSEUDO) {
                cr_utils_trace_info ("This handler is for :first-of-type only");
                return FALSE;
        }
        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);
        if (!parent)
                return FALSE;

        cur_node = get_first_child_element_node (node_iface, parent);

        while (cur_node) {
                if(!strcmp(node_iface->getLocalName(cur_node), a_sel->content.pseudo->sel_name->stryng->str)) {
                        child++;
                }
                if (cur_node == a_node) {
                        found = TRUE;
                        break;
                }
                cur_node = get_next_element_node (node_iface, cur_node);
        }

        if (!found)
                return FALSE;

        return (child == 1);
}

static gboolean
last_child_pseudo_class_handler (CRSelEng *const a_this,
                                  CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr parent = NULL;

        CRXMLNodePtr cur_node = NULL;
        int m = 0;
        int child = 0;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "last-child")
            || a_sel->content.pseudo->type != IDENT_PSEUDO) {
                cr_utils_trace_info ("This handler is for :last-child only");
                return FALSE;
        }
        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);
        if (!parent)
                return FALSE;

        cur_node = get_first_child_element_node (node_iface, parent);
        while (cur_node) {
                ++m;
                if (cur_node == a_node) {
                        child = m;
                }
                cur_node = get_next_element_node (node_iface, cur_node);

        }
        return (m == child);
}

static gboolean
last_of_type_pseudo_class_handler (CRSelEng *const a_this,
                                  CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr parent = NULL;

        CRXMLNodePtr cur_node = NULL;
        int m = 0;
        int child = 0;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "last-of-type")
            || a_sel->content.pseudo->type != IDENT_PSEUDO) {
                cr_utils_trace_info ("This handler is for :last-of-type only");
                return FALSE;
        }
        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);
        if (!parent)
                return FALSE;

        cur_node = get_first_child_element_node (node_iface, parent);

        while (cur_node) {
                if (!strcmp(node_iface->getLocalName(cur_node), a_sel->content.pseudo->sel_name->stryng->str)) {
                        ++m;
                }
                if (cur_node == a_node) {
                        child = m;
                }
                cur_node = get_next_element_node (node_iface, cur_node);
        }

        return (m == child);
}

// See https://www.w3.org/TR/selectors/#nth-child-pseudo
static gboolean
nth_child_pseudo_class_handler (CRSelEng *const a_this,
                                  CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr parent = NULL;

        /* Count which child this is */
        CRXMLNodePtr cur_node = NULL;
        int child = 0;
        int found = FALSE;
        int a, b;
        CRArguments arg;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "nth-child")
            || a_sel->content.pseudo->type != FUNCTION_PSEUDO) {
                cr_utils_trace_info ("This handler is for :nth-child only");
                return FALSE;
        }

        /*pseude code term should exist */
        if (!a_sel->content.pseudo->term)
                return FALSE;

        arg = get_arguments_from_function (a_sel);

        if (arg.a == 0 && arg.b == 0)
                return FALSE;

        a = arg.a;
        b = arg.b;

        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);
        if (!parent)
                return FALSE;

        cur_node = get_first_child_element_node (node_iface, parent);

        while (cur_node) {
                ++child;
                if (cur_node == a_node) {
                        found = TRUE;
                        break;
                }
                cur_node = get_next_element_node (node_iface, cur_node);
        }

        if (!found)
                return FALSE;

        if (a == 0)
                return (b == child);

        return ((child - b)%a == 0 && (child - b)/a > -1);
}

static gboolean
nth_of_type_pseudo_class_handler (CRSelEng *const a_this,
                                  CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr parent = NULL;

        // Count which child no. of required type
        CRXMLNodePtr cur_node = NULL;
        int child = 0;
        int found = FALSE;
        int a, b;
        CRArguments arg;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "nth-of-type")
            || a_sel->content.pseudo->type != FUNCTION_PSEUDO) {
                cr_utils_trace_info ("This handler is for :nth-of-type only");
                return FALSE;
        }

        // pseudo code term should exist
        if (!a_sel->content.pseudo->term)
                return FALSE;

        arg = get_arguments_from_function (a_sel);

        if (arg.a == 0 && arg.b == 0)
                return FALSE;

        a = arg.a;
        b = arg.b;

        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);
        if (!parent)
                return FALSE;

        cur_node = get_first_child_element_node (node_iface, parent);

        while (cur_node) {
                // check if type match
                if (!strcmp(node_iface->getLocalName(cur_node), a_sel->content.pseudo->sel_name->stryng->str))
                        ++child;
                if (cur_node == a_node) {
                        found = TRUE;
                        break;
                }
                cur_node = get_next_element_node (node_iface, cur_node);
        }

        if (!found)
                return FALSE;

        if (a == 0)
                return (b == child);

        return ((child - b)%a == 0 && (child - b)/a > -1);
}

static gboolean
nth_last_child_pseudo_class_handler (CRSelEng *const a_this,
                                  CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr parent = NULL;

        /* Count which child this is (child) and total number of children (m). */
        CRXMLNodePtr cur_node = NULL;
        int m = 0;
        int child = 0;
        int found = FALSE;
        int a, b;
        CRArguments arg;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "nth-last-child")) {
                cr_utils_trace_info ("This handler is for :nth-last-child only");
                return FALSE;
        }

        /*pseudo code term should exist */
        if (!a_sel->content.pseudo->term)
                return FALSE;

        arg = get_arguments_from_function (a_sel);

        if (arg.a == 0 && arg.b == 0)
                return FALSE;

        a = arg.a;
        b = arg.b;

        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);

        if (!parent) {
                return FALSE;
        }

        cur_node = get_first_child_element_node (node_iface, parent);

        while (cur_node) {
                if (cur_node == a_node) {
                        found = TRUE;
                        child = m;
                }
                cur_node = get_next_element_node (node_iface,cur_node);
                ++m;
        }

        if (!found)
                return FALSE;

        if (a == 0)
                return ((m - b) == child);

        return ((m - child - b)%a == 0 && (m - child - b)/a > -1);
}

static gboolean
nth_last_of_type_pseudo_class_handler (CRSelEng *const a_this,
                                  CRAdditionalSel * a_sel, CRXMLNodePtr const a_node)
{
        CRNodeIface const *node_iface = NULL;
        CRXMLNodePtr parent = NULL;
        CRXMLNodePtr cur_node = NULL;
        int m = 0;
        int child = 0;
        int found = FALSE;
        CRArguments arg;
        int a, b;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_sel && a_sel->content.pseudo
                              && a_sel->content.pseudo
                              && a_sel->content.pseudo->name
                              && a_sel->content.pseudo->name->stryng
                              && a_node, FALSE);

        if (strcmp (a_sel->content.pseudo->name->stryng->str, "nth-last-of-type")) {
                cr_utils_trace_info ("This handler is for :nth-last-of-type only");
                return FALSE;
        }

        /*pseude code term should exist */
        if (!a_sel->content.pseudo->term)
                return FALSE;

        arg = get_arguments_from_function (a_sel);

        if (arg.a == 0 && arg.b == 0)
                return FALSE;

        a = arg.a;
        b = arg.b;

        node_iface = PRIVATE(a_this)->node_iface;
        parent = node_iface->getParentNode (a_node);
        if (!parent) {
                return FALSE;
        }

        cur_node = get_first_child_element_node (node_iface, parent);

        while (cur_node) {
                if (!strcmp(node_iface->getLocalName(cur_node), a_sel->content.pseudo->sel_name->stryng->str))
                        ++m;
                if (cur_node == a_node) {
                        found = TRUE;
                        child = m;
                }
                cur_node = get_next_element_node (node_iface, cur_node);
        }

        if (!found)
                return FALSE;

        if (a == 0)
                return ((m - b) == child);

        return ((m - child - b +1)%a == 0 && (m - child - b +1)/a > -1);

}

static gboolean
pseudo_class_add_sel_matches_node (CRSelEng * a_this,
                                   CRAdditionalSel * a_add_sel,
                                   CRXMLNodePtr a_node)
{
        enum CRStatus status = CR_OK;
        CRPseudoClassSelectorHandler handler = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_add_sel
                              && a_add_sel->content.pseudo
                              && a_add_sel->content.pseudo->name
                              && a_add_sel->content.pseudo->name->stryng
                              && a_add_sel->content.pseudo->name->stryng->str
                              && a_node, FALSE);

        status = cr_sel_eng_get_pseudo_class_selector_handler
                (a_this, (guchar *) a_add_sel->content.pseudo->name->stryng->str,
                 a_add_sel->content.pseudo->type, &handler);
        if (status != CR_OK || !handler)
                return FALSE;

        return handler (a_this, a_add_sel, a_node);
}

/**
 *@param a_add_sel the class additional selector to consider.
 *@param a_node the xml node to consider.
 *@return TRUE if the class additional selector matches
 *the xml node given in argument, FALSE otherwise.
 */
static gboolean
class_add_sel_matches_node (CRAdditionalSel * a_add_sel,
                            CRNodeIface const * a_node_iface, CRXMLNodePtr a_node)
{
        gboolean result = FALSE;
        char *klass = NULL;

        g_return_val_if_fail (a_add_sel
                              && a_add_sel->type == CLASS_ADD_SELECTOR
                              && a_add_sel->content.class_name
                              && a_add_sel->content.class_name->stryng
                              && a_add_sel->content.class_name->stryng->str
                              && a_node, FALSE);

        klass = a_node_iface->getProp (a_node, "class");
        if (klass) {
                char const *cur;
                for (cur = klass; cur && *cur; cur++) {
                        while (cur && *cur
                               && cr_utils_is_white_space (*cur) 
                               == TRUE)
                                cur++;

                        if (!strncmp ((const char *) cur, 
                                      a_add_sel->content.class_name->stryng->str,
                                      a_add_sel->content.class_name->stryng->len)) {
                                cur += a_add_sel->content.class_name->stryng->len;
                                if ((cur && !*cur)
                                    || cr_utils_is_white_space (*cur) == TRUE)
                                        result = TRUE;
                        } else {  /* if it doesn't match,  */
                                /*   then skip to next whitespace character to try again */
                                while (cur && *cur && !(cr_utils_is_white_space(*cur) == TRUE)) 
                                        cur++;
                        }
                        if (cur && !*cur)
                                break ;
                }
                a_node_iface->freePropVal (klass);
                klass = NULL;
        }
        return result;

}

/**
 *@return TRUE if the additional attribute selector matches
 *the current xml node given in argument, FALSE otherwise.
 *@param a_add_sel the additional attribute selector to consider.
 *@param a_node the xml node to consider.
 */
static gboolean
id_add_sel_matches_node (CRAdditionalSel * a_add_sel,
                         CRNodeIface const * a_node_iface, CRXMLNodePtr a_node)
{
        gboolean result = FALSE;
        char *id = NULL;

        g_return_val_if_fail (a_add_sel
                              && a_add_sel->type == ID_ADD_SELECTOR
                              && a_add_sel->content.id_name
                              && a_add_sel->content.id_name->stryng
                              && a_add_sel->content.id_name->stryng->str
                              && a_node, FALSE);
        g_return_val_if_fail (a_add_sel
                              && a_add_sel->type == ID_ADD_SELECTOR
                              && a_node, FALSE);

        id = a_node_iface->getProp (a_node, "id");
        if (id) {
                if (!strqcmp ((const char *) id, a_add_sel->content.id_name->stryng->str,
                              a_add_sel->content.id_name->stryng->len)) {
                        result = TRUE;
                }
                a_node_iface->freePropVal (id);
                id = NULL;
        }
        return result;
}

/**
 *Returns TRUE if the instance of #CRAdditional selector matches
 *the node given in parameter, FALSE otherwise.
 *@param a_add_sel the additional selector to evaluate.
 *@param a_node the xml node against which the selector is to
 *be evaluated
 *return TRUE if the additional selector matches the current xml node
 *FALSE otherwise.
 */
static gboolean
attr_add_sel_matches_node (CRAdditionalSel * a_add_sel,
                           CRNodeIface const * a_node_iface, CRXMLNodePtr a_node)
{
        CRAttrSel *cur_sel = NULL;

        g_return_val_if_fail (a_add_sel
                              && a_add_sel->type == ATTRIBUTE_ADD_SELECTOR
                              && a_node, FALSE);

        for (cur_sel = a_add_sel->content.attr_sel;
             cur_sel; cur_sel = cur_sel->next) {
                char *value;

                if (!cur_sel->name
                    || !cur_sel->name->stryng
                    || !cur_sel->name->stryng->str)
                        return FALSE;

                value = a_node_iface->getProp (a_node, cur_sel->name->stryng->str);
                if (!value)
                        goto free_and_return_false;

                switch (cur_sel->match_way) {
                case SET:
                        break;

                case EQUALS:
                        if (!cur_sel->value
                            || !cur_sel->value->stryng
                            || !cur_sel->value->stryng->str) {
                                goto free_and_return_false;
                        }
                        if (strcmp 
                            (value, 
                             cur_sel->value->stryng->str)) {
                                goto free_and_return_false;
                        }
                        break;

                case INCLUDES:
                        {
                                char const *ptr1 = NULL,
                                        *ptr2 = NULL,
                                        *cur = NULL;
                                gboolean found = FALSE;

                                /*
                                 *here, make sure value is a space
                                 *separated list of "words", where one
                                 *value is exactly cur_sel->value->str
                                 */
                                for (cur = value; *cur; cur++) {
                                        /*
                                         *set ptr1 to the first non white space
                                         *char addr.
                                         */
                                        while (cr_utils_is_white_space (*cur)
                                               && *cur)
                                                cur++;
                                        if (!*cur)
                                                break;
                                        ptr1 = cur;

                                        /*
                                         *set ptr2 to the end the word.
                                         */
                                        while (!cr_utils_is_white_space (*cur)
                                               && *cur)
                                                cur++;
                                        cur--;
                                        ptr2 = cur;

                                        if (!strncmp
                                            ((const char *) ptr1, 
                                             cur_sel->value->stryng->str,
                                             ptr2 - ptr1 + 1)) {
                                                found = TRUE;
                                                break;
                                        }
                                        ptr1 = ptr2 = NULL;
                                }

                                if (!found) {
                                        goto free_and_return_false;
                                }
                        }
                        break;

                case DASHMATCH:
                        {
                                char const *ptr1 = NULL,
                                        *ptr2 = NULL,
                                        *cur = NULL;
                                gboolean found = FALSE;

                                /*
                                 *here, make sure value is an hyphen
                                 *separated list of "words", each of which
                                 *starting with "cur_sel->value->str"
                                 */
                                for (cur = value; *cur; cur++) {
                                        if (*cur == '-')
                                                cur++;
                                        ptr1 = cur;

                                        while (*cur != '-' && *cur)
                                                cur++;
                                        cur--;
                                        ptr2 = cur;

                                        if (g_strstr_len
                                            ((const gchar *) ptr1, ptr2 - ptr1 + 1,
                                             cur_sel->value->stryng->str)
                                            == ptr1) {
                                                found = TRUE;
                                                break;
                                        }
                                }

                                if (!found) {
                                        goto free_and_return_false;
                                }
                        }
                        break;
                default:
                        goto free_and_return_false;
                }

                a_node_iface->freePropVal (value);
                continue;

        free_and_return_false:
                a_node_iface->freePropVal (value);
                return FALSE;
        }

        return TRUE;
}

/**
 *Evaluates if a given additional selector matches an xml node.
 *@param a_add_sel the additional selector to consider.
 *@param a_node the xml node to consider.
 *@return TRUE is a_add_sel matches a_node, FALSE otherwise.
 */
static gboolean
additional_selector_matches_node (CRSelEng * a_this,
                                  CRAdditionalSel * a_add_sel,
                                  CRXMLNodePtr a_node)
{
        CRAdditionalSel *cur_add_sel = NULL, *tail = NULL ;
        gboolean evaluated = FALSE ;

        for (tail = a_add_sel ; 
             tail && tail->next; 
             tail = tail->next) ;

        g_return_val_if_fail (tail, FALSE) ;

        for (cur_add_sel = tail ;
             cur_add_sel ;
             cur_add_sel = cur_add_sel->prev) {

                evaluated = TRUE ;
                if (cur_add_sel->type == NO_ADD_SELECTOR) {
                        return FALSE;
                }

                if (cur_add_sel->type == CLASS_ADD_SELECTOR
                    && cur_add_sel->content.class_name
                    && cur_add_sel->content.class_name->stryng
                    && cur_add_sel->content.class_name->stryng->str) {
                        if (!class_add_sel_matches_node (cur_add_sel,
                                                         PRIVATE(a_this)->node_iface,
                                                         a_node)) {
                                return FALSE;
                        }
                        continue ;
                } else if (cur_add_sel->type == ID_ADD_SELECTOR
                           && cur_add_sel->content.id_name
                           && cur_add_sel->content.id_name->stryng
                           && cur_add_sel->content.id_name->stryng->str) {
                        if (!id_add_sel_matches_node (cur_add_sel,
                                                      PRIVATE(a_this)->node_iface,
                                                      a_node)) {
                                return FALSE;
                        }
                        continue ;
                } else if (cur_add_sel->type == ATTRIBUTE_ADD_SELECTOR
                           && cur_add_sel->content.attr_sel) {
                        /*
                         *here, call a function that does the match
                         *against an attribute additional selector
                         *and an xml node.
                         */
                        if (!attr_add_sel_matches_node (cur_add_sel,
                                                       PRIVATE(a_this)->node_iface,
                                                       a_node)) {
                                return FALSE;
                        }
                        continue ;
                } else if (cur_add_sel->type == PSEUDO_CLASS_ADD_SELECTOR
                           && cur_add_sel->content.pseudo) {
                        if (!pseudo_class_add_sel_matches_node
                            (a_this, cur_add_sel, a_node)) {
                                return FALSE;
                        }
                        continue ;
                }
        }
        if (evaluated == TRUE)
                return TRUE;
        return FALSE ;
}

static CRXMLNodePtr
get_next_element_node (CRNodeIface const * a_node_iface, CRXMLNodePtr a_node)
{
        CRXMLNodePtr cur_node = a_node;

        g_return_val_if_fail (a_node, NULL);

        do {
                cur_node = a_node_iface->getNextSibling (cur_node);
        } while (cur_node && !a_node_iface->isElementNode(cur_node));
        return cur_node;
}

static CRXMLNodePtr
get_first_child_element_node (CRNodeIface const * a_node_iface, CRXMLNodePtr a_node)
{
        CRXMLNodePtr cur_node = NULL;

        g_return_val_if_fail (a_node, NULL);

        cur_node = a_node_iface->getFirstChild (a_node);
        if (!cur_node)
                return cur_node;
        if (a_node_iface->isElementNode (cur_node))
                return cur_node;
        return get_next_element_node (a_node_iface, cur_node);
}

static CRXMLNodePtr
get_prev_element_node (CRNodeIface const * a_node_iface, CRXMLNodePtr a_node)
{
        CRXMLNodePtr cur_node = a_node;

        g_return_val_if_fail (a_node, NULL);

        do {
                cur_node = a_node_iface->getPrevSibling (cur_node);
        } while (cur_node && !a_node_iface->isElementNode(cur_node));
        return cur_node;
}

static CRXMLNodePtr
get_next_parent_element_node (CRNodeIface const * a_node_iface, CRXMLNodePtr a_node)
{
        CRXMLNodePtr cur_node = a_node;

        g_return_val_if_fail (a_node, NULL);

        do {
                cur_node = a_node_iface->getParentNode (cur_node);
        } while (cur_node && !a_node_iface->isElementNode (cur_node));
        return cur_node;
}

static CRArguments
get_arguments_from_function (CRAdditionalSel * a_sel)
{
        CRArguments arg;
        arg.a = 0;
        arg.b = 0;
        switch (a_sel->content.pseudo->term->type) {
        case TERM_NUMBER:
                if (a_sel->content.pseudo->term->content.num) {
                        arg.b = a_sel->content.pseudo->term->content.num->val;
                }
                if (a_sel->content.pseudo->term->n) {
                        arg.a = arg.b;
                        arg.b = 0;
                }
                break;

        case TERM_IDENT:
                if (a_sel->content.pseudo->term->content.str) {
                        if (!strcmp(a_sel->content.pseudo->term->content.str->stryng->str, "even")) {
                                arg.a = 2;
                                arg.b = 0;
                        } else if (!strcmp(a_sel->content.pseudo->term->content.str->stryng->str, "odd")) {
                                arg.a = 2;
                                arg.b = 1;
                        } else if (!strcmp(a_sel->content.pseudo->term->content.str->stryng->str, "n")) {
                                /* 'n' without number */
                                arg.a = 1;
                        } else if (!strcmp(a_sel->content.pseudo->term->content.str->stryng->str, "-n")) {
                                /* '-n' without number */
                                arg.a = -1;
                        } else {
                                /* Unknown string */
                                arg.a = 0;
                                arg.b = 0;
                                return (arg);
                        }
                }
                break;

        default:
                cr_utils_trace_info ("Unknown term in nth style handler");
                arg.a = 0;
                arg.b = 0;
                return (arg);
        }

        if (arg.a != 0 && a_sel->content.pseudo->term->next) {
                /* check for b in 'an+b' */
                if (a_sel->content.pseudo->term->next->type == TERM_NUMBER &&
                    a_sel->content.pseudo->term->next->content.num ) {
                        arg.b = a_sel->content.pseudo->term->next->content.num->val;
                }
        }

        return (arg);
}

/**
 *Evaluate a selector (a simple selectors list) and says
 *if it matches the xml node given in parameter.
 *The algorithm used here is the following:
 *Walk the combinator separated list of simple selectors backward, starting
 *from the end of the list. For each simple selector, looks if
 *if matches the current node.
 *
 *@param a_this the selection engine.
 *@param a_sel the simple selection list.
 *@param a_node the xml node.
 *@param a_result out parameter. Set to true if the
 *selector matches the xml node, FALSE otherwise.
 *@param a_recurse if set to TRUE, the function will walk to
 *the next simple selector (after the evaluation of the current one) 
 *and recursively evaluate it. Must be usually set to TRUE unless you
 *know what you are doing.
 */
static enum CRStatus
sel_matches_node_real (CRSelEng * a_this, CRSimpleSel * a_sel,
                       CRXMLNodePtr a_node, gboolean * a_result,
                       gboolean a_eval_sel_list_from_end,
                       gboolean a_recurse)
{
        CRSimpleSel *cur_sel = NULL;
        CRXMLNodePtr cur_node = NULL;
        CRNodeIface const *node_iface = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_this && a_node
                              && a_result, CR_BAD_PARAM_ERROR);

        node_iface = PRIVATE(a_this)->node_iface;
        *a_result = FALSE;

        if (!node_iface->isElementNode(a_node))
                return CR_OK;

        if (a_eval_sel_list_from_end == TRUE) {
                /*go and get the last simple selector of the list */
                for (cur_sel = a_sel;
                     cur_sel && cur_sel->next; cur_sel = cur_sel->next) ;
        } else {
                cur_sel = a_sel;
        }

        for (cur_node = a_node; cur_sel; cur_sel = cur_sel->prev) {
                if (((cur_sel->type_mask & TYPE_SELECTOR)
                     && (cur_sel->name 
                         && cur_sel->name->stryng
                         && cur_sel->name->stryng->str)
                     && (!strcmp (cur_sel->name->stryng->str,
                                  (const char *) node_iface->getLocalName(cur_node))))
                    || (cur_sel->type_mask & UNIVERSAL_SELECTOR)) {
                        /*
                         *this simple selector
                         *matches the current xml node
                         *Let's see if the preceding
                         *simple selectors also match
                         *their xml node counterpart.
                         */
                        if (cur_sel->add_sel) {
                                if (additional_selector_matches_node (a_this, cur_sel->add_sel, 
                                                                      cur_node) == TRUE) {
                                        goto walk_a_step_in_expr;
                                } else {
                                        goto done;
                                }
                        } else {
                                goto walk_a_step_in_expr;
                        }                                
                } 
                if (!(cur_sel->type_mask & TYPE_SELECTOR)
                    && !(cur_sel->type_mask & UNIVERSAL_SELECTOR)) {
                        if (!cur_sel->add_sel) {
                                goto done;
                        }
                        if (additional_selector_matches_node
                            (a_this, cur_sel->add_sel, cur_node)
                            == TRUE) {
                                goto walk_a_step_in_expr;
                        } else {
                                goto done;
                        }
                } else {
                        goto done ;
                }

        walk_a_step_in_expr:
                if (a_recurse == FALSE) {
                        *a_result = TRUE;
                        goto done;
                }

                /*
                 *here, depending on the combinator of cur_sel
                 *choose the axis of the xml tree traversal
                 *and walk one step in the xml tree.
                 */
                if (!cur_sel->prev)
                        break;

                switch (cur_sel->combinator) {
                case NO_COMBINATOR:
                        break;

                case COMB_WS:  /*descendant selector */
                {
                        CRXMLNodePtr n = NULL;
                        enum CRStatus status = CR_OK;
                        gboolean matches = FALSE;

                        /*
                         *walk the xml tree upward looking for a parent
                         *node that matches the preceding selector.
                         */
                        for (n = node_iface->getParentNode (cur_node);
                             n;
                             n = node_iface->getParentNode (n)) {
                                status = sel_matches_node_real
                                        (a_this, cur_sel->prev,
                                         n, &matches, FALSE, TRUE);

                                if (status != CR_OK)
                                        goto done;

                                if (matches == TRUE) {
                                        cur_node = n ;
                                        break;
                                }
                        }

                        if (!n) {
                                /*
                                 *didn't find any ancestor that matches
                                 *the previous simple selector.
                                 */
                                goto done;
                        }
                        /*
                         *in this case, the preceding simple sel
                         *will have been interpreted twice, which
                         *is a cpu and mem waste ... I need to find
                         *another way to do this. Anyway, this is
                         *my first attempt to write this function and
                         *I am a bit clueless.
                         */
                        break;
                }

                case COMB_PLUS:
                        cur_node = get_prev_element_node (node_iface, cur_node);
                        if (!cur_node)
                                goto done;
                        break;

                case COMB_TILDE: /* General sibling selector. */
                {
                        CRXMLNodePtr n = NULL;
                        enum CRStatus status = CR_OK;
                        gboolean matches = FALSE;

                        /*
                         * Walk through previous sibing nodes looking for a
                         * node that matches the preceding selector.
                         */
                        for (n = get_prev_element_node (node_iface, cur_node);
                             n;
                             n = get_prev_element_node (node_iface, n)) {
                                status = sel_matches_node_real
                                        (a_this, cur_sel->prev,
                                         n, &matches, FALSE, TRUE);

                                if (status != CR_OK)
                                        goto done;

                                if (matches == TRUE) {
                                        cur_node = n ;
                                        break;
                                }
                        }

                        if (!n) {
                                /*
                                 * Didn't find any previous sibling that matches
                                 * the previous simple selector.
                                 */
                                goto done;
                        }
                        /*
                         * See note above in COMB_WS section.
                         */
                        break;
                }

                case COMB_GT:
                        cur_node = get_next_parent_element_node (node_iface, cur_node);
                        if (!cur_node)
                                goto done;
                        break;

                default:
                        goto done;
                }
                continue;
        }

        /*
         *if we reached this point, it means the selector matches
         *the xml node.
         */
        *a_result = TRUE;

 done:
        return CR_OK;
}


/**
 *Returns  array of the ruleset statements that matches the
 *given xml node.
 *The engine keeps in memory the last statement he
 *visited during the match. So, the next call
 *to this function will eventually return a rulesets list starting
 *from the last ruleset statement visited during the previous call.
 *The enable users to get matching rulesets in an incremental way.
 *Note that for each statement returned, 
 *the engine calculates the specificity of the selector
 *that matched the xml node and stores it in the "specifity" field
 *of the statement structure.
 *
 *@param a_sel_eng the current selection engine
 *@param a_node the xml node for which the request
 *is being made.
 *@param a_sel_list the list of selectors to perform the search in.
 *@param a_rulesets in/out parameter. A pointer to the
 *returned array of rulesets statements that match the xml node
 *given in parameter. The caller allocates the array before calling this
 *function.
 *@param a_len in/out parameter the length (in sizeof (#CRStatement*)) 
 *of the returned array.
 *(the length of a_rulesets, more precisely).
 *The caller must set it to the length of a_ruleset prior to calling this
 *function. In return, the function sets it to the length 
 *(in sizeof (#CRStatement)) of the actually returned CRStatement array.
 *@return CR_OUTPUT_TOO_SHORT_ERROR if found more rulesets than the size
 *of the a_rulesets array. In this case, the first *a_len rulesets found
 *are put in a_rulesets, and a further call will return the following
 *ruleset(s) following the same principle.
 *@return CR_OK if all the rulesets found have been returned. In this
 *case, *a_len is set to the actual number of ruleset found.
 *@return CR_BAD_PARAM_ERROR in case any of the given parameter are
 *bad (e.g null pointer).
 *@return CR_ERROR if any other error occurred.
 */
static enum CRStatus
cr_sel_eng_get_matched_rulesets_real (CRSelEng * a_this,
                                      CRStyleSheet * a_stylesheet,
                                      CRXMLNodePtr a_node,
                                      CRStatement *** a_rulesets,
                                      gulong * a_len,
                                      gulong * a_capacity)
{
        CRStatement *cur_stmt = NULL;
        CRSelector *sel_list = NULL,
                *cur_sel = NULL;
        gboolean matches = FALSE;
        enum CRStatus status = CR_OK;

        g_return_val_if_fail (a_this
                              && a_stylesheet
                              && a_node && a_rulesets, CR_BAD_PARAM_ERROR);

        if (!a_stylesheet->statements) {
                return CR_OK;
        }

        /*
         *if this stylesheet is "new one"
         *let's remember it for subsequent calls.
         */
        if (PRIVATE (a_this)->sheet != a_stylesheet) {
                PRIVATE (a_this)->sheet = a_stylesheet;
                PRIVATE (a_this)->cur_stmt = a_stylesheet->statements;
        }

        /*
         *walk through the list of statements and,
         *get the selectors list inside the statements that
         *contain some, and try to match our xml node in these
         *selectors lists.
         */
        for (cur_stmt = PRIVATE (a_this)->cur_stmt;
             (PRIVATE (a_this)->cur_stmt = cur_stmt);
             cur_stmt = cur_stmt->next) {
                /*
                 *initialize the selector list in which we will
                 *really perform the search.
                 */
                sel_list = NULL;

                /*
                 *get the damn selector list in 
                 *which we have to look
                 */
                switch (cur_stmt->type) {
                case RULESET_STMT:
                        if (cur_stmt->kind.ruleset
                            && cur_stmt->kind.ruleset->sel_list) {
                                sel_list = cur_stmt->kind.ruleset->sel_list;
                        }
                        break;

                case AT_MEDIA_RULE_STMT:
                        if (cur_stmt->kind.media_rule
                            && cur_stmt->kind.media_rule->rulesets
                            && cur_stmt->kind.media_rule->rulesets->
                            kind.ruleset
                            && cur_stmt->kind.media_rule->rulesets->
                            kind.ruleset->sel_list) {
                                sel_list =
                                        cur_stmt->kind.media_rule->
                                        rulesets->kind.ruleset->sel_list;
                        }
                        break;

                case AT_IMPORT_RULE_STMT:
                        if (cur_stmt->kind.import_rule) {
                                g_assert(!cur_stmt->kind.import_rule->sheet ||
                                         !cur_stmt->kind.import_rule->sheet->next);
                                cr_sel_eng_get_matched_rulesets_real (
                                        a_this, cur_stmt->kind.import_rule->sheet,
                                        a_node, a_rulesets,
                                        a_len, a_capacity);
                        }
                        break;
                default:
                        break;
                }

                if (!sel_list)
                        continue;

                /*
                 *now, we have a comma separated selector list to look in.
                 *let's walk it and try to match the xml_node
                 *on each item of the list.
                 */
                for (cur_sel = sel_list; cur_sel; cur_sel = cur_sel->next) {
                        if (!cur_sel->simple_sel)
                                continue;

                        status = cr_sel_eng_matches_node
                                (a_this, cur_sel->simple_sel,
                                 a_node, &matches);

                        if (status == CR_OK && matches == TRUE) {
                                /*
                                 *bingo!!! we found one ruleset that
                                 *matches that fucking node.
                                 *lets put it in the out array.
                                 */

                                if (*a_len >= *a_capacity) {
                                        *a_capacity = (*a_len) + 8;
                                        *a_rulesets = (CRStatement **) g_try_realloc (*a_rulesets,
                                                        (*a_capacity) * sizeof (CRStatement *));
                                        if (!*a_rulesets) {
                                                cr_utils_trace_info("Out of memory");
                                                return CR_ERROR;
                                        }
                                }

                                {
                                        (*a_rulesets)[(*a_len)++] = cur_stmt;

                                        /*
                                         *For the cascade computing algorithm
                                         *(which is gonna take place later)
                                         *we must compute the specificity
                                         *(css2 spec chap 6.4.1) of the selector
                                         *that matched the current xml node
                                         *and store it in the css2 statement
                                         *(statement == ruleset here).
                                         */
                                        status = cr_simple_sel_compute_specificity (cur_sel->simple_sel);

                                        g_return_val_if_fail (status == CR_OK,
                                                              CR_ERROR);
                                        cur_stmt->specificity =
                                                cur_sel->simple_sel->
                                                specificity;
                                }
                        }
                }
        }

        /*
         *if we reached this point, it means
         *we reached the end of stylesheet.
         *no need to store any info about the stylesheet
         *anymore.
         */
        g_return_val_if_fail (!PRIVATE (a_this)->cur_stmt, CR_ERROR);
        PRIVATE (a_this)->sheet = NULL;
        return CR_OK;
}

static enum CRStatus
put_css_properties_in_props_list (CRPropList ** a_props, CRStatement * a_stmt)
{
        CRPropList *props = NULL,
                *pair = NULL,
                *tmp_props = NULL;
        CRDeclaration *cur_decl = NULL;

        g_return_val_if_fail (a_props && a_stmt
                              && a_stmt->type == RULESET_STMT
                              && a_stmt->kind.ruleset, CR_BAD_PARAM_ERROR);

        props = *a_props;

        for (cur_decl = a_stmt->kind.ruleset->decl_list;
             cur_decl; cur_decl = cur_decl->next) {
                CRDeclaration *decl;

                decl = NULL;
                pair = NULL;

                if (!cur_decl->property 
                    || !cur_decl->property->stryng
                    || !cur_decl->property->stryng->str)
                        continue;
                /*
                 *First, test if the property is not
                 *already present in our properties list
                 *If yes, apply the cascading rules to
                 *compute the precedence. If not, insert
                 *the property into the list
                 */
                cr_prop_list_lookup_prop (props,
                                          cur_decl->property, 
                                          &pair);

                if (!pair) {
                        tmp_props = cr_prop_list_append2
                                (props, cur_decl->property, cur_decl);
                        if (tmp_props) {
                                props = tmp_props;
                                tmp_props = NULL;
                        }
                        continue;
                }

                /*
                 *A property with the same name already exists.
                 *We must apply here 
                 *some cascading rules
                 *to compute the precedence.
                 */
                cr_prop_list_get_decl (pair, &decl);
                g_return_val_if_fail (decl, CR_ERROR);

                /*
                 *first, look at the origin.
                 *6.4.1 says: 
                 *"for normal declarations, 
                 *author style sheets override user 
                 *style sheets which override 
                 *the default style sheet."
                 */
                if (decl->parent_statement
                    && decl->parent_statement->parent_sheet
                    && (decl->parent_statement->parent_sheet->origin
                        < a_stmt->parent_sheet->origin)) {
                        /*
                         *if the already selected declaration
                         *is marked as being !important the current
                         *declaration must not override it 
                         *(unless the already selected declaration 
                         *has an UA origin)
                         */
                        if (decl->important == TRUE && cur_decl->important != TRUE
                            && decl->parent_statement->parent_sheet->origin
                            != ORIGIN_UA) {
                                continue;
                        }
                        tmp_props = cr_prop_list_unlink (props, pair);
                        if (props) {
                                cr_prop_list_destroy (pair);
                        }
                        props = tmp_props;
                        tmp_props = NULL;
                        props = cr_prop_list_append2
                                (props, cur_decl->property, cur_decl);

                        continue;
                } else if (decl->parent_statement
                           && decl->parent_statement->parent_sheet
                           && (decl->parent_statement->
                               parent_sheet->origin
                               > a_stmt->parent_sheet->origin)) {
                        cr_utils_trace_info
                                ("We should not reach this line\n");
                        continue;
                }

                /*
                 *A property with the same
                 *name and the same origin already exists.
                 *shit. This is lasting longer than expected ...
                 *Luckily, the spec says in 6.4.1:
                 *"more specific selectors will override 
                 *more general ones"
                 *and
                 *"if two rules have the same weight, 
                 *origin and specificity, 
                 *the later specified wins"
                 */
                if (a_stmt->specificity
                    >= decl->parent_statement->specificity) {
                        if (decl->important == TRUE && cur_decl->important != TRUE)
                                continue;
                        props = cr_prop_list_unlink (props, pair);
                        if (pair) {
                                cr_prop_list_destroy (pair);
                                pair = NULL;
                        }
                        props = cr_prop_list_append2 (props,
                                                      cur_decl->property,
                                                      cur_decl);
                }
        }
        /*TODO: this may leak. Check this out */
        *a_props = props;

        return CR_OK;
}

static void
set_style_from_props (CRStyle * a_style, CRPropList * a_props)
{
        CRPropList *cur = NULL;
        CRDeclaration *decl = NULL;

        for (cur = a_props; cur; cur = cr_prop_list_get_next (cur)) {
                cr_prop_list_get_decl (cur, &decl);
                cr_style_set_style_from_decl (a_style, decl);
                decl = NULL;
        }
}

/****************************************
 *PUBLIC METHODS
 ****************************************/

/**
 * cr_sel_eng_new:
 *Creates a new instance of #CRSelEng.
 *
 *@a_node_iface: Node interface
 *
 *Returns the newly built instance of #CRSelEng of
 *NULL if an error occurs.
 */
CRSelEng *
cr_sel_eng_new (CRNodeIface const * a_node_iface)
{
        CRSelEng *result = NULL;

        result = (CRSelEng *) g_try_malloc (sizeof (CRSelEng));
        if (!result) {
                cr_utils_trace_info ("Out of memory");
                return NULL;
        }
        memset (result, 0, sizeof (CRSelEng));

        PRIVATE (result) = (CRSelEngPriv *) g_try_malloc (sizeof (CRSelEngPriv));
        if (!PRIVATE (result)) {
                cr_utils_trace_info ("Out of memory");
                g_free (result);
                return NULL;
        }
        memset (PRIVATE (result), 0, sizeof (CRSelEngPriv));
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "root",
                 IDENT_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 root_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "empty",
                 IDENT_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 empty_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "lang",
                 FUNCTION_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 lang_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "only-child",
                 IDENT_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 only_child_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "only-of-type",
                 IDENT_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 only_of_type_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "first-child",
                 IDENT_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 first_child_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "first-of-type",
                 IDENT_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 first_of_type_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "last-child",
                 IDENT_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 last_child_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "last-of-type",
                 IDENT_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 last_of_type_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "nth-child",
                 FUNCTION_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 nth_child_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "nth-of-type",
                 FUNCTION_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 nth_of_type_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "nth-last-child",
                 FUNCTION_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 nth_last_child_pseudo_class_handler);
        cr_sel_eng_register_pseudo_class_sel_handler
                (result, (guchar *) "nth-last-of-type",
                 FUNCTION_PSEUDO, /*(CRPseudoClassSelectorHandler)*/
                 nth_last_of_type_pseudo_class_handler);

        cr_sel_eng_set_node_iface (result, a_node_iface);

        return result;
}

/**
 * cr_sel_eng_register_pseudo_class_sel_handler:
 *@a_this: the current instance of #CRSelEng
 *@a_pseudo_class_sel_name: the name of the pseudo class selector.
 *@a_pseudo_class_type: the type of the pseudo class selector.
 *@a_handler: the actual handler or callback to be called during
 *the selector evaluation process.
 *
 *Adds a new handler entry in the handlers entry table.
 *
 *Returns CR_OK, upon successful completion, an error code otherwise.
 */
enum CRStatus
cr_sel_eng_register_pseudo_class_sel_handler (CRSelEng * a_this,
                                              guchar * a_name,
                                              enum CRPseudoType a_type,
                                              CRPseudoClassSelectorHandler
                                              a_handler)
{
        struct CRPseudoClassSelHandlerEntry *handler_entry = NULL;
        GList *list = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_handler && a_name, CR_BAD_PARAM_ERROR);

        handler_entry = (struct CRPseudoClassSelHandlerEntry *) g_try_malloc
                (sizeof (struct CRPseudoClassSelHandlerEntry));
        if (!handler_entry) {
                return CR_OUT_OF_MEMORY_ERROR;
        }
        memset (handler_entry, 0,
                sizeof (struct CRPseudoClassSelHandlerEntry));
        handler_entry->name = (guchar *) g_strdup ((const gchar *) a_name);
        handler_entry->type = a_type;
        handler_entry->handler = a_handler;
        list = g_list_append (PRIVATE (a_this)->pcs_handlers, handler_entry);
        if (!list) {
                return CR_OUT_OF_MEMORY_ERROR;
        }
        PRIVATE (a_this)->pcs_handlers = list;
        return CR_OK;
}

enum CRStatus
cr_sel_eng_unregister_pseudo_class_sel_handler (CRSelEng * a_this,
                                                guchar * a_name,
                                                enum CRPseudoType a_type)
{
        GList *elem = NULL,
                *deleted_elem = NULL;
        gboolean found = FALSE;
        struct CRPseudoClassSelHandlerEntry *entry = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);

        for (elem = PRIVATE (a_this)->pcs_handlers;
             elem; elem = g_list_next (elem)) {
                entry = (struct CRPseudoClassSelHandlerEntry *) elem->data;
                if (!strcmp ((const char *) entry->name, (const char *) a_name)
                    && entry->type == a_type) {
                        found = TRUE;
                        break;
                }
        }
        if (found == FALSE)
                return CR_PSEUDO_CLASS_SEL_HANDLER_NOT_FOUND_ERROR;
        PRIVATE (a_this)->pcs_handlers = g_list_delete_link
                (PRIVATE (a_this)->pcs_handlers, elem);
        entry = (struct CRPseudoClassSelHandlerEntry *) elem->data;
        if (entry->name) {
                g_free (entry->name);
                entry->name = NULL;
        }
        g_free (elem);
        g_list_free (deleted_elem);

        return CR_OK;
}

/**
 * cr_sel_eng_unregister_all_pseudo_class_sel_handlers:
 *@a_this: the current instance of #CRSelEng .
 *
 *Unregisters all the pseudo class sel handlers
 *and frees all the associated allocated datastructures.
 *
 *Returns CR_OK upon successful completion, an error code
 *otherwise.
 */
enum CRStatus
cr_sel_eng_unregister_all_pseudo_class_sel_handlers (CRSelEng * a_this)
{
        GList *elem = NULL;
        struct CRPseudoClassSelHandlerEntry *entry = NULL;

        g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);

        if (!PRIVATE (a_this)->pcs_handlers)
                return CR_OK;
        for (elem = PRIVATE (a_this)->pcs_handlers;
             elem; elem = g_list_next (elem)) {
                entry = (struct CRPseudoClassSelHandlerEntry *) elem->data;
                if (!entry)
                        continue;
                if (entry->name) {
                        g_free (entry->name);
                        entry->name = NULL;
                }
                g_free (entry);
                elem->data = NULL;
        }
        g_list_free (PRIVATE (a_this)->pcs_handlers);
        PRIVATE (a_this)->pcs_handlers = NULL;
        return CR_OK;
}

enum CRStatus
cr_sel_eng_get_pseudo_class_selector_handler (CRSelEng * a_this,
                                              guchar * a_name,
                                              enum CRPseudoType a_type,
                                              CRPseudoClassSelectorHandler *
                                              a_handler)
{
        GList *elem = NULL;
        struct CRPseudoClassSelHandlerEntry *entry = NULL;
        gboolean found = FALSE;

        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_name, CR_BAD_PARAM_ERROR);

        for (elem = PRIVATE (a_this)->pcs_handlers;
             elem; elem = g_list_next (elem)) {
                entry = (struct CRPseudoClassSelHandlerEntry *) elem->data;
                if (!strcmp ((const char *) a_name, (const char *) entry->name)
                    && entry->type == a_type) {
                        found = TRUE;
                        break;
                }
        }

        if (found == FALSE)
                return CR_PSEUDO_CLASS_SEL_HANDLER_NOT_FOUND_ERROR;
        *a_handler = entry->handler;
        return CR_OK;
}

/**
 * cr_sel_eng_matches_node:
 *@a_this: the selection engine.
 *@a_sel: the simple selector against which the xml node 
 *is going to be matched.
 *@a_node: the node against which the selector is going to be matched.
 *@a_result: out parameter. The result of the match. Is set to
 *TRUE if the selector matches the node, FALSE otherwise. This value
 *is considered if and only if this functions returns CR_OK.
 *
 *Evaluates a chained list of simple selectors (known as a css2 selector).
 *Says whether if this selector matches the xml node given in parameter or
 *not.
 *
 *Returns the CR_OK if the selection ran correctly, an error code otherwise.
 */
enum CRStatus
cr_sel_eng_matches_node (CRSelEng * a_this, CRSimpleSel * a_sel,
                         CRXMLNodePtr a_node, gboolean * a_result)
{
        g_return_val_if_fail (a_this && PRIVATE (a_this)
                              && a_this && a_node
                              && a_result, CR_BAD_PARAM_ERROR);

        if (!PRIVATE(a_this)->node_iface->isElementNode (a_node)) {
                *a_result = FALSE;
                return CR_OK;
        }

        return sel_matches_node_real (a_this, a_sel, 
                                      a_node, a_result, 
                                      TRUE, TRUE);
}

/**
 * cr_sel_eng_get_matched_rulesets:
 *@a_this: the current instance of the selection engine.
 *@a_sheet: the stylesheet that holds the selectors.
 *@a_node: the xml node to consider during the walk through
 *the stylesheet.
 *@a_rulesets: out parameter. A pointer to an array of
 *rulesets statement pointers. *a_rulesets is allocated by
 *this function and must be freed by the caller. However, the caller
 *must not alter the rulesets statements pointer because they
 *point to statements that are still in the css stylesheet.
 *@a_len: the length of *a_ruleset.
 *
 *Returns an array of pointers to selectors that matches
 *the xml node given in parameter.
 *
 *Returns CR_OK upon successful completion, an error code otherwise.
 */
enum CRStatus
cr_sel_eng_get_matched_rulesets (CRSelEng * a_this,
                                 CRStyleSheet * a_sheet,
                                 CRXMLNodePtr a_node,
                                 CRStatement *** a_rulesets, gulong * a_len)
{
        CRStatement **stmts_tab = NULL;
        enum CRStatus status = CR_OK;
        gulong capacity = 0;

        g_return_val_if_fail (a_this
                              && a_sheet
                              && a_node
                              && a_rulesets && *a_rulesets == NULL
                              && a_len, CR_BAD_PARAM_ERROR);

        *a_len = 0;

        status = cr_sel_eng_get_matched_rulesets_real
                (a_this, a_sheet, a_node, &stmts_tab, a_len, &capacity);
        if (status == CR_ERROR)
                goto error;

        *a_rulesets = stmts_tab;

        return CR_OK;

      error:

        if (stmts_tab) {
                g_free (stmts_tab);
                stmts_tab = NULL;

        }

        *a_len = 0;
        return status;
}

/**
 * Like cr_sel_eng_get_matched_rulesets_real, but process an entire (linked)
 * list of stylesheets, not only a single one.
 */
static
enum CRStatus
cr_sel_eng_process_stylesheet ( CRSelEng * a_eng,
                                CRXMLNodePtr a_node,
                                CRStyleSheet * a_stylesheet,
                                CRStatement *** stmts_tab,
                                gulong * tab_size,
                                gulong * tab_len,
                                gulong * index)
{
        enum CRStatus status = CR_OK;
        CRStyleSheet *cur = NULL;

        for (cur = a_stylesheet; cur && status == CR_OK; cur = cur->next) {
                status = cr_sel_eng_get_matched_rulesets_real
                        (a_eng, cur, a_node, stmts_tab, index, tab_size);
        }

        return status;
}

enum CRStatus
cr_sel_eng_get_matched_properties_from_cascade (CRSelEng * a_this,
                                                CRCascade * a_cascade,
                                                CRXMLNodePtr a_node,
                                                CRPropList ** a_props)
{
        CRStatement **stmts_tab = NULL;
        enum CRStatus status = CR_OK;
        gulong tab_size = 0,
                tab_len = 0,
                i = 0,
                index = 0;
        enum CRStyleOrigin origin;
        CRStyleSheet *sheet = NULL;

        g_return_val_if_fail (a_this
                              && a_cascade
                              && a_node && a_props, CR_BAD_PARAM_ERROR);

        for (origin = ORIGIN_UA; origin < NB_ORIGINS; origin = (enum CRStyleOrigin) (origin + 1)) {
                sheet = cr_cascade_get_sheet (a_cascade, origin);
                if (!sheet)
                        continue;

                status = cr_sel_eng_process_stylesheet (a_this, a_node, sheet, &stmts_tab, &tab_size, &tab_len, &index);
                if (status != CR_OK) {
                        cr_utils_trace_info ("Error while running "
                                             "selector engine");
                        return status;
                }
        }

        /*
         *TODO, walk down the stmts_tab and build the
         *property_name/declaration hashtable.
         *Make sure one can walk from the declaration to
         *the stylesheet.
         */
        for (i = 0; i < index; i++) {
                CRStatement *stmt = stmts_tab[i];
                if (!stmt)
                        continue;
                switch (stmt->type) {
                case RULESET_STMT:
                        if (!stmt->parent_sheet)
                                continue;
                        status = put_css_properties_in_props_list
                                (a_props, stmt);
                        break;
                default:
                        break;
                }

        }
        status = CR_OK ;
        if (stmts_tab) {
                g_free (stmts_tab);
                stmts_tab = NULL;
        }

        return status;
}

enum CRStatus
cr_sel_eng_get_matched_style (CRSelEng * a_this,
                              CRCascade * a_cascade,
                              CRXMLNodePtr a_node,
                              CRStyle * a_parent_style, 
                              CRStyle ** a_style,
                              gboolean a_set_props_to_initial_values)
{
        enum CRStatus status = CR_OK;

        CRPropList *props = NULL;

        g_return_val_if_fail (a_this && a_cascade
                              && a_node && a_style, CR_BAD_PARAM_ERROR);

        status = cr_sel_eng_get_matched_properties_from_cascade
                (a_this, a_cascade, a_node, &props);

        g_return_val_if_fail (status == CR_OK, status);
        if (props) {
                if (!*a_style) {
                        *a_style = cr_style_new (a_set_props_to_initial_values) ;
                        g_return_val_if_fail (*a_style, CR_ERROR);
                } else {
                        if (a_set_props_to_initial_values == TRUE) {
                                cr_style_set_props_to_initial_values (*a_style) ;
                        } else {
                                cr_style_set_props_to_default_values (*a_style);
                        }
                }
                (*a_style)->parent_style = a_parent_style;

                set_style_from_props (*a_style, props);
                if (props) {
                        cr_prop_list_destroy (props);
                        props = NULL;
                }
        }
        return CR_OK;
}

/**
 * cr_sel_eng_destroy:
 *@a_this: the current instance of the selection engine.
 *
 *The destructor of #CRSelEng
 */
void
cr_sel_eng_destroy (CRSelEng * a_this)
{
        g_return_if_fail (a_this);

        if (!PRIVATE (a_this))
                goto end ;
        if (PRIVATE (a_this)->pcs_handlers) {
                cr_sel_eng_unregister_all_pseudo_class_sel_handlers
                        (a_this) ;
                PRIVATE (a_this)->pcs_handlers = NULL ;
        }
        g_free (PRIVATE (a_this));
        PRIVATE (a_this) = NULL;
 end:
        if (a_this) {
                g_free (a_this);
        }
}