From 1e592489b152d305a44c3049ab7e263f2be9f6f7 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sun, 16 Jan 2005 00:03:45 +0000 Subject: [project @ 2005-01-16 00:03:45 by jmb] Create interface for duplication and destruction of css_style structs. svn path=/import/netsurf/; revision=1450 --- css/css.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ css/css.h | 4 +++- css/ruleset.c | 3 +-- 3 files changed, 71 insertions(+), 3 deletions(-) (limited to 'css') diff --git a/css/css.c b/css/css.c index 1083c9f69..d9cd777b6 100644 --- a/css/css.c +++ b/css/css.c @@ -467,6 +467,73 @@ void css_destroy(struct content *c) free(c->data.css.import_content); } +/** + * Duplicate a CSS style struct + * + * \param style The style to duplicate + * \return The duplicate style, or NULL if out of memory. + */ +struct css_style *css_duplicate_style(const struct css_style * const style) +{ + struct css_style *dup; + + assert(style); + + /* create duplicated style */ + dup = calloc(1, sizeof(struct css_style)); + if (!dup) + return NULL; + + /* copy all style information into duplicate style */ + memcpy(dup, style, sizeof(struct css_style)); + + /* duplicate strings, if in use */ + + /* background_image */ + if (dup->background_image.type == CSS_BACKGROUND_IMAGE_URI) { + dup->background_image.uri = NULL; + dup->background_image.uri = + strdup(style->background_image.uri); + if (!dup->background_image.uri) { + free(dup); + return NULL; + } + } + + /* list_style_image */ + if (dup->list_style_image.type == CSS_LIST_STYLE_IMAGE_URI) { + dup->list_style_image.uri = NULL; + dup->list_style_image.uri = + strdup(style->list_style_image.uri); + if (!dup->list_style_image.uri) { + if (dup->background_image.type == + CSS_BACKGROUND_IMAGE_URI) + free(dup->background_image.uri); + free(dup); + return NULL; + } + } + + return dup; +} + +/** + * Free a CSS style + * + * \param style The style to free + */ +void css_free_style(struct css_style *style) +{ + assert(style); + + if (style->background_image.type == CSS_BACKGROUND_IMAGE_URI) + free(style->background_image.uri); + + if (style->list_style_image.type == CSS_LIST_STYLE_IMAGE_URI) + free(style->list_style_image.uri); + + free(style); +} /** * Create a new struct css_node. diff --git a/css/css.h b/css/css.h index cc5e9bbd6..9c02cf26e 100644 --- a/css/css.h +++ b/css/css.h @@ -112,7 +112,7 @@ typedef enum { CSS_VERTICAL_ALIGN_PERCENT, CSS_VERTICAL_ALIGN_NOT_SET } css_vertical_align_type; - + struct css_counter { const char *name; css_list_style_type style; @@ -560,6 +560,8 @@ const char *css_parser_TokenName(int tokenType); #endif void css_get_style(struct content *c, xmlNode *n, struct css_style * style); +struct css_style *css_duplicate_style(const struct css_style * const style); +void css_free_style(struct css_style *style); void css_cascade(struct css_style * const style, const struct css_style * const apply); void css_merge(struct css_style * const style, diff --git a/css/ruleset.c b/css/ruleset.c index c2ea45618..50ed75743 100644 --- a/css/ruleset.c +++ b/css/ruleset.c @@ -483,13 +483,12 @@ void css_add_ruleset(struct content *c, if (!found) { /* not present: construct a new struct css_style */ LOG(("constructing new style")); - style = malloc(sizeof *style); + style = css_duplicate_style(&css_empty_style); if (!style) { /** \todo report to user */ css_free_selector(sel); return; } - memcpy(style, &css_empty_style, sizeof(*style)); sel->style = style; sel->next = n; if (prev) -- cgit v1.2.3