1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-07 06:06:47 +02:00

Tooltip: remove title attribute before show & add tests (#35456)

This commit is contained in:
GeoSot
2021-12-07 15:51:56 +02:00
committed by GitHub
parent ba7863a5bb
commit 328f723008
2 changed files with 33 additions and 3 deletions

View File

@@ -394,7 +394,7 @@ class Tooltip extends BaseComponent {
} }
_getTitle() { _getTitle() {
return this._resolvePossibleFunction(this._config.title) || this._element.getAttribute('title') return this._config.title
} }
// Private // Private
@@ -510,11 +510,17 @@ class Tooltip extends BaseComponent {
} }
_fixTitle() { _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.setAttribute('aria-label', title)
} }
this._element.removeAttribute('title')
} }
_enter() { _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') { if (typeof config.title === 'number') {
config.title = config.title.toString() config.title = config.title.toString()
} }

View File

@@ -180,6 +180,15 @@ describe('Tooltip', () => {
expect(getPopperConfig).toHaveBeenCalled() expect(getPopperConfig).toHaveBeenCalled()
expect(popperConfig.placement).toEqual('left') 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', () => { describe('enable', () => {
@@ -855,6 +864,19 @@ describe('Tooltip', () => {
tooltip.show() 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', () => { describe('hide', () => {