diff options
-rw-r--r-- | include/libcss/select.h | 6 | ||||
-rw-r--r-- | include/libcss/stylesheet.h | 5 | ||||
-rw-r--r-- | src/select/select.c | 118 | ||||
-rw-r--r-- | src/stylesheet.c | 42 | ||||
-rw-r--r-- | src/stylesheet.h | 3 | ||||
-rw-r--r-- | test/css21.c | 8 | ||||
-rw-r--r-- | test/parse-auto.c | 10 | ||||
-rw-r--r-- | test/parse2-auto.c | 4 | ||||
-rw-r--r-- | test/select-auto.c | 43 |
9 files changed, 116 insertions, 123 deletions
diff --git a/include/libcss/select.h b/include/libcss/select.h index 76bdcf5..931dcf1 100644 --- a/include/libcss/select.h +++ b/include/libcss/select.h @@ -84,9 +84,11 @@ css_error css_select_ctx_create(css_allocator_fn alloc, void *pw, css_error css_select_ctx_destroy(css_select_ctx *ctx); css_error css_select_ctx_append_sheet(css_select_ctx *ctx, - const css_stylesheet *sheet); + const css_stylesheet *sheet, + css_origin origin, uint64_t media); css_error css_select_ctx_insert_sheet(css_select_ctx *ctx, - const css_stylesheet *sheet, uint32_t index); + const css_stylesheet *sheet, uint32_t index, + css_origin origin, uint64_t media); css_error css_select_ctx_remove_sheet(css_select_ctx *ctx, const css_stylesheet *sheet); diff --git a/include/libcss/stylesheet.h b/include/libcss/stylesheet.h index 940e9b7..e70214d 100644 --- a/include/libcss/stylesheet.h +++ b/include/libcss/stylesheet.h @@ -26,8 +26,7 @@ typedef css_error (*css_url_resolution_fn)(void *pw, lwc_context *dict, css_error css_stylesheet_create(css_language_level level, const char *charset, const char *url, const char *title, - css_origin origin, uint64_t media, bool allow_quirks, - bool inline_style, lwc_context *dict, + bool allow_quirks, bool inline_style, lwc_context *dict, css_allocator_fn alloc, void *alloc_pw, css_url_resolution_fn resolve, void *resolve_pw, css_stylesheet **stylesheet); @@ -46,8 +45,6 @@ css_error css_stylesheet_get_language_level(css_stylesheet *sheet, css_language_level *level); css_error css_stylesheet_get_url(css_stylesheet *sheet, const char **url); css_error css_stylesheet_get_title(css_stylesheet *sheet, const char **title); -css_error css_stylesheet_get_origin(css_stylesheet *sheet, css_origin *origin); -css_error css_stylesheet_get_media(css_stylesheet *sheet, uint64_t *media); css_error css_stylesheet_quirks_allowed(css_stylesheet *sheet, bool *allowed); css_error css_stylesheet_used_quirks(css_stylesheet *sheet, bool *quirks); diff --git a/src/select/select.c b/src/select/select.c index 8224d86..7898d60 100644 --- a/src/select/select.c +++ b/src/select/select.c @@ -25,12 +25,21 @@ #undef DEBUG_CHAIN_MATCHING /** + * Container for stylesheet selection info + */ +typedef struct css_select_sheet { + const css_stylesheet *sheet; /**< Stylesheet */ + css_origin origin; /**< Stylesheet origin */ + uint64_t media; /**< Applicable media */ +} css_select_sheet; + +/** * CSS selection context */ struct css_select_ctx { uint32_t n_sheets; /**< Number of sheets */ - const css_stylesheet **sheets; /**< Array of sheets */ + css_select_sheet *sheets; /**< Array of sheets */ css_allocator_fn alloc; /**< Allocation routine */ void *pw; /**< Client-specific private data */ @@ -40,7 +49,8 @@ static css_error set_hint(css_select_state *state, uint32_t i); static css_error set_initial(css_select_state *state, uint32_t i, void *parent); static css_error select_from_sheet(css_select_ctx *ctx, - const css_stylesheet *sheet, css_select_state *state); + const css_stylesheet *sheet, css_origin origin, + css_select_state *state); static css_error intern_strings_for_sheet(css_select_ctx *ctx, const css_stylesheet *sheet, css_select_state *state); static css_error match_selectors_in_sheet(css_select_ctx *ctx, @@ -119,17 +129,21 @@ css_error css_select_ctx_destroy(css_select_ctx *ctx) /** * Append a stylesheet to a selection context * - * \param ctx The context to append to - * \param sheet The sheet to append + * \param ctx The context to append to + * \param sheet The sheet to append + * \param origin Origin of the sheet + * \param media Media types to which the sheet applies * \return CSS_OK on success, appropriate error otherwise */ css_error css_select_ctx_append_sheet(css_select_ctx *ctx, - const css_stylesheet *sheet) + const css_stylesheet *sheet, css_origin origin, + uint64_t media) { if (ctx == NULL || sheet == NULL) return CSS_BADPARM; - return css_select_ctx_insert_sheet(ctx, sheet, ctx->n_sheets); + return css_select_ctx_insert_sheet(ctx, sheet, ctx->n_sheets, + origin, media); } /** @@ -138,12 +152,15 @@ css_error css_select_ctx_append_sheet(css_select_ctx *ctx, * \param ctx The context to insert into * \param sheet Sheet to insert * \param index Index in context to insert sheet + * \param origin Origin of the sheet + * \param media Media types to which the sheet applies * \return CSS_OK on success, appropriate error otherwise */ css_error css_select_ctx_insert_sheet(css_select_ctx *ctx, - const css_stylesheet *sheet, uint32_t index) + const css_stylesheet *sheet, uint32_t index, + css_origin origin, uint64_t media) { - const css_stylesheet **temp; + css_select_sheet *temp; if (ctx == NULL || sheet == NULL) return CSS_BADPARM; @@ -158,7 +175,7 @@ css_error css_select_ctx_insert_sheet(css_select_ctx *ctx, return CSS_INVALID; temp = ctx->alloc(ctx->sheets, - (ctx->n_sheets + 1) * sizeof(css_stylesheet *), + (ctx->n_sheets + 1) * sizeof(css_select_sheet), ctx->pw); if (temp == NULL) return CSS_NOMEM; @@ -167,10 +184,12 @@ css_error css_select_ctx_insert_sheet(css_select_ctx *ctx, if (index < ctx->n_sheets) { memmove(&ctx->sheets[index + 1], &ctx->sheets[index], - (ctx->n_sheets - index) * sizeof(css_stylesheet *)); + (ctx->n_sheets - index) * sizeof(css_select_sheet)); } - ctx->sheets[index] = sheet; + ctx->sheets[index].sheet = sheet; + ctx->sheets[index].origin = origin; + ctx->sheets[index].media = media; ctx->n_sheets++; @@ -193,7 +212,7 @@ css_error css_select_ctx_remove_sheet(css_select_ctx *ctx, return CSS_BADPARM; for (index = 0; index < ctx->n_sheets; index++) { - if (ctx->sheets[index] == sheet) + if (ctx->sheets[index].sheet == sheet) break; } @@ -201,7 +220,7 @@ css_error css_select_ctx_remove_sheet(css_select_ctx *ctx, return CSS_INVALID; memmove(&ctx->sheets[index], &ctx->sheets[index + 1], - (ctx->n_sheets - index) * sizeof(css_stylesheet *)); + (ctx->n_sheets - index) * sizeof(css_select_sheet)); ctx->n_sheets--; @@ -243,7 +262,7 @@ css_error css_select_ctx_get_sheet(css_select_ctx *ctx, uint32_t index, if (index > ctx->n_sheets) return CSS_INVALID; - *sheet = ctx->sheets[index]; + *sheet = ctx->sheets[index].sheet; return CSS_OK; } @@ -301,9 +320,10 @@ css_error css_select_style(css_select_ctx *ctx, void *node, * from those which apply to our current media requirements and * are not disabled */ for (i = 0; i < ctx->n_sheets; i++) { - if ((ctx->sheets[i]->media & media) != 0 && - ctx->sheets[i]->disabled == false) { - error = select_from_sheet(ctx, ctx->sheets[i], &state); + if ((ctx->sheets[i].media & media) != 0 && + ctx->sheets[i].sheet->disabled == false) { + error = select_from_sheet(ctx, ctx->sheets[i].sheet, + ctx->sheets[i].origin, &state); if (error != CSS_OK) goto cleanup; } @@ -367,40 +387,51 @@ css_error css_select_style(css_select_ctx *ctx, void *node, error = CSS_OK; cleanup: - if (ctx->sheets[0] != NULL) { + if (ctx->n_sheets > 0 && ctx->sheets[0].sheet != NULL) { if (state.universal != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.universal); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.universal); if (state.first_child != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.first_child); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.first_child); if (state.link != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.link); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.link); if (state.visited != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.visited); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.visited); if (state.hover != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.hover); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.hover); if (state.active != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.active); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.active); if (state.focus != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.focus); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.focus); if (state.first_line != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.first_line); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.first_line); if (state.first_letter != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.first_letter); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.first_letter); if (state.before != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.before); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.before); if (state.after != NULL) - lwc_context_string_unref(ctx->sheets[0]->dictionary, - state.after); + lwc_context_string_unref( + ctx->sheets[0].sheet->dictionary, + state.after); } return error; } @@ -480,7 +511,7 @@ css_error set_initial(css_select_state *state, uint32_t i, void *parent) } css_error select_from_sheet(css_select_ctx *ctx, const css_stylesheet *sheet, - css_select_state *state) + css_origin origin, css_select_state *state) { const css_stylesheet *s = sheet; const css_rule *rule = s->rule_list; @@ -500,8 +531,7 @@ css_error select_from_sheet(css_select_ctx *ctx, const css_stylesheet *sheet, (const css_rule_import *) rule; if (import->sheet != NULL && - (import->sheet->media & - state->media) != 0) { + (import->media & state->media) != 0) { /* It's applicable, so process it */ s = import->sheet; rule = s->rule_list; @@ -515,7 +545,7 @@ css_error select_from_sheet(css_select_ctx *ctx, const css_stylesheet *sheet, /* Process this sheet */ state->sheet = s; - state->current_origin = s->origin; + state->current_origin = origin; error = intern_strings_for_sheet(ctx, s, state); if (error != CSS_OK) diff --git a/src/stylesheet.c b/src/stylesheet.c index e6d8ea7..a20ec14 100644 --- a/src/stylesheet.c +++ b/src/stylesheet.c @@ -25,8 +25,6 @@ static size_t _rule_size(const css_rule *rule); * \param charset The charset of the stylesheet data, or NULL to detect * \param url URL of stylesheet * \param title Title of stylesheet - * \param origin Origin of stylesheet - * \param media Media stylesheet applies to * \param allow_quirks Permit quirky parsing of stylesheets * \param inline_style This stylesheet is an inline style * \param dict Dictionary in which to intern strings @@ -41,8 +39,7 @@ static size_t _rule_size(const css_rule *rule); */ css_error css_stylesheet_create(css_language_level level, const char *charset, const char *url, const char *title, - css_origin origin, uint64_t media, bool allow_quirks, - bool inline_style, lwc_context *dict, + bool allow_quirks, bool inline_style, lwc_context *dict, css_allocator_fn alloc, void *alloc_pw, css_url_resolution_fn resolve, void *resolve_pw, css_stylesheet **stylesheet) @@ -136,9 +133,6 @@ css_error css_stylesheet_create(css_language_level level, memcpy(sheet->title, title, len); } - sheet->origin = origin; - sheet->media = media; - sheet->resolve = resolve; sheet->resolve_pw = resolve_pw; @@ -431,40 +425,6 @@ css_error css_stylesheet_get_title(css_stylesheet *sheet, const char **title) } /** - * Retrieve the origin of a stylesheet - * - * \param sheet The stylesheet to retrieve the origin of - * \param origin Pointer to location to receive origin - * \return CSS_OK on success, appropriate error otherwise - */ -css_error css_stylesheet_get_origin(css_stylesheet *sheet, css_origin *origin) -{ - if (sheet == NULL || origin == NULL) - return CSS_BADPARM; - - *origin = sheet->origin; - - return CSS_OK; -} - -/** - * Retrieve the media types associated with a stylesheet - * - * \param sheet The stylesheet to retrieve the media types for - * \param media Pointer to location to receive media types - * \return CSS_OK on success, appropriate error otherwise - */ -css_error css_stylesheet_get_media(css_stylesheet *sheet, uint64_t *media) -{ - if (sheet == NULL || media == NULL) - return CSS_BADPARM; - - *media = sheet->media; - - return CSS_OK; -} - -/** * Determine whether quirky parsing was permitted on a stylesheet * * \param sheet The stylesheet to consider diff --git a/src/stylesheet.h b/src/stylesheet.h index df095d9..10a9827 100644 --- a/src/stylesheet.h +++ b/src/stylesheet.h @@ -157,9 +157,6 @@ struct css_stylesheet { char *url; /**< URL of this sheet */ char *title; /**< Title of this sheet */ - css_origin origin; /**< Origin of stylesheet */ - uint64_t media; /**< Bitfield of media types */ - void *ownerNode; /**< Owning node in document */ css_rule *ownerRule; /**< Owning rule in parent */ diff --git a/test/css21.c b/test/css21.c index f288720..5e6c71a 100644 --- a/test/css21.c +++ b/test/css21.c @@ -58,8 +58,7 @@ int main(int argc, char **argv) for (count = 0; count < ITERATIONS; count++) { assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", argv[2], - NULL, CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, false, - false, ctx, myrealloc, NULL, + NULL, false, false, ctx, myrealloc, NULL, resolve_url, NULL, &sheet) == CSS_OK); fp = fopen(argv[2], "rb"); @@ -115,9 +114,8 @@ int main(int argc, char **argv) buf[lwc_string_length(url)] = '\0'; assert(css_stylesheet_create(CSS_LEVEL_21, - "UTF-8", buf, NULL, CSS_ORIGIN_AUTHOR, - media, false, false, ctx, myrealloc, - NULL, resolve_url, NULL, + "UTF-8", buf, NULL, false, false, ctx, + myrealloc, NULL, resolve_url, NULL, &import) == CSS_OK); assert(css_stylesheet_data_done(import) == diff --git a/test/parse-auto.c b/test/parse-auto.c index 135696b..070f8db 100644 --- a/test/parse-auto.c +++ b/test/parse-auto.c @@ -324,8 +324,8 @@ void run_test(const uint8_t *data, size_t len, exp_entry *exp, size_t explen) lwc_context_ref(ctx); assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", "foo", NULL, - CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, false, false, ctx, - myrealloc, NULL, resolve_url, NULL, &sheet) == CSS_OK); + false, false, ctx, myrealloc, NULL, resolve_url, NULL, + &sheet) == CSS_OK); error = css_stylesheet_append_data(sheet, data, len); if (error != CSS_OK && error != CSS_NEEDDATA) { @@ -353,9 +353,9 @@ void run_test(const uint8_t *data, size_t len, exp_entry *exp, size_t explen) buf[lwc_string_length(url)] = '\0'; assert(css_stylesheet_create(CSS_LEVEL_21, - "UTF-8", buf, NULL, CSS_ORIGIN_AUTHOR, - media, false, false, ctx, myrealloc, NULL, - resolve_url, NULL, &import) == CSS_OK); + "UTF-8", buf, NULL, false, false, ctx, + myrealloc, NULL, resolve_url, NULL, + &import) == CSS_OK); assert(css_stylesheet_register_import(sheet, import) == CSS_OK); diff --git a/test/parse2-auto.c b/test/parse2-auto.c index 57b1688..2a89ef4 100644 --- a/test/parse2-auto.c +++ b/test/parse2-auto.c @@ -188,8 +188,8 @@ void run_test(const uint8_t *data, size_t len, const char *exp, size_t explen) lwc_context_ref(ctx); assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", "foo", NULL, - CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, false, false, ctx, - myrealloc, NULL, resolve_url, NULL, &sheet) == CSS_OK); + false, false, ctx, myrealloc, NULL, resolve_url, NULL, + &sheet) == CSS_OK); error = css_stylesheet_append_data(sheet, data, len); if (error != CSS_OK && error != CSS_NEEDDATA) { diff --git a/test/select-auto.c b/test/select-auto.c index 2f3e028..b0d2594 100644 --- a/test/select-auto.c +++ b/test/select-auto.c @@ -33,6 +33,12 @@ typedef struct node { struct node *last_child; } node; +typedef struct sheet_ctx { + css_stylesheet *sheet; + css_origin origin; + uint64_t media; +} sheet_ctx; + typedef struct line_ctx { size_t explen; size_t expused; @@ -48,7 +54,7 @@ typedef struct line_ctx { uint32_t depth; uint32_t n_sheets; - css_stylesheet **sheets; + sheet_ctx *sheets; uint64_t media; uint32_t pseudo_element; @@ -235,8 +241,8 @@ bool handle_line(const char *data, size_t datalen, void *pw) } else if (ctx->insheet) { if (strncasecmp(data+1, "errors", 6) == 0) { assert(css_stylesheet_data_done( - ctx->sheets[ctx->n_sheets - 1]) - == CSS_OK); + ctx->sheets[ctx->n_sheets - 1].sheet) + == CSS_OK); ctx->intree = false; ctx->insheet = false; @@ -246,15 +252,15 @@ bool handle_line(const char *data, size_t datalen, void *pw) strncasecmp(data+1, "user", 4) == 0 || strncasecmp(data+1, "author", 6) == 0) { assert(css_stylesheet_data_done( - ctx->sheets[ctx->n_sheets - 1]) - == CSS_OK); + ctx->sheets[ctx->n_sheets - 1].sheet) + == CSS_OK); parse_sheet(ctx, data + 1, datalen - 1); } else { error = css_stylesheet_append_data( - ctx->sheets[ctx->n_sheets - 1], - (const uint8_t *) data, - datalen); + ctx->sheets[ctx->n_sheets - 1].sheet, + (const uint8_t *) data, + datalen); assert(error == CSS_OK || error == CSS_NEEDDATA); } @@ -290,7 +296,7 @@ bool handle_line(const char *data, size_t datalen, void *pw) parse_tree_data(ctx, data + 1, datalen - 1); } else if (ctx->insheet) { error = css_stylesheet_append_data( - ctx->sheets[ctx->n_sheets - 1], + ctx->sheets[ctx->n_sheets - 1].sheet, (const uint8_t *) data, datalen); assert(error == CSS_OK || error == CSS_NEEDDATA); } else if (ctx->inexp) { @@ -447,7 +453,7 @@ void parse_sheet(line_ctx *ctx, const char *data, size_t len) css_origin origin = CSS_ORIGIN_AUTHOR; uint64_t media = CSS_MEDIA_ALL; css_stylesheet *sheet; - css_stylesheet **temp; + sheet_ctx *temp; /* <origin> <media_list>? */ @@ -478,17 +484,19 @@ void parse_sheet(line_ctx *ctx, const char *data, size_t len) /** \todo How are we going to handle @import? */ assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", "foo", "foo", - origin, media, false, false, ctx->dict, - myrealloc, NULL, resolve_url, NULL, &sheet) == CSS_OK); + false, false, ctx->dict, myrealloc, NULL, + resolve_url, NULL, &sheet) == CSS_OK); /* Extend array of sheets and append new sheet to it */ temp = realloc(ctx->sheets, - (ctx->n_sheets + 1) * sizeof(css_stylesheet *)); + (ctx->n_sheets + 1) * sizeof(sheet_ctx)); assert(temp != NULL); ctx->sheets = temp; - ctx->sheets[ctx->n_sheets] = sheet; + ctx->sheets[ctx->n_sheets].sheet = sheet; + ctx->sheets[ctx->n_sheets].origin = origin; + ctx->sheets[ctx->n_sheets].media = media; ctx->n_sheets++; } @@ -656,8 +664,9 @@ void run_test(line_ctx *ctx, const char *exp, size_t explen) assert(css_select_ctx_create(myrealloc, NULL, &select) == CSS_OK); for (i = 0; i < ctx->n_sheets; i++) { - assert(css_select_ctx_append_sheet(select, ctx->sheets[i]) == - CSS_OK); + assert(css_select_ctx_append_sheet(select, + ctx->sheets[i].sheet, ctx->sheets[i].origin, + ctx->sheets[i].media) == CSS_OK); } assert(css_computed_style_create(myrealloc, NULL, &computed) == CSS_OK); @@ -685,7 +694,7 @@ void run_test(line_ctx *ctx, const char *exp, size_t explen) destroy_tree(ctx->tree); for (i = 0; i < ctx->n_sheets; i++) { - css_stylesheet_destroy(ctx->sheets[i]); + css_stylesheet_destroy(ctx->sheets[i].sheet); } ctx->tree = NULL; |