Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce “bare” nodes with no surrounding new lines #81

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "svg"
version = "0.15.1"
version = "0.16.0"
edition = "2021"
license = "Apache-2.0/MIT"
authors = [
Expand Down
70 changes: 43 additions & 27 deletions src/node/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,18 @@ impl fmt::Display for Element {
return write!(formatter, "/>");
}
write!(formatter, ">")?;
let mut bare = false;
for child in self.children.iter() {
write!(formatter, "\n{}", child)?;
bare = child.is_bare() && !formatter.alternate();
if !bare {
writeln!(formatter)?;
}
write!(formatter, "{}", child)?;
}
if !bare {
writeln!(formatter)?;
}
write!(formatter, "\n</{}>", self.name)
write!(formatter, "</{}>", self.name)
}
}

Expand Down Expand Up @@ -310,18 +318,9 @@ implement! {
#[doc = "A [`symbol`](https://www.w3.org/TR/SVG/struct.html#SymbolElement) element."]
struct Symbol

#[doc = "A [`text`](https://www.w3.org/TR/SVG/text.html#TextElement) element."]
struct Text

#[doc = "A [`textPath`](https://www.w3.org/TR/SVG/text.html#TextPathElement) element."]
struct TextPath

#[doc = "A [`title`](https://www.w3.org/TR/SVG/struct.html#TitleElement) element."]
struct Title

#[doc = "A [`tspan`](https://www.w3.org/TR/SVG/text.html#TextElement) element."]
struct TSpan

#[doc = "A [`use`](https://www.w3.org/TR/SVG/struct.html#UseElement) element."]
struct Use
}
Expand All @@ -331,7 +330,9 @@ macro_rules! implement {
($(
#[$doc:meta]
struct $struct_name:ident
[$($pn:ident: $($pt:tt)*),*] [$inner:ident $(,$an:ident: $at:ty)*] $body:block
[$($indicator_name:ident),*]
[$($trait_name:ident: $($trait_type:tt)*),*]
[$inner:ident $(,$argument_name:ident: $argument_type:ty)*] $body:block
)*) => ($(
#[$doc]
#[derive(Clone, Debug)]
Expand All @@ -343,11 +344,13 @@ macro_rules! implement {
impl $struct_name {
/// Create a node.
#[inline]
pub fn new<$($pn: $($pt)*),*>($($an: $at),*) -> Self {
pub fn new<$($trait_name: $($trait_type)*),*>($($argument_name: $argument_type),*) -> Self {
#[inline(always)]
fn initialize<$($pn: $($pt)*),*>($inner: &mut Element $(, $an: $at)*) $body
fn initialize<$($trait_name: $($trait_type)*),*>(
$inner: &mut Element $(, $argument_name: $argument_type)*
) $body
let mut inner = Element::new(tag::$struct_name);
initialize(&mut inner $(, $an)*);
initialize(&mut inner $(, $argument_name)*);
$struct_name {
inner,
}
Expand All @@ -361,23 +364,38 @@ macro_rules! implement {
}
}

node! { $struct_name::inner }
node! { $struct_name::inner [$($indicator_name),*] }
)*);
}

implement! {
#[doc = "A [`text`](https://www.w3.org/TR/SVG/text.html#TextElement) element."]
struct Text [is_bareable] [T: Into<String>] [inner, content: T] {
inner.append(crate::node::Text::new(content));
}

#[doc = "A [`title`](https://www.w3.org/TR/SVG/struct.html#TitleElement) element."]
struct Title [] [T: Into<String>] [inner, content: T] {
inner.append(crate::node::Text::new(content));
}

#[doc = "A [`tspan`](https://www.w3.org/TR/SVG/text.html#TextElement) element."]
struct TSpan [] [T: Into<String>] [inner, content: T] {
inner.append(crate::node::Text::new(content));
}

#[doc = "An [`svg`](https://www.w3.org/TR/SVG/struct.html#SVGElement) element."]
struct SVG [] [inner] {
struct SVG [is_bareable] [] [inner] {
inner.assign("xmlns", "http://www.w3.org/2000/svg");
}

#[doc = "A [`script`](https://www.w3.org/TR/SVG/script.html#ScriptElement) element."]
struct Script [T: Into<String>] [inner, content: T] {
struct Script [is_bareable] [T: Into<String>] [inner, content: T] {
inner.append(crate::node::Text::new(content));
}

#[doc = "A [`style`](https://www.w3.org/TR/SVG/styling.html#StyleElement) element."]
struct Style [T: Into<String>] [inner, content: T] {
struct Style [is_bareable] [T: Into<String>] [inner, content: T] {
inner.append(crate::node::Text::new(content));
}
}
Expand All @@ -391,14 +409,14 @@ fn escape(value: &str) -> String {
#[cfg(test)]
mod tests {
use super::{Element, Rectangle, Style, Title};
use crate::node::{self, element, Node};
use crate::node::{element, Node};

#[test]
fn element_children() {
let mut one = element::Group::new()
.add(element::Text::new().add(node::Text::new("foo")))
.add(element::Text::new().add(node::Text::new("bar")))
.add(element::Text::new().add(node::Text::new("buz")));
.add(element::Text::new("foo"))
.add(element::Text::new("bar"))
.add(element::Text::new("buz"));
let two = element::Group::new()
.add(one.get_children()[0].clone())
.add(one.get_children_mut().pop().unwrap());
Expand Down Expand Up @@ -440,15 +458,13 @@ mod tests {
.set("width", 0.3088995)
.set("x", 328.0725)
.set("y", 120)
.add(Title::new().add(node::Text::new("widgets >=3.0.9, <3.1.dev0")));
.add(Title::new("widgets >=3.0.9, <3.1.dev0"));

assert_eq!(
element.to_string().lines().collect::<Vec<_>>(),
&[
r###"<rect fill="#FF780088" height="10" width="0.3088995" x="328.0725" y="120">"###,
"<title>",
"widgets &gt;=3.0.9, &lt;3.1.dev0",
"</title>",
"<title>widgets &gt;=3.0.9, &lt;3.1.dev0</title>",
"</rect>",
],
);
Expand Down
26 changes: 25 additions & 1 deletion src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ pub trait Node:
U: Into<Value>,
{
}

#[doc(hidden)]
fn is_bare(&self) -> bool {
false
}

#[doc(hidden)]
fn is_bareable(&self) -> bool {
false
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -90,6 +100,9 @@ impl NodeDefaultHash for Box<dyn Node> {

macro_rules! node(
($struct_name:ident::$field_name:ident) => (
node!($struct_name::$field_name []);
);
($struct_name:ident::$field_name:ident [$($indicator_name:ident),*]) => (
impl $struct_name {
/// Append a node.
pub fn add<T>(mut self, node: T) -> Self
Expand Down Expand Up @@ -129,6 +142,13 @@ macro_rules! node(
{
self.$field_name.assign(name, value);
}

$(
#[inline]
fn $indicator_name(&self) -> bool {
true
}
)*
}

impl ::std::ops::Deref for $struct_name {
Expand All @@ -150,7 +170,11 @@ macro_rules! node(
impl ::std::fmt::Display for $struct_name {
#[inline]
fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
self.$field_name.fmt(formatter)
if self.is_bareable() {
write!(formatter, "{:#}", self.$field_name)
} else {
self.$field_name.fmt(formatter)
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/node/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ impl fmt::Display for Text {
}
}

impl Node for Text {}
impl Node for Text {
#[inline]
fn is_bare(&self) -> bool {
true
}
}

impl super::NodeDefaultHash for Text {
#[inline]
Expand Down
Loading