forked from swc-project/swc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.rs
92 lines (82 loc) · 1.96 KB
/
node.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::{
cell::{Cell, RefCell},
fmt, mem,
rc::{Rc, Weak},
};
use swc_atoms::JsWord;
use swc_common::Span;
use swc_xml_ast::*;
#[derive(Debug, Clone)]
pub struct TokenAndInfo {
pub span: Span,
pub acknowledged: bool,
pub token: Token,
}
#[derive(Debug, Clone)]
pub enum Data {
Document,
DocumentType {
name: Option<JsWord>,
public_id: Option<JsWord>,
system_id: Option<JsWord>,
raw: Option<JsWord>,
},
Element {
tag_name: JsWord,
attributes: RefCell<Vec<Attribute>>,
},
Text {
data: RefCell<String>,
raw: RefCell<String>,
},
ProcessingInstruction {
target: JsWord,
data: JsWord,
},
CdataSection {
data: JsWord,
raw: Option<JsWord>,
},
Comment {
data: JsWord,
raw: Option<JsWord>,
},
}
pub struct Node {
pub parent: Cell<Option<WeakNode>>,
pub children: RefCell<Vec<RcNode>>,
pub data: Data,
pub start_span: RefCell<Span>,
pub end_span: RefCell<Option<Span>>,
}
impl Node {
/// Create a new node from its contents
pub fn new(data: Data, span: Span) -> Rc<Self> {
Rc::new(Node {
parent: Cell::new(None),
children: RefCell::new(vec![]),
start_span: RefCell::new(span),
end_span: RefCell::new(None),
data,
})
}
}
impl Drop for Node {
fn drop(&mut self) {
let mut nodes = mem::take(&mut *self.children.borrow_mut());
while let Some(node) = nodes.pop() {
let children = mem::take(&mut *node.children.borrow_mut());
nodes.extend(children.into_iter());
}
}
}
impl fmt::Debug for Node {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Node")
.field("data", &self.data)
.field("children", &self.children)
.finish()
}
}
pub type RcNode = Rc<Node>;
type WeakNode = Weak<Node>;