mirror of
https://github.com/twbs/bootstrap.git
synced 2025-02-06 04:08:22 +01:00
Remove escaping selector and add a warning to inform folks to escape their selectors (#25390)
This commit is contained in:
parent
db70164d13
commit
5a6be71791
@ -32,6 +32,11 @@ Alternatively, to target a specific plugin, just include the plugin's name as a
|
|||||||
$(document).off('.alert.data-api')
|
$(document).off('.alert.data-api')
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
|
{% callout warning %}
|
||||||
|
##### Escaping selectors
|
||||||
|
If you use special selectors, for example: `collapse:Example`, be sure to escape them, because they'll be passed through jQuery.
|
||||||
|
{% endcallout %}
|
||||||
|
|
||||||
## Events
|
## Events
|
||||||
|
|
||||||
Bootstrap provides custom events for most plugins' unique actions. Generally, these come in an infinitive and past participle form - where the infinitive (ex. `show`) is triggered at the start of an event, and its past participle form (ex. `shown`) is triggered on the completion of an action.
|
Bootstrap provides custom events for most plugins' unique actions. Generally, these come in an infinitive and past participle form - where the infinitive (ex. `show`) is triggered at the start of an event, and its past participle form (ex. `shown`) is triggered on the completion of an action.
|
||||||
|
@ -72,15 +72,6 @@ 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
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* --------------------------------------------------------------------------
|
* --------------------------------------------------------------------------
|
||||||
* Public Util Api
|
* Public Util Api
|
||||||
@ -105,11 +96,6 @@ const Util = (($) => {
|
|||||||
selector = element.getAttribute('href') || ''
|
selector = element.getAttribute('href') || ''
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's an ID
|
|
||||||
if (selector.charAt(0) === '#') {
|
|
||||||
selector = escapeId(selector)
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const $selector = $(document).find(selector)
|
const $selector = $(document).find(selector)
|
||||||
return $selector.length > 0 ? selector : null
|
return $selector.length > 0 ? selector : null
|
||||||
|
@ -4,7 +4,7 @@ $(function () {
|
|||||||
QUnit.module('util')
|
QUnit.module('util')
|
||||||
|
|
||||||
QUnit.test('Util.getSelectorFromElement should return the correct element', function (assert) {
|
QUnit.test('Util.getSelectorFromElement should return the correct element', function (assert) {
|
||||||
assert.expect(5)
|
assert.expect(2)
|
||||||
|
|
||||||
var $el = $('<div data-target="body"></div>').appendTo($('#qunit-fixture'))
|
var $el = $('<div data-target="body"></div>').appendTo($('#qunit-fixture'))
|
||||||
assert.strictEqual(Util.getSelectorFromElement($el[0]), 'body')
|
assert.strictEqual(Util.getSelectorFromElement($el[0]), 'body')
|
||||||
@ -12,18 +12,6 @@ $(function () {
|
|||||||
// Not found element
|
// Not found element
|
||||||
var $el2 = $('<div data-target="#fakeDiv"></div>').appendTo($('#qunit-fixture'))
|
var $el2 = $('<div data-target="#fakeDiv"></div>').appendTo($('#qunit-fixture'))
|
||||||
assert.strictEqual(Util.getSelectorFromElement($el2[0]), null)
|
assert.strictEqual(Util.getSelectorFromElement($el2[0]), null)
|
||||||
|
|
||||||
// Should escape ID and find the correct element
|
|
||||||
var $el3 = $('<div data-target="#collapse:Example"></div>').appendTo($('#qunit-fixture'))
|
|
||||||
$('<div id="collapse:Example"></div>').appendTo($('#qunit-fixture'))
|
|
||||||
assert.strictEqual(Util.getSelectorFromElement($el3[0]), '#collapse\\:Example')
|
|
||||||
|
|
||||||
// If $.escapeSelector doesn't exist in older jQuery versions (< 3)
|
|
||||||
var tmpEscapeSelector = $.escapeSelector
|
|
||||||
delete $.escapeSelector
|
|
||||||
assert.ok(typeof $.escapeSelector === 'undefined', '$.escapeSelector undefined')
|
|
||||||
assert.strictEqual(Util.getSelectorFromElement($el3[0]), '#collapse\\:Example')
|
|
||||||
$.escapeSelector = tmpEscapeSelector
|
|
||||||
})
|
})
|
||||||
|
|
||||||
QUnit.test('Util.typeCheckConfig should thrown an error when a bad config is passed', function (assert) {
|
QUnit.test('Util.typeCheckConfig should thrown an error when a bad config is passed', function (assert) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user