0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-11-29 11:24:18 +01:00

ref: refactor getSelector not to be exported (#37438)

This commit is contained in:
GeoSot 2022-11-07 14:55:34 +02:00 committed by GitHub
parent ef4e2daa48
commit fcdfee90b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,9 +7,30 @@
import { isDisabled, isVisible, parseSelector } from '../util/index.js' import { isDisabled, isVisible, parseSelector } from '../util/index.js'
/** const getSelector = element => {
* Constants 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
}
return parseSelector(selector)
}
const SelectorEngine = { const SelectorEngine = {
find(selector, element = document.documentElement) { find(selector, element = document.documentElement) {
@ -79,34 +100,8 @@ const SelectorEngine = {
return this.find(focusables, element).filter(el => !isDisabled(el) && isVisible(el)) 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) { getSelectorFromElement(element) {
const selector = SelectorEngine.getSelector(element) const selector = getSelector(element)
if (selector) { if (selector) {
return SelectorEngine.findOne(selector) ? selector : null return SelectorEngine.findOne(selector) ? selector : null
@ -116,13 +111,13 @@ const SelectorEngine = {
}, },
getElementFromSelector(element) { getElementFromSelector(element) {
const selector = SelectorEngine.getSelector(element) const selector = getSelector(element)
return selector ? SelectorEngine.findOne(selector) : null return selector ? SelectorEngine.findOne(selector) : null
}, },
getMultipleElementsFromSelector(element) { getMultipleElementsFromSelector(element) {
const selector = SelectorEngine.getSelector(element) const selector = getSelector(element)
return selector ? SelectorEngine.find(selector) : [] return selector ? SelectorEngine.find(selector) : []
} }