[librsvg: 1/4] Save the value of the "style" attribute, and use it instead of poking at the pbag



commit 0c6ee42533c6e52bad088a105809df7517cfd50f
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri May 3 19:00:30 2019 -0500

    Save the value of the "style" attribute, and use it instead of poking at the pbag
    
    This will let us process the "style" attribute at the end of the
    loading process, so that we can remove the special case where NodeSvg
    stores a copy of its pbag so it can set the style attribute at the end.

 rsvg_internals/src/node.rs      | 52 ++++++++++++++++++++++++++---------------
 rsvg_internals/src/structure.rs | 11 +--------
 rsvg_internals/src/xml.rs       |  2 +-
 3 files changed, 35 insertions(+), 30 deletions(-)
---
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index 62a8375b..33c3fd46 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -154,6 +154,7 @@ pub struct NodeData {
     values: RefCell<ComputedValues>,
     cond: Cell<bool>,
     node_impl: Box<NodeTrait>,
+    style_attr: RefCell<String>,
 }
 
 pub type Node = tree_utils::Node<NodeData>;
@@ -294,6 +295,7 @@ impl Node {
             values: RefCell::new(ComputedValues::default()),
             cond: Cell::new(true),
             node_impl,
+            style_attr: RefCell::new(String::new()),
         };
 
         tree_utils::Node::<NodeData> {
@@ -367,7 +369,21 @@ impl Node {
         Ok(())
     }
 
+    fn save_style_attribute(&self, pbag: &PropertyBag<'_>) {
+        let mut style_attr = self.data.style_attr.borrow_mut();
+
+        for (attr, value) in pbag.iter() {
+            match attr {
+                Attribute::Style => style_attr.push_str(value),
+
+                _ => (),
+            }
+        }
+    }
+
     pub fn set_atts(&self, node: &RsvgNode, pbag: &PropertyBag<'_>, locale: &Locale) {
+        self.save_style_attribute(pbag);
+
         if let Err(e) = self
             .set_transform_attribute(pbag)
             .and_then(|_| self.parse_conditional_processing_attributes(pbag, locale))
@@ -531,34 +547,32 @@ impl Node {
         }
     }
 
-    /// Looks for the "style" attribute in the pbag, and applies CSS styles from it
-    fn set_style_attribute(&self, pbag: &PropertyBag<'_>) {
-        let mut important_styles = self.data.important_styles.borrow_mut();
+    /// Applies CSS styles from the saved value of the "style" attribute
+    fn set_style_attribute(&self) {
+        let mut style_attr = self.data.style_attr.borrow_mut();
 
-        for (attr, value) in pbag.iter() {
-            match attr {
-                Attribute::Style => {
-                    if let Err(e) = self
-                        .data
-                        .specified_values
-                        .borrow_mut()
-                        .parse_style_declarations(value, &mut important_styles)
-                    {
-                        self.set_error(e);
-                        break;
-                    }
-                }
+        if !style_attr.is_empty() {
+            let mut important_styles = self.data.important_styles.borrow_mut();
 
-                _ => (),
+            if let Err(e) = self
+                .data
+                .specified_values
+                .borrow_mut()
+                .parse_style_declarations(style_attr.as_str(), &mut important_styles)
+            {
+                self.set_error(e);
             }
+
+            style_attr.clear();
+            style_attr.shrink_to_fit();
         }
     }
 
     // Sets the node's specified values from the style-related attributes in the pbag.
     // Also applies CSS rules in our limited way based on the node's tag/class/id.
-    pub fn set_style(&self, css_rules: &CssRules, pbag: &PropertyBag<'_>) {
+    pub fn set_style(&self, css_rules: &CssRules) {
         self.set_css_styles(css_rules);
-        self.set_style_attribute(pbag);
+        self.set_style_attribute();
     }
 
     pub fn set_overridden_properties(&self) {
diff --git a/rsvg_internals/src/structure.rs b/rsvg_internals/src/structure.rs
index 6ddb079c..41e47ae0 100644
--- a/rsvg_internals/src/structure.rs
+++ b/rsvg_internals/src/structure.rs
@@ -113,7 +113,6 @@ pub struct NodeSvg {
     w: Cell<Option<LengthHorizontal>>,
     h: Cell<Option<LengthVertical>>,
     vbox: Cell<Option<ViewBox>>,
-    pbag: RefCell<Option<OwnedPropertyBag>>,
 }
 
 impl NodeSvg {
@@ -125,15 +124,11 @@ impl NodeSvg {
             w: Cell::new(None),
             h: Cell::new(None),
             vbox: Cell::new(None),
-            pbag: RefCell::new(None),
         }
     }
 
     pub fn set_delayed_style(&self, node: &RsvgNode, css_rules: &CssRules) {
-        if let Some(owned_pbag) = self.pbag.borrow().as_ref() {
-            let pbag = PropertyBag::from_owned(owned_pbag);
-            node.set_style(css_rules, &pbag);
-        }
+        node.set_style(css_rules);
     }
 
     pub fn get_size(&self, values: &ComputedValues, dpi: Dpi) -> Option<(i32, i32)> {
@@ -253,10 +248,6 @@ impl NodeTrait for NodeSvg {
             }
         }
 
-        // The "style" sub-element is not loaded yet here, so we need
-        // to store other attributes to be applied later.
-        *self.pbag.borrow_mut() = Some(pbag.to_owned());
-
         Ok(())
     }
 
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index 9f39fc81..08e8b85d 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -326,7 +326,7 @@ impl XmlState {
         // The "svg" node is special; it will parse its style attributes
         // until the end, in standard_element_end().
         if new_node.get_type() != NodeType::Svg {
-            new_node.set_style(self.css_rules.as_ref().unwrap(), pbag);
+            new_node.set_style(self.css_rules.as_ref().unwrap());
         }
 
         new_node.set_overridden_properties();


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