1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-10 07:37:27 +02:00

Escape ID in Util.getSelectorFromElement (#24700)

This commit is contained in:
Johann-S
2017-11-07 12:41:06 +01:00
committed by GitHub
parent 26dc17bcd2
commit 79d6b574cc
2 changed files with 27 additions and 1 deletions

View File

@@ -87,6 +87,14 @@ const Util = (($) => {
}
}
function escapeId(selector) {
// we escape IDs in case of special selectors (selector = '#myId:something')
// $.escapeSelector does not exist in jQuery < 3
selector = typeof $.escapeSelector === 'function' ? $.escapeSelector(selector).substr(1) :
selector.replace(/(:|\.|\[|\]|,|=|@)/g, '\\$1')
return selector
}
/**
* --------------------------------------------------------------------------
@@ -112,6 +120,11 @@ const Util = (($) => {
selector = element.getAttribute('href') || ''
}
// if it's an ID
if (selector.charAt(0) === '#') {
selector = escapeId(selector)
}
try {
const $selector = $(document).find(selector)
return $selector.length > 0 ? selector : null