calling any optionless method on element with no data-* attributes creates empty tooltip/popover object
Created by: IanKemp
http://jsbin.com/cujijohutu/1/
Steps:
- Run attached JS Bin and hover over the link. Notice how the popup appears after ~1 second. Move mouse over/out the link multiple times and notice the popup always appears, as expected.
- Click the "Run with JS" button to reset the bin. Hover over the link and move the mouse cursor away before the timeout has elapsed. Move mouse over/out the link multiple times and notice that the popup never appears - unexpected/erroneous.
- Edit the JavaScript code in the bin and uncomment the commented-out section. Click the "Run with JS" button to reset the bin. Hover over the link and move the mouse cursor away before the timeout has elapsed. Move mouse over/out the link multiple times and notice that the popup always appears, as expected.
Cause:
The excerpt below is from the popover plugin code. The first time this function is invoked via a call to "element.popover()", it will construct a Popover() object and cache it on the attached element via the "bs.popover" data attribute. However, if the first invocation does not pass in an object, the value of the variable "options" becomes false, and the Popover() object is constructed with that value, which effectively means "use the default popover values, or those specified via data- attributes". Since there are no values specified in my example, the title and content attributes are set to the empty string, and because any subsequent call(s) to set the options are ignored, the result is a "zombie" popup.
function Plugin(option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.popover')
var options = typeof option == 'object' && option
if (!data && option == 'destroy') return
// **below line is the problem**
if (!data) $this.data('bs.popover', (data = new Popover(this, options)))
if (typeof option == 'string') data[option]()
})
}
Resolution:
This is quite an edge case, but I feel that the plugin should only assign and cache the Popover object if the options specified are not equal to the plugin's default options, perhaps in the tooltip.init() method.
Notes:
This affects tooltip as well.