From bad49d49c0b754b6a8cede71e93376507978acc5 Mon Sep 17 00:00:00 2001 From: msifeed Date: Mon, 18 Jul 2016 13:07:58 +0300 Subject: [PATCH 1/3] Add namespace support for element and attribute names --- maud_macros/src/parse.rs | 10 ++++++++++ maud_macros/tests/tests.rs | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/maud_macros/src/parse.rs b/maud_macros/src/parse.rs index 4027c5ed..d67d2815 100644 --- a/maud_macros/src/parse.rs +++ b/maud_macros/src/parse.rs @@ -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)) } @@ -548,6 +551,13 @@ impl<'cx, 'a, 'i> Parser<'cx, 'a, 'i> { /// Parses a HTML element or attribute name. fn name(&mut self) -> PResult { let mut s = match *self.input { + [ident!(_, namespace), colon!(), ident!(_, name), ..] => { + self.shift(3); + let mut r = String::from(&namespace.name.as_str() as &str); + r.push(':'); + r.push_str(&name.name.as_str() as &str); + r + }, [ident!(_, name), ..] => { self.shift(1); String::from(&name.name.as_str() as &str) diff --git a/maud_macros/tests/tests.rs b/maud_macros/tests/tests.rs index f5f202cb..dc5e9a59 100644 --- a/maud_macros/tests/tests.rs +++ b/maud_macros/tests/tests.rs @@ -87,6 +87,13 @@ mod elements { html!(s, div readonly? input type="checkbox" checked? /).unwrap(); assert_eq!(s, r#"
"#); } + + #[test] + fn namespaces() { + let mut s = String::new(); + html!(s, pon:controls a on:click="yay()" "Yay!").unwrap(); + assert_eq!(s, r#"Yay!"#); + } } mod splices { From e222f5385dd8ab4f4f77acd43b41ca053f04b6ca Mon Sep 17 00:00:00 2001 From: msifeed Date: Mon, 18 Jul 2016 14:40:45 +0300 Subject: [PATCH 2/3] Fixed minuses in namespaces --- maud_macros/src/parse.rs | 35 ++++++++++++++++++++--------------- maud_macros/tests/tests.rs | 4 ++-- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/maud_macros/src/parse.rs b/maud_macros/src/parse.rs index d67d2815..eec31209 100644 --- a/maud_macros/src/parse.rs +++ b/maud_macros/src/parse.rs @@ -550,25 +550,30 @@ impl<'cx, 'a, 'i> Parser<'cx, 'a, 'i> { /// Parses a HTML element or attribute name. fn name(&mut self) -> PResult { - let mut s = match *self.input { - [ident!(_, namespace), colon!(), ident!(_, name), ..] => { - self.shift(3); - let mut r = String::from(&namespace.name.as_str() as &str); - r.push(':'); - r.push_str(&name.name.as_str() as &str); - r - }, - [ident!(_, name), ..] => { + macro_rules! ident_with_minuses { + ($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) } diff --git a/maud_macros/tests/tests.rs b/maud_macros/tests/tests.rs index dc5e9a59..71cace1e 100644 --- a/maud_macros/tests/tests.rs +++ b/maud_macros/tests/tests.rs @@ -91,8 +91,8 @@ mod elements { #[test] fn namespaces() { let mut s = String::new(); - html!(s, pon:controls a on:click="yay()" "Yay!").unwrap(); - assert_eq!(s, r#"Yay!"#); + html!(s, pon-pon:controls-alpha a on:click="yay()" "Yay!").unwrap(); + assert_eq!(s, r#"Yay!"#); } } From 36a88bd74f2b9e4cec45522e27f20820dddfd589 Mon Sep 17 00:00:00 2001 From: msifeed Date: Tue, 19 Jul 2016 14:57:05 +0300 Subject: [PATCH 3/3] Move ident parsing to separate method --- maud_macros/src/parse.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/maud_macros/src/parse.rs b/maud_macros/src/parse.rs index eec31209..7166b81b 100644 --- a/maud_macros/src/parse.rs +++ b/maud_macros/src/parse.rs @@ -548,32 +548,34 @@ impl<'cx, 'a, 'i> Parser<'cx, 'a, 'i> { Ok(()) } - /// Parses a HTML element or attribute name. - fn name(&mut self) -> PResult { - macro_rules! ident_with_minuses { - ($name:ident) => ({ - self.shift(1); - 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 - }) - } - + /// Parses ident with minuses. + fn ident_with_minuses(&mut self) -> PResult { let mut s = match *self.input { - [ident!(_, name), ..] => ident_with_minuses!(name), + [ident!(_, name), ..] => { + self.shift(1); + String::from(&name.name.as_str() as &str) + }, _ => return Err(FatalError), }; + while let [minus!(), ident!(_, name), ..] = *self.input { + self.shift(2); + s.push('-'); + s.push_str(&name.name.as_str()); + } + Ok(s) + } - if let [colon!(), ident!(_, name), ..] = *self.input { + /// Parses a HTML element or attribute name. + fn name(&mut self) -> PResult { + let mut s = match self.ident_with_minuses() { + Ok(ident) => ident, + Err(e) => return Err(e), + }; + if let [colon!(), ident!(_, _), ..] = *self.input { self.shift(1); s.push(':'); - s.push_str(ident_with_minuses!(name).as_str()); + s.push_str(self.ident_with_minuses().unwrap().as_str()); } - Ok(s) }