-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attrs.js
332 lines (301 loc) · 8.72 KB
/
attrs.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* @copyright 2023 Chris Zuber <admin@kernvalley.us>
*/
import { clamp, between } from './math.js';
import { isObject, isNullish } from './utility.js';
import { toHexColor, parseHexColor } from './color-utils.js';
import { setAttr, isScriptURL, isTrustedType } from './trust.js';
import { COLOR } from './patterns.js';
export function getAttrs(el) {
if (typeof el === 'string') {
return getAttrs(document.querySelector(el));
} else if (el instanceof Element) {
return Object.fromEntries([...el.attributes].map(({ name, value }) => [name, value]));
} else {
throw new TypeError(`Expected an element or selector but got a ${typeof el}`);
}
}
export function aria(el, props = {}) {
if (! (el instanceof Element)) {
throw new TypeError('`el` must be an Element');
} else {
Object.entries(props).forEach(([prop, val]) => {
if (prop !== 'role') {
prop = `aria-${prop.toLowerCase()}`;
}
if (isNullish(val)) {
el.removeAttribute(prop);
} else {
el.setAttribute(prop, val);
}
});
}
}
export function data(el, props = {}) {
if (! (el instanceof Element)) {
throw new TypeError('el must be an Element');
} if (! isObject(props)) {
throw new TypeError('props must be an object');
} else {
Object.entries(props).forEach(([p, v]) => {
switch (typeof v) {
case 'string':
case 'number':
el.dataset[p] = v.toString();
break;
case 'boolean':
if (v) {
el.dataset[p] = '';
} else {
delete el.dataset[p];
}
break;
case 'undefined':
delete el.dataset[p];
break;
case 'object':
if (Object.is(v, null)) {
delete el.dataset[p];
} else if (v instanceof Date) {
el.dataset[p] = v.toISOString();
} else if (v instanceof URL) {
el.dataset[p] = v.href;
} else {
el.dataset[p] = JSON.stringify(v);
}
break;
default:
throw new TypeError(`Unable to handle type: ${typeof v}`);
}
});
}
}
export function css(el, props = {}, { priority } = {}) {
if (! (el instanceof Element)) {
throw new TypeError('el must be an Element');
} if (! isObject(props)) {
throw new TypeError('props must be an object');
} else {
Object.entries(props).forEach(([p, v]) => {
if (typeof v === 'string' || typeof v === 'number') {
el.style.setProperty(p, v, priority);
} else if (v instanceof URL) {
el.type.setProperty(p, v.href, priority);
} else if (typeof v === 'object' && ! Object.is(v, null) && v.toString instanceof Function) {
el.style.setProperty(p, v.toString(), priority);
} else {
el.style.removeProperty(p);
}
});
}
}
export function attr(el, props = {}, {
namespace: elementNs = null,
policy,
} = {}) {
if (! (el instanceof Element)) {
throw new TypeError('el must be an Element');
} if (! isObject(props)) {
throw new TypeError('props must be an object');
} else {
Object.entries(props).forEach(([p, v]) => {
if (typeof v === 'string' || typeof v === 'number') {
setAttr(el, p, v, { policy, elementNs });
} else if ('trustedTypes' in globalThis && isTrustedType(v)) {
if (v.toString().length === 0) {
el.removeAttribute(p);
} else {
setAttr(el, p, v, { policy, elementNs });
}
} else if (typeof v === 'boolean') {
if (typeof namespace === 'string') {
v ? el.setAttributeNS(elementNs, p, '') : el.removeAttributeNS(elementNs, p);
} else {
el.toggleAttribute(p, v);
}
} else if (v instanceof Date) {
setAttr(el, p, v.toISOString(), { policy, elementNs });
} else if (v instanceof URL) {
setAttr(el, p, v.href, { policy, elementNs });
} else if (typeof v === 'undefined' || v === null) {
if (typeof namespace === 'string') {
el.removeAttributeNS(elementNs, p);
} else {
el.removeAttribute(p);
}
} else {
setAttr(el, p, JSON.stringify(v), { policy, elementNs });
}
});
}
}
export function getBool(el, attr) {
return el.hasAttribute(attr);
}
export function setBool(el, attr, val) {
el.toggleAttribute(attr, val);
}
export function getInt(el, attr, {
fallback = NaN,
min = Number.MIN_SAFE_INTEGER,
max = Number.MAX_SAFE_INTEGER,
} = {}) {
if (el.hasAttribute(attr)) {
const val = clamp(min, parseInt(el.getAttribute(attr)), max);
return Number.isNaN(val) ? fallback : val;
} else {
return fallback;
}
}
export function setInt(el, attr, val, {
min = Number.MIN_SAFE_INTEGER,
max = Number.MAX_SAFE_INTEGER,
policy,
} = {}) {
if (val instanceof Date) {
setAttr(el, attr, val.getTime(), { min, max, policy});
} else if (Number.isInteger(val)) {
setAttr(el, attr, clamp(min, val, max), { policy });
} else if (typeof val === 'string') {
setInt(el, attr, parseInt(val), { min, max });
} else {
el.removeAttribute(attr);
}
}
export function getFloat(el, attr, {
fallback = NaN,
min = Number.MIN_SAFE_INTEGER,
max = Number.MAX_SAFE_INTEGER,
} = {}) {
if (el.hasAttribute(attr)) {
const val = clamp(min, parseFloat(el.getAttribute(attr)), max);
return Number.isNaN(val) ? fallback : val;
} else {
return fallback;
}
}
export function setFloat(el, attr, val, {
min = Number.MIN_SAFE_INTEGER,
max = Number.MAX_SAFE_INTEGER,
policy,
} = {}) {
if (typeof val === 'number' && ! Number.isNaN(val)) {
setAttr(el, attr, clamp(min, val, max), { policy });
} else if (typeof val === 'string') {
setFloat(el, attr, parseFloat(val), { min, max });
} else {
el.removeAttribute(attr);
}
}
export function getString(el, attr, { fallback = null } = {}) {
if (el.hasAttribute(attr)) {
return el.getAttribute(attr) || fallback;
} else {
return fallback;
}
}
export function setString(el, attr, val, {
minLength = 1,
maxLength = Infinity,
pattern = null,
fallback,
policy,
} = {}) {
if (
typeof val === 'string'
&& between(minLength, val.length, maxLength)
&& (! (pattern instanceof RegExp) || pattern.test(val))
) {
setAttr(el, attr, val, { policy });
} else if(isTrustedType(val)) {
const str = val.toString();
if (
between(minLength, str.length, maxLength)
&& (! (pattern instanceof RegExp) || pattern.test(str))
) {
setAttr(el, attr, val, { policy });
} else {
el.removeAttribute(attr);
}
} else if (typeof fallback === 'string' || isTrustedType(fallback)) {
setString(el, attr, fallback, { pattern, policy, minLength, maxLength });
} else {
el.removeAttribute(attr);
}
}
export function getURL(el, attr, { base = document.baseURI } = {}) {
if (el.hasAttribute(attr)) {
return new URL(el.getAttribute(attr), base);
} else {
return null;
}
}
export function setURL(el, attr, val, {
base = document.baseURI,
requirePath = false,
policy,
} = {}) {
if ((val instanceof URL) && (! requirePath || val.pathname.length > 1)) {
setAttr(el, attr, val.href, { policy });
} else if (typeof val === 'string' && val.length !== 0) {
setURL(el, attr, new URL(val, base), { requirePath });
} else if (isScriptURL(val)) {
const url = new URL(val.toString(), document.baseURI);
if (! requirePath || url.pathname.length > 1) {
setAttr(el, attr, val, { policy });
} else {
el.removeAttribute(attr);
}
} else {
el.removeAttribute(attr);
}
}
export function getEnum(el, attr, {
allowed = [],
fallback = undefined,
}) {
if (! Array.isArray(allowed) || allowed.length === 0) {
throw new TypeError('`allowed` must be a non-empty array.');
} else if (el.hasAttribute(attr)) {
const val = el.getAttribute(attr);
return allowed.includes(val) ? val : fallback;
} else {
return fallback;
}
}
export function setEnum(el, attr, val, {
allowed = [],
fallback = undefined,
}) {
if (! Array.isArray(allowed) || allowed.length === 0) {
throw new TypeError('`allowed` must be a non-empty array.');
} else if (allowed.includes(val)) {
setAttr(el, attr, val);
} else if (! isNullish(fallback)) {
setAttr(el, attr, fallback);
} else {
el.removeAttribute(attr);
}
}
export function getColor(el, attr, { fallback } = {}) {
const val = getString(el, attr);
return typeof val === 'string' && COLOR.test(val) ? val : fallback;
}
export function setColor(el, attr, val, { fallback } = {}) {
setString(el, attr, val, { fallback, minLength: 3, maxLength: 9, pattern: COLOR });
}
export function getRGB(el, attr, { fallback = '#000000' } = {}) {
const { red = 0, green = 0, blue = 0 } = parseHexColor(getColor(el, attr, { fallback }));
return { red, green, blue };
}
export function setRGB(el, attr, { red = 0, green = 0, blue = 0 } = {}) {
setColor(el, attr, toHexColor({ red, green, blue }));
}
export function getRGBA(el, attr, { fallback = '#000000' } = {}) {
const { red = 0, green = 0, blue = 0, alpha = 1 } = parseHexColor(getColor(el, attr, { fallback }));
return { red, green, blue, alpha };
}
export function setRGBA(el, attr, { red = 0, green = 0, blue = 0, alpha = 1 } = {}) {
setColor(el, attr, toHexColor({ red, green, blue, alpha }));
}
export { setAttr };