css: Clean up tree_match implementations

A bunch of repeated code is broken out into the helper
gtk_css_selector_tree_match_previous().
This commit is contained in:
Alexander Larsson 2012-12-10 12:28:44 +01:00
parent eb4667b6e1
commit 089eafb468

View File

@ -195,6 +195,19 @@ gtk_css_selector_tree_get_sibling (const GtkCssSelectorTree *tree)
return gtk_css_selector_tree_at_offset (tree, tree->sibling_offset); return gtk_css_selector_tree_at_offset (tree, tree->sibling_offset);
} }
static void
gtk_css_selector_tree_match_previous (const GtkCssSelectorTree *tree,
const GtkCssMatcher *matcher,
GHashTable *res)
{
const GtkCssSelectorTree *prev;
for (prev = gtk_css_selector_tree_get_previous (tree);
prev != NULL;
prev = gtk_css_selector_tree_get_sibling (prev))
gtk_css_selector_tree_match (prev, matcher, res);
}
static GtkCssChange static GtkCssChange
gtk_css_selector_tree_get_previous_change (const GtkCssSelectorTree *tree, gtk_css_selector_tree_get_previous_change (const GtkCssSelectorTree *tree,
const GtkCssMatcher *matcher) const GtkCssMatcher *matcher)
@ -242,16 +255,12 @@ gtk_css_selector_descendant_tree_match (const GtkCssSelectorTree *tree,
GHashTable *res) GHashTable *res)
{ {
GtkCssMatcher ancestor; GtkCssMatcher ancestor;
const GtkCssSelectorTree *prev;
while (_gtk_css_matcher_get_parent (&ancestor, matcher)) while (_gtk_css_matcher_get_parent (&ancestor, matcher))
{ {
matcher = &ancestor; matcher = &ancestor;
for (prev = gtk_css_selector_tree_get_previous (tree); gtk_css_selector_tree_match_previous (tree, matcher, res);
prev != NULL;
prev = gtk_css_selector_tree_get_sibling (prev))
gtk_css_selector_tree_match (prev, matcher, res);
/* any matchers are dangerous here, as we may loop forever, but /* any matchers are dangerous here, as we may loop forever, but
we can terminate now as all possible matches have already been added */ we can terminate now as all possible matches have already been added */
@ -338,15 +347,11 @@ gtk_css_selector_child_tree_match (const GtkCssSelectorTree *tree,
GHashTable *res) GHashTable *res)
{ {
GtkCssMatcher parent; GtkCssMatcher parent;
const GtkCssSelectorTree *prev;
if (!_gtk_css_matcher_get_parent (&parent, matcher)) if (!_gtk_css_matcher_get_parent (&parent, matcher))
return; return;
for (prev = gtk_css_selector_tree_get_previous (tree); gtk_css_selector_tree_match_previous (tree, &parent, res);
prev != NULL;
prev = gtk_css_selector_tree_get_sibling (prev))
gtk_css_selector_tree_match (prev, &parent, res);
} }
@ -426,16 +431,12 @@ gtk_css_selector_sibling_tree_match (const GtkCssSelectorTree *tree,
GHashTable *res) GHashTable *res)
{ {
GtkCssMatcher previous; GtkCssMatcher previous;
const GtkCssSelectorTree *prev;
while (_gtk_css_matcher_get_previous (&previous, matcher)) while (_gtk_css_matcher_get_previous (&previous, matcher))
{ {
matcher = &previous; matcher = &previous;
for (prev = gtk_css_selector_tree_get_previous (tree); gtk_css_selector_tree_match_previous (tree, matcher, res);
prev != NULL;
prev = gtk_css_selector_tree_get_sibling (prev))
gtk_css_selector_tree_match (prev, matcher, res);
/* any matchers are dangerous here, as we may loop forever, but /* any matchers are dangerous here, as we may loop forever, but
we can terminate now as all possible matches have already been added */ we can terminate now as all possible matches have already been added */
@ -524,17 +525,13 @@ gtk_css_selector_adjacent_tree_match (const GtkCssSelectorTree *tree,
GHashTable *res) GHashTable *res)
{ {
GtkCssMatcher previous; GtkCssMatcher previous;
const GtkCssSelectorTree *prev;
if (!_gtk_css_matcher_get_previous (&previous, matcher)) if (!_gtk_css_matcher_get_previous (&previous, matcher))
return; return;
matcher = &previous; matcher = &previous;
for (prev = gtk_css_selector_tree_get_previous (tree); gtk_css_selector_tree_match_previous (tree, matcher, res);
prev != NULL;
prev = gtk_css_selector_tree_get_sibling (prev))
gtk_css_selector_tree_match (prev, matcher, res);
} }
static GtkCssChange static GtkCssChange
@ -612,7 +609,7 @@ gtk_css_selector_any_tree_match (const GtkCssSelectorTree *tree,
const GtkCssMatcher *matcher, const GtkCssMatcher *matcher,
GHashTable *res) GHashTable *res)
{ {
const GtkCssSelectorTree *prev, *prev2; const GtkCssSelectorTree *prev;
gtk_css_selector_tree_found_match (tree, res); gtk_css_selector_tree_found_match (tree, res);
@ -622,12 +619,7 @@ gtk_css_selector_any_tree_match (const GtkCssSelectorTree *tree,
{ {
if (prev->selector.class == &GTK_CSS_SELECTOR_DESCENDANT && if (prev->selector.class == &GTK_CSS_SELECTOR_DESCENDANT &&
_gtk_css_matcher_has_regions (matcher)) _gtk_css_matcher_has_regions (matcher))
{ gtk_css_selector_tree_match_previous (prev, matcher, res);
for (prev2 = gtk_css_selector_tree_get_previous (prev);
prev2 != NULL;
prev2 = gtk_css_selector_tree_get_sibling (prev2))
gtk_css_selector_tree_match (prev2, matcher, res);
}
gtk_css_selector_tree_match (prev, matcher, res); gtk_css_selector_tree_match (prev, matcher, res);
} }
@ -712,17 +704,12 @@ gtk_css_selector_name_tree_match (const GtkCssSelectorTree *tree,
const GtkCssMatcher *matcher, const GtkCssMatcher *matcher,
GHashTable *res) GHashTable *res)
{ {
const GtkCssSelectorTree *prev;
if (!_gtk_css_matcher_has_name (matcher, tree->selector.data)) if (!_gtk_css_matcher_has_name (matcher, tree->selector.data))
return; return;
gtk_css_selector_tree_found_match (tree, res); gtk_css_selector_tree_found_match (tree, res);
for (prev = gtk_css_selector_tree_get_previous (tree); gtk_css_selector_tree_match_previous (tree, matcher, res);
prev != NULL;
prev = gtk_css_selector_tree_get_sibling (prev))
gtk_css_selector_tree_match (prev, matcher, res);
} }
static GtkCssChange static GtkCssChange
@ -803,7 +790,7 @@ gtk_css_selector_region_tree_match (const GtkCssSelectorTree *tree,
const GtkCssMatcher *matcher, const GtkCssMatcher *matcher,
GHashTable *res) GHashTable *res)
{ {
const GtkCssSelectorTree *prev, *prev2; const GtkCssSelectorTree *prev;
if (!_gtk_css_matcher_has_region (matcher, tree->selector.data, 0)) if (!_gtk_css_matcher_has_region (matcher, tree->selector.data, 0))
return; return;
@ -815,12 +802,7 @@ gtk_css_selector_region_tree_match (const GtkCssSelectorTree *tree,
prev = gtk_css_selector_tree_get_sibling (prev)) prev = gtk_css_selector_tree_get_sibling (prev))
{ {
if (prev->selector.class == &GTK_CSS_SELECTOR_DESCENDANT) if (prev->selector.class == &GTK_CSS_SELECTOR_DESCENDANT)
{ gtk_css_selector_tree_match_previous (prev, matcher, res);
for (prev2 = gtk_css_selector_tree_get_previous (prev);
prev2 != NULL;
prev2 = gtk_css_selector_tree_get_sibling (prev2))
gtk_css_selector_tree_match (prev2, matcher, res);
}
gtk_css_selector_tree_match (prev, matcher, res); gtk_css_selector_tree_match (prev, matcher, res);
} }
@ -917,17 +899,12 @@ gtk_css_selector_class_tree_match (const GtkCssSelectorTree *tree,
const GtkCssMatcher *matcher, const GtkCssMatcher *matcher,
GHashTable *res) GHashTable *res)
{ {
const GtkCssSelectorTree *prev;
if (!_gtk_css_matcher_has_class (matcher, GPOINTER_TO_UINT (tree->selector.data))) if (!_gtk_css_matcher_has_class (matcher, GPOINTER_TO_UINT (tree->selector.data)))
return; return;
gtk_css_selector_tree_found_match (tree, res); gtk_css_selector_tree_found_match (tree, res);
for (prev = gtk_css_selector_tree_get_previous (tree); gtk_css_selector_tree_match_previous (tree, matcher, res);
prev != NULL;
prev = gtk_css_selector_tree_get_sibling (prev))
gtk_css_selector_tree_match (prev, matcher, res);
} }
static GtkCssChange static GtkCssChange
@ -1002,17 +979,12 @@ gtk_css_selector_id_tree_match (const GtkCssSelectorTree *tree,
const GtkCssMatcher *matcher, const GtkCssMatcher *matcher,
GHashTable *res) GHashTable *res)
{ {
const GtkCssSelectorTree *prev;
if (!_gtk_css_matcher_has_id (matcher, tree->selector.data)) if (!_gtk_css_matcher_has_id (matcher, tree->selector.data))
return; return;
gtk_css_selector_tree_found_match (tree, res); gtk_css_selector_tree_found_match (tree, res);
for (prev = gtk_css_selector_tree_get_previous (tree); gtk_css_selector_tree_match_previous (tree, matcher, res);
prev != NULL;
prev = gtk_css_selector_tree_get_sibling (prev))
gtk_css_selector_tree_match (prev, matcher, res);
} }
static GtkCssChange static GtkCssChange
@ -1112,17 +1084,13 @@ gtk_css_selector_pseudoclass_state_tree_match (const GtkCssSelectorTree *tree,
GHashTable *res) GHashTable *res)
{ {
GtkStateFlags state = GPOINTER_TO_UINT (tree->selector.data); GtkStateFlags state = GPOINTER_TO_UINT (tree->selector.data);
const GtkCssSelectorTree *prev;
if ((_gtk_css_matcher_get_state (matcher) & state) != state) if ((_gtk_css_matcher_get_state (matcher) & state) != state)
return; return;
gtk_css_selector_tree_found_match (tree, res); gtk_css_selector_tree_found_match (tree, res);
for (prev = gtk_css_selector_tree_get_previous (tree); gtk_css_selector_tree_match_previous (tree, matcher, res);
prev != NULL;
prev = gtk_css_selector_tree_get_sibling (prev))
gtk_css_selector_tree_match (prev, matcher, res);
} }
static GtkCssChange static GtkCssChange