[librsvg: 1/5] node: move code to NodeData



commit 58427e77207c942be27c2d2e4d5dd701cd15cfb3
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri May 17 09:12:00 2019 +0200

    node: move code to NodeData
    
    Start making RsvgNode a bit thinner

 rsvg_internals/src/node.rs | 342 +++++++++++++++++++++++----------------------
 1 file changed, 176 insertions(+), 166 deletions(-)
---
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index 6f6a904a..431c4c8b 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -44,7 +44,29 @@ impl NodeRef<NodeData> {
         class: Option<&str>,
         node_impl: Box<NodeTrait>,
     ) -> NodeRef<NodeData> {
-        let data = NodeData {
+        NodeRef(Rc::new(tree_utils::Node::new(
+            NodeData::new(node_type, id, class, node_impl),
+            parent,
+        )))
+    }
+
+    pub fn downgrade(&self) -> RsvgWeakNode {
+        Rc::downgrade(&self.0)
+    }
+
+    pub fn upgrade(weak: &RsvgWeakNode) -> Option<NodeRef<NodeData>> {
+        weak.upgrade().map(NodeRef)
+    }
+}
+
+impl NodeData {
+    pub fn new(
+        node_type: NodeType,
+        id: Option<&str>,
+        class: Option<&str>,
+        node_impl: Box<NodeTrait>,
+    ) -> NodeData {
+        NodeData {
             node_type,
             id: id.map(str::to_string),
             class: class.map(str::to_string),
@@ -56,23 +78,164 @@ impl NodeRef<NodeData> {
             cond: Cell::new(true),
             node_impl,
             style_attr: RefCell::new(String::new()),
-        };
+        }
+    }
 
-        NodeRef(Rc::new(tree_utils::Node::new(data, parent)))
+    pub fn get_impl<T: NodeTrait>(&self) -> Option<&T> {
+        (&self.node_impl).downcast_ref::<T>()
     }
 
-    pub fn downgrade(&self) -> RsvgWeakNode {
-        Rc::downgrade(&self.0)
+    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.set_conditional_processing_attributes(pbag, locale))
+            .and_then(|_| self.node_impl.set_atts(node, pbag))
+            .and_then(|_| self.set_presentation_attributes(pbag))
+        {
+            self.set_error(e);
+        }
     }
 
-    pub fn upgrade(weak: &RsvgWeakNode) -> Option<NodeRef<NodeData>> {
-        weak.upgrade().map(NodeRef)
+    fn save_style_attribute(&self, pbag: &PropertyBag<'_>) {
+        let mut style_attr = self.style_attr.borrow_mut();
+
+        for (attr, value) in pbag.iter() {
+            match attr {
+                local_name!("style") => style_attr.push_str(value),
+
+                _ => (),
+            }
+        }
     }
-}
 
-impl NodeData {
-    pub fn get_impl<T: NodeTrait>(&self) -> Option<&T> {
-        (&self.node_impl).downcast_ref::<T>()
+    fn set_transform_attribute(&self, pbag: &PropertyBag<'_>) -> Result<(), NodeError> {
+        for (attr, value) in pbag.iter() {
+            match attr {
+                local_name!("transform") => {
+                    return Matrix::parse_str(value)
+                        .attribute(attr)
+                        .and_then(|affine| Ok(self.transform.set(affine)));
+                }
+
+                _ => (),
+            }
+        }
+
+        Ok(())
+    }
+
+    fn set_conditional_processing_attributes(
+        &self,
+        pbag: &PropertyBag<'_>,
+        locale: &Locale,
+    ) -> Result<(), NodeError> {
+        let mut cond = self.cond.get();
+
+        for (attr, value) in pbag.iter() {
+            // FIXME: move this to "try {}" when we can bump the rustc version dependency
+            let mut parse = || {
+                match attr {
+                    local_name!("requiredExtensions") if cond => {
+                        cond = RequiredExtensions::from_attribute(value)
+                            .map(|RequiredExtensions(res)| res)?;
+                    }
+
+                    local_name!("requiredFeatures") if cond => {
+                        cond = RequiredFeatures::from_attribute(value)
+                            .map(|RequiredFeatures(res)| res)?;
+                    }
+
+                    local_name!("systemLanguage") if cond => {
+                        cond = SystemLanguage::from_attribute(value, locale)
+                            .map(|SystemLanguage(res)| res)?;
+                    }
+
+                    _ => {}
+                }
+
+                Ok(cond)
+            };
+
+            parse()
+                .map(|c| self.cond.set(c))
+                .map_err(|e| NodeError::attribute_error(attr, e))?;
+        }
+
+        Ok(())
+    }
+
+    /// Hands the pbag to the node's state, to apply the presentation attributes
+    fn set_presentation_attributes(&self, pbag: &PropertyBag<'_>) -> Result<(), NodeError> {
+        match self
+            .specified_values
+            .borrow_mut()
+            .parse_presentation_attributes(pbag)
+        {
+            Ok(_) => Ok(()),
+            Err(e) => {
+                // FIXME: we'll ignore errors here for now.
+                //
+                // If we set the node to be in error, we expose buggy handling of the
+                // enable-background property; we are not parsing it correctly. This
+                // causes tests/fixtures/reftests/bugs/587721-text-transform.svg to fail
+                // because it has enable-background="new 0 0 1179.75118 687.74173" in the
+                // toplevel svg element.
+                //
+                //   self.set_error(e);
+                //   return;
+
+                rsvg_log!("(attribute error: {})", e);
+                Ok(())
+            }
+        }
+    }
+
+    /// Applies the CSS rules that match into the node's specified_values
+    fn set_css_styles(&self, node: &RsvgNode, css_rules: &CssRules) {
+        let mut specified_values = self.specified_values.borrow_mut();
+        let mut important_styles = self.important_styles.borrow_mut();
+
+        for selector in &css_rules.get_matches(node) {
+            if let Some(decl_list) = css_rules.get_declarations(selector) {
+                for declaration in decl_list.iter() {
+                    specified_values
+                        .set_property_from_declaration(declaration, &mut important_styles);
+                }
+            }
+        }
+    }
+
+    /// Applies CSS styles from the saved value of the "style" attribute
+    fn set_style_attribute(&self) {
+        let mut style_attr = self.style_attr.borrow_mut();
+
+        if !style_attr.is_empty() {
+            let mut important_styles = self.important_styles.borrow_mut();
+
+            if let Err(e) = self
+                .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.
+    fn set_style(&self, node: &RsvgNode, css_rules: &CssRules) {
+        self.set_css_styles(node, css_rules);
+        self.set_style_attribute();
+    }
+
+    fn set_error(&self, error: NodeError) {
+        *self.result.borrow_mut() = Err(error);
     }
 }
 
@@ -350,7 +513,7 @@ impl RsvgNode {
     }
 
     pub fn set_styles_recursively(&self, node: &RsvgNode, css_rules: &CssRules) {
-        self.set_style(node, css_rules);
+        self.borrow().set_style(node, css_rules);
 
         for child in self.children() {
             child.set_styles_recursively(&child, css_rules);
@@ -361,155 +524,8 @@ impl RsvgNode {
         self.borrow().cond.get()
     }
 
-    fn set_transform_attribute(&self, pbag: &PropertyBag<'_>) -> Result<(), NodeError> {
-        for (attr, value) in pbag.iter() {
-            match attr {
-                local_name!("transform") => {
-                    return Matrix::parse_str(value)
-                        .attribute(attr)
-                        .and_then(|affine| Ok(self.borrow().transform.set(affine)));
-                }
-
-                _ => (),
-            }
-        }
-
-        Ok(())
-    }
-
-    fn save_style_attribute(&self, pbag: &PropertyBag<'_>) {
-        let mut style_attr = self.borrow().style_attr.borrow_mut();
-
-        for (attr, value) in pbag.iter() {
-            match attr {
-                local_name!("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))
-            .and_then(|_| self.borrow().node_impl.set_atts(node, pbag))
-            .and_then(|_| self.set_presentation_attributes(pbag))
-        {
-            self.set_error(e);
-        }
-    }
-
-    fn parse_conditional_processing_attributes(
-        &self,
-        pbag: &PropertyBag<'_>,
-        locale: &Locale,
-    ) -> Result<(), NodeError> {
-        let mut cond = self.get_cond();
-
-        for (attr, value) in pbag.iter() {
-            // FIXME: move this to "try {}" when we can bump the rustc version dependency
-            let mut parse = || {
-                match attr {
-                    local_name!("requiredExtensions") if cond => {
-                        cond = RequiredExtensions::from_attribute(value)
-                            .map(|RequiredExtensions(res)| res)?;
-                    }
-
-                    local_name!("requiredFeatures") if cond => {
-                        cond = RequiredFeatures::from_attribute(value)
-                            .map(|RequiredFeatures(res)| res)?;
-                    }
-
-                    local_name!("systemLanguage") if cond => {
-                        cond = SystemLanguage::from_attribute(value, locale)
-                            .map(|SystemLanguage(res)| res)?;
-                    }
-
-                    _ => {}
-                }
-
-                Ok(cond)
-            };
-
-            parse()
-                .map(|c| self.borrow().cond.set(c))
-                .map_err(|e| NodeError::attribute_error(attr, e))?;
-        }
-
-        Ok(())
-    }
-
-    /// Hands the pbag to the node's state, to apply the presentation attributes
-    fn set_presentation_attributes(&self, pbag: &PropertyBag<'_>) -> Result<(), NodeError> {
-        match self
-            .borrow()
-            .specified_values
-            .borrow_mut()
-            .parse_presentation_attributes(pbag)
-        {
-            Ok(_) => Ok(()),
-            Err(e) => {
-                // FIXME: we'll ignore errors here for now.
-                //
-                // If we set the node to be in error, we expose buggy handling of the
-                // enable-background property; we are not parsing it correctly. This
-                // causes tests/fixtures/reftests/bugs/587721-text-transform.svg to fail
-                // because it has enable-background="new 0 0 1179.75118 687.74173" in the
-                // toplevel svg element.
-                //
-                //   self.set_error(e);
-                //   return;
-
-                rsvg_log!("(attribute error: {})", e);
-                Ok(())
-            }
-        }
-    }
-
-    /// Applies the CSS rules that match into the node's specified_values
-    fn set_css_styles(&self, node: &RsvgNode, css_rules: &CssRules) {
-        let mut specified_values = self.borrow().specified_values.borrow_mut();
-        let mut important_styles = self.borrow().important_styles.borrow_mut();
-
-        for selector in &css_rules.get_matches(node) {
-            if let Some(decl_list) = css_rules.get_declarations(selector) {
-                for declaration in decl_list.iter() {
-                    specified_values
-                        .set_property_from_declaration(declaration, &mut important_styles);
-                }
-            }
-        }
-    }
-
-    /// Applies CSS styles from the saved value of the "style" attribute
-    fn set_style_attribute(&self) {
-        let mut style_attr = self.borrow().style_attr.borrow_mut();
-
-        if !style_attr.is_empty() {
-            let mut important_styles = self.borrow().important_styles.borrow_mut();
-
-            if let Err(e) = self
-                .borrow()
-                .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.
-    fn set_style(&self, node: &RsvgNode, css_rules: &CssRules) {
-        self.set_css_styles(node, css_rules);
-        self.set_style_attribute();
+        self.borrow().set_atts(node, pbag, locale);
     }
 
     pub fn set_overridden_properties(&self) {
@@ -540,12 +556,6 @@ impl RsvgNode {
         }
     }
 
-    pub fn set_error(&self, error: NodeError) {
-        rsvg_log!("(attribute error for {}:\n  {})", self, error);
-
-        *self.borrow().result.borrow_mut() = Err(error);
-    }
-
     pub fn is_in_error(&self) -> bool {
         self.borrow().result.borrow().is_err()
     }


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