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

Tooltip/Popover: add underscore prefix to protected functions

This commit is contained in:
GeoSot
2021-11-29 16:25:12 +02:00
committed by XhmikosR
parent bd79d69a73
commit 385fea49e8
4 changed files with 48 additions and 48 deletions

View File

@@ -73,14 +73,14 @@ class Popover extends Tooltip {
} }
// Overrides // Overrides
isWithContent() { _isWithContent() {
return this.getTitle() || this._getContent() return this._getTitle() || this._getContent()
} }
// Private // Private
_getContentForTemplate() { _getContentForTemplate() {
return { return {
[SELECTOR_TITLE]: this.getTitle(), [SELECTOR_TITLE]: this._getTitle(),
[SELECTOR_CONTENT]: this._getContent() [SELECTOR_CONTENT]: this._getContent()
} }
} }

View File

@@ -181,7 +181,7 @@ class Tooltip extends BaseComponent {
context._leave() context._leave()
} }
} else { } else {
if (this.getTipElement().classList.contains(CLASS_NAME_SHOW)) { if (this._getTipElement().classList.contains(CLASS_NAME_SHOW)) {
this._leave() this._leave()
return return
} }
@@ -208,7 +208,7 @@ class Tooltip extends BaseComponent {
throw new Error('Please use show on visible elements') throw new Error('Please use show on visible elements')
} }
if (!(this.isWithContent() && this._isEnabled)) { if (!(this._isWithContent() && this._isEnabled)) {
return return
} }
@@ -222,7 +222,7 @@ class Tooltip extends BaseComponent {
return return
} }
const tip = this.getTipElement() const tip = this._getTipElement()
this._element.setAttribute('aria-describedby', tip.getAttribute('id')) this._element.setAttribute('aria-describedby', tip.getAttribute('id'))
@@ -279,7 +279,7 @@ class Tooltip extends BaseComponent {
return return
} }
const tip = this.getTipElement() const tip = this._getTipElement()
tip.classList.remove(CLASS_NAME_SHOW) tip.classList.remove(CLASS_NAME_SHOW)
// If this is a touch-enabled device we remove the extra // If this is a touch-enabled device we remove the extra
@@ -320,11 +320,11 @@ class Tooltip extends BaseComponent {
} }
// Protected // Protected
isWithContent() { _isWithContent() {
return Boolean(this.getTitle()) return Boolean(this._getTitle())
} }
getTipElement() { _getTipElement() {
if (!this.tip) { if (!this.tip) {
this.tip = this._createTipElement(this._getContentForTemplate()) this.tip = this._createTipElement(this._getContentForTemplate())
} }
@@ -389,11 +389,11 @@ class Tooltip extends BaseComponent {
_getContentForTemplate() { _getContentForTemplate() {
return { return {
[SELECTOR_TOOLTIP_INNER]: this.getTitle() [SELECTOR_TOOLTIP_INNER]: this._getTitle()
} }
} }
getTitle() { _getTitle() {
return this._resolvePossibleFunction(this._config.title) || this._element.getAttribute('title') return this._resolvePossibleFunction(this._config.title) || this._element.getAttribute('title')
} }
@@ -518,7 +518,7 @@ class Tooltip extends BaseComponent {
} }
_enter() { _enter() {
if (this.getTipElement().classList.contains(CLASS_NAME_SHOW) || this._isHovered) { if (this._getTipElement().classList.contains(CLASS_NAME_SHOW) || this._isHovered) {
this._isHovered = true this._isHovered = true
return return
} }

View File

@@ -162,7 +162,7 @@ describe('Popover', () => {
const popover = new Popover(popoverEl) const popover = new Popover(popoverEl)
popover.setContent({ '.tooltip-inner': 'foo' }) popover.setContent({ '.tooltip-inner': 'foo' })
const tip = popover.getTipElement() const tip = popover._getTipElement()
expect(tip).toHaveClass('popover') expect(tip).toHaveClass('popover')
expect(tip).toHaveClass('bs-popover-auto') expect(tip).toHaveClass('bs-popover-auto')

View File

@@ -466,12 +466,12 @@ describe('Tooltip', () => {
}) })
tooltipEl.addEventListener('inserted.bs.tooltip', () => { tooltipEl.addEventListener('inserted.bs.tooltip', () => {
expect(tooltip.getTipElement()).toHaveClass('bs-tooltip-auto') expect(tooltip._getTipElement()).toHaveClass('bs-tooltip-auto')
}) })
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(tooltip.getTipElement()).toHaveClass('bs-tooltip-auto') expect(tooltip._getTipElement()).toHaveClass('bs-tooltip-auto')
expect(tooltip.getTipElement().getAttribute('data-popper-placement')).toEqual('bottom') expect(tooltip._getTipElement().getAttribute('data-popper-placement')).toEqual('bottom')
done() done()
}) })
@@ -689,20 +689,20 @@ describe('Tooltip', () => {
}) })
setTimeout(() => { setTimeout(() => {
expect(tooltip.getTipElement()).toHaveClass('show') expect(tooltip._getTipElement()).toHaveClass('show')
tooltipEl.dispatchEvent(createEvent('mouseout')) tooltipEl.dispatchEvent(createEvent('mouseout'))
setTimeout(() => { setTimeout(() => {
expect(tooltip.getTipElement()).toHaveClass('show') expect(tooltip._getTipElement()).toHaveClass('show')
tooltipEl.dispatchEvent(createEvent('mouseover')) tooltipEl.dispatchEvent(createEvent('mouseover'))
}, 100) }, 100)
setTimeout(() => { setTimeout(() => {
expect(tooltip.getTipElement()).toHaveClass('show') expect(tooltip._getTipElement()).toHaveClass('show')
expect(document.querySelectorAll('.tooltip')).toHaveSize(1) expect(document.querySelectorAll('.tooltip')).toHaveSize(1)
done() done()
}, 200) }, 200)
}, 3) }, 10)
tooltipEl.dispatchEvent(createEvent('mouseover')) tooltipEl.dispatchEvent(createEvent('mouseover'))
}) })
@@ -752,20 +752,20 @@ describe('Tooltip', () => {
setTimeout(() => { setTimeout(() => {
expect(tooltip._popper).not.toBeNull() expect(tooltip._popper).not.toBeNull()
expect(tooltip.getTipElement().getAttribute('data-popper-placement')).toEqual('top') expect(tooltip._getTipElement().getAttribute('data-popper-placement')).toEqual('top')
tooltipEl.dispatchEvent(createEvent('mouseout')) tooltipEl.dispatchEvent(createEvent('mouseout'))
setTimeout(() => { setTimeout(() => {
expect(tooltip.getTipElement()).not.toHaveClass('show') expect(tooltip._getTipElement()).not.toHaveClass('show')
tooltipEl.dispatchEvent(createEvent('mouseover')) tooltipEl.dispatchEvent(createEvent('mouseover'))
}, 100) }, 100)
setTimeout(() => { setTimeout(() => {
expect(tooltip._popper).not.toBeNull() expect(tooltip._popper).not.toBeNull()
expect(tooltip.getTipElement().getAttribute('data-popper-placement')).toEqual('top') expect(tooltip._getTipElement().getAttribute('data-popper-placement')).toEqual('top')
done() done()
}, 200) }, 200)
}, 3) }, 10)
tooltipEl.dispatchEvent(createEvent('mouseover')) tooltipEl.dispatchEvent(createEvent('mouseover'))
}) })
@@ -986,14 +986,14 @@ describe('Tooltip', () => {
}) })
}) })
describe('isWithContent', () => { describe('_isWithContent', () => {
it('should return true if there is content', () => { it('should return true if there is content', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
expect(tooltip.isWithContent()).toBeTrue() expect(tooltip._isWithContent()).toBeTrue()
}) })
it('should return false if there is no content', () => { it('should return false if there is no content', () => {
@@ -1002,11 +1002,11 @@ describe('Tooltip', () => {
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
expect(tooltip.isWithContent()).toBeFalse() expect(tooltip._isWithContent()).toBeFalse()
}) })
}) })
describe('getTipElement', () => { describe('_getTipElement', () => {
it('should create the tip element and return it', () => { it('should create the tip element and return it', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
@@ -1015,7 +1015,7 @@ describe('Tooltip', () => {
spyOn(document, 'createElement').and.callThrough() spyOn(document, 'createElement').and.callThrough()
expect(tooltip.getTipElement()).toBeDefined() expect(tooltip._getTipElement()).toBeDefined()
expect(document.createElement).toHaveBeenCalled() expect(document.createElement).toHaveBeenCalled()
}) })
@@ -1027,12 +1027,12 @@ describe('Tooltip', () => {
const spy = spyOn(document, 'createElement').and.callThrough() const spy = spyOn(document, 'createElement').and.callThrough()
expect(tooltip.getTipElement()).toBeDefined() expect(tooltip._getTipElement()).toBeDefined()
expect(spy).toHaveBeenCalled() expect(spy).toHaveBeenCalled()
spy.calls.reset() spy.calls.reset()
expect(tooltip.getTipElement()).toBeDefined() expect(tooltip._getTipElement()).toBeDefined()
expect(spy).not.toHaveBeenCalled() expect(spy).not.toHaveBeenCalled()
}) })
}) })
@@ -1044,7 +1044,7 @@ describe('Tooltip', () => {
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { animation: false }) const tooltip = new Tooltip(tooltipEl, { animation: false })
const tip = tooltip.getTipElement() const tip = tooltip._getTipElement()
tooltip.setContent(tip) tooltip.setContent(tip)
@@ -1059,7 +1059,7 @@ describe('Tooltip', () => {
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltip.show() tooltip.show()
const tip = () => tooltip.getTipElement() const tip = () => tooltip._getTipElement()
expect(tip()).toHaveClass('show') expect(tip()).toHaveClass('show')
tooltip.setContent({ '.tooltip-inner': 'foo' }) tooltip.setContent({ '.tooltip-inner': 'foo' })
@@ -1073,7 +1073,7 @@ describe('Tooltip', () => {
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
const tip = () => tooltip.getTipElement() const tip = () => tooltip._getTipElement()
expect(tip()).not.toHaveClass('show') expect(tip()).not.toHaveClass('show')
tooltip.setContent({ '.tooltip-inner': 'foo' }) tooltip.setContent({ '.tooltip-inner': 'foo' })
@@ -1089,7 +1089,7 @@ describe('Tooltip', () => {
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltip.setContent({ '.tooltip-inner': 'foo' }) tooltip.setContent({ '.tooltip-inner': 'foo' })
const tip = tooltip.getTipElement() const tip = tooltip._getTipElement()
expect(tip).toHaveClass('tooltip') expect(tip).toHaveClass('tooltip')
expect(tip).toHaveClass('bs-tooltip-auto') expect(tip).toHaveClass('bs-tooltip-auto')
@@ -1121,7 +1121,7 @@ describe('Tooltip', () => {
html: true html: true
}) })
tooltip.getTipElement().append(childContent) tooltip._getTipElement().append(childContent)
tooltip.setContent({ '.tooltip': childContent }) tooltip.setContent({ '.tooltip': childContent })
expect().nothing() expect().nothing()
@@ -1141,7 +1141,7 @@ describe('Tooltip', () => {
tooltip.setContent({ '.tooltip': { 0: childContent, jquery: 'jQuery' } }) tooltip.setContent({ '.tooltip': { 0: childContent, jquery: 'jQuery' } })
expect(childContent.parentNode).toEqual(tooltip.getTipElement()) expect(childContent.parentNode).toEqual(tooltip._getTipElement())
}) })
it('should add the child text content in the element', () => { it('should add the child text content in the element', () => {
@@ -1156,7 +1156,7 @@ describe('Tooltip', () => {
tooltip.setContent({ '.tooltip': childContent }) tooltip.setContent({ '.tooltip': childContent })
expect(childContent.textContent).toEqual(tooltip.getTipElement().textContent) expect(childContent.textContent).toEqual(tooltip._getTipElement().textContent)
}) })
it('should add html without sanitize it', () => { it('should add html without sanitize it', () => {
@@ -1170,7 +1170,7 @@ describe('Tooltip', () => {
tooltip.setContent({ '.tooltip': '<div id="childContent">Tooltip</div>' }) tooltip.setContent({ '.tooltip': '<div id="childContent">Tooltip</div>' })
expect(tooltip.getTipElement().querySelector('div').id).toEqual('childContent') expect(tooltip._getTipElement().querySelector('div').id).toEqual('childContent')
}) })
it('should add html sanitized', () => { it('should add html sanitized', () => {
@@ -1188,8 +1188,8 @@ describe('Tooltip', () => {
].join('') ].join('')
tooltip.setContent({ '.tooltip': content }) tooltip.setContent({ '.tooltip': content })
expect(tooltip.getTipElement().querySelector('div').id).toEqual('childContent') expect(tooltip._getTipElement().querySelector('div').id).toEqual('childContent')
expect(tooltip.getTipElement().querySelector('button')).toBeNull() expect(tooltip._getTipElement().querySelector('button')).toBeNull()
}) })
it('should add text content', () => { it('should add text content', () => {
@@ -1200,18 +1200,18 @@ describe('Tooltip', () => {
tooltip.setContent({ '.tooltip': 'test' }) tooltip.setContent({ '.tooltip': 'test' })
expect(tooltip.getTipElement().textContent).toEqual('test') expect(tooltip._getTipElement().textContent).toEqual('test')
}) })
}) })
describe('getTitle', () => { describe('_getTitle', () => {
it('should return the title', () => { it('should return the title', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
expect(tooltip.getTitle()).toEqual('Another tooltip') expect(tooltip._getTitle()).toEqual('Another tooltip')
}) })
it('should call title function', () => { it('should call title function', () => {
@@ -1222,7 +1222,7 @@ describe('Tooltip', () => {
title: () => 'test' title: () => 'test'
}) })
expect(tooltip.getTitle()).toEqual('test') expect(tooltip._getTitle()).toEqual('test')
}) })
}) })
@@ -1331,7 +1331,7 @@ describe('Tooltip', () => {
}) })
expect(tooltip).toBeInstanceOf(Tooltip) expect(tooltip).toBeInstanceOf(Tooltip)
expect(tooltip.getTitle()).toEqual('test') expect(tooltip._getTitle()).toEqual('test')
}) })
it('should return the instance when exists without given configuration', () => { it('should return the instance when exists without given configuration', () => {
@@ -1349,7 +1349,7 @@ describe('Tooltip', () => {
expect(tooltip).toBeInstanceOf(Tooltip) expect(tooltip).toBeInstanceOf(Tooltip)
expect(tooltip2).toEqual(tooltip) expect(tooltip2).toEqual(tooltip)
expect(tooltip2.getTitle()).toEqual('nothing') expect(tooltip2._getTitle()).toEqual('nothing')
}) })
}) })