-
Notifications
You must be signed in to change notification settings - Fork 132
/
dom.rs
61 lines (48 loc) · 1.3 KB
/
dom.rs
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
//! Basic DOM data structures.
use std::collections::hashmap::{HashMap, HashSet};
pub type AttrMap = HashMap<String, String>;
#[deriving(Show)]
pub struct Node {
// data common to all nodes:
pub children: Vec<Node>,
// data specific to each node type:
pub node_type: NodeType,
}
#[deriving(Show)]
pub enum NodeType {
Element(ElementData),
Text(String),
}
#[deriving(Show)]
pub struct ElementData {
pub tag_name: String,
pub attributes: AttrMap,
}
// Constructor functions for convenience:
pub fn text(data: String) -> Node {
Node { children: vec![], node_type: Text(data) }
}
pub fn elem(name: String, attrs: AttrMap, children: Vec<Node>) -> Node {
Node {
children: children,
node_type: Element(ElementData {
tag_name: name,
attributes: attrs,
})
}
}
// Element methods
impl ElementData {
pub fn get_attribute<'a>(&'a self, key: &str) -> Option<&'a String> {
self.attributes.find_equiv(&key)
}
pub fn id<'a>(&'a self) -> Option<&'a String> {
self.get_attribute("id")
}
pub fn classes<'a>(&'a self) -> HashSet<&'a str> {
match self.get_attribute("class") {
Some(classlist) => classlist.as_slice().split(' ').collect(),
None => HashSet::new()
}
}
}