Merge pull request #16 from jonathanKingston/self-closing-tags

Support allow list of self-closing tags. Fixes #15
This commit is contained in:
Bodil Stokke 2018-11-29 15:50:34 +00:00 committed by GitHub
commit e80c8e6d88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 8 deletions

View File

@ -34,6 +34,7 @@ fn index() -> Html {
<link rel=LinkType::StyleSheet href="lol.css"/> <link rel=LinkType::StyleSheet href="lol.css"/>
</head> </head>
<body> <body>
<div></div>
<h1 data-lol="omg">"Hello Kitty!"</h1> <h1 data-lol="omg">"Hello Kitty!"</h1>
<p class="official-position-of-sanrio-ltd emphasis"> <p class="official-position-of-sanrio-ltd emphasis">
"She is not a "<em><a href="https://en.wikipedia.org/wiki/Cat">"cat"</a></em>". She is a "<em>"human girl"</em>"." "She is not a "<em><a href="https://en.wikipedia.org/wiki/Cat">"cat"</a></em>". She is a "<em>"human girl"</em>"."

View File

@ -37,6 +37,25 @@ pub fn global_attrs(span: Span) -> StringyMap<Ident, TokenStream> {
attrs attrs
} }
pub static SELF_CLOSING: &[&str] = &[
"area",
"base",
"br",
"col",
"command",
"embed",
"hr",
"img",
"input",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr",
];
// This NEEDS to be a sorted list! // This NEEDS to be a sorted list!
pub static ATTR_EVENTS: &[&str] = &[ pub static ATTR_EVENTS: &[&str] = &[
"abort", "abort",

View File

@ -1,7 +1,7 @@
use proc_macro2::{Ident, Literal, Span, TokenStream, TokenTree}; use proc_macro2::{Ident, Literal, Span, TokenStream, TokenTree};
use quote::quote; use quote::quote;
use config::{global_attrs, ATTR_EVENTS}; use config::{global_attrs, ATTR_EVENTS, SELF_CLOSING};
use error::ParseError; use error::ParseError;
use ident; use ident;
use lexer::{Lexer, Token}; use lexer::{Lexer, Token};
@ -306,6 +306,15 @@ impl Declare {
let print_children = if self.req_children.is_empty() { let print_children = if self.req_children.is_empty() {
if self.opt_children.is_some() { if self.opt_children.is_some() {
if !SELF_CLOSING.contains(&elem_name.to_string().as_str()) {
quote!(if self.children.is_empty() {
write!(f, "></{}>", #name)
} else {
write!(f, ">")?;
#print_opt_children
write!(f, "</{}>", #name)
})
} else {
quote!(if self.children.is_empty() { quote!(if self.children.is_empty() {
write!(f, " />") write!(f, " />")
} else { } else {
@ -313,9 +322,14 @@ impl Declare {
#print_opt_children #print_opt_children
write!(f, "</{}>", #name) write!(f, "</{}>", #name)
}) })
}
} else {
if !SELF_CLOSING.contains(&elem_name.to_string().as_str()) {
quote!(write!(f, "></{}>", #name))
} else { } else {
quote!(write!(f, "/>")) quote!(write!(f, "/>"))
} }
}
} else { } else {
quote!( quote!(
write!(f, ">")?; write!(f, ">")?;