mirror of
https://github.com/twbs/bootstrap.git
synced 2025-02-06 04:08:22 +01:00
better polyfill for closest and matches functions
This commit is contained in:
parent
0b16c8c6d9
commit
d6560bbc81
@ -5,8 +5,45 @@
|
|||||||
* --------------------------------------------------------------------------
|
* --------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// matches polyfill (see: https://mzl.la/2ikXneG)
|
||||||
|
let fnMatches = null
|
||||||
|
if (!Element.prototype.matches) {
|
||||||
|
fnMatches =
|
||||||
|
Element.prototype.msMatchesSelector ||
|
||||||
|
Element.prototype.webkitMatchesSelector
|
||||||
|
} else {
|
||||||
|
fnMatches = Element.prototype.matches
|
||||||
|
}
|
||||||
|
|
||||||
|
// closest polyfill (see: https://mzl.la/2vXggaI)
|
||||||
|
let fnClosest = null
|
||||||
|
if (!Element.prototype.closest) {
|
||||||
|
fnClosest = (element, selector) => {
|
||||||
|
let ancestor = element
|
||||||
|
if (!document.documentElement.contains(element)) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (fnMatches.call(ancestor, selector)) {
|
||||||
|
return ancestor
|
||||||
|
}
|
||||||
|
|
||||||
|
ancestor = ancestor.parentElement
|
||||||
|
} while (ancestor !== null)
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fnClosest = (element, selector) => {
|
||||||
|
return element.closest(selector)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const SelectorEngine = {
|
const SelectorEngine = {
|
||||||
matches: Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector,
|
matches(element, selector) {
|
||||||
|
return fnMatches.call(element, selector)
|
||||||
|
},
|
||||||
|
|
||||||
find(selector) {
|
find(selector) {
|
||||||
if (typeof selector !== 'string') {
|
if (typeof selector !== 'string') {
|
||||||
@ -22,20 +59,7 @@ const SelectorEngine = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
closest(element, selector) {
|
closest(element, selector) {
|
||||||
let ancestor = element
|
return fnClosest(element, selector)
|
||||||
if (!document.documentElement.contains(element)) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
if (SelectorEngine.matches.call(ancestor, selector)) {
|
|
||||||
return ancestor
|
|
||||||
}
|
|
||||||
|
|
||||||
ancestor = ancestor.parentElement
|
|
||||||
} while (ancestor !== null)
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user