dioxus_html_macro/
lib.rs

1/// # dioxus html macro
2/// This crate offers an `html!` like macro for
3/// dioxus applications. It expands to the equivalent `rsx!` macro
4/// call you would have made otherwise, so it does not rely on any
5/// dioxus' internals.
6/// ```rust
7/// # use dioxus::prelude::*;
8/// # use dioxus_html_macro::html;
9/// fn app(cx: Scope) -> Element {
10///     let mut count = use_state(&cx, || 0);
11///     cx.render(html!(
12///         <h1>"High-Five counter: {count}"</h1>
13///         <button onclick={move |_| count += 1}>"Up high!"</button>
14///         <button onclick={move |_| count -= 1}>"Down low!"</button>
15///     ))
16/// }
17/// ```
18/// Note that unlike HTML and JSX, styling of html tags is done via
19/// attributes:
20/// ```rust
21/// # use dioxus::prelude::*;
22/// # use dioxus_html_macro::html;
23/// html!(
24///     <h1 color="red">"Title"</h1>
25/// );
26/// ```
27
28#[macro_use]
29extern crate syn;
30#[macro_use]
31extern crate quote;
32
33use html_non_recursive::HtmlNonRecursive;
34use proc_macro::TokenStream;
35
36mod assertions;
37mod attribute;
38mod attribute_ident;
39mod attributes;
40mod close_tag;
41mod element;
42mod html;
43mod html_non_recursive;
44mod item;
45mod open_tag;
46mod prelude;
47mod rsx_expr;
48
49/// macro for generating components using HTML syntax instead of rsx.
50#[proc_macro]
51pub fn html(input: TokenStream) -> TokenStream {
52    let html: HtmlNonRecursive = parse_macro_input!(input);
53    quote! {
54        dioxus::prelude::rsx! {
55            #html
56        }
57    }
58    .into()
59}
60
61#[cfg(test)]
62#[test]
63fn trybuild() {
64    let t = trybuild::TestCases::new();
65    t.compile_fail("test/tag/trailing.rs");
66    t.compile_fail("test/tag/extra_close.rs");
67    t.compile_fail("test/tag/missing_close.rs");
68
69    t.compile_fail("test/attribute/non_str_custom.rs");
70    t.compile_fail("test/attribute/format_str.rs");
71    t.compile_fail("test/attribute/missing_equals.rs");
72    t.compile_fail("test/attribute/random_expression.rs");
73    t.pass("test/attribute/passes.rs");
74
75    t.compile_fail("test/body/plain_text.rs");
76    t.pass("test/body/expression.rs");
77    t.pass("test/props/enum.rs");
78}