From abd0cc6247e1adea0ff851d0ff0c5c2c364d2466 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 13 Dec 2024 22:34:26 +0100 Subject: [PATCH] Add Attr::set The name "set" makes more sense compared to "append" than "new". "new" only exists because originally there was no "append". --- CHANGELOG.md | 8 ++++++++ src/element.rs | 22 ++++++++++++++++------ src/lib.rs | 12 ++++++------ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b990096..0e6a3fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,14 @@ A dependency update to an incompatible version is considered a breaking change. ## Unreleased +### Added + +- `Attr::set` + +### Deprecated + +- `Attr::new` in favor of `Attr::set` + ## v0.1.1 - 2024-12-08 ### Added diff --git a/src/element.rs b/src/element.rs index 144b64a..4b3e450 100644 --- a/src/element.rs +++ b/src/element.rs @@ -301,7 +301,7 @@ impl Attr { /// When this attribute is added to an [`Element`] through /// [`ElementComponent::add_to_element`] and an attribute of the same name /// already exists, it replaces that attribute's value. - pub fn new(name: impl ToString, value: impl ToString) -> Self { + pub fn set(name: impl ToString, value: impl ToString) -> Self { Self { name: name.to_string(), value: value.to_string(), @@ -309,6 +309,16 @@ impl Attr { } } + /// Create or replace an attribute. + /// + /// When this attribute is added to an [`Element`] through + /// [`ElementComponent::add_to_element`] and an attribute of the same name + /// already exists, it replaces that attribute's value. + #[deprecated = "use `Attr::set` instead"] + pub fn new(name: impl ToString, value: impl ToString) -> Self { + Self::set(name, value) + } + /// Create or append to an attribute. /// /// When this attribute is added to an [`Element`] through @@ -330,14 +340,14 @@ impl Attr { /// When rendering an empty attribute as HTML, the value can be omitted: /// `name=""` is equivalent to just `name`. pub fn yes(name: impl ToString) -> Self { - Self::new(name, "") + Self::set(name, "") } /// Create (or replace) an `id` attribute. /// /// `Attr::id(id)` is equivalent to `Attr::new("id", id)`. pub fn id(id: impl ToString) -> Self { - Self::new("id", id) + Self::set("id", id) } /// Create (or append to) a `class` attribute. @@ -363,7 +373,7 @@ impl Attr { /// /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-* pub fn data(name: impl ToString, value: impl ToString) -> Self { - Self::new(format!("data-{}", name.to_string()), value) + Self::set(format!("data-{}", name.to_string()), value) } } @@ -393,7 +403,7 @@ impl ElementComponent for Attr { impl ElementComponent for HashMap { fn add_to_element(self, element: &mut Element) { for (name, value) in self { - Attr::new(name, value).add_to_element(element); + Attr::set(name, value).add_to_element(element); } } } @@ -401,7 +411,7 @@ impl ElementComponent for HashMap { impl ElementComponent for BTreeMap { fn add_to_element(self, element: &mut Element) { for (name, value) in self { - Attr::new(name, value).add_to_element(element); + Attr::set(name, value).add_to_element(element); } } } diff --git a/src/lib.rs b/src/lib.rs index 62ceca5..6407579 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -150,10 +150,10 @@ mod tests { fn attributes() { assert_eq!( input(( - Attr::new("name", "tentacles"), - Attr::new("type", "number"), - Attr::new("min", 10), - Attr::new("max", 100), + Attr::set("name", "tentacles"), + Attr::set("type", "number"), + Attr::set("min", 10), + Attr::set("max", 100), )) .render_to_string() .unwrap(), @@ -161,7 +161,7 @@ mod tests { ); assert_eq!( - input((Attr::new("name", "horns"), Attr::yes("checked"))) + input((Attr::set("name", "horns"), Attr::yes("checked"))) .render_to_string() .unwrap(), r#""#, @@ -172,7 +172,7 @@ mod tests { fn always_lowercase() { assert_eq!( Element::normal("HTML") - .with(Attr::new("LANG", "EN")) + .with(Attr::set("LANG", "EN")) .render_to_string() .unwrap(), r#""#,