Skip to content

Commit a3e45d8

Browse files
authored
add information about valid selectors (#27137)
1 parent 54d86e6 commit a3e45d8

3 files changed

Lines changed: 5 additions & 35 deletions

File tree

js/src/util.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,13 @@ const Util = (($) => {
7777

7878
getSelectorFromElement(element) {
7979
let selector = element.getAttribute('data-target')
80-
let method = 'querySelector'
8180

8281
if (!selector || selector === '#') {
8382
selector = (element.getAttribute('href') || '').trim()
8483
}
8584

86-
const validSelector = selector
87-
if (selector.charAt(0) === '#' && selector.indexOf(',') === -1) {
88-
selector = selector.substr(1)
89-
method = 'getElementById'
90-
}
91-
9285
try {
93-
return document[method](selector) ? validSelector : null
86+
return document.querySelector(selector) ? selector : null
9487
} catch (err) {
9588
return null
9689
}

js/tests/unit/util.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,6 @@ $(function () {
2020
assert.strictEqual(Util.getSelectorFromElement($el2[0]), null)
2121
})
2222

23-
QUnit.test('Util.getSelectorFromElement should use getElementById', function (assert) {
24-
assert.expect(2)
25-
26-
var spy = sinon.spy(document, 'getElementById')
27-
28-
var $el = $('<div data-target="#7"></div>').appendTo($('#qunit-fixture'))
29-
$('<div id="7" />').appendTo($('#qunit-fixture'))
30-
31-
assert.strictEqual(Util.getSelectorFromElement($el[0]), '#7')
32-
assert.ok(spy.called)
33-
})
34-
35-
QUnit.test('Util.getSelectorFromElement should use querySelector when there are multi ids', function (assert) {
36-
assert.expect(2)
37-
38-
var spy = sinon.spy(document, 'querySelector')
39-
40-
var $el = $('<div data-target="#j7, #j8"></div>').appendTo($('#qunit-fixture'))
41-
$('<div id="j7" />').appendTo($('#qunit-fixture'))
42-
$('<div id="j8" />').appendTo($('#qunit-fixture'))
43-
44-
assert.strictEqual(Util.getSelectorFromElement($el[0]), '#j7, #j8')
45-
assert.ok(spy.called)
46-
})
47-
4823
QUnit.test('Util.typeCheckConfig should thrown an error when a bad config is passed', function (assert) {
4924
assert.expect(1)
5025
var namePlugin = 'collapse'

site/docs/4.1/getting-started/javascript.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ $(document).off('.alert.data-api')
3535
{% endhighlight %}
3636

3737
{% capture callout %}
38-
##### Escaping selectors
39-
If you use special selectors, for example: `collapse:Example`, be sure to escape them, because they'll be passed through jQuery.
38+
## Selectors
39+
40+
Currently to query DOM elements we use the native methods `querySelector` and `querySelectorAll` for performance reasons, so you have to use [valid selectors](https://www.w3.org/TR/CSS21/syndata.html#value-def-identifier).
41+
If you use special selectors, for example: `collapse:Example` be sure to escape them.
4042
{% endcapture %}
4143
{% include callout.html content=callout type="warning" %}
4244

0 commit comments

Comments
 (0)