blitz_dom/node/
attributes.rs1use std::ops::{Deref, DerefMut};
2
3use markup5ever::QualName;
4
5#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
7pub struct Attribute {
8 pub name: QualName,
10 pub value: String,
12}
13
14#[derive(Clone, Debug)]
15pub struct Attributes {
16 inner: Vec<Attribute>,
17}
18
19impl Attributes {
20 pub fn new(inner: Vec<Attribute>) -> Self {
21 Self { inner }
22 }
23
24 pub fn get(&mut self, name: &QualName) -> Option<&Attribute> {
25 self.inner.iter().find(|attr| attr.name == *name)
26 }
27
28 pub fn set(&mut self, name: QualName, value: &str) {
29 let existing_attr = self.inner.iter_mut().find(|a| a.name == name);
30 if let Some(existing_attr) = existing_attr {
31 existing_attr.value.clear();
32 existing_attr.value.push_str(value);
33 } else {
34 self.push(Attribute {
35 name: name.clone(),
36 value: value.to_string(),
37 });
38 }
39 }
40
41 pub fn remove(&mut self, name: &QualName) -> Option<Attribute> {
42 let idx = self.inner.iter().position(|attr| attr.name == *name);
43 idx.map(|idx| self.inner.remove(idx))
44 }
45}
46
47impl Deref for Attributes {
48 type Target = Vec<Attribute>;
49 fn deref(&self) -> &Self::Target {
50 &self.inner
51 }
52}
53impl DerefMut for Attributes {
54 fn deref_mut(&mut self) -> &mut Self::Target {
55 &mut self.inner
56 }
57}