Merge pull request #28 from Nemo157/class-shorthand
Add a shorthand syntax to define element classes
This commit is contained in:
commit
6b141294fd
2 changed files with 56 additions and 0 deletions
|
@ -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 {
|
||||
|
|
|
@ -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>");
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue