swc_xml_parser/parser/
node.rs

1#![allow(dead_code)]
2
3use std::{
4    cell::{Cell, RefCell},
5    fmt, mem,
6    rc::{Rc, Weak},
7};
8
9use swc_atoms::Atom;
10use swc_common::Span;
11use swc_xml_ast::*;
12
13#[derive(Debug, Clone)]
14pub struct TokenAndInfo {
15    pub span: Span,
16    pub acknowledged: bool,
17    pub token: Token,
18}
19
20#[derive(Debug, Clone)]
21pub enum Data {
22    Document,
23    DocumentType {
24        name: Option<Atom>,
25        public_id: Option<Atom>,
26        system_id: Option<Atom>,
27        raw: Option<Atom>,
28    },
29    Element {
30        tag_name: Atom,
31        attributes: RefCell<Vec<Attribute>>,
32    },
33    Text {
34        data: RefCell<String>,
35        raw: RefCell<String>,
36    },
37    ProcessingInstruction {
38        target: Atom,
39        data: Atom,
40    },
41    CdataSection {
42        data: Atom,
43        raw: Option<Atom>,
44    },
45    Comment {
46        data: Atom,
47        raw: Option<Atom>,
48    },
49}
50
51pub struct Node {
52    pub parent: Cell<Option<WeakNode>>,
53    pub children: RefCell<Vec<RcNode>>,
54    pub data: Data,
55    pub start_span: RefCell<Span>,
56    pub end_span: RefCell<Option<Span>>,
57}
58
59impl Node {
60    /// Create a new node from its contents
61    pub fn new(data: Data, span: Span) -> Rc<Self> {
62        Rc::new(Node {
63            parent: Cell::new(None),
64            children: RefCell::new(Vec::new()),
65            start_span: RefCell::new(span),
66            end_span: RefCell::new(None),
67            data,
68        })
69    }
70}
71
72impl Drop for Node {
73    fn drop(&mut self) {
74        let mut nodes = mem::take(&mut *self.children.borrow_mut());
75
76        while let Some(node) = nodes.pop() {
77            let children = mem::take(&mut *node.children.borrow_mut());
78
79            nodes.extend(children);
80        }
81    }
82}
83
84impl fmt::Debug for Node {
85    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
86        fmt.debug_struct("Node")
87            .field("data", &self.data)
88            .field("children", &self.children)
89            .finish()
90    }
91}
92
93pub type RcNode = Rc<Node>;
94type WeakNode = Weak<Node>;