aboutsummaryrefslogtreecommitdiff
path: root/js/src/util/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/util/index.js')
-rw-r--r--js/src/util/index.js38
1 files changed, 28 insertions, 10 deletions
diff --git a/js/src/util/index.js b/js/src/util/index.js
index a4ad9c941..6f5838992 100644
--- a/js/src/util/index.js
+++ b/js/src/util/index.js
@@ -27,7 +27,7 @@ const toType = obj => {
const getUID = prefix => {
do {
prefix += Math.floor(Math.random() * MAX_UID)
- } while (document.getElementById(prefix))
+ } while (getDocument().getElementById(prefix))
return prefix
}
@@ -61,7 +61,7 @@ const getSelectorFromElement = element => {
const selector = getSelector(element)
if (selector) {
- return document.querySelector(selector) ? selector : null
+ return getDocument().querySelector(selector) ? selector : null
}
return null
@@ -70,7 +70,7 @@ const getSelectorFromElement = element => {
const getElementFromSelector = element => {
const selector = getSelector(element)
- return selector ? document.querySelector(selector) : null
+ return selector ? getDocument().querySelector(selector) : null
}
const getTransitionDurationFromElement = element => {
@@ -79,7 +79,7 @@ const getTransitionDurationFromElement = element => {
}
// Get transition-duration of the element
- let { transitionDuration, transitionDelay } = window.getComputedStyle(element)
+ let { transitionDuration, transitionDelay } = getWindow().getComputedStyle(element)
const floatTransitionDuration = Number.parseFloat(transitionDuration)
const floatTransitionDelay = Number.parseFloat(transitionDelay)
@@ -163,7 +163,7 @@ const isDisabled = element => {
}
const findShadowRoot = element => {
- if (!document.documentElement.attachShadow) {
+ if (!getDocument().documentElement.attachShadow) {
return null
}
@@ -203,7 +203,7 @@ const reflow = element => {
const getjQuery = () => {
const { jQuery } = window
- if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
+ if (jQuery && !getDocument().body.hasAttribute('data-bs-no-jquery')) {
return jQuery
}
@@ -213,10 +213,10 @@ const getjQuery = () => {
const DOMContentLoadedCallbacks = []
const onDOMContentLoaded = callback => {
- if (document.readyState === 'loading') {
+ if (getDocument().readyState === 'loading') {
// add listener on the first call when the document is in loading state
if (!DOMContentLoadedCallbacks.length) {
- document.addEventListener('DOMContentLoaded', () => {
+ getDocument().addEventListener('DOMContentLoaded', () => {
DOMContentLoadedCallbacks.forEach(callback => callback())
})
}
@@ -227,7 +227,7 @@ const onDOMContentLoaded = callback => {
}
}
-const isRTL = () => document.documentElement.dir === 'rtl'
+const isRTL = () => getDocument().documentElement.dir === 'rtl'
const defineJQueryPlugin = plugin => {
onDOMContentLoaded(() => {
@@ -309,6 +309,22 @@ const getNextActiveElement = (list, activeElement, shouldGetNext, isCycleAllowed
return list[Math.max(0, Math.min(index, listLength - 1))]
}
+const getWindow = () => {
+ if (typeof window !== 'undefined') {
+ return window
+ }
+
+ return {}
+}
+
+const getDocument = () => {
+ if (typeof document !== 'undefined') {
+ return document
+ }
+
+ return {}
+}
+
export {
getElement,
getUID,
@@ -329,5 +345,7 @@ export {
isRTL,
defineJQueryPlugin,
execute,
- executeAfterTransition
+ executeAfterTransition,
+ getWindow,
+ getDocument
}