1
/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
4
* This file is part of The Croco Library
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of version 2.1 of the GNU Lesser General Public
8
* License as published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
* Author: Dodji Seketeli
21
* See COPYRIGHTS file for copyright information.
26
#include "cr-simple-sel.h"
29
*The constructor of #CRSimpleSel.
31
*@return the new instance of #CRSimpleSel.
34
cr_simple_sel_new (void)
36
CRSimpleSel *result = NULL;
38
result = g_try_malloc (sizeof (CRSimpleSel));
40
cr_utils_trace_info ("Out of memory");
43
memset (result, 0, sizeof (CRSimpleSel));
49
*Appends a simpe selector to the current list of simple selector.
51
*@param a_this the this pointer of the current instance of #CRSimpleSel.
52
*@param a_sel the simple selector to append.
53
*@return the new list upon successfull completion, an error code otherwise.
56
cr_simple_sel_append_simple_sel (CRSimpleSel * a_this, CRSimpleSel * a_sel)
58
CRSimpleSel *cur = NULL;
60
g_return_val_if_fail (a_sel, NULL);
65
for (cur = a_this; cur->next; cur = cur->next) ;
74
*Prepends a simple selector to the current list of simple selectors.
75
*@param a_this the this pointer of the current instance of #CRSimpleSel.
76
*@param a_sel the simple selector to prepend.
77
*@return the new list upon successfull completion, an error code otherwise.
80
cr_simple_sel_prepend_simple_sel (CRSimpleSel * a_this, CRSimpleSel * a_sel)
82
g_return_val_if_fail (a_sel, NULL);
94
cr_simple_sel_to_string (CRSimpleSel * a_this)
96
GString *str_buf = NULL;
97
guchar *result = NULL;
99
CRSimpleSel *cur = NULL;
101
g_return_val_if_fail (a_this, NULL);
103
str_buf = g_string_new (NULL);
104
for (cur = a_this; cur; cur = cur->next) {
106
guchar *str = g_strndup (cur->name->stryng->str,
107
cur->name->stryng->len);
110
switch (cur->combinator) {
112
g_string_append (str_buf, " ");
116
g_string_append (str_buf, "+");
120
g_string_append (str_buf, ">");
127
g_string_append (str_buf, str);
134
guchar *tmp_str = NULL;
136
tmp_str = cr_additional_sel_to_string (cur->add_sel);
138
g_string_append (str_buf, tmp_str);
146
result = str_buf->str;
147
g_string_free (str_buf, FALSE);
156
cr_simple_sel_one_to_string (CRSimpleSel * a_this)
158
GString *str_buf = NULL;
159
guchar *result = NULL;
161
g_return_val_if_fail (a_this, NULL);
163
str_buf = g_string_new (NULL);
165
guchar *str = g_strndup (a_this->name->stryng->str,
166
a_this->name->stryng->len);
169
g_string_append_printf (str_buf, "%s", str);
175
if (a_this->add_sel) {
176
guchar *tmp_str = NULL;
178
tmp_str = cr_additional_sel_to_string (a_this->add_sel);
180
g_string_append_printf
181
(str_buf, "%s", tmp_str);
188
result = str_buf->str;
189
g_string_free (str_buf, FALSE);
197
*Dumps the selector to a file.
198
*TODO: add the support of unicode in the dump.
200
*@param a_this the current instance of #CRSimpleSel.
201
*@param a_fp the destination file pointer.
202
*@return CR_OK upon successfull completion, an error code
206
cr_simple_sel_dump (CRSimpleSel * a_this, FILE * a_fp)
208
guchar *tmp_str = NULL;
210
g_return_val_if_fail (a_fp, CR_BAD_PARAM_ERROR);
213
tmp_str = cr_simple_sel_to_string (a_this);
215
fprintf (a_fp, "%s", tmp_str);
225
*Computes the selector (combinator separated list of simple selectors)
226
*as defined in the css2 spec in chapter 6.4.3
227
*@param a_this the current instance of #CRSimpleSel
228
*@return CR_OK upon successfull completion, an error code otherwise.
231
cr_simple_sel_compute_specificity (CRSimpleSel * a_this)
233
CRAdditionalSel *cur_add_sel = NULL;
234
CRSimpleSel *cur_sel = NULL;
239
g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
241
for (cur_sel = a_this; cur_sel; cur_sel = cur_sel->next) {
242
if (cur_sel->type_mask | TYPE_SELECTOR) {
243
c++; /*hmmh, is this a new language ? */
244
} else if (!cur_sel->name
245
|| !cur_sel->name->stryng
246
|| !cur_sel->name->stryng->str) {
247
if (cur_sel->add_sel->type ==
248
PSEUDO_CLASS_ADD_SELECTOR) {
250
*this is a pseudo element, and
251
*the spec says, "ignore pseudo elements".
257
for (cur_add_sel = cur_sel->add_sel;
258
cur_add_sel; cur_add_sel = cur_add_sel->next) {
259
switch (cur_add_sel->type) {
260
case ID_ADD_SELECTOR:
264
case NO_ADD_SELECTOR:
274
/*we suppose a, b and c have 1 to 3 digits */
275
a_this->specificity = a * 1000000 + b * 1000 + c;
281
*The destructor of the current instance of
283
*@param a_this the this pointer of the current instance of #CRSimpleSel.
287
cr_simple_sel_destroy (CRSimpleSel * a_this)
289
g_return_if_fail (a_this);
292
cr_string_destroy (a_this->name);
296
if (a_this->add_sel) {
297
cr_additional_sel_destroy (a_this->add_sel);
298
a_this->add_sel = NULL;
302
cr_simple_sel_destroy (a_this->next);