selectorEngine.js 3.49 KB
Newer Older
1
2
import Util from '../util'

Johann-S's avatar
Johann-S committed
3
4
5
6
7
8
9
/**
 * --------------------------------------------------------------------------
 * Bootstrap (v4.0.0-beta): dom/selectorEngine.js
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

Johann-S's avatar
Johann-S committed
10
11
12
13
14
15
const SelectorEngine = (() => {
  /**
   * ------------------------------------------------------------------------
   * Polyfills
   * ------------------------------------------------------------------------
   */
16

Johann-S's avatar
Johann-S committed
17
18
19
20
21
22
23
24
  // 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
25
26
  }

Johann-S's avatar
Johann-S committed
27
28
29
30
31
32
33
34
35
36
37
  // closest polyfill (see: https://mzl.la/2vXggaI)
  let fnClosest = null
  if (!Element.prototype.closest) {
    fnClosest = (element, selector) => {
      let ancestor = element
      do {
        if (fnMatches.call(ancestor, selector)) {
          return ancestor
        }

        ancestor = ancestor.parentElement
38
      } while (ancestor !== null && ancestor.nodeType === Node.ELEMENT_NODE)
Johann-S's avatar
Johann-S committed
39
40
41

      return null
    }
Johann-S's avatar
Johann-S committed
42
43
44
45
  } else {
    // eslint-disable-next-line arrow-body-style
    fnClosest = (element, selector) => {
      return element.closest(selector)
Johann-S's avatar
Johann-S committed
46
    }
Johann-S's avatar
Johann-S committed
47
  }
Johann-S's avatar
Johann-S committed
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  const scopeSelectorRegex = /:scope\b/
  const supportScopeQuery = (() => {
    const element = document.createElement('div')
    try {
      element.querySelectorAll(':scope *')
    } catch (e) {
      return false
    }

    return true
  })()

  let findFn = null
  let findOneFn = null
  if (supportScopeQuery) {
    findFn = Element.prototype.querySelectorAll
    findOneFn = Element.prototype.querySelector
  } else {
    findFn = function (selector) {
      if (!scopeSelectorRegex.test(selector)) {
        return this.querySelectorAll(selector)
      }

      const hasId = Boolean(this.id)
      if (!hasId) {
        this.id = Util.getUID('scope')
      }

      let nodeList = null
      try {
        selector = selector.replace(scopeSelectorRegex, `#${this.id}`)
        nodeList = this.querySelectorAll(selector)
      } finally {
        if (!hasId) {
          this.removeAttribute('id')
        }
      }

      return nodeList
    }

    findOneFn = function (selector) {
      if (!scopeSelectorRegex.test(selector)) {
        return this.querySelector(selector)
      }

      const matches = findFn.call(this, selector)
      if (typeof matches[0] !== 'undefined') {
        return matches[0]
      }

      return null
    }
  }

Johann-S's avatar
Johann-S committed
104
105
106
107
  return {
    matches(element, selector) {
      return fnMatches.call(element, selector)
    },
Johann-S's avatar
Johann-S committed
108

109
    find(selector, element = document.documentElement) {
Johann-S's avatar
Johann-S committed
110
111
112
      if (typeof selector !== 'string') {
        return null
      }
Johann-S's avatar
Johann-S committed
113

Johann-S's avatar
Johann-S committed
114
115
116
117
      if (selector.indexOf('#') === 0) {
        return SelectorEngine.findOne(selector, element)
      }

118
      return findFn.call(element, selector)
Johann-S's avatar
Johann-S committed
119
    },
Johann-S's avatar
Johann-S committed
120

121
    findOne(selector, element = document.documentElement) {
Johann-S's avatar
Johann-S committed
122
123
124
125
      if (typeof selector !== 'string') {
        return null
      }

126
127
128
129
130
131
      return findOneFn.call(element, selector)
    },

    children(element, selector) {
      if (typeof selector !== 'string') {
        return null
Johann-S's avatar
Johann-S committed
132
      }
Johann-S's avatar
Johann-S committed
133

134
135
      const children = Util.makeArray(element.children)
      return children.filter((child) => this.matches(child, selector))
Johann-S's avatar
Johann-S committed
136
137
138
139
140
    },

    closest(element, selector) {
      return fnClosest(element, selector)
    }
Johann-S's avatar
Johann-S committed
141
  }
Johann-S's avatar
Johann-S committed
142
})()
Johann-S's avatar
Johann-S committed
143
144

export default SelectorEngine