From d7d80ade6b155da92a8ea355be3e20079ae62364 Mon Sep 17 00:00:00 2001 From: Johann-S Date: Fri, 25 Aug 2017 10:14:18 +0200 Subject: [PATCH] Collapse - preventDefault only on elements not inside the collapsible element --- js/src/collapse.js | 3 ++- js/tests/unit/collapse.js | 25 +++++++++++++++++++++++++ js/tests/visual/collapse.html | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/js/src/collapse.js b/js/src/collapse.js index 718630bd3791..7d1ba4c54fc3 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -361,7 +361,8 @@ const Collapse = (($) => { */ $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) { - if (!/input|textarea/i.test(event.target.tagName)) { + // preventDefault only for elements (which change the URL) not inside the collapsible element + if (event.target.tagName === 'A' && !$.contains(this, event.target)) { event.preventDefault() } diff --git a/js/tests/unit/collapse.js b/js/tests/unit/collapse.js index 4470a18c2792..9ecb60994ff9 100644 --- a/js/tests/unit/collapse.js +++ b/js/tests/unit/collapse.js @@ -673,4 +673,29 @@ $(function () { }) $trigger3.trigger('click') }) + + QUnit.test('should not prevent interactions inside the collapse element', function (assert) { + assert.expect(2) + var done = assert.async() + + var $target = $('').appendTo('#qunit-fixture') + var htmlCollapse = + '
' + + ' ' + + '
' + + $(htmlCollapse) + .appendTo('#qunit-fixture') + .on('shown.bs.collapse', function () { + assert.ok($target.prop('checked'), '$trigger is checked') + var $testCheckbox = $('#testCheckbox') + $testCheckbox.trigger($.Event('click')) + setTimeout(function () { + assert.ok($testCheckbox.prop('checked'), '$testCheckbox is checked too') + done() + }, 5) + }) + + $target.trigger($.Event('click')) + }) }) diff --git a/js/tests/visual/collapse.html b/js/tests/visual/collapse.html index 1d61ef19c987..6b4e89bfb333 100644 --- a/js/tests/visual/collapse.html +++ b/js/tests/visual/collapse.html @@ -57,7 +57,7 @@
- +