Skip to content

Only output self-closing tags for void elements #46

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion render/src/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::fmt::{Result, Write};
/// <b />
/// </>
/// };
/// assert_eq!(result, "<a/><b/>");
/// assert_eq!(result, "<a></a><b></b>");
/// ```
#[derive(Debug)]
pub struct Fragment<T: Render> {
Expand Down
2 changes: 1 addition & 1 deletion render/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::fmt::{Result, Write};
/// </html>
/// </>
/// };
/// # assert_eq!(result, "<!DOCTYPE html><html><body/></html>");
/// # assert_eq!(result, "<!DOCTYPE html><html><body></body></html>");
/// ```
#[derive(Debug)]
pub struct HTML5Doctype;
Expand Down
12 changes: 11 additions & 1 deletion render/src/simple_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ impl<T: Render> Render for SimpleElement<'_, T> {
None => {
write!(writer, "<{}", self.tag_name)?;
write_attributes(self.attributes, writer)?;
write!(writer, "/>")

match self.tag_name {
"area" | "base" | "br" | "col" | "embed" | "hr" | "img" | "input" | "link"
| "meta" | "param" | "source" | "track" | "wbr" => {
// void element, can be self-closing
write!(writer, "/>")
}
_ => {
write!(writer, "></{}>", self.tag_name)
}
}
}
Some(renderable) => {
write!(writer, "<{}", self.tag_name)?;
Expand Down
10 changes: 5 additions & 5 deletions render_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use syn::parse_macro_input;
/// <div id={"main"} />
/// };
///
/// assert_eq!(rendered, r#"<div id="main"/>"#);
/// assert_eq!(rendered, r#"<div id="main"></div>"#);
/// ```
///
/// ### HTML entities can accept dashed-separated value
Expand All @@ -66,10 +66,10 @@ use syn::parse_macro_input;
/// # use render_macros::html;
/// # use pretty_assertions::assert_eq;
/// let rendered = html! {
/// <div data-testid={"sometestid"} />
/// <div data-testid={"sometestid"}></div>
/// };
///
/// assert_eq!(rendered, r#"<div data-testid="sometestid"/>"#);
/// assert_eq!(rendered, r#"<div data-testid="sometestid"></div>"#);
/// ```
///
/// ### Custom components can't accept dashed-separated values
Expand All @@ -95,7 +95,7 @@ use syn::parse_macro_input;
/// <div class />
/// };
///
/// assert_eq!(rendered, r#"<div class="someclass"/>"#);
/// assert_eq!(rendered, r#"<div class="someclass"></div>"#);
/// ```
///
/// ### Punning is not supported for dashed-delimited attributes
Expand All @@ -107,7 +107,7 @@ use syn::parse_macro_input;
/// <div this-wont-work />
/// };
///
/// assert_eq!(rendered, r#"<div class="some_class"/>"#);
/// assert_eq!(rendered, r#"<div class="some_class"></div>"#);
/// ```
#[proc_macro]
#[proc_macro_error]
Expand Down
15 changes: 13 additions & 2 deletions render_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn works_with_dashes() {
use pretty_assertions::assert_eq;

let value = render::html! { <div data-id={"myid"} /> };
assert_eq!(value, r#"<div data-id="myid"/>"#);
assert_eq!(value, r#"<div data-id="myid"></div>"#);
}

#[test]
Expand Down Expand Up @@ -41,7 +41,18 @@ fn works_with_keywords() {
use render::html;

assert_eq!(html! { <input type={"text"} /> }, r#"<input type="text"/>"#);
assert_eq!(html! { <label for={"me"} /> }, r#"<label for="me"/>"#);
assert_eq!(
html! { <label for={"me"} /> },
r#"<label for="me"></label>"#
);
}

#[test]
fn selfclosing_void_element() {
use pretty_assertions::assert_eq;
use render::html;

assert_eq!(html! { <hr /> }, r#"<hr/>"#);
}

#[test]
Expand Down