From c5911904de7f412b67bafc8fa9bb57a0dbe6a881 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sun, 25 Jan 2009 18:59:53 +0000 Subject: Beginnings of specificity. Note that we store the specificity on each simple selector. Thus the total specificity for a combinator chain is obtained by summing the specificity of each chain member. TODO: distinguish between pseudo classes and elements. svn path=/trunk/libcss/; revision=6267 --- src/stylesheet.c | 30 ++++++++++++++++++++++++++++-- src/stylesheet.h | 4 ++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/stylesheet.c b/src/stylesheet.c index c8eb2e7..34439ec 100644 --- a/src/stylesheet.c +++ b/src/stylesheet.c @@ -395,8 +395,12 @@ css_error css_stylesheet_selector_create(css_stylesheet *sheet, sel->data.name = name; sel->data.value = NULL; - /** \todo specificity */ - sel->specificity = 0; + /* Initial specificity -- 1 for an element, 0 for universal */ + if (name->len != 1 || name->data[0] != '*') + sel->specificity = CSS_SPECIFICITY_D; + else + sel->specificity = 0; + sel->data.comb = CSS_COMBINATOR_NONE; *selector = sel; @@ -493,6 +497,28 @@ css_error css_stylesheet_selector_append_specific(css_stylesheet *sheet, (*parent) = temp; + /* Update parent's specificity */ + switch (detail->type) { + case CSS_SELECTOR_CLASS: + case CSS_SELECTOR_ATTRIBUTE: + case CSS_SELECTOR_ATTRIBUTE_EQUAL: + case CSS_SELECTOR_ATTRIBUTE_DASHMATCH: + case CSS_SELECTOR_ATTRIBUTE_INCLUDES: + (*parent)->specificity += CSS_SPECIFICITY_C; + break; + case CSS_SELECTOR_ID: + (*parent)->specificity += CSS_SPECIFICITY_B; + break; + case CSS_SELECTOR_PSEUDO: + /** \todo distinguish between pseudo classes and elements */ + /* Assume pseudo class for now */ + (*parent)->specificity += CSS_SPECIFICITY_C; + break; + case CSS_SELECTOR_ELEMENT: + (*parent)->specificity += CSS_SPECIFICITY_D; + break; + } + return CSS_OK; } diff --git a/src/stylesheet.h b/src/stylesheet.h index 532afc0..48b08c2 100644 --- a/src/stylesheet.h +++ b/src/stylesheet.h @@ -63,6 +63,10 @@ struct css_selector { css_rule *rule; /**< Owning rule */ +#define CSS_SPECIFICITY_A 0x01000000 +#define CSS_SPECIFICITY_B 0x00010000 +#define CSS_SPECIFICITY_C 0x00000100 +#define CSS_SPECIFICITY_D 0x00000001 uint32_t specificity; /**< Specificity of selector */ css_selector_detail data; /**< Selector data */ -- cgit v1.2.3