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

132 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-08-21 09:11:37 +02:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.2.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
* --------------------------------------------------------------------------
*/
import { isDisabled, isVisible, parseSelector } from '../util/index.js'
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) {
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 = []
let ancestor = element.parentNode.closest(selector)
2017-09-25 09:09:01 +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-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 []
},
// TODO: this is now unused; remove later along with prev()
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"]'
2021-12-09 21:52:39 +02:00
].map(selector => `${selector}:not([tabindex^="-"])`).join(',')
return this.find(focusables, element).filter(el => !isDisabled(el) && isVisible(el))
},
getSelector(element) {
let selector = element.getAttribute('data-bs-target')
if (!selector || selector === '#') {
let hrefAttribute = element.getAttribute('href')
// The only valid content that could double as a selector are IDs or classes,
// so everything starting with `#` or `.`. If a "real" URL is used as the selector,
// `document.querySelector` will rightfully complain it is invalid.
// See https://github.com/twbs/bootstrap/issues/32273
if (!hrefAttribute || (!hrefAttribute.includes('#') && !hrefAttribute.startsWith('.'))) {
return null
}
// Just in case some CMS puts out a full URL with the anchor appended
if (hrefAttribute.includes('#') && !hrefAttribute.startsWith('#')) {
hrefAttribute = `#${hrefAttribute.split('#')[1]}`
}
selector = hrefAttribute && hrefAttribute !== '#' ? hrefAttribute.trim() : null
selector = parseSelector(selector)
}
return selector
},
getSelectorFromElement(element) {
const selector = SelectorEngine.getSelector(element)
if (selector) {
return SelectorEngine.findOne(selector) ? selector : null
}
return null
},
getElementFromSelector(element) {
const selector = SelectorEngine.getSelector(element)
return selector ? SelectorEngine.findOne(selector) : null
},
getMultipleElementsFromSelector(element) {
const selector = SelectorEngine.getSelector(element)
return selector ? SelectorEngine.find(selector) : []
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