From 01d8e176628db2dc5a2a192ac7b097bd1998c8eb Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sun, 5 Dec 2010 14:20:58 +0000 Subject: Provide notification hook for imported stylesheets, to enable clients to parallelise their processing svn path=/trunk/libcss/; revision=10999 --- include/libcss/stylesheet.h | 23 +++++++++++++++++++++-- src/parse/language.c | 12 ++++++++++++ src/stylesheet.c | 6 ++++++ src/stylesheet.h | 3 +++ test/css21.c | 5 +++-- test/parse-auto.c | 4 ++-- test/parse2-auto.c | 2 +- test/select-auto.c | 2 +- 8 files changed, 49 insertions(+), 8 deletions(-) diff --git a/include/libcss/stylesheet.h b/include/libcss/stylesheet.h index 32e8d00..33d2a5c 100644 --- a/include/libcss/stylesheet.h +++ b/include/libcss/stylesheet.h @@ -19,7 +19,7 @@ extern "C" /** * Callback to resolve an URL * - * \param pw Client data + * \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 @@ -29,11 +29,30 @@ extern "C" typedef css_error (*css_url_resolution_fn)(void *pw, const char *base, lwc_string *rel, lwc_string **abs); +/** + * Callback to be notified of the need for an imported stylesheet + * + * \param pw Client data + * \param parent Stylesheet requesting the import + * \param url URL of the imported sheet + * \param media Applicable media for the imported sheet + * \return CSS_OK on success, appropriate error otherwise + * + * \note This function will be invoked for notification purposes + * only. The client may use this to trigger a parallel fetch + * of the imported stylesheet. The imported sheet must be + * registered with its parent using the post-parse import + * registration API. + */ +typedef css_error (*css_import_notification_fn)(void *pw, + css_stylesheet *parent, lwc_string *url, uint64_t media); + css_error css_stylesheet_create(css_language_level level, const char *charset, const char *url, const char *title, bool allow_quirks, bool inline_style, - css_allocator_fn alloc, void *alloc_pw, + css_allocator_fn alloc, void *alloc_pw, css_url_resolution_fn resolve, void *resolve_pw, + css_import_notification_fn, void *import_pw, css_stylesheet **stylesheet); css_error css_stylesheet_destroy(css_stylesheet *sheet); diff --git a/src/parse/language.c b/src/parse/language.c index cfe78e8..9605e5f 100644 --- a/src/parse/language.c +++ b/src/parse/language.c @@ -450,6 +450,18 @@ css_error handleStartAtRule(css_language *c, const parserutils_vector *vector) return error; } + /* Inform client of need for import */ + if (c->sheet->import != NULL) { + error = c->sheet->import(c->sheet->import_pw, + c->sheet, url, media); + if (error != CSS_OK) { + lwc_string_unref(url); + css_stylesheet_rule_destroy(c->sheet, + rule); + return error; + } + } + /* No longer care about url */ lwc_string_unref(url); diff --git a/src/stylesheet.c b/src/stylesheet.c index eed4735..f2bb81a 100644 --- a/src/stylesheet.c +++ b/src/stylesheet.c @@ -32,6 +32,8 @@ static size_t _rule_size(const css_rule *rule); * \param alloc_pw Client private data for alloc * \param resolve URL resolution function * \param resolve_pw Client private data for resolve + * \param import Import notification function + * \param import_pw Client private data for import * \param stylesheet Pointer to location to receive stylesheet * \return CSS_OK on success, * CSS_BADPARM on bad parameters, @@ -42,6 +44,7 @@ css_error css_stylesheet_create(css_language_level level, bool allow_quirks, bool inline_style, css_allocator_fn alloc, void *alloc_pw, css_url_resolution_fn resolve, void *resolve_pw, + css_import_notification_fn import, void *import_pw, css_stylesheet **stylesheet) { css_parser_optparams params; @@ -135,6 +138,9 @@ css_error css_stylesheet_create(css_language_level level, sheet->resolve = resolve; sheet->resolve_pw = resolve_pw; + sheet->import = import; + sheet->import_pw = import_pw; + sheet->alloc = alloc; sheet->pw = alloc_pw; diff --git a/src/stylesheet.h b/src/stylesheet.h index 2c6caac..fbd76b0 100644 --- a/src/stylesheet.h +++ b/src/stylesheet.h @@ -170,6 +170,9 @@ struct css_stylesheet { css_style *free_styles[4]; /**< Free styles: 16B buckets */ + css_import_notification_fn import; /**< Import notification function */ + void *import_pw; /**< Private word */ + css_url_resolution_fn resolve; /**< URL resolution function */ void *resolve_pw; /**< Private word */ diff --git a/test/css21.c b/test/css21.c index 5d6bb04..2ce4ebc 100644 --- a/test/css21.c +++ b/test/css21.c @@ -51,7 +51,8 @@ int main(int argc, char **argv) assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", argv[1], NULL, false, false, myrealloc, NULL, - resolve_url, NULL, &sheet) == CSS_OK); + resolve_url, NULL, NULL, NULL, + &sheet) == CSS_OK); fp = fopen(argv[1], "rb"); if (fp == NULL) { @@ -108,7 +109,7 @@ int main(int argc, char **argv) assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", buf, NULL, false, false, myrealloc, NULL, resolve_url, NULL, - &import) == CSS_OK); + NULL, 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 e050a08..ffff3f2 100644 --- a/test/parse-auto.c +++ b/test/parse-auto.c @@ -327,7 +327,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, false, false, myrealloc, NULL, resolve_url, NULL, - &sheet) == CSS_OK); + NULL, NULL, &sheet) == CSS_OK); error = css_stylesheet_append_data(sheet, data, len); if (error != CSS_OK && error != CSS_NEEDDATA) { @@ -357,7 +357,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, false, false, myrealloc, NULL, resolve_url, NULL, - &import) == CSS_OK); + NULL, 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 f649fd4..83bab14 100644 --- a/test/parse2-auto.c +++ b/test/parse2-auto.c @@ -191,7 +191,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, false, false, myrealloc, NULL, resolve_url, NULL, - &sheet) == CSS_OK); + NULL, 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 74f4a03..8642cfd 100644 --- a/test/select-auto.c +++ b/test/select-auto.c @@ -489,7 +489,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", false, false, myrealloc, NULL, - resolve_url, NULL, &sheet) == CSS_OK); + resolve_url, NULL, NULL, NULL, &sheet) == CSS_OK); /* Extend array of sheets and append new sheet to it */ temp = realloc(ctx->sheets, -- cgit v1.2.3