selectorEngine.js 4.15 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

114
      return findFn.call(element, selector)
Johann-S's avatar
Johann-S committed
115
    },
Johann-S's avatar
Johann-S committed
116

117
    findOne(selector, element = document.documentElement) {
Johann-S's avatar
Johann-S committed
118
119
120
121
      if (typeof selector !== 'string') {
        return null
      }

122
123
124
125
126
127
      return findOneFn.call(element, selector)
    },

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

130
131
      const children = Util.makeArray(element.children)
      return children.filter((child) => this.matches(child, selector))
Johann-S's avatar
Johann-S committed
132
133
    },

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    parents(element, selector) {
      if (typeof selector !== 'string') {
        return null
      }

      const parents = []

      let ancestor = element.parentNode
      while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE) {
        if (fnMatches.call(ancestor, selector)) {
          parents.push(ancestor)
        }

        ancestor = ancestor.parentNode
      }

      return parents
    },

Johann-S's avatar
Johann-S committed
153
154
    closest(element, selector) {
      return fnClosest(element, selector)
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    },

    prev(element, selector) {
      if (typeof selector !== 'string') {
        return null
      }

      const siblings = []

      let previous = element.previousSibling
      while (previous) {
        if (fnMatches.call(previous, selector)) {
          siblings.push(previous)
        }

        previous = previous.previousSibling
      }

      return siblings
Johann-S's avatar
Johann-S committed
174
    }
Johann-S's avatar
Johann-S committed
175
  }
Johann-S's avatar
Johann-S committed
176
})()
Johann-S's avatar
Johann-S committed
177
178

export default SelectorEngine