Add Attr::set

The name "set" makes more sense compared to "append" than "new". "new"
only exists because originally there was no "append".
This commit is contained in:
Joscha 2024-12-13 22:34:26 +01:00
parent 099f07ebac
commit abd0cc6247
3 changed files with 30 additions and 12 deletions

View file

@ -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<String, String> {
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<String, String> {
impl ElementComponent for BTreeMap<String, String> {
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);
}
}
}

View file

@ -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#"<input checked name="horns">"#,
@ -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#"<html lang="EN"></html>"#,