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.
27 #include "cr-simple-sel.h"
32 *The constructor of #CRSimpleSel.
34 *Returns the new instance of #CRSimpleSel.
37 cr_simple_sel_new (void)
39 CRSimpleSel *result = NULL;
41 result = g_try_malloc (sizeof (CRSimpleSel));
43 cr_utils_trace_info ("Out of memory");
46 memset (result, 0, sizeof (CRSimpleSel));
52 * cr_simple_sel_append_simple_sel:
54 *Appends a simpe selector to the current list of simple selector.
56 *@a_this: the this pointer of the current instance of #CRSimpleSel.
57 *@a_sel: the simple selector to append.
58 *Returns: the new list upon successfull completion, an error code otherwise.
61 cr_simple_sel_append_simple_sel (CRSimpleSel * a_this, CRSimpleSel * a_sel)
63 CRSimpleSel *cur = NULL;
65 g_return_val_if_fail (a_sel, NULL);
70 for (cur = a_this; cur->next; cur = cur->next) ;
79 * cr_simple_sel_prepend_simple_sel:
81 *@a_this: the this pointer of the current instance of #CRSimpleSel.
82 *@a_sel: the simple selector to prepend.
84 *Prepends a simple selector to the current list of simple selectors.
86 *Returns the new list upon successfull completion, an error code otherwise.
89 cr_simple_sel_prepend_simple_sel (CRSimpleSel * a_this, CRSimpleSel * a_sel)
91 g_return_val_if_fail (a_sel, NULL);
103 cr_simple_sel_to_string (CRSimpleSel * a_this)
105 GString *str_buf = NULL;
106 guchar *result = NULL;
108 CRSimpleSel *cur = NULL;
110 g_return_val_if_fail (a_this, NULL);
112 str_buf = g_string_new (NULL);
113 for (cur = a_this; cur; cur = cur->next) {
115 guchar *str = g_strndup (cur->name->stryng->str,
116 cur->name->stryng->len);
119 switch (cur->combinator) {
121 g_string_append (str_buf, " ");
125 g_string_append (str_buf, "+");
129 g_string_append (str_buf, ">");
136 g_string_append (str_buf, str);
143 guchar *tmp_str = NULL;
145 tmp_str = cr_additional_sel_to_string (cur->add_sel);
147 g_string_append (str_buf, tmp_str);
155 result = str_buf->str;
156 g_string_free (str_buf, FALSE);
165 cr_simple_sel_one_to_string (CRSimpleSel * a_this)
167 GString *str_buf = NULL;
168 guchar *result = NULL;
170 g_return_val_if_fail (a_this, NULL);
172 str_buf = g_string_new (NULL);
174 guchar *str = g_strndup (a_this->name->stryng->str,
175 a_this->name->stryng->len);
178 g_string_append_printf (str_buf, "%s", str);
184 if (a_this->add_sel) {
185 guchar *tmp_str = NULL;
187 tmp_str = cr_additional_sel_to_string (a_this->add_sel);
189 g_string_append_printf
190 (str_buf, "%s", tmp_str);
197 result = str_buf->str;
198 g_string_free (str_buf, FALSE);
206 * cr_simple_sel_dump:
207 *@a_this: the current instance of #CRSimpleSel.
208 *@a_fp: the destination file pointer.
210 *Dumps the selector to a file.
211 *TODO: add the support of unicode in the dump.
213 *Returns CR_OK upon successfull completion, an error code
217 cr_simple_sel_dump (CRSimpleSel * a_this, FILE * a_fp)
219 guchar *tmp_str = NULL;
221 g_return_val_if_fail (a_fp, CR_BAD_PARAM_ERROR);
224 tmp_str = cr_simple_sel_to_string (a_this);
226 fprintf (a_fp, "%s", tmp_str);
236 * cr_simple_sel_compute_specificity:
238 *@a_this: the current instance of #CRSimpleSel
240 *Computes the selector (combinator separated list of simple selectors)
241 *as defined in the css2 spec in chapter 6.4.3
243 *Returns CR_OK upon successfull completion, an error code otherwise.
246 cr_simple_sel_compute_specificity (CRSimpleSel * a_this)
248 CRAdditionalSel *cur_add_sel = NULL;
249 CRSimpleSel *cur_sel = NULL;
254 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
256 for (cur_sel = a_this; cur_sel; cur_sel = cur_sel->next) {
257 if (cur_sel->type_mask | TYPE_SELECTOR) {
258 c++; /*hmmh, is this a new language ? */
259 } else if (!cur_sel->name
260 || !cur_sel->name->stryng
261 || !cur_sel->name->stryng->str) {
262 if (cur_sel->add_sel->type ==
263 PSEUDO_CLASS_ADD_SELECTOR) {
265 *this is a pseudo element, and
266 *the spec says, "ignore pseudo elements".
272 for (cur_add_sel = cur_sel->add_sel;
273 cur_add_sel; cur_add_sel = cur_add_sel->next) {
274 switch (cur_add_sel->type) {
275 case ID_ADD_SELECTOR:
279 case NO_ADD_SELECTOR:
289 /*we suppose a, b and c have 1 to 3 digits */
290 a_this->specificity = a * 1000000 + b * 1000 + c;
296 * cr_simple_sel_destroy:
298 *@a_this: the this pointer of the current instance of #CRSimpleSel.
300 *The destructor of the current instance of
304 cr_simple_sel_destroy (CRSimpleSel * a_this)
306 g_return_if_fail (a_this);
309 cr_string_destroy (a_this->name);
313 if (a_this->add_sel) {
314 cr_additional_sel_destroy (a_this->add_sel);
315 a_this->add_sel = NULL;
319 cr_simple_sel_destroy (a_this->next);