From 94ff706d99220d915be8cedcda6b93ead998f5b8 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Wed, 1 Jul 2009 10:35:37 +0000 Subject: Make all URIs absolute svn path=/trunk/libcss/; revision=8228 --- include/libcss/stylesheet.h | 14 ++++++++++++++ src/parse/properties/aural.c | 25 ++++++++++++++++++++----- src/parse/properties/background.c | 14 +++++++++++--- src/parse/properties/generated_list.c | 33 +++++++++++++++++++++++++-------- src/parse/properties/ui.c | 13 +++++++++++-- src/stylesheet.c | 11 ++++++++++- src/stylesheet.h | 3 +++ test/css21.c | 18 ++++++++++++++++-- test/parse-auto.c | 16 ++++++++++++++-- test/parse2-auto.c | 14 +++++++++++++- test/select-auto.c | 14 +++++++++++++- 11 files changed, 150 insertions(+), 25 deletions(-) diff --git a/include/libcss/stylesheet.h b/include/libcss/stylesheet.h index df95e17..bdb45fa 100644 --- a/include/libcss/stylesheet.h +++ b/include/libcss/stylesheet.h @@ -11,11 +11,25 @@ #include #include +/** + * Callback to resolve an URL + * + * \param pw Client data + * \param dict String internment context + * \param base Base URI (absolute) + * \param rel URL to resolve, either absolute or relative to base + * \param abs Pointer to location to receive result + * \return CSS_OK on success, appropriate error otherwise. + */ +typedef css_error (*css_url_resolution_fn)(void *pw, lwc_context *dict, + const char *base, lwc_string *rel, lwc_string **abs); + 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, css_allocator_fn alloc, void *alloc_pw, + css_url_resolution_fn resolve, void *resolve_pw, css_stylesheet **stylesheet); css_error css_stylesheet_destroy(css_stylesheet *sheet); diff --git a/src/parse/properties/aural.c b/src/parse/properties/aural.c index e87d7e3..b5b3c90 100644 --- a/src/parse/properties/aural.c +++ b/src/parse/properties/aural.c @@ -949,7 +949,14 @@ css_error parse_play_during(css_language *c, int modifiers; value = PLAY_DURING_URI; - uri = token->idata; + + error = c->sheet->resolve(c->sheet->resolve_pw, + c->sheet->dictionary, c->sheet->url, + token->idata, &uri); + if (error != CSS_OK) { + *ctx = orig_ctx; + return error; + } for (modifiers = 0; modifiers < 2; modifiers++) { consumeWhitespace(vector, ctx); @@ -999,7 +1006,7 @@ css_error parse_play_during(css_language *c, memcpy((*result)->bytecode, &opv, sizeof(opv)); if ((flags & FLAG_INHERIT) == false && (value & PLAY_DURING_TYPE_MASK) == PLAY_DURING_URI) { - lwc_context_string_ref(c->sheet->dictionary, uri); + /* Don't ref URI -- we want to pass ownership to the bytecode */ memcpy((uint8_t *) (*result)->bytecode + sizeof(opv), &uri, sizeof(lwc_string *)); } @@ -1814,6 +1821,7 @@ css_error parse_cue_common(css_language *c, uint16_t value = 0; uint32_t opv; uint32_t required_size; + lwc_string *uri = NULL; /* URI | IDENT (none, inherit) */ token = parserutils_vector_iterate(vector, ctx); @@ -1831,6 +1839,14 @@ css_error parse_cue_common(css_language *c, value = CUE_AFTER_NONE; } else if (token->type == CSS_TOKEN_URI) { value = CUE_AFTER_URI; + + error = c->sheet->resolve(c->sheet->resolve_pw, + c->sheet->dictionary, c->sheet->url, + token->idata, &uri); + if (error != CSS_OK) { + *ctx = orig_ctx; + return error; + } } else { *ctx = orig_ctx; return CSS_INVALID; @@ -1852,10 +1868,9 @@ css_error parse_cue_common(css_language *c, /* Copy the bytecode to it */ memcpy((*result)->bytecode, &opv, sizeof(opv)); if ((flags & FLAG_INHERIT) == false && value == CUE_AFTER_URI) { - lwc_context_string_ref(c->sheet->dictionary, token->idata); + /* Don't ref URI -- we want to pass ownership to the bytecode */ memcpy((uint8_t *) (*result)->bytecode + sizeof(opv), - &token->idata, - sizeof(lwc_string *)); + &uri, sizeof(lwc_string *)); } return CSS_OK; diff --git a/src/parse/properties/background.c b/src/parse/properties/background.c index 0ee4dbb..bec2b2f 100644 --- a/src/parse/properties/background.c +++ b/src/parse/properties/background.c @@ -400,6 +400,7 @@ css_error parse_background_image(css_language *c, uint16_t value = 0; uint32_t opv; uint32_t required_size; + lwc_string *uri = NULL; /* URI | IDENT (none, inherit) */ token = parserutils_vector_iterate(vector, ctx); @@ -417,6 +418,14 @@ css_error parse_background_image(css_language *c, value = BACKGROUND_IMAGE_NONE; } else if (token->type == CSS_TOKEN_URI) { value = BACKGROUND_IMAGE_URI; + + error = c->sheet->resolve(c->sheet->resolve_pw, + c->sheet->dictionary, c->sheet->url, + token->idata, &uri); + if (error != CSS_OK) { + *ctx = orig_ctx; + return error; + } } else { *ctx = orig_ctx; return CSS_INVALID; @@ -438,10 +447,9 @@ css_error parse_background_image(css_language *c, /* Copy the bytecode to it */ memcpy((*result)->bytecode, &opv, sizeof(opv)); if ((flags & FLAG_INHERIT) == false && value == BACKGROUND_IMAGE_URI) { - lwc_context_string_ref(c->sheet->dictionary, token->idata); + /* Don't ref URI -- we want to pass ownership to the bytecode */ memcpy((uint8_t *) (*result)->bytecode + sizeof(opv), - &token->idata, - sizeof(lwc_string *)); + &uri, sizeof(lwc_string *)); } return CSS_OK; diff --git a/src/parse/properties/generated_list.c b/src/parse/properties/generated_list.c index 1183558..3efe305 100644 --- a/src/parse/properties/generated_list.c +++ b/src/parse/properties/generated_list.c @@ -378,6 +378,7 @@ css_error parse_list_style_image(css_language *c, uint16_t value = 0; uint32_t opv; uint32_t required_size; + lwc_string *uri = NULL; /* URI | IDENT (none, inherit) */ token = parserutils_vector_iterate(vector, ctx); @@ -395,6 +396,14 @@ css_error parse_list_style_image(css_language *c, value = LIST_STYLE_IMAGE_NONE; } else if (token->type == CSS_TOKEN_URI) { value = LIST_STYLE_IMAGE_URI; + + error = c->sheet->resolve(c->sheet->resolve_pw, + c->sheet->dictionary, c->sheet->url, + token->idata, &uri); + if (error != CSS_OK) { + *ctx = orig_ctx; + return error; + } } else { *ctx = orig_ctx; return CSS_INVALID; @@ -416,10 +425,9 @@ css_error parse_list_style_image(css_language *c, /* Copy the bytecode to it */ memcpy((*result)->bytecode, &opv, sizeof(opv)); if ((flags & FLAG_INHERIT) == false && value == LIST_STYLE_IMAGE_URI) { - lwc_context_string_ref(c->sheet->dictionary, token->idata); + /* Don't ref URI -- we want to pass ownership to the bytecode */ memcpy((uint8_t *) (*result)->bytecode + sizeof(opv), - &token->idata, - sizeof(lwc_string *)); + &uri, sizeof(lwc_string *)); } return CSS_OK; @@ -874,6 +882,8 @@ css_error parse_content_list(css_language *c, offset += sizeof(token->idata); } else if (token->type == CSS_TOKEN_URI) { + lwc_string *uri; + opv = CONTENT_URI; if (first == false) { @@ -886,13 +896,20 @@ css_error parse_content_list(css_language *c, } if (buffer != NULL) { - lwc_context_string_ref(c->sheet->dictionary, - token->idata); - memcpy(buffer + offset, &token->idata, - sizeof(token->idata)); + error = c->sheet->resolve(c->sheet->resolve_pw, + c->sheet->dictionary, c->sheet->url, + token->idata, &uri); + if (error != CSS_OK) { + *ctx = orig_ctx; + return error; + } + + /* Don't ref URI -- we want to pass ownership + * to the bytecode */ + memcpy(buffer + offset, &uri, sizeof(uri)); } - offset += sizeof(token->idata); + offset += sizeof(uri); } else if (token->type == CSS_TOKEN_FUNCTION && token->ilower == c->strings[ATTR]) { opv = CONTENT_ATTR; diff --git a/src/parse/properties/ui.c b/src/parse/properties/ui.c index 118e835..4a6c34f 100644 --- a/src/parse/properties/ui.c +++ b/src/parse/properties/ui.c @@ -204,7 +204,15 @@ css_error parse_cursor(css_language *c, /* URI* */ while (token != NULL && token->type == CSS_TOKEN_URI) { - lwc_string *uri = token->idata; + lwc_string *uri; + + error = c->sheet->resolve(c->sheet->resolve_pw, + c->sheet->dictionary, c->sheet->url, + token->idata, &uri); + if (error != CSS_OK) { + *ctx = orig_ctx; + return error; + } if (first == false) { opv = CURSOR_URI; @@ -212,7 +220,8 @@ css_error parse_cursor(css_language *c, ptr += sizeof(opv); } - lwc_context_string_ref(c->sheet->dictionary, uri); + /* Don't ref URI -- we want to pass ownership to the + * bytecode */ memcpy(ptr, &uri, sizeof(uri)); ptr += sizeof(uri); diff --git a/src/stylesheet.c b/src/stylesheet.c index 9afed2b..bfd159f 100644 --- a/src/stylesheet.c +++ b/src/stylesheet.c @@ -31,6 +31,8 @@ static css_error _remove_selectors(css_stylesheet *sheet, css_rule *rule); * \param dict Dictionary in which to intern strings * \param alloc Memory (de)allocation function * \param alloc_pw Client private data for alloc + * \param resolve URL resolution function + * \param resolve_pw Client private data for resolve * \param stylesheet Pointer to location to receive stylesheet * \return CSS_OK on success, * CSS_BADPARM on bad parameters, @@ -41,6 +43,7 @@ css_error css_stylesheet_create(css_language_level level, css_origin origin, uint64_t media, 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) { css_parser_optparams params; @@ -48,7 +51,8 @@ css_error css_stylesheet_create(css_language_level level, css_stylesheet *sheet; size_t len; - if (url == NULL || alloc == NULL || stylesheet == NULL) + if (url == NULL || alloc == NULL || + resolve == NULL || stylesheet == NULL) return CSS_BADPARM; sheet = alloc(NULL, sizeof(css_stylesheet), alloc_pw); @@ -115,6 +119,7 @@ css_error css_stylesheet_create(css_language_level level, return CSS_NOMEM; } memcpy(sheet->url, url, len); + sheet->url[len] = '\0'; if (title != NULL) { len = strlen(title) + 1; @@ -128,11 +133,15 @@ css_error css_stylesheet_create(css_language_level level, return CSS_NOMEM; } memcpy(sheet->title, title, len); + sheet->title[len] = '\0'; } sheet->origin = origin; sheet->media = media; + sheet->resolve = resolve; + sheet->resolve_pw = resolve_pw; + sheet->alloc = alloc; sheet->pw = alloc_pw; diff --git a/src/stylesheet.h b/src/stylesheet.h index f3b2079..4ac560c 100644 --- a/src/stylesheet.h +++ b/src/stylesheet.h @@ -175,6 +175,9 @@ struct css_stylesheet { bool inline_style; /**< Is an inline style */ + css_url_resolution_fn resolve; /**< URL resolution function */ + void *resolve_pw; /**< Private word */ + css_allocator_fn alloc; /**< Allocation function */ void *pw; /**< Private word */ }; diff --git a/test/css21.c b/test/css21.c index d3bf96d..f288720 100644 --- a/test/css21.c +++ b/test/css21.c @@ -20,6 +20,18 @@ static void *myrealloc(void *ptr, size_t len, void *pw) return realloc(ptr, len); } +static css_error resolve_url(void *pw, lwc_context *dict, + const char *base, lwc_string *rel, lwc_string **abs) +{ + UNUSED(pw); + UNUSED(base); + + /* About as useless as possible */ + *abs = lwc_context_string_ref(dict, rel); + + return CSS_OK; +} + int main(int argc, char **argv) { css_stylesheet *sheet; @@ -47,7 +59,8 @@ int main(int argc, char **argv) assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", argv[2], NULL, CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, false, - false, ctx, myrealloc, NULL, &sheet) == CSS_OK); + false, ctx, myrealloc, NULL, + resolve_url, NULL, &sheet) == CSS_OK); fp = fopen(argv[2], "rb"); if (fp == NULL) { @@ -104,7 +117,8 @@ int main(int argc, char **argv) assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", buf, NULL, CSS_ORIGIN_AUTHOR, media, false, false, ctx, myrealloc, - NULL, &import) == CSS_OK); + NULL, resolve_url, NULL, + &import) == CSS_OK); assert(css_stylesheet_data_done(import) == CSS_OK); diff --git a/test/parse-auto.c b/test/parse-auto.c index ebbc5e7..135696b 100644 --- a/test/parse-auto.c +++ b/test/parse-auto.c @@ -68,6 +68,18 @@ static void *myrealloc(void *data, size_t len, void *pw) return realloc(data, len); } +static css_error resolve_url(void *pw, lwc_context *dict, + const char *base, lwc_string *rel, lwc_string **abs) +{ + UNUSED(pw); + UNUSED(base); + + /* About as useless as possible */ + *abs = lwc_context_string_ref(dict, rel); + + return CSS_OK; +} + int main(int argc, char **argv) { line_ctx ctx; @@ -313,7 +325,7 @@ void run_test(const uint8_t *data, size_t len, exp_entry *exp, size_t explen) assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", "foo", NULL, CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, false, false, ctx, - myrealloc, NULL, &sheet) == CSS_OK); + myrealloc, NULL, resolve_url, NULL, &sheet) == CSS_OK); error = css_stylesheet_append_data(sheet, data, len); if (error != CSS_OK && error != CSS_NEEDDATA) { @@ -343,7 +355,7 @@ void run_test(const uint8_t *data, size_t len, exp_entry *exp, size_t explen) assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", buf, NULL, CSS_ORIGIN_AUTHOR, media, false, false, ctx, myrealloc, NULL, - &import) == CSS_OK); + 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 ace1d6c..57b1688 100644 --- a/test/parse2-auto.c +++ b/test/parse2-auto.c @@ -41,6 +41,18 @@ static void *myrealloc(void *data, size_t len, void *pw) return realloc(data, len); } +static css_error resolve_url(void *pw, lwc_context *dict, + const char *base, lwc_string *rel, lwc_string **abs) +{ + UNUSED(pw); + UNUSED(base); + + /* About as useless as possible */ + *abs = lwc_context_string_ref(dict, rel); + + return CSS_OK; +} + int main(int argc, char **argv) { line_ctx ctx; @@ -177,7 +189,7 @@ void run_test(const uint8_t *data, size_t len, const char *exp, size_t explen) assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", "foo", NULL, CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, false, false, ctx, - myrealloc, NULL, &sheet) == CSS_OK); + 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 3f82433..e884a18 100644 --- a/test/select-auto.c +++ b/test/select-auto.c @@ -145,6 +145,18 @@ static void *myrealloc(void *data, size_t len, void *pw) return realloc(data, len); } +static css_error resolve_url(void *pw, lwc_context *dict, + const char *base, lwc_string *rel, lwc_string **abs) +{ + UNUSED(pw); + UNUSED(base); + + /* About as useless as possible */ + *abs = lwc_context_string_ref(dict, rel); + + return CSS_OK; +} + int main(int argc, char **argv) { line_ctx ctx; @@ -451,7 +463,7 @@ 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, &sheet) == CSS_OK); + myrealloc, NULL, resolve_url, NULL, &sheet) == CSS_OK); /* Extend array of sheets and append new sheet to it */ temp = realloc(ctx->sheets, -- cgit v1.2.3