Merge pull request from Nemo157/class-shorthand

Add a shorthand syntax to define element classes
This commit is contained in:
Chris Wong 2016-02-08 23:34:53 +11:00
commit 6b141294fd
2 changed files with 56 additions and 0 deletions
maud_macros

View file

@ -364,6 +364,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 +376,26 @@ 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");
self.render.string(&classes.join(" "));
self.render.attribute_end();
}
Ok(())
}
/// Parses and renders the attributes of an element.
fn attrs(&mut self) -> PResult<()> {
loop {

View file

@ -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, "<p>Hi, <span class=\"name\">Lyra</span>!</p>");
}
#[test]
fn class_shorthand_with_space() {
let mut s = String::new();
html!(s, p { "Hi, " span .name { "Lyra" } "!" }).unwrap();
assert_eq!(s, "<p>Hi, <span class=\"name\">Lyra</span>!</p>");
}
#[test]
fn classes_shorthand() {
let mut s = String::new();
html!(s, p { "Hi, " span.name.here { "Lyra" } "!" }).unwrap();
assert_eq!(s, "<p>Hi, <span class=\"name here\">Lyra</span>!</p>");
}
#[test]
fn classes_shorthand_with_space() {
let mut s = String::new();
html!(s, p { "Hi, " span .name .here { "Lyra" } "!" }).unwrap();
assert_eq!(s, "<p>Hi, <span class=\"name here\">Lyra</span>!</p>");
}
#[test]
fn classes_shorthand_with_attrs() {
let mut s = String::new();
html!(s, p { "Hi, " span.name.here id="thing" { "Lyra" } "!" }).unwrap();
assert_eq!(s, "<p>Hi, <span class=\"name here\" id=\"thing\">Lyra</span>!</p>");
}