selectorEngine.js 2.26 KB
Newer Older
1
import Polyfill from './polyfill'
2
3
import Util from '../util'

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

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

18
19
20
  const closest = Polyfill.closest
  const find = Polyfill.find
  const findOne = Polyfill.findOne
21

Johann-S's avatar
Johann-S committed
22
23
  return {
    matches(element, selector) {
24
      return element.matches(selector)
Johann-S's avatar
Johann-S committed
25
    },
Johann-S's avatar
Johann-S committed
26

27
    find(selector, element = document.documentElement) {
Johann-S's avatar
Johann-S committed
28
29
30
      if (typeof selector !== 'string') {
        return null
      }
Johann-S's avatar
Johann-S committed
31

32
      return find.call(element, selector)
Johann-S's avatar
Johann-S committed
33
    },
Johann-S's avatar
Johann-S committed
34

35
    findOne(selector, element = document.documentElement) {
Johann-S's avatar
Johann-S committed
36
37
38
39
      if (typeof selector !== 'string') {
        return null
      }

40
      return findOne.call(element, selector)
41
42
43
44
45
    },

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

48
49
      const children = Util.makeArray(element.children)
      return children.filter((child) => this.matches(child, selector))
Johann-S's avatar
Johann-S committed
50
51
    },

52
53
54
55
56
57
58
59
60
    parents(element, selector) {
      if (typeof selector !== 'string') {
        return null
      }

      const parents = []

      let ancestor = element.parentNode
      while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE) {
61
        if (ancestor.matches(selector)) {
62
63
64
65
66
67
68
69
70
          parents.push(ancestor)
        }

        ancestor = ancestor.parentNode
      }

      return parents
    },

Johann-S's avatar
Johann-S committed
71
    closest(element, selector) {
72
73
74
75
      if (typeof selector !== 'string') {
        return null
      }

76
      return closest(element, selector)
77
78
79
80
81
82
83
84
85
86
87
    },

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

      const siblings = []

      let previous = element.previousSibling
      while (previous) {
88
        if (previous.matches(selector)) {
89
90
91
92
93
94
95
          siblings.push(previous)
        }

        previous = previous.previousSibling
      }

      return siblings
Johann-S's avatar
Johann-S committed
96
    }
Johann-S's avatar
Johann-S committed
97
  }
Johann-S's avatar
Johann-S committed
98
})()
Johann-S's avatar
Johann-S committed
99
100

export default SelectorEngine