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:
parent
099f07ebac
commit
abd0cc6247
3 changed files with 30 additions and 12 deletions
|
|
@ -17,6 +17,14 @@ A dependency update to an incompatible version is considered a breaking change.
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `Attr::set`
|
||||||
|
|
||||||
|
### Deprecated
|
||||||
|
|
||||||
|
- `Attr::new` in favor of `Attr::set`
|
||||||
|
|
||||||
## v0.1.1 - 2024-12-08
|
## v0.1.1 - 2024-12-08
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
||||||
|
|
@ -301,7 +301,7 @@ impl Attr {
|
||||||
/// When this attribute is added to an [`Element`] through
|
/// When this attribute is added to an [`Element`] through
|
||||||
/// [`ElementComponent::add_to_element`] and an attribute of the same name
|
/// [`ElementComponent::add_to_element`] and an attribute of the same name
|
||||||
/// already exists, it replaces that attribute's value.
|
/// 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 {
|
Self {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
value: value.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.
|
/// Create or append to an attribute.
|
||||||
///
|
///
|
||||||
/// When this attribute is added to an [`Element`] through
|
/// 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:
|
/// When rendering an empty attribute as HTML, the value can be omitted:
|
||||||
/// `name=""` is equivalent to just `name`.
|
/// `name=""` is equivalent to just `name`.
|
||||||
pub fn yes(name: impl ToString) -> Self {
|
pub fn yes(name: impl ToString) -> Self {
|
||||||
Self::new(name, "")
|
Self::set(name, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create (or replace) an `id` attribute.
|
/// Create (or replace) an `id` attribute.
|
||||||
///
|
///
|
||||||
/// `Attr::id(id)` is equivalent to `Attr::new("id", id)`.
|
/// `Attr::id(id)` is equivalent to `Attr::new("id", id)`.
|
||||||
pub fn id(id: impl ToString) -> Self {
|
pub fn id(id: impl ToString) -> Self {
|
||||||
Self::new("id", id)
|
Self::set("id", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create (or append to) a `class` attribute.
|
/// 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-*
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*
|
||||||
pub fn data(name: impl ToString, value: impl ToString) -> Self {
|
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> {
|
impl ElementComponent for HashMap<String, String> {
|
||||||
fn add_to_element(self, element: &mut Element) {
|
fn add_to_element(self, element: &mut Element) {
|
||||||
for (name, value) in self {
|
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> {
|
impl ElementComponent for BTreeMap<String, String> {
|
||||||
fn add_to_element(self, element: &mut Element) {
|
fn add_to_element(self, element: &mut Element) {
|
||||||
for (name, value) in self {
|
for (name, value) in self {
|
||||||
Attr::new(name, value).add_to_element(element);
|
Attr::set(name, value).add_to_element(element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
src/lib.rs
12
src/lib.rs
|
|
@ -150,10 +150,10 @@ mod tests {
|
||||||
fn attributes() {
|
fn attributes() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
input((
|
input((
|
||||||
Attr::new("name", "tentacles"),
|
Attr::set("name", "tentacles"),
|
||||||
Attr::new("type", "number"),
|
Attr::set("type", "number"),
|
||||||
Attr::new("min", 10),
|
Attr::set("min", 10),
|
||||||
Attr::new("max", 100),
|
Attr::set("max", 100),
|
||||||
))
|
))
|
||||||
.render_to_string()
|
.render_to_string()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
|
@ -161,7 +161,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
input((Attr::new("name", "horns"), Attr::yes("checked")))
|
input((Attr::set("name", "horns"), Attr::yes("checked")))
|
||||||
.render_to_string()
|
.render_to_string()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
r#"<input checked name="horns">"#,
|
r#"<input checked name="horns">"#,
|
||||||
|
|
@ -172,7 +172,7 @@ mod tests {
|
||||||
fn always_lowercase() {
|
fn always_lowercase() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Element::normal("HTML")
|
Element::normal("HTML")
|
||||||
.with(Attr::new("LANG", "EN"))
|
.with(Attr::set("LANG", "EN"))
|
||||||
.render_to_string()
|
.render_to_string()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
r#"<html lang="EN"></html>"#,
|
r#"<html lang="EN"></html>"#,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue