]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Tooltip: remove title attribute before show & add tests (#35456)
authorGeoSot <geo.sotis@gmail.com>
Tue, 7 Dec 2021 13:51:56 +0000 (15:51 +0200)
committerGitHub <noreply@github.com>
Tue, 7 Dec 2021 13:51:56 +0000 (15:51 +0200)
js/src/tooltip.js
js/tests/unit/tooltip.spec.js

index 2f3acda384da6287ddc769cf54bc67c8b288bdef..19a9b316858486bf7316a6fb4e250299eca683cb 100644 (file)
@@ -394,7 +394,7 @@ class Tooltip extends BaseComponent {
   }
 
   _getTitle() {
-    return this._resolvePossibleFunction(this._config.title) || this._element.getAttribute('title')
+    return this._config.title
   }
 
   // Private
@@ -510,11 +510,17 @@ class Tooltip extends BaseComponent {
   }
 
   _fixTitle() {
-    const title = this._element.getAttribute('title')
+    const title = this._config.originalTitle
 
-    if (title && !this._element.getAttribute('aria-label') && !this._element.textContent) {
+    if (!title) {
+      return
+    }
+
+    if (!this._element.getAttribute('aria-label') && !this._element.textContent) {
       this._element.setAttribute('aria-label', title)
     }
+
+    this._element.removeAttribute('title')
   }
 
   _enter() {
@@ -579,6 +585,8 @@ class Tooltip extends BaseComponent {
       }
     }
 
+    config.originalTitle = this._element.getAttribute('title') || ''
+    config.title = this._resolvePossibleFunction(config.title) || config.originalTitle
     if (typeof config.title === 'number') {
       config.title = config.title.toString()
     }
index f92b74d963dd8bd1ed58a30fa0c12342ab0a428e..9054c0f642ebb2f3ca1008b4a724838d2e6786e0 100644 (file)
@@ -180,6 +180,15 @@ describe('Tooltip', () => {
       expect(getPopperConfig).toHaveBeenCalled()
       expect(popperConfig.placement).toEqual('left')
     })
+
+    it('should use original title, if not "data-bs-title" is given', () => {
+      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>'
+
+      const tooltipEl = fixtureEl.querySelector('a')
+      const tooltip = new Tooltip(tooltipEl)
+
+      expect(tooltip._config.title).toEqual('Another tooltip')
+    })
   })
 
   describe('enable', () => {
@@ -855,6 +864,19 @@ describe('Tooltip', () => {
 
       tooltip.show()
     })
+
+    it('should remove `title` attribute if exists', done => {
+      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>'
+
+      const tooltipEl = fixtureEl.querySelector('a')
+      const tooltip = new Tooltip(tooltipEl)
+
+      tooltipEl.addEventListener('shown.bs.tooltip', () => {
+        expect(tooltipEl.getAttribute('title')).toBeNull()
+        done()
+      })
+      tooltip.show()
+    })
   })
 
   describe('hide', () => {