1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use html5ever::{LocalName, Prefix, Namespace};
use std::collections::hash_map::{self, HashMap};
#[derive(Debug, PartialEq, Clone)]
pub struct Attributes {
pub map: HashMap<ExpandedName, Attribute>,
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct ExpandedName {
pub ns: Namespace,
pub local: LocalName,
}
impl ExpandedName {
pub fn new<N: Into<Namespace>, L: Into<LocalName>>(ns: N, local: L) -> Self {
ExpandedName {
ns: ns.into(),
local: local.into(),
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct Attribute {
pub prefix: Option<Prefix>,
pub value: String,
}
impl Attributes {
pub fn contains<A: Into<LocalName>>(&self, local_name: A) -> bool {
self.map.contains_key(&ExpandedName::new(ns!(), local_name))
}
pub fn get<A: Into<LocalName>>(&self, local_name: A) -> Option<&str> {
self.map.get(&ExpandedName::new(ns!(), local_name)).map(|attr| &*attr.value)
}
pub fn get_mut<A: Into<LocalName>>(&mut self, local_name: A) -> Option<&mut String> {
self.map.get_mut(&ExpandedName::new(ns!(), local_name)).map(|attr| &mut attr.value)
}
pub fn entry<A: Into<LocalName>>(&mut self, local_name: A)
-> hash_map::Entry<ExpandedName, Attribute> {
self.map.entry(ExpandedName::new(ns!(), local_name))
}
pub fn insert<A: Into<LocalName>>(&mut self, local_name: A, value: String) -> Option<Attribute> {
self.map.insert(ExpandedName::new(ns!(), local_name), Attribute { prefix: None, value })
}
pub fn remove<A: Into<LocalName>>(&mut self, local_name: A) -> Option<Attribute> {
self.map.remove(&ExpandedName::new(ns!(), local_name))
}
}