1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-09-29 23:09:05 +02:00

Move get element functionality to a helper (#33327)

Looking around on js components I found out many checks, different expressed but with same purpose.
Some of them are trying to parse string to element, others, jQuery element to js simple nodeElement etc

With this Pr, I am trying to give a standard way to parse an element

So this pr:

* Creates `getElement` helper that tries to parse an argument to element or null
* Changes `isElement` to make  explicit checks and return Boolean 
* fixes tests deficiencies
This commit is contained in:
GeoSot
2021-05-13 18:17:20 +03:00
committed by GitHub
parent e376142d85
commit 6e1c9096f0
8 changed files with 85 additions and 49 deletions

View File

@@ -1,3 +1,5 @@
import SelectorEngine from '../dom/selector-engine'
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.0): util/index.js
@@ -100,7 +102,29 @@ const triggerTransitionEnd = element => {
element.dispatchEvent(new Event(TRANSITION_END))
}
const isElement = obj => (obj[0] || obj).nodeType
const isElement = obj => {
if (!obj || typeof obj !== 'object') {
return false
}
if (typeof obj.jquery !== 'undefined') {
obj = obj[0]
}
return typeof obj.nodeType !== 'undefined'
}
const getElement = obj => {
if (isElement(obj)) { // it's a jQuery object or a node element
return obj.jquery ? obj[0] : obj
}
if (typeof obj === 'string' && obj.length > 0) {
return SelectorEngine.findOne(obj)
}
return null
}
const emulateTransitionEnd = (element, duration) => {
let called = false
@@ -238,6 +262,7 @@ const execute = callback => {
}
export {
getElement,
getUID,
getSelectorFromElement,
getElementFromSelector,