[go: nahoru, domu]

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

Properly escape IDs in getSelector() to handle weird IDs #35566

Merged
Next Next commit
style: pass linter
  • Loading branch information
Pierre Souchay committed Nov 7, 2022
commit 5181d606ff1210df6dd061384b79dc81311170e9
49 changes: 49 additions & 0 deletions js/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,55 @@ const getUID = prefix => {
return prefix
}

<<<<<<< HEAD
=======
const 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
}

if (selector !== null && selector.startsWith('#')) {
// document.querySelector needs escaping to handle IDs (html5+) containing for instance /
selector = '#' + CSS.escape(selector.slice(1))
}

return selector
}

const getSelectorFromElement = element => {
const selector = getSelector(element)

if (selector) {
return document.querySelector(selector) ? selector : null
}

return null
}

const getElementFromSelector = element => {
const selector = getSelector(element)

return selector ? document.querySelector(selector) : null
}

>>>>>>> 534297292 (style: pass linter)
const getTransitionDurationFromElement = element => {
if (!element) {
return 0
Expand Down