Skip to content

Commit

Permalink
Add implicit div class/id shorthand #172 (#173)
Browse files Browse the repository at this point in the history
* Add implicit div class/id shorthand

* Add newline

* Add short explanation and example of implicit div
  • Loading branch information
yhtez authored and lambda-fairy committed Apr 20, 2019
1 parent f652784 commit f3a4c29
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/content/basic-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ html! {

## Classes and IDs `.foo` `#bar`

Add classes and IDs to an element using `.foo` and `#bar` syntax. You can chain multiple classes and IDs together, and mix and match them with other attributes:
Add classes and IDs to an element using `.foo` and `#bar` syntax. The tag will default to `div` if an element begins with a class or ID. You can chain multiple classes and IDs together, and mix and match them with other attributes:

```rust
html! {
div.container#main {
.container#main {
input.big.scary.bright-red type="button" value="Launch Party Cannon";
}
}
Expand Down
24 changes: 24 additions & 0 deletions maud/tests/basic_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,27 @@ fn classes_attrs_ids_mixed_up() {
let s = html!(p { "Hi, " span.name.here lang="en" #thing { "Lyra" } "!" }).into_string();
assert_eq!(s, r#"<p>Hi, <span class="name here" id="thing" lang="en">Lyra</span>!</p>"#);
}

#[test]
fn div_shorthand_class() {
let s = html!(.awesome-class {}).into_string();
assert_eq!(s, r#"<div class="awesome-class"></div>"#);
}

#[test]
fn div_shorthand_id() {
let s = html!(#unique-id {}).into_string();
assert_eq!(s, r#"<div id="unique-id"></div>"#);
}

#[test]
fn div_shorthand_class_with_attrs() {
let s = html!(.awesome-class contenteditable? dir="rtl" #unique-id {}).into_string();
assert_eq!(s, r#"<div class="awesome-class" id="unique-id" contenteditable dir="rtl"></div>"#);
}

#[test]
fn div_shorthand_id_with_attrs() {
let s = html!(#unique-id contenteditable? dir="rtl" .awesome-class {}).into_string();
assert_eq!(s, r#"<div class="awesome-class" id="unique-id" contenteditable dir="rtl"></div>"#);
}
6 changes: 6 additions & 0 deletions maud_macros/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use proc_macro::{
Delimiter,
Ident,
Literal,
Spacing,
Span,
Expand Down Expand Up @@ -164,6 +165,11 @@ impl Parser {
let name = self.try_namespaced_name().expect("identifier");
self.element(name)?
},
// Div element shorthand
TokenTree::Punct(ref punct) if punct.as_char() == '.' || punct.as_char() == '#' => {
let name = TokenTree::Ident(Ident::new("div", punct.span()));
self.element(name.into())?
},
// Splice
TokenTree::Group(ref group) if group.delimiter() == Delimiter::Parenthesis => {
self.advance();
Expand Down

0 comments on commit f3a4c29

Please sign in to comment.