swc_html_utils/
lib.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
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use swc_atoms::JsWord;
use swc_common::collections::AHashMap;

#[derive(Serialize, Deserialize, Debug)]
pub struct Entity {
    pub characters: String,
}

pub static HTML_ENTITIES: Lazy<AHashMap<String, Entity>> = Lazy::new(|| {
    let entities: AHashMap<String, Entity> =
        serde_json::from_str(include_str!("../data/entities.json"))
            .expect("failed to parse entities.json for html entities");

    entities
});

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AttributeInfo {
    #[serde(default)]
    pub initial: Option<String>,
    #[serde(default)]
    pub inherited: Option<bool>,
    #[serde(default)]
    pub boolean: Option<bool>,
    #[serde(default)]
    pub deprecated: Option<bool>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Element {
    _extends: Option<Vec<JsWord>>,
    #[serde(flatten)]
    pub other: AHashMap<JsWord, AttributeInfo>,
}

pub static HTML_ELEMENTS_AND_ATTRIBUTES: Lazy<AHashMap<JsWord, Element>> = Lazy::new(|| {
    let default_attributes: AHashMap<JsWord, Element> =
        serde_json::from_str(include_str!("../data/html_elements_and_attributes.json"))
            .expect("failed to parse html_elements_and_attributes.json for default attributes");

    default_attributes
});

pub static SVG_ELEMENTS_AND_ATTRIBUTES: Lazy<AHashMap<JsWord, Element>> = Lazy::new(|| {
    let svg_elements_and_attributes: AHashMap<JsWord, Element> =
        serde_json::from_str(include_str!("../data/svg_elements_and_attributes.json"))
            .expect("failed to parse svg_elements_and_attributes.json for default attributes");

    svg_elements_and_attributes
});