Skip to content

Commit

Permalink
Selector Engine: fix multiple IDs (#39201)
Browse files Browse the repository at this point in the history
* fix: regression of #38989

* Add unit test in selector-engine.spec.js

---------

Co-authored-by: Julien Déramond <juderamond@gmail.com>
Co-authored-by: XhmikosR <xhmikosr@gmail.com>
  • Loading branch information
3 people authored Feb 18, 2024
1 parent 1bc85bf commit 6ed1cdd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions js/src/dom/selector-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ const getSelector = element => {
hrefAttribute = `#${hrefAttribute.split('#')[1]}`
}

selector = hrefAttribute && hrefAttribute !== '#' ? parseSelector(hrefAttribute.trim()) : null
selector = hrefAttribute && hrefAttribute !== '#' ? hrefAttribute.trim() : null
}

return selector
return selector ? selector.split(',').map(sel => parseSelector(sel)).join(',') : null
}

const SelectorEngine = {
Expand Down
12 changes: 12 additions & 0 deletions js/tests/unit/dom/selector-engine.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,18 @@ describe('SelectorEngine', () => {
expect(SelectorEngine.getMultipleElementsFromSelector(testEl)).toEqual(Array.from(fixtureEl.querySelectorAll('.target')))
})

it('should get elements if several ids with special chars are given', () => {
fixtureEl.innerHTML = [
'<div id="test" data-bs-target="#j_id11:exampleModal,#j_id22:exampleModal"></div>',
'<div class="target" id="j_id11:exampleModal"></div>',
'<div class="target" id="j_id22:exampleModal"></div>'
].join('')

const testEl = fixtureEl.querySelector('#test')

expect(SelectorEngine.getMultipleElementsFromSelector(testEl)).toEqual(Array.from(fixtureEl.querySelectorAll('.target')))
})

it('should get elements in array, from href if no data-bs-target set', () => {
fixtureEl.innerHTML = [
'<a id="test" href=".target"></a>',
Expand Down
29 changes: 29 additions & 0 deletions js/tests/unit/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,35 @@ describe('Modal', () => {
trigger.click()
})
})

it('should open modal, having special characters in its id', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#j_id22:exampleModal">',
' Launch demo modal',
'</button>',
'<div class="modal fade" id="j_id22:exampleModal" aria-labelledby="exampleModalLabel" aria-hidden="true">',
' <div class="modal-dialog">',
' <div class="modal-content">',
' <div class="modal-body">',
' <p>modal body</p>',
' </div>',
' </div>',
' </div>',
'</div>'
].join('')

const modalEl = fixtureEl.querySelector('.modal')
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')

modalEl.addEventListener('shown.bs.modal', () => {
resolve()
})

trigger.click()
})
})

it('should not prevent default when a click occurred on data-bs-dismiss="modal" where tagName is DIFFERENT than <a> or <area>', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
Expand Down

0 comments on commit 6ed1cdd

Please sign in to comment.