@oscarpalmer/toretto 0.32.0 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/attribute/index.js +2 -2
- package/dist/attribute/set.js +7 -12
- package/dist/data.js +2 -2
- package/dist/index.js +1 -1
- package/dist/internal/attribute.js +29 -36
- package/dist/internal/element-value.js +26 -13
- package/dist/style.js +3 -3
- package/dist/toretto.full.js +258 -131
- package/package.json +7 -7
- package/src/attribute/set.ts +52 -56
- package/src/data.ts +2 -1
- package/src/index.ts +1 -1
- package/src/internal/attribute.ts +51 -55
- package/src/internal/element-value.ts +43 -13
- package/src/models.ts +2 -7
- package/src/style.ts +3 -2
- package/types/attribute/set.d.ts +19 -37
- package/types/index.d.ts +1 -1
- package/types/internal/attribute.d.ts +3 -4
- package/types/internal/element-value.d.ts +3 -3
- package/types/models.d.ts +2 -6
package/dist/toretto.full.js
CHANGED
|
@@ -25,33 +25,23 @@ var touch_default = (() => {
|
|
|
25
25
|
} });
|
|
26
26
|
return instance;
|
|
27
27
|
})();
|
|
28
|
+
function isNumber(value) {
|
|
29
|
+
return typeof value === "number" && !Number.isNaN(value);
|
|
30
|
+
}
|
|
28
31
|
function isPlainObject(value) {
|
|
29
32
|
if (value === null || typeof value !== "object") return false;
|
|
30
33
|
if (Symbol.toStringTag in value || Symbol.iterator in value) return false;
|
|
31
34
|
const prototype = Object.getPrototypeOf(value);
|
|
32
35
|
return prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null;
|
|
33
36
|
}
|
|
34
|
-
new Set([
|
|
35
|
-
Int8Array,
|
|
36
|
-
Uint8Array,
|
|
37
|
-
Uint8ClampedArray,
|
|
38
|
-
Int16Array,
|
|
39
|
-
Uint16Array,
|
|
40
|
-
Int32Array,
|
|
41
|
-
Uint32Array,
|
|
42
|
-
Float32Array,
|
|
43
|
-
Float64Array,
|
|
44
|
-
BigInt64Array,
|
|
45
|
-
BigUint64Array
|
|
46
|
-
]);
|
|
47
37
|
function compact(array, strict) {
|
|
48
38
|
if (!Array.isArray(array)) return [];
|
|
39
|
+
if (strict === true) return array.filter(Boolean);
|
|
49
40
|
const { length } = array;
|
|
50
|
-
const isStrict = strict ?? false;
|
|
51
41
|
const compacted = [];
|
|
52
42
|
for (let index = 0; index < length; index += 1) {
|
|
53
43
|
const item = array[index];
|
|
54
|
-
if (
|
|
44
|
+
if (item != null) compacted.push(item);
|
|
55
45
|
}
|
|
56
46
|
return compacted;
|
|
57
47
|
}
|
|
@@ -74,64 +64,66 @@ function isNullableOrWhitespace(value) {
|
|
|
74
64
|
return value == null || EXPRESSION_WHITESPACE$1.test(getString(value));
|
|
75
65
|
}
|
|
76
66
|
var EXPRESSION_WHITESPACE$1 = /^\s*$/;
|
|
77
|
-
function
|
|
78
|
-
return
|
|
67
|
+
function isEventTarget(value) {
|
|
68
|
+
return typeof value === "object" && value != null && typeof value.addEventListener === "function" && typeof value.removeEventListener === "function" && typeof value.dispatchEvent === "function";
|
|
79
69
|
}
|
|
80
|
-
function
|
|
81
|
-
|
|
82
|
-
return value.length === 1 ? value.toLocaleUpperCase() : `${value.charAt(0).toLocaleUpperCase()}${value.slice(1).toLocaleLowerCase()}`;
|
|
70
|
+
function isHTMLOrSVGElement(value) {
|
|
71
|
+
return value instanceof HTMLElement || value instanceof SVGElement;
|
|
83
72
|
}
|
|
84
|
-
function
|
|
85
|
-
return
|
|
73
|
+
function isChildNode(value) {
|
|
74
|
+
return value instanceof Node && CHILD_NODE_TYPES.has(value.nodeType);
|
|
86
75
|
}
|
|
87
|
-
function
|
|
88
|
-
if (
|
|
89
|
-
if (
|
|
90
|
-
|
|
91
|
-
const partsLength = parts.length;
|
|
92
|
-
const result = [];
|
|
93
|
-
for (let partIndex = 0; partIndex < partsLength; partIndex += 1) {
|
|
94
|
-
const items = parts[partIndex].replace(EXPRESSION_ACRONYM, (full, one, two, three) => three === "s" ? full : `${one}-${two}${three}`).replace(EXPRESSION_CAMEL_CASE, "$1-$2").split("-");
|
|
95
|
-
const itemsLength = items.length;
|
|
96
|
-
const partResult = [];
|
|
97
|
-
let itemCount = 0;
|
|
98
|
-
for (let itemIndex = 0; itemIndex < itemsLength; itemIndex += 1) {
|
|
99
|
-
const item = items[itemIndex];
|
|
100
|
-
if (item.length === 0) continue;
|
|
101
|
-
if (!capitalizeAny || itemCount === 0 && partIndex === 0 && !capitalizeFirst) partResult.push(item.toLocaleLowerCase());
|
|
102
|
-
else partResult.push(capitalize(item));
|
|
103
|
-
itemCount += 1;
|
|
104
|
-
}
|
|
105
|
-
result.push(join(partResult, delimiter));
|
|
106
|
-
}
|
|
107
|
-
return join(result, delimiter);
|
|
76
|
+
function isInDocument(node, doc) {
|
|
77
|
+
if (!(node instanceof Node)) return false;
|
|
78
|
+
if (!(doc instanceof Document)) return node.ownerDocument?.contains(node) ?? true;
|
|
79
|
+
return node.ownerDocument == null ? node === doc : node.ownerDocument === doc && doc.contains(node);
|
|
108
80
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
81
|
+
const CHILD_NODE_TYPES = new Set([
|
|
82
|
+
Node.ELEMENT_NODE,
|
|
83
|
+
Node.TEXT_NODE,
|
|
84
|
+
Node.PROCESSING_INSTRUCTION_NODE,
|
|
85
|
+
Node.COMMENT_NODE,
|
|
86
|
+
Node.DOCUMENT_TYPE_NODE
|
|
87
|
+
]);
|
|
88
|
+
function setElementValue(element, first, second, third, callback) {
|
|
89
|
+
if (!isHTMLOrSVGElement(element)) return;
|
|
90
|
+
if (typeof first === "string") setElementValues(element, first, second, third, callback);
|
|
91
|
+
else if (isAttribute(first)) setElementValues(element, first.name, first.value, third, callback);
|
|
92
|
+
}
|
|
93
|
+
function setElementValues(element, first, second, third, callback) {
|
|
94
|
+
if (!isHTMLOrSVGElement(element)) return;
|
|
95
|
+
if (typeof first === "string") {
|
|
96
|
+
callback(element, first, second, third);
|
|
115
97
|
return;
|
|
116
98
|
}
|
|
99
|
+
const isArray = Array.isArray(first);
|
|
100
|
+
if (!isArray && !(typeof first === "object" && first !== null)) return;
|
|
101
|
+
const entries = isArray ? first : Object.entries(first).map(([name, value]) => ({
|
|
102
|
+
name,
|
|
103
|
+
value
|
|
104
|
+
}));
|
|
105
|
+
const { length } = entries;
|
|
106
|
+
for (let index = 0; index < length; index += 1) {
|
|
107
|
+
const entry = entries[index];
|
|
108
|
+
if (typeof entry === "object" && typeof entry?.name === "string") callback(element, entry.name, entry.value, third);
|
|
109
|
+
}
|
|
117
110
|
}
|
|
118
|
-
function
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
function isHTMLOrSVGElement(value) {
|
|
122
|
-
return value instanceof HTMLElement || value instanceof SVGElement;
|
|
111
|
+
function updateElementValue(element, key, value, set, remove, isBoolean, json) {
|
|
112
|
+
if (isBoolean ? value == null : isNullableOrWhitespace(value)) remove.call(element, key);
|
|
113
|
+
else set.call(element, key, json ? JSON.stringify(value) : String(value));
|
|
123
114
|
}
|
|
124
115
|
function badAttributeHandler(name, value) {
|
|
125
|
-
if (name
|
|
116
|
+
if (typeof name !== "string" || name.trim().length === 0 || typeof value !== "string") return true;
|
|
126
117
|
if (EXPRESSION_CLOBBERED_NAME.test(name) && (value in document || value in formElement) || EXPRESSION_EVENT_NAME.test(name)) return true;
|
|
127
118
|
if (EXPRESSION_SKIP_NAME.test(name) || EXPRESSION_URI_VALUE.test(value) || isValidSourceAttribute(name, value)) return false;
|
|
128
119
|
return EXPRESSION_DATA_OR_SCRIPT.test(value);
|
|
129
120
|
}
|
|
130
121
|
function booleanAttributeHandler(name, value) {
|
|
131
|
-
if (name
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
122
|
+
if (typeof name !== "string" || name.trim().length === 0 || typeof value !== "string") return true;
|
|
123
|
+
const normalizedName = name.toLowerCase();
|
|
124
|
+
if (!booleanAttributesSet.has(normalizedName)) return false;
|
|
125
|
+
const normalized = value.toLowerCase();
|
|
126
|
+
return !(normalized.length === 0 || normalized === normalizedName);
|
|
135
127
|
}
|
|
136
128
|
function decodeAttribute(value) {
|
|
137
129
|
textArea ??= document.createElement("textarea");
|
|
@@ -152,7 +144,7 @@ function handleAttribute(callback, decode, first, second) {
|
|
|
152
144
|
return callback(name, value?.replace(EXPRESSION_WHITESPACE, ""));
|
|
153
145
|
}
|
|
154
146
|
function isAttribute(value) {
|
|
155
|
-
return value instanceof Attr || isPlainObject(value) && typeof value.name === "string" &&
|
|
147
|
+
return value instanceof Attr || isPlainObject(value) && typeof value.name === "string" && "value" in value;
|
|
156
148
|
}
|
|
157
149
|
function _isBadAttribute(first, second, decode) {
|
|
158
150
|
return handleAttribute(badAttributeHandler, decode, first, second);
|
|
@@ -166,26 +158,21 @@ function _isEmptyNonBooleanAttribute(first, second, decode) {
|
|
|
166
158
|
function _isInvalidBooleanAttribute(first, second, decode) {
|
|
167
159
|
return handleAttribute(booleanAttributeHandler, decode, first, second);
|
|
168
160
|
}
|
|
169
|
-
function isProperty(value) {
|
|
170
|
-
return isPlainObject(value) && typeof value.name === "string";
|
|
171
|
-
}
|
|
172
161
|
function isValidSourceAttribute(name, value) {
|
|
173
162
|
return EXPRESSION_SOURCE_NAME.test(name) && EXPRESSION_SOURCE_VALUE.test(value);
|
|
174
163
|
}
|
|
175
|
-
function updateAttribute(element, name, value) {
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
164
|
+
function updateAttribute(element, name, value, dispatch$1) {
|
|
165
|
+
const normalizedName = name.toLowerCase();
|
|
166
|
+
const isBoolean = booleanAttributesSet.has(normalizedName);
|
|
167
|
+
const next = isBoolean ? value === true || typeof value === "string" && (value === "" || value.toLowerCase() === normalizedName) : value == null ? "" : value;
|
|
168
|
+
if (name in element) updateProperty(element, normalizedName, next, dispatch$1);
|
|
169
|
+
updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute, isBoolean, false);
|
|
180
170
|
}
|
|
181
|
-
function updateProperty(element, name, value) {
|
|
182
|
-
|
|
183
|
-
element[
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if (!isHTMLOrSVGElement(element)) return;
|
|
187
|
-
if (isProperty(first)) updateAttribute(element, first.name, first.value);
|
|
188
|
-
else if (typeof first === "string") updateAttribute(element, first, second);
|
|
171
|
+
function updateProperty(element, name, value, dispatch$1) {
|
|
172
|
+
if (Object.is(element[name], value)) return;
|
|
173
|
+
element[name] = value;
|
|
174
|
+
const event = dispatch$1 !== false && elementEvents[element.tagName]?.[name];
|
|
175
|
+
if (typeof event === "string") element.dispatchEvent(new Event(event, { bubbles: true }));
|
|
189
176
|
}
|
|
190
177
|
const EXPRESSION_CLOBBERED_NAME = /^(id|name)$/i;
|
|
191
178
|
const EXPRESSION_DATA_OR_SCRIPT = /^(?:data|\w+script):/i;
|
|
@@ -222,8 +209,196 @@ const booleanAttributes = Object.freeze([
|
|
|
222
209
|
"selected"
|
|
223
210
|
]);
|
|
224
211
|
const booleanAttributesSet = new Set(booleanAttributes);
|
|
212
|
+
const elementEvents = {
|
|
213
|
+
DETAILS: { open: "toggle" },
|
|
214
|
+
INPUT: {
|
|
215
|
+
checked: "change",
|
|
216
|
+
value: "input"
|
|
217
|
+
},
|
|
218
|
+
SELECT: { value: "change" },
|
|
219
|
+
TEXTAREA: { value: "input" }
|
|
220
|
+
};
|
|
225
221
|
const formElement = document.createElement("form");
|
|
226
222
|
let textArea;
|
|
223
|
+
function noop() {}
|
|
224
|
+
function calculate() {
|
|
225
|
+
return new Promise((resolve) => {
|
|
226
|
+
const values = [];
|
|
227
|
+
let last;
|
|
228
|
+
function step(now) {
|
|
229
|
+
if (last != null) values.push(now - last);
|
|
230
|
+
last = now;
|
|
231
|
+
if (values.length >= TOTAL) resolve(values.sort().slice(TRIM_PART, -TRIM_PART).reduce((first, second) => first + second, 0) / (values.length - TRIM_TOTAL));
|
|
232
|
+
else requestAnimationFrame(step);
|
|
233
|
+
}
|
|
234
|
+
requestAnimationFrame(step);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
var TOTAL = 10;
|
|
238
|
+
var TRIM_PART = 2;
|
|
239
|
+
var TRIM_TOTAL = 4;
|
|
240
|
+
await calculate();
|
|
241
|
+
function clamp(value, minimum, maximum, loop) {
|
|
242
|
+
if (![
|
|
243
|
+
value,
|
|
244
|
+
minimum,
|
|
245
|
+
maximum
|
|
246
|
+
].every(isNumber)) return NaN;
|
|
247
|
+
if (value < minimum) return loop === true ? maximum : minimum;
|
|
248
|
+
return value > maximum ? loop === true ? minimum : maximum : value;
|
|
249
|
+
}
|
|
250
|
+
var SizedMap = class extends Map {
|
|
251
|
+
#maximumSize;
|
|
252
|
+
get full() {
|
|
253
|
+
return this.size >= this.#maximumSize;
|
|
254
|
+
}
|
|
255
|
+
get maximum() {
|
|
256
|
+
return this.#maximumSize;
|
|
257
|
+
}
|
|
258
|
+
constructor(first, second) {
|
|
259
|
+
const maximum = getMaximum(first, second);
|
|
260
|
+
super();
|
|
261
|
+
this.#maximumSize = maximum;
|
|
262
|
+
if (Array.isArray(first)) {
|
|
263
|
+
const { length } = first;
|
|
264
|
+
if (length <= maximum) for (let index = 0; index < length; index += 1) this.set(...first[index]);
|
|
265
|
+
else for (let index = 0; index < maximum; index += 1) this.set(...first[length - maximum + index]);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
get(key) {
|
|
269
|
+
const value = super.get(key);
|
|
270
|
+
if (value !== void 0 || this.has(key)) this.set(key, value);
|
|
271
|
+
return value;
|
|
272
|
+
}
|
|
273
|
+
set(key, value) {
|
|
274
|
+
if (this.has(key)) this.delete(key);
|
|
275
|
+
else if (this.size >= this.#maximumSize) this.delete(this.keys().next().value);
|
|
276
|
+
return super.set(key, value);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
function getMaximum(first, second) {
|
|
280
|
+
let actual;
|
|
281
|
+
if (typeof first === "number") actual = first;
|
|
282
|
+
else actual = typeof second === "number" ? second : MAXIMUM_DEFAULT;
|
|
283
|
+
return clamp(actual, 1, MAXIMUM_ABSOLUTE);
|
|
284
|
+
}
|
|
285
|
+
var MAXIMUM_ABSOLUTE = 16777216;
|
|
286
|
+
var MAXIMUM_DEFAULT = 1048576;
|
|
287
|
+
var Memoized = class {
|
|
288
|
+
#state;
|
|
289
|
+
get maximum() {
|
|
290
|
+
return this.#state.cache?.maximum ?? NaN;
|
|
291
|
+
}
|
|
292
|
+
get size() {
|
|
293
|
+
return this.#state.cache?.size ?? NaN;
|
|
294
|
+
}
|
|
295
|
+
constructor(callback, options) {
|
|
296
|
+
const cache = new SizedMap(options.cacheSize);
|
|
297
|
+
const getter = (...parameters) => {
|
|
298
|
+
const key = options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString), "_"));
|
|
299
|
+
if (cache.has(key)) return cache.get(key);
|
|
300
|
+
const value = callback(...parameters);
|
|
301
|
+
cache.set(key, value);
|
|
302
|
+
return value;
|
|
303
|
+
};
|
|
304
|
+
this.#state = {
|
|
305
|
+
cache,
|
|
306
|
+
getter
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
clear() {
|
|
310
|
+
this.#state.cache?.clear();
|
|
311
|
+
}
|
|
312
|
+
delete(key) {
|
|
313
|
+
return this.#state.cache?.delete(key) ?? false;
|
|
314
|
+
}
|
|
315
|
+
destroy() {
|
|
316
|
+
this.#state.cache?.clear();
|
|
317
|
+
this.#state.cache = void 0;
|
|
318
|
+
this.#state.getter = void 0;
|
|
319
|
+
}
|
|
320
|
+
get(key) {
|
|
321
|
+
return this.#state.cache?.get(key);
|
|
322
|
+
}
|
|
323
|
+
has(key) {
|
|
324
|
+
return this.#state.cache?.has(key) ?? false;
|
|
325
|
+
}
|
|
326
|
+
run(...parameters) {
|
|
327
|
+
if (this.#state.cache == null || this.#state.getter == null) throw new Error("The Memoized instance has been destroyed");
|
|
328
|
+
return this.#state.getter(...parameters);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
function getMemoizationOptions(input) {
|
|
332
|
+
const { cacheKey, cacheSize } = isPlainObject(input) ? input : {};
|
|
333
|
+
return {
|
|
334
|
+
cacheKey: typeof cacheKey === "function" ? cacheKey : void 0,
|
|
335
|
+
cacheSize: typeof cacheSize === "number" && cacheSize > 0 ? cacheSize : DEFAULT_CACHE_SIZE
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
function memoize(callback, options) {
|
|
339
|
+
return new Memoized(callback, getMemoizationOptions(options));
|
|
340
|
+
}
|
|
341
|
+
var DEFAULT_CACHE_SIZE = 1024;
|
|
342
|
+
function camelCase(value) {
|
|
343
|
+
return toCase("camel", value, true, false);
|
|
344
|
+
}
|
|
345
|
+
function capitalize(value) {
|
|
346
|
+
if (typeof value !== "string" || value.length === 0) return "";
|
|
347
|
+
memoizedCapitalize ??= memoize((v) => v.length === 1 ? v.toLocaleUpperCase() : `${v.charAt(0).toLocaleUpperCase()}${v.slice(1).toLocaleLowerCase()}`);
|
|
348
|
+
return memoizedCapitalize.run(value);
|
|
349
|
+
}
|
|
350
|
+
function kebabCase(value) {
|
|
351
|
+
return toCase("kebab", value, false, false);
|
|
352
|
+
}
|
|
353
|
+
function toCase(type, value, capitalizeAny, capitalizeFirst) {
|
|
354
|
+
memoizers[type] ??= memoize(toCaseCallback.bind({
|
|
355
|
+
type,
|
|
356
|
+
capitalizeAny,
|
|
357
|
+
capitalizeFirst
|
|
358
|
+
}));
|
|
359
|
+
return memoizers[type].run(value);
|
|
360
|
+
}
|
|
361
|
+
function toCaseCallback(value) {
|
|
362
|
+
if (typeof value !== "string") return "";
|
|
363
|
+
if (value.length < 1) return value;
|
|
364
|
+
const { capitalizeAny, capitalizeFirst, type } = this;
|
|
365
|
+
const parts = words(value);
|
|
366
|
+
const partsLength = parts.length;
|
|
367
|
+
const result = [];
|
|
368
|
+
for (let partIndex = 0; partIndex < partsLength; partIndex += 1) {
|
|
369
|
+
const items = parts[partIndex].replace(EXPRESSION_ACRONYM, (full, one, two, three) => three === "s" ? full : `${one}-${two}${three}`).replace(EXPRESSION_CAMEL_CASE, REPLACEMENT_CAMEL_CASE).split("-");
|
|
370
|
+
const itemsLength = items.length;
|
|
371
|
+
const partResult = [];
|
|
372
|
+
let itemCount = 0;
|
|
373
|
+
for (let itemIndex = 0; itemIndex < itemsLength; itemIndex += 1) {
|
|
374
|
+
const item = items[itemIndex];
|
|
375
|
+
if (item.length === 0) continue;
|
|
376
|
+
if (!capitalizeAny || itemCount === 0 && partIndex === 0 && !capitalizeFirst) partResult.push(item.toLocaleLowerCase());
|
|
377
|
+
else partResult.push(capitalize(item));
|
|
378
|
+
itemCount += 1;
|
|
379
|
+
}
|
|
380
|
+
result.push(join(partResult, delimiters[type]));
|
|
381
|
+
}
|
|
382
|
+
return join(result, delimiters[type]);
|
|
383
|
+
}
|
|
384
|
+
var EXPRESSION_CAMEL_CASE = /(\p{Ll})(\p{Lu})/gu;
|
|
385
|
+
var EXPRESSION_ACRONYM = /(\p{Lu}*)(\p{Lu})(\p{Ll}+)/gu;
|
|
386
|
+
var REPLACEMENT_CAMEL_CASE = "$1-$2";
|
|
387
|
+
var delimiters = {
|
|
388
|
+
camel: "",
|
|
389
|
+
kebab: "-",
|
|
390
|
+
pascal: "",
|
|
391
|
+
snake: "_"
|
|
392
|
+
};
|
|
393
|
+
var memoizers = {};
|
|
394
|
+
var memoizedCapitalize;
|
|
395
|
+
function parse(value, reviver) {
|
|
396
|
+
try {
|
|
397
|
+
return JSON.parse(value, reviver);
|
|
398
|
+
} catch {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
227
402
|
function getBoolean(value, defaultValue) {
|
|
228
403
|
return typeof value === "boolean" ? value : defaultValue ?? false;
|
|
229
404
|
}
|
|
@@ -232,8 +407,8 @@ function getStyleValue(element, property, computed) {
|
|
|
232
407
|
return computed ? getComputedStyle(element)[name] : element.style[name];
|
|
233
408
|
}
|
|
234
409
|
const EXPRESSION_DATA_PREFIX = /^data-/i;
|
|
235
|
-
function setAttribute(element, first, second) {
|
|
236
|
-
|
|
410
|
+
function setAttribute(element, first, second, third) {
|
|
411
|
+
setElementValue(element, first, second, third, updateAttribute);
|
|
237
412
|
}
|
|
238
413
|
function isBadAttribute(first, second) {
|
|
239
414
|
return _isBadAttribute(first, second, true);
|
|
@@ -247,36 +422,6 @@ function isEmptyNonBooleanAttribute(first, second) {
|
|
|
247
422
|
function isInvalidBooleanAttribute(first, second) {
|
|
248
423
|
return _isInvalidBooleanAttribute(first, second, true);
|
|
249
424
|
}
|
|
250
|
-
function isChildNode(value) {
|
|
251
|
-
return value instanceof Node && CHILD_NODE_TYPES.has(value.nodeType);
|
|
252
|
-
}
|
|
253
|
-
function isInDocument(node, doc) {
|
|
254
|
-
if (!(node instanceof Node)) return false;
|
|
255
|
-
if (!(doc instanceof Document)) return node.ownerDocument?.contains(node) ?? true;
|
|
256
|
-
return node.ownerDocument == null ? node === doc : node.ownerDocument === doc && doc.contains(node);
|
|
257
|
-
}
|
|
258
|
-
const CHILD_NODE_TYPES = new Set([
|
|
259
|
-
Node.ELEMENT_NODE,
|
|
260
|
-
Node.TEXT_NODE,
|
|
261
|
-
Node.PROCESSING_INSTRUCTION_NODE,
|
|
262
|
-
Node.COMMENT_NODE,
|
|
263
|
-
Node.DOCUMENT_TYPE_NODE
|
|
264
|
-
]);
|
|
265
|
-
function setElementValues(element, first, second, callback) {
|
|
266
|
-
if (!isHTMLOrSVGElement(element)) return;
|
|
267
|
-
if (isPlainObject(first)) {
|
|
268
|
-
const entries = Object.entries(first);
|
|
269
|
-
const { length } = entries;
|
|
270
|
-
for (let index = 0; index < length; index += 1) {
|
|
271
|
-
const [key, value] = entries[index];
|
|
272
|
-
callback(element, key, value);
|
|
273
|
-
}
|
|
274
|
-
} else if (typeof first === "string") callback(element, first, second);
|
|
275
|
-
}
|
|
276
|
-
function updateElementValue(element, key, value, set, remove, json) {
|
|
277
|
-
if (isNullableOrWhitespace(value)) remove.call(element, key);
|
|
278
|
-
else set.call(element, key, json ? JSON.stringify(value) : String(value));
|
|
279
|
-
}
|
|
280
425
|
function getData(element, keys, parseValues) {
|
|
281
426
|
if (!isHTMLOrSVGElement(element)) return;
|
|
282
427
|
const shouldParse = parseValues !== false;
|
|
@@ -299,30 +444,12 @@ function getName(original) {
|
|
|
299
444
|
return `${ATTRIBUTE_DATA_PREFIX}${kebabCase(original).replace(EXPRESSION_DATA_PREFIX, "")}`;
|
|
300
445
|
}
|
|
301
446
|
function setData(element, first, second) {
|
|
302
|
-
setElementValues(element, first, second, updateDataAttribute);
|
|
447
|
+
setElementValues(element, first, second, null, updateDataAttribute);
|
|
303
448
|
}
|
|
304
449
|
function updateDataAttribute(element, key, value) {
|
|
305
|
-
updateElementValue(element, getName(key), value, element.setAttribute, element.removeAttribute, true);
|
|
450
|
+
updateElementValue(element, getName(key), value, element.setAttribute, element.removeAttribute, false, true);
|
|
306
451
|
}
|
|
307
452
|
const ATTRIBUTE_DATA_PREFIX = "data-";
|
|
308
|
-
function noop() {}
|
|
309
|
-
function calculate() {
|
|
310
|
-
return new Promise((resolve) => {
|
|
311
|
-
const values = [];
|
|
312
|
-
let last;
|
|
313
|
-
function step(now) {
|
|
314
|
-
if (last != null) values.push(now - last);
|
|
315
|
-
last = now;
|
|
316
|
-
if (values.length >= CALCULATION_TOTAL) resolve(values.sort().slice(CALCULATION_TRIM_PART, -CALCULATION_TRIM_PART).reduce((first, second) => first + second, 0) / (values.length - CALCULATION_TRIM_TOTAL));
|
|
317
|
-
else requestAnimationFrame(step);
|
|
318
|
-
}
|
|
319
|
-
requestAnimationFrame(step);
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
var CALCULATION_TOTAL = 10;
|
|
323
|
-
var CALCULATION_TRIM_PART = 2;
|
|
324
|
-
var CALCULATION_TRIM_TOTAL = 4;
|
|
325
|
-
calculate().then((value) => {});
|
|
326
453
|
function addDelegatedHandler(doc, type, name, passive) {
|
|
327
454
|
if (DELEGATED.has(name)) return;
|
|
328
455
|
DELEGATED.add(name);
|
|
@@ -868,10 +995,10 @@ function getTextDirection(element, computed) {
|
|
|
868
995
|
return value === "rtl" ? value : "ltr";
|
|
869
996
|
}
|
|
870
997
|
function setStyle(element, property, value) {
|
|
871
|
-
setElementValues(element, property, value, updateStyleProperty);
|
|
998
|
+
setElementValues(element, property, value, null, updateStyleProperty);
|
|
872
999
|
}
|
|
873
1000
|
function setStyles(element, styles) {
|
|
874
|
-
setElementValues(element, styles, null, updateStyleProperty);
|
|
1001
|
+
setElementValues(element, styles, null, null, updateStyleProperty);
|
|
875
1002
|
}
|
|
876
1003
|
function toggleStyles(element, styles) {
|
|
877
1004
|
function toggle(set) {
|
|
@@ -904,7 +1031,7 @@ function updateStyleProperty(element, key, value) {
|
|
|
904
1031
|
this.style[property] = style;
|
|
905
1032
|
}, function(property) {
|
|
906
1033
|
this.style[property] = "";
|
|
907
|
-
}, false);
|
|
1034
|
+
}, false, false);
|
|
908
1035
|
}
|
|
909
1036
|
const ATTRIBUTE_DIRECTION = "dir";
|
|
910
1037
|
const EXPRESSION_DIRECTION = /^(ltr|rtl)$/i;
|
package/package.json
CHANGED
|
@@ -4,19 +4,19 @@
|
|
|
4
4
|
"url": "https://oscarpalmer.se"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@oscarpalmer/atoms": "^0.
|
|
7
|
+
"@oscarpalmer/atoms": "^0.124"
|
|
8
8
|
},
|
|
9
9
|
"description": "A collection of badass DOM utilities.",
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"@types/node": "^25",
|
|
12
12
|
"@vitest/coverage-istanbul": "^4",
|
|
13
|
-
"jsdom": "^27.
|
|
14
|
-
"oxfmt": "^0.
|
|
15
|
-
"oxlint": "^1.
|
|
16
|
-
"rolldown": "1.0.0-beta.
|
|
13
|
+
"jsdom": "^27.4",
|
|
14
|
+
"oxfmt": "^0.21",
|
|
15
|
+
"oxlint": "^1.36",
|
|
16
|
+
"rolldown": "1.0.0-beta.57",
|
|
17
17
|
"tslib": "^2.8",
|
|
18
18
|
"typescript": "^5.9",
|
|
19
|
-
"vite": "8.0.0-beta.
|
|
19
|
+
"vite": "8.0.0-beta.5",
|
|
20
20
|
"vitest": "^4"
|
|
21
21
|
},
|
|
22
22
|
"exports": {
|
|
@@ -93,5 +93,5 @@
|
|
|
93
93
|
},
|
|
94
94
|
"type": "module",
|
|
95
95
|
"types": "types/index.d.ts",
|
|
96
|
-
"version": "0.
|
|
96
|
+
"version": "0.34.0"
|
|
97
97
|
}
|
package/src/attribute/set.ts
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import {updateAttribute} from '../internal/attribute';
|
|
2
|
+
import {setElementValue, setElementValues} from '../internal/element-value';
|
|
3
|
+
import type {Attribute} from '../models';
|
|
4
|
+
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
type DispatchedAttribute = 'checked' | 'open' | 'value';
|
|
8
|
+
|
|
9
|
+
//
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Set an attribute on an element
|
|
13
|
+
*
|
|
14
|
+
* _(Or remove it, if value is `null` or `undefined`)_
|
|
15
|
+
* @param element Element for attribute
|
|
16
|
+
* @param name Attribute name
|
|
17
|
+
* @param value Attribute value
|
|
18
|
+
* @param dispatch Dispatch event for attribute? _(defaults to `true`)_
|
|
19
|
+
*/
|
|
20
|
+
export function setAttribute<Name extends DispatchedAttribute>(
|
|
21
|
+
element: Element,
|
|
22
|
+
name: Name,
|
|
23
|
+
value?: unknown,
|
|
24
|
+
dispatch?: boolean,
|
|
25
|
+
): void;
|
|
3
26
|
|
|
4
27
|
/**
|
|
5
28
|
* Set an attribute on an element
|
|
@@ -17,11 +40,21 @@ export function setAttribute(element: Element, name: string, value?: unknown): v
|
|
|
17
40
|
* _(Or remove it, if value is `null` or `undefined`)_
|
|
18
41
|
* @param element Element for attribute
|
|
19
42
|
* @param attribute Attribute to set
|
|
43
|
+
* @param dispatch Dispatch event for attribute? _(defaults to `true`)_
|
|
20
44
|
*/
|
|
21
|
-
export function setAttribute(
|
|
45
|
+
export function setAttribute(
|
|
46
|
+
element: Element,
|
|
47
|
+
attribute: Attr | Attribute,
|
|
48
|
+
dispatch?: boolean,
|
|
49
|
+
): void;
|
|
22
50
|
|
|
23
|
-
export function setAttribute(
|
|
24
|
-
|
|
51
|
+
export function setAttribute(
|
|
52
|
+
element: Element,
|
|
53
|
+
first: unknown,
|
|
54
|
+
second?: unknown,
|
|
55
|
+
third?: unknown,
|
|
56
|
+
): void {
|
|
57
|
+
setElementValue(element, first, second, third, updateAttribute);
|
|
25
58
|
}
|
|
26
59
|
|
|
27
60
|
/**
|
|
@@ -30,8 +63,13 @@ export function setAttribute(element: Element, first: unknown, second?: unknown)
|
|
|
30
63
|
* _(Or remove them, if their value is `null` or `undefined`)_
|
|
31
64
|
* @param element Element for attributes
|
|
32
65
|
* @param attributes Attributes to set
|
|
66
|
+
* @param dispatch Dispatch events for relevant attributes? _(defaults to `true`)_
|
|
33
67
|
*/
|
|
34
|
-
export function setAttributes(
|
|
68
|
+
export function setAttributes(
|
|
69
|
+
element: Element,
|
|
70
|
+
attributes: Array<Attr | Attribute>,
|
|
71
|
+
dispatch?: boolean,
|
|
72
|
+
): void;
|
|
35
73
|
|
|
36
74
|
/**
|
|
37
75
|
* Set one or more attributes on an element
|
|
@@ -39,60 +77,18 @@ export function setAttributes(element: Element, attributes: Array<Attr | Attribu
|
|
|
39
77
|
* _(Or remove them, if their value is `null` or `undefined`)_
|
|
40
78
|
* @param element Element for attributes
|
|
41
79
|
* @param attributes Attributes to set
|
|
80
|
+
* @param dispatch Dispatch events for relevant attributes? _(defaults to `true`)_
|
|
42
81
|
*/
|
|
43
|
-
export function setAttributes(element: Element, attributes: Record<string, unknown>): void;
|
|
44
|
-
|
|
45
82
|
export function setAttributes(
|
|
46
83
|
element: Element,
|
|
47
|
-
attributes:
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Set a property on an element
|
|
54
|
-
*
|
|
55
|
-
* _(Or remove it, if value is not an empty string or does not match the name)_
|
|
56
|
-
* @param element Element for property
|
|
57
|
-
* @param name Property name
|
|
58
|
-
* @param value Property value
|
|
59
|
-
*/
|
|
60
|
-
export function setProperty(element: Element, name: string, value: boolean | string): void;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Set a property on an element
|
|
64
|
-
*
|
|
65
|
-
* _(Or remove it, if value is not an empty string or does not match the name)_
|
|
66
|
-
* @param element Element for property
|
|
67
|
-
* @param property Property to set
|
|
68
|
-
*/
|
|
69
|
-
export function setProperty(element: Element, property: Property): void;
|
|
70
|
-
|
|
71
|
-
export function setProperty(element: Element, first: unknown, second?: unknown): void {
|
|
72
|
-
updateValue(element, first, second);
|
|
73
|
-
}
|
|
84
|
+
attributes: Record<string, unknown>,
|
|
85
|
+
dispatch?: boolean,
|
|
86
|
+
): void;
|
|
74
87
|
|
|
75
|
-
|
|
76
|
-
* Set one or more properties on an element
|
|
77
|
-
*
|
|
78
|
-
* _(Or remove them, if their value is not an empty string or does not match the name)_
|
|
79
|
-
* @param element Element for properties
|
|
80
|
-
* @param properties Properties to set
|
|
81
|
-
*/
|
|
82
|
-
export function setProperties(element: Element, properties: Property[]): void;
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Set one or more properties on an element
|
|
86
|
-
*
|
|
87
|
-
* _(Or remove them, if their value is not an empty string or does not match the name)_
|
|
88
|
-
* @param element Element for properties
|
|
89
|
-
* @param properties Properties to set
|
|
90
|
-
*/
|
|
91
|
-
export function setProperties(element: Element, properties: Record<string, unknown>): void;
|
|
92
|
-
|
|
93
|
-
export function setProperties(
|
|
88
|
+
export function setAttributes(
|
|
94
89
|
element: Element,
|
|
95
|
-
|
|
90
|
+
attributes: Attribute[] | Record<string, unknown>,
|
|
91
|
+
dispatch?: boolean,
|
|
96
92
|
): void {
|
|
97
|
-
|
|
93
|
+
setElementValues(element, attributes, null, dispatch, updateAttribute);
|
|
98
94
|
}
|
package/src/data.ts
CHANGED
|
@@ -81,7 +81,7 @@ export function setData(element: Element, data: PlainObject): void;
|
|
|
81
81
|
export function setData(element: Element, key: string, value: unknown): void;
|
|
82
82
|
|
|
83
83
|
export function setData(element: Element, first: PlainObject | string, second?: unknown): void {
|
|
84
|
-
setElementValues(element, first, second, updateDataAttribute);
|
|
84
|
+
setElementValues(element, first, second, null, updateDataAttribute);
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
function updateDataAttribute(element: Element, key: string, value: unknown): void {
|
|
@@ -91,6 +91,7 @@ function updateDataAttribute(element: Element, key: string, value: unknown): voi
|
|
|
91
91
|
value,
|
|
92
92
|
element.setAttribute,
|
|
93
93
|
element.removeAttribute,
|
|
94
|
+
false,
|
|
94
95
|
true,
|
|
95
96
|
);
|
|
96
97
|
}
|