[librsvg: 2/10] node: add and use a is_chars method



commit 842220e2ae02c670bbe11477eb56182a05c597df
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Mar 15 19:22:03 2020 +0100

    node: add and use a is_chars method

 rsvg_internals/src/css.rs      |  7 ++++---
 rsvg_internals/src/document.rs |  5 +----
 rsvg_internals/src/node.rs     | 10 ++++++++++
 rsvg_internals/src/text.rs     | 10 +++++-----
 4 files changed, 20 insertions(+), 12 deletions(-)
---
diff --git a/rsvg_internals/src/css.rs b/rsvg_internals/src/css.rs
index 40ee8d18..701832f9 100644
--- a/rsvg_internals/src/css.rs
+++ b/rsvg_internals/src/css.rs
@@ -547,9 +547,10 @@ impl selectors::Element for RsvgElement {
     /// See http://dev.w3.org/csswg/selectors-3/#empty-pseudo
     fn is_empty(&self) -> bool {
         !self.0.has_children()
-            || self.0.children().all(|child| {
-                child.borrow().get_type() == NodeType::Chars && child.borrow_chars().is_empty()
-            })
+            || self
+                .0
+                .children()
+                .all(|child| child.is_chars() && child.borrow_chars().is_empty())
     }
 
     /// Returns whether this element matches `:root`,
diff --git a/rsvg_internals/src/document.rs b/rsvg_internals/src/document.rs
index 46d94a98..91a63ca6 100644
--- a/rsvg_internals/src/document.rs
+++ b/rsvg_internals/src/document.rs
@@ -476,10 +476,7 @@ impl DocumentBuilder {
     fn append_chars_to_parent(&mut self, text: &str, parent: &mut RsvgNode) {
         // When the last child is a Chars node we can coalesce
         // the text and avoid screwing up the Pango layouts
-        let chars_node = if let Some(child) = parent
-            .last_child()
-            .filter(|c| c.borrow().get_type() == NodeType::Chars)
-        {
+        let chars_node = if let Some(child) = parent.last_child().filter(|c| c.is_chars()) {
             child
         } else {
             let child = RsvgNode::new(NodeData::new_chars());
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index 10469712..dfae73c4 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -532,6 +532,9 @@ pub trait NodeBorrow {
     /// Returns `false` for NodeData::Text, `true` otherwise.
     fn is_element(&self) -> bool;
 
+    /// Returns `true` for NodeData::Text, `false` otherwise.
+    fn is_chars(&self) -> bool;
+
     /// Borrows a `NodeChars` reference.
     ///
     /// Panics: will panic if `&self` is not a `NodeData::Text` node
@@ -556,6 +559,13 @@ impl NodeBorrow for RsvgNode {
         }
     }
 
+    fn is_chars(&self) -> bool {
+        match *self.borrow() {
+            NodeData::Text(_) => true,
+            _ => false,
+        }
+    }
+
     fn borrow_chars(&self) -> Ref<NodeChars> {
         Ref::map(self.borrow(), |n| match *n {
             NodeData::Text(ref c) => c,
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index cc2c2369..1b4345dc 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -708,12 +708,12 @@ fn extract_chars_children_to_chunks_recursively(
     depth: usize,
 ) {
     for child in node.children() {
-        match child.borrow().get_type() {
-            NodeType::Chars => child
+        if child.is_chars() {
+            child
                 .borrow_chars()
-                .to_chunks(&child, values, chunks, None, None, depth),
-
-            _ => extract_chars_children_to_chunks_recursively(chunks, &child, values, depth + 1),
+                .to_chunks(&child, values, chunks, None, None, depth)
+        } else {
+            extract_chars_children_to_chunks_recursively(chunks, &child, values, depth + 1)
         }
     }
 }


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