@danielgindi/dom-utils 1.0.11 → 2.0.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.
@@ -1,434 +1,448 @@
1
- /**
2
- * @param {Element} el
3
- * @param {string[]} props
4
- * @returns {Object<string, string>}
5
- */
6
- const getCssProps = function (el, props) {
7
- const style = getComputedStyle(el);
8
- let res = {};
9
- for (let prop of props) {
10
- let val = style[prop];
11
-
12
- if ((prop === 'width' || prop === 'height') && val === 'auto' && el instanceof HTMLElement) {
13
- val = el['offset' + prop.substr(0, 1).toUpperCase() + prop.substr(1)];
14
-
15
- if (style.boxSizing !== 'border-box') {
16
- if (prop === 'width') {
17
- val -= parseFloat(style.paddingLeft || 0);
18
- val -= parseFloat(style.paddingRight || 0);
19
- val -= parseFloat(style.borderLeftWidth || 0);
20
- val -= parseFloat(style.borderRightWidth || 0);
21
- } else {
22
- val -= parseFloat(style.paddingTop || 0);
23
- val -= parseFloat(style.paddingBottom || 0);
24
- val -= parseFloat(style.borderTopWidth || 0);
25
- val -= parseFloat(style.borderBottomWidth || 0);
26
- }
27
- if (val < 0)
28
- val = 0;
29
- }
30
-
31
- val += 'px';
32
- }
33
-
34
- res[prop] = val;
35
- }
36
- return res;
37
- };
38
-
39
- /**
40
- * @param {ElementCSSInlineStyle} el
41
- * @param {Object<string, string>} props
42
- */
43
- const setCssProps = function (el, props) {
44
- for (let [key, value] of Object.entries(props))
45
- el.style[key] = (value === undefined || value === null) ? '' : String(value);
46
- };
47
-
48
- const generateGetElementSizeFn = function (pseudo, dim, dimCased, startDim, endDim) {
49
- return (...args) => {
50
- let /**Element|ElementCSSInlineStyle*/el = args[0],
51
- /**string*/pseudoSelector,
52
- /**boolean*/paddings,
53
- /**boolean*/borders,
54
- /**boolean*/margins;
55
-
56
- if (pseudo) {
57
- pseudoSelector = args[1];
58
- paddings = !!args[2];
59
- borders = !!args[3];
60
- margins = !!args[4];
61
- } else {
62
- paddings = !!args[1];
63
- borders = !!args[2];
64
- margins = !!args[3];
65
- }
66
-
67
- if ((/**@type Window*/el) === window) {
68
- return (/**@type Window*/el).document.documentElement[`client${dimCased}`];
69
- }
70
- else if (el.nodeType === 9) { // document
71
- const doc = (/**@type Document*/el).documentElement;
72
- const body = (/**@type Document*/el).body;
73
-
74
- return Math.max(
75
- body[`scroll${dimCased}`],
76
- doc[`scroll${dimCased}`],
77
- body[`offset${dimCased}`],
78
- doc[`offset${dimCased}`],
79
- doc[`client${dimCased}`],
80
- );
81
- }
82
- else {
83
- let value;
84
- let style;
85
- let includesPadding = false, includesBorders = false;
86
-
87
- if (!pseudo && ('getBoundingClientRect' in el)) {
88
- value = el.getBoundingClientRect()[dim];
89
- includesPadding = true;
90
- includesBorders = true;
91
- }
92
-
93
- if (value === undefined || margins || includesPadding !== paddings || includesBorders !== borders) {
94
- style = pseudo ? getComputedStyle(el, pseudoSelector) : getComputedStyle(el);
95
- }
96
-
97
- if (value === undefined) {
98
- let raw = style[dim];
99
- if (raw === 'auto') { // computedStyle is no good here, probably an 'inline' element
100
- value = el[`client${dimCased}`]; // take clientWidth/clientHeight (damn it, it's rounded)
101
- includesPadding = true;
102
- } else {
103
- value = parseFloat(raw);
104
- }
105
-
106
- if (style.boxSizing === 'border-box') {
107
- includesPadding = true;
108
- includesBorders = true;
109
- }
110
- }
111
-
112
- if (paddings !== includesPadding) {
113
- let totalPadding = parseFloat(style[`padding-${startDim}`] || 0) +
114
- parseFloat(style[`padding-${endDim}`] || 0);
115
- if (paddings) value += totalPadding;
116
- else value -= totalPadding;
117
- }
118
-
119
- if (borders !== includesBorders) {
120
- let totalBorders = parseFloat(style[`border-${startDim}-width`] || 0) +
121
- parseFloat(style[`border-${endDim}-width`] || 0);
122
- if (borders) value += totalBorders;
123
- else value -= totalBorders;
124
- }
125
-
126
- if (value < 0)
127
- value = 0;
128
-
129
- if (margins) {
130
- let totalMargins = parseFloat(style[`margin-${startDim}`] || 0) +
131
- parseFloat(style[`margin-${endDim}`] || 0);
132
- value += totalMargins;
133
- }
134
-
135
- return value;
136
- }
137
- };
138
- };
139
-
140
- const generateSetElementSizeFn = (dim, dimCased, startDim, endDim) => {
141
- return (/**Element|ElementCSSInlineStyle*/el,
142
- /**number*/value,
143
- paddings = false, borders = false, margins = false) => {
144
-
145
- if ((/**@type Window*/el) === window) {
146
- return;
147
- }
148
-
149
- if (el.nodeType === 9) { // document
150
- return;
151
- }
152
-
153
- const style = getComputedStyle(el);
154
-
155
- let includesPaddingAndBorders = false;
156
-
157
- if (style.boxSizing === 'border-box') {
158
- includesPaddingAndBorders = true;
159
- }
160
-
161
- paddings = !!paddings;
162
- borders = !!borders;
163
- margins = !!margins;
164
-
165
- if (margins)
166
- value -= (parseFloat(style[`margin-${startDim}`]) || 0) + (parseFloat(style[`margin-${endDim}`]) || 0);
167
-
168
- if (paddings !== includesPaddingAndBorders) {
169
- let totalPadding = parseFloat(style[`padding-${startDim}`] || 0) +
170
- parseFloat(style[`padding-${endDim}`] || 0);
171
- if (paddings) value -= totalPadding;
172
- else value += totalPadding;
173
- }
174
-
175
- if (borders !== includesPaddingAndBorders) {
176
- let totalBorders = (parseFloat(style[`border-${startDim}-width`]) || 0) +
177
- (parseFloat(style[`border-${endDim}-width`]) || 0);
178
- if (borders) value -= totalBorders;
179
- else value += totalBorders;
180
- }
181
-
182
- if (value < 0)
183
- value = 0;
184
-
185
- el.style[dim] = value + 'px';
186
- };
187
- };
188
-
189
- /**
190
- * Gets the width of an element, with fractions
191
- * @function getElementWidth
192
- * @param {Element} el
193
- * @param {boolean} [paddings=false] - include paddings
194
- * @param {boolean} [borders=false] - include borders
195
- * @param {boolean} [margins=false] - include margins
196
- * @returns {number}
197
- */
198
- const getElementWidth = generateGetElementSizeFn(false, 'width', 'Width', 'left', 'right');
199
-
200
- /**
201
- * Gets the width of an element, with fractions
202
- * @function getElementHeight
203
- * @param {Element} el
204
- * @param {boolean} [paddings=false] - include paddings
205
- * @param {boolean} [borders=false] - include borders
206
- * @param {boolean} [margins=false] - include margins
207
- * @returns {number}
208
- */
209
- const getElementHeight = generateGetElementSizeFn(false, 'height', 'Height', 'top', 'bottom');
210
-
211
- /**
212
- * Gets the width of an element, with fractions
213
- * @function getPseudoElementWidth
214
- * @param {Element} el
215
- * @param {string} pseudo
216
- * @param {boolean} [paddings=false] - include paddings
217
- * @param {boolean} [borders=false] - include borders
218
- * @param {boolean} [margins=false] - include margins
219
- * @returns {number}
220
- */
221
- const getPseudoElementWidth = generateGetElementSizeFn(true, 'width', 'Width', 'left', 'right');
222
-
223
- /**
224
- * Gets the width of an element, with fractions
225
- * @function getPseudoElementHeight
226
- * @param {Element} el
227
- * @param {string} pseudo
228
- * @param {boolean} [paddings=false] - include paddings
229
- * @param {boolean} [borders=false] - include borders
230
- * @param {boolean} [margins=false] - include margins
231
- * @returns {number}
232
- */
233
- const getPseudoElementHeight = generateGetElementSizeFn(true, 'height', 'Height', 'top', 'bottom');
234
-
235
- /**
236
- * Sets the width of an element
237
- * @function setElementWidth
238
- * @param {Element} el
239
- * @param {number} value
240
- * @param {boolean} [paddings=false] - include paddings
241
- * @param {boolean} [borders=false] - include borders
242
- * @param {boolean} [margins=false] - include margins
243
- */
244
- const setElementWidth = generateSetElementSizeFn('width', 'Width', 'left', 'right');
245
-
246
- /**
247
- * Sets the width of an element, with fractions
248
- * @function setElementHeight
249
- * @param {Element} el
250
- * @param {number} value
251
- * @param {boolean} [paddings=false] - include paddings
252
- * @param {boolean} [borders=false] - include borders
253
- * @param {boolean} [margins=false] - include margins
254
- */
255
- const setElementHeight = generateSetElementSizeFn('height', 'Height', 'top', 'bottom');
256
-
257
- /**
258
- * Find offset of an element relative to the document, considering scroll offsets
259
- * @param {Element} el
260
- * @returns {{top: number, left: number}}
261
- */
262
- const getElementOffset = el => {
263
- if (!el.getClientRects().length) {
264
- return { top: 0, left: 0 };
265
- }
266
-
267
- let rect = el.getBoundingClientRect();
268
- let view = el.ownerDocument.defaultView;
269
-
270
- return {
271
- top: rect.top + view.pageYOffset,
272
- left: rect.left + view.pageXOffset,
273
- };
274
- };
275
-
276
- /**
277
- * Calculates the anchored position relative to this element. <br />
278
- * The return value will include an `left`/`top` position on left/top axis, <br />
279
- * an `right`/`bottom` position which is the exact opposite, <br />
280
- * and the `xSpec`/`ySpec` after normalizing rtl based values (`'start'`/`'end'`).
281
- * @param {Element} el? - the target element to base the calculations on. If `size` and `rtl` are specified - then this argument is optional.
282
- * @param {'left'|'center'|'right'|'start'|'end'|string|number} xSpec - horizontal anchor specification (could be either `'left'|'center'|'right'|'start'|'end'` or a percentage `'50%'` or a fixed decimal `Number`)
283
- * @param {'top'|'center'|'bottom'|string|number} ySpec - vertical anchor specification (could be either `'top'|'center'|'bottom'` or a percentage `'50%'` or a fixed decimal `Number`)
284
- * @param {{width: number, height: number}?} size - can be specified if already known, to avoid extra dom calculations. This is the outer size of the element, including padding and borders.
285
- * @param {boolean?} [rtl] - if not specified and values like `'start'`/`'end'` are use, then `rtl` is auto detected
286
- * @returns {{left: number, top: number, right: number, bottom: number, xSpec: string|number, ySpec: string|number}}
287
- */
288
- const anchoredPosition = function (el, xSpec, ySpec, size, rtl) {
289
- if (!size) {
290
- size = {
291
- width: getElementWidth(el, true, true),
292
- height: getElementWidth(el, true, true),
293
- };
294
- }
295
-
296
- if (xSpec === 'start' || xSpec === 'end') {
297
- if ((/**@type boolean|null*/rtl) === null || rtl === undefined) {
298
- rtl = getComputedStyle(el).direction === 'rtl';
299
- }
300
- xSpec = xSpec === 'start' ? (rtl ? 'right' : 'left') : (rtl ? 'left' : 'right');
301
- }
302
-
303
- let x, y;
304
- let xInverted, yInverted;
305
-
306
- if (xSpec === 'right') {
307
- x = size.width;
308
- xInverted = 0;
309
- }
310
- else if (xSpec === 'center') {
311
- x = size.width / 2;
312
- xInverted = x;
313
- }
314
- else if (xSpec === 'left') {
315
- x = 0;
316
- xInverted = size.width;
317
- }
318
- else if (xSpec && (typeof xSpec === 'string') && xSpec.charAt(xSpec.length - 1) === '%') {
319
- let percent = ((parseFloat(xSpec) || 0) / 100);
320
- x = size.width * percent;
321
- xInverted = size.width * -percent;
322
-
323
- if ((/**@type boolean|null*/rtl) === null || rtl === undefined)
324
- rtl = getComputedStyle(el).direction === 'rtl';
325
-
326
- if (rtl) {
327
- x = size.width - x;
328
- xInverted = size.width - x;
329
- }
330
- }
331
- else {
332
- x = parseFloat(xSpec) || 0;
333
- xInverted = -x;
334
- }
335
-
336
- if (ySpec === 'bottom') {
337
- y = size.height;
338
- yInverted = 0;
339
- }
340
- else if (ySpec === 'center') {
341
- y = size.height / 2;
342
- yInverted = y;
343
- }
344
- else if (ySpec === 'top') {
345
- y = 0;
346
- yInverted = size.height;
347
- }
348
- else if (ySpec && (typeof ySpec === 'string') && ySpec.charAt(ySpec.length - 1) === '%') {
349
- let percent = ((parseFloat(ySpec) || 0) / 100);
350
- y = size.height * percent;
351
- yInverted = size.height * -percent;
352
- }
353
- else {
354
- y = parseFloat(ySpec) || 0;
355
- yInverted = -y;
356
- }
357
-
358
- return {
359
- left: x,
360
- top: y,
361
- right: xInverted,
362
- bottom: yInverted,
363
- xSpec: xSpec,
364
- ySpec: ySpec,
365
- };
366
- };
367
-
368
- const timeToMs = (value) => {
369
- if (!value) {
370
- return;
371
- }
372
-
373
- let isMs = value.endsWith('ms');
374
- return parseFloat(value) * (isMs ? 1 : 1000);
375
- };
376
-
377
- const isTimeString = (value) => {
378
- return /^-?(0?\.)?\d+m?s$/.test(value);
379
- };
380
-
381
- /**
382
- *
383
- * @param {string} transition
384
- * @return {{duration: number, delay: number, name: string, timingFunction: string?}}
385
- */
386
- const parseSingleTransition = (transition) => {
387
- let parts = transition.split(/\s+/),
388
- name = parts[0],
389
- duration = parts[1],
390
- timingFunctionOrDelay = parts[2],
391
- delay = parts[3];
392
-
393
- if (isTimeString(timingFunctionOrDelay)) {
394
- return {
395
- delay: timeToMs(timingFunctionOrDelay),
396
- duration: timeToMs(duration),
397
- name: name,
398
- };
399
- }
400
-
401
- return {
402
- delay: timeToMs(delay),
403
- duration: timeToMs(duration),
404
- name: name,
405
- timingFunction: timingFunctionOrDelay,
406
- };
407
- };
408
-
409
- /**
410
- *
411
- * @param {string} transition
412
- * @return {{duration: number, delay: number, name: string, timingFunction: string?}[]}
413
- */
414
- const parseTransition = (transition) => {
415
- if (!transition) return [];
416
- return (transition.match(/([^,()]|\([^)]*\))+/g) ?? []).map(value => value.trim()).filter(value => value.length > 0).map(parseSingleTransition);
417
- };
418
-
419
- export {
420
- getCssProps,
421
- setCssProps,
422
- getElementWidth,
423
- getElementHeight,
424
- setElementWidth,
425
- setElementHeight,
426
- getPseudoElementWidth,
427
- getPseudoElementHeight,
428
- getElementOffset,
429
- anchoredPosition,
430
- parseTransition,
431
- parseSingleTransition,
432
- isTimeString,
433
- timeToMs,
434
- };
1
+ /**
2
+ * @param {Element} el
3
+ * @param {string[]} props
4
+ * @returns {Object<string, string>}
5
+ */
6
+ const getCssProps = function (el: Element, props: string[]): Record<string, string> {
7
+ const style = getComputedStyle(el) as CSSStyleDeclaration & Record<string, string>;
8
+ let res: Record<string, string> = {};
9
+ for (let prop of props) {
10
+ let val: string | number = style[prop];
11
+
12
+ if ((prop === 'width' || prop === 'height') && val === 'auto' && el instanceof HTMLElement) {
13
+ val = el[prop === 'width' ? 'offsetWidth' : 'offsetHeight'];
14
+
15
+ if (style.boxSizing !== 'border-box') {
16
+ if (prop === 'width') {
17
+ val -= parseFloat(style.paddingLeft || '0');
18
+ val -= parseFloat(style.paddingRight || '0');
19
+ val -= parseFloat(style.borderLeftWidth || '0');
20
+ val -= parseFloat(style.borderRightWidth || '0');
21
+ } else {
22
+ val -= parseFloat(style.paddingTop || '0');
23
+ val -= parseFloat(style.paddingBottom || '0');
24
+ val -= parseFloat(style.borderTopWidth || '0');
25
+ val -= parseFloat(style.borderBottomWidth || '0');
26
+ }
27
+ if (val < 0)
28
+ val = 0;
29
+ }
30
+
31
+ val = val + 'px';
32
+ }
33
+
34
+ res[prop] = val as string;
35
+ }
36
+ return res;
37
+ };
38
+
39
+ /**
40
+ * @param {ElementCSSInlineStyle} el
41
+ * @param {Object<string, string>} props
42
+ */
43
+ const setCssProps = function (el: ElementCSSInlineStyle, props: Record<string, unknown>): void {
44
+ const style = el.style as CSSStyleDeclaration & Record<string, string>;
45
+ for (let [key, value] of Object.entries(props))
46
+ style[key] = (value === undefined || value === null) ? '' : String(value);
47
+ };
48
+
49
+ const generateGetElementSizeFn = function (pseudo: boolean, dim: 'width' | 'height', dimCased: 'Width' | 'Height', startDim: string, endDim: string) {
50
+ // The exported wrappers below provide precise signatures for these dynamic arguments.
51
+ return (...args: any[]) => {
52
+ let /**Element|ElementCSSInlineStyle*/el = args[0],
53
+ /**string*/pseudoSelector,
54
+ /**boolean*/paddings,
55
+ /**boolean*/borders,
56
+ /**boolean*/margins;
57
+
58
+ if (pseudo) {
59
+ pseudoSelector = args[1];
60
+ paddings = !!args[2];
61
+ borders = !!args[3];
62
+ margins = !!args[4];
63
+ } else {
64
+ paddings = !!args[1];
65
+ borders = !!args[2];
66
+ margins = !!args[3];
67
+ }
68
+
69
+ if ((/**@type Window*/el) === window) {
70
+ return (/**@type Window*/el).document.documentElement[`client${dimCased}`];
71
+ }
72
+ else if (el.nodeType === 9) { // document
73
+ const doc = (/**@type Document*/el).documentElement;
74
+ const body = (/**@type Document*/el).body;
75
+
76
+ return Math.max(
77
+ body[`scroll${dimCased}`],
78
+ doc[`scroll${dimCased}`],
79
+ body[`offset${dimCased}`],
80
+ doc[`offset${dimCased}`],
81
+ doc[`client${dimCased}`],
82
+ );
83
+ }
84
+ else {
85
+ let value;
86
+ let style: (CSSStyleDeclaration & Record<string, string>) | undefined;
87
+ let includesPadding = false, includesBorders = false;
88
+
89
+ if (!pseudo && ('getBoundingClientRect' in el)) {
90
+ value = el.getBoundingClientRect()[dim];
91
+ includesPadding = true;
92
+ includesBorders = true;
93
+ }
94
+
95
+ if (value === undefined || margins || includesPadding !== paddings || includesBorders !== borders) {
96
+ style = (pseudo ? getComputedStyle(el, pseudoSelector) : getComputedStyle(el)) as CSSStyleDeclaration & Record<string, string>;
97
+ }
98
+
99
+ if (value === undefined) {
100
+ let raw = style[dim];
101
+ if (raw === 'auto') { // computedStyle is no good here, probably an 'inline' element
102
+ value = el[`client${dimCased}`]; // take clientWidth/clientHeight (damn it, it's rounded)
103
+ includesPadding = true;
104
+ } else {
105
+ value = parseFloat(raw);
106
+ }
107
+
108
+ if (style.boxSizing === 'border-box') {
109
+ includesPadding = true;
110
+ includesBorders = true;
111
+ }
112
+ }
113
+
114
+ if (paddings !== includesPadding) {
115
+ let totalPadding = parseFloat(style[`padding-${startDim}`] || '0') +
116
+ parseFloat(style[`padding-${endDim}`] || '0');
117
+ if (paddings) value += totalPadding;
118
+ else value -= totalPadding;
119
+ }
120
+
121
+ if (borders !== includesBorders) {
122
+ let totalBorders = parseFloat(style[`border-${startDim}-width`] || '0') +
123
+ parseFloat(style[`border-${endDim}-width`] || '0');
124
+ if (borders) value += totalBorders;
125
+ else value -= totalBorders;
126
+ }
127
+
128
+ if (value < 0)
129
+ value = 0;
130
+
131
+ if (margins) {
132
+ let totalMargins = parseFloat(style[`margin-${startDim}`] || '0') +
133
+ parseFloat(style[`margin-${endDim}`] || '0');
134
+ value += totalMargins;
135
+ }
136
+
137
+ return value;
138
+ }
139
+ };
140
+ };
141
+
142
+ const generateSetElementSizeFn = (dim: 'width' | 'height', dimCased: 'Width' | 'Height', startDim: string, endDim: string) => {
143
+ return (/**Element|ElementCSSInlineStyle*/el: any,
144
+ /**number*/value: number,
145
+ paddings = false, borders = false, margins = false) => {
146
+
147
+ if ((/**@type Window*/el) === window) {
148
+ return;
149
+ }
150
+
151
+ if (el.nodeType === 9) { // document
152
+ return;
153
+ }
154
+
155
+ const style = getComputedStyle(el) as CSSStyleDeclaration & Record<string, string>;
156
+
157
+ let includesPaddingAndBorders = false;
158
+
159
+ if (style.boxSizing === 'border-box') {
160
+ includesPaddingAndBorders = true;
161
+ }
162
+
163
+ paddings = !!paddings;
164
+ borders = !!borders;
165
+ margins = !!margins;
166
+
167
+ if (margins)
168
+ value -= (parseFloat(style[`margin-${startDim}`]) || 0) + (parseFloat(style[`margin-${endDim}`]) || 0);
169
+
170
+ if (paddings !== includesPaddingAndBorders) {
171
+ let totalPadding = parseFloat(style[`padding-${startDim}`] || '0') +
172
+ parseFloat(style[`padding-${endDim}`] || '0');
173
+ if (paddings) value -= totalPadding;
174
+ else value += totalPadding;
175
+ }
176
+
177
+ if (borders !== includesPaddingAndBorders) {
178
+ let totalBorders = (parseFloat(style[`border-${startDim}-width`]) || 0) +
179
+ (parseFloat(style[`border-${endDim}-width`]) || 0);
180
+ if (borders) value -= totalBorders;
181
+ else value += totalBorders;
182
+ }
183
+
184
+ if (value < 0)
185
+ value = 0;
186
+
187
+ el.style[dim] = value + 'px';
188
+ };
189
+ };
190
+
191
+ /**
192
+ * Gets the width of an element, with fractions
193
+ * @function getElementWidth
194
+ * @param {Element} el
195
+ * @param {boolean} [paddings=false] - include paddings
196
+ * @param {boolean} [borders=false] - include borders
197
+ * @param {boolean} [margins=false] - include margins
198
+ * @returns {number}
199
+ */
200
+ const getElementWidth = generateGetElementSizeFn(false, 'width', 'Width', 'left', 'right') as (
201
+ el: Element | Document | Window, paddings?: boolean, borders?: boolean, margins?: boolean
202
+ ) => number;
203
+
204
+ /**
205
+ * Gets the width of an element, with fractions
206
+ * @function getElementHeight
207
+ * @param {Element} el
208
+ * @param {boolean} [paddings=false] - include paddings
209
+ * @param {boolean} [borders=false] - include borders
210
+ * @param {boolean} [margins=false] - include margins
211
+ * @returns {number}
212
+ */
213
+ const getElementHeight = generateGetElementSizeFn(false, 'height', 'Height', 'top', 'bottom') as (
214
+ el: Element | Document | Window, paddings?: boolean, borders?: boolean, margins?: boolean
215
+ ) => number;
216
+
217
+ /**
218
+ * Gets the width of an element, with fractions
219
+ * @function getPseudoElementWidth
220
+ * @param {Element} el
221
+ * @param {string} pseudo
222
+ * @param {boolean} [paddings=false] - include paddings
223
+ * @param {boolean} [borders=false] - include borders
224
+ * @param {boolean} [margins=false] - include margins
225
+ * @returns {number}
226
+ */
227
+ const getPseudoElementWidth = generateGetElementSizeFn(true, 'width', 'Width', 'left', 'right') as (
228
+ el: Element, pseudo: string, paddings?: boolean, borders?: boolean, margins?: boolean
229
+ ) => number;
230
+
231
+ /**
232
+ * Gets the width of an element, with fractions
233
+ * @function getPseudoElementHeight
234
+ * @param {Element} el
235
+ * @param {string} pseudo
236
+ * @param {boolean} [paddings=false] - include paddings
237
+ * @param {boolean} [borders=false] - include borders
238
+ * @param {boolean} [margins=false] - include margins
239
+ * @returns {number}
240
+ */
241
+ const getPseudoElementHeight = generateGetElementSizeFn(true, 'height', 'Height', 'top', 'bottom') as (
242
+ el: Element, pseudo: string, paddings?: boolean, borders?: boolean, margins?: boolean
243
+ ) => number;
244
+
245
+ /**
246
+ * Sets the width of an element
247
+ * @function setElementWidth
248
+ * @param {Element} el
249
+ * @param {number} value
250
+ * @param {boolean} [paddings=false] - include paddings
251
+ * @param {boolean} [borders=false] - include borders
252
+ * @param {boolean} [margins=false] - include margins
253
+ */
254
+ const setElementWidth = generateSetElementSizeFn('width', 'Width', 'left', 'right') as (
255
+ el: ElementCSSInlineStyle, value: number, paddings?: boolean, borders?: boolean, margins?: boolean
256
+ ) => void;
257
+
258
+ /**
259
+ * Sets the width of an element, with fractions
260
+ * @function setElementHeight
261
+ * @param {Element} el
262
+ * @param {number} value
263
+ * @param {boolean} [paddings=false] - include paddings
264
+ * @param {boolean} [borders=false] - include borders
265
+ * @param {boolean} [margins=false] - include margins
266
+ */
267
+ const setElementHeight = generateSetElementSizeFn('height', 'Height', 'top', 'bottom') as (
268
+ el: ElementCSSInlineStyle, value: number, paddings?: boolean, borders?: boolean, margins?: boolean
269
+ ) => void;
270
+
271
+ /**
272
+ * Find offset of an element relative to the document, considering scroll offsets
273
+ * @param {Element} el
274
+ * @returns {{top: number, left: number}}
275
+ */
276
+ const getElementOffset = (el: Element): { top: number; left: number } => {
277
+ if (!el.getClientRects().length) {
278
+ return { top: 0, left: 0 };
279
+ }
280
+
281
+ let rect = el.getBoundingClientRect();
282
+ let view = el.ownerDocument.defaultView;
283
+
284
+ return {
285
+ top: rect.top + view.pageYOffset,
286
+ left: rect.left + view.pageXOffset,
287
+ };
288
+ };
289
+
290
+ /**
291
+ * Calculates the anchored position relative to this element. <br />
292
+ * The return value will include an `left`/`top` position on left/top axis, <br />
293
+ * an `right`/`bottom` position which is the exact opposite, <br />
294
+ * and the `xSpec`/`ySpec` after normalizing rtl based values (`'start'`/`'end'`).
295
+ * @param {Element} el? - the target element to base the calculations on. If `size` and `rtl` are specified - then this argument is optional.
296
+ * @param {'left'|'center'|'right'|'start'|'end'|string|number} xSpec - horizontal anchor specification (could be either `'left'|'center'|'right'|'start'|'end'` or a percentage `'50%'` or a fixed decimal `Number`)
297
+ * @param {'top'|'center'|'bottom'|string|number} ySpec - vertical anchor specification (could be either `'top'|'center'|'bottom'` or a percentage `'50%'` or a fixed decimal `Number`)
298
+ * @param {{width: number, height: number}?} size - can be specified if already known, to avoid extra dom calculations. This is the outer size of the element, including padding and borders.
299
+ * @param {boolean?} [rtl] - if not specified and values like `'start'`/`'end'` are use, then `rtl` is auto detected
300
+ * @returns {{left: number, top: number, right: number, bottom: number, xSpec: string|number, ySpec: string|number}}
301
+ */
302
+ const anchoredPosition = function (el: Element | undefined, xSpec: string | number, ySpec: string | number, size?: { width: number; height: number }, rtl?: boolean | null): { left: number; top: number; right: number; bottom: number; xSpec: string | number; ySpec: string | number } {
303
+ if (!size) {
304
+ size = {
305
+ width: getElementWidth(el, true, true),
306
+ height: getElementWidth(el, true, true),
307
+ };
308
+ }
309
+
310
+ if (xSpec === 'start' || xSpec === 'end') {
311
+ if ((/**@type boolean|null*/rtl) === null || rtl === undefined) {
312
+ rtl = getComputedStyle(el).direction === 'rtl';
313
+ }
314
+ xSpec = xSpec === 'start' ? (rtl ? 'right' : 'left') : (rtl ? 'left' : 'right');
315
+ }
316
+
317
+ let x, y;
318
+ let xInverted, yInverted;
319
+
320
+ if (xSpec === 'right') {
321
+ x = size.width;
322
+ xInverted = 0;
323
+ }
324
+ else if (xSpec === 'center') {
325
+ x = size.width / 2;
326
+ xInverted = x;
327
+ }
328
+ else if (xSpec === 'left') {
329
+ x = 0;
330
+ xInverted = size.width;
331
+ }
332
+ else if (xSpec && (typeof xSpec === 'string') && xSpec.charAt(xSpec.length - 1) === '%') {
333
+ let percent = ((parseFloat(xSpec) || 0) / 100);
334
+ x = size.width * percent;
335
+ xInverted = size.width * -percent;
336
+
337
+ if ((/**@type boolean|null*/rtl) === null || rtl === undefined)
338
+ rtl = getComputedStyle(el).direction === 'rtl';
339
+
340
+ if (rtl) {
341
+ x = size.width - x;
342
+ xInverted = size.width - x;
343
+ }
344
+ }
345
+ else {
346
+ x = parseFloat(String(xSpec)) || 0;
347
+ xInverted = -x;
348
+ }
349
+
350
+ if (ySpec === 'bottom') {
351
+ y = size.height;
352
+ yInverted = 0;
353
+ }
354
+ else if (ySpec === 'center') {
355
+ y = size.height / 2;
356
+ yInverted = y;
357
+ }
358
+ else if (ySpec === 'top') {
359
+ y = 0;
360
+ yInverted = size.height;
361
+ }
362
+ else if (ySpec && (typeof ySpec === 'string') && ySpec.charAt(ySpec.length - 1) === '%') {
363
+ let percent = ((parseFloat(ySpec) || 0) / 100);
364
+ y = size.height * percent;
365
+ yInverted = size.height * -percent;
366
+ }
367
+ else {
368
+ y = parseFloat(String(ySpec)) || 0;
369
+ yInverted = -y;
370
+ }
371
+
372
+ return {
373
+ left: x,
374
+ top: y,
375
+ right: xInverted,
376
+ bottom: yInverted,
377
+ xSpec: xSpec,
378
+ ySpec: ySpec,
379
+ };
380
+ };
381
+
382
+ const timeToMs = (value?: string): number | undefined => {
383
+ if (!value) {
384
+ return;
385
+ }
386
+
387
+ let isMs = value.endsWith('ms');
388
+ return parseFloat(value) * (isMs ? 1 : 1000);
389
+ };
390
+
391
+ const isTimeString = (value: string): boolean => {
392
+ return /^-?(0?\.)?\d+m?s$/.test(value);
393
+ };
394
+
395
+ /**
396
+ *
397
+ * @param {string} transition
398
+ * @return {{duration: number, delay: number, name: string, timingFunction: string?}}
399
+ */
400
+ const parseSingleTransition = (transition: string): { duration?: number; delay?: number; name: string; timingFunction?: string } => {
401
+ let parts = transition.split(/\s+/),
402
+ name = parts[0],
403
+ duration = parts[1],
404
+ timingFunctionOrDelay = parts[2],
405
+ delay = parts[3];
406
+
407
+ if (isTimeString(timingFunctionOrDelay)) {
408
+ return {
409
+ delay: timeToMs(timingFunctionOrDelay),
410
+ duration: timeToMs(duration),
411
+ name: name,
412
+ };
413
+ }
414
+
415
+ return {
416
+ delay: timeToMs(delay),
417
+ duration: timeToMs(duration),
418
+ name: name,
419
+ timingFunction: timingFunctionOrDelay,
420
+ };
421
+ };
422
+
423
+ /**
424
+ *
425
+ * @param {string} transition
426
+ * @return {{duration: number, delay: number, name: string, timingFunction: string?}[]}
427
+ */
428
+ const parseTransition = (transition: string): { duration?: number; delay?: number; name: string; timingFunction?: string }[] => {
429
+ if (!transition) return [];
430
+ return (transition.match(/([^,()]|\([^)]*\))+/g) ?? []).map(value => value.trim()).filter(value => value.length > 0).map(parseSingleTransition);
431
+ };
432
+
433
+ export {
434
+ getCssProps,
435
+ setCssProps,
436
+ getElementWidth,
437
+ getElementHeight,
438
+ setElementWidth,
439
+ setElementHeight,
440
+ getPseudoElementWidth,
441
+ getPseudoElementHeight,
442
+ getElementOffset,
443
+ anchoredPosition,
444
+ parseTransition,
445
+ parseSingleTransition,
446
+ isTimeString,
447
+ timeToMs,
448
+ };