selectorEngine.js 2.3 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)
 * --------------------------------------------------------------------------
 */

11
12
13
14
15
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */
Johann-S's avatar
Johann-S committed
16

17
const closest = Polyfill.closest
18
const matchesFn = Element.prototype.matches
19
20
21
const find = Polyfill.find
const findOne = Polyfill.findOne
const nodeText = 3
Johann-S's avatar
Johann-S committed
22

23
24
25
26
const SelectorEngine = {
  matches(element, selector) {
    return matchesFn.call(element, selector)
  },
Johann-S's avatar
Johann-S committed
27

28
29
30
31
  find(selector, element = document.documentElement) {
    if (typeof selector !== 'string') {
      return null
    }
32

33
34
35
36
37
38
39
    return find.call(element, selector)
  },

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

41
42
    return findOne.call(element, selector)
  },
Johann-S's avatar
Johann-S committed
43

44
45
46
47
48
49
50
51
  children(element, selector) {
    if (typeof selector !== 'string') {
      return null
    }

    const children = Util.makeArray(element.children)
    return children.filter((child) => this.matches(child, selector))
  },
52

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

58
    const parents = []
59

60
61
62
63
    let ancestor = element.parentNode
    while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== nodeText) {
      if (this.matches(ancestor, selector)) {
        parents.push(ancestor)
64
65
      }

66
67
      ancestor = ancestor.parentNode
    }
68

69
70
    return parents
  },
71

72
73
74
75
  closest(element, selector) {
    if (typeof selector !== 'string') {
      return null
    }
76

77
78
    return closest(element, selector)
  },
79

80
81
82
83
  prev(element, selector) {
    if (typeof selector !== 'string') {
      return null
    }
84

85
    const siblings = []
86

87
88
89
90
    let previous = element.previousSibling
    while (previous && previous.nodeType === Node.ELEMENT_NODE && previous.nodeType !== nodeText) {
      if (this.matches(previous, selector)) {
        siblings.push(previous)
91
92
      }

93
      previous = previous.previousSibling
Johann-S's avatar
Johann-S committed
94
    }
95
96

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

export default SelectorEngine