Libcroco
cr-additional-sel.c
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
2 
3 /*
4  * This file is part of The Croco Library
5  *
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.
9  *
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.
14  *
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
18  * USA
19  *
20  * Author: Dodji Seketeli
21  * See COPYRIGHTS file for copyright information.
22  *
23  */
24 
25 #include "cr-additional-sel.h"
26 #include "string.h"
27 
28 /**
29  * CRAdditionalSel:
30  *
31  * #CRAdditionalSel abstracts an additionnal selector.
32  * An additional selector is the selector part
33  * that comes after the combination of type selectors.
34  * It can be either "a class selector (the .class part),
35  * a pseudo class selector, an attribute selector
36  * or an id selector.
37  */
38 
39 /**
40  * cr_additional_sel_new:
41  *
42  * Default constructor of #CRAdditionalSel.
43  * Returns the newly build instance of #CRAdditionalSel.
44  */
47 {
48  CRAdditionalSel *result = NULL;
49 
50  result = g_try_malloc (sizeof (CRAdditionalSel));
51 
52  if (result == NULL) {
53  cr_utils_trace_debug ("Out of memory");
54  return NULL;
55  }
56 
57  memset (result, 0, sizeof (CRAdditionalSel));
58 
59  return result;
60 }
61 
62 /**
63  * cr_additional_sel_new_with_type:
64  * @a_sel_type: the type of the newly built instance
65  * of #CRAdditionalSel.
66  *
67  * Constructor of #CRAdditionalSel.
68  * Returns the newly built instance of #CRAdditionalSel.
69  */
72 {
73  CRAdditionalSel *result = NULL;
74 
75  result = cr_additional_sel_new ();
76 
77  g_return_val_if_fail (result, NULL);
78 
79  result->type = a_sel_type;
80 
81  return result;
82 }
83 
84 /**
85  * cr_additional_sel_set_class_name:
86  * @a_this: the "this pointer" of the current instance
87  * of #CRAdditionalSel .
88  * @a_class_name: the new class name to set.
89  *
90  * Sets a new class name to a
91  * CLASS additional selector.
92  */
93 void
95  CRString * a_class_name)
96 {
97  g_return_if_fail (a_this && a_this->type == CLASS_ADD_SELECTOR);
98 
99  if (a_this->content.class_name) {
101  }
102 
103  a_this->content.class_name = a_class_name;
104 }
105 
106 /**
107  * cr_additional_sel_set_id_name:
108  * @a_this: the "this pointer" of the current instance
109  * of #CRAdditionalSel .
110  * @a_id: the new id to set.
111  *
112  * Sets a new id name to an
113  * ID additional selector.
114  */
115 void
117 {
118  g_return_if_fail (a_this && a_this->type == ID_ADD_SELECTOR);
119 
120  if (a_this->content.id_name) {
122  }
123 
124  a_this->content.id_name = a_id;
125 }
126 
127 /**
128  * cr_additional_sel_set_pseudo:
129  * @a_this: the "this pointer" of the current instance
130  * of #CRAdditionalSel .
131  * @a_pseudo: the new pseudo to set.
132  *
133  * Sets a new pseudo to a
134  * PSEUDO additional selector.
135  */
136 void
138 {
139  g_return_if_fail (a_this
140  && a_this->type == PSEUDO_CLASS_ADD_SELECTOR);
141 
142  if (a_this->content.pseudo) {
143  cr_pseudo_destroy (a_this->content.pseudo);
144  }
145 
146  a_this->content.pseudo = a_pseudo;
147 }
148 
149 /**
150  * cr_additional_sel_set_attr_sel:
151  * @a_this: the "this pointer" of the current instance
152  * of #CRAdditionalSel .
153  * @a_sel: the new instance of #CRAttrSel to set.
154  *
155  * Sets a new instance of #CRAttrSel to
156  * a ATTRIBUTE additional selector.
157  */
158 void
160 {
161  g_return_if_fail (a_this && a_this->type == ATTRIBUTE_ADD_SELECTOR);
162 
163  if (a_this->content.attr_sel) {
165  }
166 
167  a_this->content.attr_sel = a_sel;
168 }
169 
170 /**
171  * cr_additional_sel_append:
172  * @a_this: the "this pointer" of the current instance
173  * of #CRAdditionalSel .
174  * @a_sel: the new instance to #CRAdditional to append.
175  *
176  * Appends a new instance of #CRAdditional to the
177  * current list of #CRAdditional.
178  *
179  * Returns the new list of CRAdditionalSel or NULL if an error arises.
180  */
183 {
184  CRAdditionalSel *cur_sel = NULL;
185 
186  g_return_val_if_fail (a_sel, NULL);
187 
188  if (a_this == NULL) {
189  return a_sel;
190  }
191 
192  if (a_sel == NULL)
193  return NULL;
194 
195  for (cur_sel = a_this;
196  cur_sel && cur_sel->next; cur_sel = cur_sel->next) ;
197 
198  g_return_val_if_fail (cur_sel != NULL, NULL);
199 
200  cur_sel->next = a_sel;
201  a_sel->prev = cur_sel;
202 
203  return a_this;
204 }
205 
206 /**
207  * cr_additional_sel_prepend:
208  * @a_this: the "this pointer" of the current instance
209  * of #CRAdditionalSel .
210  * @a_sel: the new instance to #CRAdditional to preappend.
211  *
212  * Preppends a new instance of #CRAdditional to the
213  * current list of #CRAdditional.
214  *
215  * Returns the new list of CRAdditionalSel or NULL if an error arises.
216  */
219 {
220  g_return_val_if_fail (a_sel, NULL);
221 
222  if (a_this == NULL) {
223  return a_sel;
224  }
225 
226  a_sel->next = a_this;
227  a_this->prev = a_sel;
228 
229  return a_sel;
230 }
231 
232 guchar *
234 {
235  guchar *result = NULL;
236  GString *str_buf = NULL;
237  CRAdditionalSel const *cur = NULL;
238 
239  g_return_val_if_fail (a_this, NULL);
240 
241  str_buf = g_string_new (NULL);
242 
243  for (cur = a_this; cur; cur = cur->next) {
244  switch (cur->type) {
245  case CLASS_ADD_SELECTOR:
246  {
247  guchar *name = NULL;
248 
249  if (cur->content.class_name) {
250  name = (guchar *) g_strndup
251  (cur->content.class_name->stryng->str,
252  cur->content.class_name->stryng->len);
253 
254  if (name) {
255  g_string_append_printf
256  (str_buf, ".%s",
257  name);
258  g_free (name);
259  name = NULL;
260  }
261  }
262  }
263  break;
264 
265  case ID_ADD_SELECTOR:
266  {
267  guchar *name = NULL;
268 
269  if (cur->content.id_name) {
270  name = (guchar *) g_strndup
271  (cur->content.id_name->stryng->str,
272  cur->content.id_name->stryng->len);
273 
274  if (name) {
275  g_string_append_printf
276  (str_buf, "#%s",
277  name);
278  g_free (name);
279  name = NULL;
280  }
281  }
282  }
283 
284  break;
285 
287  {
288  if (cur->content.pseudo) {
289  guchar *tmp_str = NULL;
290 
291  tmp_str = cr_pseudo_to_string
292  (cur->content.pseudo);
293  if (tmp_str) {
294  g_string_append_printf
295  (str_buf, ":%s",
296  tmp_str);
297  g_free (tmp_str);
298  tmp_str = NULL;
299  }
300  }
301  }
302  break;
303 
305  if (cur->content.attr_sel) {
306  guchar *tmp_str = NULL;
307 
308  g_string_append_c (str_buf, '[');
309  tmp_str = cr_attr_sel_to_string
310  (cur->content.attr_sel);
311  if (tmp_str) {
312  g_string_append_printf
313  (str_buf, "%s]", tmp_str);
314  g_free (tmp_str);
315  tmp_str = NULL;
316  }
317  }
318  break;
319 
320  default:
321  break;
322  }
323  }
324 
325  if (str_buf) {
326  result = (guchar *) str_buf->str;
327  g_string_free (str_buf, FALSE);
328  str_buf = NULL;
329  }
330 
331  return result;
332 }
333 
334 guchar *
336 {
337  guchar *result = NULL;
338  GString *str_buf = NULL;
339 
340  g_return_val_if_fail (a_this, NULL) ;
341 
342  str_buf = g_string_new (NULL) ;
343 
344  switch (a_this->type) {
345  case CLASS_ADD_SELECTOR:
346  {
347  guchar *name = NULL;
348 
349  if (a_this->content.class_name) {
350  name = (guchar *) g_strndup
351  (a_this->content.class_name->stryng->str,
352  a_this->content.class_name->stryng->len);
353 
354  if (name) {
355  g_string_append_printf
356  (str_buf, ".%s",
357  name);
358  g_free (name);
359  name = NULL;
360  }
361  }
362  }
363  break;
364 
365  case ID_ADD_SELECTOR:
366  {
367  guchar *name = NULL;
368 
369  if (a_this->content.id_name) {
370  name = (guchar *) g_strndup
371  (a_this->content.id_name->stryng->str,
372  a_this->content.id_name->stryng->len);
373 
374  if (name) {
375  g_string_append_printf
376  (str_buf, "#%s",
377  name);
378  g_free (name);
379  name = NULL;
380  }
381  }
382  }
383 
384  break;
385 
387  {
388  if (a_this->content.pseudo) {
389  guchar *tmp_str = NULL;
390 
391  tmp_str = cr_pseudo_to_string
392  (a_this->content.pseudo);
393  if (tmp_str) {
394  g_string_append_printf
395  (str_buf, ":%s",
396  tmp_str);
397  g_free (tmp_str);
398  tmp_str = NULL;
399  }
400  }
401  }
402  break;
403 
405  if (a_this->content.attr_sel) {
406  guchar *tmp_str = NULL;
407 
408  g_string_append_printf (str_buf, "[");
409  tmp_str = cr_attr_sel_to_string
410  (a_this->content.attr_sel);
411  if (tmp_str) {
412  g_string_append_printf
413  (str_buf, "%s]", tmp_str);
414  g_free (tmp_str);
415  tmp_str = NULL;
416  }
417  }
418  break;
419 
420  default:
421  break;
422  }
423 
424  if (str_buf) {
425  result = (guchar *) str_buf->str;
426  g_string_free (str_buf, FALSE);
427  str_buf = NULL;
428  }
429 
430  return result;
431 }
432 
433 /**
434  * cr_additional_sel_dump:
435  * @a_this: the "this pointer" of the current instance of
436  * #CRAdditionalSel.
437  * @a_fp: the destination file.
438  *
439  * Dumps the current instance of #CRAdditionalSel to a file
440  */
441 void
442 cr_additional_sel_dump (CRAdditionalSel const * a_this, FILE * a_fp)
443 {
444  guchar *tmp_str = NULL;
445 
446  g_return_if_fail (a_fp);
447 
448  if (a_this) {
449  tmp_str = cr_additional_sel_to_string (a_this);
450  if (tmp_str) {
451  fprintf (a_fp, "%s", tmp_str);
452  g_free (tmp_str);
453  tmp_str = NULL;
454  }
455  }
456 }
457 
458 /**
459  * cr_additional_sel_destroy:
460  * @a_this: the "this pointer" of the current instance
461  * of #CRAdditionalSel .
462  *
463  * Destroys an instance of #CRAdditional.
464  */
465 void
467 {
468  g_return_if_fail (a_this);
469 
470  switch (a_this->type) {
471  case CLASS_ADD_SELECTOR:
473  a_this->content.class_name = NULL;
474  break;
475 
477  cr_pseudo_destroy (a_this->content.pseudo);
478  a_this->content.pseudo = NULL;
479  break;
480 
481  case ID_ADD_SELECTOR:
483  a_this->content.id_name = NULL;
484  break;
485 
488  a_this->content.attr_sel = NULL;
489  break;
490 
491  default:
492  break;
493  }
494 
495  if (a_this->next) {
497  }
498 
499  g_free (a_this);
500 }
_CRAdditionalSel
Definition: cr-additional-sel.h:56
AddSelectorType
AddSelectorType
Definition: cr-additional-sel.h:37
CRAdditionalSelectorContent::class_name
CRString * class_name
Definition: cr-additional-sel.h:48
PSEUDO_CLASS_ADD_SELECTOR
@ PSEUDO_CLASS_ADD_SELECTOR
Definition: cr-additional-sel.h:41
cr_additional_sel_new_with_type
CRAdditionalSel * cr_additional_sel_new_with_type(enum AddSelectorType a_sel_type)
cr_additional_sel_new_with_type: @a_sel_type: the type of the newly built instance of CRAdditionalSel...
Definition: cr-additional-sel.c:71
CRAdditionalSelectorContent::attr_sel
CRAttrSel * attr_sel
Definition: cr-additional-sel.h:51
_CRPseudo
The CRPseudo Class.
Definition: cr-pseudo.h:46
cr_pseudo_to_string
guchar * cr_pseudo_to_string(CRPseudo const *a_this)
cr_pseudo_to_string: @a_this: the current instance of #CRPseud.
Definition: cr-pseudo.c:55
cr_utils_trace_debug
#define cr_utils_trace_debug(a_msg)
Trace a debug message.
Definition: cr-utils.h:137
cr_additional_sel_new
CRAdditionalSel * cr_additional_sel_new(void)
CRAdditionalSel:
Definition: cr-additional-sel.c:46
CRString
typedefG_BEGIN_DECLS struct _CRString CRString
Definition: cr-string.h:37
_CRAdditionalSel::prev
CRAdditionalSel * prev
Definition: cr-additional-sel.h:62
_CRAdditionalSel::content
union CRAdditionalSelectorContent content
Definition: cr-additional-sel.h:59
cr_additional_sel_to_string
guchar * cr_additional_sel_to_string(CRAdditionalSel const *a_this)
Definition: cr-additional-sel.c:233
cr_additional_sel_set_pseudo
void cr_additional_sel_set_pseudo(CRAdditionalSel *a_this, CRPseudo *a_pseudo)
cr_additional_sel_set_pseudo: @a_this: the "this pointer" of the current instance of CRAdditionalSel ...
Definition: cr-additional-sel.c:137
cr_additional_sel_one_to_string
guchar * cr_additional_sel_one_to_string(CRAdditionalSel const *a_this)
Definition: cr-additional-sel.c:335
CLASS_ADD_SELECTOR
@ CLASS_ADD_SELECTOR
Definition: cr-additional-sel.h:40
cr_additional_sel_set_id_name
void cr_additional_sel_set_id_name(CRAdditionalSel *a_this, CRString *a_id)
cr_additional_sel_set_id_name: @a_this: the "this pointer" of the current instance of CRAdditionalSel...
Definition: cr-additional-sel.c:116
cr_string_destroy
void cr_string_destroy(CRString *a_this)
Definition: cr-string.c:159
cr_attr_sel_to_string
guchar * cr_attr_sel_to_string(CRAttrSel const *a_this)
cr_attr_sel_to_string: @a_this: the current instance of CRAttrSel.
Definition: cr-attr-sel.c:108
cr_additional_sel_set_attr_sel
void cr_additional_sel_set_attr_sel(CRAdditionalSel *a_this, CRAttrSel *a_sel)
cr_additional_sel_set_attr_sel: @a_this: the "this pointer" of the current instance of CRAdditionalSe...
Definition: cr-additional-sel.c:159
cr_additional_sel_prepend
CRAdditionalSel * cr_additional_sel_prepend(CRAdditionalSel *a_this, CRAdditionalSel *a_sel)
cr_additional_sel_prepend: @a_this: the "this pointer" of the current instance of CRAdditionalSel .
Definition: cr-additional-sel.c:218
cr_additional_sel_destroy
void cr_additional_sel_destroy(CRAdditionalSel *a_this)
cr_additional_sel_destroy: @a_this: the "this pointer" of the current instance of CRAdditionalSel .
Definition: cr-additional-sel.c:466
cr-additional-sel.h
CRAdditionalSelectorContent::id_name
CRString * id_name
Definition: cr-additional-sel.h:49
cr_additional_sel_dump
void cr_additional_sel_dump(CRAdditionalSel const *a_this, FILE *a_fp)
cr_additional_sel_dump: @a_this: the "this pointer" of the current instance of CRAdditionalSel.
Definition: cr-additional-sel.c:442
CRAdditionalSelectorContent::pseudo
CRPseudo * pseudo
Definition: cr-additional-sel.h:50
_CRAdditionalSel::next
CRAdditionalSel * next
Definition: cr-additional-sel.h:61
cr_additional_sel_append
CRAdditionalSel * cr_additional_sel_append(CRAdditionalSel *a_this, CRAdditionalSel *a_sel)
cr_additional_sel_append: @a_this: the "this pointer" of the current instance of CRAdditionalSel .
Definition: cr-additional-sel.c:182
cr_additional_sel_set_class_name
void cr_additional_sel_set_class_name(CRAdditionalSel *a_this, CRString *a_class_name)
cr_additional_sel_set_class_name: @a_this: the "this pointer" of the current instance of CRAdditional...
Definition: cr-additional-sel.c:94
_CRAttrSel
Definition: cr-attr-sel.h:48
ATTRIBUTE_ADD_SELECTOR
@ ATTRIBUTE_ADD_SELECTOR
Definition: cr-additional-sel.h:43
cr_attr_sel_destroy
void cr_attr_sel_destroy(CRAttrSel *a_this)
cr_attr_sel_destroy: @a_this: the "this pointer" of the current instance of CRAttrSel.
Definition: cr-attr-sel.c:211
cr_pseudo_destroy
void cr_pseudo_destroy(CRPseudo *a_this)
cr_pseudo_destroy: @a_this: the current instance to destroy.
Definition: cr-pseudo.c:152
_CRAdditionalSel::type
enum AddSelectorType type
Definition: cr-additional-sel.h:58
ID_ADD_SELECTOR
@ ID_ADD_SELECTOR
Definition: cr-additional-sel.h:42