From c0c14d1ac7d26792b34e30dbed9e41735ac3e5cd Mon Sep 17 00:00:00 2001 From: Andrew Couch Date: Mon, 15 Oct 2012 16:15:36 -0400 Subject: [PATCH 1/3] 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 | 14 ++++++++++++++ ui/jquery.ui.tooltip.js | 11 +++++++++-- 3 files changed, 24 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..de6b06e47f7 100644 --- a/tests/unit/tooltip/tooltip_core.js +++ b/tests/unit/tooltip/tooltip_core.js @@ -44,4 +44,18 @@ test( "accessibility", function() { equal( element.attr( "title" ), "...", "title restored when closed" ); }); +test( "delegated removal", function() { + expect( 2 ); + + var tooltip, + 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 980b43868e9..1f0fb8fc10f 100644 --- a/ui/jquery.ui.tooltip.js +++ b/ui/jquery.ui.tooltip.js @@ -180,7 +180,9 @@ $.widget( "ui.tooltip", { }, _open: function( event, target, content ) { - var tooltip, positionOption; + var tooltip, positionOption, id, + that = this; + if ( !content ) { return; } @@ -209,7 +211,8 @@ $.widget( "ui.tooltip", { } tooltip = this._tooltip( target ); - addDescribedBy( target, tooltip.attr( "id" ) ); + id = tooltip.attr( "id" ); + addDescribedBy( target, id ); tooltip.find( ".ui-tooltip-content" ).html( content ); function position( event ) { @@ -244,6 +247,10 @@ $.widget( "ui.tooltip", { fakeEvent.currentTarget = target[0]; this.close( fakeEvent, true ); } + }, + remove: function( event ) { + tooltip.remove(); + delete that.tooltips[ id ]; } }); }, From caccd2c2bd485257470dbcbe34c9a7a0ec9c80a4 Mon Sep 17 00:00:00 2001 From: Andrew Couch Date: Mon, 15 Oct 2012 16:33:26 -0400 Subject: [PATCH 2/3] Tooltip: refactor tooltip removal --- ui/jquery.ui.tooltip.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ui/jquery.ui.tooltip.js b/ui/jquery.ui.tooltip.js index 1f0fb8fc10f..c0aa13a1d05 100644 --- a/ui/jquery.ui.tooltip.js +++ b/ui/jquery.ui.tooltip.js @@ -180,7 +180,7 @@ $.widget( "ui.tooltip", { }, _open: function( event, target, content ) { - var tooltip, positionOption, id, + var tooltip, positionOption, that = this; if ( !content ) { @@ -211,8 +211,7 @@ $.widget( "ui.tooltip", { } tooltip = this._tooltip( target ); - id = tooltip.attr( "id" ); - addDescribedBy( target, id ); + addDescribedBy( target, tooltip.attr( "id" ) ); tooltip.find( ".ui-tooltip-content" ).html( content ); function position( event ) { @@ -249,8 +248,7 @@ $.widget( "ui.tooltip", { } }, remove: function( event ) { - tooltip.remove(); - delete that.tooltips[ id ]; + that._removeTooltip( tooltip ); } }); }, @@ -285,8 +283,7 @@ $.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" ); @@ -323,6 +320,11 @@ $.widget( "ui.tooltip", { return id ? $( "#" + id ) : $(); }, + _removeTooltip: function( tooltip ) { + tooltip.remove(); + delete this.tooltips[ tooltip.attr( "id" ) ]; + }, + _destroy: function() { var that = this; From 96eca2cb781c9fc29bc4a743d7f90265480ba28d Mon Sep 17 00:00:00 2001 From: Andrew Couch Date: Tue, 16 Oct 2012 10:01:52 -0400 Subject: [PATCH 3/3] Tooltip: change unnecessary "that" to keyword "this" --- ui/jquery.ui.tooltip.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ui/jquery.ui.tooltip.js b/ui/jquery.ui.tooltip.js index c0aa13a1d05..068a08c6c2d 100644 --- a/ui/jquery.ui.tooltip.js +++ b/ui/jquery.ui.tooltip.js @@ -180,8 +180,7 @@ $.widget( "ui.tooltip", { }, _open: function( event, target, content ) { - var tooltip, positionOption, - that = this; + var tooltip, positionOption; if ( !content ) { return; @@ -248,7 +247,7 @@ $.widget( "ui.tooltip", { } }, remove: function( event ) { - that._removeTooltip( tooltip ); + this._removeTooltip( tooltip ); } }); },