diff options
| author | Pierre Souchay <[email protected]> | 2022-11-07 13:43:06 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-11-07 14:43:06 +0200 |
| commit | ef4e2daa48193463b36fdc297d79c6a002e4ee67 (patch) | |
| tree | db68bf3bc32bfdfce97f1c3824c29c86e24b8f27 /js/src/util/index.js | |
| parent | e81e7cda90026cdb2a05fcdadd2d66f48f0bbdc4 (diff) | |
| download | bootstrap-ef4e2daa48193463b36fdc297d79c6a002e4ee67.tar.xz bootstrap-ef4e2daa48193463b36fdc297d79c6a002e4ee67.zip | |
Properly escape IDs in getSelector() to handle weird IDs (#35565) (#35566)
Diffstat (limited to 'js/src/util/index.js')
| -rw-r--r-- | js/src/util/index.js | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/js/src/util/index.js b/js/src/util/index.js index b92eddba2..8c6922173 100644 --- a/js/src/util/index.js +++ b/js/src/util/index.js @@ -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 |
