summaryrefslogtreecommitdiffstats
path: root/src/st/croco/cr-additional-sel.c
blob: 9bd8d6a7d0f18e91776e57e4cbb746ea6adeb47e (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
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
/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */

/*
 * This file is part of The Croco Library
 *
 * 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
 * See COPYRIGHTS file for copyright information.
 *
 */

#include "cr-additional-sel.h"
#include "string.h"

/**
 * CRAdditionalSel:
 *
 * #CRAdditionalSel abstracts an additional selector.
 * An additional selector is the selector part
 * that comes after the combination of type selectors.
 * It can be either "a class selector (the .class part),
 * a pseudo class selector, an attribute selector 
 * or an id selector.
 */

/**
 * cr_additional_sel_new:
 *
 * Default constructor of #CRAdditionalSel.
 * Returns the newly build instance of #CRAdditionalSel.
 */
CRAdditionalSel *
cr_additional_sel_new (void)
{
        CRAdditionalSel *result = NULL;

        result = g_try_malloc (sizeof (CRAdditionalSel));

        if (result == NULL) {
                cr_utils_trace_debug ("Out of memory");
                return NULL;
        }

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

        return result;
}

/**
 * cr_additional_sel_new_with_type:
 * @a_sel_type: the type of the newly built instance 
 * of #CRAdditionalSel.
 *
 * Constructor of #CRAdditionalSel.
 * Returns the newly built instance of #CRAdditionalSel.
 */
CRAdditionalSel *
cr_additional_sel_new_with_type (enum AddSelectorType a_sel_type)
{
        CRAdditionalSel *result = NULL;

        result = cr_additional_sel_new ();

        g_return_val_if_fail (result, NULL);

        result->type = a_sel_type;

        return result;
}

/**
 * cr_additional_sel_set_class_name:
 * @a_this: the "this pointer" of the current instance
 * of #CRAdditionalSel .
 * @a_class_name: the new class name to set.
 *
 * Sets a new class name to a
 * CLASS additional selector.
 */
void
cr_additional_sel_set_class_name (CRAdditionalSel * a_this,
                                  CRString * a_class_name)
{
        g_return_if_fail (a_this && a_this->type == CLASS_ADD_SELECTOR);

        if (a_this->content.class_name) {
                cr_string_destroy (a_this->content.class_name);
        }

        a_this->content.class_name = a_class_name;
}

/**
 * cr_additional_sel_set_id_name:
 * @a_this: the "this pointer" of the current instance
 * of #CRAdditionalSel .
 * @a_id: the new id to set.
 *
 * Sets a new id name to an
 * ID additional selector.
 */
void
cr_additional_sel_set_id_name (CRAdditionalSel * a_this, CRString * a_id)
{
        g_return_if_fail (a_this && a_this->type == ID_ADD_SELECTOR);

        if (a_this->content.id_name) {
                cr_string_destroy (a_this->content.id_name);
        }

        a_this->content.id_name = a_id;
}

/**
 * cr_additional_sel_set_pseudo:
 * @a_this: the "this pointer" of the current instance
 * of #CRAdditionalSel .
 * @a_pseudo: the new pseudo to set.
 *
 * Sets a new pseudo to a
 * PSEUDO additional selector.
 */
void
cr_additional_sel_set_pseudo (CRAdditionalSel * a_this, CRPseudo * a_pseudo)
{
        g_return_if_fail (a_this
                          && a_this->type == PSEUDO_CLASS_ADD_SELECTOR);

        if (a_this->content.pseudo) {
                cr_pseudo_destroy (a_this->content.pseudo);
        }

        a_this->content.pseudo = a_pseudo;
}

/**
 * cr_additional_sel_set_attr_sel:
 * @a_this: the "this pointer" of the current instance
 * of #CRAdditionalSel .
 * @a_sel: the new instance of #CRAttrSel to set.
 *
 * Sets a new instance of #CRAttrSel to 
 * a ATTRIBUTE additional selector.
 */
void
cr_additional_sel_set_attr_sel (CRAdditionalSel * a_this, CRAttrSel * a_sel)
{
        g_return_if_fail (a_this && a_this->type == ATTRIBUTE_ADD_SELECTOR);

        if (a_this->content.attr_sel) {
                cr_attr_sel_destroy (a_this->content.attr_sel);
        }

        a_this->content.attr_sel = a_sel;
}

/**
 * cr_additional_sel_append:
 * @a_this: the "this pointer" of the current instance
 * of #CRAdditionalSel .
 * @a_sel: the new instance to #CRAdditional to append.
 *
 * Appends a new instance of #CRAdditional to the
 * current list of #CRAdditional.
 *
 * Returns the new list of CRAdditionalSel or NULL if an error arises.
 */
CRAdditionalSel *
cr_additional_sel_append (CRAdditionalSel * a_this, CRAdditionalSel * a_sel)
{
        CRAdditionalSel *cur_sel = NULL;

        g_return_val_if_fail (a_sel, NULL);

        if (a_this == NULL) {
                return a_sel;
        }

        if (a_sel == NULL)
                return NULL;

        for (cur_sel = a_this;
             cur_sel && cur_sel->next; cur_sel = cur_sel->next) ;

        g_return_val_if_fail (cur_sel != NULL, NULL);

        cur_sel->next = a_sel;
        a_sel->prev = cur_sel;

        return a_this;
}

/**
 * cr_additional_sel_prepend:
 * @a_this: the "this pointer" of the current instance
 * of #CRAdditionalSel .
 * @a_sel: the new instance to #CRAdditional to preappend.
 *
 * Preppends a new instance of #CRAdditional to the
 * current list of #CRAdditional.
 *
 * Returns the new list of CRAdditionalSel or NULL if an error arises.
 */
CRAdditionalSel *
cr_additional_sel_prepend (CRAdditionalSel * a_this, CRAdditionalSel * a_sel)
{
        g_return_val_if_fail (a_sel, NULL);

        if (a_this == NULL) {
                return a_sel;
        }

        a_sel->next = a_this;
        a_this->prev = a_sel;

        return a_sel;
}

guchar *
cr_additional_sel_to_string (CRAdditionalSel const * a_this)
{
        guchar *result = NULL;
        GString *str_buf = NULL;
        CRAdditionalSel const *cur = NULL;

        g_return_val_if_fail (a_this, NULL);

        str_buf = g_string_new (NULL);

        for (cur = a_this; cur; cur = cur->next) {
                switch (cur->type) {
                case CLASS_ADD_SELECTOR:
                        {
                                guchar *name = NULL;

                                if (cur->content.class_name) {
                                        name = (guchar *) g_strndup
                                                (cur->content.class_name->stryng->str,
                                                 cur->content.class_name->stryng->len);

                                        if (name) {
                                                g_string_append_printf
                                                        (str_buf, ".%s",
                                                         name);
                                                g_free (name);
                                                name = NULL;
                                        }
                                }
                        }
                        break;

                case ID_ADD_SELECTOR:
                        {
                                guchar *name = NULL;

                                if (cur->content.id_name) {
                                        name = (guchar *) g_strndup
                                                (cur->content.id_name->stryng->str,
                                                 cur->content.id_name->stryng->len);

                                        if (name) {
                                                g_string_append_printf
                                                        (str_buf, "#%s",
                                                         name);
                                                g_free (name);
                                                name = NULL;
                                        }
                                }
                        }

                        break;

                case PSEUDO_CLASS_ADD_SELECTOR:
                        {
                                if (cur->content.pseudo) {
                                        guchar *tmp_str = NULL;

                                        tmp_str = cr_pseudo_to_string
                                                (cur->content.pseudo);
                                        if (tmp_str) {
                                                g_string_append_printf
                                                        (str_buf, ":%s",
                                                         tmp_str);
                                                g_free (tmp_str);
                                                tmp_str = NULL;
                                        }
                                }
                        }
                        break;

                case ATTRIBUTE_ADD_SELECTOR:
                        if (cur->content.attr_sel) {
                                guchar *tmp_str = NULL;

                                g_string_append_c (str_buf, '[');
                                tmp_str = cr_attr_sel_to_string
                                        (cur->content.attr_sel);
                                if (tmp_str) {
                                        g_string_append_printf
                                                (str_buf, "%s]", tmp_str);
                                        g_free (tmp_str);
                                        tmp_str = NULL;
                                }
                        }
                        break;

                default:
                        break;
                }
        }

        if (str_buf) {
                result = (guchar *) g_string_free (str_buf, FALSE);
                str_buf = NULL;
        }

        return result;
}

guchar * 
cr_additional_sel_one_to_string (CRAdditionalSel const *a_this)
{
        guchar *result = NULL;
        GString *str_buf = NULL;

        g_return_val_if_fail (a_this, NULL) ;

        str_buf = g_string_new (NULL) ;

        switch (a_this->type) {
        case CLASS_ADD_SELECTOR:
        {
                guchar *name = NULL;

                if (a_this->content.class_name) {
                        name = (guchar *) g_strndup
                                (a_this->content.class_name->stryng->str,
                                 a_this->content.class_name->stryng->len);

                        if (name) {
                                g_string_append_printf
                                        (str_buf, ".%s",
                                         name);
                                g_free (name);
                                name = NULL;
                        }
                }
        }
        break;

        case ID_ADD_SELECTOR:
        {
                guchar *name = NULL;

                if (a_this->content.id_name) {
                        name = (guchar *) g_strndup
                                (a_this->content.id_name->stryng->str,
                                 a_this->content.id_name->stryng->len);

                        if (name) {
                                g_string_append_printf
                                        (str_buf, "#%s",
                                         name);
                                g_free (name);
                                name = NULL;
                        }
                }
        }

        break;

        case PSEUDO_CLASS_ADD_SELECTOR:
        {
                if (a_this->content.pseudo) {
                        guchar *tmp_str = NULL;

                        tmp_str = cr_pseudo_to_string
                                (a_this->content.pseudo);
                        if (tmp_str) {
                                g_string_append_printf
                                        (str_buf, ":%s",
                                         tmp_str);
                                g_free (tmp_str);
                                tmp_str = NULL;
                        }
                }
        }
        break;

        case ATTRIBUTE_ADD_SELECTOR:
                if (a_this->content.attr_sel) {
                        guchar *tmp_str = NULL;

                        g_string_append_printf (str_buf, "[");
                        tmp_str = cr_attr_sel_to_string
                                (a_this->content.attr_sel);
                        if (tmp_str) {
                                g_string_append_printf
                                        (str_buf, "%s]", tmp_str);
                                g_free (tmp_str);
                                tmp_str = NULL;
                        }
                }
                break;

        default:
                break;
        }

        if (str_buf) {
                result = (guchar *) g_string_free (str_buf, FALSE);
                str_buf = NULL;
        }

        return result;
}

/**
 * cr_additional_sel_dump:
 * @a_this: the "this pointer" of the current instance of
 * #CRAdditionalSel.
 * @a_fp: the destination file.
 *
 * Dumps the current instance of #CRAdditionalSel to a file
 */
void
cr_additional_sel_dump (CRAdditionalSel const * a_this, FILE * a_fp)
{
        guchar *tmp_str = NULL;

        g_return_if_fail (a_fp);

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

/**
 * cr_additional_sel_destroy:
 * @a_this: the "this pointer" of the current instance
 * of #CRAdditionalSel .
 *
 * Destroys an instance of #CRAdditional.
 */
void
cr_additional_sel_destroy (CRAdditionalSel * a_this)
{
        g_return_if_fail (a_this);

        switch (a_this->type) {
        case CLASS_ADD_SELECTOR:
                cr_string_destroy (a_this->content.class_name);
                a_this->content.class_name = NULL;
                break;

        case PSEUDO_CLASS_ADD_SELECTOR:
                cr_pseudo_destroy (a_this->content.pseudo);
                a_this->content.pseudo = NULL;
                break;

        case ID_ADD_SELECTOR:
                cr_string_destroy (a_this->content.id_name);
                a_this->content.id_name = NULL;
                break;

        case ATTRIBUTE_ADD_SELECTOR:
                cr_attr_sel_destroy (a_this->content.attr_sel);
                a_this->content.attr_sel = NULL;
                break;

        default:
                break;
        }

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

        g_free (a_this);
}