@thednp/shorty 1.0.1 → 1.0.2
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/README.md +12 -7
- package/dist/shorty.esm.js +379 -312
- package/dist/shorty.esm.min.js +2 -2
- package/dist/shorty.js +392 -319
- package/dist/shorty.min.js +2 -2
- package/package.json +11 -2
- package/src/attr/getAttribute.js +1 -1
- package/src/attr/getAttributeNS.js +1 -1
- package/src/attr/hasAttribute.js +1 -1
- package/src/attr/hasAttributeNS.js +1 -1
- package/src/attr/removeAttribute.js +1 -1
- package/src/attr/removeAttributeNS.js +1 -1
- package/src/attr/setAttribute.js +1 -1
- package/src/attr/setAttributeNS.js +1 -1
- package/src/boolean/isApple.js +2 -2
- package/src/boolean/isFirefox.js +2 -1
- package/src/boolean/isMobile.js +2 -2
- package/src/boolean/support3DTransform.js +2 -1
- package/src/boolean/supportAnimation.js +2 -1
- package/src/boolean/supportPassive.js +1 -1
- package/src/boolean/supportTouch.js +2 -1
- package/src/boolean/supportTransform.js +2 -1
- package/src/boolean/supportTransition.js +2 -1
- package/src/class/addClass.js +1 -1
- package/src/class/hasClass.js +1 -1
- package/src/class/removeClass.js +1 -1
- package/src/event/off.js +2 -6
- package/src/event/on.js +2 -6
- package/src/event/one.js +3 -6
- package/src/get/getBoundingClientRect.js +9 -5
- package/src/get/getDocument.js +8 -3
- package/src/get/getDocumentBody.js +2 -2
- package/src/get/getDocumentElement.js +2 -2
- package/src/get/getDocumentHead.js +2 -2
- package/src/get/getElementAnimationDelay.js +3 -3
- package/src/get/getElementAnimationDelayLegacy.js +3 -3
- package/src/get/getElementAnimationDuration.js +3 -3
- package/src/get/getElementAnimationDurationLegacy.js +3 -3
- package/src/get/getElementStyle.js +5 -3
- package/src/get/getElementTransitionDelay.js +3 -4
- package/src/get/getElementTransitionDelayLegacy.js +3 -3
- package/src/get/getElementTransitionDuration.js +3 -3
- package/src/get/getElementTransitionDurationLegacy.js +3 -3
- package/src/get/getNodeScroll.js +1 -1
- package/src/get/getParentNode.js +4 -5
- package/src/get/getRectRelativeToOffsetParent.js +7 -5
- package/src/get/getUID.js +1 -1
- package/src/get/getWindow.js +16 -7
- package/src/index.js +10 -6
- package/src/is/isArray.js +0 -1
- package/src/is/isCustomElement.js +1 -1
- package/src/is/isDocument.js +3 -2
- package/src/is/isElement.js +14 -1
- package/src/is/isElementInScrollRange.js +4 -2
- package/src/is/isElementInViewport.js +6 -2
- package/src/is/isElementsArray.js +3 -1
- package/src/is/isFunction.js +2 -3
- package/src/is/isHTMLCollection.js +1 -1
- package/src/is/isHTMLElement.js +2 -1
- package/src/is/isHTMLImageElement.js +1 -1
- package/src/is/isMedia.js +5 -3
- package/src/is/isNode.js +2 -1
- package/src/is/isNodeList.js +2 -1
- package/src/is/isNumber.js +8 -0
- package/src/is/isRTL.js +1 -1
- package/src/is/isSVGElement.js +4 -1
- package/src/is/isScaledElement.js +2 -0
- package/src/is/isShadowRoot.js +2 -5
- package/src/is/isString.js +2 -2
- package/src/is/isTableElement.js +5 -1
- package/src/is/isWindow.js +5 -5
- package/src/misc/ObjectEntries.js +7 -0
- package/src/misc/OriginalEvent.js +2 -1
- package/src/misc/createElement.js +3 -1
- package/src/misc/createElementNS.js +7 -3
- package/src/misc/data.js +16 -16
- package/src/misc/dispatchEvent.js +1 -1
- package/src/misc/emulateAnimationEnd.js +5 -2
- package/src/misc/emulateAnimationEndLegacy.js +9 -6
- package/src/misc/emulateTransitionEnd.js +5 -2
- package/src/misc/emulateTransitionEndLegacy.js +9 -6
- package/src/misc/focus.js +1 -2
- package/src/misc/normalizeOptions.js +2 -2
- package/src/misc/normalizeValue.js +8 -6
- package/src/misc/passiveHandlerLegacy.js +2 -1
- package/src/misc/reflow.js +1 -2
- package/src/misc/setElementStyle.js +12 -3
- package/src/misc/timer.js +16 -19
- package/src/misc/version.js +0 -1
- package/src/selectors/closest.js +3 -3
- package/src/selectors/getCustomElements.js +4 -5
- package/src/selectors/getElementById.js +6 -3
- package/src/selectors/getElementsByClassName.js +4 -3
- package/src/selectors/getElementsByTagName.js +4 -3
- package/src/selectors/matches.js +1 -1
- package/src/selectors/matchesLegacy.js +8 -8
- package/src/selectors/querySelector.js +9 -7
- package/src/selectors/querySelectorAll.js +4 -3
- package/src/strings/animationDelayLegacy.js +2 -1
- package/src/strings/animationDurationLegacy.js +2 -1
- package/src/strings/animationEndEventLegacy.js +2 -1
- package/src/strings/animationNameLegacy.js +2 -1
- package/src/strings/mouseHoverEvents.js +2 -1
- package/src/strings/transitionDelayLegacy.js +2 -1
- package/src/strings/transitionDurationLegacy.js +2 -1
- package/src/strings/transitionEndEventLegacy.js +2 -1
- package/src/strings/transitionPropertyLegacy.js +2 -2
- package/src/strings/userAgentData.js +0 -1
- package/types/index.d.ts +9 -3
- package/types/module/shorty.ts +2 -2
- package/types/shorty.d.ts +237 -224
- package/src/misc/tryWrapper.js +0 -11
- package/src/selectors/documentAll.js +0 -8
package/src/misc/data.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import isHTMLElement from '../is/isHTMLElement';
|
|
2
2
|
|
|
3
|
-
/** @type {Map<string, Map<HTMLElement
|
|
3
|
+
/** @type {Map<string, Map<HTMLElement, Record<string, any>>>} */
|
|
4
4
|
const componentData = new Map();
|
|
5
5
|
/**
|
|
6
6
|
* An interface for web components background data.
|
|
@@ -9,27 +9,27 @@ const componentData = new Map();
|
|
|
9
9
|
const Data = {
|
|
10
10
|
/**
|
|
11
11
|
* Sets web components data.
|
|
12
|
-
* @param {HTMLElement
|
|
12
|
+
* @param {HTMLElement} element target element
|
|
13
13
|
* @param {string} component the component's name or a unique key
|
|
14
14
|
* @param {Record<string, any>} instance the component instance
|
|
15
15
|
*/
|
|
16
|
-
set: (
|
|
17
|
-
|
|
18
|
-
if (!element) return;
|
|
16
|
+
set: (element, component, instance) => {
|
|
17
|
+
if (!isHTMLElement(element)) return;
|
|
19
18
|
|
|
19
|
+
/* istanbul ignore else */
|
|
20
20
|
if (!componentData.has(component)) {
|
|
21
21
|
componentData.set(component, new Map());
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
const instanceMap = componentData.get(component);
|
|
25
|
-
//
|
|
25
|
+
// not undefined, but defined right above
|
|
26
26
|
instanceMap.set(element, instance);
|
|
27
27
|
},
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Returns all instances for specified component.
|
|
31
31
|
* @param {string} component the component's name or a unique key
|
|
32
|
-
* @returns {Map<HTMLElement
|
|
32
|
+
* @returns {Map<HTMLElement, Record<string, any>>?} all the component instances
|
|
33
33
|
*/
|
|
34
34
|
getAllFor: (component) => {
|
|
35
35
|
const instanceMap = componentData.get(component);
|
|
@@ -39,12 +39,12 @@ const Data = {
|
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
41
|
* Returns the instance associated with the target.
|
|
42
|
-
* @param {HTMLElement
|
|
42
|
+
* @param {HTMLElement} element target element
|
|
43
43
|
* @param {string} component the component's name or a unique key
|
|
44
44
|
* @returns {Record<string, any>?} the instance
|
|
45
45
|
*/
|
|
46
|
-
get: (
|
|
47
|
-
|
|
46
|
+
get: (element, component) => {
|
|
47
|
+
if (!isHTMLElement(element) || !component) return null;
|
|
48
48
|
const allForC = Data.getAllFor(component);
|
|
49
49
|
const instance = element && allForC && allForC.get(element);
|
|
50
50
|
|
|
@@ -53,16 +53,16 @@ const Data = {
|
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
55
|
* Removes web components data.
|
|
56
|
-
* @param {HTMLElement
|
|
56
|
+
* @param {HTMLElement} element target element
|
|
57
57
|
* @param {string} component the component's name or a unique key
|
|
58
58
|
*/
|
|
59
|
-
remove: (
|
|
60
|
-
const element = querySelector(target);
|
|
59
|
+
remove: (element, component) => {
|
|
61
60
|
const instanceMap = componentData.get(component);
|
|
62
|
-
if (!instanceMap || !element) return;
|
|
61
|
+
if (!instanceMap || !isHTMLElement(element)) return;
|
|
63
62
|
|
|
64
63
|
instanceMap.delete(element);
|
|
65
64
|
|
|
65
|
+
/* istanbul ignore else */
|
|
66
66
|
if (instanceMap.size === 0) {
|
|
67
67
|
componentData.delete(component);
|
|
68
68
|
}
|
|
@@ -71,7 +71,7 @@ const Data = {
|
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
73
|
* An alias for `Data.get()`.
|
|
74
|
-
* @type {
|
|
74
|
+
* @type {SHORTY.getInstance<any>}
|
|
75
75
|
*/
|
|
76
76
|
export const getInstance = (target, component) => Data.get(target, component);
|
|
77
77
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shortcut for the `Element.dispatchEvent(Event)` method.
|
|
3
3
|
*
|
|
4
|
-
* @param {HTMLElement
|
|
4
|
+
* @param {HTMLElement} element is the target
|
|
5
5
|
* @param {Event} event is the `Event` object
|
|
6
6
|
*/
|
|
7
7
|
const dispatchEvent = (element, event) => element.dispatchEvent(event);
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import animationEndEvent from '../strings/animationEndEvent';
|
|
2
2
|
import getElementAnimationDelay from '../get/getElementAnimationDelay';
|
|
3
3
|
import getElementAnimationDuration from '../get/getElementAnimationDuration';
|
|
4
|
+
import dispatchEvent from './dispatchEvent';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Utility to make sure callbacks are consistently
|
|
7
8
|
* called when animation ends.
|
|
8
9
|
*
|
|
9
|
-
* @param {HTMLElement
|
|
10
|
+
* @param {HTMLElement} element target
|
|
10
11
|
* @param {EventListener} handler `animationend` callback
|
|
11
12
|
*/
|
|
12
13
|
export default function emulateAnimationEnd(element, handler) {
|
|
@@ -21,6 +22,7 @@ export default function emulateAnimationEnd(element, handler) {
|
|
|
21
22
|
* @type {EventListener}
|
|
22
23
|
*/
|
|
23
24
|
const animationEndWrapper = (e) => {
|
|
25
|
+
/* istanbul ignore else */
|
|
24
26
|
if (e.target === element) {
|
|
25
27
|
handler.apply(element, [e]);
|
|
26
28
|
element.removeEventListener(animationEndEvent, animationEndWrapper);
|
|
@@ -29,7 +31,8 @@ export default function emulateAnimationEnd(element, handler) {
|
|
|
29
31
|
};
|
|
30
32
|
element.addEventListener(animationEndEvent, animationEndWrapper);
|
|
31
33
|
setTimeout(() => {
|
|
32
|
-
|
|
34
|
+
/* istanbul ignore next */
|
|
35
|
+
if (!called) dispatchEvent(element, endEvent);
|
|
33
36
|
}, duration + delay + 17);
|
|
34
37
|
} else {
|
|
35
38
|
handler.apply(element, [endEvent]);
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import supportAnimation from '../boolean/supportAnimation';
|
|
2
|
-
import
|
|
2
|
+
import animationEndEventLegacy from '../strings/animationEndEventLegacy';
|
|
3
3
|
import getElementAnimationDelay from '../get/getElementAnimationDelayLegacy';
|
|
4
4
|
import getElementAnimationDuration from '../get/getElementAnimationDurationLegacy';
|
|
5
|
+
import dispatchEvent from './dispatchEvent';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Utility to make sure callbacks are consistently
|
|
8
9
|
* called when animation ends.
|
|
9
10
|
*
|
|
10
|
-
* @param {HTMLElement
|
|
11
|
+
* @param {HTMLElement} element target
|
|
11
12
|
* @param {EventListener} handler `animationend` callback
|
|
12
13
|
*/
|
|
13
14
|
export default function emulateAnimationEnd(element, handler) {
|
|
14
15
|
let called = 0;
|
|
15
|
-
const endEvent = new Event(
|
|
16
|
+
const endEvent = new Event(animationEndEventLegacy);
|
|
16
17
|
const duration = getElementAnimationDuration(element);
|
|
17
18
|
const delay = getElementAnimationDelay(element);
|
|
18
19
|
|
|
@@ -22,15 +23,17 @@ export default function emulateAnimationEnd(element, handler) {
|
|
|
22
23
|
* @param {Event} e Event object
|
|
23
24
|
*/
|
|
24
25
|
const animationEndWrapper = (e) => {
|
|
26
|
+
/* istanbul ignore else */
|
|
25
27
|
if (e.target === element) {
|
|
26
28
|
handler.apply(element, [e]);
|
|
27
|
-
element.removeEventListener(
|
|
29
|
+
element.removeEventListener(animationEndEventLegacy, animationEndWrapper);
|
|
28
30
|
called = 1;
|
|
29
31
|
}
|
|
30
32
|
};
|
|
31
|
-
element.addEventListener(
|
|
33
|
+
element.addEventListener(animationEndEventLegacy, animationEndWrapper);
|
|
32
34
|
setTimeout(() => {
|
|
33
|
-
|
|
35
|
+
/* istanbul ignore next */
|
|
36
|
+
if (!called) dispatchEvent(element, endEvent);
|
|
34
37
|
}, duration + delay + 17);
|
|
35
38
|
} else {
|
|
36
39
|
handler.apply(element, [endEvent]);
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import transitionEndEvent from '../strings/transitionEndEvent';
|
|
2
2
|
import getElementTransitionDelay from '../get/getElementTransitionDelay';
|
|
3
3
|
import getElementTransitionDuration from '../get/getElementTransitionDuration';
|
|
4
|
+
import dispatchEvent from './dispatchEvent';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Utility to make sure callbacks are consistently
|
|
7
8
|
* called when transition ends.
|
|
8
9
|
*
|
|
9
|
-
* @param {HTMLElement
|
|
10
|
+
* @param {HTMLElement} element target
|
|
10
11
|
* @param {EventListener} handler `transitionend` callback
|
|
11
12
|
*/
|
|
12
13
|
export default function emulateTransitionEnd(element, handler) {
|
|
@@ -21,6 +22,7 @@ export default function emulateTransitionEnd(element, handler) {
|
|
|
21
22
|
* @type {EventListener} e Event object
|
|
22
23
|
*/
|
|
23
24
|
const transitionEndWrapper = (e) => {
|
|
25
|
+
/* istanbul ignore else */
|
|
24
26
|
if (e.target === element) {
|
|
25
27
|
handler.apply(element, [e]);
|
|
26
28
|
element.removeEventListener(transitionEndEvent, transitionEndWrapper);
|
|
@@ -29,7 +31,8 @@ export default function emulateTransitionEnd(element, handler) {
|
|
|
29
31
|
};
|
|
30
32
|
element.addEventListener(transitionEndEvent, transitionEndWrapper);
|
|
31
33
|
setTimeout(() => {
|
|
32
|
-
|
|
34
|
+
/* istanbul ignore next */
|
|
35
|
+
if (!called) dispatchEvent(element, endEvent);
|
|
33
36
|
}, duration + delay + 17);
|
|
34
37
|
} else {
|
|
35
38
|
handler.apply(element, [endEvent]);
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import supportTransition from '../boolean/supportTransition';
|
|
2
|
-
import
|
|
2
|
+
import transitionEndEventLegacy from '../strings/transitionEndEventLegacy';
|
|
3
3
|
import getElementTransitionDelay from '../get/getElementTransitionDelayLegacy';
|
|
4
4
|
import getElementTransitionDuration from '../get/getElementTransitionDurationLegacy';
|
|
5
|
+
import dispatchEvent from './dispatchEvent';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Utility to make sure callbacks are consistently
|
|
8
9
|
* called when transition ends.
|
|
9
10
|
*
|
|
10
|
-
* @param {HTMLElement
|
|
11
|
+
* @param {HTMLElement} element target
|
|
11
12
|
* @param {EventListener} handler `transitionend` callback
|
|
12
13
|
*/
|
|
13
14
|
export default function emulateTransitionEnd(element, handler) {
|
|
14
15
|
let called = 0;
|
|
15
|
-
const endEvent = new Event(
|
|
16
|
+
const endEvent = new Event(transitionEndEventLegacy);
|
|
16
17
|
const duration = getElementTransitionDuration(element);
|
|
17
18
|
const delay = getElementTransitionDelay(element);
|
|
18
19
|
|
|
@@ -22,15 +23,17 @@ export default function emulateTransitionEnd(element, handler) {
|
|
|
22
23
|
* @param {Event} e Event object
|
|
23
24
|
*/
|
|
24
25
|
const transitionEndWrapper = (e) => {
|
|
26
|
+
/* istanbul ignore else */
|
|
25
27
|
if (e.target === element) {
|
|
26
28
|
handler.apply(element, [e]);
|
|
27
|
-
element.removeEventListener(
|
|
29
|
+
element.removeEventListener(transitionEndEventLegacy, transitionEndWrapper);
|
|
28
30
|
called = 1;
|
|
29
31
|
}
|
|
30
32
|
};
|
|
31
|
-
element.addEventListener(
|
|
33
|
+
element.addEventListener(transitionEndEventLegacy, transitionEndWrapper);
|
|
32
34
|
setTimeout(() => {
|
|
33
|
-
|
|
35
|
+
/* istanbul ignore next */
|
|
36
|
+
if (!called) dispatchEvent(element, endEvent);
|
|
34
37
|
}, duration + delay + 17);
|
|
35
38
|
} else {
|
|
36
39
|
handler.apply(element, [endEvent]);
|
package/src/misc/focus.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Utility to focus an `HTMLElement` target.
|
|
3
3
|
*
|
|
4
|
-
* @param {HTMLElement
|
|
4
|
+
* @param {HTMLElement} element is the target
|
|
5
5
|
*/
|
|
6
|
-
// @ts-ignore -- `Element`s resulted from querySelector can focus too
|
|
7
6
|
const focus = (element) => element.focus();
|
|
8
7
|
export default focus;
|
|
@@ -6,14 +6,13 @@ import toLowerCase from './toLowerCase';
|
|
|
6
6
|
/**
|
|
7
7
|
* Utility to normalize component options.
|
|
8
8
|
*
|
|
9
|
-
* @param {HTMLElement
|
|
9
|
+
* @param {HTMLElement} element target
|
|
10
10
|
* @param {Record<string, any>} defaultOps component default options
|
|
11
11
|
* @param {Record<string, any>} inputOps component instance options
|
|
12
12
|
* @param {string=} ns component namespace
|
|
13
13
|
* @return {Record<string, any>} normalized component options object
|
|
14
14
|
*/
|
|
15
15
|
export default function normalizeOptions(element, defaultOps, inputOps, ns) {
|
|
16
|
-
// @ts-ignore -- our targets are always `HTMLElement`
|
|
17
16
|
const data = { ...element.dataset };
|
|
18
17
|
/** @type {Record<string, any>} */
|
|
19
18
|
const normalOps = {};
|
|
@@ -34,6 +33,7 @@ export default function normalizeOptions(element, defaultOps, inputOps, ns) {
|
|
|
34
33
|
});
|
|
35
34
|
|
|
36
35
|
ObjectKeys(defaultOps).forEach((k) => {
|
|
36
|
+
/* istanbul ignore else */
|
|
37
37
|
if (k in inputOps) {
|
|
38
38
|
normalOps[k] = inputOps[k];
|
|
39
39
|
} else if (k in dataOps) {
|
|
@@ -11,22 +11,24 @@
|
|
|
11
11
|
* @return {niceValue} the normalized value
|
|
12
12
|
*/
|
|
13
13
|
export default function normalizeValue(value) {
|
|
14
|
-
if (
|
|
14
|
+
if (['true', true].includes(value)) { // boolean
|
|
15
|
+
// if ('true' === value) { // boolean
|
|
15
16
|
return true;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
if (
|
|
19
|
+
if (['false', false].includes(value)) { // boolean
|
|
20
|
+
// if ('false' === value) { // boolean
|
|
19
21
|
return false;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
if (!Number.isNaN(+value)) { // number
|
|
23
|
-
return +value;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
24
|
if (value === '' || value === 'null') { // null
|
|
27
25
|
return null;
|
|
28
26
|
}
|
|
29
27
|
|
|
28
|
+
if (value !== '' && !Number.isNaN(+value)) { // number
|
|
29
|
+
return +value;
|
|
30
|
+
}
|
|
31
|
+
|
|
30
32
|
// string / function / HTMLElement / object
|
|
31
33
|
return value;
|
|
32
34
|
}
|
|
@@ -4,6 +4,7 @@ import supportPassive from '../boolean/supportPassive';
|
|
|
4
4
|
* A global namespace for most scroll event listeners in legacy browsers.
|
|
5
5
|
* @type {Partial<AddEventListenerOptions> | boolean}
|
|
6
6
|
*/
|
|
7
|
-
const passiveHandler = supportPassive ? { passive: true }
|
|
7
|
+
const passiveHandler = supportPassive ? { passive: true }
|
|
8
|
+
: /* istanbul ignore next */false;
|
|
8
9
|
|
|
9
10
|
export default passiveHandler;
|
package/src/misc/reflow.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Utility to force re-paint of an `HTMLElement` target.
|
|
3
3
|
*
|
|
4
|
-
* @param {HTMLElement
|
|
4
|
+
* @param {HTMLElement} element is the target
|
|
5
5
|
* @return {number} the `Element.offsetHeight` value
|
|
6
6
|
*/
|
|
7
|
-
// @ts-ignore
|
|
8
7
|
const reflow = (element) => element.offsetHeight;
|
|
9
8
|
export default reflow;
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import ObjectAssign from './ObjectAssign';
|
|
2
|
+
import ObjectEntries from './ObjectEntries';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Shortcut for multiple uses of `HTMLElement.style.propertyName` method.
|
|
5
|
-
* @param {HTMLElement
|
|
6
|
+
* @param {HTMLElement} element target element
|
|
6
7
|
* @param {Partial<CSSStyleDeclaration>} styles attribute value
|
|
7
8
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
const setElementStyle = (element, styles) => {
|
|
10
|
+
ObjectEntries(styles).forEach(([key, value]) => {
|
|
11
|
+
if (key.includes('--')) {
|
|
12
|
+
element.style.setProperty(key, value);
|
|
13
|
+
} else {
|
|
14
|
+
const propObject = {}; propObject[key] = value;
|
|
15
|
+
ObjectAssign(element.style, propObject);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
};
|
|
10
19
|
export default setElementStyle;
|
package/src/misc/timer.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import isHTMLElement from '../is/isHTMLElement';
|
|
2
2
|
|
|
3
|
-
/** @type {Map<HTMLElement
|
|
3
|
+
/** @type {Map<HTMLElement, any>} */
|
|
4
4
|
const TimeCache = new Map();
|
|
5
5
|
/**
|
|
6
6
|
* An interface for one or more `TimerHandler`s per `Element`.
|
|
@@ -9,17 +9,17 @@ const TimeCache = new Map();
|
|
|
9
9
|
const Timer = {
|
|
10
10
|
/**
|
|
11
11
|
* Sets a new timeout timer for an element, or element -> key association.
|
|
12
|
-
* @param {HTMLElement
|
|
12
|
+
* @param {HTMLElement} element target element
|
|
13
13
|
* @param {ReturnType<TimerHandler>} callback the callback
|
|
14
14
|
* @param {number} delay the execution delay
|
|
15
15
|
* @param {string=} key a unique key
|
|
16
16
|
*/
|
|
17
|
-
set: (
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (!element) return;
|
|
17
|
+
set: (element, callback, delay, key) => {
|
|
18
|
+
if (!isHTMLElement(element)) return;
|
|
21
19
|
|
|
20
|
+
/* istanbul ignore else */
|
|
22
21
|
if (key && key.length) {
|
|
22
|
+
/* istanbul ignore else */
|
|
23
23
|
if (!TimeCache.has(element)) {
|
|
24
24
|
TimeCache.set(element, new Map());
|
|
25
25
|
}
|
|
@@ -32,38 +32,35 @@ const Timer = {
|
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* Returns the timer associated with the target.
|
|
35
|
-
* @param {HTMLElement
|
|
35
|
+
* @param {HTMLElement} element target element
|
|
36
36
|
* @param {string=} key a unique
|
|
37
37
|
* @returns {number?} the timer
|
|
38
38
|
*/
|
|
39
|
-
get: (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (!element) return null;
|
|
39
|
+
get: (element, key) => {
|
|
40
|
+
if (!isHTMLElement(element)) return null;
|
|
43
41
|
const keyTimers = TimeCache.get(element);
|
|
44
42
|
|
|
45
43
|
if (key && key.length && keyTimers && keyTimers.get) {
|
|
46
|
-
return keyTimers.get(key) || null;
|
|
44
|
+
return keyTimers.get(key) || /* istanbul ignore next */null;
|
|
47
45
|
}
|
|
48
46
|
return keyTimers || null;
|
|
49
47
|
},
|
|
50
48
|
|
|
51
49
|
/**
|
|
52
50
|
* Clears the element's timer.
|
|
53
|
-
* @param {HTMLElement
|
|
51
|
+
* @param {HTMLElement} element target element
|
|
54
52
|
* @param {string=} key a unique key
|
|
55
53
|
*/
|
|
56
|
-
clear: (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (!element) return;
|
|
54
|
+
clear: (element, key) => {
|
|
55
|
+
if (!isHTMLElement(element)) return;
|
|
60
56
|
|
|
61
57
|
if (key && key.length) {
|
|
62
58
|
const keyTimers = TimeCache.get(element);
|
|
63
|
-
|
|
59
|
+
/* istanbul ignore else */
|
|
64
60
|
if (keyTimers && keyTimers.get) {
|
|
65
61
|
clearTimeout(keyTimers.get(key));
|
|
66
62
|
keyTimers.delete(key);
|
|
63
|
+
/* istanbul ignore else */
|
|
67
64
|
if (keyTimers.size === 0) {
|
|
68
65
|
TimeCache.delete(element);
|
|
69
66
|
}
|
package/src/misc/version.js
CHANGED
package/src/selectors/closest.js
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @see https://stackoverflow.com/q/54520554/803358
|
|
7
7
|
*
|
|
8
|
-
* @param {HTMLElement
|
|
8
|
+
* @param {HTMLElement} element Element to look into
|
|
9
9
|
* @param {string} selector the selector name
|
|
10
|
-
* @return {
|
|
10
|
+
* @return {HTMLElement?} the query result
|
|
11
11
|
*/
|
|
12
12
|
export default function closest(element, selector) {
|
|
13
13
|
return element ? (element.closest(selector)
|
|
14
|
-
//
|
|
14
|
+
// break out of `ShadowRoot`
|
|
15
15
|
|| closest(element.getRootNode().host, selector)) : null;
|
|
16
16
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import documentAll from './documentAll';
|
|
2
1
|
import isCustomElement from '../is/isCustomElement';
|
|
3
2
|
import getElementsByTagName from './getElementsByTagName';
|
|
4
3
|
|
|
@@ -7,11 +6,11 @@ import getElementsByTagName from './getElementsByTagName';
|
|
|
7
6
|
* `CustomElement`.
|
|
8
7
|
* @see https://stackoverflow.com/questions/27334365/how-to-get-list-of-registered-custom-elements
|
|
9
8
|
*
|
|
10
|
-
* @param {
|
|
11
|
-
* @returns {Array<HTMLElement
|
|
9
|
+
* @param {ParentNode=} parent parent to look into
|
|
10
|
+
* @returns {Array<HTMLElement>} the query result
|
|
12
11
|
*/
|
|
13
12
|
export default function getCustomElements(parent) {
|
|
14
|
-
const collection =
|
|
15
|
-
|
|
13
|
+
const collection = getElementsByTagName('*', parent);
|
|
14
|
+
|
|
16
15
|
return [...collection].filter(isCustomElement);
|
|
17
16
|
}
|
|
@@ -2,10 +2,13 @@ import getDocument from '../get/getDocument';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns an `Element` that matches the id in the document.
|
|
5
|
+
* Within multiple <iframe> elements, a `parent` parameter
|
|
6
|
+
* would decisively locate the correct element.
|
|
5
7
|
*
|
|
6
8
|
* @param {string} id
|
|
7
|
-
* @
|
|
9
|
+
* @param {ParentNode=} context
|
|
10
|
+
* @returns {HTMLElement?}
|
|
8
11
|
*/
|
|
9
|
-
export default function getElementById(id) {
|
|
10
|
-
return getDocument().getElementById(id);
|
|
12
|
+
export default function getElementById(id, context) {
|
|
13
|
+
return getDocument(context).getElementById(id);
|
|
11
14
|
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import getDocument from '../get/getDocument';
|
|
2
|
+
import isNode from '../is/isNode';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Shortcut for `HTMLElement.getElementsByClassName` method. Some `Node` elements
|
|
5
6
|
* like `ShadowRoot` do not support `getElementsByClassName`.
|
|
6
7
|
*
|
|
7
8
|
* @param {string} selector the class name
|
|
8
|
-
* @param {
|
|
9
|
-
* @return {HTMLCollectionOf<HTMLElement
|
|
9
|
+
* @param {ParentNode=} parent optional Element to look into
|
|
10
|
+
* @return {HTMLCollectionOf<HTMLElement>} the 'HTMLCollection'
|
|
10
11
|
*/
|
|
11
12
|
export default function getElementsByClassName(selector, parent) {
|
|
12
|
-
const lookUp =
|
|
13
|
+
const lookUp = isNode(parent) ? parent : getDocument();
|
|
13
14
|
return lookUp.getElementsByClassName(selector);
|
|
14
15
|
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import getDocument from '../get/getDocument';
|
|
2
|
+
import isNode from '../is/isNode';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Shortcut for `HTMLElement.getElementsByTagName` method. Some `Node` elements
|
|
5
6
|
* like `ShadowRoot` do not support `getElementsByTagName`.
|
|
6
7
|
*
|
|
7
8
|
* @param {string} selector the tag name
|
|
8
|
-
* @param {
|
|
9
|
-
* @return {HTMLCollectionOf<HTMLElement
|
|
9
|
+
* @param {ParentNode=} parent optional Element to look into
|
|
10
|
+
* @return {HTMLCollectionOf<HTMLElement>} the 'HTMLCollection'
|
|
10
11
|
*/
|
|
11
12
|
export default function getElementsByTagName(selector, parent) {
|
|
12
|
-
const lookUp =
|
|
13
|
+
const lookUp = isNode(parent) ? parent : getDocument();
|
|
13
14
|
return lookUp.getElementsByTagName(selector);
|
|
14
15
|
}
|
package/src/selectors/matches.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
1
|
const ElementProto = Element.prototype;
|
|
2
|
+
|
|
3
3
|
const matchesFn = ElementProto.matches
|
|
4
|
-
|| ElementProto.matchesSelector
|
|
5
|
-
|| ElementProto.webkitMatchesSelector
|
|
6
|
-
|| ElementProto.mozMatchesSelector
|
|
7
|
-
|| ElementProto.msMatchesSelector
|
|
8
|
-
|| ElementProto.oMatchesSelector
|
|
9
|
-
|| function matchesNotSupported() {
|
|
4
|
+
|| /* istanbul ignore next */ ElementProto.matchesSelector
|
|
5
|
+
|| /* istanbul ignore next */ ElementProto.webkitMatchesSelector
|
|
6
|
+
|| /* istanbul ignore next */ ElementProto.mozMatchesSelector
|
|
7
|
+
|| /* istanbul ignore next */ ElementProto.msMatchesSelector
|
|
8
|
+
|| /* istanbul ignore next */ ElementProto.oMatchesSelector
|
|
9
|
+
|| /* istanbul ignore next */ function matchesNotSupported() {
|
|
10
10
|
return false;
|
|
11
11
|
};
|
|
12
12
|
|
|
@@ -14,7 +14,7 @@ const matchesFn = ElementProto.matches
|
|
|
14
14
|
* Check if element matches a CSS selector,
|
|
15
15
|
* supporting a range of legacy browsers.
|
|
16
16
|
*
|
|
17
|
-
* @param {HTMLElement
|
|
17
|
+
* @param {HTMLElement} target
|
|
18
18
|
* @param {string} selector
|
|
19
19
|
* @returns {boolean}
|
|
20
20
|
*/
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import getDocument from '../get/getDocument';
|
|
2
|
+
import isNode from '../is/isNode';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Utility to check if target is typeof `HTMLElement`, `Element`, `Node`
|
|
5
6
|
* or find one that matches a selector.
|
|
6
7
|
*
|
|
7
|
-
* @param {Node |
|
|
8
|
-
* @param {
|
|
9
|
-
* @return {
|
|
8
|
+
* @param {Node | string} selector the input selector or target element
|
|
9
|
+
* @param {ParentNode=} parent optional node to look into
|
|
10
|
+
* @return {HTMLElement?} the `HTMLElement` or `querySelector` result
|
|
10
11
|
*/
|
|
11
12
|
export default function querySelector(selector, parent) {
|
|
12
|
-
if (
|
|
13
|
-
|
|
14
|
-
return lookUp.querySelector(selector);
|
|
13
|
+
if (isNode(selector)) {
|
|
14
|
+
return selector;
|
|
15
15
|
}
|
|
16
|
-
|
|
16
|
+
const lookUp = isNode(parent) ? parent : getDocument();
|
|
17
|
+
|
|
18
|
+
return lookUp.querySelector(selector);
|
|
17
19
|
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import getDocument from '../get/getDocument';
|
|
2
|
+
import isNode from '../is/isNode';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* A shortcut for `(document|Element).querySelectorAll`.
|
|
5
6
|
*
|
|
6
7
|
* @param {string} selector the input selector
|
|
7
|
-
* @param {
|
|
8
|
-
* @return {NodeListOf<HTMLElement
|
|
8
|
+
* @param {ParentNode=} parent optional node to look into
|
|
9
|
+
* @return {NodeListOf<HTMLElement>} the query result
|
|
9
10
|
*/
|
|
10
11
|
export default function querySelectorAll(selector, parent) {
|
|
11
|
-
const lookUp =
|
|
12
|
+
const lookUp = isNode(parent) ? parent : getDocument();
|
|
12
13
|
return lookUp.querySelectorAll(selector);
|
|
13
14
|
}
|
|
@@ -4,5 +4,6 @@ import documentHead from '../blocks/documentHead';
|
|
|
4
4
|
* A global namespace for 'animationDelay' string.
|
|
5
5
|
* @type {string}
|
|
6
6
|
*/
|
|
7
|
-
const animationDelay = 'webkitAnimation' in documentHead.style ? 'webkitAnimationDelay'
|
|
7
|
+
const animationDelay = 'webkitAnimation' in documentHead.style ? 'webkitAnimationDelay'
|
|
8
|
+
: /* istanbul ignore next */'animationDelay';
|
|
8
9
|
export default animationDelay;
|
|
@@ -4,5 +4,6 @@ import documentHead from '../blocks/documentHead';
|
|
|
4
4
|
* A global namespace for 'animationDuration' string.
|
|
5
5
|
* @type {string}
|
|
6
6
|
*/
|
|
7
|
-
const animationDuration = 'webkitAnimation' in documentHead.style ? 'webkitAnimationDuration'
|
|
7
|
+
const animationDuration = 'webkitAnimation' in documentHead.style ? 'webkitAnimationDuration'
|
|
8
|
+
: /* istanbul ignore next */'animationDuration';
|
|
8
9
|
export default animationDuration;
|