Skip to content

Commit 3b2d1e7

Browse files
couchandjzaefferer
authored andcommitted
Tooltip: handle removal of elements with delegated tooltips. Fixed #8646 - Delegated tooltips don't close when the tooltipped element is removed
1 parent 6b48ef5 commit 3b2d1e7

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

tests/unit/tooltip/tooltip.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ <h2 id="qunit-userAgent"></h2>
4343
<input title="inputtitle">
4444
<span id="multiple-describedby" aria-describedby="fixture-span" title="...">aria-describedby</span>
4545
<span id="fixture-span" title="title-text">span</span>
46+
<span id="contains-tooltipped"><span id="contained-tooltipped" title="foobar">baz</span></span>
4647
</div>
4748

4849
</div>

tests/unit/tooltip/tooltip_core.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,17 @@ test( "accessibility", function() {
4444
equal( element.attr( "title" ), "...", "title restored when closed" );
4545
});
4646

47+
test( "delegated removal", function() {
48+
expect( 2 );
49+
50+
var container = $( "#contains-tooltipped" ).tooltip(),
51+
element = $( "#contained-tooltipped" );
52+
53+
element.trigger( "mouseover" );
54+
equal( $( ".ui-tooltip" ).length, 1 );
55+
56+
container.empty();
57+
equal( $( ".ui-tooltip" ).length, 0 );
58+
});
59+
4760
}( jQuery ) );

ui/jquery.ui.tooltip.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ $.widget( "ui.tooltip", {
207207

208208
_open: function( event, target, content ) {
209209
var tooltip, positionOption, events;
210+
210211
if ( !content ) {
211212
return;
212213
}
@@ -268,6 +269,9 @@ $.widget( "ui.tooltip", {
268269
fakeEvent.currentTarget = target[0];
269270
this.close( fakeEvent, true );
270271
}
272+
},
273+
remove: function( event ) {
274+
this._removeTooltip( tooltip );
271275
}
272276
};
273277
if ( !event || event.type === "mouseover" ) {
@@ -299,12 +303,15 @@ $.widget( "ui.tooltip", {
299303

300304
tooltip.stop( true );
301305
this._hide( tooltip, this.options.hide, function() {
302-
$( this ).remove();
303-
delete that.tooltips[ this.id ];
306+
that._removeTooltip( $( this ) );
304307
});
305308

306309
target.removeData( "tooltip-open" );
307310
this._off( target, "mouseleave focusout keyup" );
311+
// Remove 'remove' binding only on delegated targets
312+
if ( target[0] !== this.element[0] ) {
313+
this._off( target, "remove" );
314+
}
308315
this._off( this.document, "mousemove" );
309316

310317
if ( event && event.type === "mouseleave" ) {
@@ -344,6 +351,11 @@ $.widget( "ui.tooltip", {
344351
return id ? $( "#" + id ) : $();
345352
},
346353

354+
_removeTooltip: function( tooltip ) {
355+
tooltip.remove();
356+
delete this.tooltips[ tooltip.attr( "id" ) ];
357+
},
358+
347359
_destroy: function() {
348360
var that = this;
349361

0 commit comments

Comments
 (0)