-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
attributes.ts
129 lines (103 loc) Β· 2.74 KB
/
attributes.ts
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
import type { Helper, Site } from "../core.ts";
const escapeChars: Record<string, string> = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
/**
* A plugin to register the filters "attrs" and "class",
* which allow to handle HTML attributes and class names easily
*/
export default function () {
return (site: Site) => {
site.filter("attr", attributes as Helper);
site.filter("class", className);
};
}
export function attributes(values: unknown, ...validNames: string[]) {
const attributes = new Map();
handleAttributes(attributes, values, validNames);
return joinAttributes(attributes);
}
export function className(...names: unknown[]) {
const classes = new Set<string>();
names.forEach((name) => handleClass(classes, name));
return Array.from(classes).join(" ");
}
function handleClass(classes: Set<string>, name: unknown): void {
if (!name) {
return;
}
if (typeof name === "string") {
classes.add(name);
return;
}
if (Array.isArray(name)) {
return name.forEach((value) => handleClass(classes, value));
}
for (const [key, value] of Object.entries(name as Record<string, unknown>)) {
if (value) {
classes.add(key);
}
}
}
function handleAttributes(
attributes: Map<string, unknown>,
name: unknown,
validNames: string[],
): void {
if (!name) {
return;
}
if (typeof name === "string") {
if (isValid(name, validNames)) {
attributes.set(name, true);
}
return;
}
if (Array.isArray(name)) {
return name.forEach((value) =>
handleAttributes(attributes, value, validNames)
);
}
for (const [key, value] of Object.entries(name as Record<string, unknown>)) {
if (!isValid(key, validNames)) {
continue;
}
if (key === "class") {
const classList = (attributes.get("class") || new Set()) as Set<string>;
handleClass(classList, value);
attributes.set("class", classList);
continue;
}
attributes.set(key, value);
}
}
function joinAttributes(attributes: Map<string, unknown>) {
const values = [];
for (const [name, value] of attributes) {
if (value === undefined || value === null || value === false) {
continue;
}
if (value === true) {
values.push(name);
continue;
}
if (value instanceof Set) {
if (value.size) {
values.push(`${name}="${escape(Array.from(value).join(" "))}"`);
}
continue;
}
values.push(`${name}="${escape(value as string)}"`);
}
return values.join(" ");
}
function escape(value: string) {
return value.replace(/[&<>'"]/g, (match) => escapeChars[match]);
}
function isValid(name: string, validNames: string[]) {
return name && (!validNames.length || validNames.includes(name));
}