From 476045efcd3d938d5ac506eafef3b1551fa8edac Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Wed, 11 Oct 2023 12:09:57 -0400 Subject: [PATCH] src/svgtiny.c: add svgtiny_parse_style_inline() function This function parses the contents of an inline style="..." attribute into a libcss stylesheet, or NULL if anything went wrong. --- src/svgtiny.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/svgtiny.c b/src/svgtiny.c index a325d97..db0d59b 100644 --- a/src/svgtiny.c +++ b/src/svgtiny.c @@ -42,6 +42,8 @@ static svgtiny_code svgtiny_parse_style_element(dom_element *style, struct svgtiny_parse_state state); +static css_stylesheet *svgtiny_parse_style_inline(const uint8_t *data, + size_t len); static svgtiny_code svgtiny_preparse_styles(dom_element *svg, struct svgtiny_parse_state state); static svgtiny_code svgtiny_parse_svg(dom_element *svg, @@ -834,6 +836,37 @@ svgtiny_code svgtiny_parse_style_element(dom_element *style, } +/** + * Parse the contents of an inline style and return (a pointer to) the + * corresponding stylesheet for use with css_select_style(). Returns + * NULL if anything goes wrong. + */ +css_stylesheet *svgtiny_parse_style_inline(const uint8_t *data, + size_t len) +{ + css_stylesheet *sheet; + css_error code; + + code = svgtiny_create_stylesheet(&sheet, true); + if (code != CSS_OK) { + return NULL; + } + + code = css_stylesheet_append_data(sheet, data, len); + if (code != CSS_OK && code != CSS_NEEDDATA) { + css_stylesheet_destroy(sheet); + return NULL; + } + + code = css_stylesheet_data_done(sheet); + if (code != CSS_OK) { + css_stylesheet_destroy(sheet); + return NULL; + } + + return sheet; +} + /** * Parse all