gtkcssselector.c 55.4 KB
Newer Older
Benjamin Otte's avatar
Benjamin Otte committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GTK - The GIMP Toolkit
 * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Benjamin Otte's avatar
Benjamin Otte committed
16 17 18 19 20 21
 */

#include "config.h"

#include "gtkcssselectorprivate.h"

22
#include <stdlib.h>
23 24
#include <string.h>

25
#include "gtkcssprovider.h"
26 27
#include "gtkstylecontextprivate.h"

28
#if defined(_MSC_VER) && _MSC_VER >= 1500
29 30 31
# include <intrin.h>
#endif

Benjamin Otte's avatar
Benjamin Otte committed
32
typedef struct _GtkCssSelectorClass GtkCssSelectorClass;
33 34 35
typedef gboolean (* GtkCssSelectorForeachFunc) (const GtkCssSelector *selector,
                                                const GtkCssMatcher  *matcher,
                                                gpointer              data);
Benjamin Otte's avatar
Benjamin Otte committed
36 37 38 39

struct _GtkCssSelectorClass {
  const char        *name;

40 41
  void              (* print)       (const GtkCssSelector       *selector,
                                     GString                    *string);
42
  /* NULL or an iterator that calls func with each submatcher of @matcher.
43
   * Potentially no submatcher exists.
44 45 46 47 48 49 50 51
   * If any @invocation of @func returns %TRUE, the function will immediately
   * return %TRUE itself. If @func never returns %TRUE (or isn't called at all),
   * %FALSE will be returned.
   */
  gboolean          (* foreach_matcher)  (const GtkCssSelector       *selector,
                                          const GtkCssMatcher        *matcher,
                                          GtkCssSelectorForeachFunc   func,
                                          gpointer                    data);
52 53
  gboolean          (* match_one)   (const GtkCssSelector       *selector,
                                     const GtkCssMatcher        *matcher);
54 55
  GtkCssChange      (* get_change)  (const GtkCssSelector       *selector,
				     GtkCssChange                previous_change);
56 57 58 59
  void              (* add_specificity)  (const GtkCssSelector  *selector,
                                          guint                 *ids,
                                          guint                 *classes,
                                          guint                 *elements);
60
  guint             (* hash_one)    (const GtkCssSelector       *selector);
61 62
  int               (* compare_one) (const GtkCssSelector       *a,
				     const GtkCssSelector       *b);
Benjamin Otte's avatar
Benjamin Otte committed
63

64
  guint         is_simple :1;
Benjamin Otte's avatar
Benjamin Otte committed
65
};
66

67 68 69 70 71 72
typedef enum {
  POSITION_FORWARD,
  POSITION_BACKWARD,
  POSITION_ONLY,
  POSITION_SORTED
} PositionType;
73
#define POSITION_TYPE_BITS 4
74 75 76 77 78 79 80 81
#define POSITION_NUMBER_BITS ((sizeof (gpointer) * 8 - POSITION_TYPE_BITS) / 2)

union _GtkCssSelector
{
  const GtkCssSelectorClass     *class;         /* type of check this selector does */
  struct {
    const GtkCssSelectorClass   *class;
    const char                  *name;          /* interned */
82 83 84 85 86 87
  }                              id;
  struct {
    const GtkCssSelectorClass   *class;
    const char                  *name;          /* interned */
    GtkRegionFlags               flags;
  }                              region;
88 89 90 91 92 93
  struct {
    const GtkCssSelectorClass   *class;
    GQuark                       style_class;
  }                              style_class;
  struct {
    const GtkCssSelectorClass   *class;
94
    const char                  *name;          /* interned */
95 96 97 98 99 100 101 102 103 104 105
  }                              name;
  struct {
    const GtkCssSelectorClass   *class;
    GtkStateFlags                state;
  }                              state;
  struct {
    const GtkCssSelectorClass   *class;
    PositionType                 type :POSITION_TYPE_BITS;
    gssize                       a :POSITION_NUMBER_BITS;
    gssize                       b :POSITION_NUMBER_BITS;
  }                              position;
Benjamin Otte's avatar
Benjamin Otte committed
106 107
};

108
#define GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET G_MAXINT32
109 110 111
struct _GtkCssSelectorTree
{
  GtkCssSelector selector;
112 113 114 115
  gint32 parent_offset;
  gint32 previous_offset;
  gint32 sibling_offset;
  gint32 matches_offset; /* pointers that we return as matches if selector matches */
116 117 118 119 120 121 122 123
};

static gboolean
gtk_css_selector_equal (const GtkCssSelector *a,
			const GtkCssSelector *b)
{
  return
    a->class == b->class &&
124
    a->class->compare_one (a, b) == 0;
125 126 127
}

static guint
128
gtk_css_selector_hash_one (const GtkCssSelector *selector)
129
{
130
  return GPOINTER_TO_UINT (selector->class) ^ selector->class->hash_one (selector);
131 132
}

133 134 135 136 137 138 139 140 141
static gpointer *
gtk_css_selector_tree_get_matches (const GtkCssSelectorTree *tree)
{
  if (tree->matches_offset == GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET)
    return NULL;

  return (gpointer *) ((guint8 *)tree + tree->matches_offset);
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
static void
g_ptr_array_insert_sorted (GPtrArray *array,
                           gpointer   data)
{
  gint i;

  for (i = 0; i < array->len; i++)
    {
      if (data == array->pdata[i])
        return;

      if (data < array->pdata[i])
        break;
    }

  g_ptr_array_insert (array, i, data);
}

160
static void
161 162
gtk_css_selector_tree_found_match (const GtkCssSelectorTree  *tree,
				   GPtrArray                **array)
163 164
{
  int i;
165
  gpointer *matches;
166

167 168
  matches = gtk_css_selector_tree_get_matches (tree);
  if (matches)
169
    {
170 171 172
      if (!*array)
        *array = g_ptr_array_sized_new (16);

173
      for (i = 0; matches[i] != NULL; i++)
174
        g_ptr_array_insert_sorted (*array, matches[i]);
175 176 177
    }
}

Benjamin Otte's avatar
Benjamin Otte committed
178 179
static gboolean
gtk_css_selector_match (const GtkCssSelector *selector,
Benjamin Otte's avatar
Benjamin Otte committed
180
                        const GtkCssMatcher  *matcher)
Benjamin Otte's avatar
Benjamin Otte committed
181
{
182 183
  return selector->class->match_one (selector, matcher);
}
Benjamin Otte's avatar
Benjamin Otte committed
184

185 186 187 188 189 190 191
static gboolean
gtk_css_selector_foreach (const GtkCssSelector      *selector,
                          const GtkCssMatcher       *matcher,
                          GtkCssSelectorForeachFunc  func,
                          gpointer                   data)
{
  return selector->class->foreach_matcher (selector, matcher, func, data);
Benjamin Otte's avatar
Benjamin Otte committed
192 193
}

194 195 196 197 198 199 200 201 202
static int
gtk_css_selector_compare_one (const GtkCssSelector *a, const GtkCssSelector *b)
{
  if (a->class != b->class)
    return strcmp (a->class->name, b->class->name);
  else
    return a->class->compare_one (a, b);
}
  
203 204 205
static const GtkCssSelector *
gtk_css_selector_previous (const GtkCssSelector *selector)
{
206 207 208
  selector = selector + 1;

  return selector->class ? selector : NULL;
209 210
}

211 212 213 214 215 216 217 218 219 220
static const GtkCssSelectorTree *
gtk_css_selector_tree_at_offset (const GtkCssSelectorTree *tree,
				 gint32 offset)
{
  if (offset == GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET)
    return NULL;

  return (GtkCssSelectorTree *) ((guint8 *)tree + offset);
}

221 222 223
static const GtkCssSelectorTree *
gtk_css_selector_tree_get_parent (const GtkCssSelectorTree *tree)
{
224
  return gtk_css_selector_tree_at_offset (tree, tree->parent_offset);
225 226 227 228 229
}

static const GtkCssSelectorTree *
gtk_css_selector_tree_get_previous (const GtkCssSelectorTree *tree)
{
230
  return gtk_css_selector_tree_at_offset (tree, tree->previous_offset);
231 232 233 234 235
}

static const GtkCssSelectorTree *
gtk_css_selector_tree_get_sibling (const GtkCssSelectorTree *tree)
{
236
  return gtk_css_selector_tree_at_offset (tree, tree->sibling_offset);
237 238
}

239 240 241 242 243 244 245 246 247 248 249
/* DEFAULTS */

static void
gtk_css_selector_default_add_specificity (const GtkCssSelector *selector,
                                          guint                *ids,
                                          guint                *classes,
                                          guint                *elements)
{
  /* no specificity changes */
}
 
250
static gboolean
251 252 253 254
gtk_css_selector_default_foreach_matcher (const GtkCssSelector       *selector,
                                          const GtkCssMatcher        *matcher,
                                          GtkCssSelectorForeachFunc   func,
                                          gpointer                    data)
255
{
256
  return func (selector, matcher, data);
257 258 259
}

static gboolean
260 261
gtk_css_selector_default_match_one (const GtkCssSelector *selector,
                                    const GtkCssMatcher  *matcher)
262
{
263
  return TRUE;
264 265
}

266 267 268 269 270 271 272 273 274 275 276 277 278
static guint
gtk_css_selector_default_hash_one (const GtkCssSelector *selector)
{
  return 0;
}

static int
gtk_css_selector_default_compare_one (const GtkCssSelector *a,
                                      const GtkCssSelector *b)
{
  return 0;
}

Benjamin Otte's avatar
Benjamin Otte committed
279 280 281 282 283 284 285 286 287 288
/* DESCENDANT */

static void
gtk_css_selector_descendant_print (const GtkCssSelector *selector,
                                   GString              *string)
{
  g_string_append_c (string, ' ');
}

static gboolean
289 290 291 292
gtk_css_selector_descendant_foreach_matcher (const GtkCssSelector      *selector,
                                             const GtkCssMatcher       *matcher,
                                             GtkCssSelectorForeachFunc  func,
                                             gpointer                   data)
Benjamin Otte's avatar
Benjamin Otte committed
293
{
Benjamin Otte's avatar
Benjamin Otte committed
294 295 296
  GtkCssMatcher ancestor;

  while (_gtk_css_matcher_get_parent (&ancestor, matcher))
Benjamin Otte's avatar
Benjamin Otte committed
297
    {
Benjamin Otte's avatar
Benjamin Otte committed
298 299
      matcher = &ancestor;

300
      if (func (selector, &ancestor, data))
Benjamin Otte's avatar
Benjamin Otte committed
301
        return TRUE;
302 303 304 305 306

      /* any matchers are dangerous here, as we may loop forever, but
	 we can terminate now as all possible matches have already been added */
      if (_gtk_css_matcher_matches_any (matcher))
	break;
Benjamin Otte's avatar
Benjamin Otte committed
307 308 309 310 311
    }

  return FALSE;
}

312
static GtkCssChange
313
gtk_css_selector_descendant_get_change (const GtkCssSelector *selector, GtkCssChange previous_change)
314
{
315
  return _gtk_css_change_for_child (previous_change);
316 317
}

Benjamin Otte's avatar
Benjamin Otte committed
318 319 320
static const GtkCssSelectorClass GTK_CSS_SELECTOR_DESCENDANT = {
  "descendant",
  gtk_css_selector_descendant_print,
321
  gtk_css_selector_descendant_foreach_matcher,
322
  gtk_css_selector_default_match_one,
323
  gtk_css_selector_descendant_get_change,
324
  gtk_css_selector_default_add_specificity,
325 326
  gtk_css_selector_default_hash_one,
  gtk_css_selector_default_compare_one,
327
  FALSE
Benjamin Otte's avatar
Benjamin Otte committed
328 329 330 331 332 333 334 335 336 337 338 339
};

/* CHILD */

static void
gtk_css_selector_child_print (const GtkCssSelector *selector,
                              GString              *string)
{
  g_string_append (string, " > ");
}

static gboolean
340 341 342 343
gtk_css_selector_child_foreach_matcher (const GtkCssSelector      *selector,
                                        const GtkCssMatcher       *matcher,
                                        GtkCssSelectorForeachFunc  func,
                                        gpointer                   data)
Benjamin Otte's avatar
Benjamin Otte committed
344
{
Benjamin Otte's avatar
Benjamin Otte committed
345 346 347
  GtkCssMatcher parent;

  if (!_gtk_css_matcher_get_parent (&parent, matcher))
Benjamin Otte's avatar
Benjamin Otte committed
348 349
    return FALSE;

350
  return func (selector, &parent, data);
Benjamin Otte's avatar
Benjamin Otte committed
351 352
}

353
static GtkCssChange
354
gtk_css_selector_child_get_change (const GtkCssSelector *selector, GtkCssChange previous_change)
355
{
356
  return _gtk_css_change_for_child (previous_change);
357 358
}

Benjamin Otte's avatar
Benjamin Otte committed
359 360 361
static const GtkCssSelectorClass GTK_CSS_SELECTOR_CHILD = {
  "child",
  gtk_css_selector_child_print,
362
  gtk_css_selector_child_foreach_matcher,
363
  gtk_css_selector_default_match_one,
364
  gtk_css_selector_child_get_change,
365
  gtk_css_selector_default_add_specificity,
366 367
  gtk_css_selector_default_hash_one,
  gtk_css_selector_default_compare_one,
368
  FALSE
Benjamin Otte's avatar
Benjamin Otte committed
369 370
};

371 372 373 374 375 376 377 378 379 380
/* SIBLING */

static void
gtk_css_selector_sibling_print (const GtkCssSelector *selector,
                                GString              *string)
{
  g_string_append (string, " ~ ");
}

static gboolean
381 382 383 384
gtk_css_selector_sibling_foreach_matcher (const GtkCssSelector      *selector,
                                          const GtkCssMatcher       *matcher,
                                          GtkCssSelectorForeachFunc  func,
                                          gpointer                   data)
385
{
Benjamin Otte's avatar
Benjamin Otte committed
386 387 388
  GtkCssMatcher previous;

  while (_gtk_css_matcher_get_previous (&previous, matcher))
389
    {
Benjamin Otte's avatar
Benjamin Otte committed
390 391
      matcher = &previous;

392
      if (func (selector, matcher, data))
393
        return TRUE;
394 395 396 397 398

      /* any matchers are dangerous here, as we may loop forever, but
	 we can terminate now as all possible matches have already been added */
      if (_gtk_css_matcher_matches_any (matcher))
	break;
399 400 401 402 403
    }

  return FALSE;
}

404
static GtkCssChange
405
gtk_css_selector_sibling_get_change (const GtkCssSelector *selector, GtkCssChange previous_change)
406
{
407
  return _gtk_css_change_for_sibling (previous_change);
408 409
}

410 411 412
static const GtkCssSelectorClass GTK_CSS_SELECTOR_SIBLING = {
  "sibling",
  gtk_css_selector_sibling_print,
413
  gtk_css_selector_sibling_foreach_matcher,
414
  gtk_css_selector_default_match_one,
415
  gtk_css_selector_sibling_get_change,
416
  gtk_css_selector_default_add_specificity,
417 418
  gtk_css_selector_default_hash_one,
  gtk_css_selector_default_compare_one,
419
  FALSE
420 421 422 423 424 425 426 427 428 429 430 431
};

/* ADJACENT */

static void
gtk_css_selector_adjacent_print (const GtkCssSelector *selector,
                                 GString              *string)
{
  g_string_append (string, " + ");
}

static gboolean
432 433 434 435
gtk_css_selector_adjacent_foreach_matcher (const GtkCssSelector      *selector,
                                           const GtkCssMatcher       *matcher,
                                           GtkCssSelectorForeachFunc  func,
                                           gpointer                   data)
436
{
Benjamin Otte's avatar
Benjamin Otte committed
437 438 439
  GtkCssMatcher previous;

  if (!_gtk_css_matcher_get_previous (&previous, matcher))
440
    return FALSE;
441 442
  
  return func (selector, &previous, data);
443 444
}

445
static GtkCssChange
446
gtk_css_selector_adjacent_get_change (const GtkCssSelector *selector, GtkCssChange previous_change)
447
{
448
  return _gtk_css_change_for_sibling (previous_change);
449 450
}

451 452 453
static const GtkCssSelectorClass GTK_CSS_SELECTOR_ADJACENT = {
  "adjacent",
  gtk_css_selector_adjacent_print,
454
  gtk_css_selector_adjacent_foreach_matcher,
455
  gtk_css_selector_default_match_one,
456
  gtk_css_selector_adjacent_get_change,
457
  gtk_css_selector_default_add_specificity,
458 459
  gtk_css_selector_default_hash_one,
  gtk_css_selector_default_compare_one,
460
  FALSE
461 462
};

463 464 465 466 467 468
/* SIMPLE SELECTOR DEFINE */

#define DEFINE_SIMPLE_SELECTOR(n, \
                               c, \
                               print_func, \
                               match_func, \
469
                               hash_func, \
470 471 472 473 474 475 476 477 478 479 480
                               comp_func, \
                               increase_id_specificity, \
                               increase_class_specificity, \
                               increase_element_specificity) \
static void \
gtk_css_selector_ ## n ## _print (const GtkCssSelector *selector, \
                                    GString              *string) \
{ \
  print_func (selector, string); \
} \
\
481 482 483 484 485 486 487 488 489
static void \
gtk_css_selector_not_ ## n ## _print (const GtkCssSelector *selector, \
                                      GString              *string) \
{ \
  g_string_append (string, ":not("); \
  print_func (selector, string); \
  g_string_append (string, ")"); \
} \
\
490
static gboolean \
491 492
gtk_css_selector_not_ ## n ## _match_one (const GtkCssSelector *selector, \
                                          const GtkCssMatcher  *matcher) \
493
{ \
494
  return !match_func (selector, matcher); \
495 496
} \
\
497 498 499 500 501 502
static GtkCssChange \
gtk_css_selector_ ## n ## _get_change (const GtkCssSelector *selector, GtkCssChange previous_change) \
{ \
  return previous_change | GTK_CSS_CHANGE_ ## c; \
} \
\
503 504 505 506 507 508 509
static void \
gtk_css_selector_ ## n ## _add_specificity (const GtkCssSelector *selector, \
                                            guint                *ids, \
                                            guint                *classes, \
                                            guint                *elements) \
{ \
  if (increase_id_specificity) \
510 511 512
    { \
      (*ids)++; \
    } \
513
  if (increase_class_specificity) \
514 515 516
    { \
      (*classes)++; \
    } \
517
  if (increase_element_specificity) \
518 519 520
    { \
      (*elements)++; \
    } \
521 522
} \
\
523 524 525
static const GtkCssSelectorClass GTK_CSS_SELECTOR_ ## c = { \
  G_STRINGIFY(n), \
  gtk_css_selector_ ## n ## _print, \
526
  gtk_css_selector_default_foreach_matcher, \
527
  match_func, \
528
  gtk_css_selector_ ## n ## _get_change, \
529
  gtk_css_selector_ ## n ## _add_specificity, \
530
  hash_func, \
531
  comp_func, \
532
  TRUE \
533 534 535 536 537
};\
\
static const GtkCssSelectorClass GTK_CSS_SELECTOR_NOT_ ## c = { \
  "not_" G_STRINGIFY(n), \
  gtk_css_selector_not_ ## n ## _print, \
538
  gtk_css_selector_default_foreach_matcher, \
539
  gtk_css_selector_not_ ## n ## _match_one, \
540
  gtk_css_selector_ ## n ## _get_change, \
541
  gtk_css_selector_ ## n ## _add_specificity, \
542
  hash_func, \
543
  comp_func, \
544
  TRUE \
545 546
};

547 548 549
/* ANY */

static void
550 551
print_any (const GtkCssSelector *selector,
           GString              *string)
552 553 554 555 556
{
  g_string_append_c (string, '*');
}

static gboolean
557 558
match_any (const GtkCssSelector *selector,
           const GtkCssMatcher  *matcher)
559
{
560
  return TRUE;
561 562
}

563 564
#undef GTK_CSS_CHANGE_ANY
#define GTK_CSS_CHANGE_ANY 0
565 566 567
DEFINE_SIMPLE_SELECTOR(any, ANY, print_any, match_any, 
                       gtk_css_selector_default_hash_one, gtk_css_selector_default_compare_one,
                       FALSE, FALSE, FALSE)
568
#undef GTK_CSS_CHANGE_ANY
569

Benjamin Otte's avatar
Benjamin Otte committed
570 571 572
/* NAME */

static void
573 574
print_name (const GtkCssSelector *selector,
            GString              *string)
Benjamin Otte's avatar
Benjamin Otte committed
575
{
576
  g_string_append (string, selector->name.name);
Benjamin Otte's avatar
Benjamin Otte committed
577 578 579
}

static gboolean
580 581
match_name (const GtkCssSelector *selector,
            const GtkCssMatcher  *matcher)
Benjamin Otte's avatar
Benjamin Otte committed
582
{
583
  return _gtk_css_matcher_has_name (matcher, selector->name.name);
584 585
}

586 587 588
static guint
hash_name (const GtkCssSelector *a)
{
589
  return g_str_hash (a->name.name);
590 591
}

592
static int
593 594
comp_name (const GtkCssSelector *a,
           const GtkCssSelector *b)
595
{
596 597
  return strcmp (a->name.name,
		 b->name.name);
598 599
}

600
DEFINE_SIMPLE_SELECTOR(name, NAME, print_name, match_name, hash_name, comp_name, FALSE, FALSE, TRUE)
Benjamin Otte's avatar
Benjamin Otte committed
601 602 603 604

/* CLASS */

static void
605 606
print_class (const GtkCssSelector *selector,
             GString              *string)
Benjamin Otte's avatar
Benjamin Otte committed
607 608
{
  g_string_append_c (string, '.');
609
  g_string_append (string, g_quark_to_string (selector->style_class.style_class));
Benjamin Otte's avatar
Benjamin Otte committed
610 611 612
}

static gboolean
613 614
match_class (const GtkCssSelector *selector,
             const GtkCssMatcher  *matcher)
615
{
616
  return _gtk_css_matcher_has_class (matcher, selector->style_class.style_class);
617 618
}

619 620 621
static guint
hash_class (const GtkCssSelector *a)
{
622
  return a->style_class.style_class;
623 624
}

625
static int
626 627
comp_class (const GtkCssSelector *a,
            const GtkCssSelector *b)
628
{
629 630 631 632 633 634
  if (a->style_class.style_class < b->style_class.style_class)
    return -1;
  if (a->style_class.style_class > b->style_class.style_class)
    return 1;
  else
    return 0;
635 636
}

637
DEFINE_SIMPLE_SELECTOR(class, CLASS, print_class, match_class, hash_class, comp_class, FALSE, TRUE, FALSE)
Benjamin Otte's avatar
Benjamin Otte committed
638 639 640 641

/* ID */

static void
642 643
print_id (const GtkCssSelector *selector,
          GString              *string)
Benjamin Otte's avatar
Benjamin Otte committed
644 645
{
  g_string_append_c (string, '#');
646
  g_string_append (string, selector->id.name);
Benjamin Otte's avatar
Benjamin Otte committed
647 648 649
}

static gboolean
650 651
match_id (const GtkCssSelector *selector,
          const GtkCssMatcher  *matcher)
652
{
653
  return _gtk_css_matcher_has_id (matcher, selector->id.name);
654 655
}

656 657 658
static guint
hash_id (const GtkCssSelector *a)
{
659
  return GPOINTER_TO_UINT (a->id.name);
660 661
}

662
static int
663 664
comp_id (const GtkCssSelector *a,
	 const GtkCssSelector *b)
665
{
666 667 668 669 670 671
  if (a->id.name < b->id.name)
    return -1;
  else if (a->id.name > b->id.name)
    return 1;
  else
    return 0;
672 673
}

674
DEFINE_SIMPLE_SELECTOR(id, ID, print_id, match_id, hash_id, comp_id, TRUE, FALSE, FALSE)
Benjamin Otte's avatar
Benjamin Otte committed
675

676 677
const gchar *
gtk_css_pseudoclass_name (GtkStateFlags state)
Benjamin Otte's avatar
Benjamin Otte committed
678 679 680 681 682
{
  static const char * state_names[] = {
    "active",
    "hover",
    "selected",
683 684
    "disabled",
    "indeterminate",
Benjamin Otte's avatar
Benjamin Otte committed
685
    "focus",
686 687
    "backdrop",
    "dir(ltr)",
688 689
    "dir(rtl)",
    "link",
690
    "visited",
691
    "checked",
692
    "drop(active)"
Benjamin Otte's avatar
Benjamin Otte committed
693
  };
694
  guint i;
Benjamin Otte's avatar
Benjamin Otte committed
695 696 697

  for (i = 0; i < G_N_ELEMENTS (state_names); i++)
    {
698 699
      if (state == (1 << i))
        return state_names[i];
Benjamin Otte's avatar
Benjamin Otte committed
700 701
    }

702 703 704 705 706 707 708 709 710 711
  return NULL;
}

/* PSEUDOCLASS FOR STATE */
static void
print_pseudoclass_state (const GtkCssSelector *selector,
                         GString              *string)
{
  g_string_append_c (string, ':');
  g_string_append (string, gtk_css_pseudoclass_name (selector->state.state));
Benjamin Otte's avatar
Benjamin Otte committed
712 713 714
}

static gboolean
715 716
match_pseudoclass_state (const GtkCssSelector *selector,
                         const GtkCssMatcher  *matcher)
Benjamin Otte's avatar
Benjamin Otte committed
717
{
718
  return (_gtk_css_matcher_get_state (matcher) & selector->state.state) == selector->state.state;
719 720
}

721
static guint
722
hash_pseudoclass_state (const GtkCssSelector *selector)
723
{
724
  return selector->state.state;
725 726 727
}


728
static int
729 730
comp_pseudoclass_state (const GtkCssSelector *a,
		        const GtkCssSelector *b)
731
{
732
  return a->state.state - b->state.state;
733 734
}

735
#define GTK_CSS_CHANGE_PSEUDOCLASS_STATE GTK_CSS_CHANGE_STATE
736 737 738
DEFINE_SIMPLE_SELECTOR(pseudoclass_state, PSEUDOCLASS_STATE, print_pseudoclass_state,
                       match_pseudoclass_state, hash_pseudoclass_state, comp_pseudoclass_state,
                       FALSE, TRUE, FALSE)
739
#undef GTK_CSS_CHANGE_PSEUDOCLASS_STATE
Benjamin Otte's avatar
Benjamin Otte committed
740

741
/* PSEUDOCLASS FOR POSITION */
Benjamin Otte's avatar
Benjamin Otte committed
742 743

static void
744 745
print_pseudoclass_position (const GtkCssSelector *selector,
                            GString              *string)
Benjamin Otte's avatar
Benjamin Otte committed
746
{
747
  switch (selector->position.type)
Benjamin Otte's avatar
Benjamin Otte committed
748
    {
749
    case POSITION_FORWARD:
750
      if (selector->position.a == 0)
Benjamin Otte's avatar
Benjamin Otte committed
751
        {
752
          if (selector->position.b == 1)
753 754
            g_string_append (string, ":first-child");
          else
755
            g_string_append_printf (string, ":nth-child(%d)", selector->position.b);
756
        }
757
      else if (selector->position.a == 2 && selector->position.b == 0)
758
        g_string_append (string, ":nth-child(even)");
759
      else if (selector->position.a == 2 && selector->position.b == 1)
760 761 762 763
        g_string_append (string, ":nth-child(odd)");
      else
        {
          g_string_append (string, ":nth-child(");
764
          if (selector->position.a == 1)
765
            g_string_append (string, "n");
766
          else if (selector->position.a == -1)
767 768
            g_string_append (string, "-n");
          else
769 770 771 772 773
            g_string_append_printf (string, "%dn", selector->position.a);
          if (selector->position.b > 0)
            g_string_append_printf (string, "+%d)", selector->position.b);
          else if (selector->position.b < 0)
            g_string_append_printf (string, "%d)", selector->position.b);
774 775
          else
            g_string_append (string, ")");
Benjamin Otte's avatar
Benjamin Otte committed
776
        }
777 778
      break;
    case POSITION_BACKWARD:
779
      if (selector->position.a == 0)
780
        {
781
          if (selector->position.b == 1)
782 783
            g_string_append (string, ":last-child");
          else
784
            g_string_append_printf (string, ":nth-last-child(%d)", selector->position.b);
785
        }
786
      else if (selector->position.a == 2 && selector->position.b == 0)
787
        g_string_append (string, ":nth-last-child(even)");
788
      else if (selector->position.a == 2 && selector->position.b == 1)
789 790 791 792
        g_string_append (string, ":nth-last-child(odd)");
      else
        {
          g_string_append (string, ":nth-last-child(");
793
          if (selector->position.a == 1)
794
            g_string_append (string, "n");
795
          else if (selector->position.a == -1)
796 797
            g_string_append (string, "-n");
          else
798 799 800 801 802
            g_string_append_printf (string, "%dn", selector->position.a);
          if (selector->position.b > 0)
            g_string_append_printf (string, "+%d)", selector->position.b);
          else if (selector->position.b < 0)
            g_string_append_printf (string, "%d)", selector->position.b);
803 804 805 806 807 808 809 810 811 812 813 814 815
          else
            g_string_append (string, ")");
        }
      break;
    case POSITION_ONLY:
      g_string_append (string, ":only-child");
      break;
    case POSITION_SORTED:
      g_string_append (string, ":sorted");
      break;
    default:
      g_assert_not_reached ();
      break;
Benjamin Otte's avatar
Benjamin Otte committed
816 817 818 819
    }
}

static gboolean
820 821
match_pseudoclass_position (const GtkCssSelector *selector,
		            const GtkCssMatcher  *matcher)
Benjamin Otte's avatar
Benjamin Otte committed
822
{
823
  switch (selector->position.type)
Benjamin Otte's avatar
Benjamin Otte committed
824
    {
825
    case POSITION_FORWARD:
826
      if (!_gtk_css_matcher_has_position (matcher, TRUE, selector->position.a, selector->position.b))
Benjamin Otte's avatar
Benjamin Otte committed
827 828
        return FALSE;
      break;
829
    case POSITION_BACKWARD:
830
      if (!_gtk_css_matcher_has_position (matcher, FALSE, selector->position.a, selector->position.b))
Benjamin Otte's avatar
Benjamin Otte committed
831 832
        return FALSE;
      break;
833 834 835
    case POSITION_ONLY:
      if (!_gtk_css_matcher_has_position (matcher, TRUE, 0, 1) ||
          !_gtk_css_matcher_has_position (matcher, FALSE, 0, 1))
Benjamin Otte's avatar
Benjamin Otte committed
836 837
        return FALSE;
      break;
838
    case POSITION_SORTED:
Benjamin Otte's avatar
Benjamin Otte committed
839 840 841 842 843 844
      return FALSE;
    default:
      g_assert_not_reached ();
      return FALSE;
    }

845 846 847
  return TRUE;
}

848
static guint
849
hash_pseudoclass_position (const GtkCssSelector *a)
850
{
851
  return (guint)(((((gulong)a->position.type) << POSITION_NUMBER_BITS) | a->position.a) << POSITION_NUMBER_BITS) | a->position.b;
852 853
}

854
static int
855 856
comp_pseudoclass_position (const GtkCssSelector *a,
			   const GtkCssSelector *b)
857
{
858 859 860 861 862 863 864 865 866 867 868
  int diff;
  
  diff = a->position.type - b->position.type;
  if (diff)
    return diff;

  diff = a->position.a - b->position.a;
  if (diff)
    return diff;

  return a->position.b - b->position.b;
869 870
}

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
static GtkCssChange
change_pseudoclass_position (const GtkCssSelector *selector)
{
  switch (selector->position.type)
    {
    case POSITION_FORWARD:
      if (selector->position.a == 0 && selector->position.b == 1)
        return GTK_CSS_CHANGE_FIRST_CHILD;
      else
        return GTK_CSS_CHANGE_NTH_CHILD;
    case POSITION_BACKWARD:
      if (selector->position.a == 0 && selector->position.b == 1)
        return GTK_CSS_CHANGE_LAST_CHILD;
      else
        return GTK_CSS_CHANGE_NTH_LAST_CHILD;
    case POSITION_ONLY:
      return GTK_CSS_CHANGE_FIRST_CHILD | GTK_CSS_CHANGE_LAST_CHILD;
    default:
      g_assert_not_reached ();
    case POSITION_SORTED:
      return 0;
    }
}

#define GTK_CSS_CHANGE_PSEUDOCLASS_POSITION change_pseudoclass_position(selector)
896 897 898 899
DEFINE_SIMPLE_SELECTOR(pseudoclass_position, PSEUDOCLASS_POSITION, print_pseudoclass_position,
                       match_pseudoclass_position, hash_pseudoclass_position, comp_pseudoclass_position,
                       FALSE, TRUE, FALSE)
#undef GTK_CSS_CHANGE_PSEUDOCLASS_POSITION
Benjamin Otte's avatar
Benjamin Otte committed
900 901
/* API */

902 903 904 905 906 907 908 909 910 911 912 913 914 915
static guint
gtk_css_selector_size (const GtkCssSelector *selector)
{
  guint size = 0;

  while (selector)
    {
      selector = gtk_css_selector_previous (selector);
      size++;
    }

  return size;
}

916
static GtkCssSelector *
Benjamin Otte's avatar
Benjamin Otte committed
917
gtk_css_selector_new (const GtkCssSelectorClass *class,
918
                      GtkCssSelector            *selector)
Benjamin Otte's avatar
Benjamin Otte committed
919
{
920 921 922 923 924 925 926 927
  guint size;

  size = gtk_css_selector_size (selector);
  selector = g_realloc (selector, sizeof (GtkCssSelector) * (size + 1) + sizeof (gpointer));
  if (size == 0)
    selector[1].class = NULL;
  else
    memmove (selector + 1, selector, sizeof (GtkCssSelector) * size + sizeof (gpointer));
Benjamin Otte's avatar
Benjamin Otte committed
928

929
  memset (selector, 0, sizeof (GtkCssSelector));
Benjamin Otte's avatar
Benjamin Otte committed
930
  selector->class = class;
Benjamin Otte's avatar
Benjamin Otte committed
931 932 933 934

  return selector;
}

Benjamin Otte's avatar
Benjamin Otte committed
935
static GtkCssSelector *
936 937 938
parse_selector_class (GtkCssParser   *parser,
                      GtkCssSelector *selector,
                      gboolean        negate)
939 940 941 942 943 944 945 946
{
  char *name;
    
  name = _gtk_css_parser_try_name (parser, FALSE);

  if (name == NULL)
    {
      _gtk_css_parser_error (parser, "Expected a valid name for class");
Benjamin Otte's avatar
Benjamin Otte committed
947 948 949
      if (selector)
        _gtk_css_selector_free (selector);
      return NULL;
950 951
    }

952 953
  selector = gtk_css_selector_new (negate ? &GTK_CSS_SELECTOR_NOT_CLASS
                                          : &GTK_CSS_SELECTOR_CLASS,
954 955
                                   selector);
  selector->style_class.style_class = g_quark_from_string (name);
Benjamin Otte's avatar
Benjamin Otte committed
956

957
  g_free (name);
Benjamin Otte's avatar
Benjamin Otte committed
958 959

  return selector;
960 961
}

Benjamin Otte's avatar
Benjamin Otte committed
962
static GtkCssSelector *
963 964 965
parse_selector_id (GtkCssParser   *parser,
                   GtkCssSelector *selector,
                   gboolean        negate)
966 967 968 969 970 971 972 973
{
  char *name;
    
  name = _gtk_css_parser_try_name (parser, FALSE);

  if (name == NULL)
    {
      _gtk_css_parser_error (parser, "Expected a valid name for id");
Benjamin Otte's avatar
Benjamin Otte committed
974 975 976
      if (selector)
        _gtk_css_selector_free (selector);
      return NULL;
977 978
    }

979 980
  selector = gtk_css_selector_new (negate ? &GTK_CSS_SELECTOR_NOT_ID
                                          : &GTK_CSS_SELECTOR_ID,
981 982
                                   selector);
  selector->id.name = g_intern_string (name);
Benjamin Otte's avatar
Benjamin Otte committed
983

984
  g_free (name);
Benjamin Otte's avatar
Benjamin Otte committed
985 986

  return selector;
987 988
}

Benjamin Otte's avatar
Benjamin Otte committed
989
static GtkCssSelector *
990 991
parse_selector_pseudo_class_nth_child (GtkCssParser   *parser,
                                       GtkCssSelector *selector,
992 993
                                       PositionType    type,
                                       gboolean        negate)
994
{
995
  int a, b;
996

997
  if (!_gtk_css_parser_try (parser, "(", TRUE))
998
    {
999
      _gtk_css_parser_error (parser, "Missing opening bracket for pseudo-class");
Benjamin Otte's avatar
Benjamin Otte committed
1000 1001 1002
      if (selector)
        _gtk_css_selector_free (selector);
      return NULL;
1003 1004
    }

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
  if (_gtk_css_parser_try (parser, "even", TRUE))
    {
      a = 2;
      b = 0;
    }
  else if (_gtk_css_parser_try (parser, "odd", TRUE))
    {
      a = 2;
      b = 1;
    }
  else if (type == POSITION_FORWARD &&
           _gtk_css_parser_try (parser, "first", TRUE))
1017
    {
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
      a = 0;
      b = 1;
    }
  else if (type == POSITION_FORWARD &&
           _gtk_css_parser_try (parser, "last", TRUE))
    {
      a = 0;
      b = 1;
      type = POSITION_BACKWARD;
    }
  else
    {
      int multiplier;
1031

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
      if (_gtk_css_parser_try (parser, "+", TRUE))
        multiplier = 1;
      else if (_gtk_css_parser_try (parser, "-", TRUE))
        multiplier = -1;
      else
        multiplier = 1;

      if (_gtk_css_parser_try_int (parser, &a))
        {
          if (a < 0)
            {
              _gtk_css_parser_error (parser, "Expected an integer");
              if (selector)
                _gtk_css_selector_free (selector);
              return NULL;
            }
          a *= multiplier;
        }
      else if (_gtk_css_parser_has_prefix (parser, "n"))
1051
        {
1052 1053 1054 1055 1056
          a = multiplier;
        }
      else
        {
          _gtk_css_parser_error (parser, "Expected an integer");
Benjamin Otte's avatar
Benjamin Otte committed
1057 1058 1059
          if (selector)
            _gtk_css_selector_free (selector);
          return NULL;
1060 1061
        }

1062
      if (_gtk_css_parser_try (parser, "n", TRUE))
1063
        {
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
          if (_gtk_css_parser_try (parser, "+", TRUE))
            multiplier = 1;
          else if (_gtk_css_parser_try (parser, "-", TRUE))
            multiplier = -1;
          else
            multiplier = 1;

          if (_gtk_css_parser_try_int (parser, &b))
            {
              if (b < 0)
                {
                  _gtk_css_parser_error (parser, "Expected an integer");
                  if (selector)
                    _gtk_css_selector_free (selector);
                  return NULL;
                }
            }
          else
            b = 0;

          b *= multiplier;
1085
        }
1086
      else
1087
        {
1088 1089
          b = a;
          a = 0;
1090
        }
1091
    }
1092

1093 1094 1095 1096 1097 1098
  if (!_gtk_css_parser_try (parser, ")", FALSE))
    {
      _gtk_css_parser_error (parser, "Missing closing bracket for pseudo-class");
      if (selector)
        _gtk_css_selector_free (selector);
      return NULL;
1099 1100
    }

1101 1102
  selector = gtk_css_selector_new (negate ? &GTK_CSS_SELECTOR_NOT_PSEUDOCLASS_POSITION
                                          : &GTK_CSS_SELECTOR_PSEUDOCLASS_POSITION,
1103 1104 1105 1106
                                   selector);
  selector->position.type = type;
  selector->position.a = a;
  selector->position.b = b;
1107 1108 1109 1110 1111 1112

  return selector;
}

static GtkCssSelector *
parse_selector_pseudo_class (GtkCssParser   *parser,
1113 1114
                             GtkCssSelector *selector,
                             gboolean        negate)
1115 1116 1117
{
  static const struct {
    const char    *name;
1118
    gboolean       deprecated;
1119 1120 1121 1122 1123
    GtkStateFlags  state_flag;
    PositionType   position_type;
    int            position_a;
    int            position_b;
  } pseudo_classes[] = {
1124 1125 1126
    { "first-child",   0, 0,                           POSITION_FORWARD,  0, 1 },
    { "last-child",    0, 0,                           POSITION_BACKWARD, 0, 1 },
    { "only-child",    0, 0,                           POSITION_ONLY,     0, 0 },
1127
    { "sorted",        1, 0,                           POSITION_SORTED,   0, 0 },
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
    { "active",        0, GTK_STATE_FLAG_ACTIVE, },
    { "prelight",      1, GTK_STATE_FLAG_PRELIGHT, },
    { "hover",         0, GTK_STATE_FLAG_PRELIGHT, },
    { "selected",      0, GTK_STATE_FLAG_SELECTED, },
    { "insensitive",   1, GTK_STATE_FLAG_INSENSITIVE, },
    { "disabled",      0, GTK_STATE_FLAG_INSENSITIVE, },
    { "inconsistent",  1, GTK_STATE_FLAG_INCONSISTENT, },
    { "indeterminate", 0, GTK_STATE_FLAG_INCONSISTENT, },
    { "focused",       1, GTK_STATE_FLAG_FOCUSED, },
    { "focus",         0, GTK_STATE_FLAG_FOCUSED, },
    { "backdrop",      0, GTK_STATE_FLAG_BACKDROP, },
    { "dir(ltr)",      0, GTK_STATE_FLAG_DIR_LTR, },
    { "dir(rtl)",      0, GTK_STATE_FLAG_DIR_RTL, },
    { "link",          0, GTK_STATE_FLAG_LINK, },
    { "visited",       0, GTK_STATE_FLAG_VISITED, },
    { "checked",       0, GTK_STATE_FLAG_CHECKED, },
    { "drop(active)",  0, GTK_STATE_FLAG_DROP_ACTIVE, }
1145
  };
1146

1147 1148 1149
  guint i;

  if (_gtk_css_parser_try (parser, "nth-child", FALSE))
1150
    return parse_selector_pseudo_class_nth_child (parser, selector, POSITION_FORWARD, negate);
1151
  else if (_gtk_css_parser_try (parser, "nth-last-child", FALSE))
1152
    return parse_selector_pseudo_class_nth_child (parser, selector, POSITION_BACKWARD, negate);
1153 1154

  for (i = 0; i < G_N_ELEMENTS (pseudo_classes); i++)
1155
    {
1156
      if (_gtk_css_parser_try (parser, pseudo_classes[i].name, FALSE))
1157
        {
1158
          if (pseudo_classes[i].state_flag)
1159 1160 1161 1162 1163
            {
              selector = gtk_css_selector_new (negate ? &GTK_CSS_SELECTOR_NOT_PSEUDOCLASS_STATE
                                                      : &GTK_CSS_SELECTOR_PSEUDOCLASS_STATE,
                                               selector);
              selector->state.state = pseudo_classes[i].state_flag;
1164
              if (pseudo_classes[i].deprecated)
1165
                {
1166 1167
                  if (i + 1 < G_N_ELEMENTS (pseudo_classes) &&
                      pseudo_classes[i + 1].state_flag == pseudo_classes[i].state_flag)
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
                    _gtk_css_parser_error_full (parser,
                                                GTK_CSS_PROVIDER_ERROR_DEPRECATED,
                                                "The :%s pseudo-class is deprecated. Use :%s instead.",
                                                pseudo_classes[i].name,
                                                pseudo_classes[i + 1].name);
                  else
                    _gtk_css_parser_error_full (parser,
                                                GTK_CSS_PROVIDER_ERROR_DEPRECATED,
                                                "The :%s pseudo-class is deprecated.",
                                                pseudo_classes[i].name);
1178
                }
1179
            }
Benjamin Otte's avatar
Benjamin Otte committed
1180
          else
1181
            {
1182 1183
              selector = gtk_css_selector_new (negate ? &GTK_CSS_SELECTOR_NOT_PSEUDOCLASS_POSITION
                                                      : &GTK_CSS_SELECTOR_PSEUDOCLASS_POSITION,
1184 1185 1186 1187 1188
                                               selector);
              selector->position.type = pseudo_classes[i].position_type;
              selector->position.a = pseudo_classes[i].position_a;
              selector->position.b = pseudo_classes[i].position_b;
            }
Benjamin Otte's avatar
Benjamin Otte committed
1189
          return selector;
1190 1191
        }
    }
1192
      
1193
  _gtk_css_parser_error (parser, "Invalid name of pseudo-class");
Benjamin Otte's avatar
Benjamin Otte committed
1194 1195 1196
  if (selector)
    _gtk_css_selector_free (selector);
  return NULL;
1197 1198
}

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
static GtkCssSelector *
parse_selector_negation (GtkCssParser   *parser,
                         GtkCssSelector *selector)
{
  char *name;

  name = _gtk_css_parser_try_ident (parser, FALSE);
  if (name)
    {
      selector = gtk_css_selector_new (&GTK_CSS_SELECTOR_NOT_NAME,
1209
                                       selector);
1210
      selector->name.name = g_intern_string (name);
1211 1212
      g_free (name);
    }
1213
  else if (_gtk_css_parser_try (parser, "*", FALSE))
1214
    selector = gtk_css_selector_new (&GTK_CSS_SELECTOR_NOT_ANY, selector);
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
  else if (_gtk_css_parser_try (parser, "#", FALSE))
    selector = parse_selector_id (parser, selector, TRUE);
  else if (_gtk_css_parser_try (parser, ".", FALSE))
    selector = parse_selector_class (parser, selector, TRUE);
  else if (_gtk_css_parser_try (parser, ":", FALSE))
    selector = parse_selector_pseudo_class (parser, selector, TRUE);
  else
    {
      _gtk_css_parser_error (parser, "Not a valid selector for :not()");
      if (selector)
        _gtk_css_selector_free (selector);
      return NULL;
    }
  
  _gtk_css_parser_skip_whitespace (parser);

  if (!_gtk_css_parser_try (parser, ")", FALSE))
    {
      _gtk_css_parser_error (parser, "Missing closing bracket for :not()");
      if (selector)
        _gtk_css_selector_free (selector);
      return NULL;
    }

  return selector;
}

Benjamin Otte's avatar
Benjamin Otte committed
1242 1243
static GtkCssSelector *
parse_simple_selector (GtkCssParser   *parser,
1244
                       GtkCssSelector *selector)
Benjamin Otte's avatar
Benjamin Otte committed
1245
{
1246 1247 1248 1249 1250 1251
  gboolean parsed_something = FALSE;
  char *name;

  name = _gtk_css_parser_try_ident (parser, FALSE);
  if (name)
    {
1252 1253
      selector = gtk_css_selector_new (&GTK_CSS_SELECTOR_NAME,
                                       selector);
1254
      selector->name.name = g_intern_string (name);
1255 1256 1257 1258 1259
      g_free (name);
      parsed_something = TRUE;
    }
  else if (_gtk_css_parser_try (parser, "*", FALSE))
    {
1260
      selector = gtk_css_selector_new (&GTK_CSS_SELECTOR_ANY, selector);
1261 1262
      parsed_something = TRUE;
    }
1263 1264 1265

  do {
      if (_gtk_css_parser_try (parser, "#", FALSE))
1266
        selector = parse_selector_id (parser, selector, FALSE);
1267
      else if (_gtk_css_parser_try (parser, ".", FALSE))
1268 1269 1270
        selector = parse_selector_class (parser, selector, FALSE);
      else if (_gtk_css_parser_try (parser, ":not(", TRUE))
        selector = parse_selector_negation (parser, selector);
1271
      else if (_gtk_css_parser_try (parser, ":", FALSE))
1272
        selector = parse_selector_pseudo_class (parser, selector, FALSE);
1273
      else if (!parsed_something)
1274 1275
        {
          _gtk_css_parser_error (parser, "Expected a valid selector");
Benjamin Otte's avatar
Benjamin Otte committed
1276 1277
          if (selector)
            _gtk_css_selector_free (selector);
1278
          return NULL;
1279 1280 1281
        }
      else
        break;
1282 1283

      parsed_something = TRUE;
1284
    }
Benjamin Otte's avatar
Benjamin Otte committed
1285
  while (selector && !_gtk_css_parser_is_eof (parser));
1286 1287

  _gtk_css_parser_skip_whitespace (parser);
Benjamin Otte's avatar
Benjamin Otte committed
1288 1289

  return selector;
1290 1291 1292 1293 1294 1295 1296
}

GtkCssSelector *
_gtk_css_selector_parse (GtkCssParser *parser)
{
  GtkCssSelector *selector = NULL;

Benjamin Otte's avatar
Benjamin Otte committed
1297 1298
  while ((selector = parse_simple_selector (parser, selector)) &&
         !_gtk_css_parser_is_eof (parser) &&
1299
         !_gtk_css_parser_begins_with (parser, ',') &&
Benjamin Otte's avatar
Benjamin Otte committed
1300 1301
         !_gtk_css_parser_begins_with (parser, '{'))
    {
1302
      if (_gtk_css_parser_try (parser, "+", TRUE))
1303
        selector = gtk_css_selector_new (&GTK_CSS_SELECTOR_ADJACENT, selector);
1304
      else if (_gtk_css_parser_try (parser, "~", TRUE))
1305
        selector = gtk_css_selector_new (&GTK_CSS_SELECTOR_SIBLING, selector);
1306
      else if (_gtk_css_parser_try (parser, ">", TRUE))
1307
        selector = gtk_css_selector_new (&GTK_CSS_SELECTOR_CHILD, selector);
Benjamin Otte's avatar
Benjamin Otte committed
1308
      else
1309
        selector = gtk_css_selector_new (&GTK_CSS_SELECTOR_DESCENDANT, selector);
Benjamin Otte's avatar
Benjamin Otte committed
1310
    }
1311 1312 1313 1314

  return selector;
}

Benjamin Otte's avatar
Benjamin Otte committed
1315 1316 1317 1318 1319
void
_gtk_css_selector_free (GtkCssSelector *selector)
{
  g_return_if_fail (selector != NULL);

1320
  g_free (selector);
Benjamin Otte's avatar
Benjamin Otte committed
1321 1322 1323 1324 1325 1326
}

void
_gtk_css_selector_print (const GtkCssSelector *selector,
                         GString *             str)
{
1327 1328
  const GtkCssSelector *previous;

Benjamin Otte's avatar
Benjamin Otte committed
1329
  g_return_if_fail (selector != NULL);
Benjamin Otte's avatar
Benjamin Otte committed
1330

1331 1332 1333
  previous = gtk_css_selector_previous (selector);
  if (previous)
    _gtk_css_selector_print (previous, str);
Benjamin Otte's avatar
Benjamin Otte committed
1334

Benjamin Otte's avatar
Benjamin Otte committed
1335
  selector->class->print (selector, str);
Benjamin Otte's avatar
Benjamin Otte committed
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
}

char *
_gtk_css_selector_to_string (const GtkCssSelector *selector)
{
  GString *string;

  g_return_val_if_fail (selector != NULL, NULL);

  string = g_string_new (NULL);

  _gtk_css_selector_print (selector, string);

  return g_string_free (string, FALSE);
}

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
static gboolean
gtk_css_selector_foreach_match (const GtkCssSelector *selector,
                                const GtkCssMatcher  *matcher,
                                gpointer              unused)
{
  selector = gtk_css_selector_previous (selector);

  if (selector == NULL)
    return TRUE;

  if (!gtk_css_selector_match (selector, matcher))
    return FALSE;

  return gtk_css_selector_foreach (selector, matcher, gtk_css_selector_foreach_match, NULL);
}

1368 1369 1370 1371
/**
 * _gtk_css_selector_matches:
 * @selector: the selector
 * @path: the path to check
1372
 * @state: The state to match
1373 1374 1375 1376 1377 1378 1379 1380 1381
 *
 * Checks if the @selector matches the given @path. If @length is
 * smaller than the number of elements in @path, it is assumed that
 * only the first @length element of @path are valid and the rest
 * does not exist. This is useful for doing parent matches for the
 * 'inherit' keyword.
 *
 * Returns: %TRUE if the selector matches @path
 **/
Benjamin Otte's avatar
Benjamin Otte committed
1382
gboolean
1383 1384
_gtk_css_selector_matches (const GtkCssSelector *selector,
                           const GtkCssMatcher  *matcher)
Benjamin Otte's avatar
Benjamin Otte committed
1385 1386 1387
{

  g_return_val_if_fail (selector != NULL, FALSE);
1388
  g_return_val_if_fail (matcher != NULL, FALSE);
Benjamin Otte's avatar
Benjamin Otte committed
1389

1390 1391 1392 1393
  if (!gtk_css_selector_match (selector, matcher))
    return FALSE;

  return gtk_css_selector_foreach (selector, matcher, gtk_css_selector_foreach_match, NULL);
Benjamin Otte's avatar
Benjamin Otte committed
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
}

/* Computes specificity according to CSS 2.1.
 * The arguments must be initialized to 0 */
static void
_gtk_css_selector_get_specificity (const GtkCssSelector *selector,
                                   guint                *ids,
                                   guint                *classes,
                                   guint                *elements)
{