2011-05-14 11:27:31 +00:00
|
|
|
/* 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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-05-14 11:27:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkcssselectorprivate.h"
|
|
|
|
|
2012-11-28 10:15:53 +00:00
|
|
|
#include <stdlib.h>
|
2012-03-01 17:33:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-02-15 14:42:00 +00:00
|
|
|
#include "gtkcssprovider.h"
|
2011-05-16 02:31:07 +00:00
|
|
|
#include "gtkstylecontextprivate.h"
|
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
#include <errno.h>
|
2015-09-23 15:24:36 +00:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER >= 1500
|
2015-09-23 10:01:46 +00:00
|
|
|
# include <intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2020-01-21 15:51:55 +00:00
|
|
|
/*
|
|
|
|
* @GTK_CSS_SELECTOR_CATEGORY_SIMPLE: A simple selector
|
|
|
|
* @GTK_CSS_SELECTOR_CATEGORY_SIMPLE_RADICAL: A simple selector that matches
|
|
|
|
* what change tracking considers a "radical change"
|
|
|
|
* @GTK_CSS_SELECTOR_SIBLING: A selector matching siblings
|
|
|
|
* @GTK_CSS_SELECTOR_CATEGORY_PARENT: A selector matching a parent or other
|
|
|
|
* ancestor
|
|
|
|
*
|
|
|
|
* Categorize the selectors. This helps in various loops when matching.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
GTK_CSS_SELECTOR_CATEGORY_SIMPLE,
|
|
|
|
GTK_CSS_SELECTOR_CATEGORY_SIMPLE_RADICAL,
|
|
|
|
GTK_CSS_SELECTOR_CATEGORY_PARENT,
|
|
|
|
GTK_CSS_SELECTOR_CATEGORY_SIBLING,
|
|
|
|
} GtkCssSelectorCategory;
|
|
|
|
|
2012-02-16 14:16:18 +00:00
|
|
|
typedef struct _GtkCssSelectorClass GtkCssSelectorClass;
|
|
|
|
|
|
|
|
struct _GtkCssSelectorClass {
|
|
|
|
const char *name;
|
2020-01-21 15:51:55 +00:00
|
|
|
GtkCssSelectorCategory category;
|
2012-02-16 14:16:18 +00:00
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
void (* print) (const GtkCssSelector *selector,
|
|
|
|
GString *string);
|
|
|
|
/* NULL or an iterator that returns the next node or %NULL if there are no
|
|
|
|
* more nodes.
|
|
|
|
* Call it like:
|
|
|
|
* for (iter = gtk_css_selector_iterator (node, NULL);
|
|
|
|
* iter;
|
|
|
|
* iter = gtk_css_selector_iterator (node, iter))
|
|
|
|
* {
|
|
|
|
* do_stuff();
|
|
|
|
* }
|
2014-12-08 23:35:55 +00:00
|
|
|
*/
|
2020-01-28 02:53:48 +00:00
|
|
|
GtkCssNode * (* iterator) (const GtkCssSelector *selector,
|
|
|
|
GtkCssNode *node,
|
|
|
|
GtkCssNode *current);
|
|
|
|
gboolean (* match_one) (const GtkCssSelector *selector,
|
|
|
|
GtkCssNode *node);
|
|
|
|
GtkCssChange (* get_change) (const GtkCssSelector *selector,
|
|
|
|
GtkCssChange previous_change);
|
|
|
|
void (* add_specificity) (const GtkCssSelector *selector,
|
|
|
|
guint *ids,
|
|
|
|
guint *classes,
|
|
|
|
guint *elements);
|
|
|
|
guint (* hash_one) (const GtkCssSelector *selector);
|
|
|
|
int (* compare_one) (const GtkCssSelector *a,
|
|
|
|
const GtkCssSelector *b);
|
2012-02-16 14:16:18 +00:00
|
|
|
};
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2014-12-06 13:01:02 +00:00
|
|
|
typedef enum {
|
|
|
|
POSITION_FORWARD,
|
|
|
|
POSITION_BACKWARD,
|
|
|
|
POSITION_ONLY,
|
|
|
|
} PositionType;
|
2014-12-11 11:45:20 +00:00
|
|
|
#define POSITION_TYPE_BITS 4
|
2014-12-06 13:01:02 +00:00
|
|
|
#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;
|
2020-01-23 23:43:26 +00:00
|
|
|
GQuark name;
|
2014-12-08 02:06:21 +00:00
|
|
|
} id;
|
2014-12-06 13:01:02 +00:00
|
|
|
struct {
|
|
|
|
const GtkCssSelectorClass *class;
|
|
|
|
GQuark style_class;
|
|
|
|
} style_class;
|
|
|
|
struct {
|
|
|
|
const GtkCssSelectorClass *class;
|
2020-01-23 23:43:26 +00:00
|
|
|
GQuark name;
|
2014-12-06 13:01:02 +00:00
|
|
|
} 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;
|
2012-02-16 14:16:18 +00:00
|
|
|
};
|
|
|
|
|
2012-11-29 19:52:46 +00:00
|
|
|
#define GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET G_MAXINT32
|
2012-11-28 10:15:53 +00:00
|
|
|
struct _GtkCssSelectorTree
|
|
|
|
{
|
|
|
|
GtkCssSelector selector;
|
2012-11-29 19:52:46 +00:00
|
|
|
gint32 parent_offset;
|
|
|
|
gint32 previous_offset;
|
|
|
|
gint32 sibling_offset;
|
|
|
|
gint32 matches_offset; /* pointers that we return as matches if selector matches */
|
2012-11-28 10:15:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_css_selector_equal (const GtkCssSelector *a,
|
|
|
|
const GtkCssSelector *b)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
a->class == b->class &&
|
2014-12-06 00:00:10 +00:00
|
|
|
a->class->compare_one (a, b) == 0;
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
2014-12-06 00:00:10 +00:00
|
|
|
gtk_css_selector_hash_one (const GtkCssSelector *selector)
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
2020-01-24 16:56:53 +00:00
|
|
|
return selector->class->hash_one (selector);
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 20:48:17 +00:00
|
|
|
static inline gpointer *
|
2012-11-29 19:52:46 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:46:48 +00:00
|
|
|
static void
|
2020-07-02 16:30:55 +00:00
|
|
|
gtk_css_selector_matches_insert_sorted (GtkCssSelectorMatches *matches,
|
|
|
|
gpointer data)
|
2015-09-09 14:46:48 +00:00
|
|
|
{
|
2020-04-29 08:46:53 +00:00
|
|
|
guint i;
|
2015-09-09 14:46:48 +00:00
|
|
|
|
2020-07-02 16:30:55 +00:00
|
|
|
for (i = 0; i < gtk_css_selector_matches_get_size (matches); i++)
|
2015-09-09 14:46:48 +00:00
|
|
|
{
|
2020-07-02 16:30:55 +00:00
|
|
|
gpointer elem = gtk_css_selector_matches_get (matches, i);
|
2020-04-29 08:46:53 +00:00
|
|
|
|
|
|
|
if (data == elem)
|
2015-09-09 14:46:48 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-29 08:46:53 +00:00
|
|
|
if (data < elem)
|
2015-09-09 14:46:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:30:55 +00:00
|
|
|
gtk_css_selector_matches_splice (matches, i, 0, (gpointer[1]) { data }, 1);
|
2015-09-09 14:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 20:48:17 +00:00
|
|
|
static inline gboolean
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_match_one (const GtkCssSelector *selector,
|
|
|
|
GtkCssNode *node)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2020-01-23 02:34:25 +00:00
|
|
|
return selector->class->match_one (selector, node);
|
2014-12-08 23:35:55 +00:00
|
|
|
}
|
2012-02-16 14:16:18 +00:00
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
static inline GtkCssNode *
|
|
|
|
gtk_css_selector_iterator (const GtkCssSelector *selector,
|
|
|
|
GtkCssNode *node,
|
|
|
|
GtkCssNode *current)
|
2014-12-08 23:35:55 +00:00
|
|
|
{
|
2020-01-28 02:53:48 +00:00
|
|
|
return selector->class->iterator (selector, node, current);
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 15:20:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-03-01 16:40:19 +00:00
|
|
|
static const GtkCssSelector *
|
|
|
|
gtk_css_selector_previous (const GtkCssSelector *selector)
|
|
|
|
{
|
2012-03-01 17:33:33 +00:00
|
|
|
selector = selector + 1;
|
|
|
|
|
|
|
|
return selector->class ? selector : NULL;
|
2012-03-01 16:40:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 20:48:17 +00:00
|
|
|
static inline const GtkCssSelectorTree *
|
2012-11-29 19:52:46 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-19 20:48:17 +00:00
|
|
|
static inline const GtkCssSelectorTree *
|
2012-11-29 16:56:00 +00:00
|
|
|
gtk_css_selector_tree_get_parent (const GtkCssSelectorTree *tree)
|
|
|
|
{
|
2012-11-29 19:52:46 +00:00
|
|
|
return gtk_css_selector_tree_at_offset (tree, tree->parent_offset);
|
2012-11-29 16:56:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 20:48:17 +00:00
|
|
|
static inline const GtkCssSelectorTree *
|
2012-11-29 16:56:00 +00:00
|
|
|
gtk_css_selector_tree_get_previous (const GtkCssSelectorTree *tree)
|
|
|
|
{
|
2012-11-29 19:52:46 +00:00
|
|
|
return gtk_css_selector_tree_at_offset (tree, tree->previous_offset);
|
2012-11-29 16:56:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 20:48:17 +00:00
|
|
|
static inline const GtkCssSelectorTree *
|
2012-11-29 16:56:00 +00:00
|
|
|
gtk_css_selector_tree_get_sibling (const GtkCssSelectorTree *tree)
|
|
|
|
{
|
2012-11-29 19:52:46 +00:00
|
|
|
return gtk_css_selector_tree_at_offset (tree, tree->sibling_offset);
|
2012-11-29 16:56:00 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 05:46:38 +00:00
|
|
|
/* DEFAULTS */
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_selector_default_add_specificity (const GtkCssSelector *selector,
|
|
|
|
guint *ids,
|
|
|
|
guint *classes,
|
|
|
|
guint *elements)
|
|
|
|
{
|
|
|
|
/* no specificity changes */
|
|
|
|
}
|
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
static GtkCssNode *
|
|
|
|
gtk_css_selector_default_iterator (const GtkCssSelector *selector,
|
|
|
|
GtkCssNode *node,
|
|
|
|
GtkCssNode *current)
|
2014-12-08 17:48:24 +00:00
|
|
|
{
|
2020-01-28 02:53:48 +00:00
|
|
|
if (current)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return node;
|
2014-12-08 17:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-12-08 23:35:55 +00:00
|
|
|
gtk_css_selector_default_match_one (const GtkCssSelector *selector,
|
2020-01-23 02:34:25 +00:00
|
|
|
GtkCssNode *node)
|
2014-12-08 17:48:24 +00:00
|
|
|
{
|
2014-12-08 23:35:55 +00:00
|
|
|
return TRUE;
|
2014-12-08 17:48:24 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 05:56:26 +00:00
|
|
|
static guint
|
|
|
|
gtk_css_selector_default_hash_one (const GtkCssSelector *selector)
|
|
|
|
{
|
2020-01-24 16:56:53 +00:00
|
|
|
return GPOINTER_TO_UINT (selector->class);
|
2014-12-08 05:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gtk_css_selector_default_compare_one (const GtkCssSelector *a,
|
|
|
|
const GtkCssSelector *b)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:16:18 +00:00
|
|
|
/* DESCENDANT */
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_selector_descendant_print (const GtkCssSelector *selector,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
g_string_append_c (string, ' ');
|
|
|
|
}
|
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
static GtkCssNode *
|
|
|
|
gtk_css_selector_descendant_iterator (const GtkCssSelector *selector,
|
|
|
|
GtkCssNode *node,
|
|
|
|
GtkCssNode *current)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2020-01-28 02:53:48 +00:00
|
|
|
return gtk_css_node_get_parent (current ? current : node);
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 18:23:53 +00:00
|
|
|
static GtkCssChange
|
2012-11-29 15:43:02 +00:00
|
|
|
gtk_css_selector_descendant_get_change (const GtkCssSelector *selector, GtkCssChange previous_change)
|
2012-03-17 18:23:53 +00:00
|
|
|
{
|
2012-11-29 15:43:02 +00:00
|
|
|
return _gtk_css_change_for_child (previous_change);
|
2012-03-17 18:23:53 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 14:16:18 +00:00
|
|
|
static const GtkCssSelectorClass GTK_CSS_SELECTOR_DESCENDANT = {
|
|
|
|
"descendant",
|
2020-01-21 15:51:55 +00:00
|
|
|
GTK_CSS_SELECTOR_CATEGORY_PARENT,
|
2012-02-16 14:16:18 +00:00
|
|
|
gtk_css_selector_descendant_print,
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_descendant_iterator,
|
2014-12-08 17:48:24 +00:00
|
|
|
gtk_css_selector_default_match_one,
|
2012-03-17 18:23:53 +00:00
|
|
|
gtk_css_selector_descendant_get_change,
|
2014-12-08 05:46:38 +00:00
|
|
|
gtk_css_selector_default_add_specificity,
|
2014-12-08 05:56:26 +00:00
|
|
|
gtk_css_selector_default_hash_one,
|
|
|
|
gtk_css_selector_default_compare_one,
|
2012-02-16 14:16:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* CHILD */
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_selector_child_print (const GtkCssSelector *selector,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
g_string_append (string, " > ");
|
|
|
|
}
|
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
static GtkCssNode *
|
|
|
|
gtk_css_selector_child_iterator (const GtkCssSelector *selector,
|
|
|
|
GtkCssNode *node,
|
|
|
|
GtkCssNode *current)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2020-01-28 02:53:48 +00:00
|
|
|
if (current == NULL)
|
|
|
|
return gtk_css_node_get_parent (node);
|
2012-03-17 01:17:23 +00:00
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
return NULL;
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 18:23:53 +00:00
|
|
|
static GtkCssChange
|
2012-11-29 15:43:02 +00:00
|
|
|
gtk_css_selector_child_get_change (const GtkCssSelector *selector, GtkCssChange previous_change)
|
2012-03-17 18:23:53 +00:00
|
|
|
{
|
2012-11-29 15:43:02 +00:00
|
|
|
return _gtk_css_change_for_child (previous_change);
|
2012-03-17 18:23:53 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 14:16:18 +00:00
|
|
|
static const GtkCssSelectorClass GTK_CSS_SELECTOR_CHILD = {
|
|
|
|
"child",
|
2020-01-21 15:51:55 +00:00
|
|
|
GTK_CSS_SELECTOR_CATEGORY_PARENT,
|
2012-02-16 14:16:18 +00:00
|
|
|
gtk_css_selector_child_print,
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_child_iterator,
|
2014-12-08 17:48:24 +00:00
|
|
|
gtk_css_selector_default_match_one,
|
2012-03-17 18:23:53 +00:00
|
|
|
gtk_css_selector_child_get_change,
|
2014-12-08 05:46:38 +00:00
|
|
|
gtk_css_selector_default_add_specificity,
|
2014-12-08 05:56:26 +00:00
|
|
|
gtk_css_selector_default_hash_one,
|
|
|
|
gtk_css_selector_default_compare_one,
|
2012-02-16 14:16:18 +00:00
|
|
|
};
|
|
|
|
|
2012-03-02 01:04:15 +00:00
|
|
|
/* SIBLING */
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_selector_sibling_print (const GtkCssSelector *selector,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
g_string_append (string, " ~ ");
|
|
|
|
}
|
|
|
|
|
2020-01-23 02:34:25 +00:00
|
|
|
static GtkCssNode *
|
|
|
|
get_previous_visible_sibling (GtkCssNode *node)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
node = gtk_css_node_get_previous_sibling (node);
|
|
|
|
} while (node && !gtk_css_node_get_visible (node));
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkCssNode *
|
|
|
|
get_next_visible_sibling (GtkCssNode *node)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
node = gtk_css_node_get_next_sibling (node);
|
|
|
|
} while (node && !gtk_css_node_get_visible (node));
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
static GtkCssNode *
|
|
|
|
gtk_css_selector_sibling_iterator (const GtkCssSelector *selector,
|
|
|
|
GtkCssNode *node,
|
|
|
|
GtkCssNode *current)
|
2012-03-02 01:04:15 +00:00
|
|
|
{
|
2020-01-28 02:53:48 +00:00
|
|
|
return get_previous_visible_sibling (current ? current : node);
|
2012-03-02 01:04:15 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 18:23:53 +00:00
|
|
|
static GtkCssChange
|
2012-11-29 15:43:02 +00:00
|
|
|
gtk_css_selector_sibling_get_change (const GtkCssSelector *selector, GtkCssChange previous_change)
|
2012-03-17 18:23:53 +00:00
|
|
|
{
|
2012-11-29 15:43:02 +00:00
|
|
|
return _gtk_css_change_for_sibling (previous_change);
|
2012-03-17 18:23:53 +00:00
|
|
|
}
|
|
|
|
|
2012-03-02 01:04:15 +00:00
|
|
|
static const GtkCssSelectorClass GTK_CSS_SELECTOR_SIBLING = {
|
|
|
|
"sibling",
|
2020-01-21 15:51:55 +00:00
|
|
|
GTK_CSS_SELECTOR_CATEGORY_SIBLING,
|
2012-03-02 01:04:15 +00:00
|
|
|
gtk_css_selector_sibling_print,
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_sibling_iterator,
|
2014-12-08 17:48:24 +00:00
|
|
|
gtk_css_selector_default_match_one,
|
2012-03-17 18:23:53 +00:00
|
|
|
gtk_css_selector_sibling_get_change,
|
2014-12-08 05:46:38 +00:00
|
|
|
gtk_css_selector_default_add_specificity,
|
2014-12-08 05:56:26 +00:00
|
|
|
gtk_css_selector_default_hash_one,
|
|
|
|
gtk_css_selector_default_compare_one,
|
2012-03-02 01:04:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ADJACENT */
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_selector_adjacent_print (const GtkCssSelector *selector,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
g_string_append (string, " + ");
|
|
|
|
}
|
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
static GtkCssNode *
|
|
|
|
gtk_css_selector_adjacent_iterator (const GtkCssSelector *selector,
|
|
|
|
GtkCssNode *node,
|
|
|
|
GtkCssNode *current)
|
2012-03-02 01:04:15 +00:00
|
|
|
{
|
2020-01-28 02:53:48 +00:00
|
|
|
if (current == NULL)
|
|
|
|
return get_previous_visible_sibling (node);
|
2012-03-17 01:17:23 +00:00
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
return NULL;
|
2012-03-02 01:04:15 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 18:23:53 +00:00
|
|
|
static GtkCssChange
|
2012-11-29 15:43:02 +00:00
|
|
|
gtk_css_selector_adjacent_get_change (const GtkCssSelector *selector, GtkCssChange previous_change)
|
2012-03-17 18:23:53 +00:00
|
|
|
{
|
2012-11-29 15:43:02 +00:00
|
|
|
return _gtk_css_change_for_sibling (previous_change);
|
2012-03-17 18:23:53 +00:00
|
|
|
}
|
|
|
|
|
2012-03-02 01:04:15 +00:00
|
|
|
static const GtkCssSelectorClass GTK_CSS_SELECTOR_ADJACENT = {
|
|
|
|
"adjacent",
|
2020-01-21 15:51:55 +00:00
|
|
|
GTK_CSS_SELECTOR_CATEGORY_SIBLING,
|
2012-03-02 01:04:15 +00:00
|
|
|
gtk_css_selector_adjacent_print,
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_adjacent_iterator,
|
2014-12-08 17:48:24 +00:00
|
|
|
gtk_css_selector_default_match_one,
|
2012-03-17 18:23:53 +00:00
|
|
|
gtk_css_selector_adjacent_get_change,
|
2014-12-08 05:46:38 +00:00
|
|
|
gtk_css_selector_default_add_specificity,
|
2014-12-08 05:56:26 +00:00
|
|
|
gtk_css_selector_default_hash_one,
|
|
|
|
gtk_css_selector_default_compare_one,
|
2012-03-02 01:04:15 +00:00
|
|
|
};
|
|
|
|
|
2014-11-25 17:51:13 +00:00
|
|
|
/* SIMPLE SELECTOR DEFINE */
|
|
|
|
|
|
|
|
#define DEFINE_SIMPLE_SELECTOR(n, \
|
|
|
|
c, \
|
|
|
|
print_func, \
|
|
|
|
match_func, \
|
2014-12-06 00:00:10 +00:00
|
|
|
hash_func, \
|
2014-11-25 17:51:13 +00:00
|
|
|
comp_func, \
|
|
|
|
increase_id_specificity, \
|
|
|
|
increase_class_specificity, \
|
2020-01-17 16:10:14 +00:00
|
|
|
increase_element_specificity, \
|
|
|
|
ignore_for_change) \
|
2014-11-25 17:51:13 +00:00
|
|
|
static void \
|
|
|
|
gtk_css_selector_ ## n ## _print (const GtkCssSelector *selector, \
|
|
|
|
GString *string) \
|
|
|
|
{ \
|
|
|
|
print_func (selector, string); \
|
|
|
|
} \
|
|
|
|
\
|
2014-11-25 18:15:36 +00:00
|
|
|
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, ")"); \
|
|
|
|
} \
|
|
|
|
\
|
2014-11-25 17:51:13 +00:00
|
|
|
static gboolean \
|
2014-12-08 17:48:24 +00:00
|
|
|
gtk_css_selector_not_ ## n ## _match_one (const GtkCssSelector *selector, \
|
2020-01-23 02:34:25 +00:00
|
|
|
GtkCssNode *node) \
|
2014-11-25 18:15:36 +00:00
|
|
|
{ \
|
2020-01-23 02:34:25 +00:00
|
|
|
return !match_func (selector, node); \
|
2014-11-25 18:15:36 +00:00
|
|
|
} \
|
|
|
|
\
|
2014-11-25 17:51:13 +00:00
|
|
|
static GtkCssChange \
|
|
|
|
gtk_css_selector_ ## n ## _get_change (const GtkCssSelector *selector, GtkCssChange previous_change) \
|
|
|
|
{ \
|
|
|
|
return previous_change | GTK_CSS_CHANGE_ ## c; \
|
|
|
|
} \
|
|
|
|
\
|
2014-12-08 05:46:38 +00:00
|
|
|
static void \
|
|
|
|
gtk_css_selector_ ## n ## _add_specificity (const GtkCssSelector *selector, \
|
|
|
|
guint *ids, \
|
|
|
|
guint *classes, \
|
|
|
|
guint *elements) \
|
|
|
|
{ \
|
|
|
|
if (increase_id_specificity) \
|
2016-08-25 21:01:28 +00:00
|
|
|
{ \
|
|
|
|
(*ids)++; \
|
|
|
|
} \
|
2014-12-08 05:46:38 +00:00
|
|
|
if (increase_class_specificity) \
|
2016-08-25 21:01:28 +00:00
|
|
|
{ \
|
|
|
|
(*classes)++; \
|
|
|
|
} \
|
2014-12-08 05:46:38 +00:00
|
|
|
if (increase_element_specificity) \
|
2016-08-25 21:01:28 +00:00
|
|
|
{ \
|
|
|
|
(*elements)++; \
|
|
|
|
} \
|
2014-12-08 05:46:38 +00:00
|
|
|
} \
|
|
|
|
\
|
2014-11-25 17:51:13 +00:00
|
|
|
static const GtkCssSelectorClass GTK_CSS_SELECTOR_ ## c = { \
|
|
|
|
G_STRINGIFY(n), \
|
2020-01-21 15:51:55 +00:00
|
|
|
ignore_for_change ? GTK_CSS_SELECTOR_CATEGORY_SIMPLE : GTK_CSS_SELECTOR_CATEGORY_SIMPLE_RADICAL, \
|
2014-11-25 17:51:13 +00:00
|
|
|
gtk_css_selector_ ## n ## _print, \
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_default_iterator, \
|
2014-12-08 17:48:24 +00:00
|
|
|
match_func, \
|
2014-11-25 17:51:13 +00:00
|
|
|
gtk_css_selector_ ## n ## _get_change, \
|
2014-12-08 05:46:38 +00:00
|
|
|
gtk_css_selector_ ## n ## _add_specificity, \
|
2014-12-06 00:00:10 +00:00
|
|
|
hash_func, \
|
2014-11-25 17:51:13 +00:00
|
|
|
comp_func, \
|
2014-11-25 18:15:36 +00:00
|
|
|
};\
|
|
|
|
\
|
|
|
|
static const GtkCssSelectorClass GTK_CSS_SELECTOR_NOT_ ## c = { \
|
|
|
|
"not_" G_STRINGIFY(n), \
|
2020-01-27 01:49:36 +00:00
|
|
|
GTK_CSS_SELECTOR_CATEGORY_SIMPLE, \
|
2014-11-25 18:15:36 +00:00
|
|
|
gtk_css_selector_not_ ## n ## _print, \
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_default_iterator, \
|
2014-12-08 17:48:24 +00:00
|
|
|
gtk_css_selector_not_ ## n ## _match_one, \
|
2014-11-25 18:15:36 +00:00
|
|
|
gtk_css_selector_ ## n ## _get_change, \
|
2014-12-08 05:46:38 +00:00
|
|
|
gtk_css_selector_ ## n ## _add_specificity, \
|
2014-12-06 00:00:10 +00:00
|
|
|
hash_func, \
|
2014-11-25 18:15:36 +00:00
|
|
|
comp_func, \
|
2014-11-25 17:51:13 +00:00
|
|
|
};
|
|
|
|
|
2012-03-07 02:47:19 +00:00
|
|
|
/* ANY */
|
|
|
|
|
|
|
|
static void
|
2014-12-08 05:08:30 +00:00
|
|
|
print_any (const GtkCssSelector *selector,
|
|
|
|
GString *string)
|
2012-03-07 02:47:19 +00:00
|
|
|
{
|
|
|
|
g_string_append_c (string, '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-12-08 05:08:30 +00:00
|
|
|
match_any (const GtkCssSelector *selector,
|
2020-01-23 02:34:25 +00:00
|
|
|
GtkCssNode *node)
|
2014-11-25 18:31:23 +00:00
|
|
|
{
|
2014-12-08 05:08:30 +00:00
|
|
|
return TRUE;
|
2014-11-25 18:31:23 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 05:08:30 +00:00
|
|
|
#undef GTK_CSS_CHANGE_ANY
|
|
|
|
#define GTK_CSS_CHANGE_ANY 0
|
2014-12-08 05:56:26 +00:00
|
|
|
DEFINE_SIMPLE_SELECTOR(any, ANY, print_any, match_any,
|
|
|
|
gtk_css_selector_default_hash_one, gtk_css_selector_default_compare_one,
|
2020-01-17 16:10:14 +00:00
|
|
|
FALSE, FALSE, FALSE, TRUE)
|
2014-12-08 05:08:30 +00:00
|
|
|
#undef GTK_CSS_CHANGE_ANY
|
2014-11-25 18:31:23 +00:00
|
|
|
|
2012-02-16 14:16:18 +00:00
|
|
|
/* NAME */
|
|
|
|
|
|
|
|
static void
|
2014-11-25 17:51:13 +00:00
|
|
|
print_name (const GtkCssSelector *selector,
|
|
|
|
GString *string)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2020-01-23 23:43:26 +00:00
|
|
|
g_string_append (string, g_quark_to_string (selector->name.name));
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-11-25 17:51:13 +00:00
|
|
|
match_name (const GtkCssSelector *selector,
|
2020-01-23 02:34:25 +00:00
|
|
|
GtkCssNode *node)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2020-01-23 02:34:25 +00:00
|
|
|
return gtk_css_node_get_name (node) == selector->name.name;
|
2012-03-17 18:23:53 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:00:10 +00:00
|
|
|
static guint
|
|
|
|
hash_name (const GtkCssSelector *a)
|
|
|
|
{
|
2020-01-24 16:56:53 +00:00
|
|
|
return gtk_css_hash_name (a->name.name);
|
2014-12-06 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 15:20:14 +00:00
|
|
|
static int
|
2014-11-25 17:51:13 +00:00
|
|
|
comp_name (const GtkCssSelector *a,
|
|
|
|
const GtkCssSelector *b)
|
2012-11-29 15:20:14 +00:00
|
|
|
{
|
2020-01-23 23:43:26 +00:00
|
|
|
return a->name.name - b->name.name;
|
2012-11-29 15:20:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 16:10:14 +00:00
|
|
|
DEFINE_SIMPLE_SELECTOR(name, NAME, print_name, match_name, hash_name, comp_name, FALSE, FALSE, TRUE, FALSE)
|
2012-02-16 14:16:18 +00:00
|
|
|
|
|
|
|
/* CLASS */
|
|
|
|
|
|
|
|
static void
|
2014-11-25 17:51:13 +00:00
|
|
|
print_class (const GtkCssSelector *selector,
|
|
|
|
GString *string)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
|
|
|
g_string_append_c (string, '.');
|
2014-12-06 13:01:02 +00:00
|
|
|
g_string_append (string, g_quark_to_string (selector->style_class.style_class));
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-11-25 17:51:13 +00:00
|
|
|
match_class (const GtkCssSelector *selector,
|
2020-01-23 02:34:25 +00:00
|
|
|
GtkCssNode *node)
|
2012-03-17 18:23:53 +00:00
|
|
|
{
|
2020-01-23 02:34:25 +00:00
|
|
|
return gtk_css_node_has_class (node, selector->style_class.style_class);
|
2012-03-17 18:23:53 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:00:10 +00:00
|
|
|
static guint
|
|
|
|
hash_class (const GtkCssSelector *a)
|
|
|
|
{
|
2020-01-24 16:56:53 +00:00
|
|
|
return gtk_css_hash_class (a->style_class.style_class);
|
2014-12-06 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 15:20:14 +00:00
|
|
|
static int
|
2014-11-25 17:51:13 +00:00
|
|
|
comp_class (const GtkCssSelector *a,
|
|
|
|
const GtkCssSelector *b)
|
2012-11-29 15:20:14 +00:00
|
|
|
{
|
2015-09-11 00:42:58 +00:00
|
|
|
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;
|
2012-11-29 15:20:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 16:10:14 +00:00
|
|
|
DEFINE_SIMPLE_SELECTOR(class, CLASS, print_class, match_class, hash_class, comp_class, FALSE, TRUE, FALSE, FALSE)
|
2012-02-16 14:16:18 +00:00
|
|
|
|
|
|
|
/* ID */
|
|
|
|
|
|
|
|
static void
|
2014-11-25 17:51:13 +00:00
|
|
|
print_id (const GtkCssSelector *selector,
|
|
|
|
GString *string)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
|
|
|
g_string_append_c (string, '#');
|
2020-01-23 23:43:26 +00:00
|
|
|
g_string_append (string, g_quark_to_string (selector->id.name));
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-11-25 17:51:13 +00:00
|
|
|
match_id (const GtkCssSelector *selector,
|
2020-01-23 02:34:25 +00:00
|
|
|
GtkCssNode *node)
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
2020-01-23 02:34:25 +00:00
|
|
|
return gtk_css_node_get_id (node) == selector->id.name;
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:00:10 +00:00
|
|
|
static guint
|
|
|
|
hash_id (const GtkCssSelector *a)
|
|
|
|
{
|
2020-01-24 16:56:53 +00:00
|
|
|
return gtk_css_hash_id (a->id.name);
|
2014-12-06 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 15:20:14 +00:00
|
|
|
static int
|
2014-11-25 17:51:13 +00:00
|
|
|
comp_id (const GtkCssSelector *a,
|
|
|
|
const GtkCssSelector *b)
|
2012-11-29 15:20:14 +00:00
|
|
|
{
|
2020-01-23 23:43:26 +00:00
|
|
|
return a->id.name - b->id.name;
|
2012-11-29 15:20:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 16:10:14 +00:00
|
|
|
DEFINE_SIMPLE_SELECTOR(id, ID, print_id, match_id, hash_id, comp_id, TRUE, FALSE, FALSE, FALSE)
|
2012-02-16 14:16:18 +00:00
|
|
|
|
2016-01-24 15:56:23 +00:00
|
|
|
/* 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));
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-11-25 17:51:13 +00:00
|
|
|
match_pseudoclass_state (const GtkCssSelector *selector,
|
2020-01-23 02:34:25 +00:00
|
|
|
GtkCssNode *node)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2020-01-23 02:34:25 +00:00
|
|
|
return (gtk_css_node_get_state (node) & selector->state.state) == selector->state.state;
|
2012-03-17 18:23:53 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:00:10 +00:00
|
|
|
static guint
|
2014-12-06 13:01:02 +00:00
|
|
|
hash_pseudoclass_state (const GtkCssSelector *selector)
|
2014-12-06 00:00:10 +00:00
|
|
|
{
|
2014-12-06 13:01:02 +00:00
|
|
|
return selector->state.state;
|
2014-12-06 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-29 15:20:14 +00:00
|
|
|
static int
|
2014-11-25 17:51:13 +00:00
|
|
|
comp_pseudoclass_state (const GtkCssSelector *a,
|
|
|
|
const GtkCssSelector *b)
|
2012-11-29 15:20:14 +00:00
|
|
|
{
|
2014-12-06 13:01:02 +00:00
|
|
|
return a->state.state - b->state.state;
|
2012-11-29 15:20:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 23:19:23 +00:00
|
|
|
static GtkCssChange
|
|
|
|
change_pseudoclass_state (const GtkCssSelector *selector)
|
|
|
|
{
|
|
|
|
GtkStateFlags states = selector->state.state;
|
|
|
|
GtkCssChange change = 0;
|
2020-01-15 20:10:06 +00:00
|
|
|
|
2020-01-18 23:19:23 +00:00
|
|
|
if (states & GTK_STATE_FLAG_PRELIGHT)
|
|
|
|
change |= GTK_CSS_CHANGE_HOVER;
|
|
|
|
if (states & GTK_STATE_FLAG_INSENSITIVE)
|
|
|
|
change |= GTK_CSS_CHANGE_DISABLED;
|
|
|
|
if (states & GTK_STATE_FLAG_BACKDROP)
|
|
|
|
change |= GTK_CSS_CHANGE_BACKDROP;
|
|
|
|
if (states & GTK_STATE_FLAG_SELECTED)
|
|
|
|
change |= GTK_CSS_CHANGE_SELECTED;
|
|
|
|
if (states & ~(GTK_STATE_FLAG_PRELIGHT |
|
|
|
|
GTK_STATE_FLAG_INSENSITIVE |
|
|
|
|
GTK_STATE_FLAG_BACKDROP |
|
|
|
|
GTK_STATE_FLAG_SELECTED))
|
|
|
|
change |= GTK_CSS_CHANGE_STATE;
|
2020-01-16 00:19:38 +00:00
|
|
|
|
2020-01-18 23:19:23 +00:00
|
|
|
return change;
|
|
|
|
}
|
2020-01-16 01:27:12 +00:00
|
|
|
|
2020-01-18 23:19:23 +00:00
|
|
|
#define GTK_CSS_CHANGE_PSEUDOCLASS_STATE change_pseudoclass_state (selector)
|
2014-12-06 00:00:10 +00:00
|
|
|
DEFINE_SIMPLE_SELECTOR(pseudoclass_state, PSEUDOCLASS_STATE, print_pseudoclass_state,
|
|
|
|
match_pseudoclass_state, hash_pseudoclass_state, comp_pseudoclass_state,
|
2020-01-17 16:10:14 +00:00
|
|
|
FALSE, TRUE, FALSE, TRUE)
|
2014-11-25 17:51:13 +00:00
|
|
|
#undef GTK_CSS_CHANGE_PSEUDOCLASS_STATE
|
2011-05-14 11:27:31 +00:00
|
|
|
|
2012-03-17 20:02:43 +00:00
|
|
|
/* PSEUDOCLASS FOR POSITION */
|
2012-02-16 14:16:18 +00:00
|
|
|
|
|
|
|
static void
|
2014-12-08 04:39:01 +00:00
|
|
|
print_pseudoclass_position (const GtkCssSelector *selector,
|
|
|
|
GString *string)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2014-12-06 13:01:02 +00:00
|
|
|
switch (selector->position.type)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2012-03-18 01:11:44 +00:00
|
|
|
case POSITION_FORWARD:
|
2014-12-06 13:01:02 +00:00
|
|
|
if (selector->position.a == 0)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2014-12-06 13:01:02 +00:00
|
|
|
if (selector->position.b == 1)
|
2012-03-18 01:11:44 +00:00
|
|
|
g_string_append (string, ":first-child");
|
|
|
|
else
|
2014-12-06 13:01:02 +00:00
|
|
|
g_string_append_printf (string, ":nth-child(%d)", selector->position.b);
|
2012-03-18 01:11:44 +00:00
|
|
|
}
|
2014-12-06 13:01:02 +00:00
|
|
|
else if (selector->position.a == 2 && selector->position.b == 0)
|
2012-03-18 01:11:44 +00:00
|
|
|
g_string_append (string, ":nth-child(even)");
|
2014-12-06 13:01:02 +00:00
|
|
|
else if (selector->position.a == 2 && selector->position.b == 1)
|
2012-03-18 01:11:44 +00:00
|
|
|
g_string_append (string, ":nth-child(odd)");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_string_append (string, ":nth-child(");
|
2014-12-06 13:01:02 +00:00
|
|
|
if (selector->position.a == 1)
|
2012-03-18 01:11:44 +00:00
|
|
|
g_string_append (string, "n");
|
2014-12-06 13:01:02 +00:00
|
|
|
else if (selector->position.a == -1)
|
2012-03-18 01:11:44 +00:00
|
|
|
g_string_append (string, "-n");
|
|
|
|
else
|
2014-12-06 13:01:02 +00:00
|
|
|
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);
|
2012-03-18 01:11:44 +00:00
|
|
|
else
|
|
|
|
g_string_append (string, ")");
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
2012-03-18 01:11:44 +00:00
|
|
|
break;
|
|
|
|
case POSITION_BACKWARD:
|
2014-12-06 13:01:02 +00:00
|
|
|
if (selector->position.a == 0)
|
2012-03-18 01:11:44 +00:00
|
|
|
{
|
2014-12-06 13:01:02 +00:00
|
|
|
if (selector->position.b == 1)
|
2012-03-18 01:11:44 +00:00
|
|
|
g_string_append (string, ":last-child");
|
|
|
|
else
|
2014-12-06 13:01:02 +00:00
|
|
|
g_string_append_printf (string, ":nth-last-child(%d)", selector->position.b);
|
2012-03-18 01:11:44 +00:00
|
|
|
}
|
2014-12-06 13:01:02 +00:00
|
|
|
else if (selector->position.a == 2 && selector->position.b == 0)
|
2012-03-18 01:11:44 +00:00
|
|
|
g_string_append (string, ":nth-last-child(even)");
|
2014-12-06 13:01:02 +00:00
|
|
|
else if (selector->position.a == 2 && selector->position.b == 1)
|
2012-03-18 01:11:44 +00:00
|
|
|
g_string_append (string, ":nth-last-child(odd)");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_string_append (string, ":nth-last-child(");
|
2014-12-06 13:01:02 +00:00
|
|
|
if (selector->position.a == 1)
|
2012-03-18 01:11:44 +00:00
|
|
|
g_string_append (string, "n");
|
2014-12-06 13:01:02 +00:00
|
|
|
else if (selector->position.a == -1)
|
2012-03-18 01:11:44 +00:00
|
|
|
g_string_append (string, "-n");
|
|
|
|
else
|
2014-12-06 13:01:02 +00:00
|
|
|
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);
|
2012-03-18 01:11:44 +00:00
|
|
|
else
|
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case POSITION_ONLY:
|
|
|
|
g_string_append (string, ":only-child");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 02:34:25 +00:00
|
|
|
static gboolean
|
|
|
|
match_position (GtkCssNode *node,
|
|
|
|
GtkCssNode *(* prev_node_func) (GtkCssNode *),
|
|
|
|
int a,
|
|
|
|
int b)
|
|
|
|
{
|
|
|
|
int pos, x;
|
|
|
|
|
|
|
|
/* special-case the common "first-child" and "last-child" */
|
|
|
|
if (a == 0)
|
|
|
|
{
|
|
|
|
while (b > 0 && node != NULL)
|
|
|
|
{
|
|
|
|
b--;
|
|
|
|
node = prev_node_func (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return b == 0 && node == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* count nodes */
|
|
|
|
for (pos = 0; node != NULL; pos++)
|
|
|
|
node = prev_node_func (node);
|
|
|
|
|
|
|
|
/* solve pos = a * X + b
|
|
|
|
* and return TRUE if X is integer >= 0 */
|
|
|
|
x = pos - b;
|
|
|
|
|
|
|
|
if (x % a)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return x / a >= 0;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:16:18 +00:00
|
|
|
static gboolean
|
2014-12-08 04:39:01 +00:00
|
|
|
match_pseudoclass_position (const GtkCssSelector *selector,
|
2020-01-23 02:34:25 +00:00
|
|
|
GtkCssNode *node)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2014-12-06 13:01:02 +00:00
|
|
|
switch (selector->position.type)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2012-03-18 01:11:44 +00:00
|
|
|
case POSITION_FORWARD:
|
2020-01-23 02:34:25 +00:00
|
|
|
if (!match_position (node, get_previous_visible_sibling, selector->position.a, selector->position.b))
|
2012-02-16 14:16:18 +00:00
|
|
|
return FALSE;
|
|
|
|
break;
|
2012-03-18 01:11:44 +00:00
|
|
|
case POSITION_BACKWARD:
|
2020-01-23 02:34:25 +00:00
|
|
|
if (!match_position (node, get_next_visible_sibling, selector->position.a, selector->position.b))
|
2012-02-16 14:16:18 +00:00
|
|
|
return FALSE;
|
|
|
|
break;
|
2012-03-18 01:11:44 +00:00
|
|
|
case POSITION_ONLY:
|
2020-01-23 02:34:25 +00:00
|
|
|
if (get_previous_visible_sibling (node) ||
|
|
|
|
get_next_visible_sibling (node))
|
2012-02-16 14:16:18 +00:00
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-12-07 17:55:49 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-12-06 00:00:10 +00:00
|
|
|
static guint
|
2014-12-08 04:39:01 +00:00
|
|
|
hash_pseudoclass_position (const GtkCssSelector *a)
|
2014-12-06 00:00:10 +00:00
|
|
|
{
|
2016-02-26 19:48:21 +00:00
|
|
|
return (guint)(((((gulong)a->position.type) << POSITION_NUMBER_BITS) | a->position.a) << POSITION_NUMBER_BITS) | a->position.b;
|
2014-12-06 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 15:20:14 +00:00
|
|
|
static int
|
2014-12-08 04:39:01 +00:00
|
|
|
comp_pseudoclass_position (const GtkCssSelector *a,
|
|
|
|
const GtkCssSelector *b)
|
2012-11-29 15:20:14 +00:00
|
|
|
{
|
2014-12-06 13:01:02 +00:00
|
|
|
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;
|
2012-11-29 15:20:14 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 22:21:03 +00:00
|
|
|
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 ();
|
2017-10-24 16:13:45 +00:00
|
|
|
return 0;
|
2015-07-05 22:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GTK_CSS_CHANGE_PSEUDOCLASS_POSITION change_pseudoclass_position(selector)
|
2014-12-08 04:39:01 +00:00
|
|
|
DEFINE_SIMPLE_SELECTOR(pseudoclass_position, PSEUDOCLASS_POSITION, print_pseudoclass_position,
|
|
|
|
match_pseudoclass_position, hash_pseudoclass_position, comp_pseudoclass_position,
|
2020-01-17 16:10:14 +00:00
|
|
|
FALSE, TRUE, FALSE, TRUE)
|
2014-12-08 04:39:01 +00:00
|
|
|
#undef GTK_CSS_CHANGE_PSEUDOCLASS_POSITION
|
2012-02-16 14:16:18 +00:00
|
|
|
/* API */
|
|
|
|
|
2012-03-01 17:33:33 +00:00
|
|
|
static guint
|
|
|
|
gtk_css_selector_size (const GtkCssSelector *selector)
|
|
|
|
{
|
|
|
|
guint size = 0;
|
|
|
|
|
|
|
|
while (selector)
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_previous (selector);
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2012-02-15 14:42:00 +00:00
|
|
|
static GtkCssSelector *
|
2012-02-16 14:16:18 +00:00
|
|
|
gtk_css_selector_new (const GtkCssSelectorClass *class,
|
2014-12-06 13:01:02 +00:00
|
|
|
GtkCssSelector *selector)
|
2011-05-14 11:27:31 +00:00
|
|
|
{
|
2012-03-01 17:33:33 +00:00
|
|
|
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));
|
2011-05-14 11:27:31 +00:00
|
|
|
|
2014-12-06 13:01:02 +00:00
|
|
|
memset (selector, 0, sizeof (GtkCssSelector));
|
2012-02-16 14:16:18 +00:00
|
|
|
selector->class = class;
|
2011-05-14 11:27:31 +00:00
|
|
|
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:16:18 +00:00
|
|
|
static GtkCssSelector *
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_selector_parse_selector_class (GtkCssParser *parser,
|
|
|
|
GtkCssSelector *selector,
|
|
|
|
gboolean negate)
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
const GtkCssToken *token;
|
|
|
|
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
for (token = gtk_css_parser_peek_token (parser);
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_COMMENT);
|
|
|
|
token = gtk_css_parser_peek_token (parser))
|
|
|
|
{
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
if (gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (negate ? >K_CSS_SELECTOR_NOT_CLASS
|
|
|
|
: >K_CSS_SELECTOR_CLASS,
|
|
|
|
selector);
|
|
|
|
selector->style_class.style_class = g_quark_from_string (token->string.string);
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
else
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "No class name after '.' in selector");
|
2012-02-16 14:16:18 +00:00
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
return NULL;
|
2012-02-15 14:42:00 +00:00
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
}
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
static gboolean
|
|
|
|
string_has_number (const char *string,
|
|
|
|
const char *prefix,
|
|
|
|
int *number)
|
|
|
|
{
|
|
|
|
gsize len = strlen (prefix);
|
|
|
|
char *end;
|
2012-02-16 14:16:18 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
if (g_ascii_strncasecmp (string, prefix, len) != 0)
|
|
|
|
return FALSE;
|
2012-02-16 14:16:18 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
errno = 0;
|
|
|
|
*number = strtoul (string + len, &end, 10);
|
|
|
|
if (*end != '\0' || errno != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2012-02-15 14:42:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
static gboolean
|
|
|
|
parse_plus_b (GtkCssParser *parser,
|
|
|
|
gboolean negate,
|
|
|
|
gint *b)
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
const GtkCssToken *token;
|
|
|
|
gboolean has_seen_sign;
|
|
|
|
|
|
|
|
token = gtk_css_parser_get_token (parser);
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
if (negate)
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
has_seen_sign = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (gtk_css_token_is_delim (token, '+'))
|
|
|
|
{
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
has_seen_sign = TRUE;
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_delim (token, '-'))
|
|
|
|
{
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
negate = TRUE;
|
|
|
|
has_seen_sign = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
has_seen_sign = FALSE;
|
|
|
|
}
|
2012-02-15 14:42:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
token = gtk_css_parser_get_token (parser);
|
|
|
|
if (!has_seen_sign && gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_INTEGER))
|
|
|
|
{
|
|
|
|
*b = token->number.number;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (has_seen_sign && gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_INTEGER))
|
|
|
|
{
|
|
|
|
*b = token->number.number;
|
|
|
|
if (negate)
|
|
|
|
*b = - *b;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (!has_seen_sign)
|
|
|
|
{
|
|
|
|
*b = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_css_parser_error_syntax (parser, "Not a valid an+b type");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-02-16 14:16:18 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
static gboolean
|
|
|
|
parse_n_plus_b (GtkCssParser *parser,
|
|
|
|
gint before,
|
|
|
|
gint *a,
|
|
|
|
gint *b)
|
|
|
|
{
|
|
|
|
const GtkCssToken *token;
|
2012-02-16 14:16:18 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
token = gtk_css_parser_get_token (parser);
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
if (gtk_css_token_is_ident (token, "n"))
|
|
|
|
{
|
|
|
|
*a = before;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return parse_plus_b (parser, FALSE, b);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_ident (token, "n-"))
|
|
|
|
{
|
|
|
|
*a = before;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return parse_plus_b (parser, TRUE, b);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT) &&
|
|
|
|
string_has_number (token->string.string, "n-", b))
|
|
|
|
{
|
|
|
|
*a = before;
|
|
|
|
*b = -*b;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*b = before;
|
|
|
|
*a = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_css_parser_error_syntax (parser, "Not a valid an+b type");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
parse_a_n_plus_b (GtkCssParser *parser,
|
|
|
|
gint seen_sign,
|
|
|
|
gint *a,
|
|
|
|
gint *b)
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
const GtkCssToken *token;
|
|
|
|
|
|
|
|
token = gtk_css_parser_get_token (parser);
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
if (!seen_sign && gtk_css_token_is_ident (token, "even"))
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
*a = 2;
|
|
|
|
*b = 0;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (!seen_sign && gtk_css_token_is_ident (token, "odd"))
|
|
|
|
{
|
|
|
|
*a = 2;
|
|
|
|
*b = 1;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return TRUE;
|
2012-02-15 14:42:00 +00:00
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
else if (!seen_sign && gtk_css_token_is_delim (token, '+'))
|
|
|
|
{
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return parse_a_n_plus_b (parser, 1, a, b);
|
|
|
|
}
|
|
|
|
else if (!seen_sign && gtk_css_token_is_delim (token, '-'))
|
|
|
|
{
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return parse_a_n_plus_b (parser, -1, a, b);
|
|
|
|
}
|
|
|
|
else if ((!seen_sign && gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_INTEGER)) ||
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_INTEGER))
|
|
|
|
{
|
|
|
|
int x = token->number.number * (seen_sign ? seen_sign : 1);
|
|
|
|
gtk_css_parser_consume_token (parser);
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
return parse_n_plus_b (parser, x , a, b);
|
|
|
|
}
|
|
|
|
else if (((!seen_sign && gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_INTEGER_DIMENSION)) ||
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_INTEGER_DIMENSION)) &&
|
|
|
|
g_ascii_strcasecmp (token->dimension.dimension, "n") == 0)
|
|
|
|
{
|
|
|
|
*a = token->dimension.value * (seen_sign ? seen_sign : 1);
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return parse_plus_b (parser, FALSE, b);
|
|
|
|
}
|
|
|
|
else if (((!seen_sign && gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_INTEGER_DIMENSION)) ||
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_INTEGER_DIMENSION)) &&
|
|
|
|
g_ascii_strcasecmp (token->dimension.dimension, "n-") == 0)
|
|
|
|
{
|
|
|
|
*a = token->dimension.value * (seen_sign ? seen_sign : 1);
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return parse_plus_b (parser, TRUE, b);
|
|
|
|
}
|
|
|
|
else if (((!seen_sign && gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_INTEGER_DIMENSION)) ||
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_INTEGER_DIMENSION)) &&
|
|
|
|
string_has_number (token->dimension.dimension, "n-", b))
|
|
|
|
{
|
|
|
|
*a = token->dimension.value * (seen_sign ? seen_sign : 1);
|
|
|
|
*b = -*b;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (!seen_sign && gtk_css_token_is_ident (token, "-n"))
|
|
|
|
{
|
|
|
|
*a = -1;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return parse_plus_b (parser, FALSE, b);
|
|
|
|
}
|
|
|
|
else if (!seen_sign && gtk_css_token_is_ident (token, "-n-"))
|
|
|
|
{
|
|
|
|
*a = -1;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return parse_plus_b (parser, TRUE, b);
|
|
|
|
}
|
|
|
|
else if (!seen_sign &&
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT) &&
|
|
|
|
string_has_number (token->string.string, "-n-", b))
|
|
|
|
{
|
|
|
|
*a = -1;
|
|
|
|
*b = -*b;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_ident (token, "n") ||
|
|
|
|
gtk_css_token_is_ident (token, "n-"))
|
2012-03-18 01:11:44 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
return parse_n_plus_b (parser, seen_sign ? seen_sign : 1, a, b);
|
2012-03-18 01:11:44 +00:00
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
else if (gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT) &&
|
|
|
|
string_has_number (token->string.string, "n-", b))
|
2012-03-18 01:11:44 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
*a = seen_sign ? seen_sign : 1;
|
|
|
|
*b = -*b;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return TRUE;
|
2012-03-18 01:11:44 +00:00
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
else if (!seen_sign && gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT) &&
|
|
|
|
string_has_number (token->string.string, "-n-", b))
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
*a = -1;
|
|
|
|
*b = -*b;
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return TRUE;
|
2012-03-18 01:11:44 +00:00
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
|
|
|
|
gtk_css_parser_error_syntax (parser, "Not a valid an+b type");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
parse_a_n_plus_b_arg (GtkCssParser *parser,
|
|
|
|
guint arg,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gint *ab = data;
|
|
|
|
|
|
|
|
if (!parse_a_n_plus_b (parser, FALSE, &ab[0], &ab[1]))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
parse_dir_arg (GtkCssParser *parser,
|
|
|
|
guint arg,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkStateFlags *flag = data;
|
|
|
|
|
|
|
|
if (gtk_css_parser_try_ident (parser, "ltr"))
|
|
|
|
{
|
|
|
|
*flag = GTK_STATE_FLAG_DIR_LTR;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (gtk_css_parser_try_ident (parser, "rtl"))
|
2012-03-18 01:11:44 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
*flag = GTK_STATE_FLAG_DIR_RTL;
|
|
|
|
return 1;
|
2012-03-18 01:11:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_error_value (parser, "Expected \"ltr\" or \"rtl\"");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
parse_identifier_arg (GtkCssParser *parser,
|
|
|
|
guint arg,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
const char *ident = data;
|
|
|
|
|
|
|
|
if (!gtk_css_parser_try_ident (parser, ident))
|
|
|
|
{
|
|
|
|
gtk_css_parser_error_value (parser, "Expected \"%s\"", ident);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkCssSelector *
|
|
|
|
gtk_css_selector_parse_selector_pseudo_class (GtkCssParser *parser,
|
|
|
|
GtkCssSelector *selector,
|
|
|
|
gboolean negate)
|
|
|
|
{
|
2019-04-11 17:44:46 +00:00
|
|
|
GtkCssLocation start_location;
|
2019-04-09 02:47:00 +00:00
|
|
|
const GtkCssToken *token;
|
|
|
|
|
2019-04-11 17:44:46 +00:00
|
|
|
start_location = *gtk_css_parser_get_start_location (parser);
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
for (token = gtk_css_parser_peek_token (parser);
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_COMMENT);
|
|
|
|
token = gtk_css_parser_peek_token (parser))
|
|
|
|
{
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
2012-03-18 01:11:44 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
if (gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT))
|
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
GtkStateFlags state_flag;
|
|
|
|
PositionType position_type;
|
|
|
|
int position_a;
|
|
|
|
int position_b;
|
|
|
|
} pseudo_classes[] = {
|
|
|
|
{ "first-child", 0, POSITION_FORWARD, 0, 1 },
|
|
|
|
{ "last-child", 0, POSITION_BACKWARD, 0, 1 },
|
|
|
|
{ "only-child", 0, POSITION_ONLY, 0, 0 },
|
|
|
|
{ "active", GTK_STATE_FLAG_ACTIVE, },
|
|
|
|
{ "hover", GTK_STATE_FLAG_PRELIGHT, },
|
|
|
|
{ "selected", GTK_STATE_FLAG_SELECTED, },
|
|
|
|
{ "disabled", GTK_STATE_FLAG_INSENSITIVE, },
|
|
|
|
{ "indeterminate", GTK_STATE_FLAG_INCONSISTENT, },
|
|
|
|
{ "focus", GTK_STATE_FLAG_FOCUSED, },
|
|
|
|
{ "backdrop", GTK_STATE_FLAG_BACKDROP, },
|
|
|
|
{ "link", GTK_STATE_FLAG_LINK, },
|
|
|
|
{ "visited", GTK_STATE_FLAG_VISITED, },
|
|
|
|
{ "checked", GTK_STATE_FLAG_CHECKED, },
|
2020-04-16 03:09:36 +00:00
|
|
|
{ "focus-visible", GTK_STATE_FLAG_FOCUS_VISIBLE, },
|
2020-04-16 05:36:13 +00:00
|
|
|
{ "focus-within", GTK_STATE_FLAG_FOCUS_WITHIN, },
|
2019-04-09 02:47:00 +00:00
|
|
|
};
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (pseudo_classes); i++)
|
2012-03-18 01:11:44 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
if (g_ascii_strcasecmp (pseudo_classes[i].name, token->string.string) == 0)
|
|
|
|
{
|
|
|
|
if (pseudo_classes[i].state_flag)
|
|
|
|
{
|
2020-01-18 23:19:23 +00:00
|
|
|
selector = gtk_css_selector_new (negate ? >K_CSS_SELECTOR_NOT_PSEUDOCLASS_STATE
|
2020-01-15 20:10:06 +00:00
|
|
|
: >K_CSS_SELECTOR_PSEUDOCLASS_STATE,
|
2020-01-18 23:19:23 +00:00
|
|
|
selector);
|
2019-04-09 02:47:00 +00:00
|
|
|
selector->state.state = pseudo_classes[i].state_flag;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (negate ? >K_CSS_SELECTOR_NOT_PSEUDOCLASS_POSITION
|
|
|
|
: >K_CSS_SELECTOR_PSEUDOCLASS_POSITION,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 17:44:46 +00:00
|
|
|
gtk_css_parser_error (parser,
|
|
|
|
GTK_CSS_PARSER_ERROR_UNKNOWN_VALUE,
|
|
|
|
&start_location,
|
|
|
|
gtk_css_parser_get_end_location (parser),
|
|
|
|
"Unknown name of pseudo-class");
|
2019-04-09 02:47:00 +00:00
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is (token, GTK_CSS_TOKEN_FUNCTION))
|
|
|
|
{
|
|
|
|
if (gtk_css_token_is_function (token, "nth-child"))
|
|
|
|
{
|
|
|
|
gint ab[2];
|
|
|
|
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, parse_a_n_plus_b_arg, ab))
|
2019-04-05 11:41:08 +00:00
|
|
|
{
|
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
|
|
|
|
selector = gtk_css_selector_new (negate ? >K_CSS_SELECTOR_NOT_PSEUDOCLASS_POSITION
|
|
|
|
: >K_CSS_SELECTOR_PSEUDOCLASS_POSITION,
|
|
|
|
selector);
|
|
|
|
selector->position.type = POSITION_FORWARD;
|
|
|
|
selector->position.a = ab[0];
|
|
|
|
selector->position.b = ab[1];
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "nth-last-child"))
|
|
|
|
{
|
|
|
|
gint ab[2];
|
|
|
|
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, parse_a_n_plus_b_arg, ab))
|
2012-03-18 01:11:44 +00:00
|
|
|
{
|
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
selector = gtk_css_selector_new (negate ? >K_CSS_SELECTOR_NOT_PSEUDOCLASS_POSITION
|
|
|
|
: >K_CSS_SELECTOR_PSEUDOCLASS_POSITION,
|
|
|
|
selector);
|
|
|
|
selector->position.type = POSITION_BACKWARD;
|
|
|
|
selector->position.a = ab[0];
|
|
|
|
selector->position.b = ab[1];
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "not"))
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
if (negate)
|
|
|
|
{
|
|
|
|
gtk_css_parser_error_syntax (parser, "Nesting of :not() not allowed");
|
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-18 01:11:44 +00:00
|
|
|
else
|
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_start_block (parser);
|
|
|
|
token = gtk_css_parser_get_token (parser);
|
|
|
|
|
|
|
|
if (gtk_css_token_is_delim (token, '*'))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (>K_CSS_SELECTOR_NOT_ANY, selector);
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (>K_CSS_SELECTOR_NOT_NAME, selector);
|
2020-01-23 23:43:26 +00:00
|
|
|
selector->name.name = g_quark_from_string (token->string.string);
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is (token, GTK_CSS_TOKEN_HASH_ID))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (>K_CSS_SELECTOR_NOT_ID, selector);
|
2020-01-23 23:43:26 +00:00
|
|
|
selector->id.name = g_quark_from_string (token->string.string);
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_delim (token, '.'))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_parse_selector_class (parser, selector, TRUE);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is (token, GTK_CSS_TOKEN_COLON))
|
2019-04-05 11:41:08 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
selector = gtk_css_selector_parse_selector_pseudo_class (parser, selector, TRUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gtk_css_parser_error_syntax (parser, "Invalid contents of :not() selector");
|
2019-12-02 16:31:15 +00:00
|
|
|
gtk_css_parser_end_block (parser);
|
2019-04-05 11:41:08 +00:00
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
2019-04-09 02:47:00 +00:00
|
|
|
selector = NULL;
|
2019-04-05 11:41:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
|
|
|
|
token = gtk_css_parser_get_token (parser);
|
|
|
|
if (!gtk_css_token_is (token, GTK_CSS_TOKEN_EOF))
|
2012-03-18 01:11:44 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Invalid contents of :not() selector");
|
2019-12-02 16:31:15 +00:00
|
|
|
gtk_css_parser_end_block (parser);
|
2012-03-18 01:11:44 +00:00
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
2019-04-09 02:47:00 +00:00
|
|
|
selector = NULL;
|
2012-03-18 01:11:44 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_end_block (parser);
|
2012-03-18 01:11:44 +00:00
|
|
|
}
|
2012-02-15 14:42:00 +00:00
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
else if (gtk_css_token_is_function (token, "dir"))
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
GtkStateFlags flag;
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, parse_dir_arg, &flag))
|
|
|
|
{
|
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-18 01:11:44 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
selector = gtk_css_selector_new (negate ? >K_CSS_SELECTOR_NOT_PSEUDOCLASS_STATE
|
|
|
|
: >K_CSS_SELECTOR_PSEUDOCLASS_STATE,
|
|
|
|
selector);
|
|
|
|
selector->state.state = flag;
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "drop"))
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, parse_identifier_arg, (gpointer) "active"))
|
2014-12-06 13:01:02 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
return NULL;
|
2014-12-06 13:01:02 +00:00
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
selector = gtk_css_selector_new (negate ? >K_CSS_SELECTOR_NOT_PSEUDOCLASS_STATE
|
|
|
|
: >K_CSS_SELECTOR_PSEUDOCLASS_STATE,
|
|
|
|
selector);
|
|
|
|
selector->state.state = GTK_STATE_FLAG_DROP_ACTIVE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-11 17:44:46 +00:00
|
|
|
gtk_css_parser_error (parser,
|
|
|
|
GTK_CSS_PARSER_ERROR_UNKNOWN_VALUE,
|
|
|
|
&start_location,
|
|
|
|
gtk_css_parser_get_end_location (parser),
|
|
|
|
"Unknown pseudoclass");
|
2019-04-09 02:47:00 +00:00
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
return NULL;
|
2012-02-15 14:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-25 18:15:36 +00:00
|
|
|
else
|
|
|
|
{
|
2019-04-11 17:44:46 +00:00
|
|
|
gtk_css_parser_error (parser,
|
|
|
|
GTK_CSS_PARSER_ERROR_UNKNOWN_VALUE,
|
|
|
|
&start_location,
|
|
|
|
gtk_css_parser_get_end_location (parser),
|
|
|
|
"Unknown pseudoclass");
|
2014-11-25 18:15:36 +00:00
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:16:18 +00:00
|
|
|
static GtkCssSelector *
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_selector_parse_simple_selector (GtkCssParser *parser,
|
|
|
|
GtkCssSelector *selector)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2014-12-04 02:42:50 +00:00
|
|
|
gboolean parsed_something = FALSE;
|
2019-04-09 02:47:00 +00:00
|
|
|
const GtkCssToken *token;
|
2012-02-15 14:42:00 +00:00
|
|
|
|
|
|
|
do {
|
2019-04-09 02:47:00 +00:00
|
|
|
for (token = gtk_css_parser_peek_token (parser);
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_COMMENT);
|
|
|
|
token = gtk_css_parser_peek_token (parser))
|
2012-02-15 14:42:00 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parsed_something && gtk_css_token_is_delim (token, '*'))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (>K_CSS_SELECTOR_ANY, selector);
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
else if (!parsed_something && gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (>K_CSS_SELECTOR_NAME, selector);
|
2020-01-23 23:43:26 +00:00
|
|
|
selector->name.name = g_quark_from_string (token->string.string);
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is (token, GTK_CSS_TOKEN_HASH_ID))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (>K_CSS_SELECTOR_ID, selector);
|
2020-01-23 23:43:26 +00:00
|
|
|
selector->id.name = g_quark_from_string (token->string.string);
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_delim (token, '.'))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_parse_selector_class (parser, selector, FALSE);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is (token, GTK_CSS_TOKEN_COLON))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_parse_selector_pseudo_class (parser, selector, FALSE);
|
2012-02-15 14:42:00 +00:00
|
|
|
}
|
|
|
|
else
|
2019-04-09 02:47:00 +00:00
|
|
|
{
|
|
|
|
if (!parsed_something)
|
|
|
|
{
|
|
|
|
gtk_css_parser_error_syntax (parser, "Expected a valid selector");
|
|
|
|
if (selector)
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
selector = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-12-04 02:42:50 +00:00
|
|
|
|
|
|
|
parsed_something = TRUE;
|
2012-02-15 14:42:00 +00:00
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
while (TRUE);
|
2012-02-16 14:16:18 +00:00
|
|
|
|
|
|
|
return selector;
|
2012-02-15 14:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkCssSelector *
|
|
|
|
_gtk_css_selector_parse (GtkCssParser *parser)
|
|
|
|
{
|
|
|
|
GtkCssSelector *selector = NULL;
|
2019-04-09 02:47:00 +00:00
|
|
|
const GtkCssToken *token;
|
2012-02-15 14:42:00 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
while (TRUE)
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
gboolean seen_whitespace = FALSE;
|
|
|
|
|
|
|
|
/* skip all whitespace and comments */
|
|
|
|
gtk_css_parser_get_token (parser);
|
|
|
|
|
|
|
|
selector = gtk_css_selector_parse_simple_selector (parser, selector);
|
|
|
|
if (selector == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (token = gtk_css_parser_peek_token (parser);
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_COMMENT) ||
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_WHITESPACE);
|
|
|
|
token = gtk_css_parser_peek_token (parser))
|
|
|
|
{
|
|
|
|
seen_whitespace |= gtk_css_token_is (token, GTK_CSS_TOKEN_WHITESPACE);
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gtk_css_token_is_delim (token, '+'))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (>K_CSS_SELECTOR_ADJACENT, selector);
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_delim (token, '~'))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (>K_CSS_SELECTOR_SIBLING, selector);
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_delim (token, '>'))
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (>K_CSS_SELECTOR_CHILD, selector);
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is (token, GTK_CSS_TOKEN_EOF) ||
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_COMMA) ||
|
|
|
|
gtk_css_token_is (token, GTK_CSS_TOKEN_OPEN_CURLY))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (seen_whitespace)
|
|
|
|
{
|
|
|
|
selector = gtk_css_selector_new (>K_CSS_SELECTOR_DESCENDANT, selector);
|
|
|
|
}
|
2012-02-16 14:16:18 +00:00
|
|
|
else
|
2019-04-09 02:47:00 +00:00
|
|
|
{
|
|
|
|
gtk_css_parser_error_syntax (parser, "Expected a valid selector");
|
|
|
|
_gtk_css_selector_free (selector);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
2012-02-15 14:42:00 +00:00
|
|
|
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
2011-05-14 11:27:31 +00:00
|
|
|
void
|
|
|
|
_gtk_css_selector_free (GtkCssSelector *selector)
|
|
|
|
{
|
|
|
|
g_return_if_fail (selector != NULL);
|
|
|
|
|
2012-03-01 17:33:33 +00:00
|
|
|
g_free (selector);
|
2011-05-14 11:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_css_selector_print (const GtkCssSelector *selector,
|
|
|
|
GString * str)
|
|
|
|
{
|
2012-03-01 16:40:19 +00:00
|
|
|
const GtkCssSelector *previous;
|
|
|
|
|
2012-02-16 14:16:18 +00:00
|
|
|
g_return_if_fail (selector != NULL);
|
2011-05-14 11:27:31 +00:00
|
|
|
|
2012-03-01 16:40:19 +00:00
|
|
|
previous = gtk_css_selector_previous (selector);
|
|
|
|
if (previous)
|
|
|
|
_gtk_css_selector_print (previous, str);
|
2011-05-14 11:27:31 +00:00
|
|
|
|
2012-02-16 14:16:18 +00:00
|
|
|
selector->class->print (selector, str);
|
2011-05-14 11:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-05-16 18:20:55 +00:00
|
|
|
/**
|
2020-01-28 02:53:48 +00:00
|
|
|
* gtk_css_selector_matches:
|
2011-05-16 18:20:55 +00:00
|
|
|
* @selector: the selector
|
2020-01-23 02:34:25 +00:00
|
|
|
* @node: The node to match
|
2011-05-16 18:20:55 +00:00
|
|
|
*
|
2020-01-23 02:34:25 +00:00
|
|
|
* Checks if the @selector matches the given @node.
|
2011-05-16 18:20:55 +00:00
|
|
|
*
|
2020-01-23 02:34:25 +00:00
|
|
|
* Returns: %TRUE if the selector matches @node
|
2011-05-16 18:20:55 +00:00
|
|
|
**/
|
2011-05-14 11:27:31 +00:00
|
|
|
gboolean
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_matches (const GtkCssSelector *selector,
|
|
|
|
GtkCssNode *node)
|
2011-05-14 11:27:31 +00:00
|
|
|
{
|
2020-01-28 02:53:48 +00:00
|
|
|
GtkCssNode *child;
|
|
|
|
const GtkCssSelector *prev;
|
2011-05-14 11:27:31 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (selector != NULL, FALSE);
|
2020-01-23 02:34:25 +00:00
|
|
|
g_return_val_if_fail (node != NULL, FALSE);
|
2011-05-14 11:27:31 +00:00
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
if (!gtk_css_selector_match_one (selector, node))
|
2014-12-08 23:35:55 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
prev = gtk_css_selector_previous (selector);
|
|
|
|
if (prev == NULL)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
for (child = gtk_css_selector_iterator (selector, node, NULL);
|
|
|
|
child;
|
|
|
|
child = gtk_css_selector_iterator (selector, node, child))
|
|
|
|
{
|
|
|
|
if (gtk_css_selector_matches (prev, child))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2011-05-14 11:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2012-03-01 16:40:19 +00:00
|
|
|
for (; selector; selector = gtk_css_selector_previous (selector))
|
2012-02-16 14:16:18 +00:00
|
|
|
{
|
2014-12-08 05:46:38 +00:00
|
|
|
selector->class->add_specificity (selector, ids, classes, elements);
|
2012-02-16 14:16:18 +00:00
|
|
|
}
|
2011-05-14 11:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_gtk_css_selector_compare (const GtkCssSelector *a,
|
|
|
|
const GtkCssSelector *b)
|
|
|
|
{
|
|
|
|
guint a_ids = 0, a_classes = 0, a_elements = 0;
|
|
|
|
guint b_ids = 0, b_classes = 0, b_elements = 0;
|
|
|
|
int compare;
|
|
|
|
|
|
|
|
_gtk_css_selector_get_specificity (a, &a_ids, &a_classes, &a_elements);
|
|
|
|
_gtk_css_selector_get_specificity (b, &b_ids, &b_classes, &b_elements);
|
|
|
|
|
|
|
|
compare = a_ids - b_ids;
|
|
|
|
if (compare)
|
|
|
|
return compare;
|
|
|
|
|
|
|
|
compare = a_classes - b_classes;
|
|
|
|
if (compare)
|
|
|
|
return compare;
|
|
|
|
|
|
|
|
return a_elements - b_elements;
|
|
|
|
}
|
|
|
|
|
2014-12-09 02:21:13 +00:00
|
|
|
GtkCssChange
|
|
|
|
_gtk_css_selector_get_change (const GtkCssSelector *selector)
|
|
|
|
{
|
|
|
|
if (selector == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return selector->class->get_change (selector, _gtk_css_selector_get_change (gtk_css_selector_previous (selector)));
|
|
|
|
}
|
2012-11-28 10:15:53 +00:00
|
|
|
|
|
|
|
/******************** SelectorTree handling *****************/
|
|
|
|
|
2020-01-21 15:51:55 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_css_selector_is_simple (const GtkCssSelector *selector)
|
|
|
|
{
|
|
|
|
switch (selector->class->category)
|
|
|
|
{
|
|
|
|
case GTK_CSS_SELECTOR_CATEGORY_SIMPLE:
|
|
|
|
case GTK_CSS_SELECTOR_CATEGORY_SIMPLE_RADICAL:
|
|
|
|
return TRUE;
|
|
|
|
case GTK_CSS_SELECTOR_CATEGORY_PARENT:
|
|
|
|
case GTK_CSS_SELECTOR_CATEGORY_SIBLING:
|
|
|
|
return FALSE;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:15:53 +00:00
|
|
|
static GHashTable *
|
|
|
|
gtk_css_selectors_count_initial_init (void)
|
|
|
|
{
|
2014-12-06 00:00:10 +00:00
|
|
|
return g_hash_table_new ((GHashFunc)gtk_css_selector_hash_one, (GEqualFunc)gtk_css_selector_equal);
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-12-06 00:00:10 +00:00
|
|
|
gtk_css_selectors_count_initial (const GtkCssSelector *selector, GHashTable *hash_one)
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
2020-01-21 15:51:55 +00:00
|
|
|
if (!gtk_css_selector_is_simple (selector))
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
2014-12-06 00:00:10 +00:00
|
|
|
guint count = GPOINTER_TO_INT (g_hash_table_lookup (hash_one, selector));
|
|
|
|
g_hash_table_replace (hash_one, (gpointer)selector, GUINT_TO_POINTER (count + 1));
|
2012-11-28 10:15:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-29 13:21:20 +00:00
|
|
|
for (;
|
2020-01-21 15:51:55 +00:00
|
|
|
selector && gtk_css_selector_is_simple (selector);
|
2012-11-29 13:21:20 +00:00
|
|
|
selector = gtk_css_selector_previous (selector))
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
2014-12-06 00:00:10 +00:00
|
|
|
guint count = GPOINTER_TO_INT (g_hash_table_lookup (hash_one, selector));
|
|
|
|
g_hash_table_replace (hash_one, (gpointer)selector, GUINT_TO_POINTER (count + 1));
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_css_selectors_has_initial_selector (const GtkCssSelector *selector, const GtkCssSelector *initial)
|
|
|
|
{
|
2020-01-21 15:51:55 +00:00
|
|
|
if (!gtk_css_selector_is_simple (selector))
|
2012-11-28 10:15:53 +00:00
|
|
|
return gtk_css_selector_equal (selector, initial);
|
|
|
|
|
2012-11-29 13:21:20 +00:00
|
|
|
for (;
|
2020-01-21 15:51:55 +00:00
|
|
|
selector && gtk_css_selector_is_simple (selector);
|
2012-11-29 13:21:20 +00:00
|
|
|
selector = gtk_css_selector_previous (selector))
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
|
|
|
if (gtk_css_selector_equal (selector, initial))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkCssSelector *
|
|
|
|
gtk_css_selectors_skip_initial_selector (GtkCssSelector *selector, const GtkCssSelector *initial)
|
|
|
|
{
|
|
|
|
GtkCssSelector *found;
|
|
|
|
GtkCssSelector tmp;
|
|
|
|
|
|
|
|
/* If the initial simple selector is not first, move it there so we can skip it
|
|
|
|
without losing any other selectors */
|
|
|
|
if (!gtk_css_selector_equal (selector, initial))
|
|
|
|
{
|
2020-01-21 15:51:55 +00:00
|
|
|
for (found = selector; found && gtk_css_selector_is_simple (found); found = (GtkCssSelector *)gtk_css_selector_previous (found))
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
|
|
|
if (gtk_css_selector_equal (found, initial))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-21 15:51:55 +00:00
|
|
|
g_assert (found != NULL && gtk_css_selector_is_simple (found));
|
2012-11-28 10:15:53 +00:00
|
|
|
|
|
|
|
tmp = *found;
|
|
|
|
*found = *selector;
|
|
|
|
*selector = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (GtkCssSelector *)gtk_css_selector_previous (selector);
|
|
|
|
}
|
|
|
|
|
2020-01-28 17:28:32 +00:00
|
|
|
/* When checking for changes via the tree we need to know if a rule further
|
|
|
|
down the tree matched, because if so we need to add "our bit" to the
|
|
|
|
Change. For instance in a match like *.class:active we'll
|
|
|
|
get a tree that first checks :active, if that matches we continue down
|
|
|
|
to the tree, and if we get a match we add CHANGE_CLASS. However, the
|
|
|
|
end of the tree where we have a match is an ANY which doesn't actually
|
|
|
|
modify the change, so we don't know if we have a match or not. We fix
|
|
|
|
this by setting GTK_CSS_CHANGE_GOT_MATCH which lets us guarantee
|
|
|
|
that change != 0 on any match. */
|
|
|
|
#define GTK_CSS_CHANGE_GOT_MATCH GTK_CSS_CHANGE_RESERVED_BIT
|
|
|
|
|
|
|
|
/* The code for collecting matches assumes that the name, id and classes
|
|
|
|
* of a node remain unchanged, and anything else can change. This needs to
|
|
|
|
* be kept in sync with the definition of 'radical change' in gtkcssnode.c.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static GtkCssChange
|
|
|
|
gtk_css_selector_tree_get_change (const GtkCssSelectorTree *tree,
|
|
|
|
const GtkCountingBloomFilter *filter,
|
|
|
|
GtkCssNode *node,
|
|
|
|
gboolean skipping)
|
|
|
|
{
|
|
|
|
GtkCssChange change = 0;
|
|
|
|
const GtkCssSelectorTree *prev;
|
|
|
|
|
|
|
|
switch (tree->selector.class->category)
|
|
|
|
{
|
|
|
|
case GTK_CSS_SELECTOR_CATEGORY_SIMPLE:
|
|
|
|
break;
|
|
|
|
case GTK_CSS_SELECTOR_CATEGORY_SIMPLE_RADICAL:
|
|
|
|
if (skipping)
|
|
|
|
break;
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
if (!tree->selector.class->match_one (&tree->selector, node))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (filter)
|
|
|
|
{
|
|
|
|
if (!gtk_counting_bloom_filter_may_contain (filter,
|
|
|
|
gtk_css_selector_hash_one (&tree->selector)))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GTK_CSS_SELECTOR_CATEGORY_PARENT:
|
|
|
|
skipping = FALSE;
|
|
|
|
node = NULL;
|
|
|
|
break;
|
|
|
|
case GTK_CSS_SELECTOR_CATEGORY_SIBLING:
|
|
|
|
skipping = TRUE;
|
|
|
|
node = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (prev = gtk_css_selector_tree_get_previous (tree);
|
|
|
|
prev != NULL;
|
|
|
|
prev = gtk_css_selector_tree_get_sibling (prev))
|
|
|
|
change |= gtk_css_selector_tree_get_change (prev, filter, node, skipping);
|
|
|
|
|
|
|
|
if (change || gtk_css_selector_tree_get_matches (tree))
|
|
|
|
change = tree->selector.class->get_change (&tree->selector, change & ~GTK_CSS_CHANGE_GOT_MATCH) | GTK_CSS_CHANGE_GOT_MATCH;
|
|
|
|
|
|
|
|
return change;
|
|
|
|
}
|
|
|
|
|
2020-01-26 03:37:17 +00:00
|
|
|
static void
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_tree_found_match (const GtkCssSelectorTree *tree,
|
2020-07-02 16:30:55 +00:00
|
|
|
GtkCssSelectorMatches *results)
|
2020-01-26 03:37:17 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
gpointer *matches;
|
|
|
|
|
|
|
|
matches = gtk_css_selector_tree_get_matches (tree);
|
2020-01-28 02:53:48 +00:00
|
|
|
if (!matches)
|
|
|
|
return;
|
2020-01-26 03:37:17 +00:00
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
for (i = 0; matches[i] != NULL; i++)
|
2020-07-02 16:30:55 +00:00
|
|
|
gtk_css_selector_matches_insert_sorted (results, matches[i]);
|
2020-01-26 03:37:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 03:20:47 +00:00
|
|
|
static gboolean
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_tree_match (const GtkCssSelectorTree *tree,
|
|
|
|
const GtkCountingBloomFilter *filter,
|
2020-01-29 03:20:47 +00:00
|
|
|
gboolean match_filter,
|
2020-01-28 02:53:48 +00:00
|
|
|
GtkCssNode *node,
|
2020-07-02 16:30:55 +00:00
|
|
|
GtkCssSelectorMatches *results)
|
2014-12-09 01:59:14 +00:00
|
|
|
{
|
|
|
|
const GtkCssSelectorTree *prev;
|
2020-01-28 02:53:48 +00:00
|
|
|
GtkCssNode *child;
|
2014-12-09 01:59:14 +00:00
|
|
|
|
2020-01-29 03:20:47 +00:00
|
|
|
if (match_filter && tree->selector.class->category == GTK_CSS_SELECTOR_CATEGORY_SIMPLE_RADICAL &&
|
|
|
|
!gtk_counting_bloom_filter_may_contain (filter, gtk_css_selector_hash_one (&tree->selector)))
|
|
|
|
return FALSE;
|
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
if (!gtk_css_selector_match_one (&tree->selector, node))
|
2020-01-29 03:20:47 +00:00
|
|
|
return TRUE;
|
2014-12-09 01:59:14 +00:00
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
gtk_css_selector_tree_found_match (tree, results);
|
2020-01-26 03:37:17 +00:00
|
|
|
|
2020-01-28 02:53:48 +00:00
|
|
|
if (filter && !gtk_css_selector_is_simple (&tree->selector))
|
2020-01-29 03:20:47 +00:00
|
|
|
match_filter = tree->selector.class->category == GTK_CSS_SELECTOR_CATEGORY_PARENT;
|
2014-12-09 01:59:14 +00:00
|
|
|
|
|
|
|
for (prev = gtk_css_selector_tree_get_previous (tree);
|
|
|
|
prev != NULL;
|
|
|
|
prev = gtk_css_selector_tree_get_sibling (prev))
|
2020-01-28 02:53:48 +00:00
|
|
|
{
|
|
|
|
for (child = gtk_css_selector_iterator (&tree->selector, node, NULL);
|
|
|
|
child;
|
|
|
|
child = gtk_css_selector_iterator (&tree->selector, node, child))
|
|
|
|
{
|
2020-01-29 03:20:47 +00:00
|
|
|
if (!gtk_css_selector_tree_match (prev, filter, match_filter, child, results))
|
|
|
|
break;
|
2020-01-28 02:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-09 01:59:14 +00:00
|
|
|
|
2020-01-29 03:20:47 +00:00
|
|
|
return TRUE;
|
2014-12-09 01:59:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 08:46:53 +00:00
|
|
|
void
|
2020-01-26 03:37:17 +00:00
|
|
|
_gtk_css_selector_tree_match_all (const GtkCssSelectorTree *tree,
|
|
|
|
const GtkCountingBloomFilter *filter,
|
2020-04-29 08:46:53 +00:00
|
|
|
GtkCssNode *node,
|
2020-07-02 16:30:55 +00:00
|
|
|
GtkCssSelectorMatches *out_tree_rules)
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
2020-01-29 03:20:47 +00:00
|
|
|
const GtkCssSelectorTree *iter;
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2020-01-29 03:20:47 +00:00
|
|
|
for (iter = tree;
|
|
|
|
iter != NULL;
|
|
|
|
iter = gtk_css_selector_tree_get_sibling (iter))
|
2020-01-28 02:53:48 +00:00
|
|
|
{
|
2020-04-29 08:46:53 +00:00
|
|
|
gtk_css_selector_tree_match (iter, filter, FALSE, node, out_tree_rules);
|
2020-01-28 02:53:48 +00:00
|
|
|
}
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 08:35:53 +00:00
|
|
|
gboolean
|
|
|
|
_gtk_css_selector_tree_is_empty (const GtkCssSelectorTree *tree)
|
|
|
|
{
|
|
|
|
return tree == NULL;
|
|
|
|
}
|
|
|
|
|
2020-01-17 15:54:37 +00:00
|
|
|
GtkCssChange
|
2020-01-27 02:26:39 +00:00
|
|
|
gtk_css_selector_tree_get_change_all (const GtkCssSelectorTree *tree,
|
|
|
|
const GtkCountingBloomFilter *filter,
|
|
|
|
GtkCssNode *node)
|
2020-01-17 15:54:37 +00:00
|
|
|
{
|
2020-01-27 02:26:39 +00:00
|
|
|
GtkCssChange change = 0;
|
2020-01-17 15:54:37 +00:00
|
|
|
|
|
|
|
for (; tree != NULL;
|
|
|
|
tree = gtk_css_selector_tree_get_sibling (tree))
|
2020-01-27 02:26:39 +00:00
|
|
|
change |= gtk_css_selector_tree_get_change (tree, filter, node, FALSE);
|
2020-01-17 15:54:37 +00:00
|
|
|
|
|
|
|
/* Never return reserved bit set */
|
|
|
|
return change & ~GTK_CSS_CHANGE_RESERVED_BIT;
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:15:53 +00:00
|
|
|
#ifdef PRINT_TREE
|
|
|
|
static void
|
2020-01-29 03:20:47 +00:00
|
|
|
_gtk_css_selector_tree_print (const GtkCssSelectorTree *tree, GString *str, const char *prefix)
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
|
|
|
gboolean first = TRUE;
|
|
|
|
int len, i;
|
2020-01-24 16:50:40 +00:00
|
|
|
gpointer *matches;
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2014-07-21 17:53:12 +00:00
|
|
|
for (; tree != NULL; tree = gtk_css_selector_tree_get_sibling (tree), first = FALSE)
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
g_string_append (str, prefix);
|
|
|
|
|
|
|
|
if (first)
|
|
|
|
{
|
2014-07-21 17:53:12 +00:00
|
|
|
if (gtk_css_selector_tree_get_sibling (tree))
|
2012-11-28 10:15:53 +00:00
|
|
|
g_string_append (str, "─┬─");
|
|
|
|
else
|
|
|
|
g_string_append (str, "───");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-21 17:53:12 +00:00
|
|
|
if (gtk_css_selector_tree_get_sibling (tree))
|
2012-11-28 10:15:53 +00:00
|
|
|
g_string_append (str, " ├─");
|
|
|
|
else
|
|
|
|
g_string_append (str, " └─");
|
|
|
|
}
|
|
|
|
|
|
|
|
len = str->len;
|
|
|
|
tree->selector.class->print (&tree->selector, str);
|
2020-01-24 16:50:40 +00:00
|
|
|
matches = gtk_css_selector_tree_get_matches (tree);
|
|
|
|
if (matches)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
for (n = 0; matches[n] != NULL; n++) ;
|
|
|
|
if (n == 1)
|
|
|
|
g_string_append (str, " (1 match)");
|
|
|
|
else
|
|
|
|
g_string_append_printf (str, " (%d matches)", n);
|
|
|
|
}
|
2012-11-28 10:15:53 +00:00
|
|
|
len = str->len - len;
|
|
|
|
|
2012-11-29 16:56:00 +00:00
|
|
|
if (gtk_css_selector_tree_get_previous (tree))
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
|
|
|
GString *prefix2 = g_string_new (prefix);
|
|
|
|
|
2014-07-21 17:53:12 +00:00
|
|
|
if (gtk_css_selector_tree_get_sibling (tree))
|
2012-11-28 10:15:53 +00:00
|
|
|
g_string_append (prefix2, " │ ");
|
|
|
|
else
|
|
|
|
g_string_append (prefix2, " ");
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
g_string_append_c (prefix2, ' ');
|
|
|
|
|
2012-11-29 16:56:00 +00:00
|
|
|
_gtk_css_selector_tree_print (gtk_css_selector_tree_get_previous (tree), str, prefix2->str);
|
2012-11-28 10:15:53 +00:00
|
|
|
g_string_free (prefix2, TRUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_string_append (str, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-29 15:00:01 +00:00
|
|
|
void
|
|
|
|
_gtk_css_selector_tree_match_print (const GtkCssSelectorTree *tree,
|
|
|
|
GString *str)
|
|
|
|
{
|
2015-10-27 19:11:08 +00:00
|
|
|
const GtkCssSelectorTree *iter;
|
2012-11-29 16:56:00 +00:00
|
|
|
|
2012-11-29 15:00:01 +00:00
|
|
|
g_return_if_fail (tree != NULL);
|
|
|
|
|
2015-10-27 19:11:08 +00:00
|
|
|
/* print name and * selector before others */
|
|
|
|
for (iter = tree;
|
2020-01-21 15:51:55 +00:00
|
|
|
iter && gtk_css_selector_is_simple (&iter->selector);
|
2015-10-27 19:11:08 +00:00
|
|
|
iter = gtk_css_selector_tree_get_parent (iter))
|
|
|
|
{
|
|
|
|
if (iter->selector.class == >K_CSS_SELECTOR_NAME ||
|
|
|
|
iter->selector.class == >K_CSS_SELECTOR_ANY)
|
|
|
|
{
|
|
|
|
iter->selector.class->print (&iter->selector, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* now print other simple selectors */
|
|
|
|
for (iter = tree;
|
2020-01-21 15:51:55 +00:00
|
|
|
iter && gtk_css_selector_is_simple (&iter->selector);
|
2015-10-27 19:11:08 +00:00
|
|
|
iter = gtk_css_selector_tree_get_parent (iter))
|
|
|
|
{
|
|
|
|
if (iter->selector.class != >K_CSS_SELECTOR_NAME &&
|
|
|
|
iter->selector.class != >K_CSS_SELECTOR_ANY)
|
|
|
|
{
|
|
|
|
iter->selector.class->print (&iter->selector, str);
|
|
|
|
}
|
|
|
|
}
|
2012-11-29 15:00:01 +00:00
|
|
|
|
2015-10-27 19:11:08 +00:00
|
|
|
/* now if there's a combinator, print that one */
|
|
|
|
if (iter != NULL)
|
|
|
|
{
|
|
|
|
iter->selector.class->print (&iter->selector, str);
|
|
|
|
tree = gtk_css_selector_tree_get_parent (iter);
|
|
|
|
if (tree)
|
|
|
|
_gtk_css_selector_tree_match_print (tree, str);
|
|
|
|
}
|
2012-11-29 15:00:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 10:15:53 +00:00
|
|
|
void
|
|
|
|
_gtk_css_selector_tree_free (GtkCssSelectorTree *tree)
|
|
|
|
{
|
|
|
|
if (tree == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_free (tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2012-11-29 14:01:47 +00:00
|
|
|
gpointer match;
|
2012-11-28 10:15:53 +00:00
|
|
|
GtkCssSelector *current_selector;
|
2012-11-29 14:29:14 +00:00
|
|
|
GtkCssSelectorTree **selector_match;
|
2012-11-28 10:15:53 +00:00
|
|
|
} GtkCssSelectorRuleSetInfo;
|
|
|
|
|
2012-11-29 19:52:46 +00:00
|
|
|
static GtkCssSelectorTree *
|
|
|
|
get_tree (GByteArray *array, gint32 offset)
|
|
|
|
{
|
|
|
|
return (GtkCssSelectorTree *) (array->data + offset);
|
|
|
|
}
|
2012-11-28 10:15:53 +00:00
|
|
|
|
|
|
|
static GtkCssSelectorTree *
|
2012-11-29 19:52:46 +00:00
|
|
|
alloc_tree (GByteArray *array, gint32 *offset)
|
|
|
|
{
|
|
|
|
GtkCssSelectorTree tree = { { NULL} };
|
|
|
|
|
|
|
|
*offset = array->len;
|
|
|
|
g_byte_array_append (array, (guint8 *)&tree, sizeof (GtkCssSelectorTree));
|
|
|
|
return get_tree (array, *offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint32
|
2020-04-25 19:08:13 +00:00
|
|
|
subdivide_infos (GByteArray *array,
|
|
|
|
GtkCssSelectorRuleSetInfo **infos,
|
|
|
|
guint n_infos,
|
|
|
|
gint32 parent_offset)
|
|
|
|
{
|
2020-06-12 06:42:15 +00:00
|
|
|
const GtkCssSelector *max_selector = NULL;
|
2020-04-25 19:08:13 +00:00
|
|
|
GtkCssSelectorRuleSetInfo **matched_infos;
|
|
|
|
guint n_matched = 0;
|
|
|
|
GtkCssSelectorRuleSetInfo **remaining_infos;
|
|
|
|
guint n_remaining = 0;
|
2012-12-06 22:38:06 +00:00
|
|
|
GHashTable *ht;
|
2012-11-29 19:52:46 +00:00
|
|
|
gint32 tree_offset;
|
2012-11-28 10:15:53 +00:00
|
|
|
GtkCssSelectorTree *tree;
|
|
|
|
GHashTableIter iter;
|
|
|
|
guint max_count;
|
|
|
|
gpointer key, value;
|
2020-07-02 16:30:55 +00:00
|
|
|
GtkCssSelectorMatches exact_matches;
|
2012-11-29 19:52:46 +00:00
|
|
|
gint32 res;
|
2020-04-25 19:08:13 +00:00
|
|
|
guint i;
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2020-04-25 19:08:13 +00:00
|
|
|
if (n_infos == 0)
|
2012-11-29 19:52:46 +00:00
|
|
|
return GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET;
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2012-12-06 22:38:06 +00:00
|
|
|
ht = gtk_css_selectors_count_initial_init ();
|
|
|
|
|
2020-04-25 19:08:13 +00:00
|
|
|
for (i = 0; i < n_infos; i++)
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
2020-04-25 19:08:13 +00:00
|
|
|
const GtkCssSelectorRuleSetInfo *info = infos[i];
|
2012-11-28 10:15:53 +00:00
|
|
|
gtk_css_selectors_count_initial (info->current_selector, ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pick the selector with highest count, and use as decision on this level
|
|
|
|
as that makes it possible to skip the largest amount of checks later */
|
|
|
|
|
|
|
|
max_count = 0;
|
|
|
|
|
|
|
|
g_hash_table_iter_init (&iter, ht);
|
|
|
|
while (g_hash_table_iter_next (&iter, &key, &value))
|
|
|
|
{
|
2012-11-29 15:20:14 +00:00
|
|
|
GtkCssSelector *selector = key;
|
|
|
|
if (GPOINTER_TO_UINT (value) > max_count ||
|
2020-06-12 06:42:15 +00:00
|
|
|
(GPOINTER_TO_UINT (value) == max_count &&
|
|
|
|
gtk_css_selector_compare_one (selector, max_selector) < 0))
|
|
|
|
{
|
|
|
|
max_count = GPOINTER_TO_UINT (value);
|
|
|
|
max_selector = selector;
|
|
|
|
}
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 19:52:46 +00:00
|
|
|
tree = alloc_tree (array, &tree_offset);
|
|
|
|
tree->parent_offset = parent_offset;
|
2020-06-12 06:42:15 +00:00
|
|
|
tree->selector = *max_selector;
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2020-04-25 19:08:13 +00:00
|
|
|
/* Allocate maximum for both of them */
|
|
|
|
/* TODO: Potentially dangerous? */
|
|
|
|
matched_infos = g_alloca (sizeof (GtkCssSelectorRuleSetInfo *) * n_infos);
|
|
|
|
remaining_infos = g_alloca (sizeof (GtkCssSelectorRuleSetInfo *) * n_infos);
|
|
|
|
|
2020-07-02 16:30:55 +00:00
|
|
|
gtk_css_selector_matches_init (&exact_matches);
|
2020-04-25 19:08:13 +00:00
|
|
|
for (i = 0; i < n_infos; i++)
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
2020-04-25 19:08:13 +00:00
|
|
|
GtkCssSelectorRuleSetInfo *info = infos[i];
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2020-06-12 06:42:15 +00:00
|
|
|
if (gtk_css_selectors_has_initial_selector (info->current_selector, max_selector))
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
2020-06-12 06:42:15 +00:00
|
|
|
info->current_selector = gtk_css_selectors_skip_initial_selector (info->current_selector, max_selector);
|
2012-11-28 10:15:53 +00:00
|
|
|
if (info->current_selector == NULL)
|
|
|
|
{
|
|
|
|
/* Matches current node */
|
2020-07-02 16:30:55 +00:00
|
|
|
gtk_css_selector_matches_append (&exact_matches, info->match);
|
2012-11-29 14:29:14 +00:00
|
|
|
if (info->selector_match != NULL)
|
2012-11-29 19:52:46 +00:00
|
|
|
*info->selector_match = GUINT_TO_POINTER (tree_offset);
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
else
|
2020-04-25 19:08:13 +00:00
|
|
|
{
|
|
|
|
matched_infos[n_matched] = info;
|
|
|
|
n_matched++;
|
|
|
|
}
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-25 19:08:13 +00:00
|
|
|
remaining_infos[n_remaining] = info;
|
|
|
|
n_remaining++;
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:30:55 +00:00
|
|
|
if (!gtk_css_selector_matches_is_empty (&exact_matches))
|
2012-11-28 10:15:53 +00:00
|
|
|
{
|
2020-07-02 16:30:55 +00:00
|
|
|
gtk_css_selector_matches_append (&exact_matches, NULL); /* Null terminate */
|
2012-11-29 19:52:46 +00:00
|
|
|
res = array->len;
|
2020-07-02 16:30:55 +00:00
|
|
|
g_byte_array_append (array, (guint8 *) gtk_css_selector_matches_get_data (&exact_matches),
|
|
|
|
gtk_css_selector_matches_get_size (&exact_matches) * sizeof (gpointer));
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
2012-11-29 19:52:46 +00:00
|
|
|
else
|
|
|
|
res = GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET;
|
2020-07-02 16:30:55 +00:00
|
|
|
gtk_css_selector_matches_clear (&exact_matches);
|
2012-11-29 19:52:46 +00:00
|
|
|
get_tree (array, tree_offset)->matches_offset = res;
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2020-04-25 19:08:13 +00:00
|
|
|
res = subdivide_infos (array, matched_infos, n_matched, tree_offset);
|
2012-11-29 19:52:46 +00:00
|
|
|
get_tree (array, tree_offset)->previous_offset = res;
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2020-04-25 19:08:13 +00:00
|
|
|
res = subdivide_infos (array, remaining_infos, n_remaining, parent_offset);
|
2012-11-29 19:52:46 +00:00
|
|
|
get_tree (array, tree_offset)->sibling_offset = res;
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2012-12-06 22:48:11 +00:00
|
|
|
g_hash_table_unref (ht);
|
2012-11-29 18:10:57 +00:00
|
|
|
|
2012-11-29 19:52:46 +00:00
|
|
|
return tree_offset;
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct _GtkCssSelectorTreeBuilder {
|
2020-04-25 19:08:13 +00:00
|
|
|
GArray *infos;
|
2012-11-28 10:15:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GtkCssSelectorTreeBuilder *
|
|
|
|
_gtk_css_selector_tree_builder_new (void)
|
|
|
|
{
|
2020-04-25 19:08:13 +00:00
|
|
|
GtkCssSelectorTreeBuilder *builder = g_new0 (GtkCssSelectorTreeBuilder, 1);
|
|
|
|
|
|
|
|
builder->infos = g_array_new (FALSE, TRUE, sizeof (GtkCssSelectorRuleSetInfo));
|
|
|
|
|
|
|
|
return builder;
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_css_selector_tree_builder_free (GtkCssSelectorTreeBuilder *builder)
|
|
|
|
{
|
2020-04-25 19:08:13 +00:00
|
|
|
g_array_free (builder->infos, TRUE);
|
2012-11-28 10:15:53 +00:00
|
|
|
g_free (builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_css_selector_tree_builder_add (GtkCssSelectorTreeBuilder *builder,
|
|
|
|
GtkCssSelector *selectors,
|
2012-11-29 14:29:14 +00:00
|
|
|
GtkCssSelectorTree **selector_match,
|
2012-11-28 10:15:53 +00:00
|
|
|
gpointer match)
|
|
|
|
{
|
2020-04-25 19:08:13 +00:00
|
|
|
GtkCssSelectorRuleSetInfo *info;
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2020-04-25 19:08:13 +00:00
|
|
|
g_array_set_size (builder->infos, builder->infos->len + 1);
|
|
|
|
info = &g_array_index (builder->infos, GtkCssSelectorRuleSetInfo, builder->infos->len - 1);
|
2012-11-29 14:01:47 +00:00
|
|
|
info->match = match;
|
2012-11-28 10:15:53 +00:00
|
|
|
info->current_selector = selectors;
|
2012-11-29 14:29:14 +00:00
|
|
|
info->selector_match = selector_match;
|
2012-11-28 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 19:52:46 +00:00
|
|
|
/* Convert all offsets to node-relative */
|
|
|
|
static void
|
|
|
|
fixup_offsets (GtkCssSelectorTree *tree, guint8 *data)
|
|
|
|
{
|
|
|
|
while (tree != NULL)
|
|
|
|
{
|
|
|
|
if (tree->parent_offset != GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET)
|
|
|
|
tree->parent_offset -= ((guint8 *)tree - data);
|
|
|
|
|
|
|
|
if (tree->previous_offset != GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET)
|
|
|
|
tree->previous_offset -= ((guint8 *)tree - data);
|
|
|
|
|
|
|
|
if (tree->sibling_offset != GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET)
|
|
|
|
tree->sibling_offset -= ((guint8 *)tree - data);
|
|
|
|
|
|
|
|
if (tree->matches_offset != GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET)
|
|
|
|
tree->matches_offset -= ((guint8 *)tree - data);
|
|
|
|
|
|
|
|
fixup_offsets ((GtkCssSelectorTree *)gtk_css_selector_tree_get_previous (tree), data);
|
|
|
|
|
|
|
|
tree = (GtkCssSelectorTree *)gtk_css_selector_tree_get_sibling (tree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:15:53 +00:00
|
|
|
GtkCssSelectorTree *
|
|
|
|
_gtk_css_selector_tree_builder_build (GtkCssSelectorTreeBuilder *builder)
|
|
|
|
{
|
|
|
|
GtkCssSelectorTree *tree;
|
2012-11-29 19:52:46 +00:00
|
|
|
GByteArray *array;
|
|
|
|
guint8 *data;
|
|
|
|
guint len;
|
2020-04-25 19:08:13 +00:00
|
|
|
guint i;
|
|
|
|
GtkCssSelectorRuleSetInfo **infos_array;
|
2012-11-29 19:52:46 +00:00
|
|
|
|
|
|
|
array = g_byte_array_new ();
|
2020-04-25 19:08:13 +00:00
|
|
|
|
|
|
|
infos_array = g_alloca (sizeof (GtkCssSelectorRuleSetInfo *) * builder->infos->len);
|
|
|
|
for (i = 0; i < builder->infos->len; i++)
|
|
|
|
infos_array[i] = &g_array_index (builder->infos, GtkCssSelectorRuleSetInfo, i);
|
|
|
|
|
|
|
|
subdivide_infos (array, infos_array, builder->infos->len, GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET);
|
2012-11-29 19:52:46 +00:00
|
|
|
|
|
|
|
len = array->len;
|
|
|
|
data = g_byte_array_free (array, FALSE);
|
|
|
|
|
|
|
|
/* shrink to final size */
|
|
|
|
data = g_realloc (data, len);
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2012-11-29 19:52:46 +00:00
|
|
|
tree = (GtkCssSelectorTree *)data;
|
|
|
|
|
|
|
|
fixup_offsets (tree, data);
|
|
|
|
|
|
|
|
/* Convert offsets to final pointers */
|
2020-04-25 19:08:13 +00:00
|
|
|
for (i = 0; i < builder->infos->len; i++)
|
2012-11-29 19:52:46 +00:00
|
|
|
{
|
2020-04-25 19:08:13 +00:00
|
|
|
GtkCssSelectorRuleSetInfo *info = &g_array_index (builder->infos, GtkCssSelectorRuleSetInfo, i);
|
|
|
|
|
2012-11-29 19:52:46 +00:00
|
|
|
if (info->selector_match)
|
|
|
|
*info->selector_match = (GtkCssSelectorTree *)(data + GPOINTER_TO_UINT (*info->selector_match));
|
|
|
|
}
|
2012-11-28 10:15:53 +00:00
|
|
|
|
2020-04-25 19:08:13 +00:00
|
|
|
|
2012-11-28 10:15:53 +00:00
|
|
|
#ifdef PRINT_TREE
|
2020-01-24 16:50:40 +00:00
|
|
|
{
|
|
|
|
GString *s = g_string_new ("");
|
|
|
|
_gtk_css_selector_tree_print (tree, s, "");
|
|
|
|
g_print ("%s", s->str);
|
|
|
|
g_string_free (s, TRUE);
|
|
|
|
}
|
2012-11-28 10:15:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|