diff --git a/js/src/tooltip.js b/js/src/tooltip.js
index 650bf2b092..f8d97f2408 100644
--- a/js/src/tooltip.js
+++ b/js/src/tooltip.js
@@ -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)
}
diff --git a/js/tests/unit/tooltip.spec.js b/js/tests/unit/tooltip.spec.js
index 1431d837dd..c8ab068187 100644
--- a/js/tests/unit/tooltip.spec.js
+++ b/js/tests/unit/tooltip.spec.js
@@ -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 = ' '
+
+ 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 = ''