[librsvg] Node: add an element_name field



commit 8e80f49deeeb9c692c8349bbb2a742de20c16b3e
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Sep 14 10:38:27 2018 -0500

    Node: add an element_name field
    
    Normally each node type corresponds to a single element name, so in
    theory we could derive the name from the NodeType.  However, we
    translate unknown element names to NodeDefs, so a NodeDefs may refer
    to not only <defs>.
    
    Here we add an element_name field to the Node struct, so each specific
    node may know to what SVG element it corresponds.

 rsvg_internals/src/load.rs |  7 ++++---
 rsvg_internals/src/node.rs | 16 ++++++++++++++++
 rsvg_internals/src/text.rs |  1 +
 3 files changed, 21 insertions(+), 3 deletions(-)
---
diff --git a/rsvg_internals/src/load.rs b/rsvg_internals/src/load.rs
index c0c9f610..6880b255 100644
--- a/rsvg_internals/src/load.rs
+++ b/rsvg_internals/src/load.rs
@@ -40,11 +40,12 @@ use util::utf8_cstr;
 macro_rules! node_create_fn {
     ($name:ident, $node_type:ident, $new_fn:expr) => {
         fn $name(
+            element_name: &str,
             id: Option<&str>,
             class: Option<&str>,
             parent: *const RsvgNode,
         ) -> *const RsvgNode {
-            boxed_node_new(NodeType::$node_type, parent, id, class, Box::new($new_fn()))
+            boxed_node_new(NodeType::$node_type, parent, element_name, id, class, Box::new($new_fn()))
         }
     };
 }
@@ -168,7 +169,7 @@ node_create_fn!(
 );
 node_create_fn!(create_use, Use, NodeUse::new);
 
-type NodeCreateFn = fn(Option<&str>, Option<&str>, *const RsvgNode) -> *const RsvgNode;
+type NodeCreateFn = fn(&str, Option<&str>, Option<&str>, *const RsvgNode) -> *const RsvgNode;
 
 lazy_static! {
     // Lines in comments are elements that we don't support.
@@ -301,7 +302,7 @@ pub extern "C" fn rsvg_load_new_node(
         class = None;
     };
 
-    let node = create_fn(id, class, parent);
+    let node = create_fn(name, id, class, parent);
     assert!(!node.is_null());
 
     if id.is_some() {
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index 480d8579..dfb746b8 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -162,6 +162,7 @@ pub type NodeResult = Result<(), NodeError>;
 pub struct Node {
     node_type: NodeType,
     parent: Option<Weak<Node>>, // optional; weak ref to parent
+    element_name: String,       // we may want to intern these someday
     id: Option<String>,         // id attribute from XML element
     class: Option<String>,      // class attribute from XML element
     first_child: RefCell<Option<Rc<Node>>>,
@@ -243,6 +244,7 @@ impl Node {
     pub fn new(
         node_type: NodeType,
         parent: Option<Weak<Node>>,
+        element_name: &str,
         id: Option<&str>,
         class: Option<&str>,
         node_impl: Box<NodeTrait>,
@@ -250,6 +252,7 @@ impl Node {
         Node {
             node_type,
             parent,
+            element_name: element_name.to_string(),
             id: id.map(str::to_string),
             class: class.map(str::to_string),
             first_child: RefCell::new(None),
@@ -676,6 +679,7 @@ pub fn node_ptr_to_weak(raw_parent: *const RsvgNode) -> Option<Weak<Node>> {
 pub fn boxed_node_new(
     node_type: NodeType,
     raw_parent: *const RsvgNode,
+    element_name: &str,
     id: Option<&str>,
     class: Option<&str>,
     node_impl: Box<NodeTrait>,
@@ -683,6 +687,7 @@ pub fn boxed_node_new(
     box_node(Rc::new(Node::new(
         node_type,
         node_ptr_to_weak(raw_parent),
+        element_name,
         id,
         class,
         node_impl,
@@ -864,6 +869,7 @@ mod tests {
         let node = Rc::new(Node::new(
             NodeType::Path,
             None,
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
@@ -889,6 +895,7 @@ mod tests {
         let node = Rc::new(Node::new(
             NodeType::Path,
             None,
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
@@ -911,6 +918,7 @@ mod tests {
         let node = Rc::new(Node::new(
             NodeType::Path,
             None,
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
@@ -924,6 +932,7 @@ mod tests {
         let node = Rc::new(Node::new(
             NodeType::Path,
             None,
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
@@ -932,6 +941,7 @@ mod tests {
         let child = Rc::new(Node::new(
             NodeType::Path,
             Some(Rc::downgrade(&node)),
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
@@ -948,6 +958,7 @@ mod tests {
         let node = Rc::new(Node::new(
             NodeType::Path,
             None,
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
@@ -956,6 +967,7 @@ mod tests {
         let child = Rc::new(Node::new(
             NodeType::Path,
             Some(Rc::downgrade(&node)),
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
@@ -964,6 +976,7 @@ mod tests {
         let second_child = Rc::new(Node::new(
             NodeType::Path,
             Some(Rc::downgrade(&node)),
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
@@ -993,6 +1006,7 @@ mod tests {
         let node = Rc::new(Node::new(
             NodeType::Path,
             None,
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
@@ -1001,6 +1015,7 @@ mod tests {
         let child = Rc::new(Node::new(
             NodeType::Path,
             Some(Rc::downgrade(&node)),
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
@@ -1009,6 +1024,7 @@ mod tests {
         let second_child = Rc::new(Node::new(
             NodeType::Path,
             Some(Rc::downgrade(&node)),
+            "path",
             None,
             None,
             Box::new(TestNodeImpl {}),
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index fe997e9c..3563c85d 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -756,6 +756,7 @@ pub extern "C" fn rsvg_node_chars_new(raw_parent: *const RsvgNode) -> *const Rsv
     boxed_node_new(
         NodeType::Chars,
         raw_parent,
+        "rsvg_chars",
         None,
         None,
         Box::new(NodeChars::new()),


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