0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-11 08:54:23 +01:00
Bootstrap/js/src/dom/selector-engine.js

93 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-08-21 09:11:37 +02:00
/**
* --------------------------------------------------------------------------
2021-10-05 18:50:18 +03:00
* Bootstrap (v5.1.2): 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
* --------------------------------------------------------------------------
*/
2018-09-14 14:27:30 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2017-08-26 12:44:26 +02:00
import { isDisabled, isVisible } from '../util/index'
const NODE_TEXT = 3
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) {
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 = []
2019-03-24 18:30:30 +01:00
2018-09-14 14:27:30 +02:00
let ancestor = element.parentNode
while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== NODE_TEXT) {
if (ancestor.matches(selector)) {
2018-09-14 14:27:30 +02:00
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-09-14 14:27:30 +02:00
prev(element, selector) {
let previous = element.previousElementSibling
2019-03-24 18:30:30 +01:00
while (previous) {
if (previous.matches(selector)) {
return [previous]
2017-09-25 09:09:01 +02:00
}
previous = previous.previousElementSibling
2017-09-20 14:19:10 +02:00
}
2018-09-14 14:27:30 +02:00
return []
},
next(element, selector) {
let next = element.nextElementSibling
while (next) {
if (next.matches(selector)) {
return [next]
}
next = next.nextElementSibling
}
return []
},
focusableChildren(element) {
const focusables = [
'a',
'button',
'input',
'textarea',
'select',
'details',
'[tabindex]',
'[contenteditable="true"]'
].map(selector => `${selector}:not([tabindex^="-"])`).join(', ')
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