diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-04-10 07:57:35 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-04-10 07:57:35 +0300 |
commit | 9e3623a0fae24019abfcd8858aa87030e76e08ac (patch) | |
tree | 4d86a7d77971a0a9ad91065478c9a8e890291010 | |
parent | b493d68cef283e0aa3cd5f2830376db09db49cb6 (diff) | |
download | html-page-9e3623a0fae24019abfcd8858aa87030e76e08ac.tar.gz |
feat: set attribute when creating an element
Sponsored-by: author
-rw-r--r-- | src/lib.rs | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -427,6 +427,18 @@ impl Element { self } + /// Set an attribute when creating an element. + pub fn with_attribute(mut self, name: &str, value: &str) -> Self { + self.attrs.set(name, value); + self + } + + /// Set a boolean attribute when creating an element. + pub fn with_boolean_attribute(mut self, name: &str) -> Self { + self.attrs.set_boolean(name); + self + } + /// Return the [`Tag`] of the element. pub fn tag(&self) -> Tag { self.tag @@ -656,6 +668,15 @@ mod test { } #[test] + fn can_create_element_with_attribute() { + let e = Element::new(Tag::P).with_attribute("foo", "bar"); + assert_eq!( + e.attribute("foo"), + Some(&AttributeValue::String("bar".into())) + ); + } + + #[test] fn can_add_boolean_attribute_to_element() { let mut e = Element::new(Tag::P); e.set_boolean_attribute("foo"); @@ -663,6 +684,12 @@ mod test { } #[test] + fn can_create_element_with_boolan_attribute() { + let e = Element::new(Tag::P).with_boolean_attribute("foo"); + assert_eq!(e.attribute("foo"), Some(&AttributeValue::Boolean)); + } + + #[test] fn unset_attribute_is_unset() { let e = Element::new(Tag::P); assert_eq!(e.attribute("foo"), None); |