Skip to content

Commit

Permalink
Implement ID shorthand
Browse files Browse the repository at this point in the history
Closes #35
  • Loading branch information
lambda-fairy committed Jun 3, 2016
1 parent 8f58fe4 commit 92ba854
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
44 changes: 27 additions & 17 deletions maud_macros/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ macro_rules! eq {
macro_rules! not {
() => (TokenTree::Token(_, Token::Not))
}
macro_rules! pound {
() => (TokenTree::Token(_, Token::Pound))
}
macro_rules! question {
() => (TokenTree::Token(_, Token::Question))
}
Expand Down Expand Up @@ -462,7 +465,6 @@ impl<'cx, 'i> Parser<'cx, 'i> {
parse_error!(self, sp, "unexpected element, you silly bumpkin");
}
self.render.element_open_start(name);
self.class_shorthand()?;
self.attrs()?;
self.render.element_open_end();
if let [slash!(), ..] = self.input {
Expand All @@ -474,23 +476,10 @@ 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();
while let [dot!(), ident!(_, _), ..] = self.input {
self.shift(1);
classes.push(self.name()?);
}
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<()> {
let mut classes = Vec::new();
let mut ids = Vec::new();
loop {
let old_input = self.input;
let maybe_name = self.name();
Expand Down Expand Up @@ -528,11 +517,32 @@ impl<'cx, 'i> Parser<'cx, 'i> {
self.render.attribute_empty(&name);
}
},
(Err(_), [dot!(), ident!(_, _), ..]) => {
// Class shorthand
self.shift(1);
classes.push(self.name()?);
},
(Err(_), [pound!(), ident!(_, _), ..]) => {
// ID shorthand
self.shift(1);
ids.push(self.name()?);
},
_ => {
self.input = old_input;
break;
},
}}
}
}
if !classes.is_empty() {
self.render.attribute_start("class");
self.render.string(&classes.join(" "));
self.render.attribute_end();
}
if !ids.is_empty() {
self.render.attribute_start("id");
self.render.string(&ids.join(" "));
self.render.attribute_end();
}
Ok(())
}

Expand Down
13 changes: 10 additions & 3 deletions maud_macros/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,17 @@ fn classes_shorthand_with_space() {
}

#[test]
fn classes_shorthand_with_attrs() {
fn ids_shorthand() {
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>");
html!(s, p { "Hi, " span#thing { "Lyra" } "!" }).unwrap();
assert_eq!(s, "<p>Hi, <span id=\"thing\">Lyra</span>!</p>");
}

#[test]
fn classes_attrs_ids_mixed_up() {
let mut s = String::new();
html!(s, p { "Hi, " span.name.here lang="en" #thing { "Lyra" } "!" }).unwrap();
assert_eq!(s, "<p>Hi, <span lang=\"en\" class=\"name here\" id=\"thing\">Lyra</span>!</p>");
}

#[test]
Expand Down

0 comments on commit 92ba854

Please sign in to comment.