]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
src/svgtiny.c: add svgtiny_parse_style_inline() function
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 11 Oct 2023 16:09:57 +0000 (12:09 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 20 Nov 2023 01:37:04 +0000 (20:37 -0500)
This function parses the contents of an inline style="..." attribute
into a libcss stylesheet, or NULL if anything went wrong.

src/svgtiny.c

index a325d9720514cb4e43390ab70484e237c5a40601..db0d59b02cd72209a51f409d7f62aef338bb64e3 100644 (file)
@@ -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 <style> elements within a root <svg> element. This
  * should be called before svgtiny_parse_svg() because that function