@oscarpalmer/toretto 0.41.0 → 0.43.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.
Files changed (55) hide show
  1. package/dist/attribute/{get.d.mts → get.attribute.d.mts} +3 -2
  2. package/dist/attribute/{get.mjs → get.attribute.mjs} +4 -3
  3. package/dist/attribute/index.d.mts +3 -16
  4. package/dist/attribute/index.mjs +4 -7
  5. package/dist/attribute/{set.d.mts → set.attribute.d.mts} +4 -4
  6. package/dist/attribute/{set.mjs → set.attribute.mjs} +2 -2
  7. package/dist/create.d.mts +25 -0
  8. package/dist/create.mjs +17 -0
  9. package/dist/data.mjs +7 -7
  10. package/dist/event/delegation.mjs +8 -1
  11. package/dist/html/index.d.mts +23 -26
  12. package/dist/html/index.mjs +85 -18
  13. package/dist/html/sanitize.mjs +6 -5
  14. package/dist/index.d.mts +117 -54
  15. package/dist/index.mjs +541 -380
  16. package/dist/internal/attribute.d.mts +4 -3
  17. package/dist/internal/attribute.mjs +13 -23
  18. package/dist/internal/element-value.d.mts +2 -2
  19. package/dist/internal/element-value.mjs +12 -6
  20. package/dist/internal/get-value.mjs +3 -1
  21. package/dist/internal/property.d.mts +4 -0
  22. package/dist/internal/property.mjs +24 -0
  23. package/dist/property/get.property.d.mts +20 -0
  24. package/dist/property/get.property.mjs +35 -0
  25. package/dist/property/index.d.mts +3 -0
  26. package/dist/property/index.mjs +3 -0
  27. package/dist/property/set.property.d.mts +32 -0
  28. package/dist/property/set.property.mjs +32 -0
  29. package/dist/style.d.mts +16 -9
  30. package/dist/style.mjs +22 -21
  31. package/package.json +14 -6
  32. package/src/attribute/{get.ts → get.attribute.ts} +14 -3
  33. package/src/attribute/index.ts +10 -22
  34. package/src/attribute/{set.ts → set.attribute.ts} +9 -5
  35. package/src/create.ts +81 -0
  36. package/src/data.ts +16 -8
  37. package/src/event/delegation.ts +24 -3
  38. package/src/event/index.ts +9 -3
  39. package/src/find/index.ts +11 -3
  40. package/src/find/relative.ts +4 -0
  41. package/src/focusable.ts +10 -2
  42. package/src/html/index.ts +166 -58
  43. package/src/html/sanitize.ts +14 -11
  44. package/src/index.ts +2 -1
  45. package/src/internal/attribute.ts +23 -42
  46. package/src/internal/element-value.ts +25 -6
  47. package/src/internal/get-value.ts +14 -0
  48. package/src/internal/is.ts +4 -0
  49. package/src/internal/property.ts +42 -0
  50. package/src/is.ts +10 -2
  51. package/src/property/get.property.ts +73 -0
  52. package/src/property/index.ts +2 -0
  53. package/src/property/set.property.ts +103 -0
  54. package/src/style.ts +81 -36
  55. package/src/touch.ts +14 -2
package/src/style.ts CHANGED
@@ -3,6 +3,10 @@ import {getStyleValue} from './internal/get-value';
3
3
  import {isHTMLOrSVGElement} from './internal/is';
4
4
  import type {TextDirection} from './models';
5
5
 
6
+ // #region Types
7
+
8
+ type CSSStyleValues = Variables & CSSStyleDeclaration;
9
+
6
10
  export type StyleToggler = {
7
11
  /**
8
12
  * Set the provided styles on the element
@@ -14,6 +18,18 @@ export type StyleToggler = {
14
18
  remove(): void;
15
19
  };
16
20
 
21
+ type Styles = Partial<Record<keyof CSSStyleValues, unknown>>;
22
+
23
+ type Variables<
24
+ Value extends Record<string, string | undefined> = Record<string, string | undefined>,
25
+ > = {
26
+ [property in keyof Value as `--${string & property}`]?: string | undefined;
27
+ };
28
+
29
+ // #endregion
30
+
31
+ // #region Functions
32
+
17
33
  /**
18
34
  * Get a style from an element
19
35
  * @param element Element to get the style from
@@ -23,14 +39,12 @@ export type StyleToggler = {
23
39
  */
24
40
  export function getStyle(
25
41
  element: Element,
26
- property: keyof CSSStyleDeclaration,
42
+ property: keyof CSSStyleValues,
27
43
  computed?: boolean,
28
44
  ): string | undefined {
29
- if (!isHTMLOrSVGElement(element) || typeof property !== 'string') {
30
- return undefined;
45
+ if (isHTMLOrSVGElement(element) && typeof property === 'string') {
46
+ return getStyleValue(element, property, computed === true);
31
47
  }
32
-
33
- return getStyleValue(element, property, computed === true);
34
48
  }
35
49
 
36
50
  /**
@@ -40,7 +54,7 @@ export function getStyle(
40
54
  * @param computed Get the computed styles? _(defaults to `false`)_
41
55
  * @returns Style values
42
56
  */
43
- export function getStyles<Property extends keyof CSSStyleDeclaration>(
57
+ export function getStyles<Property extends keyof CSSStyleValues>(
44
58
  element: Element,
45
59
  properties: Property[],
46
60
  computed?: boolean,
@@ -65,25 +79,37 @@ export function getStyles<Property extends keyof CSSStyleDeclaration>(
65
79
  }
66
80
 
67
81
  /**
68
- * Get the text direction of an element
69
- * @param element Element to get the text direction from
70
- * @param computed Get the computed text direction? _(defaults to `false`)_
82
+ * Get the text direction of a node or element _(or document, if element is invalid)_
83
+ * @param node Node or element to get the text direction from
84
+ * @returns Text direction
85
+ */
86
+ export function getTextDirection(node: Element | Node): TextDirection;
87
+
88
+ /**
89
+ * Get the text direction of the document
71
90
  * @returns Text direction
72
91
  */
73
- export function getTextDirection(element: Element, computed?: boolean): TextDirection {
74
- if (!(element instanceof Element)) {
75
- return undefined as never;
92
+ export function getTextDirection(): TextDirection;
93
+
94
+ export function getTextDirection(node?: Element | Node): TextDirection {
95
+ let target: HTMLElement | SVGElement;
96
+
97
+ if (isHTMLOrSVGElement(node)) {
98
+ target = node;
99
+ } else {
100
+ target =
101
+ node instanceof Node
102
+ ? (node.ownerDocument?.documentElement ?? document.documentElement)
103
+ : document.documentElement;
76
104
  }
77
105
 
78
- const direction = element.getAttribute(ATTRIBUTE_DIRECTION);
106
+ let {direction} = target.style;
79
107
 
80
- if (direction != null && EXPRESSION_DIRECTION.test(direction)) {
81
- return direction.toLowerCase() as TextDirection;
108
+ if (direction === '') {
109
+ direction = getStyleValue(target, PROPERTY_DIRECTION, true)!;
82
110
  }
83
111
 
84
- const value = getStyleValue(element, 'direction', computed === true);
85
-
86
- return value === 'rtl' ? value : 'ltr';
112
+ return direction === DIRECTION_RTL ? DIRECTION_RTL : DIRECTION_LTR;
87
113
  }
88
114
 
89
115
  /**
@@ -92,12 +118,8 @@ export function getTextDirection(element: Element, computed?: boolean): TextDire
92
118
  * @param property Style name
93
119
  * @param value Style value
94
120
  */
95
- export function setStyle(
96
- element: Element,
97
- property: keyof CSSStyleDeclaration,
98
- value?: string,
99
- ): void {
100
- setElementValues(element, property as string, value, null, updateStyleProperty);
121
+ export function setStyle(element: Element, property: keyof CSSStyleValues, value?: unknown): void {
122
+ setElementValues(element, property as string, value, null, updateStyleProperty, true);
101
123
  }
102
124
 
103
125
  /**
@@ -105,8 +127,8 @@ export function setStyle(
105
127
  * @param element Element to set the styles on
106
128
  * @param styles Styles to set
107
129
  */
108
- export function setStyles(element: Element, styles: Partial<CSSStyleDeclaration>): void {
109
- setElementValues(element, styles as never, null, null, updateStyleProperty);
130
+ export function setStyles(element: Element, styles: Styles): void {
131
+ setElementValues(element, styles as never, null, null, updateStyleProperty, true);
110
132
  }
111
133
 
112
134
  /**
@@ -115,11 +137,11 @@ export function setStyles(element: Element, styles: Partial<CSSStyleDeclaration>
115
137
  * @param styles Styles to be set or removed
116
138
  * @returns Style toggler
117
139
  */
118
- export function toggleStyles(element: Element, styles: Partial<CSSStyleDeclaration>): StyleToggler {
140
+ export function toggleStyles(element: Element, styles: Styles): StyleToggler {
119
141
  function toggle(set: boolean): void {
120
142
  hasSet = set;
121
143
 
122
- let next: Partial<CSSStyleDeclaration>;
144
+ let next: Styles;
123
145
 
124
146
  if (set) {
125
147
  values = getStyles(element, keys);
@@ -130,8 +152,8 @@ export function toggleStyles(element: Element, styles: Partial<CSSStyleDeclarati
130
152
 
131
153
  values = {};
132
154
 
133
- for (const key of keys) {
134
- values[key as never] = undefined;
155
+ for (let index = 0; index < length; index += 1) {
156
+ values[keys[index] as never] = undefined;
135
157
  }
136
158
  }
137
159
 
@@ -139,6 +161,7 @@ export function toggleStyles(element: Element, styles: Partial<CSSStyleDeclarati
139
161
  }
140
162
 
141
163
  const keys = Object.keys(styles) as (keyof CSSStyleDeclaration)[];
164
+ const {length} = keys;
142
165
 
143
166
  let hasSet = false;
144
167
  let values: Record<string, string | undefined> = {};
@@ -162,19 +185,41 @@ function updateStyleProperty(element: Element, key: string, value: unknown): voi
162
185
  element,
163
186
  key,
164
187
  value,
165
- function (this: Element, property: string, style: string) {
166
- (this as HTMLElement).style[property as never] = style;
188
+ function (this: Element, property: string, style: unknown) {
189
+ if (property.startsWith(VARIABLE_PREFIX)) {
190
+ (this as HTMLElement).style.setProperty(property, String(style));
191
+ } else {
192
+ (this as HTMLElement).style[property as never] = String(style);
193
+ }
167
194
  },
168
195
  function (this: Element, property: string) {
169
- (this as HTMLElement).style[property as never] = '';
196
+ if (property.startsWith(VARIABLE_PREFIX)) {
197
+ (this as HTMLElement).style.removeProperty(property);
198
+ } else {
199
+ (this as HTMLElement).style[property as never] = '';
200
+ }
201
+
202
+ if ((this as HTMLElement).getAttribute(ATTRIBUTE_STYLE) === '') {
203
+ (this as HTMLElement).removeAttribute(ATTRIBUTE_STYLE);
204
+ }
170
205
  },
171
206
  false,
172
207
  false,
173
208
  );
174
209
  }
175
210
 
176
- //
211
+ // #endregion
212
+
213
+ // #region Variables
214
+
215
+ const ATTRIBUTE_STYLE = 'style';
216
+
217
+ const DIRECTION_LTR = 'ltr';
218
+
219
+ const DIRECTION_RTL = 'rtl';
220
+
221
+ const PROPERTY_DIRECTION = 'direction';
177
222
 
178
- const ATTRIBUTE_DIRECTION = 'dir';
223
+ const VARIABLE_PREFIX = '--';
179
224
 
180
- const EXPRESSION_DIRECTION = /^(ltr|rtl)$/i;
225
+ // #endregion
package/src/touch.ts CHANGED
@@ -1,3 +1,5 @@
1
+ // #region Types
2
+
1
3
  type NavigatorWithMsMaxTouchPoints = Navigator & {
2
4
  msMaxTouchPoints: number;
3
5
  };
@@ -17,7 +19,9 @@ type SupporsTouch = {
17
19
  update(): boolean;
18
20
  };
19
21
 
20
- //
22
+ // #endregion
23
+
24
+ // #region Functions
21
25
 
22
26
  function getSupport(): boolean {
23
27
  if (window == null || navigator == null) {
@@ -50,7 +54,9 @@ function getSupport(): boolean {
50
54
  return false;
51
55
  }
52
56
 
53
- //
57
+ // #endregion
58
+
59
+ // #region Variables
54
60
 
55
61
  /**
56
62
  * Does the device support touch events?
@@ -78,4 +84,10 @@ const supportsTouch: SupporsTouch = (() => {
78
84
  return instance;
79
85
  })();
80
86
 
87
+ // #endregion
88
+
89
+ // #region Exports
90
+
81
91
  export default supportsTouch;
92
+
93
+ // #endregion