Available on crate feature
typed
only.Expand description
Typed HTML nodes.
Examples
use html_node::typed::{self, elements::*};
// ^^^^^^^^^^^
// required to bring type definitions
// of all basic html elements into
// the current scope.
// (can also use `elements::div`, etc.)
#[derive(Clone, Debug)]
struct Location {
x: i32,
y: i32,
}
impl std::fmt::Display for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{},{}", self.x, self.y)
}
}
// defines a custom element named `CustomElement`, with the specified attributes.
// underscores in attributes get converted to and from hyphens in the
// `typed::html!` macro and rendering.
// note that global attributes like `id` will be pre-defined when
// using the `typed::element!` macro.
typed::element! {
CustomElement("custom-element") {
custom_attr, // implictly typed as a `String`
location: Location,
}
}
typed::attributes! {
[TestAttrs] {
test_val: i32,
}
}
// creates a normal `Node`, but checks types at compile-time!
let html = typed::html! { (test: TestAttrs, any)
// ^^^^^^^^^^^^^^^^^^^^^^ these are extension attributes.
// they are not required, but allow you to specify extra attributes
// which will be available within this macro invocation.
// those of the form `attr-prefix: Type` will be type checked, and
// those with just `attr-prefix` will be considered "catch-all" prefixes
// allowing any attribute with that prefix to be specified.
// `data-*` and `aria-*` are predefined as catch-all prefixes.
<div id="container">
<CustomElement test-val=42 any-whatever data-cool=true id="el" custom-attr="test" location=Location { x: 1, y: 2 } />
</div>
};
assert_eq!(
html.to_string(),
"\
<div id=\"container\">\
<custom-element id=\"el\" custom-attr=\"test\" location=\"1,2\" test-val=\"42\" any-whatever data-cool=\"true\">\
</custom-element>\
</div>\
",
);
Modules
- Predefined HTML elements.
Macros
- Make a typed set of HTML attributes.
- Make a typed HTML node.
- Make a typed element.
- Make many typed elements.
- Make a typed HTML node.
Enums
- A typed attribute.
Traits
- A typed set of HTML attributes.
- A typed HTML element.