forked from wilsonzlin/minify-html
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattrs.ts
More file actions
133 lines (120 loc) · 3.27 KB
/
attrs.ts
File metadata and controls
133 lines (120 loc) · 3.27 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import htmlData from "@wzlin/html-data";
import { writeFileSync } from "fs";
import { join } from "path";
import { RUST_OUT_DIR } from "./_common";
const rsTagAttr = ({
boolean = false,
caseInsensitive = false,
collapse = false,
defaultValue,
redundantIfEmpty = false,
trim = false,
}: {
boolean?: boolean;
caseInsensitive?: boolean;
collapse?: boolean;
defaultValue?: string;
redundantIfEmpty?: boolean;
trim?: boolean;
}) =>
`
AttributeMinification {
boolean: ${boolean},
case_insensitive: ${caseInsensitive},
collapse: ${collapse},
default_value: ${
defaultValue == undefined ? "None" : `Some(b"${defaultValue}")`
},
redundant_if_empty: ${redundantIfEmpty},
trim: ${trim},
}
`;
let code = `
use lazy_static::lazy_static;
use rustc_hash::FxHashMap;
use crate::common::spec::tag::ns::Namespace;
pub struct AttributeMinification {
pub boolean: bool,
pub case_insensitive: bool,
pub collapse: bool,
pub default_value: Option<&'static [u8]>,
pub redundant_if_empty: bool,
pub trim: bool,
}
pub enum AttrMapEntry {
AllNamespaceElements(AttributeMinification),
SpecificNamespaceElements(FxHashMap<&'static [u8], AttributeMinification>),
}
pub struct ByNamespace {
// Make pub so this struct can be statically created in gen/attrs.rs.
pub html: Option<AttrMapEntry>,
pub svg: Option<AttrMapEntry>,
}
impl ByNamespace {
fn get(&self, ns: Namespace) -> Option<&AttrMapEntry> {
match ns {
Namespace::Html => self.html.as_ref(),
Namespace::Svg => self.svg.as_ref(),
}
}
}
pub struct AttrMap(FxHashMap<&'static [u8], ByNamespace>);
impl AttrMap {
pub const fn new(map: FxHashMap<&'static [u8], ByNamespace>) -> AttrMap {
AttrMap(map)
}
pub fn get(&self, ns: Namespace, tag: &[u8], attr: &[u8]) -> Option<&AttributeMinification> {
self.0.get(attr).and_then(|namespaces| namespaces.get(ns)).and_then(|entry| match entry {
AttrMapEntry::AllNamespaceElements(min) => Some(min),
AttrMapEntry::SpecificNamespaceElements(map) => map.get(tag),
})
}
}
`;
code += `
lazy_static! {
pub static ref ATTRS: AttrMap = {
let mut m = FxHashMap::<&'static [u8], ByNamespace>::default();
${[...Object.entries(htmlData.attributes)]
.map(
([attr_name, namespaces]) => ` m.insert(b\"${attr_name}\", ByNamespace {
${(["html", "svg"] as const)
.map(
(ns) =>
` ${ns}: ` +
(() => {
const tagsMap = namespaces[ns];
if (!tagsMap) {
return "None";
}
const globalAttr = tagsMap["*"];
if (globalAttr) {
return `Some(AttrMapEntry::AllNamespaceElements(${rsTagAttr(
globalAttr
)}))`;
}
const entries = Object.entries(tagsMap);
return `Some({
let ${
entries.length ? "mut" : ""
} m = FxHashMap::<&'static [u8], AttributeMinification>::default();
${entries
.map(
([tagName, tagAttr]) =>
` m.insert(b\"${tagName}\", ${rsTagAttr(tagAttr)});`
)
.join("\n")}
AttrMapEntry::SpecificNamespaceElements(m)
})`;
})() +
","
)
.join("\n")}
});
`
)
.join("")}
AttrMap::new(m)
};
}`;
writeFileSync(join(RUST_OUT_DIR, "attrs.rs"), code);