Skip to content

Commit 153cf3a

Browse files
Don't add empty content holder when there is no content available (#33982)
* Remove content holder when there is no content * Add tests to check the removal of header/content Co-authored-by: XhmikosR <[email protected]>
1 parent 0b2d20b commit 153cf3a

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

js/src/popover.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const Default = {
3030
content: '',
3131
template: '<div class="popover" role="tooltip">' +
3232
'<div class="popover-arrow"></div>' +
33-
'<h3 class="popover-header"></h3>' +
33+
'<h3 class="popover-header"></h3>' +
3434
'<div class="popover-body"></div>' +
3535
'</div>'
3636
}
@@ -90,6 +90,24 @@ class Popover extends Tooltip {
9090
return this.getTitle() || this._getContent()
9191
}
9292

93+
getTipElement() {
94+
if (this.tip) {
95+
return this.tip
96+
}
97+
98+
this.tip = super.getTipElement()
99+
100+
if (!this.getTitle()) {
101+
this.tip.removeChild(SelectorEngine.findOne(SELECTOR_TITLE, this.tip))
102+
}
103+
104+
if (!this._getContent()) {
105+
this.tip.removeChild(SelectorEngine.findOne(SELECTOR_CONTENT, this.tip))
106+
}
107+
108+
return this.tip
109+
}
110+
93111
setContent() {
94112
const tip = this.getTipElement()
95113

js/tests/unit/popover.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,46 @@ describe('Popover', () => {
117117
popover.show()
118118
})
119119

120+
it('should show a popover with just content without having header', done => {
121+
fixtureEl.innerHTML = '<a href="#">Nice link</a>'
122+
123+
const popoverEl = fixtureEl.querySelector('a')
124+
const popover = new Popover(popoverEl, {
125+
content: 'Some beautiful content :)'
126+
})
127+
128+
popoverEl.addEventListener('shown.bs.popover', () => {
129+
const popoverDisplayed = document.querySelector('.popover')
130+
131+
expect(popoverDisplayed).not.toBeNull()
132+
expect(popoverDisplayed.querySelector('.popover-header')).toBeNull()
133+
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('Some beautiful content :)')
134+
done()
135+
})
136+
137+
popover.show()
138+
})
139+
140+
it('should show a popover with just title without having body', done => {
141+
fixtureEl.innerHTML = '<a href="#">Nice link</a>'
142+
143+
const popoverEl = fixtureEl.querySelector('a')
144+
const popover = new Popover(popoverEl, {
145+
title: 'Title, which does not require content'
146+
})
147+
148+
popoverEl.addEventListener('shown.bs.popover', () => {
149+
const popoverDisplayed = document.querySelector('.popover')
150+
151+
expect(popoverDisplayed).not.toBeNull()
152+
expect(popoverDisplayed.querySelector('.popover-body')).toBeNull()
153+
expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Title, which does not require content')
154+
done()
155+
})
156+
157+
popover.show()
158+
})
159+
120160
it('should show a popover with provided custom class', done => {
121161
fixtureEl.innerHTML = '<a href="#" title="Popover" data-bs-content="https://twitter.com/getbootstrap" data-bs-custom-class="custom-class">BS twitter</a>'
122162

0 commit comments

Comments
 (0)