Skip to content

Commit

Permalink
add information about valid selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann-S committed Aug 26, 2018
1 parent 0e88315 commit 2ad5192
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 38 deletions.
9 changes: 1 addition & 8 deletions js/src/util.js
Expand Up @@ -77,20 +77,13 @@ const Util = (($) => {

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

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

const validSelector = selector
if (selector.charAt(0) === '#' && selector.indexOf(',') === -1) {
selector = selector.substr(1)
method = 'getElementById'
}

try {
return document[method](selector) ? validSelector : null
return document.querySelector(selector) ? selector : null
} catch (err) {
return null
}
Expand Down
25 changes: 0 additions & 25 deletions js/tests/unit/util.js
Expand Up @@ -20,31 +20,6 @@ $(function () {
assert.strictEqual(Util.getSelectorFromElement($el2[0]), null)
})

QUnit.test('Util.getSelectorFromElement should use getElementById', function (assert) {
assert.expect(2)

var spy = sinon.spy(document, 'getElementById')

var $el = $('<div data-target="#7"></div>').appendTo($('#qunit-fixture'))
$('<div id="7" />').appendTo($('#qunit-fixture'))

assert.strictEqual(Util.getSelectorFromElement($el[0]), '#7')
assert.ok(spy.called)
})

QUnit.test('Util.getSelectorFromElement should use querySelector when there are multi ids', function (assert) {
assert.expect(2)

var spy = sinon.spy(document, 'querySelector')

var $el = $('<div data-target="#j7, #j8"></div>').appendTo($('#qunit-fixture'))
$('<div id="j7" />').appendTo($('#qunit-fixture'))
$('<div id="j8" />').appendTo($('#qunit-fixture'))

assert.strictEqual(Util.getSelectorFromElement($el[0]), '#j7, #j8')
assert.ok(spy.called)
})

QUnit.test('Util.typeCheckConfig should thrown an error when a bad config is passed', function (assert) {
assert.expect(1)
var namePlugin = 'collapse'
Expand Down
9 changes: 4 additions & 5 deletions site/docs/4.1/getting-started/javascript.md
Expand Up @@ -34,11 +34,10 @@ Alternatively, to target a specific plugin, just include the plugin's name as a
$(document).off('.alert.data-api')
{% endhighlight %}

{% capture callout %}
##### Escaping selectors
If you use special selectors, for example: `collapse:Example`, be sure to escape them, because they'll be passed through jQuery.
{% endcapture %}
{% include callout.html content=callout type="warning" %}
## Selectors

Currently to queried DOM elements we use native methods (for performance reasons) `querySelector` and `querySelectorAll`, so you have to use [valid selectors](https://www.w3.org/TR/CSS21/syndata.html#value-def-identifier).
If you use special selectors, for example: `collapse:Example`, be sure to escape them.

## Events

Expand Down

0 comments on commit 2ad5192

Please sign in to comment.