2017-08-21 09:11:37 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2022-07-19 18:43:58 +03:00
|
|
|
* Bootstrap (v5.2.0): 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
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2021-10-13 15:19:28 +03:00
|
|
|
import { isDisabled, isVisible } from '../util/index'
|
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2017-08-26 12:44:26 +02:00
|
|
|
|
2018-09-14 14:27:30 +02:00
|
|
|
const SelectorEngine = {
|
|
|
|
find(selector, element = document.documentElement) {
|
2020-10-26 16:39:23 +02:00
|
|
|
return [].concat(...Element.prototype.querySelectorAll.call(element, selector))
|
2018-09-14 14:27:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
findOne(selector, element = document.documentElement) {
|
2020-10-26 16:39:23 +02:00
|
|
|
return Element.prototype.querySelector.call(element, 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
|
|
|
children(element, selector) {
|
2021-10-13 15:19:28 +03:00
|
|
|
return [].concat(...element.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 = []
|
2022-01-30 16:24:03 +02:00
|
|
|
let ancestor = element.parentNode.closest(selector)
|
2017-09-25 09:09:01 +02:00
|
|
|
|
2022-01-30 16:24:03 +02:00
|
|
|
while (ancestor) {
|
|
|
|
parents.push(ancestor)
|
|
|
|
ancestor = ancestor.parentNode.closest(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
|
|
|
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 []
|
|
|
|
},
|
2021-12-13 02:10:26 +02:00
|
|
|
// TODO: this is now unused; remove later along with prev()
|
2020-02-01 14:56:20 +01:00
|
|
|
next(element, selector) {
|
|
|
|
let next = element.nextElementSibling
|
|
|
|
|
|
|
|
while (next) {
|
2020-12-07 19:10:20 +02:00
|
|
|
if (next.matches(selector)) {
|
2020-02-01 14:56:20 +01:00
|
|
|
return [next]
|
|
|
|
}
|
|
|
|
|
|
|
|
next = next.nextElementSibling
|
|
|
|
}
|
|
|
|
|
2020-03-09 16:21:04 +01:00
|
|
|
return []
|
2021-07-27 01:01:04 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
focusableChildren(element) {
|
|
|
|
const focusables = [
|
|
|
|
'a',
|
|
|
|
'button',
|
|
|
|
'input',
|
|
|
|
'textarea',
|
|
|
|
'select',
|
|
|
|
'details',
|
|
|
|
'[tabindex]',
|
|
|
|
'[contenteditable="true"]'
|
2021-12-09 21:52:39 +02:00
|
|
|
].map(selector => `${selector}:not([tabindex^="-"])`).join(',')
|
2021-07-27 01:01:04 -04:00
|
|
|
|
|
|
|
return this.find(focusables, element).filter(el => !isDisabled(el) && isVisible(el))
|
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
|