Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: refactor getSelector not to be exported #37438

Merged
merged 2 commits into from Nov 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 27 additions & 32 deletions js/src/dom/selector-engine.js
Expand Up @@ -7,9 +7,30 @@

import { isDisabled, isVisible, parseSelector } from '../util/index.js'

/**
* Constants
*/
const getSelector = element => {
GeoSot marked this conversation as resolved.
Show resolved Hide resolved
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 = {
find(selector, element = document.documentElement) {
Expand Down Expand Up @@ -79,34 +100,8 @@ const SelectorEngine = {
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)
const selector = getSelector(element)

if (selector) {
return SelectorEngine.findOne(selector) ? selector : null
Expand All @@ -116,13 +111,13 @@ const SelectorEngine = {
},

getElementFromSelector(element) {
const selector = SelectorEngine.getSelector(element)
const selector = getSelector(element)

return selector ? SelectorEngine.findOne(selector) : null
},

getMultipleElementsFromSelector(element) {
const selector = SelectorEngine.getSelector(element)
const selector = getSelector(element)

return selector ? SelectorEngine.find(selector) : []
}
Expand Down