From b1680636cf3d89067b92613eac66e9bfead1ed96 Mon Sep 17 00:00:00 2001
From: Wim Looman
Date: Mon, 1 Feb 2016 17:21:29 +0100
Subject: [PATCH 1/3] Add a shorthand syntax to define element classes
---
maud_macros/src/parse.rs | 29 +++++++++++++++++++++++++++++
maud_macros/tests/tests.rs | 35 +++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+)
diff --git a/maud_macros/src/parse.rs b/maud_macros/src/parse.rs
index 713eafc3..772e8be1 100644
--- a/maud_macros/src/parse.rs
+++ b/maud_macros/src/parse.rs
@@ -190,6 +190,10 @@ impl<'cx, 'i> Parser<'cx, 'i> {
let name = try!(self.name());
try!(self.element(sp, &name));
},
+ // Shorthand div element
+ [dot!(), ident!(sp, _), ..] => {
+ try!(self.element(sp, "div"));
+ },
// Block
[TokenTree::Delimited(_, ref d), ..] if d.delim == DelimToken::Brace => {
self.shift(1);
@@ -364,6 +368,7 @@ impl<'cx, 'i> Parser<'cx, 'i> {
parse_error!(self, sp, "unexpected element, you silly bumpkin");
}
self.render.element_open_start(name);
+ try!(self.class_shorthand());
try!(self.attrs());
self.render.element_open_end();
if let [slash!(), ..] = self.input {
@@ -375,6 +380,30 @@ impl<'cx, 'i> Parser<'cx, 'i> {
Ok(())
}
+ /// Parses and renders the attributes of an element.
+ fn class_shorthand(&mut self) -> PResult<()> {
+ let mut classes = Vec::new();
+ loop {
+ match self.input {
+ [dot!(), ident!(_, _), ..] => {
+ self.shift(1);
+ classes.push(try!(self.name()));
+ },
+ _ => break,
+ }
+ }
+ if !classes.is_empty() {
+ self.render.attribute_start("class");
+ let mut s = String::new();
+ for class in classes {
+ s = s + &*class + " ";
+ }
+ self.render.string(s.trim());
+ self.render.attribute_end();
+ }
+ Ok(())
+ }
+
/// Parses and renders the attributes of an element.
fn attrs(&mut self) -> PResult<()> {
loop {
diff --git a/maud_macros/tests/tests.rs b/maud_macros/tests/tests.rs
index 265cc004..52123cba 100644
--- a/maud_macros/tests/tests.rs
+++ b/maud_macros/tests/tests.rs
@@ -330,3 +330,38 @@ fn splice_with_path() {
html!(s, $inner::name()).unwrap();
assert_eq!(s, "Maud");
}
+
+#[test]
+fn class_shorthand() {
+ let mut s = String::new();
+ html!(s, p { "Hi, " span.name { "Lyra" } "!" }).unwrap();
+ assert_eq!(s, "Hi, Lyra!
");
+}
+
+#[test]
+fn div_class_shorthand() {
+ let mut s = String::new();
+ html!(s, p { "Hi, " .name { "Lyra" } "!" }).unwrap();
+ assert_eq!(s, "Hi,
Lyra
!
");
+}
+
+#[test]
+fn classes_shorthand() {
+ let mut s = String::new();
+ html!(s, p { "Hi, " span.name.here { "Lyra" } "!" }).unwrap();
+ assert_eq!(s, "Hi, Lyra!
");
+}
+
+#[test]
+fn div_classes_shorthand() {
+ let mut s = String::new();
+ html!(s, p { "Hi, " .name.here { "Lyra" } "!" }).unwrap();
+ assert_eq!(s, "Hi,
Lyra
!");
+}
+
+#[test]
+fn div_classes_shorthand_with_attrs() {
+ let mut s = String::new();
+ html!(s, p { "Hi, " .name.here id="thing" { "Lyra" } "!" }).unwrap();
+ assert_eq!(s, "Hi,
Lyra
!");
+}
From 359079ca6cd7e5d8bc447bb1bdbc5156416a23c1 Mon Sep 17 00:00:00 2001
From: Wim Looman
Date: Mon, 1 Feb 2016 23:14:03 +0100
Subject: [PATCH 2/3] Use join
---
maud_macros/src/parse.rs | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/maud_macros/src/parse.rs b/maud_macros/src/parse.rs
index 772e8be1..28149638 100644
--- a/maud_macros/src/parse.rs
+++ b/maud_macros/src/parse.rs
@@ -394,11 +394,7 @@ impl<'cx, 'i> Parser<'cx, 'i> {
}
if !classes.is_empty() {
self.render.attribute_start("class");
- let mut s = String::new();
- for class in classes {
- s = s + &*class + " ";
- }
- self.render.string(s.trim());
+ self.render.string(&classes.join(" "));
self.render.attribute_end();
}
Ok(())
From da86d54b051b1512ac6bfee803895f67b5251922 Mon Sep 17 00:00:00 2001
From: Wim Looman
Date: Sun, 7 Feb 2016 10:59:20 +0100
Subject: [PATCH 3/3] Remove div shorthand
---
maud_macros/src/parse.rs | 4 ----
maud_macros/tests/tests.rs | 18 +++++++++---------
2 files changed, 9 insertions(+), 13 deletions(-)
diff --git a/maud_macros/src/parse.rs b/maud_macros/src/parse.rs
index 28149638..c394c017 100644
--- a/maud_macros/src/parse.rs
+++ b/maud_macros/src/parse.rs
@@ -190,10 +190,6 @@ impl<'cx, 'i> Parser<'cx, 'i> {
let name = try!(self.name());
try!(self.element(sp, &name));
},
- // Shorthand div element
- [dot!(), ident!(sp, _), ..] => {
- try!(self.element(sp, "div"));
- },
// Block
[TokenTree::Delimited(_, ref d), ..] if d.delim == DelimToken::Brace => {
self.shift(1);
diff --git a/maud_macros/tests/tests.rs b/maud_macros/tests/tests.rs
index 52123cba..8879bccb 100644
--- a/maud_macros/tests/tests.rs
+++ b/maud_macros/tests/tests.rs
@@ -339,10 +339,10 @@ fn class_shorthand() {
}
#[test]
-fn div_class_shorthand() {
+fn class_shorthand_with_space() {
let mut s = String::new();
- html!(s, p { "Hi, " .name { "Lyra" } "!" }).unwrap();
- assert_eq!(s, "Hi,
Lyra
!");
+ html!(s, p { "Hi, " span .name { "Lyra" } "!" }).unwrap();
+ assert_eq!(s, "Hi, Lyra!
");
}
#[test]
@@ -353,15 +353,15 @@ fn classes_shorthand() {
}
#[test]
-fn div_classes_shorthand() {
+fn classes_shorthand_with_space() {
let mut s = String::new();
- html!(s, p { "Hi, " .name.here { "Lyra" } "!" }).unwrap();
- assert_eq!(s, "Hi,
Lyra
!");
+ html!(s, p { "Hi, " span .name .here { "Lyra" } "!" }).unwrap();
+ assert_eq!(s, "Hi, Lyra!
");
}
#[test]
-fn div_classes_shorthand_with_attrs() {
+fn classes_shorthand_with_attrs() {
let mut s = String::new();
- html!(s, p { "Hi, " .name.here id="thing" { "Lyra" } "!" }).unwrap();
- assert_eq!(s, "Hi,
Lyra
!");
+ html!(s, p { "Hi, " span.name.here id="thing" { "Lyra" } "!" }).unwrap();
+ assert_eq!(s, "Hi, Lyra!
");
}