From 5134709a436559f89871d0dc5cac57c17aa7d3ab Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Wed, 14 Jan 2009 00:29:45 +0000 Subject: Create charset rule objects. svn path=/trunk/libcss/; revision=6059 --- src/parse/language.c | 30 ++++++++++++++++++++++++++++-- src/stylesheet.c | 26 ++++++++++++++++++++++++++ src/stylesheet.h | 5 ++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/parse/language.c b/src/parse/language.c index 78b2045..c583c51 100644 --- a/src/parse/language.c +++ b/src/parse/language.c @@ -359,18 +359,44 @@ css_error handleStartAtRule(css_language *c, const parserutils_vector *vector) if (atkeyword->ilower == c->strings[CHARSET]) { if (c->state == BEFORE_CHARSET) { + const css_token *charset; + css_rule *rule; + css_error error; + /* any0 = STRING */ if (ctx == 0) return CSS_INVALID; - token = parserutils_vector_iterate(vector, &ctx); - if (token == NULL || token->type != CSS_TOKEN_STRING) + charset = parserutils_vector_iterate(vector, &ctx); + if (charset == NULL || + charset->type != CSS_TOKEN_STRING) return CSS_INVALID; token = parserutils_vector_iterate(vector, &ctx); if (token != NULL) return CSS_INVALID; + error = css_stylesheet_rule_create(c->sheet, + CSS_RULE_CHARSET, &rule); + if (error != CSS_OK) + return error; + + error = css_stylesheet_rule_set_charset(c->sheet, rule, + charset->idata); + if (error != CSS_OK) { + css_stylesheet_rule_destroy(c->sheet, rule); + return error; + } + + error = css_stylesheet_add_rule(c->sheet, rule); + if (error != CSS_OK) { + css_stylesheet_rule_destroy(c->sheet, rule); + return error; + } + + /* Rule is now owned by the sheet, + * so no need to destroy it */ + c->state = BEFORE_RULES; } else { return CSS_INVALID; diff --git a/src/stylesheet.c b/src/stylesheet.c index 72c5f02..ce971bc 100644 --- a/src/stylesheet.c +++ b/src/stylesheet.c @@ -673,6 +673,32 @@ css_error css_stylesheet_rule_append_style(css_stylesheet *sheet, return CSS_OK; } +/** + * Set the charset of a CSS rule + * + * \param sheet The stylesheet context + * \param rule The rule to add to (must be of type CSS_RULE_CHARSET) + * \param charset The charset + * \return CSS_OK on success, appropriate error otherwise + */ +css_error css_stylesheet_rule_set_charset(css_stylesheet *sheet, + css_rule *rule, const parserutils_hash_entry *charset) +{ + css_rule_charset *r = (css_rule_charset *) rule; + + if (sheet == NULL || rule == NULL || charset == NULL) + return CSS_BADPARM; + + /* Ensure rule is a CSS_RULE_CHARSET */ + if (rule->type != CSS_RULE_CHARSET) + return CSS_INVALID; + + /* Set rule's encoding field */ + r->encoding = charset; + + return CSS_OK; +} + /** * Add a rule to a stylesheet * diff --git a/src/stylesheet.h b/src/stylesheet.h index f32bbe3..ff32d97 100644 --- a/src/stylesheet.h +++ b/src/stylesheet.h @@ -133,7 +133,7 @@ typedef struct css_rule_import { typedef struct css_rule_charset { css_rule base; - char *encoding; /** \todo use MIB enum? */ + const parserutils_hash_entry *encoding; /** \todo use MIB enum? */ } css_rule_charset; struct css_stylesheet { @@ -206,6 +206,9 @@ css_error css_stylesheet_rule_add_selector(css_stylesheet *sheet, css_error css_stylesheet_rule_append_style(css_stylesheet *sheet, css_rule *rule, css_style *style); +css_error css_stylesheet_rule_set_charset(css_stylesheet *sheet, + css_rule *rule, const parserutils_hash_entry *charset); + /** \todo registering other rule-type data with css_rules */ css_error css_stylesheet_add_rule(css_stylesheet *sheet, css_rule *rule); -- cgit v1.2.3