Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add namespace support for element and attribute names #38

Merged
merged 3 commits into from
Jul 20, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions maud_macros/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ macro_rules! question {
macro_rules! semi {
() => (TokenTree::Token(_, Token::Semi))
}
macro_rules! colon {
() => (TokenTree::Token(_, Token::Colon))
}
macro_rules! comma {
() => (TokenTree::Token(_, Token::Comma))
}
Expand Down Expand Up @@ -547,18 +550,30 @@ impl<'cx, 'a, 'i> Parser<'cx, 'a, 'i> {

/// Parses a HTML element or attribute name.
fn name(&mut self) -> PResult<String> {
let mut s = match *self.input {
[ident!(_, name), ..] => {
macro_rules! ident_with_minuses {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be a macro? It looks like it can be written as a plain method.

($name:ident) => ({
self.shift(1);
String::from(&name.name.as_str() as &str)
},
let mut s = String::from(&$name.name.as_str() as &str);
while let [minus!(), ident!(_, name), ..] = *self.input {
self.shift(2);
s.push('-');
s.push_str(&name.name.as_str());
}
s
})
}

let mut s = match *self.input {
[ident!(_, name), ..] => ident_with_minuses!(name),
_ => return Err(FatalError),
};
while let [minus!(), ident!(_, name), ..] = *self.input {
self.shift(2);
s.push('-');
s.push_str(&name.name.as_str());

if let [colon!(), ident!(_, name), ..] = *self.input {
self.shift(1);
s.push(':');
s.push_str(ident_with_minuses!(name).as_str());
}

Ok(s)
}

Expand Down
7 changes: 7 additions & 0 deletions maud_macros/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ mod elements {
html!(s, div readonly? input type="checkbox" checked? /).unwrap();
assert_eq!(s, r#"<div readonly><input type="checkbox" checked></div>"#);
}

#[test]
fn namespaces() {
let mut s = String::new();
html!(s, pon-pon:controls-alpha a on:click="yay()" "Yay!").unwrap();
assert_eq!(s, r#"<pon-pon:controls-alpha><a on:click="yay()">Yay!</a></pon-pon:controls-alpha>"#);
}
}

mod splices {
Expand Down