Add html::attr

This commit is contained in:
Joscha 2024-12-12 14:32:51 +01:00
parent abd0cc6247
commit ceefa426f6
6 changed files with 1082 additions and 16 deletions

View file

@ -346,6 +346,7 @@ impl Attr {
/// Create (or replace) an `id` attribute.
///
/// `Attr::id(id)` is equivalent to `Attr::new("id", id)`.
#[deprecated = "use `html::attr::id` instead"]
pub fn id(id: impl ToString) -> Self {
Self::set("id", id)
}
@ -354,6 +355,7 @@ impl Attr {
///
/// `Attr::class(class)` is equivalent to
/// `Attr::append("class", class, " ")`.
#[deprecated = "use `html::attr::class` instead"]
pub fn class(class: impl ToString) -> Self {
Self::append("class", class, " ")
}
@ -362,6 +364,7 @@ impl Attr {
///
/// `Attr::style(style)` is equivalent to
/// `Attr::append("style", style, ";")`.
#[deprecated = "use `html::attr::style` instead"]
pub fn style(style: impl ToString) -> Self {
Self::append("style", style, ";")
}
@ -372,6 +375,7 @@ impl Attr {
/// `Attr::new(format!("data-{name}"), value)`.
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*
#[deprecated = "use `html::attr::data_x` instead"]
pub fn data(name: impl ToString, value: impl ToString) -> Self {
Self::set(format!("data-{}", name.to_string()), value)
}

View file

@ -1,5 +1,9 @@
//! Definitions for all non-deprecated HTML elements
//! Definitions for HTML elements and attributes
//! ([MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element)).
//!
//! Deprecated HTML elements are not included.
pub mod attr;
use crate::{Element, ElementComponent, ElementKind};

1044
src/html/attr.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -37,15 +37,14 @@
//!
//! let page: String = html((
//! head((
//! meta(Attr::new("charset", "utf-8")),
//! meta((
//! Attr::new("name", "viewport"),
//! Attr::new("content", "width=device-width, initial-scale=1"),
//! attr::name("viewport"),
//! attr::content("width=device-width, initial-scale=1"),
//! )),
//! title("Example page"),
//! )),
//! body((
//! h1((Attr::id("heading"), "Example page")),
//! h1((attr::id("heading"), "Example page")),
//! p(("This is an example for a ", em("simple"), " web page.")),
//! )),
//! ))
@ -151,9 +150,9 @@ mod tests {
assert_eq!(
input((
Attr::set("name", "tentacles"),
Attr::set("type", "number"),
Attr::set("min", 10),
Attr::set("max", 100),
attr::TypeInput::Number,
attr::min(10),
Attr::append("max", 100, "FOOBAA"),
))
.render_to_string()
.unwrap(),
@ -166,6 +165,18 @@ mod tests {
.unwrap(),
r#"<input checked name="horns">"#,
);
assert_eq!(
p((
attr::id("foo"),
attr::id("bar"),
attr::class("foo"),
attr::class("bar"),
))
.render_to_string()
.unwrap(),
r#"<p class="foo bar" id="bar"></p>"#,
)
}
#[test]