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

/*
 * This file is part of The Croco Library
 *
 * Copyright (C) 2002-2004 Dodji Seketeli
 *
 * 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
 */

#include "string.h"
#include "cr-stylesheet.h"

/**
 *@file
 *The definition of the #CRStyleSheet class
 */

/**
 *Constructor of the #CRStyleSheet class.
 *@param the initial list of css statements.
 *@return the newly built css2 stylesheet, or NULL in case of error.
 */
CRStyleSheet *
cr_stylesheet_new (CRStatement * a_stmts)
{
        CRStyleSheet *result;

        result = (CRStyleSheet *) g_try_malloc (sizeof (CRStyleSheet));
        if (!result) {
                cr_utils_trace_info ("Out of memory");
                return NULL;
        }

        memset (result, 0, sizeof (CRStyleSheet));

        if (a_stmts)
                result->statements = a_stmts;

        return result;
}

/**
 *@param a_this the current instance of #CRStyleSheet
 *@return the serialized stylesheet.
 */
gchar *
cr_stylesheet_to_string (CRStyleSheet const *a_this)
{
	gchar *str = NULL;
	GString *stringue = NULL;
	CRStatement const *cur_stmt = NULL;
        CRStyleSheet *cur = NULL;

        g_return_val_if_fail (a_this, NULL);

        stringue = g_string_new (NULL) ;
        g_return_val_if_fail (stringue, NULL) ;

        if (a_this->import) {
                str = cr_stylesheet_to_string (a_this->import);
                if (str) {
                        g_string_append (stringue, str) ;
                        g_free (str) ;
                        g_string_append (stringue, "\n") ;
                        str = NULL ;
                }
        }

        for (cur_stmt = a_this->statements;
             cur_stmt; cur_stmt = cur_stmt->next) {
		if (cur_stmt->prev) {
			g_string_append (stringue, "\n\n") ;
		}
		str = cr_statement_to_string (cur_stmt, 0) ;
		if (str) {
			g_string_append (stringue, str) ;
			g_free (str) ;
			str = NULL ;
		}
        }

        if (a_this->next) {
                str = cr_stylesheet_to_string (a_this->next);
                if (str) {
                        g_string_append (stringue, "\n") ;
                        g_string_append (stringue, str) ;
                        g_free (str) ;
                        str = NULL ;
                }
        }

	if (stringue) {
		str = stringue->str ;
		g_string_free (stringue, FALSE) ;
		stringue = NULL ;
	}
	return str ;
}

/**
 *Dumps the current css2 stylesheet to a file.
 *@param a_this the current instance of #CRStyleSheet.
 *@param a_fp the destination file
 */
void
cr_stylesheet_dump (CRStyleSheet const * a_this, FILE * a_fp)
{
	gchar *str = NULL ;

        g_return_if_fail (a_this);

	str = cr_stylesheet_to_string (a_this) ;
	if (str) {
		fprintf (a_fp, "%s", str) ;
		g_free (str) ;
		str = NULL ;
	}
}

/**
 *Return the number of rules in the stylesheet.
 *@param a_this the current instance of #CRStyleSheet.
 *@return number of rules in the stylesheet.
 */
gint
cr_stylesheet_nr_rules (CRStyleSheet const * a_this)
{
        g_return_val_if_fail (a_this, -1);

        return cr_statement_nr_rules (a_this->statements);
}

/**
 *Use an index to get a CRStatement from the rules in a given stylesheet.
 *@param a_this the current instance of #CRStatement.
 *@param itemnr the index into the rules.
 *@return CRStatement at position itemnr, if itemnr > number of rules - 1,
 *it will return NULL.
 */
CRStatement *
cr_stylesheet_statement_get_from_list (CRStyleSheet * a_this, int itemnr)
{
        g_return_val_if_fail (a_this, NULL);

        return cr_statement_get_from_list (a_this->statements, itemnr);
}

/**
 *Appends a new stylesheet to the current list of #CRStylesheet, setting
 *the "origin" of the new stylesheet to be the same as the others in list.
 *
 *@param a_this the "this pointer" of the current instance
 *of #CRStylesheet .
 *@param a_new_stylesheet the stylesheet to append.
 *@return the list of stylesheets with the a_new_stylesheet appended to it.
 */
CRStyleSheet *
cr_stylesheet_append_stylesheet (CRStyleSheet * a_this, CRStyleSheet * a_new_stylesheet)
{
        CRStyleSheet *cur = NULL;

        g_return_val_if_fail (a_new_stylesheet, NULL);

        if (a_this == NULL)
                return a_new_stylesheet;

        for (cur = a_this; cur->next; cur = cur->next) ;

        cur->next = a_new_stylesheet;
        a_new_stylesheet->prev = cur;

        /* The "origin" must apriori be the same for all stylesheets
           in a list. We must set it correctly or errors will occur in
           put_css_properties_in_props_list(). The "origin" of the initial
           stylesheet in the list is set in cr_cascade_set_sheet(). */
        a_new_stylesheet->origin = cur->origin;

        return a_this;
}

/**
 * cr_stylesheet_unlink:
 *@a_this: the stylesheet to unlink.
 *
 *Unlinks the stylesheet from the stylesheet list.
 *
 *Returns a pointer to the unlinked stylesheet in
 *case of a successfull completion, NULL otherwise.
 */
CRStyleSheet *
cr_stylesheet_unlink (CRStyleSheet * a_this)
{
        CRStyleSheet *result = a_this;

        g_return_val_if_fail (result, NULL);

        /*
         *some sanity checks first
         */
        if (a_this->prev) {
                g_return_val_if_fail (a_this->prev->next == a_this, NULL);

        }
        if (a_this->next) {
                g_return_val_if_fail (a_this->next->prev == a_this, NULL);
        }

        /*
         *now, the real unlinking job.
         */
        if (a_this->prev) {
                a_this->prev->next = a_this->next;
        }
        if (a_this->next) {
                a_this->next->prev = a_this->prev;
        }

        a_this->next = NULL;
        a_this->prev = NULL;

        return a_this;
}

/**
 *Appends a new import stylesheet to the current list of imports.
 *
 *@param a_this the "this pointer" of the current instance
 *of #CRStylesheet .
 *@param a_new_stylesheet the import stylesheet to append.
 *@return the list of stylesheets with the a_new_import appended to it.
 */
CRStyleSheet *
cr_stylesheet_append_import (CRStyleSheet * a_this, CRStyleSheet * a_new_import)
{
        CRStyleSheet *cur = NULL;

        g_return_val_if_fail (a_new_import, NULL);

        if (a_this->import == NULL) {
                a_this->import = a_new_import;
                return a_this;
        }

        for (cur = a_this->import; cur->next; cur = cur->next) ;

        cur->next = a_new_import;

        return a_this;
}


void
cr_stylesheet_ref (CRStyleSheet * a_this)
{
        g_return_if_fail (a_this);

        a_this->ref_count++;
}

gboolean
cr_stylesheet_unref (CRStyleSheet * a_this)
{
        g_return_val_if_fail (a_this, FALSE);

        if (a_this->ref_count)
                a_this->ref_count--;

        if (!a_this->ref_count) {
                cr_stylesheet_destroy (a_this);
                return TRUE;
        }

        return FALSE;
}

/**
 *Destructor of the #CRStyleSheet class.
 *@param a_this the current instance of the #CRStyleSheet class.
 */
void
cr_stylesheet_destroy (CRStyleSheet * a_this)
{
        g_return_if_fail (a_this);

        if (a_this->statements) {
                cr_statement_destroy (a_this->statements);
                a_this->statements = NULL;
        }

        if (a_this->import) {
                cr_stylesheet_destroy (a_this->import);
        }

        if (a_this->next) {
                cr_stylesheet_destroy (a_this->next);
        }

        g_free (a_this);
}