@oscarpalmer/toretto 0.26.1 → 0.28.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/get.js +1 -1
- package/dist/attribute/index.js +1 -1
- package/dist/attribute/set.js +4 -4
- package/dist/data.js +1 -1
- package/dist/event/delegation.js +18 -19
- package/dist/event/index.js +1 -1
- package/dist/index.js +4 -4
- package/dist/internal/attribute.js +32 -1
- package/dist/style.js +1 -1
- package/dist/toretto.full.js +162 -208
- package/package.json +5 -5
- package/src/attribute/set.ts +20 -59
- package/src/data.ts +3 -15
- package/src/event/delegation.ts +29 -35
- package/src/event/index.ts +33 -23
- package/src/find/index.ts +10 -26
- package/src/find/relative.ts +4 -16
- package/src/focusable.ts +15 -51
- package/src/html.ts +10 -37
- package/src/internal/attribute.ts +67 -15
- package/src/internal/element-value.ts +0 -1
- package/src/internal/get-value.ts +2 -6
- package/src/internal/sanitize.ts +3 -14
- package/src/is.ts +1 -1
- package/src/style.ts +4 -18
- package/src/touch.ts +2 -6
- package/types/attribute/set.d.ts +10 -10
- package/types/event/index.d.ts +8 -0
- package/types/internal/attribute.d.ts +4 -1
- package/dist/attribute/is.js +0 -5
- package/dist/attribute/misc.js +0 -0
- package/dist/attribute/update.js +0 -31
- package/src/attribute/is.ts +0 -9
- package/src/attribute/misc.ts +0 -0
- package/src/attribute/update.ts +0 -86
- package/types/attribute/is.d.ts +0 -2
- package/types/attribute/misc.d.ts +0 -1
- package/types/attribute/update.d.ts +0 -5
package/src/html.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
|
-
import {
|
|
3
|
-
type SanitizeOptions,
|
|
4
|
-
getSanitizeOptions,
|
|
5
|
-
sanitizeNodes,
|
|
6
|
-
} from './internal/sanitize';
|
|
2
|
+
import {getSanitizeOptions, type SanitizeOptions, sanitizeNodes} from './internal/sanitize';
|
|
7
3
|
|
|
8
4
|
//
|
|
9
5
|
|
|
@@ -59,18 +55,13 @@ function createTemplate(html: string, ignore: boolean): HTMLTemplateElement {
|
|
|
59
55
|
return template;
|
|
60
56
|
}
|
|
61
57
|
|
|
62
|
-
function getHtml(
|
|
63
|
-
value: string | HTMLTemplateElement,
|
|
64
|
-
options: Options,
|
|
65
|
-
): Node[] {
|
|
58
|
+
function getHtml(value: string | HTMLTemplateElement, options: Options): Node[] {
|
|
66
59
|
if (typeof value !== 'string' && !(value instanceof HTMLTemplateElement)) {
|
|
67
60
|
return [];
|
|
68
61
|
}
|
|
69
62
|
|
|
70
63
|
const template =
|
|
71
|
-
value instanceof HTMLTemplateElement
|
|
72
|
-
? value
|
|
73
|
-
: getTemplate(value, options.ignoreCache);
|
|
64
|
+
value instanceof HTMLTemplateElement ? value : getTemplate(value, options.ignoreCache);
|
|
74
65
|
|
|
75
66
|
if (template == null) {
|
|
76
67
|
return [];
|
|
@@ -92,8 +83,7 @@ function getHtml(
|
|
|
92
83
|
function getOptions(input?: HtmlOptions): Options {
|
|
93
84
|
const options = isPlainObject(input) ? input : {};
|
|
94
85
|
|
|
95
|
-
options.ignoreCache =
|
|
96
|
-
typeof options.ignoreCache === 'boolean' ? options.ignoreCache : false;
|
|
86
|
+
options.ignoreCache = typeof options.ignoreCache === 'boolean' ? options.ignoreCache : false;
|
|
97
87
|
|
|
98
88
|
options.sanitizeBooleanAttributes =
|
|
99
89
|
typeof options.sanitizeBooleanAttributes === 'boolean'
|
|
@@ -103,10 +93,7 @@ function getOptions(input?: HtmlOptions): Options {
|
|
|
103
93
|
return options as Options;
|
|
104
94
|
}
|
|
105
95
|
|
|
106
|
-
function getTemplate(
|
|
107
|
-
value: string,
|
|
108
|
-
ignore: boolean,
|
|
109
|
-
): HTMLTemplateElement | undefined {
|
|
96
|
+
function getTemplate(value: string, ignore: boolean): HTMLTemplateElement | undefined {
|
|
110
97
|
if (typeof value !== 'string' || value.trim().length === 0) {
|
|
111
98
|
return;
|
|
112
99
|
}
|
|
@@ -117,22 +104,14 @@ function getTemplate(
|
|
|
117
104
|
return template;
|
|
118
105
|
}
|
|
119
106
|
|
|
120
|
-
const element = EXPRESSION_ID.test(value)
|
|
121
|
-
? document.querySelector(`#${value}`)
|
|
122
|
-
: null;
|
|
107
|
+
const element = EXPRESSION_ID.test(value) ? document.querySelector(`#${value}`) : null;
|
|
123
108
|
|
|
124
|
-
template =
|
|
125
|
-
element instanceof HTMLTemplateElement
|
|
126
|
-
? element
|
|
127
|
-
: createTemplate(value, ignore);
|
|
109
|
+
template = element instanceof HTMLTemplateElement ? element : createTemplate(value, ignore);
|
|
128
110
|
|
|
129
111
|
return template;
|
|
130
112
|
}
|
|
131
113
|
|
|
132
|
-
const html = ((
|
|
133
|
-
value: string | HTMLTemplateElement,
|
|
134
|
-
options?: Options,
|
|
135
|
-
): Node[] => {
|
|
114
|
+
const html = ((value: string | HTMLTemplateElement, options?: Options): Node[] => {
|
|
136
115
|
return getHtml(value, getOptions(options));
|
|
137
116
|
}) as Html;
|
|
138
117
|
|
|
@@ -167,14 +146,8 @@ html.remove = (template: string): void => {
|
|
|
167
146
|
* @param options Sanitization options
|
|
168
147
|
* @returns Sanitized nodes
|
|
169
148
|
*/
|
|
170
|
-
export function sanitize(
|
|
171
|
-
value
|
|
172
|
-
options?: SanitizeOptions,
|
|
173
|
-
): Node[] {
|
|
174
|
-
return sanitizeNodes(
|
|
175
|
-
Array.isArray(value) ? value : [value],
|
|
176
|
-
getSanitizeOptions(options),
|
|
177
|
-
);
|
|
149
|
+
export function sanitize(value: Node | Node[], options?: SanitizeOptions): Node[] {
|
|
150
|
+
return sanitizeNodes(Array.isArray(value) ? value : [value], getSanitizeOptions(options));
|
|
178
151
|
}
|
|
179
152
|
|
|
180
153
|
//
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type {PlainObject} from '@oscarpalmer/atoms';
|
|
2
2
|
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
3
|
-
import
|
|
3
|
+
import {getString} from '@oscarpalmer/atoms/string';
|
|
4
|
+
import type {Attribute, HTMLOrSVGElement, Property} from '../models';
|
|
5
|
+
import {isHTMLOrSVGElement} from './is';
|
|
4
6
|
|
|
5
7
|
function isAttribute(value: unknown): value is Attr | Attribute {
|
|
6
8
|
return (
|
|
@@ -26,10 +28,7 @@ export function isBadAttribute(attribute: Attr | Attribute): boolean;
|
|
|
26
28
|
*/
|
|
27
29
|
export function isBadAttribute(name: string, value: string): boolean;
|
|
28
30
|
|
|
29
|
-
export function isBadAttribute(
|
|
30
|
-
first: string | Attr | Attribute,
|
|
31
|
-
second?: string,
|
|
32
|
-
): boolean {
|
|
31
|
+
export function isBadAttribute(first: string | Attr | Attribute, second?: string): boolean {
|
|
33
32
|
return isValidAttribute(
|
|
34
33
|
attribute =>
|
|
35
34
|
attribute == null ||
|
|
@@ -57,9 +56,7 @@ export function isBooleanAttribute(name: string): boolean;
|
|
|
57
56
|
|
|
58
57
|
export function isBooleanAttribute(value: string | Attr | Attribute): boolean {
|
|
59
58
|
return isValidAttribute(
|
|
60
|
-
attribute =>
|
|
61
|
-
attribute != null &&
|
|
62
|
-
booleanAttributes.includes(attribute.name.toLowerCase()),
|
|
59
|
+
attribute => attribute != null && booleanAttributes.includes(attribute.name.toLowerCase()),
|
|
63
60
|
value,
|
|
64
61
|
'',
|
|
65
62
|
);
|
|
@@ -70,9 +67,7 @@ export function isBooleanAttribute(value: string | Attr | Attribute): boolean {
|
|
|
70
67
|
* @param attribute Attribute to check
|
|
71
68
|
* @returns `true` if attribute is empty and not a boolean attribute
|
|
72
69
|
*/
|
|
73
|
-
export function isEmptyNonBooleanAttribute(
|
|
74
|
-
attribute: Attr | Attribute,
|
|
75
|
-
): boolean;
|
|
70
|
+
export function isEmptyNonBooleanAttribute(attribute: Attr | Attribute): boolean;
|
|
76
71
|
|
|
77
72
|
/**
|
|
78
73
|
* Is the attribute empty and not a boolean attribute?
|
|
@@ -80,10 +75,7 @@ export function isEmptyNonBooleanAttribute(
|
|
|
80
75
|
* @param value Attribute value
|
|
81
76
|
* @returns `true` if attribute is empty and not a boolean attribute
|
|
82
77
|
*/
|
|
83
|
-
export function isEmptyNonBooleanAttribute(
|
|
84
|
-
name: string,
|
|
85
|
-
value: string,
|
|
86
|
-
): boolean;
|
|
78
|
+
export function isEmptyNonBooleanAttribute(name: string, value: string): boolean;
|
|
87
79
|
|
|
88
80
|
export function isEmptyNonBooleanAttribute(
|
|
89
81
|
first: string | Attr | Attribute,
|
|
@@ -141,6 +133,10 @@ export function isInvalidBooleanAttribute(
|
|
|
141
133
|
);
|
|
142
134
|
}
|
|
143
135
|
|
|
136
|
+
export function isProperty(value: unknown): value is Property {
|
|
137
|
+
return isPlainObject(value) && typeof (value as PlainObject).name === 'string';
|
|
138
|
+
}
|
|
139
|
+
|
|
144
140
|
function isValidAttribute(
|
|
145
141
|
callback: (attribute: Attr | Attribute | undefined) => boolean,
|
|
146
142
|
first: string | Attr | Attribute,
|
|
@@ -157,6 +153,62 @@ function isValidAttribute(
|
|
|
157
153
|
return callback(attribute);
|
|
158
154
|
}
|
|
159
155
|
|
|
156
|
+
function updateAttribute(element: HTMLOrSVGElement, name: string, value: unknown): void {
|
|
157
|
+
const isBoolean = booleanAttributes.includes(name.toLowerCase());
|
|
158
|
+
|
|
159
|
+
if (isBoolean) {
|
|
160
|
+
updateProperty(element, name, value);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (isBoolean ? value !== true : value == null) {
|
|
164
|
+
element.removeAttribute(name);
|
|
165
|
+
} else {
|
|
166
|
+
element.setAttribute(name, isBoolean ? '' : getString(value));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function updateProperty(element: HTMLOrSVGElement, name: string, value: unknown): void {
|
|
171
|
+
const actual = name.toLowerCase();
|
|
172
|
+
|
|
173
|
+
(element as unknown as PlainObject)[actual] =
|
|
174
|
+
value === '' || (typeof value === 'string' && value.toLowerCase() === actual) || value === true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function updateValue(element: HTMLOrSVGElement, first: unknown, second: unknown): void {
|
|
178
|
+
if (!isHTMLOrSVGElement(element)) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (isProperty(first)) {
|
|
183
|
+
updateAttribute(element, (first as Attribute).name, (first as Attribute).value);
|
|
184
|
+
} else if (typeof first === 'string') {
|
|
185
|
+
updateAttribute(element, first as string, second);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function updateValues(
|
|
190
|
+
element: HTMLOrSVGElement,
|
|
191
|
+
values: Attribute<unknown>[] | Record<string, unknown>,
|
|
192
|
+
): void {
|
|
193
|
+
if (!isHTMLOrSVGElement(element)) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const isArray = Array.isArray(values);
|
|
198
|
+
const entries = Object.entries(values);
|
|
199
|
+
const {length} = entries;
|
|
200
|
+
|
|
201
|
+
for (let index = 0; index < length; index += 1) {
|
|
202
|
+
const entry = entries[index];
|
|
203
|
+
|
|
204
|
+
if (isArray) {
|
|
205
|
+
updateAttribute(element, (entry[1] as Attribute).name, (entry[1] as Attribute).value);
|
|
206
|
+
} else {
|
|
207
|
+
updateAttribute(element, entry[0], entry[1]);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
160
212
|
//
|
|
161
213
|
|
|
162
214
|
const EXPRESSION_ON_PREFIX = /^on/i;
|
|
@@ -14,9 +14,7 @@ export function getAttributeValue(
|
|
|
14
14
|
const attribute = element.attributes[normalized as keyof NamedNodeMap];
|
|
15
15
|
const value = attribute instanceof Attr ? attribute.value : undefined;
|
|
16
16
|
|
|
17
|
-
return EXPRESSION_DATA_PREFIX.test(normalized) &&
|
|
18
|
-
typeof value === 'string' &&
|
|
19
|
-
parseValue
|
|
17
|
+
return EXPRESSION_DATA_PREFIX.test(normalized) && typeof value === 'string' && parseValue
|
|
20
18
|
? (parse(value) ?? value)
|
|
21
19
|
: value;
|
|
22
20
|
}
|
|
@@ -28,9 +26,7 @@ export function getStyleValue(
|
|
|
28
26
|
): string | undefined {
|
|
29
27
|
const name = camelCase(property);
|
|
30
28
|
|
|
31
|
-
return computed
|
|
32
|
-
? getComputedStyle(element)[name as never]
|
|
33
|
-
: element.style[name as never];
|
|
29
|
+
return computed ? getComputedStyle(element)[name as never] : element.style[name as never];
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
export const EXPRESSION_DATA_PREFIX = /^data-/i;
|
package/src/internal/sanitize.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
|
-
import {
|
|
3
|
-
isBadAttribute,
|
|
4
|
-
isEmptyNonBooleanAttribute,
|
|
5
|
-
isInvalidBooleanAttribute,
|
|
6
|
-
} from './attribute';
|
|
2
|
+
import {isBadAttribute, isEmptyNonBooleanAttribute, isInvalidBooleanAttribute} from './attribute';
|
|
7
3
|
|
|
8
4
|
//
|
|
9
5
|
|
|
@@ -31,11 +27,7 @@ export function getSanitizeOptions(input?: SanitizeOptions): Options {
|
|
|
31
27
|
return options as Options;
|
|
32
28
|
}
|
|
33
29
|
|
|
34
|
-
export function sanitizeAttributes(
|
|
35
|
-
element: Element,
|
|
36
|
-
attributes: Attr[],
|
|
37
|
-
options: Options,
|
|
38
|
-
): void {
|
|
30
|
+
export function sanitizeAttributes(element: Element, attributes: Attr[], options: Options): void {
|
|
39
31
|
const {length} = attributes;
|
|
40
32
|
|
|
41
33
|
for (let index = 0; index < length; index += 1) {
|
|
@@ -43,10 +35,7 @@ export function sanitizeAttributes(
|
|
|
43
35
|
|
|
44
36
|
if (isBadAttribute(attribute) || isEmptyNonBooleanAttribute(attribute)) {
|
|
45
37
|
element.removeAttribute(attribute.name);
|
|
46
|
-
} else if (
|
|
47
|
-
options.sanitizeBooleanAttributes &&
|
|
48
|
-
isInvalidBooleanAttribute(attribute)
|
|
49
|
-
) {
|
|
38
|
+
} else if (options.sanitizeBooleanAttributes && isInvalidBooleanAttribute(attribute)) {
|
|
50
39
|
element.setAttribute(attribute.name, '');
|
|
51
40
|
}
|
|
52
41
|
}
|
package/src/is.ts
CHANGED
package/src/style.ts
CHANGED
|
@@ -57,11 +57,7 @@ export function getStyles<Property extends keyof CSSStyleDeclaration>(
|
|
|
57
57
|
const property = properties[index];
|
|
58
58
|
|
|
59
59
|
if (typeof property === 'string') {
|
|
60
|
-
styles[property] = getStyleValue(
|
|
61
|
-
element,
|
|
62
|
-
property,
|
|
63
|
-
computed === true,
|
|
64
|
-
) as never;
|
|
60
|
+
styles[property] = getStyleValue(element, property, computed === true) as never;
|
|
65
61
|
}
|
|
66
62
|
}
|
|
67
63
|
|
|
@@ -74,10 +70,7 @@ export function getStyles<Property extends keyof CSSStyleDeclaration>(
|
|
|
74
70
|
* @param computed Get the computed text direction? _(defaults to `false`)_
|
|
75
71
|
* @returns Text direction
|
|
76
72
|
*/
|
|
77
|
-
export function getTextDirection(
|
|
78
|
-
element: HTMLOrSVGElement,
|
|
79
|
-
computed?: boolean,
|
|
80
|
-
): TextDirection {
|
|
73
|
+
export function getTextDirection(element: HTMLOrSVGElement, computed?: boolean): TextDirection {
|
|
81
74
|
if (!(element instanceof Element)) {
|
|
82
75
|
return undefined as never;
|
|
83
76
|
}
|
|
@@ -112,10 +105,7 @@ export function setStyle(
|
|
|
112
105
|
* @param element Element to set the styles on
|
|
113
106
|
* @param styles Styles to set
|
|
114
107
|
*/
|
|
115
|
-
export function setStyles(
|
|
116
|
-
element: HTMLOrSVGElement,
|
|
117
|
-
styles: Partial<CSSStyleDeclaration>,
|
|
118
|
-
): void {
|
|
108
|
+
export function setStyles(element: HTMLOrSVGElement, styles: Partial<CSSStyleDeclaration>): void {
|
|
119
109
|
setElementValues(element, styles as never, null, updateStyleProperty);
|
|
120
110
|
}
|
|
121
111
|
|
|
@@ -170,11 +160,7 @@ export function toggleStyles(
|
|
|
170
160
|
};
|
|
171
161
|
}
|
|
172
162
|
|
|
173
|
-
function updateStyleProperty(
|
|
174
|
-
element: HTMLOrSVGElement,
|
|
175
|
-
key: string,
|
|
176
|
-
value: unknown,
|
|
177
|
-
): void {
|
|
163
|
+
function updateStyleProperty(element: HTMLOrSVGElement, key: string, value: unknown): void {
|
|
178
164
|
updateElementValue(
|
|
179
165
|
element,
|
|
180
166
|
key,
|
package/src/touch.ts
CHANGED
|
@@ -36,16 +36,12 @@ function getSupport(): boolean {
|
|
|
36
36
|
return true;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
if (
|
|
40
|
-
typeof navigator.maxTouchPoints === 'number' &&
|
|
41
|
-
navigator.maxTouchPoints > 0
|
|
42
|
-
) {
|
|
39
|
+
if (typeof navigator.maxTouchPoints === 'number' && navigator.maxTouchPoints > 0) {
|
|
43
40
|
return true;
|
|
44
41
|
}
|
|
45
42
|
|
|
46
43
|
if (
|
|
47
|
-
typeof (navigator as NavigatorWithMsMaxTouchPoints).msMaxTouchPoints ===
|
|
48
|
-
'number' &&
|
|
44
|
+
typeof (navigator as NavigatorWithMsMaxTouchPoints).msMaxTouchPoints === 'number' &&
|
|
49
45
|
(navigator as NavigatorWithMsMaxTouchPoints).msMaxTouchPoints > 0
|
|
50
46
|
) {
|
|
51
47
|
return true;
|
package/types/attribute/set.d.ts
CHANGED
|
@@ -4,18 +4,18 @@ import type { Attribute, HTMLOrSVGElement, Property } from '../models';
|
|
|
4
4
|
*
|
|
5
5
|
* _(Or remove it, if value is `null` or `undefined`)_
|
|
6
6
|
* @param element Element for attribute
|
|
7
|
-
* @param
|
|
7
|
+
* @param name Attribute name
|
|
8
|
+
* @param value Attribute value
|
|
8
9
|
*/
|
|
9
|
-
export declare function setAttribute(element: HTMLOrSVGElement,
|
|
10
|
+
export declare function setAttribute(element: HTMLOrSVGElement, name: string, value?: unknown): void;
|
|
10
11
|
/**
|
|
11
12
|
* Set an attribute on an element
|
|
12
13
|
*
|
|
13
14
|
* _(Or remove it, if value is `null` or `undefined`)_
|
|
14
15
|
* @param element Element for attribute
|
|
15
|
-
* @param
|
|
16
|
-
* @param value Attribute value
|
|
16
|
+
* @param attribute Attribute to set
|
|
17
17
|
*/
|
|
18
|
-
export declare function setAttribute(element: HTMLOrSVGElement,
|
|
18
|
+
export declare function setAttribute(element: HTMLOrSVGElement, attribute: Attr | Attribute): void;
|
|
19
19
|
/**
|
|
20
20
|
* Set one or more attributes on an element
|
|
21
21
|
*
|
|
@@ -37,18 +37,18 @@ export declare function setAttributes(element: HTMLOrSVGElement, attributes: Rec
|
|
|
37
37
|
*
|
|
38
38
|
* _(Or remove it, if value is not an empty string or does not match the name)_
|
|
39
39
|
* @param element Element for property
|
|
40
|
-
* @param
|
|
40
|
+
* @param name Property name
|
|
41
|
+
* @param value Property value
|
|
41
42
|
*/
|
|
42
|
-
export declare function setProperty(element: HTMLOrSVGElement,
|
|
43
|
+
export declare function setProperty(element: HTMLOrSVGElement, name: string, value: boolean | string): void;
|
|
43
44
|
/**
|
|
44
45
|
* Set a property on an element
|
|
45
46
|
*
|
|
46
47
|
* _(Or remove it, if value is not an empty string or does not match the name)_
|
|
47
48
|
* @param element Element for property
|
|
48
|
-
* @param
|
|
49
|
-
* @param value Property value
|
|
49
|
+
* @param property Property to set
|
|
50
50
|
*/
|
|
51
|
-
export declare function setProperty(element: HTMLOrSVGElement,
|
|
51
|
+
export declare function setProperty(element: HTMLOrSVGElement, property: Property): void;
|
|
52
52
|
/**
|
|
53
53
|
* Set one or more properties on an element
|
|
54
54
|
*
|
package/types/event/index.d.ts
CHANGED
|
@@ -19,6 +19,14 @@ export declare function dispatch(target: EventTarget, type: string, options?: Cu
|
|
|
19
19
|
* @returns X- and Y-coordinates
|
|
20
20
|
*/
|
|
21
21
|
export declare function getPosition(event: MouseEvent | TouchEvent): EventPosition | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Remove an event listener
|
|
24
|
+
* @param target Event target
|
|
25
|
+
* @param type Type of event
|
|
26
|
+
* @param listener Event listener
|
|
27
|
+
* @param options Options for event
|
|
28
|
+
*/
|
|
29
|
+
export declare function off(target: EventTarget, type: keyof HTMLElementEventMap, listener: EventListener | CustomEventListener, options?: EventListenerOptions): void;
|
|
22
30
|
/**
|
|
23
31
|
* Remove an event listener
|
|
24
32
|
* @param target Event target
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Attribute } from '../models';
|
|
1
|
+
import type { Attribute, HTMLOrSVGElement, Property } from '../models';
|
|
2
2
|
/**
|
|
3
3
|
* Is the attribute considered bad and potentially harmful?
|
|
4
4
|
* @param attribute Attribute to check
|
|
@@ -54,6 +54,9 @@ export declare function isInvalidBooleanAttribute(attribute: Attr | Attribute):
|
|
|
54
54
|
* @returns `true` if attribute is an invalid boolean attribute
|
|
55
55
|
*/
|
|
56
56
|
export declare function isInvalidBooleanAttribute(name: string, value: string): boolean;
|
|
57
|
+
export declare function isProperty(value: unknown): value is Property;
|
|
58
|
+
export declare function updateValue(element: HTMLOrSVGElement, first: unknown, second: unknown): void;
|
|
59
|
+
export declare function updateValues(element: HTMLOrSVGElement, values: Attribute<unknown>[] | Record<string, unknown>): void;
|
|
57
60
|
/**
|
|
58
61
|
* List of boolean attributes
|
|
59
62
|
*/
|
package/dist/attribute/is.js
DELETED
package/dist/attribute/misc.js
DELETED
|
File without changes
|
package/dist/attribute/update.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { isHTMLOrSVGElement } from "../internal/is.js";
|
|
2
|
-
import { booleanAttributes } from "../internal/attribute.js";
|
|
3
|
-
import { isProperty } from "./is.js";
|
|
4
|
-
import { getString } from "@oscarpalmer/atoms/string";
|
|
5
|
-
function updateAttribute(element, name, value) {
|
|
6
|
-
if (booleanAttributes.includes(name.toLowerCase())) updateProperty(element, name, value, false);
|
|
7
|
-
else if (value == null) element.removeAttribute(name);
|
|
8
|
-
else element.setAttribute(name, typeof value === "string" ? value : getString(value));
|
|
9
|
-
}
|
|
10
|
-
function updateProperty(element, name, value, validate) {
|
|
11
|
-
const actual = validate ?? true ? name.toLowerCase() : name;
|
|
12
|
-
if (actual === "hidden") element.hidden = value === "" || value === true;
|
|
13
|
-
else element[actual] = value === "" || typeof value === "string" && value.toLowerCase() === actual || value === true;
|
|
14
|
-
}
|
|
15
|
-
function updateValue(element, first, second, callback) {
|
|
16
|
-
if (!isHTMLOrSVGElement(element)) return;
|
|
17
|
-
if (isProperty(first)) callback(element, first.name, first.value);
|
|
18
|
-
else if (typeof first === "string") callback(element, first, second);
|
|
19
|
-
}
|
|
20
|
-
function updateValues(element, values, callback) {
|
|
21
|
-
if (!isHTMLOrSVGElement(element)) return;
|
|
22
|
-
const isArray = Array.isArray(values);
|
|
23
|
-
const entries = Object.entries(values);
|
|
24
|
-
const { length } = entries;
|
|
25
|
-
for (let index = 0; index < length; index += 1) {
|
|
26
|
-
const entry = entries[index];
|
|
27
|
-
if (isArray) (callback ?? updateAttribute)(element, entry[1].name, entry[1].value);
|
|
28
|
-
else (callback ?? updateAttribute)(element, entry[0], entry[1]);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
export { updateAttribute, updateProperty, updateValue, updateValues };
|
package/src/attribute/is.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type {PlainObject} from '@oscarpalmer/atoms';
|
|
2
|
-
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
3
|
-
import type {Property} from '../models';
|
|
4
|
-
|
|
5
|
-
export function isProperty(value: unknown): value is Property {
|
|
6
|
-
return (
|
|
7
|
-
isPlainObject(value) && typeof (value as PlainObject).name === 'string'
|
|
8
|
-
);
|
|
9
|
-
}
|
package/src/attribute/misc.ts
DELETED
|
File without changes
|
package/src/attribute/update.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import type {PlainObject} from '@oscarpalmer/atoms';
|
|
2
|
-
import {getString} from '@oscarpalmer/atoms/string';
|
|
3
|
-
import {booleanAttributes} from '../internal/attribute';
|
|
4
|
-
import {isHTMLOrSVGElement} from '../internal/is';
|
|
5
|
-
import type {Attribute, HTMLOrSVGElement} from '../models';
|
|
6
|
-
import {isProperty} from './is';
|
|
7
|
-
|
|
8
|
-
export function updateAttribute(
|
|
9
|
-
element: HTMLOrSVGElement,
|
|
10
|
-
name: string,
|
|
11
|
-
value: unknown,
|
|
12
|
-
): void {
|
|
13
|
-
if (booleanAttributes.includes(name.toLowerCase())) {
|
|
14
|
-
updateProperty(element, name, value, false);
|
|
15
|
-
} else if (value == null) {
|
|
16
|
-
element.removeAttribute(name);
|
|
17
|
-
} else {
|
|
18
|
-
element.setAttribute(
|
|
19
|
-
name,
|
|
20
|
-
typeof value === 'string' ? value : getString(value),
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function updateProperty(
|
|
26
|
-
element: HTMLOrSVGElement,
|
|
27
|
-
name: string,
|
|
28
|
-
value: unknown,
|
|
29
|
-
validate?: boolean,
|
|
30
|
-
): void {
|
|
31
|
-
const actual = (validate ?? true) ? name.toLowerCase() : name;
|
|
32
|
-
|
|
33
|
-
if (actual === 'hidden') {
|
|
34
|
-
(element as HTMLElement).hidden = value === '' || value === true;
|
|
35
|
-
} else {
|
|
36
|
-
(element as unknown as PlainObject)[actual] =
|
|
37
|
-
value === '' ||
|
|
38
|
-
(typeof value === 'string' && value.toLowerCase() === actual) ||
|
|
39
|
-
value === true;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function updateValue(
|
|
44
|
-
element: HTMLOrSVGElement,
|
|
45
|
-
first: unknown,
|
|
46
|
-
second: unknown,
|
|
47
|
-
callback: (element: HTMLOrSVGElement, name: string, value: unknown) => void,
|
|
48
|
-
): void {
|
|
49
|
-
if (!isHTMLOrSVGElement(element)) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (isProperty(first)) {
|
|
54
|
-
callback(element, (first as Attribute).name, (first as Attribute).value);
|
|
55
|
-
} else if (typeof first === 'string') {
|
|
56
|
-
callback(element, first as string, second);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function updateValues(
|
|
61
|
-
element: HTMLOrSVGElement,
|
|
62
|
-
values: Attribute<unknown>[] | Record<string, unknown>,
|
|
63
|
-
callback?: (element: HTMLOrSVGElement, name: string, value: unknown) => void,
|
|
64
|
-
): void {
|
|
65
|
-
if (!isHTMLOrSVGElement(element)) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const isArray = Array.isArray(values);
|
|
70
|
-
const entries = Object.entries(values);
|
|
71
|
-
const {length} = entries;
|
|
72
|
-
|
|
73
|
-
for (let index = 0; index < length; index += 1) {
|
|
74
|
-
const entry = entries[index];
|
|
75
|
-
|
|
76
|
-
if (isArray) {
|
|
77
|
-
(callback ?? updateAttribute)(
|
|
78
|
-
element,
|
|
79
|
-
(entry[1] as Attribute).name,
|
|
80
|
-
(entry[1] as Attribute).value,
|
|
81
|
-
);
|
|
82
|
-
} else {
|
|
83
|
-
(callback ?? updateAttribute)(element, entry[0], entry[1]);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
package/types/attribute/is.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { Attribute, HTMLOrSVGElement } from '../models';
|
|
2
|
-
export declare function updateAttribute(element: HTMLOrSVGElement, name: string, value: unknown): void;
|
|
3
|
-
export declare function updateProperty(element: HTMLOrSVGElement, name: string, value: unknown, validate?: boolean): void;
|
|
4
|
-
export declare function updateValue(element: HTMLOrSVGElement, first: unknown, second: unknown, callback: (element: HTMLOrSVGElement, name: string, value: unknown) => void): void;
|
|
5
|
-
export declare function updateValues(element: HTMLOrSVGElement, values: Attribute<unknown>[] | Record<string, unknown>, callback?: (element: HTMLOrSVGElement, name: string, value: unknown) => void): void;
|