From 3b2d1e7736be99671af70aa3b775d67b917517e5 Mon Sep 17 00:00:00 2001 From: Andrew Couch Date: Mon, 15 Oct 2012 16:15:36 -0400 Subject: [PATCH] Tooltip: handle removal of elements with delegated tooltips. Fixed #8646 - Delegated tooltips don't close when the tooltipped element is removed --- tests/unit/tooltip/tooltip.html | 1 + tests/unit/tooltip/tooltip_core.js | 13 +++++++++++++ ui/jquery.ui.tooltip.js | 16 ++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/unit/tooltip/tooltip.html b/tests/unit/tooltip/tooltip.html index f6e60b36735..2c9667ff507 100644 --- a/tests/unit/tooltip/tooltip.html +++ b/tests/unit/tooltip/tooltip.html @@ -43,6 +43,7 @@

aria-describedby span + baz diff --git a/tests/unit/tooltip/tooltip_core.js b/tests/unit/tooltip/tooltip_core.js index 2b39253a270..06fb194cd7c 100644 --- a/tests/unit/tooltip/tooltip_core.js +++ b/tests/unit/tooltip/tooltip_core.js @@ -44,4 +44,17 @@ test( "accessibility", function() { equal( element.attr( "title" ), "...", "title restored when closed" ); }); +test( "delegated removal", function() { + expect( 2 ); + + var container = $( "#contains-tooltipped" ).tooltip(), + element = $( "#contained-tooltipped" ); + + element.trigger( "mouseover" ); + equal( $( ".ui-tooltip" ).length, 1 ); + + container.empty(); + equal( $( ".ui-tooltip" ).length, 0 ); +}); + }( jQuery ) ); diff --git a/ui/jquery.ui.tooltip.js b/ui/jquery.ui.tooltip.js index b56d939e831..1be56c7ea35 100644 --- a/ui/jquery.ui.tooltip.js +++ b/ui/jquery.ui.tooltip.js @@ -207,6 +207,7 @@ $.widget( "ui.tooltip", { _open: function( event, target, content ) { var tooltip, positionOption, events; + if ( !content ) { return; } @@ -268,6 +269,9 @@ $.widget( "ui.tooltip", { fakeEvent.currentTarget = target[0]; this.close( fakeEvent, true ); } + }, + remove: function( event ) { + this._removeTooltip( tooltip ); } }; if ( !event || event.type === "mouseover" ) { @@ -299,12 +303,15 @@ $.widget( "ui.tooltip", { tooltip.stop( true ); this._hide( tooltip, this.options.hide, function() { - $( this ).remove(); - delete that.tooltips[ this.id ]; + that._removeTooltip( $( this ) ); }); target.removeData( "tooltip-open" ); this._off( target, "mouseleave focusout keyup" ); + // Remove 'remove' binding only on delegated targets + if ( target[0] !== this.element[0] ) { + this._off( target, "remove" ); + } this._off( this.document, "mousemove" ); if ( event && event.type === "mouseleave" ) { @@ -344,6 +351,11 @@ $.widget( "ui.tooltip", { return id ? $( "#" + id ) : $(); }, + _removeTooltip: function( tooltip ) { + tooltip.remove(); + delete this.tooltips[ tooltip.attr( "id" ) ]; + }, + _destroy: function() { var that = this;