diff options
author | GeoSot <geo.sotis@gmail.com> | 2021-05-13 18:17:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-13 18:17:20 +0300 |
commit | 6e1c9096f08ed21063fd6ba16aa998a3ac4149f9 (patch) | |
tree | f6d0ecf0ebe37529a6b5eb3c262e1d423093b6cc /js/src/util | |
parent | e376142d85e3ce0a5f5e49f3d676e419287a7292 (diff) |
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
Diffstat (limited to 'js/src/util')
-rw-r--r-- | js/src/util/index.js | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/js/src/util/index.js b/js/src/util/index.js index 0dd6b1d454..5ee211c73b 100644 --- a/js/src/util/index.js +++ b/js/src/util/index.js @@ -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, |