Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/bootstrap.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorJohann-S <johann.servoire@gmail.com>2017-08-21 17:45:29 +0300
committerXhmikosR <xhmikosr@gmail.com>2019-02-20 23:05:45 +0300
commitd6560bbc81f2e4a4c3c19a7ea9c7269df2594580 (patch)
tree0832e32780bd4c73eb962ed094901f62691831c5 /js
parent0b16c8c6d9a9690d537bd08eac8a8292ebf938cd (diff)
better polyfill for closest and matches functions
Diffstat (limited to 'js')
-rw-r--r--js/src/dom/selectorEngine.js54
1 files changed, 39 insertions, 15 deletions
diff --git a/js/src/dom/selectorEngine.js b/js/src/dom/selectorEngine.js
index f6bcf6da26..1b33bf62d6 100644
--- a/js/src/dom/selectorEngine.js
+++ b/js/src/dom/selectorEngine.js
@@ -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 = {
- matches: Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector,
+ matches(element, selector) {
+ return fnMatches.call(element, selector)
+ },
find(selector) {
if (typeof selector !== 'string') {
@@ -22,20 +59,7 @@ const SelectorEngine = {
},
closest(element, selector) {
- let ancestor = element
- if (!document.documentElement.contains(element)) {
- return null
- }
-
- do {
- if (SelectorEngine.matches.call(ancestor, selector)) {
- return ancestor
- }
-
- ancestor = ancestor.parentElement
- } while (ancestor !== null)
-
- return null
+ return fnClosest(element, selector)
}
}