diff --git a/js/src/tooltip.js b/js/src/tooltip.js index fbf145e3bf45..a5586d6f640d 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -539,6 +539,18 @@ class Tooltip extends BaseComponent { _getConfig(config) { const dataAttributes = Manipulator.getDataAttributes(this._element) + // Read text attributes without coercing their values. + const title = this._element.getAttribute('data-bs-title') + const content = this._element.getAttribute('data-bs-content') + + if (title !== null) { + dataAttributes.title = title + } + + if (content !== null) { + dataAttributes.content = content + } + for (const dataAttribute of Object.keys(dataAttributes)) { if (DISALLOWED_ATTRIBUTES.has(dataAttribute)) { delete dataAttributes[dataAttribute] diff --git a/js/tests/unit/popover.spec.js b/js/tests/unit/popover.spec.js index 1338821bc86d..5905b709e12b 100644 --- a/js/tests/unit/popover.spec.js +++ b/js/tests/unit/popover.spec.js @@ -58,6 +58,42 @@ describe('Popover', () => { }) describe('show', () => { + it('should show literal content when the data title is empty', () => { + fixtureEl.innerHTML = '' + const element = fixtureEl.querySelector('button') + const popover = new Popover(element, { animation: false, trigger: 'manual' }) + + popover.show() + + expect(document.querySelector('.popover-header')).toBeNull() + expect(document.querySelector('.popover-body').textContent).toEqual('9.90') + popover.dispose() + }) + + it('should prefer explicit text options over data attributes', () => { + fixtureEl.innerHTML = '' + const element = fixtureEl.querySelector('button') + const popover = new Popover(element, { title: 'Explicit title', content: 'Explicit content' }) + + expect(popover._config.title).toEqual('Explicit title') + expect(popover._config.content).toEqual('Explicit content') + popover.dispose() + }) + + it('should sanitize HTML from the raw content attribute', () => { + const element = document.createElement('button') + element.setAttribute('data-bs-content', 'Help') + element.setAttribute('data-bs-sanitize', 'false') + fixtureEl.append(element) + const popover = new Popover(element, { animation: false, html: true, trigger: 'manual' }) + + popover.show() + + const body = document.querySelector('.popover-body') + expect(body.innerHTML).toEqual('Help') + popover.dispose() + }) + it('should toggle a popover after show', () => { return new Promise(resolve => { fixtureEl.innerHTML = 'BS X' diff --git a/js/tests/unit/tooltip.spec.js b/js/tests/unit/tooltip.spec.js index 37f2c230d037..8ea8499a364b 100644 --- a/js/tests/unit/tooltip.spec.js +++ b/js/tests/unit/tooltip.spec.js @@ -90,6 +90,19 @@ describe('Tooltip', () => { expect(tooltip._config.content).toEqual('7') }) + for (const title of ['9.90', '', '[1,2]', 'null']) { + it(`should preserve the literal data title ${JSON.stringify(title)}`, () => { + const element = document.createElement('button') + element.setAttribute('data-bs-title', title) + fixtureEl.append(element) + + const tooltip = new Tooltip(element) + + expect(tooltip._config.title).toEqual(title) + tooltip.dispose() + }) + } + it('should enable selector delegation', () => { return new Promise(resolve => { fixtureEl.innerHTML = '
'