1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
|
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* libcroco - Library for parsing and applying CSS
* Copyright (C) 2006-2019 Free Software Foundation, Inc.
*
* This file is not part of the GNU gettext program, but is used with
* GNU gettext.
*
* The original copyright notice is as follows:
*/
/*
* This file is part of The Croco Library
*
* Copyright (C) 2003-2004 Dodji Seketeli. All Rights Reserved.
*
* 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
*
* Author: Dodji Seketeli.
*/
#include <config.h>
#include <string.h>
#include "cr-statement.h"
#include "cr-parser.h"
/**
*@file
*Definition of the #CRStatement class.
*/
#define DECLARATION_INDENT_NB 2
static void cr_statement_clear (CRStatement * a_this);
static void
parse_font_face_start_font_face_cb (CRDocHandler * a_this,
CRParsingLocation *a_location)
{
CRStatement *stmt = NULL;
enum CRStatus status = CR_OK;
stmt = cr_statement_new_at_font_face_rule (NULL, NULL);
g_return_if_fail (stmt);
status = cr_doc_handler_set_ctxt (a_this, stmt);
g_return_if_fail (status == CR_OK);
}
static void
parse_font_face_unrecoverable_error_cb (CRDocHandler * a_this)
{
CRStatement *stmt = NULL;
CRStatement **stmtptr = NULL;
enum CRStatus status = CR_OK;
g_return_if_fail (a_this);
stmtptr = &stmt;
status = cr_doc_handler_get_ctxt (a_this, (gpointer *) stmtptr);
if (status != CR_OK) {
cr_utils_trace_info ("Couldn't get parsing context. "
"This may lead to some memory leaks.");
return;
}
if (stmt) {
cr_statement_destroy (stmt);
cr_doc_handler_set_ctxt (a_this, NULL);
return;
}
}
static void
parse_font_face_property_cb (CRDocHandler * a_this,
CRString * a_name,
CRTerm * a_value, gboolean a_important)
{
enum CRStatus status = CR_OK;
CRString *name = NULL;
CRDeclaration *decl = NULL;
CRStatement *stmt = NULL;
CRStatement **stmtptr = NULL;
g_return_if_fail (a_this && a_name);
stmtptr = &stmt;
status = cr_doc_handler_get_ctxt (a_this, (gpointer *) stmtptr);
g_return_if_fail (status == CR_OK && stmt);
g_return_if_fail (stmt->type == AT_FONT_FACE_RULE_STMT);
name = cr_string_dup (a_name) ;
g_return_if_fail (name);
decl = cr_declaration_new (stmt, name, a_value);
if (!decl) {
cr_utils_trace_info ("cr_declaration_new () failed.");
goto error;
}
name = NULL;
stmt->kind.font_face_rule->decl_list =
cr_declaration_append (stmt->kind.font_face_rule->decl_list,
decl);
if (!stmt->kind.font_face_rule->decl_list)
goto error;
decl = NULL;
error:
if (decl) {
cr_declaration_unref (decl);
decl = NULL;
}
if (name) {
cr_string_destroy (name);
name = NULL;
}
}
static void
parse_font_face_end_font_face_cb (CRDocHandler * a_this)
{
CRStatement *result = NULL;
CRStatement **resultptr = NULL;
enum CRStatus status = CR_OK;
g_return_if_fail (a_this);
resultptr = &result;
status = cr_doc_handler_get_ctxt (a_this, (gpointer *) resultptr);
g_return_if_fail (status == CR_OK && result);
g_return_if_fail (result->type == AT_FONT_FACE_RULE_STMT);
status = cr_doc_handler_set_result (a_this, result);
g_return_if_fail (status == CR_OK);
}
static void
parse_page_start_page_cb (CRDocHandler * a_this,
CRString * a_name,
CRString * a_pseudo_page,
CRParsingLocation *a_location)
{
CRStatement *stmt = NULL;
enum CRStatus status = CR_OK;
CRString *page_name = NULL, *pseudo_name = NULL ;
if (a_name)
page_name = cr_string_dup (a_name) ;
if (a_pseudo_page)
pseudo_name = cr_string_dup (a_pseudo_page) ;
stmt = cr_statement_new_at_page_rule (NULL, NULL,
page_name,
pseudo_name);
page_name = NULL ;
pseudo_name = NULL ;
g_return_if_fail (stmt);
status = cr_doc_handler_set_ctxt (a_this, stmt);
g_return_if_fail (status == CR_OK);
}
static void
parse_page_unrecoverable_error_cb (CRDocHandler * a_this)
{
CRStatement *stmt = NULL;
CRStatement **stmtptr = NULL;
enum CRStatus status = CR_OK;
g_return_if_fail (a_this);
stmtptr = &stmt;
status = cr_doc_handler_get_ctxt (a_this, (gpointer *) stmtptr);
if (status != CR_OK) {
cr_utils_trace_info ("Couldn't get parsing context. "
"This may lead to some memory leaks.");
return;
}
if (stmt) {
cr_statement_destroy (stmt);
stmt = NULL;
cr_doc_handler_set_ctxt (a_this, NULL);
}
}
static void
parse_page_property_cb (CRDocHandler * a_this,
CRString * a_name,
CRTerm * a_expression, gboolean a_important)
{
CRString *name = NULL;
CRStatement *stmt = NULL;
CRStatement **stmtptr = NULL;
CRDeclaration *decl = NULL;
enum CRStatus status = CR_OK;
stmtptr = &stmt;
status = cr_doc_handler_get_ctxt (a_this, (gpointer *) stmtptr);
g_return_if_fail (status == CR_OK && stmt->type == AT_PAGE_RULE_STMT);
name = cr_string_dup (a_name);
g_return_if_fail (name);
decl = cr_declaration_new (stmt, name, a_expression);
g_return_if_fail (decl);
decl->important = a_important;
stmt->kind.page_rule->decl_list =
cr_declaration_append (stmt->kind.page_rule->decl_list, decl);
g_return_if_fail (stmt->kind.page_rule->decl_list);
}
static void
parse_page_end_page_cb (CRDocHandler * a_this,
CRString * a_name,
CRString * a_pseudo_page)
{
enum CRStatus status = CR_OK;
CRStatement *stmt = NULL;
CRStatement **stmtptr = NULL;
stmtptr = &stmt;
status = cr_doc_handler_get_ctxt (a_this, (gpointer *) stmtptr);
g_return_if_fail (status == CR_OK && stmt);
g_return_if_fail (stmt->type == AT_PAGE_RULE_STMT);
status = cr_doc_handler_set_result (a_this, stmt);
g_return_if_fail (status == CR_OK);
}
static void
parse_at_media_start_media_cb (CRDocHandler * a_this,
GList * a_media_list,
CRParsingLocation *a_location)
{
enum CRStatus status = CR_OK;
CRStatement *at_media = NULL;
GList *media_list = NULL;
g_return_if_fail (a_this && a_this->priv);
if (a_media_list) {
/*duplicate media list */
media_list = cr_utils_dup_glist_of_cr_string
(a_media_list);
}
g_return_if_fail (media_list);
/*make sure cr_statement_new_at_media_rule works in this case. */
at_media = cr_statement_new_at_media_rule (NULL, NULL, media_list);
status = cr_doc_handler_set_ctxt (a_this, at_media);
g_return_if_fail (status == CR_OK);
status = cr_doc_handler_set_result (a_this, at_media);
g_return_if_fail (status == CR_OK);
}
static void
parse_at_media_unrecoverable_error_cb (CRDocHandler * a_this)
{
enum CRStatus status = CR_OK;
CRStatement *stmt = NULL;
CRStatement **stmtptr = NULL;
g_return_if_fail (a_this);
stmtptr = &stmt;
status = cr_doc_handler_get_result (a_this, (gpointer *) stmtptr);
if (status != CR_OK) {
cr_utils_trace_info ("Couldn't get parsing context. "
"This may lead to some memory leaks.");
return;
}
if (stmt) {
cr_statement_destroy (stmt);
stmt = NULL;
cr_doc_handler_set_ctxt (a_this, NULL);
cr_doc_handler_set_result (a_this, NULL);
}
}
static void
parse_at_media_start_selector_cb (CRDocHandler * a_this,
CRSelector * a_sellist)
{
enum CRStatus status = CR_OK;
CRStatement *at_media = NULL;
CRStatement **at_media_ptr = NULL;
CRStatement *ruleset = NULL;
g_return_if_fail (a_this && a_this->priv && a_sellist);
at_media_ptr = &at_media;
status = cr_doc_handler_get_ctxt (a_this, (gpointer *) at_media_ptr);
g_return_if_fail (status == CR_OK && at_media);
g_return_if_fail (at_media->type == AT_MEDIA_RULE_STMT);
ruleset = cr_statement_new_ruleset (NULL, a_sellist, NULL, at_media);
g_return_if_fail (ruleset);
status = cr_doc_handler_set_ctxt (a_this, ruleset);
g_return_if_fail (status == CR_OK);
}
static void
parse_at_media_property_cb (CRDocHandler * a_this,
CRString * a_name, CRTerm * a_value,
gboolean a_important)
{
enum CRStatus status = CR_OK;
/*
*the current ruleset stmt, child of the
*current at-media being parsed.
*/
CRStatement *stmt = NULL;
CRStatement **stmtptr = NULL;
CRDeclaration *decl = NULL;
CRString *name = NULL;
g_return_if_fail (a_this && a_name);
name = cr_string_dup (a_name) ;
g_return_if_fail (name);
stmtptr = &stmt;
status = cr_doc_handler_get_ctxt (a_this,
(gpointer *) stmtptr);
g_return_if_fail (status == CR_OK && stmt);
g_return_if_fail (stmt->type == RULESET_STMT);
decl = cr_declaration_new (stmt, name, a_value);
g_return_if_fail (decl);
decl->important = a_important;
status = cr_statement_ruleset_append_decl (stmt, decl);
g_return_if_fail (status == CR_OK);
}
static void
parse_at_media_end_selector_cb (CRDocHandler * a_this,
CRSelector * a_sellist)
{
enum CRStatus status = CR_OK;
/*
*the current ruleset stmt, child of the
*current at-media being parsed.
*/
CRStatement *stmt = NULL;
CRStatement **stmtptr = NULL;
g_return_if_fail (a_this && a_sellist);
stmtptr = &stmt;
status = cr_doc_handler_get_ctxt (a_this, (gpointer *) stmtptr);
g_return_if_fail (status == CR_OK && stmt
&& stmt->type == RULESET_STMT);
g_return_if_fail (stmt->kind.ruleset->parent_media_rule);
status = cr_doc_handler_set_ctxt
(a_this, stmt->kind.ruleset->parent_media_rule);
g_return_if_fail (status == CR_OK);
}
static void
parse_at_media_end_media_cb (CRDocHandler * a_this,
GList * a_media_list)
{
enum CRStatus status = CR_OK;
CRStatement *at_media = NULL;
CRStatement **at_media_ptr = NULL;
g_return_if_fail (a_this && a_this->priv);
at_media_ptr = &at_media;
status = cr_doc_handler_get_ctxt (a_this,
(gpointer *) at_media_ptr);
g_return_if_fail (status == CR_OK && at_media);
status = cr_doc_handler_set_result (a_this, at_media);
}
static void
parse_ruleset_start_selector_cb (CRDocHandler * a_this,
CRSelector * a_sellist)
{
CRStatement *ruleset = NULL;
g_return_if_fail (a_this && a_this->priv && a_sellist);
ruleset = cr_statement_new_ruleset (NULL, a_sellist, NULL, NULL);
g_return_if_fail (ruleset);
cr_doc_handler_set_result (a_this, ruleset);
}
static void
parse_ruleset_unrecoverable_error_cb (CRDocHandler * a_this)
{
CRStatement *stmt = NULL;
CRStatement **stmtptr = NULL;
enum CRStatus status = CR_OK;
stmtptr = &stmt;
status = cr_doc_handler_get_result (a_this, (gpointer *) stmtptr);
if (status != CR_OK) {
cr_utils_trace_info ("Couldn't get parsing context. "
"This may lead to some memory leaks.");
return;
}
if (stmt) {
cr_statement_destroy (stmt);
stmt = NULL;
cr_doc_handler_set_result (a_this, NULL);
}
}
static void
parse_ruleset_property_cb (CRDocHandler * a_this,
CRString * a_name,
CRTerm * a_value, gboolean a_important)
{
enum CRStatus status = CR_OK;
CRStatement *ruleset = NULL;
CRStatement **rulesetptr = NULL;
CRDeclaration *decl = NULL;
CRString *stringue = NULL;
g_return_if_fail (a_this && a_this->priv && a_name);
stringue = cr_string_dup (a_name);
g_return_if_fail (stringue);
rulesetptr = &ruleset;
status = cr_doc_handler_get_result (a_this, (gpointer *) rulesetptr);
g_return_if_fail (status == CR_OK
&& ruleset
&& ruleset->type == RULESET_STMT);
decl = cr_declaration_new (ruleset, stringue, a_value);
g_return_if_fail (decl);
decl->important = a_important;
status = cr_statement_ruleset_append_decl (ruleset, decl);
g_return_if_fail (status == CR_OK);
}
static void
parse_ruleset_end_selector_cb (CRDocHandler * a_this,
CRSelector * a_sellist)
{
CRStatement *result = NULL;
CRStatement **resultptr = NULL;
enum CRStatus status = CR_OK;
g_return_if_fail (a_this && a_sellist);
resultptr = &result;
status = cr_doc_handler_get_result (a_this, (gpointer *) resultptr);
g_return_if_fail (status == CR_OK
&& result
&& result->type == RULESET_STMT);
}
static void
cr_statement_clear (CRStatement * a_this)
{
g_return_if_fail (a_this);
switch (a_this->type) {
case AT_RULE_STMT:
break;
case RULESET_STMT:
if (!a_this->kind.ruleset)
return;
if (a_this->kind.ruleset->sel_list) {
cr_selector_unref (a_this->kind.ruleset->sel_list);
a_this->kind.ruleset->sel_list = NULL;
}
if (a_this->kind.ruleset->decl_list) {
cr_declaration_destroy
(a_this->kind.ruleset->decl_list);
a_this->kind.ruleset->decl_list = NULL;
}
g_free (a_this->kind.ruleset);
a_this->kind.ruleset = NULL;
break;
case AT_IMPORT_RULE_STMT:
if (!a_this->kind.import_rule)
return;
if (a_this->kind.import_rule->url) {
cr_string_destroy
(a_this->kind.import_rule->url) ;
a_this->kind.import_rule->url = NULL;
}
g_free (a_this->kind.import_rule);
a_this->kind.import_rule = NULL;
break;
case AT_MEDIA_RULE_STMT:
if (!a_this->kind.media_rule)
return;
if (a_this->kind.media_rule->rulesets) {
cr_statement_destroy
(a_this->kind.media_rule->rulesets);
a_this->kind.media_rule->rulesets = NULL;
}
if (a_this->kind.media_rule->media_list) {
GList *cur = NULL;
for (cur = a_this->kind.media_rule->media_list;
cur; cur = cur->next) {
if (cur->data) {
cr_string_destroy ((CRString *) cur->data);
cur->data = NULL;
}
}
g_list_free (a_this->kind.media_rule->media_list);
a_this->kind.media_rule->media_list = NULL;
}
g_free (a_this->kind.media_rule);
a_this->kind.media_rule = NULL;
break;
case AT_PAGE_RULE_STMT:
if (!a_this->kind.page_rule)
return;
if (a_this->kind.page_rule->decl_list) {
cr_declaration_destroy
(a_this->kind.page_rule->decl_list);
a_this->kind.page_rule->decl_list = NULL;
}
if (a_this->kind.page_rule->name) {
cr_string_destroy
(a_this->kind.page_rule->name);
a_this->kind.page_rule->name = NULL;
}
if (a_this->kind.page_rule->pseudo) {
cr_string_destroy
(a_this->kind.page_rule->pseudo);
a_this->kind.page_rule->pseudo = NULL;
}
g_free (a_this->kind.page_rule);
a_this->kind.page_rule = NULL;
break;
case AT_CHARSET_RULE_STMT:
if (!a_this->kind.charset_rule)
return;
if (a_this->kind.charset_rule->charset) {
cr_string_destroy
(a_this->kind.charset_rule->charset);
a_this->kind.charset_rule->charset = NULL;
}
g_free (a_this->kind.charset_rule);
a_this->kind.charset_rule = NULL;
break;
case AT_FONT_FACE_RULE_STMT:
if (!a_this->kind.font_face_rule)
return;
if (a_this->kind.font_face_rule->decl_list) {
cr_declaration_unref
(a_this->kind.font_face_rule->decl_list);
a_this->kind.font_face_rule->decl_list = NULL;
}
g_free (a_this->kind.font_face_rule);
a_this->kind.font_face_rule = NULL;
break;
default:
break;
}
}
/**
* cr_statement_ruleset_to_string:
*
*@a_this: the current instance of #CRStatement
*@a_indent: the number of whitespace to use for indentation
*
*Serializes the ruleset statement into a string
*
*Returns the newly allocated serialised string. Must be freed
*by the caller, using g_free().
*/
static gchar *
cr_statement_ruleset_to_string (CRStatement const * a_this, glong a_indent)
{
GString *stringue = NULL;
gchar *tmp_str = NULL,
*result = NULL;
g_return_val_if_fail (a_this && a_this->type == RULESET_STMT, NULL);
stringue = g_string_new (NULL);
if (a_this->kind.ruleset->sel_list) {
if (a_indent)
cr_utils_dump_n_chars2 (' ', stringue, a_indent);
tmp_str =
(gchar *) cr_selector_to_string (a_this->kind.ruleset->
sel_list);
if (tmp_str) {
g_string_append (stringue, tmp_str);
g_free (tmp_str);
tmp_str = NULL;
}
}
g_string_append (stringue, " {\n");
if (a_this->kind.ruleset->decl_list) {
tmp_str = (gchar *) cr_declaration_list_to_string2
(a_this->kind.ruleset->decl_list,
a_indent + DECLARATION_INDENT_NB, TRUE);
if (tmp_str) {
g_string_append (stringue, tmp_str);
g_free (tmp_str);
tmp_str = NULL;
}
g_string_append (stringue, "\n");
cr_utils_dump_n_chars2 (' ', stringue, a_indent);
}
g_string_append (stringue, "}");
result = stringue->str;
if (stringue) {
g_string_free (stringue, FALSE);
stringue = NULL;
}
if (tmp_str) {
g_free (tmp_str);
tmp_str = NULL;
}
return result;
}
/**
* cr_statement_font_face_rule_to_string:
*
*@a_this: the current instance of #CRStatement to consider
*It must be a font face rule statement.
*@a_indent: the number of white spaces of indentation.
*
*Serializes a font face rule statement into a string.
*
*Returns the serialized string. Must be deallocated by the caller
*using g_free().
*/
static gchar *
cr_statement_font_face_rule_to_string (CRStatement const * a_this,
glong a_indent)
{
gchar *result = NULL, *tmp_str = NULL ;
GString *stringue = NULL ;
g_return_val_if_fail (a_this
&& a_this->type == AT_FONT_FACE_RULE_STMT,
NULL);
if (a_this->kind.font_face_rule->decl_list) {
stringue = g_string_new (NULL) ;
g_return_val_if_fail (stringue, NULL) ;
if (a_indent)
cr_utils_dump_n_chars2 (' ', stringue,
a_indent);
g_string_append (stringue, "@font-face {\n");
tmp_str = (gchar *) cr_declaration_list_to_string2
(a_this->kind.font_face_rule->decl_list,
a_indent + DECLARATION_INDENT_NB, TRUE) ;
if (tmp_str) {
g_string_append (stringue,
tmp_str) ;
g_free (tmp_str) ;
tmp_str = NULL ;
}
g_string_append (stringue, "\n}");
}
if (stringue) {
result = stringue->str ;
g_string_free (stringue, FALSE) ;
stringue = NULL ;
}
return result ;
}
/**
* cr_statement_charset_to_string:
*
*Serialises an \@charset statement into a string.
*@a_this: the statement to serialize.
*@a_indent: the number of indentation spaces
*
*Returns the serialized charset statement. Must be
*freed by the caller using g_free().
*/
static gchar *
cr_statement_charset_to_string (CRStatement const *a_this,
gulong a_indent)
{
gchar *str = NULL ;
GString *stringue = NULL ;
g_return_val_if_fail (a_this
&& a_this->type == AT_CHARSET_RULE_STMT,
NULL) ;
if (a_this->kind.charset_rule
&& a_this->kind.charset_rule->charset
&& a_this->kind.charset_rule->charset->stryng
&& a_this->kind.charset_rule->charset->stryng->str) {
str = g_strndup (a_this->kind.charset_rule->charset->stryng->str,
a_this->kind.charset_rule->charset->stryng->len);
g_return_val_if_fail (str, NULL);
stringue = g_string_new (NULL) ;
g_return_val_if_fail (stringue, NULL) ;
cr_utils_dump_n_chars2 (' ', stringue, a_indent);
g_string_append_printf (stringue,
"@charset \"%s\" ;", str);
if (str) {
g_free (str);
str = NULL;
}
}
if (stringue) {
str = stringue->str ;
g_string_free (stringue, FALSE) ;
}
return str ;
}
/**
* cr_statement_at_page_rule_to_string:
*
*Serialises the at page rule statement into a string
*@a_this: the current instance of #CRStatement. Must
*be an "\@page" rule statement.
*
*Returns the serialized string. Must be freed by the caller
*/
static gchar *
cr_statement_at_page_rule_to_string (CRStatement const *a_this,
gulong a_indent)
{
GString *stringue = NULL;
gchar *result = NULL ;
stringue = g_string_new (NULL) ;
cr_utils_dump_n_chars2 (' ', stringue, a_indent) ;
g_string_append (stringue, "@page");
if (a_this->kind.page_rule->name
&& a_this->kind.page_rule->name->stryng) {
g_string_append_printf
(stringue, " %s",
a_this->kind.page_rule->name->stryng->str) ;
} else {
g_string_append (stringue, " ");
}
if (a_this->kind.page_rule->pseudo
&& a_this->kind.page_rule->pseudo->stryng) {
g_string_append_printf
(stringue, " :%s",
a_this->kind.page_rule->pseudo->stryng->str) ;
}
if (a_this->kind.page_rule->decl_list) {
gchar *str = NULL ;
g_string_append (stringue, " {\n");
str = (gchar *) cr_declaration_list_to_string2
(a_this->kind.page_rule->decl_list,
a_indent + DECLARATION_INDENT_NB, TRUE) ;
if (str) {
g_string_append (stringue, str) ;
g_free (str) ;
str = NULL ;
}
g_string_append (stringue, "\n}\n");
}
result = stringue->str ;
g_string_free (stringue, FALSE) ;
stringue = NULL ;
return result ;
}
/**
*Serializes an \@media statement.
*@param a_this the current instance of #CRStatement
*@param a_indent the number of spaces of indentation.
*@return the serialized \@media statement. Must be freed
*by the caller using g_free().
*/
static gchar *
cr_statement_media_rule_to_string (CRStatement const *a_this,
gulong a_indent)
{
gchar *str = NULL ;
GString *stringue = NULL ;
GList const *cur = NULL;
g_return_val_if_fail (a_this->type == AT_MEDIA_RULE_STMT,
NULL);
if (a_this->kind.media_rule) {
stringue = g_string_new (NULL) ;
cr_utils_dump_n_chars2 (' ', stringue, a_indent);
g_string_append (stringue, "@media");
for (cur = a_this->kind.media_rule->media_list; cur;
cur = cur->next) {
if (cur->data) {
gchar *str2 = cr_string_dup2
((CRString const *) cur->data);
if (str2) {
if (cur->prev) {
g_string_append
(stringue,
",");
}
g_string_append_printf
(stringue,
" %s", str2);
g_free (str2);
str2 = NULL;
}
}
}
g_string_append (stringue, " {\n");
str = cr_statement_list_to_string
(a_this->kind.media_rule->rulesets,
a_indent + DECLARATION_INDENT_NB) ;
if (str) {
g_string_append (stringue, str) ;
g_free (str) ;
str = NULL ;
}
g_string_append (stringue, "\n}");
}
if (stringue) {
str = stringue->str ;
g_string_free (stringue, FALSE) ;
}
return str ;
}
static gchar *
cr_statement_import_rule_to_string (CRStatement const *a_this,
gulong a_indent)
{
GString *stringue = NULL ;
gchar *str = NULL;
g_return_val_if_fail (a_this
&& a_this->type == AT_IMPORT_RULE_STMT
&& a_this->kind.import_rule,
NULL) ;
if (a_this->kind.import_rule->url
&& a_this->kind.import_rule->url->stryng) {
stringue = g_string_new (NULL) ;
g_return_val_if_fail (stringue, NULL) ;
str = g_strndup (a_this->kind.import_rule->url->stryng->str,
a_this->kind.import_rule->url->stryng->len);
cr_utils_dump_n_chars2 (' ', stringue, a_indent);
if (str) {
g_string_append_printf (stringue,
"@import url(\"%s\")",
str);
g_free (str);
str = NULL ;
} else /*there is no url, so no import rule, get out! */
return NULL;
if (a_this->kind.import_rule->media_list) {
GList const *cur = NULL;
for (cur = a_this->kind.import_rule->media_list;
cur; cur = cur->next) {
if (cur->data) {
CRString const *crstr = cur->data;
if (cur->prev) {
g_string_append
(stringue, ", ");
}
if (crstr
&& crstr->stryng
&& crstr->stryng->str) {
g_string_append_len
(stringue,
crstr->stryng->str,
crstr->stryng->len) ;
}
}
}
}
g_string_append (stringue, " ;");
}
if (stringue) {
str = stringue->str ;
g_string_free (stringue, FALSE) ;
stringue = NULL ;
}
return str ;
}
/*******************
*public functions
******************/
/**
* cr_statement_does_buf_parses_against_core:
*
*@a_buf: the buffer to parse.
*@a_encoding: the character encoding of a_buf.
*
*Tries to parse a buffer and says whether if the content of the buffer
*is a css statement as defined by the "Core CSS Grammar" (chapter 4 of the
*css spec) or not.
*
*Returns TRUE if the buffer parses against the core grammar, false otherwise.
*/
gboolean
cr_statement_does_buf_parses_against_core (const guchar * a_buf,
enum CREncoding a_encoding)
{
CRParser *parser = NULL;
enum CRStatus status = CR_OK;
gboolean result = FALSE;
parser = cr_parser_new_from_buf ((guchar*)a_buf, strlen ((const char *) a_buf),
a_encoding, FALSE);
g_return_val_if_fail (parser, FALSE);
status = cr_parser_set_use_core_grammar (parser, TRUE);
if (status != CR_OK) {
goto cleanup;
}
status = cr_parser_parse_statement_core (parser);
if (status == CR_OK) {
result = TRUE;
}
cleanup:
if (parser) {
cr_parser_destroy (parser);
}
return result;
}
/**
* cr_statement_parse_from_buf:
*
*@a_buf: the buffer to parse.
*@a_encoding: the character encoding of a_buf.
*
*Parses a buffer that contains a css statement and returns
*an instance of #CRStatement in case of successful parsing.
*TODO: at support of "\@import" rules.
*
*Returns the newly built instance of #CRStatement in case
*of successful parsing, NULL otherwise.
*/
CRStatement *
cr_statement_parse_from_buf (const guchar * a_buf, enum CREncoding a_encoding)
{
CRStatement *result = NULL;
/*
*The strategy of this function is "brute force".
*It tries to parse all the types of CRStatement it knows about.
*I could do this a smarter way but I don't have the time now.
*I think I will revisit this when time of performances and
*pull based incremental parsing comes.
*/
result = cr_statement_ruleset_parse_from_buf (a_buf, a_encoding);
if (!result) {
result = cr_statement_at_charset_rule_parse_from_buf
(a_buf, a_encoding);
} else {
goto out;
}
if (!result) {
result = cr_statement_at_media_rule_parse_from_buf
(a_buf, a_encoding);
} else {
goto out;
}
if (!result) {
result = cr_statement_at_charset_rule_parse_from_buf
(a_buf, a_encoding);
} else {
goto out;
}
if (!result) {
result = cr_statement_font_face_rule_parse_from_buf
(a_buf, a_encoding);
} else {
goto out;
}
if (!result) {
result = cr_statement_at_page_rule_parse_from_buf
(a_buf, a_encoding);
} else {
goto out;
}
if (!result) {
result = cr_statement_at_import_rule_parse_from_buf
(a_buf, a_encoding);
} else {
goto out;
}
out:
return result;
}
/**
* cr_statement_ruleset_parse_from_buf:
*
*@a_buf: the buffer to parse.
*@a_enc: the character encoding of a_buf.
*
*Parses a buffer that contains a ruleset statement an instanciates
*a #CRStatement of type RULESET_STMT.
*
*Returns the newly built instance of #CRStatement in case of successful parsing,
*NULL otherwise.
*/
CRStatement *
cr_statement_ruleset_parse_from_buf (const guchar * a_buf,
enum CREncoding a_enc)
{
enum CRStatus status = CR_OK;
CRStatement *result = NULL;
CRStatement **resultptr = NULL;
CRParser *parser = NULL;
CRDocHandler *sac_handler = NULL;
g_return_val_if_fail (a_buf, NULL);
parser = cr_parser_new_from_buf ((guchar*)a_buf, strlen ((const char *) a_buf),
a_enc, FALSE);
g_return_val_if_fail (parser, NULL);
sac_handler = cr_doc_handler_new ();
g_return_val_if_fail (parser, NULL);
sac_handler->start_selector = parse_ruleset_start_selector_cb;
sac_handler->end_selector = parse_ruleset_end_selector_cb;
sac_handler->property = parse_ruleset_property_cb;
sac_handler->unrecoverable_error =
parse_ruleset_unrecoverable_error_cb;
cr_parser_set_sac_handler (parser, sac_handler);
cr_parser_try_to_skip_spaces_and_comments (parser);
status = cr_parser_parse_ruleset (parser);
if (status != CR_OK) {
goto cleanup;
}
resultptr = &result;
status = cr_doc_handler_get_result (sac_handler,
(gpointer *) resultptr);
if (!((status == CR_OK) && result)) {
if (result) {
cr_statement_destroy (result);
result = NULL;
}
}
cleanup:
if (parser) {
cr_parser_destroy (parser);
parser = NULL;
sac_handler = NULL ;
}
if (sac_handler) {
cr_doc_handler_unref (sac_handler);
sac_handler = NULL;
}
return result;
}
/**
* cr_statement_new_ruleset:
*
*@a_sel_list: the list of #CRSimpleSel (selectors)
*the rule applies to.
*@a_decl_list: the list of instances of #CRDeclaration
*that composes the ruleset.
*@a_media_types: a list of instances of GString that
*describe the media list this ruleset applies to.
*
*Creates a new instance of #CRStatement of type
*#CRRulSet.
*
*Returns the new instance of #CRStatement or NULL if something
*went wrong.
*/
CRStatement *
cr_statement_new_ruleset (CRStyleSheet * a_sheet,
CRSelector * a_sel_list,
CRDeclaration * a_decl_list,
CRStatement * a_parent_media_rule)
{
CRStatement *result = NULL;
g_return_val_if_fail (a_sel_list, NULL);
if (a_parent_media_rule) {
g_return_val_if_fail
(a_parent_media_rule->type == AT_MEDIA_RULE_STMT,
NULL);
g_return_val_if_fail (a_parent_media_rule->kind.media_rule,
NULL);
}
result = g_try_malloc (sizeof (CRStatement));
if (!result) {
cr_utils_trace_info ("Out of memory");
return NULL;
}
memset (result, 0, sizeof (CRStatement));
result->type = RULESET_STMT;
result->kind.ruleset = g_try_malloc (sizeof (CRRuleSet));
if (!result->kind.ruleset) {
cr_utils_trace_info ("Out of memory");
if (result)
g_free (result);
return NULL;
}
memset (result->kind.ruleset, 0, sizeof (CRRuleSet));
result->kind.ruleset->sel_list = a_sel_list;
if (a_sel_list)
cr_selector_ref (a_sel_list);
result->kind.ruleset->decl_list = a_decl_list;
if (a_parent_media_rule) {
result->kind.ruleset->parent_media_rule = a_parent_media_rule;
a_parent_media_rule->kind.media_rule->rulesets =
cr_statement_append
(a_parent_media_rule->kind.media_rule->rulesets,
result);
}
cr_statement_set_parent_sheet (result, a_sheet);
return result;
}
/**
* cr_statement_at_media_rule_parse_from_buf:
*
*@a_buf: the input to parse.
*@a_enc: the encoding of the buffer.
*
*Parses a buffer that contains an "\@media" declaration
*and builds an \@media css statement.
*
*Returns the \@media statement, or NULL if the buffer could not
*be successfully parsed.
*/
CRStatement *
cr_statement_at_media_rule_parse_from_buf (const guchar * a_buf,
enum CREncoding a_enc)
{
CRParser *parser = NULL;
CRStatement *result = NULL;
CRStatement **resultptr = NULL;
CRDocHandler *sac_handler = NULL;
enum CRStatus status = CR_OK;
parser = cr_parser_new_from_buf ((guchar*)a_buf, strlen ((const char *) a_buf),
a_enc, FALSE);
if (!parser) {
cr_utils_trace_info ("Instantiation of the parser failed");
goto cleanup;
}
sac_handler = cr_doc_handler_new ();
if (!sac_handler) {
cr_utils_trace_info
("Instantiation of the sac handler failed");
goto cleanup;
}
sac_handler->start_media = parse_at_media_start_media_cb;
sac_handler->start_selector = parse_at_media_start_selector_cb;
sac_handler->property = parse_at_media_property_cb;
sac_handler->end_selector = parse_at_media_end_selector_cb;
sac_handler->end_media = parse_at_media_end_media_cb;
sac_handler->unrecoverable_error =
parse_at_media_unrecoverable_error_cb;
status = cr_parser_set_sac_handler (parser, sac_handler);
if (status != CR_OK)
goto cleanup;
status = cr_parser_try_to_skip_spaces_and_comments (parser);
if (status != CR_OK)
goto cleanup;
status = cr_parser_parse_media (parser);
if (status != CR_OK)
goto cleanup;
resultptr = &result;
status = cr_doc_handler_get_result (sac_handler,
(gpointer *) resultptr);
if (status != CR_OK)
goto cleanup;
cleanup:
if (parser) {
cr_parser_destroy (parser);
parser = NULL;
sac_handler = NULL ;
}
if (sac_handler) {
cr_doc_handler_unref (sac_handler);
sac_handler = NULL;
}
return result;
}
/**
* cr_statement_new_at_media_rule:
*
*@a_ruleset: the ruleset statements contained
*in the \@media rule.
*@a_media: the media string list. A list of GString pointers.
*
*Instanciates an instance of #CRStatement of type
*AT_MEDIA_RULE_STMT (\@media ruleset).
*
*/
CRStatement *
cr_statement_new_at_media_rule (CRStyleSheet * a_sheet,
CRStatement * a_rulesets, GList * a_media)
{
CRStatement *result = NULL,
*cur = NULL;
if (a_rulesets)
g_return_val_if_fail (a_rulesets->type == RULESET_STMT, NULL);
result = g_try_malloc (sizeof (CRStatement));
if (!result) {
cr_utils_trace_info ("Out of memory");
return NULL;
}
memset (result, 0, sizeof (CRStatement));
result->type = AT_MEDIA_RULE_STMT;
result->kind.media_rule = g_try_malloc (sizeof (CRAtMediaRule));
if (!result->kind.media_rule) {
cr_utils_trace_info ("Out of memory");
g_free (result);
return NULL;
}
memset (result->kind.media_rule, 0, sizeof (CRAtMediaRule));
result->kind.media_rule->rulesets = a_rulesets;
for (cur = a_rulesets; cur; cur = cur->next) {
if (cur->type != RULESET_STMT || !cur->kind.ruleset) {
cr_utils_trace_info ("Bad parameter a_rulesets. "
"It should be a list of "
"correct ruleset statement only !");
goto error;
}
cur->kind.ruleset->parent_media_rule = result;
}
result->kind.media_rule->media_list = a_media;
if (a_sheet) {
cr_statement_set_parent_sheet (result, a_sheet);
}
return result;
error:
return NULL;
}
/**
* cr_statement_new_at_import_rule:
*
*@a_url: the url to connect to the get the file
*to be imported.
*@a_sheet: the imported parsed stylesheet.
*
*Creates a new instance of #CRStatment of type
*#CRAtImportRule.
*
*Returns the newly built instance of #CRStatement.
*/
CRStatement *
cr_statement_new_at_import_rule (CRStyleSheet * a_container_sheet,
CRString * a_url,
GList * a_media_list,
CRStyleSheet * a_imported_sheet)
{
CRStatement *result = NULL;
result = g_try_malloc (sizeof (CRStatement));
if (!result) {
cr_utils_trace_info ("Out of memory");
return NULL;
}
memset (result, 0, sizeof (CRStatement));
result->type = AT_IMPORT_RULE_STMT;
result->kind.import_rule = g_try_malloc (sizeof (CRAtImportRule));
if (!result->kind.import_rule) {
cr_utils_trace_info ("Out of memory");
g_free (result);
return NULL;
}
memset (result->kind.import_rule, 0, sizeof (CRAtImportRule));
result->kind.import_rule->url = a_url;
result->kind.import_rule->media_list = a_media_list;
result->kind.import_rule->sheet = a_imported_sheet;
if (a_container_sheet)
cr_statement_set_parent_sheet (result, a_container_sheet);
return result;
}
/**
* cr_statement_at_import_rule_parse_from_buf:
*
*@a_buf: the buffer to parse.
*@a_encoding: the encoding of a_buf.
*
*Parses a buffer that contains an "\@import" rule and
*instanciate a #CRStatement of type AT_IMPORT_RULE_STMT
*
*Returns the newly built instance of #CRStatement in case of
*a successful parsing, NULL otherwise.
*/
CRStatement *
cr_statement_at_import_rule_parse_from_buf (const guchar * a_buf,
enum CREncoding a_encoding)
{
enum CRStatus status = CR_OK;
CRParser *parser = NULL;
CRStatement *result = NULL;
GList *media_list = NULL;
CRString *import_string = NULL;
CRParsingLocation location = {0} ;
parser = cr_parser_new_from_buf ((guchar*)a_buf, strlen ((const char *) a_buf),
a_encoding, FALSE);
if (!parser) {
cr_utils_trace_info ("Instantiation of parser failed.");
goto cleanup;
}
status = cr_parser_try_to_skip_spaces_and_comments (parser);
if (status != CR_OK)
goto cleanup;
status = cr_parser_parse_import (parser,
&media_list,
&import_string,
&location);
if (status != CR_OK || !import_string)
goto cleanup;
result = cr_statement_new_at_import_rule (NULL, import_string,
media_list, NULL);
if (result) {
cr_parsing_location_copy (&result->location,
&location) ;
import_string = NULL;
media_list = NULL;
}
cleanup:
if (parser) {
cr_parser_destroy (parser);
parser = NULL;
}
if (media_list) {
for (; media_list;
media_list = g_list_next (media_list)) {
if (media_list->data) {
cr_string_destroy ((CRString*)media_list->data);
media_list->data = NULL;
}
}
g_list_free (media_list);
media_list = NULL;
}
if (import_string) {
cr_string_destroy (import_string);
import_string = NULL;
}
return result;
}
/**
* cr_statement_new_at_page_rule:
*
*@a_decl_list: a list of instances of #CRDeclarations
*which is actually the list of declarations that applies to
*this page rule.
*@a_selector: the page rule selector.
*
*Creates a new instance of #CRStatement of type
*#CRAtPageRule.
*
*Returns the newly built instance of #CRStatement or NULL
*in case of error.
*/
CRStatement *
cr_statement_new_at_page_rule (CRStyleSheet * a_sheet,
CRDeclaration * a_decl_list,
CRString * a_name, CRString * a_pseudo)
{
CRStatement *result = NULL;
result = g_try_malloc (sizeof (CRStatement));
if (!result) {
cr_utils_trace_info ("Out of memory");
return NULL;
}
memset (result, 0, sizeof (CRStatement));
result->type = AT_PAGE_RULE_STMT;
result->kind.page_rule = g_try_malloc (sizeof (CRAtPageRule));
if (!result->kind.page_rule) {
cr_utils_trace_info ("Out of memory");
g_free (result);
return NULL;
}
memset (result->kind.page_rule, 0, sizeof (CRAtPageRule));
if (a_decl_list) {
result->kind.page_rule->decl_list = a_decl_list;
cr_declaration_ref (a_decl_list);
}
result->kind.page_rule->name = a_name;
result->kind.page_rule->pseudo = a_pseudo;
if (a_sheet)
cr_statement_set_parent_sheet (result, a_sheet);
return result;
}
/**
* cr_statement_at_page_rule_parse_from_buf:
*
*@a_buf: the character buffer to parse.
*@a_encoding: the character encoding of a_buf.
*
*Parses a buffer that contains an "\@page" production and,
*if the parsing succeeds, builds the page statement.
*
*Returns the newly built at page statement in case of successful parsing,
*NULL otherwise.
*/
CRStatement *
cr_statement_at_page_rule_parse_from_buf (const guchar * a_buf,
enum CREncoding a_encoding)
{
enum CRStatus status = CR_OK;
CRParser *parser = NULL;
CRDocHandler *sac_handler = NULL;
CRStatement *result = NULL;
CRStatement **resultptr = NULL;
g_return_val_if_fail (a_buf, NULL);
parser = cr_parser_new_from_buf ((guchar*)a_buf, strlen ((const char *) a_buf),
a_encoding, FALSE);
if (!parser) {
cr_utils_trace_info ("Instantiation of the parser failed.");
goto cleanup;
}
sac_handler = cr_doc_handler_new ();
if (!sac_handler) {
cr_utils_trace_info
("Instantiation of the sac handler failed.");
goto cleanup;
}
sac_handler->start_page = parse_page_start_page_cb;
sac_handler->property = parse_page_property_cb;
sac_handler->end_page = parse_page_end_page_cb;
sac_handler->unrecoverable_error = parse_page_unrecoverable_error_cb;
status = cr_parser_set_sac_handler (parser, sac_handler);
if (status != CR_OK)
goto cleanup;
/*Now, invoke the parser to parse the "@page production" */
cr_parser_try_to_skip_spaces_and_comments (parser);
if (status != CR_OK)
goto cleanup;
status = cr_parser_parse_page (parser);
if (status != CR_OK)
goto cleanup;
resultptr = &result;
status = cr_doc_handler_get_result (sac_handler,
(gpointer *) resultptr);
cleanup:
if (parser) {
cr_parser_destroy (parser);
parser = NULL;
sac_handler = NULL ;
}
if (sac_handler) {
cr_doc_handler_unref (sac_handler);
sac_handler = NULL;
}
return result;
}
/**
* cr_statement_new_at_charset_rule:
*
*@a_charset: the string representing the charset.
*Note that the newly built instance of #CRStatement becomes
*the owner of a_charset. The caller must not free a_charset !!!.
*
*Creates a new instance of #CRStatement of type
*#CRAtCharsetRule.
*
*Returns the newly built instance of #CRStatement or NULL
*if an error arises.
*/
CRStatement *
cr_statement_new_at_charset_rule (CRStyleSheet * a_sheet,
CRString * a_charset)
{
CRStatement *result = NULL;
g_return_val_if_fail (a_charset, NULL);
result = g_try_malloc (sizeof (CRStatement));
if (!result) {
cr_utils_trace_info ("Out of memory");
return NULL;
}
memset (result, 0, sizeof (CRStatement));
result->type = AT_CHARSET_RULE_STMT;
result->kind.charset_rule = g_try_malloc (sizeof (CRAtCharsetRule));
if (!result->kind.charset_rule) {
cr_utils_trace_info ("Out of memory");
g_free (result);
return NULL;
}
memset (result->kind.charset_rule, 0, sizeof (CRAtCharsetRule));
result->kind.charset_rule->charset = a_charset;
cr_statement_set_parent_sheet (result, a_sheet);
return result;
}
/**
* cr_statement_at_charset_rule_parse_from_buf:
*
*@a_buf: the buffer to parse.
*@a_encoding: the character encoding of the buffer.
*
*Parses a buffer that contains an '\@charset' rule and
*creates an instance of #CRStatement of type AT_CHARSET_RULE_STMT.
*
*Returns the newly built instance of #CRStatement.
*/
CRStatement *
cr_statement_at_charset_rule_parse_from_buf (const guchar * a_buf,
enum CREncoding a_encoding)
{
enum CRStatus status = CR_OK;
CRParser *parser = NULL;
CRStatement *result = NULL;
CRString *charset = NULL;
g_return_val_if_fail (a_buf, NULL);
parser = cr_parser_new_from_buf ((guchar*)a_buf, strlen ((const char *) a_buf),
a_encoding, FALSE);
if (!parser) {
cr_utils_trace_info ("Instantiation of the parser failed.");
goto cleanup;
}
/*Now, invoke the parser to parse the "@charset production" */
cr_parser_try_to_skip_spaces_and_comments (parser);
if (status != CR_OK)
goto cleanup;
status = cr_parser_parse_charset (parser, &charset, NULL);
if (status != CR_OK || !charset)
goto cleanup;
result = cr_statement_new_at_charset_rule (NULL, charset);
if (result)
charset = NULL;
cleanup:
if (parser) {
cr_parser_destroy (parser);
parser = NULL;
}
if (charset) {
cr_string_destroy (charset);
}
return result;
}
/**
* cr_statement_new_at_font_face_rule:
*
*@a_font_decls: a list of instances of #CRDeclaration. Each declaration
*is actually a font declaration.
*
*Creates an instance of #CRStatement of type #CRAtFontFaceRule.
*
*Returns the newly built instance of #CRStatement.
*/
CRStatement *
cr_statement_new_at_font_face_rule (CRStyleSheet * a_sheet,
CRDeclaration * a_font_decls)
{
CRStatement *result = NULL;
result = g_try_malloc (sizeof (CRStatement));
if (!result) {
cr_utils_trace_info ("Out of memory");
return NULL;
}
memset (result, 0, sizeof (CRStatement));
result->type = AT_FONT_FACE_RULE_STMT;
result->kind.font_face_rule = g_try_malloc
(sizeof (CRAtFontFaceRule));
if (!result->kind.font_face_rule) {
cr_utils_trace_info ("Out of memory");
g_free (result);
return NULL;
}
memset (result->kind.font_face_rule, 0, sizeof (CRAtFontFaceRule));
result->kind.font_face_rule->decl_list = a_font_decls;
if (a_sheet)
cr_statement_set_parent_sheet (result, a_sheet);
return result;
}
/**
* cr_statement_font_face_rule_parse_from_buf:
*
*
*@a_buf: the buffer to parse.
*@a_encoding: the character encoding of a_buf.
*
*Parses a buffer that contains an "\@font-face" rule and builds
*an instance of #CRStatement of type AT_FONT_FACE_RULE_STMT out of it.
*
*Returns the newly built instance of #CRStatement in case of successufull
*parsing, NULL otherwise.
*/
CRStatement *
cr_statement_font_face_rule_parse_from_buf (const guchar * a_buf,
enum CREncoding a_encoding)
{
CRStatement *result = NULL;
CRStatement **resultptr = NULL;
CRParser *parser = NULL;
CRDocHandler *sac_handler = NULL;
enum CRStatus status = CR_OK;
parser = cr_parser_new_from_buf ((guchar*)a_buf, strlen ((const char *) a_buf),
a_encoding, FALSE);
if (!parser)
goto cleanup;
sac_handler = cr_doc_handler_new ();
if (!sac_handler)
goto cleanup;
/*
*set sac callbacks here
*/
sac_handler->start_font_face = parse_font_face_start_font_face_cb;
sac_handler->property = parse_font_face_property_cb;
sac_handler->end_font_face = parse_font_face_end_font_face_cb;
sac_handler->unrecoverable_error =
parse_font_face_unrecoverable_error_cb;
status = cr_parser_set_sac_handler (parser, sac_handler);
if (status != CR_OK)
goto cleanup;
/*
*cleanup spaces of comment that may be there before the real
*"@font-face" thing.
*/
status = cr_parser_try_to_skip_spaces_and_comments (parser);
if (status != CR_OK)
goto cleanup;
status = cr_parser_parse_font_face (parser);
if (status != CR_OK)
goto cleanup;
resultptr = &result;
status = cr_doc_handler_get_result (sac_handler,
(gpointer *) resultptr);
if (status != CR_OK || !result)
goto cleanup;
cleanup:
if (parser) {
cr_parser_destroy (parser);
parser = NULL;
sac_handler = NULL ;
}
if (sac_handler) {
cr_doc_handler_unref (sac_handler);
sac_handler = NULL;
}
return result;
}
/**
* cr_statement_set_parent_sheet:
*
*@a_this: the current instance of #CRStatement.
*@a_sheet: the sheet that contains the current statement.
*
*Sets the container stylesheet.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_set_parent_sheet (CRStatement * a_this, CRStyleSheet * a_sheet)
{
g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
a_this->parent_sheet = a_sheet;
return CR_OK;
}
/**
* cr_statement_get_parent_sheet:
*
*@a_this: the current #CRStatement.
*@a_sheet: out parameter. A pointer to the sheets that
*
*Gets the sheets that contains the current statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_get_parent_sheet (CRStatement * a_this, CRStyleSheet ** a_sheet)
{
g_return_val_if_fail (a_this && a_sheet, CR_BAD_PARAM_ERROR);
*a_sheet = a_this->parent_sheet;
return CR_OK;
}
/**
* cr_statement_append:
*
*@a_this: the current instance of the statement list.
*@a_new: a_new the new instance of #CRStatement to append.
*
*Appends a new statement to the statement list.
*
*Returns the new list statement list, or NULL in cas of failure.
*/
CRStatement *
cr_statement_append (CRStatement * a_this, CRStatement * a_new)
{
CRStatement *cur = NULL;
g_return_val_if_fail (a_new, NULL);
if (!a_this) {
return a_new;
}
/*walk forward in the current list to find the tail list element */
for (cur = a_this; cur && cur->next; cur = cur->next) ;
cur->next = a_new;
a_new->prev = cur;
return a_this;
}
/**
* cr_statement_prepend:
*
*@a_this: the current instance of #CRStatement.
*@a_new: the new statement to prepend.
*
*Prepends the an instance of #CRStatement to
*the current statement list.
*
*Returns the new list with the new statement prepended,
*or NULL in case of an error.
*/
CRStatement *
cr_statement_prepend (CRStatement * a_this, CRStatement * a_new)
{
CRStatement *cur = NULL;
g_return_val_if_fail (a_new, NULL);
if (!a_this)
return a_new;
a_new->next = a_this;
a_this->prev = a_new;
/*walk backward in the prepended list to find the head list element */
for (cur = a_new; cur && cur->prev; cur = cur->prev) ;
return cur;
}
/**
* cr_statement_unlink:
*
*@a_this: the current statements list.
*@a_to_unlink: the statement to unlink from the list.
*
*Unlinks a statement from the statements list.
*
*Returns the new list where a_to_unlink has been unlinked
*from, or NULL in case of error.
*/
CRStatement *
cr_statement_unlink (CRStatement * a_stmt)
{
CRStatement *result = a_stmt;
g_return_val_if_fail (result, NULL);
/**
*Some sanity checks first
*/
if (a_stmt->next) {
g_return_val_if_fail (a_stmt->next->prev == a_stmt, NULL);
}
if (a_stmt->prev) {
g_return_val_if_fail (a_stmt->prev->next == a_stmt, NULL);
}
/**
*Now, the real unlinking job.
*/
if (a_stmt->next) {
a_stmt->next->prev = a_stmt->prev;
}
if (a_stmt->prev) {
a_stmt->prev->next = a_stmt->next;
}
if (a_stmt->parent_sheet
&& a_stmt->parent_sheet->statements == a_stmt) {
a_stmt->parent_sheet->statements =
a_stmt->parent_sheet->statements->next;
}
a_stmt->next = NULL;
a_stmt->prev = NULL;
a_stmt->parent_sheet = NULL;
return result;
}
/**
* cr_statement_nr_rules:
*
*@a_this: the current instance of #CRStatement.
*
*Gets the number of rules in the statement list;
*
*Returns number of rules in the statement list.
*/
gint
cr_statement_nr_rules (CRStatement const * a_this)
{
CRStatement const *cur = NULL;
int nr = 0;
g_return_val_if_fail (a_this, -1);
for (cur = a_this; cur; cur = cur->next)
nr++;
return nr;
}
/**
* cr_statement_get_from_list:
*
*@a_this: the current instance of #CRStatement.
*@itemnr: the index into the statement list.
*
*Use an index to get a CRStatement from the statement list.
*
*Returns CRStatement at position itemnr, if itemnr > number of statements - 1,
*it will return NULL.
*/
CRStatement *
cr_statement_get_from_list (CRStatement * a_this, int itemnr)
{
CRStatement *cur = NULL;
int nr = 0;
g_return_val_if_fail (a_this, NULL);
for (cur = a_this; cur; cur = cur->next)
if (nr++ == itemnr)
return cur;
return NULL;
}
/**
* cr_statement_ruleset_set_sel_list:
*
*@a_this: the current ruleset statement.
*@a_sel_list: the selector list to set. Note
*that this function increments the ref count of a_sel_list.
*The sel list will be destroyed at the destruction of the
*current instance of #CRStatement.
*
*Sets a selector list to a ruleset statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_ruleset_set_sel_list (CRStatement * a_this,
CRSelector * a_sel_list)
{
g_return_val_if_fail (a_this && a_this->type == RULESET_STMT,
CR_BAD_PARAM_ERROR);
if (a_this->kind.ruleset->sel_list)
cr_selector_unref (a_this->kind.ruleset->sel_list);
a_this->kind.ruleset->sel_list = a_sel_list;
if (a_sel_list)
cr_selector_ref (a_sel_list);
return CR_OK;
}
/**
* cr_statement_ruleset_get_declarations:
*
*@a_this: the current instance of #CRStatement.
*@a_decl_list: out parameter. A pointer to the the returned
*list of declaration. Must not be NULL.
*
*Gets a pointer to the list of declaration contained
*in the ruleset statement.
*
*Returns CR_OK upon successful completion, an error code if something
*bad happened.
*/
enum CRStatus
cr_statement_ruleset_get_declarations (CRStatement * a_this,
CRDeclaration ** a_decl_list)
{
g_return_val_if_fail (a_this
&& a_this->type == RULESET_STMT
&& a_this->kind.ruleset
&& a_decl_list, CR_BAD_PARAM_ERROR);
*a_decl_list = a_this->kind.ruleset->decl_list;
return CR_OK;
}
/**
* cr_statement_ruleset_get_sel_list:
*
*@a_this: the current ruleset statement.
*@a_list: out parameter. The returned selector list,
*if and only if the function returned CR_OK.
*
*Gets a pointer to the selector list contained in
*the current ruleset statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_ruleset_get_sel_list (CRStatement const * a_this, CRSelector ** a_list)
{
g_return_val_if_fail (a_this && a_this->type == RULESET_STMT
&& a_this->kind.ruleset, CR_BAD_PARAM_ERROR);
*a_list = a_this->kind.ruleset->sel_list;
return CR_OK;
}
/**
* cr_statement_ruleset_set_decl_list:
*
*@a_this: the current ruleset statement.
*@a_list: the declaration list to be added to the current
*ruleset statement.
*
*Sets a declaration list to the current ruleset statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_ruleset_set_decl_list (CRStatement * a_this,
CRDeclaration * a_list)
{
g_return_val_if_fail (a_this && a_this->type == RULESET_STMT
&& a_this->kind.ruleset, CR_BAD_PARAM_ERROR);
if (a_this->kind.ruleset->decl_list == a_list)
return CR_OK;
if (a_this->kind.ruleset->sel_list) {
cr_declaration_destroy (a_this->kind.ruleset->decl_list);
}
a_this->kind.ruleset->sel_list = NULL;
return CR_OK;
}
/**
* cr_statement_ruleset_append_decl2:
*
*@a_this: the current statement.
*@a_prop: the property of the declaration.
*@a_value: the value of the declaration.
*
*Appends a declaration to the current ruleset statement.
*
*Returns CR_OK upon successful completion, an error code
*otherwise.
*/
enum CRStatus
cr_statement_ruleset_append_decl2 (CRStatement * a_this,
CRString * a_prop,
CRTerm * a_value)
{
CRDeclaration *new_decls = NULL;
g_return_val_if_fail (a_this && a_this->type == RULESET_STMT
&& a_this->kind.ruleset, CR_BAD_PARAM_ERROR);
new_decls = cr_declaration_append2
(a_this->kind.ruleset->decl_list,
a_prop, a_value);
g_return_val_if_fail (new_decls, CR_ERROR);
a_this->kind.ruleset->decl_list = new_decls;
return CR_OK;
}
/**
* cr_statement_ruleset_append_decl:
*
*Appends a declaration to the current statement.
*
*@a_this: the current statement.
*@a_declaration: the declaration to append.
*
*Returns CR_OK upon sucessful completion, an error code
*otherwise.
*/
enum CRStatus
cr_statement_ruleset_append_decl (CRStatement * a_this,
CRDeclaration * a_decl)
{
CRDeclaration *new_decls = NULL;
g_return_val_if_fail (a_this && a_this->type == RULESET_STMT
&& a_this->kind.ruleset, CR_BAD_PARAM_ERROR);
new_decls = cr_declaration_append
(a_this->kind.ruleset->decl_list, a_decl);
g_return_val_if_fail (new_decls, CR_ERROR);
a_this->kind.ruleset->decl_list = new_decls;
return CR_OK;
}
/**
* cr_statement_at_import_rule_set_imported_sheet:
*
*Sets a stylesheet to the current \@import rule.
*@a_this: the current \@import rule.
*@a_sheet: the stylesheet. The stylesheet is owned
*by the current instance of #CRStatement, that is, the
*stylesheet will be destroyed when the current instance
*of #CRStatement is destroyed.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_import_rule_set_imported_sheet (CRStatement * a_this,
CRStyleSheet * a_sheet)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_IMPORT_RULE_STMT
&& a_this->kind.import_rule,
CR_BAD_PARAM_ERROR);
a_this->kind.import_rule->sheet = a_sheet;
return CR_OK;
}
/**
* cr_statement_at_import_rule_get_imported_sheet:
*
*@a_this: the current \@import rule statement.
*@a_sheet: out parameter. The returned stylesheet if and
*only if the function returns CR_OK.
*
*Gets the stylesheet contained by the \@import rule statement.
*Returns CR_OK upon sucessful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_import_rule_get_imported_sheet (CRStatement * a_this,
CRStyleSheet ** a_sheet)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_IMPORT_RULE_STMT
&& a_this->kind.import_rule,
CR_BAD_PARAM_ERROR);
*a_sheet = a_this->kind.import_rule->sheet;
return CR_OK;
}
/**
* cr_statement_at_import_rule_set_url:
*
*@a_this: the current \@import rule statement.
*@a_url: the url to set.
*
*Sets an url to the current \@import rule statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_import_rule_set_url (CRStatement * a_this,
CRString * a_url)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_IMPORT_RULE_STMT
&& a_this->kind.import_rule,
CR_BAD_PARAM_ERROR);
if (a_this->kind.import_rule->url) {
cr_string_destroy (a_this->kind.import_rule->url);
}
a_this->kind.import_rule->url = a_url;
return CR_OK;
}
/**
* cr_statement_at_import_rule_get_url:
*
*@a_this: the current \@import rule statement.
*@a_url: out parameter. The returned url if
*and only if the function returned CR_OK.
*
*Gets the url of the \@import rule statement.
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_import_rule_get_url (CRStatement const * a_this,
CRString ** a_url)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_IMPORT_RULE_STMT
&& a_this->kind.import_rule,
CR_BAD_PARAM_ERROR);
*a_url = a_this->kind.import_rule->url;
return CR_OK;
}
/**
* cr_statement_at_media_nr_rules:
*
*@a_this: the current instance of #CRStatement.
*
*Returns the number of rules in the media rule;
*/
int
cr_statement_at_media_nr_rules (CRStatement const * a_this)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_MEDIA_RULE_STMT
&& a_this->kind.media_rule, CR_BAD_PARAM_ERROR);
return cr_statement_nr_rules (a_this->kind.media_rule->rulesets);
}
/**
* cr_statement_at_media_get_from_list:
*
*@a_this: the current instance of #CRStatement.
*@itemnr: the index into the media rule list of rules.
*
*Use an index to get a CRStatement from the media rule list of rules.
*
*Returns CRStatement at position itemnr, if itemnr > number of rules - 1,
*it will return NULL.
*/
CRStatement *
cr_statement_at_media_get_from_list (CRStatement * a_this, int itemnr)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_MEDIA_RULE_STMT
&& a_this->kind.media_rule, NULL);
return cr_statement_get_from_list (a_this->kind.media_rule->rulesets,
itemnr);
}
/**
* cr_statement_at_page_rule_set_declarations:
*
*@a_this: the current \@page rule statement.
*@a_decl_list: the declaration list to add. Will be freed
*by the current instance of #CRStatement when it is destroyed.
*
*Sets a declaration list to the current \@page rule statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_page_rule_set_declarations (CRStatement * a_this,
CRDeclaration * a_decl_list)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_PAGE_RULE_STMT
&& a_this->kind.page_rule, CR_BAD_PARAM_ERROR);
if (a_this->kind.page_rule->decl_list) {
cr_declaration_unref (a_this->kind.page_rule->decl_list);
}
a_this->kind.page_rule->decl_list = a_decl_list;
if (a_decl_list) {
cr_declaration_ref (a_decl_list);
}
return CR_OK;
}
/**
* cr_statement_at_page_rule_get_declarations:
*
*@a_this: the current \@page rule statement.
*@a_decl_list: out parameter. The returned declaration list.
*
*Gets the declaration list associated to the current \@page rule
*statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_page_rule_get_declarations (CRStatement * a_this,
CRDeclaration ** a_decl_list)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_PAGE_RULE_STMT
&& a_this->kind.page_rule, CR_BAD_PARAM_ERROR);
*a_decl_list = a_this->kind.page_rule->decl_list;
return CR_OK;
}
/**
* cr_statement_at_charset_rule_set_charset:
*
*
*@a_this: the current \@charset rule statement.
*@a_charset: the charset to set.
*
*Sets the charset of the current \@charset rule statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_charset_rule_set_charset (CRStatement * a_this,
CRString * a_charset)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_CHARSET_RULE_STMT
&& a_this->kind.charset_rule,
CR_BAD_PARAM_ERROR);
if (a_this->kind.charset_rule->charset) {
cr_string_destroy (a_this->kind.charset_rule->charset);
}
a_this->kind.charset_rule->charset = a_charset;
return CR_OK;
}
/**
* cr_statement_at_charset_rule_get_charset:
*@a_this: the current \@charset rule statement.
*@a_charset: out parameter. The returned charset string if
*and only if the function returned CR_OK.
*
*Gets the charset string associated to the current
*\@charset rule statement.
*
* Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_charset_rule_get_charset (CRStatement const * a_this,
CRString ** a_charset)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_CHARSET_RULE_STMT
&& a_this->kind.charset_rule,
CR_BAD_PARAM_ERROR);
*a_charset = a_this->kind.charset_rule->charset;
return CR_OK;
}
/**
* cr_statement_at_font_face_rule_set_decls:
*
*@a_this: the current \@font-face rule statement.
*@a_decls: the declarations list to set.
*
*Sets a declaration list to the current \@font-face rule statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_font_face_rule_set_decls (CRStatement * a_this,
CRDeclaration * a_decls)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_FONT_FACE_RULE_STMT
&& a_this->kind.font_face_rule,
CR_BAD_PARAM_ERROR);
if (a_this->kind.font_face_rule->decl_list) {
cr_declaration_unref (a_this->kind.font_face_rule->decl_list);
}
a_this->kind.font_face_rule->decl_list = a_decls;
cr_declaration_ref (a_decls);
return CR_OK;
}
/**
* cr_statement_at_font_face_rule_get_decls:
*
*@a_this: the current \@font-face rule statement.
*@a_decls: out parameter. The returned declaration list if
*and only if this function returns CR_OK.
*
*Gets the declaration list associated to the current instance
*of \@font-face rule statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_font_face_rule_get_decls (CRStatement * a_this,
CRDeclaration ** a_decls)
{
g_return_val_if_fail (a_this
&& a_this->type == AT_FONT_FACE_RULE_STMT
&& a_this->kind.font_face_rule,
CR_BAD_PARAM_ERROR);
*a_decls = a_this->kind.font_face_rule->decl_list;
return CR_OK;
}
/**
* cr_statement_at_font_face_rule_add_decl:
*
*@a_this: the current \@font-face rule statement.
*@a_prop: the property of the declaration.
*@a_value: the value of the declaration.
*
*Adds a declaration to the current \@font-face rule
*statement.
*
*Returns CR_OK upon successful completion, an error code otherwise.
*/
enum CRStatus
cr_statement_at_font_face_rule_add_decl (CRStatement * a_this,
CRString * a_prop, CRTerm * a_value)
{
CRDeclaration *decls = NULL;
g_return_val_if_fail (a_this
&& a_this->type == AT_FONT_FACE_RULE_STMT
&& a_this->kind.font_face_rule,
CR_BAD_PARAM_ERROR);
decls = cr_declaration_append2
(a_this->kind.font_face_rule->decl_list,
a_prop, a_value);
g_return_val_if_fail (decls, CR_ERROR);
if (a_this->kind.font_face_rule->decl_list == NULL)
cr_declaration_ref (decls);
a_this->kind.font_face_rule->decl_list = decls;
return CR_OK;
}
/**
* cr_statement_to_string:
*
*@a_this: the current statement to serialize
*@a_indent: the number of white space of indentation.
*
*Serializes a css statement into a string
*
*Returns the serialized statement. Must be freed by the caller
*using g_free().
*/
gchar *
cr_statement_to_string (CRStatement const * a_this, gulong a_indent)
{
gchar *str = NULL ;
if (!a_this)
return NULL;
switch (a_this->type) {
case RULESET_STMT:
str = cr_statement_ruleset_to_string
(a_this, a_indent);
break;
case AT_FONT_FACE_RULE_STMT:
str = cr_statement_font_face_rule_to_string
(a_this, a_indent) ;
break;
case AT_CHARSET_RULE_STMT:
str = cr_statement_charset_to_string
(a_this, a_indent);
break;
case AT_PAGE_RULE_STMT:
str = cr_statement_at_page_rule_to_string
(a_this, a_indent);
break;
case AT_MEDIA_RULE_STMT:
str = cr_statement_media_rule_to_string
(a_this, a_indent);
break;
case AT_IMPORT_RULE_STMT:
str = cr_statement_import_rule_to_string
(a_this, a_indent);
break;
default:
cr_utils_trace_info ("Statement unrecognized");
break;
}
return str ;
}
gchar*
cr_statement_list_to_string (CRStatement const *a_this, gulong a_indent)
{
CRStatement const *cur_stmt = NULL ;
GString *stringue = NULL ;
gchar *str = NULL ;
g_return_val_if_fail (a_this, NULL) ;
stringue = g_string_new (NULL) ;
if (!stringue) {
cr_utils_trace_info ("Out of memory") ;
return NULL ;
}
for (cur_stmt = a_this ; cur_stmt;
cur_stmt = cur_stmt->next) {
str = cr_statement_to_string (cur_stmt, a_indent) ;
if (str) {
if (!cur_stmt->prev) {
g_string_append (stringue, str) ;
} else {
g_string_append_printf
(stringue, "\n%s", str) ;
}
g_free (str) ;
str = NULL ;
}
}
str = stringue->str ;
g_string_free (stringue, FALSE) ;
return str ;
}
/**
* cr_statement_dump:
*
*@a_this: the current css2 statement.
*@a_fp: the destination file pointer.
*@a_indent: the number of white space indentation characters.
*
*Dumps the css2 statement to a file.
*/
void
cr_statement_dump (CRStatement const * a_this, FILE * a_fp, gulong a_indent)
{
gchar *str = NULL ;
if (!a_this)
return;
str = cr_statement_to_string (a_this, a_indent) ;
if (str) {
fprintf (a_fp, "%s",str) ;
g_free (str) ;
str = NULL ;
}
}
/**
* cr_statement_dump_ruleset:
*
*@a_this: the current instance of #CRStatement.
*@a_fp: the destination file pointer.
*@a_indent: the number of indentation white spaces to add.
*
*Dumps a ruleset statement to a file.
*/
void
cr_statement_dump_ruleset (CRStatement const * a_this, FILE * a_fp, glong a_indent)
{
gchar *str = NULL;
g_return_if_fail (a_fp && a_this);
str = cr_statement_ruleset_to_string (a_this, a_indent);
if (str) {
fprintf (a_fp, "%s", str);
g_free (str);
str = NULL;
}
}
/**
* cr_statement_dump_font_face_rule:
*
*@a_this: the current instance of font face rule statement.
*@a_fp: the destination file pointer.
*@a_indent: the number of white space indentation.
*
*Dumps a font face rule statement to a file.
*/
void
cr_statement_dump_font_face_rule (CRStatement const * a_this, FILE * a_fp,
glong a_indent)
{
gchar *str = NULL ;
g_return_if_fail (a_this
&& a_this->type == AT_FONT_FACE_RULE_STMT);
str = cr_statement_font_face_rule_to_string (a_this,
a_indent) ;
if (str) {
fprintf (a_fp, "%s", str) ;
g_free (str) ;
str = NULL ;
}
}
/**
* cr_statement_dump_charset:
*
*@a_this: the current instance of the \@charset rule statement.
*@a_fp: the destination file pointer.
*@a_indent: the number of indentation white spaces.
*
*Dumps an \@charset rule statement to a file.
*/
void
cr_statement_dump_charset (CRStatement const * a_this, FILE * a_fp, gulong a_indent)
{
gchar *str = NULL;
g_return_if_fail (a_this && a_this->type == AT_CHARSET_RULE_STMT);
str = cr_statement_charset_to_string (a_this,
a_indent) ;
if (str) {
fprintf (a_fp, "%s", str) ;
g_free (str) ;
str = NULL ;
}
}
/**
* cr_statement_dump_page:
*
*@a_this: the statement to dump on stdout.
*@a_fp: the destination file pointer.
*@a_indent: the number of indentation white spaces.
*
*Dumps an \@page rule statement on stdout.
*/
void
cr_statement_dump_page (CRStatement const * a_this, FILE * a_fp, gulong a_indent)
{
gchar *str = NULL;
g_return_if_fail (a_this
&& a_this->type == AT_PAGE_RULE_STMT
&& a_this->kind.page_rule);
str = cr_statement_at_page_rule_to_string (a_this, a_indent) ;
if (str) {
fprintf (a_fp, "%s", str);
g_free (str) ;
str = NULL ;
}
}
/**
* cr_statement_dump_media_rule:
*
*@a_this: the statement to dump.
*@a_fp: the destination file pointer
*@a_indent: the number of white spaces indentation.
*
*Dumps an \@media rule statement to a file.
*/
void
cr_statement_dump_media_rule (CRStatement const * a_this,
FILE * a_fp,
gulong a_indent)
{
gchar *str = NULL ;
g_return_if_fail (a_this->type == AT_MEDIA_RULE_STMT);
str = cr_statement_media_rule_to_string (a_this, a_indent) ;
if (str) {
fprintf (a_fp, "%s", str) ;
g_free (str) ;
str = NULL ;
}
}
/**
* cr_statement_dump_import_rule:
*
*@a_fp: the destination file pointer.
*@a_indent: the number of white space indentations.
*
*Dumps an \@import rule statement to a file.
*/
void
cr_statement_dump_import_rule (CRStatement const * a_this, FILE * a_fp,
gulong a_indent)
{
gchar *str = NULL ;
g_return_if_fail (a_this
&& a_this->type == AT_IMPORT_RULE_STMT
&& a_fp
&& a_this->kind.import_rule);
str = cr_statement_import_rule_to_string (a_this, a_indent) ;
if (str) {
fprintf (a_fp, "%s", str) ;
g_free (str) ;
str = NULL ;
}
}
/**
* cr_statement_destroy:
*
* @a_this: the current instance of #CRStatement.
*
*Destructor of #CRStatement.
*/
void
cr_statement_destroy (CRStatement * a_this)
{
CRStatement *cur = NULL;
g_return_if_fail (a_this);
/*go get the tail of the list */
for (cur = a_this; cur && cur->next; cur = cur->next) {
cr_statement_clear (cur);
}
if (cur)
cr_statement_clear (cur);
if (cur->prev == NULL) {
g_free (a_this);
return;
}
/*walk backward and free next element */
for (cur = cur->prev; cur && cur->prev; cur = cur->prev) {
if (cur->next) {
g_free (cur->next);
cur->next = NULL;
}
}
if (!cur)
return;
/*free the one remaining list */
if (cur->next) {
g_free (cur->next);
cur->next = NULL;
}
g_free (cur);
cur = NULL;
}
|