From: Johann-S Date: Tue, 7 Aug 2018 16:37:46 +0000 (+0200) Subject: fix(util): use getElementById when it's possible X-Git-Tag: v4.2.0~381 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6b92321f6a04f07e0a3531d0e546c3cc20867bdb;p=thirdparty%2Fbootstrap.git fix(util): use getElementById when it's possible --- diff --git a/js/src/util.js b/js/src/util.js index eb98d449c8..3008c22785 100644 --- a/js/src/util.js +++ b/js/src/util.js @@ -77,12 +77,20 @@ const Util = (($) => { getSelectorFromElement(element) { let selector = element.getAttribute('data-target') + let method = 'querySelector' + if (!selector || selector === '#') { - selector = element.getAttribute('href') || '' + selector = (element.getAttribute('href') || '').trim() + } + + const validSelector = selector + if (selector.charAt(0) === '#') { + selector = selector.substr(1) + method = 'getElementById' } try { - return document.querySelector(selector) ? selector : null + return document[method](selector) ? validSelector : null } catch (err) { return null } diff --git a/js/tests/unit/util.js b/js/tests/unit/util.js index 37327b8681..49252701a5 100644 --- a/js/tests/unit/util.js +++ b/js/tests/unit/util.js @@ -20,6 +20,18 @@ $(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 = $('
').appendTo($('#qunit-fixture')) + $('
').appendTo($('#qunit-fixture')) + + assert.strictEqual(Util.getSelectorFromElement($el[0]), '#7') + 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'