2017-08-21 09:11:37 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2020-05-13 22:36:00 +03:00
|
|
|
* Bootstrap (v5.0.0-alpha1): dom/selector-engine.js
|
2020-06-16 21:41:47 +03:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2017-08-21 09:11:37 +02:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2020-03-09 14:34:07 +01:00
|
|
|
import { find as findFn, findOne } from './polyfill'
|
2019-02-13 19:37:52 +02:00
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2017-08-26 12:44:26 +02:00
|
|
|
|
2019-02-13 19:37:52 +02:00
|
|
|
const NODE_TEXT = 3
|
2017-08-26 12:44:26 +02:00
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
const SelectorEngine = {
|
|
|
|
matches(element, selector) {
|
2020-03-09 14:34:07 +01:00
|
|
|
return element.matches(selector)
|
2018-09-14 14:27:30 +02:00
|
|
|
},
|
2017-09-20 14:19:10 +02:00
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
find(selector, element = document.documentElement) {
|
2020-03-25 15:35:02 +01:00
|
|
|
return [].concat(...findFn.call(element, selector))
|
2018-09-14 14:27:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
findOne(selector, element = document.documentElement) {
|
|
|
|
return findOne.call(element, selector)
|
|
|
|
},
|
2017-09-20 14:19:10 +02:00
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
children(element, selector) {
|
2020-03-25 15:35:02 +01:00
|
|
|
const children = [].concat(...element.children)
|
2019-02-13 19:37:52 +02:00
|
|
|
|
2020-03-25 15:35:02 +01:00
|
|
|
return children.filter(child => child.matches(selector))
|
2018-09-14 14:27:30 +02:00
|
|
|
},
|
2017-09-25 09:09:01 +02:00
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
parents(element, selector) {
|
|
|
|
const parents = []
|
2019-03-24 18:30:30 +01:00
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
let ancestor = element.parentNode
|
2019-02-13 19:37:52 +02:00
|
|
|
|
|
|
|
while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== NODE_TEXT) {
|
2018-09-14 14:27:30 +02:00
|
|
|
if (this.matches(ancestor, selector)) {
|
|
|
|
parents.push(ancestor)
|
2017-09-25 09:09:01 +02:00
|
|
|
}
|
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
ancestor = ancestor.parentNode
|
|
|
|
}
|
2017-09-25 09:09:01 +02:00
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
return parents
|
|
|
|
},
|
2018-06-16 22:20:27 +02:00
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
prev(element, selector) {
|
2020-03-09 16:21:04 +01:00
|
|
|
let previous = element.previousElementSibling
|
2019-03-24 18:30:30 +01:00
|
|
|
|
2020-03-09 16:21:04 +01:00
|
|
|
while (previous) {
|
2020-03-25 15:35:02 +01:00
|
|
|
if (previous.matches(selector)) {
|
2020-03-09 16:21:04 +01:00
|
|
|
return [previous]
|
2017-09-25 09:09:01 +02:00
|
|
|
}
|
|
|
|
|
2020-03-09 16:21:04 +01:00
|
|
|
previous = previous.previousElementSibling
|
2017-09-20 14:19:10 +02:00
|
|
|
}
|
2018-09-14 14:27:30 +02:00
|
|
|
|
2020-02-01 14:56:20 +01:00
|
|
|
return []
|
|
|
|
},
|
|
|
|
|
|
|
|
next(element, selector) {
|
|
|
|
let next = element.nextElementSibling
|
|
|
|
|
|
|
|
while (next) {
|
|
|
|
if (this.matches(next, selector)) {
|
|
|
|
return [next]
|
|
|
|
}
|
|
|
|
|
|
|
|
next = next.nextElementSibling
|
|
|
|
}
|
|
|
|
|
2020-03-09 16:21:04 +01:00
|
|
|
return []
|
2017-08-21 09:11:37 +02:00
|
|
|
}
|
2018-09-14 14:27:30 +02:00
|
|
|
}
|
2017-08-21 09:11:37 +02:00
|
|
|
|
|
|
|
export default SelectorEngine
|