1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-06 13:46:42 +02:00

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

This commit is contained in:
Pierre Souchay
2022-11-07 13:43:06 +01:00
committed by GitHub
parent e81e7cda90
commit ef4e2daa48
5 changed files with 59 additions and 6 deletions

View File

@@ -5,7 +5,7 @@
* --------------------------------------------------------------------------
*/
import { isDisabled, isVisible } from '../util/index.js'
import { isDisabled, isVisible, parseSelector } from '../util/index.js'
/**
* Constants
@@ -99,6 +99,7 @@ const SelectorEngine = {
}
selector = hrefAttribute && hrefAttribute !== '#' ? hrefAttribute.trim() : null
selector = parseSelector(selector)
}
return selector

View File

@@ -9,6 +9,20 @@ const MAX_UID = 1_000_000
const MILLISECONDS_MULTIPLIER = 1000
const TRANSITION_END = 'transitionend'
/**
* Properly escape IDs selectors to handle weird IDs
* @param {string} selector
* @returns {string}
*/
const parseSelector = selector => {
if (selector && window.CSS && window.CSS.escape) {
// document.querySelector needs escaping to handle IDs (html5+) containing for instance /
selector = selector.replaceAll(/#([^\s"#']+)/g, (match, id) => '#' + CSS.escape(id))
}
return selector
}
// Shout-out Angus Croll (https://goo.gl/pxwQGp)
const toType = object => {
if (object === null || object === undefined) {
@@ -76,7 +90,7 @@ const getElement = object => {
}
if (typeof object === 'string' && object.length > 0) {
return document.querySelector(object)
return document.querySelector(parseSelector(object))
}
return null
@@ -285,6 +299,7 @@ export {
isVisible,
noop,
onDOMContentLoaded,
parseSelector,
reflow,
triggerTransitionEnd,
toType