[librsvg: 1/2] Change Chars::new() to take an initial text
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 1/2] Change Chars::new() to take an initial text
- Date: Mon, 24 Aug 2020 15:09:49 +0000 (UTC)
commit 476cf50e20874c1cd3b79fa54bef8ad0b1465c69
Author: Sven Neumann <sven svenfoo org>
Date: Mon Aug 24 12:07:33 2020 +0200
Change Chars::new() to take an initial text
This simplifies the single caller in document.rs.
rsvg_internals/src/document.rs | 10 +++-------
rsvg_internals/src/node.rs | 4 ++--
rsvg_internals/src/text.rs | 31 +++++++++++++++++++++++--------
3 files changed, 28 insertions(+), 17 deletions(-)
---
diff --git a/rsvg_internals/src/document.rs b/rsvg_internals/src/document.rs
index f504ef137..59e46291e 100644
--- a/rsvg_internals/src/document.rs
+++ b/rsvg_internals/src/document.rs
@@ -436,15 +436,11 @@ impl DocumentBuilder {
fn append_chars_to_parent(&mut self, text: &str, parent: &mut Node) {
// 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.is_chars()) {
- child
+ if let Some(child) = parent.last_child().filter(|c| c.is_chars()) {
+ child.borrow_chars().append(text);
} else {
- let child = Node::new(NodeData::new_chars());
- parent.append(child.clone());
- child
+ parent.append(Node::new(NodeData::new_chars(text)));
};
-
- chars_node.borrow_chars().append(text);
}
pub fn resolve_href(&self, href: &str) -> Result<AllowedUrl, AllowedUrlError> {
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index a6bdd8503..7bcdd566c 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -72,8 +72,8 @@ impl NodeData {
NodeData::Element(Element::new(name, pbag))
}
- pub fn new_chars() -> NodeData {
- NodeData::Text(Chars::new())
+ pub fn new_chars(initial_text: &str) -> NodeData {
+ NodeData::Text(Chars::new(initial_text))
}
}
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index 61d1e0f11..a1c566de0 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -484,15 +484,16 @@ fn children_to_chunks(
/// `xml:space="preserve"` attribute. A `Chars` stores the characters verbatim
/// as they come out of the XML parser, after ensuring that they are valid UTF-8.
+#[derive(Default)]
pub struct Chars {
string: RefCell<String>,
space_normalized: RefCell<Option<String>>,
}
impl Chars {
- pub fn new() -> Chars {
+ pub fn new(initial_text: &str) -> Chars {
Chars {
- string: RefCell::new(String::new()),
+ string: RefCell::new(String::from(initial_text)),
space_normalized: RefCell::new(None),
}
}
@@ -564,12 +565,6 @@ impl Chars {
}
}
-impl Default for Chars {
- fn default() -> Self {
- Self::new()
- }
-}
-
#[derive(Default)]
pub struct Text {
x: Length<Horizontal>,
@@ -1014,3 +1009,23 @@ fn create_pango_layout(
layout
}
+
+#[cfg(test)]
+mod tests {
+ use super::Chars;
+
+ #[test]
+ fn chars_default() {
+ let c = Chars::default();
+ assert!(c.is_empty());
+ assert!(c.space_normalized.borrow().is_none());
+ }
+
+ #[test]
+ fn chars_new() {
+ let example = "Test 123";
+ let c = Chars::new(example);
+ assert_eq!(c.get_string(), example);
+ assert!(c.space_normalized.borrow().is_none());
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]