summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2023-04-10 08:06:21 +0300
committerLars Wirzenius <liw@liw.fi>2023-04-10 08:06:21 +0300
commit847b1874c73fe7da0327c4b5e254937c072d8f55 (patch)
tree9315c32ec19e41061f1f2849d2e77c1c7eda3876
parent33b057ffb1d74c7ac10961f49c43e777e581a7e7 (diff)
downloadhtml-page-847b1874c73fe7da0327c4b5e254937c072d8f55.tar.gz
feat: add Element::attribute_value
Sponsored-by: author
-rw-r--r--src/lib.rs7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 50ec205..0a5c405 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -471,6 +471,12 @@ impl Element {
self.attrs.get(name)
}
+ /// Return the value of an attribute as text, if the attribute is
+ /// set. Otherwise, return `None`.
+ pub fn attribute_value(&self, name: &str) -> Option<&str> {
+ self.attrs.get(name).map(|v| v.as_str())
+ }
+
/// Set a key/value attribute. If the attribute was already set,
/// change the value it has.
pub fn set_attribute(&mut self, name: &str, value: &str) {
@@ -677,6 +683,7 @@ mod test {
Some(&AttributeValue::String("bar".into()))
);
assert_eq!(e.attribute("foo").map(|x| x.as_str()), Some("bar"));
+ assert_eq!(e.attribute_value("foo"), Some("bar"));
}
#[test]