swc_html_ast/
base.rs

1use is_macro::Is;
2use string_enum::StringEnum;
3use swc_atoms::Atom;
4use swc_common::{ast_node, EqIgnoreSpan, Span};
5
6#[ast_node("Document")]
7#[derive(Eq, Hash, EqIgnoreSpan)]
8pub struct Document {
9    pub span: Span,
10    pub mode: DocumentMode,
11    pub children: Vec<Child>,
12}
13
14#[ast_node("DocumentFragment")]
15#[derive(Eq, Hash, EqIgnoreSpan)]
16pub struct DocumentFragment {
17    pub span: Span,
18    pub children: Vec<Child>,
19}
20
21#[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan)]
22#[cfg_attr(
23    feature = "rkyv",
24    derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
25)]
26#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))]
27//#[cfg_attr(
28//    feature = "rkyv",
29//    archive(bound(serialize = "__S: rkyv::ser::ScratchSpace +
30// rkyv::ser::Serializer"))
31//)]
32#[cfg_attr(feature = "rkyv", repr(u32))]
33#[cfg_attr(
34    feature = "encoding-impl",
35    derive(::swc_common::Encode, ::swc_common::Decode)
36)]
37pub enum DocumentMode {
38    /// `no-quirks`
39    NoQuirks,
40    /// `limited-quirks`
41    LimitedQuirks,
42    /// `quirks`
43    Quirks,
44}
45
46#[ast_node(no_unknown)]
47#[derive(Eq, Hash, Is, EqIgnoreSpan)]
48pub enum Child {
49    #[tag("DocumentType")]
50    DocumentType(DocumentType),
51    #[tag("Element")]
52    Element(Element),
53    #[tag("Text")]
54    Text(Text),
55    #[tag("Comment")]
56    Comment(Comment),
57}
58
59#[ast_node("DocumentType")]
60#[derive(Eq, Hash)]
61pub struct DocumentType {
62    pub span: Span,
63
64    #[cfg_attr(
65        feature = "encoding-impl",
66        encoding(with = "cbor4ii::core::types::Maybe")
67    )]
68    pub name: Option<Atom>,
69
70    #[cfg_attr(
71        feature = "encoding-impl",
72        encoding(with = "cbor4ii::core::types::Maybe")
73    )]
74    pub public_id: Option<Atom>,
75
76    #[cfg_attr(
77        feature = "encoding-impl",
78        encoding(with = "cbor4ii::core::types::Maybe")
79    )]
80    pub system_id: Option<Atom>,
81    #[cfg_attr(
82        feature = "encoding-impl",
83        encoding(with = "cbor4ii::core::types::Maybe")
84    )]
85    pub raw: Option<Atom>,
86}
87
88impl EqIgnoreSpan for DocumentType {
89    fn eq_ignore_span(&self, other: &Self) -> bool {
90        self.name == other.name
91            && self.public_id == other.public_id
92            && self.system_id == other.system_id
93    }
94}
95
96#[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan)]
97#[cfg_attr(
98    feature = "rkyv",
99    derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
100)]
101#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))]
102//#[cfg_attr(
103//    feature = "rkyv",
104//    archive(bound(serialize = "__S: rkyv::ser::ScratchSpace +
105// rkyv::ser::Serializer"))
106//)]
107#[cfg_attr(feature = "rkyv", repr(u32))]
108#[cfg_attr(
109    feature = "encoding-impl",
110    derive(::swc_common::Encode, ::swc_common::Decode)
111)]
112pub enum Namespace {
113    /// `http://www.w3.org/1999/xhtml`
114    HTML,
115    /// `http://www.w3.org/1998/Math/MathML`
116    MATHML,
117    /// `http://www.w3.org/2000/svg`
118    SVG,
119    /// `http://www.w3.org/1999/xlink`
120    XLINK,
121    /// `http://www.w3.org/XML/1998/namespace`
122    XML,
123    /// `http://www.w3.org/2000/xmlns/`
124    XMLNS,
125}
126
127#[ast_node("Element")]
128#[derive(Eq, Hash, EqIgnoreSpan)]
129pub struct Element {
130    pub span: Span,
131
132    pub tag_name: Atom,
133    pub namespace: Namespace,
134    pub attributes: Vec<Attribute>,
135    pub children: Vec<Child>,
136    /// For child nodes in `<template>`
137    #[cfg_attr(
138        feature = "encoding-impl",
139        encoding(with = "cbor4ii::core::types::Maybe")
140    )]
141    pub content: Option<DocumentFragment>,
142    pub is_self_closing: bool,
143}
144
145#[ast_node("Attribute")]
146#[derive(Eq, Hash)]
147pub struct Attribute {
148    pub span: Span,
149    #[cfg_attr(
150        feature = "encoding-impl",
151        encoding(with = "cbor4ii::core::types::Maybe")
152    )]
153    pub namespace: Option<Namespace>,
154
155    #[cfg_attr(
156        feature = "encoding-impl",
157        encoding(with = "cbor4ii::core::types::Maybe")
158    )]
159    pub prefix: Option<Atom>,
160
161    pub name: Atom,
162    #[cfg_attr(
163        feature = "encoding-impl",
164        encoding(with = "cbor4ii::core::types::Maybe")
165    )]
166    pub raw_name: Option<Atom>,
167
168    #[cfg_attr(
169        feature = "encoding-impl",
170        encoding(with = "cbor4ii::core::types::Maybe")
171    )]
172    pub value: Option<Atom>,
173    #[cfg_attr(
174        feature = "encoding-impl",
175        encoding(with = "cbor4ii::core::types::Maybe")
176    )]
177    pub raw_value: Option<Atom>,
178}
179
180impl EqIgnoreSpan for Attribute {
181    fn eq_ignore_span(&self, other: &Self) -> bool {
182        self.namespace == other.namespace
183            && self.prefix == other.prefix
184            && self.name == other.name
185            && self.value == other.value
186    }
187}
188
189#[ast_node("Text")]
190#[derive(Eq, Hash)]
191pub struct Text {
192    pub span: Span,
193
194    pub data: Atom,
195    #[cfg_attr(
196        feature = "encoding-impl",
197        encoding(with = "cbor4ii::core::types::Maybe")
198    )]
199    pub raw: Option<Atom>,
200}
201
202impl EqIgnoreSpan for Text {
203    fn eq_ignore_span(&self, other: &Self) -> bool {
204        self.data == other.data
205    }
206}
207
208#[ast_node("Comment")]
209#[derive(Eq, Hash)]
210pub struct Comment {
211    pub span: Span,
212
213    pub data: Atom,
214    #[cfg_attr(
215        feature = "encoding-impl",
216        encoding(with = "cbor4ii::core::types::Maybe")
217    )]
218    pub raw: Option<Atom>,
219}
220
221impl EqIgnoreSpan for Comment {
222    fn eq_ignore_span(&self, other: &Self) -> bool {
223        self.data == other.data
224    }
225}