Skip to content

Commit

Permalink
Handle non-empty whitespace textContent in Tooltip trigger (#36588)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwalters512 committed Jul 6, 2022
1 parent 7d0b224 commit 3f324ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion js/src/tooltip.js
Expand Up @@ -518,7 +518,7 @@ class Tooltip extends BaseComponent {
return
}

if (!this._element.getAttribute('aria-label') && !this._element.textContent) {
if (!this._element.getAttribute('aria-label') && !this._element.textContent.trim()) {
this._element.setAttribute('aria-label', title)
}

Expand Down
19 changes: 19 additions & 0 deletions js/tests/unit/tooltip.spec.js
Expand Up @@ -1358,6 +1358,25 @@ describe('Tooltip', () => {
})
})

it('should add the aria-label attribute when element text content is a whitespace string', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="A tooltip"><span> </span></a>'

const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)

tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tooltipShown = document.querySelector('.tooltip')

expect(tooltipShown).not.toBeNull()
expect(tooltipEl.getAttribute('aria-label')).toEqual('A tooltip')
resolve()
})

tooltip.show()
})
})

it('should not add the aria-label attribute if the attribute already exists', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" aria-label="Different label" title="Another tooltip"></a>'
Expand Down

0 comments on commit 3f324ee

Please sign in to comment.