diff --git a/maud_macros/src/parse.rs b/maud_macros/src/parse.rs index 713eafc..c394c01 100644 --- a/maud_macros/src/parse.rs +++ b/maud_macros/src/parse.rs @@ -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 { diff --git a/maud_macros/tests/tests.rs b/maud_macros/tests/tests.rs index 265cc00..8879bcc 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, "<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>"); +}