Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collapse - preventDefault only on <a> elements not inside the collapse element #23666

Merged
merged 1 commit into from
Aug 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion js/src/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a> elements (which change the URL) not inside the collapsible element
if (event.target.tagName === 'A' && !$.contains(this, event.target)) {
event.preventDefault()
}

Expand Down
25 changes: 25 additions & 0 deletions js/tests/unit/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $('<input type="checkbox" data-toggle="collapse" data-target="#collapsediv1" />').appendTo('#qunit-fixture')
var htmlCollapse =
'<div id="collapsediv1" class="collapse">' +
' <input type="checkbox" id="testCheckbox" />' +
'</div>'

$(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'))
})
})
2 changes: 1 addition & 1 deletion js/tests/visual/collapse.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h5 class="mb-0">
</div>
</div>

<script src="../../../docs/assets/js/vendor/jquery-slim.min.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you go through all visual test files and see if there are more references like that and replace all of them?

Copy link
Member Author

@Johann-S Johann-S Aug 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was the only one I forget 😄 (I made a check)

<script src="../../../assets/js/vendor/jquery-slim.min.js"></script>
<script src="../../dist/util.js"></script>
<script src="../../dist/collapse.js"></script>
</body>
Expand Down