selectorEngine.js 2.22 KB
Newer Older
Johann-S's avatar
Johann-S committed
1
2
/**
 * --------------------------------------------------------------------------
XhmikosR's avatar
4.3.1.    
XhmikosR committed
3
 * Bootstrap (v4.3.1): dom/selectorEngine.js
Johann-S's avatar
Johann-S committed
4
5
6
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

8
import Polyfill from './polyfill'
9
10
11
import {
  makeArray
} from '../util/index'
12

13
14
15
16
17
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */
Johann-S's avatar
Johann-S committed
18

XhmikosR's avatar
XhmikosR committed
19
const { find: findFn, findOne } = Polyfill
20
const NODE_TEXT = 3
Johann-S's avatar
Johann-S committed
21

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

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

32
    return findFn.call(element, selector)
33
34
35
36
37
38
  },

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

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

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

48
    const children = makeArray(element.children)
49

XhmikosR's avatar
XhmikosR committed
50
    return children.filter(child => this.matches(child, selector))
51
  },
52

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

58
59
    const parents = []
    let ancestor = element.parentNode
60
61

    while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== NODE_TEXT) {
62
63
      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
    return element.closest(selector)
78
  },
79

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

85
86
    const siblings = []
    let previous = element.previousSibling
87
88

    while (previous && previous.nodeType === Node.ELEMENT_NODE && previous.nodeType !== NODE_TEXT) {
89
90
      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