[librsvg: 2/3] Respect the contentStyleType attribute of the svg element



commit 37321229a71e62bb85fdc11f540f81b357368837
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Nov 3 11:12:09 2019 +0100

    Respect the contentStyleType attribute of the svg element

 rsvg_internals/src/document.rs  | 13 ++++++++++++-
 rsvg_internals/src/structure.rs |  9 +++++++++
 rsvg_internals/src/style.rs     | 15 +++++----------
 3 files changed, 26 insertions(+), 11 deletions(-)
---
diff --git a/rsvg_internals/src/document.rs b/rsvg_internals/src/document.rs
index 9d2fa2cd..3dc645c2 100644
--- a/rsvg_internals/src/document.rs
+++ b/rsvg_internals/src/document.rs
@@ -283,7 +283,18 @@ impl DocumentBuilder {
         }
 
         if parent.borrow().get_type() == NodeType::Style {
-            if parent.borrow().get_impl::<Style>().style_type() == StyleType::TextCss {
+            // If the "type" attribute is not present, fall back to the
+            // "contentStyleType" attribute of the svg element.
+            let style_type = parent.borrow().get_impl::<Style>().style_type().unwrap_or_else(|| {
+                if self.tree.is_some()
+                    && self.tree.as_ref().unwrap().borrow().get_type() == NodeType::Svg
+                {
+                    self.tree.as_ref().unwrap().borrow().get_impl::<Svg>().content_style_type()
+                } else {
+                    StyleType::TextCss
+                }
+            });
+            if style_type == StyleType::TextCss {
                 self.inline_css.push_str(text);
             }
         } else {
diff --git a/rsvg_internals/src/structure.rs b/rsvg_internals/src/structure.rs
index e8989506..ecb0a1ed 100644
--- a/rsvg_internals/src/structure.rs
+++ b/rsvg_internals/src/structure.rs
@@ -15,6 +15,7 @@ use crate::properties::ComputedValues;
 use crate::property_bag::PropertyBag;
 use crate::property_defs::Overflow;
 use crate::rect::RectangleExt;
+use crate::style::StyleType;
 use crate::viewbox::*;
 
 #[derive(Default)]
@@ -100,6 +101,7 @@ pub struct Svg {
     w: Option<LengthHorizontal>,
     h: Option<LengthVertical>,
     vbox: Option<ViewBox>,
+    content_style_type: Option<StyleType>,
 }
 
 impl Svg {
@@ -172,6 +174,10 @@ impl Svg {
             h.normalize(values, &params),
         )
     }
+
+    pub fn content_style_type(&self) -> StyleType {
+        self.content_style_type.unwrap_or(StyleType::TextCss)
+    }
 }
 
 impl NodeTrait for Svg {
@@ -196,6 +202,9 @@ impl NodeTrait for Svg {
                         Some(attr.parse_and_validate(value, LengthVertical::check_nonnegative)?)
                 }
                 expanded_name!(svg "viewBox") => self.vbox = attr.parse(value).map(Some)?,
+                expanded_name!(svg "contentStyleType") => {
+                    self.content_style_type = Some(attr.parse(value)?)
+                }
                 _ => (),
             }
         }
diff --git a/rsvg_internals/src/style.rs b/rsvg_internals/src/style.rs
index d67836bd..e128349f 100644
--- a/rsvg_internals/src/style.rs
+++ b/rsvg_internals/src/style.rs
@@ -9,6 +9,9 @@ use crate::property_bag::PropertyBag;
 /// Represents the syntax used in the <style> node.
 ///
 /// Currently only "text/css" is supported.
+///
+/// https://www.w3.org/TR/SVG11/styling.html#StyleElementTypeAttribute
+/// https://www.w3.org/TR/SVG11/styling.html#ContentStyleTypeAttribute
 #[derive(Copy, Clone, PartialEq)]
 pub enum StyleType {
     TextCss,
@@ -39,16 +42,8 @@ pub struct Style {
 }
 
 impl Style {
-    pub fn style_type(&self) -> StyleType {
-        // FIXME: See these:
-        //
-        // https://www.w3.org/TR/SVG11/styling.html#StyleElementTypeAttribute
-        // https://www.w3.org/TR/SVG11/styling.html#ContentStyleTypeAttribute
-        //
-        // If the "type" attribute is not present, we should fallback to the
-        // "contentStyleType" attribute of the svg element, which in turn
-        // defaults to "text/css".
-        self.type_.unwrap_or(StyleType::TextCss)
+    pub fn style_type(&self) -> Option<StyleType> {
+        self.type_
     }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]