Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Tooltip: on close and destro only set title if empty or undefine. Fix…
…ed #8925 - Changes to title attribute are reverted on close

Ticket #8925 states that changes to the title attribute while the
tooltip is open are lost on tooltip close.

In the close and destroy functions, the title attribute is always
written if a value was stored in the element on open. It is possible
the attribute has changed and restoring the initial value may overwrite
the current value.

If the value is empty or undefined as set in open, do not set the title
attribute.

This fix has the limitation that if the user removed the title
attribute or set the value to an empty string the original title value
would be restored on close and destroy. Test cases 3 and 4 demonstrate
this limitation.
  • Loading branch information
robotdan committed Dec 2, 2013
commit 951d1fc2b7fc359ce5005d89a5795709d643cbe5
35 changes: 35 additions & 0 deletions tests/unit/tooltip/tooltip_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,39 @@ test( "widget", function() {
strictEqual( widgetElement[ 0 ], element[ 0 ], "same element" );
});

// #8925 - Changes to title attribute are preserved after close() or destroy()
test( "preserve changes to title attributes on close and destroy", function() {
expect( 4 );
var element = $( "#tooltipped1" ),
changed = "changed title text";

// 1. Changes to title attribute are preserved on close()
element.attr( "title", "original title text" ).tooltip()
.tooltip( "open", $.Event( "mouseover", { target: element[0] }) )
.attr( "title", changed )
.tooltip( "close" );
equal( $( "#tooltipped1" ).attr( "title" ), changed );

// 2. Changes to title attribute are preserved on destroy()
element.attr( "title", "original title text" ).tooltip()
.tooltip( "open", $.Event( "mouseover", { target: element[0] }) )
.attr( "title", changed )
.tooltip( "destroy" );
equal( $( "#tooltipped1" ).attr( "title" ), changed );

// 3. Changes to title attribute are NOT preserved when set to empty string
element.attr( "title", "original title text" ).tooltip()
.tooltip( "open", $.Event( "mouseover", { target: element[0] }) )
.attr( "title", "" )
.tooltip( "close" );
notEqual( $( "#tooltipped1" ).attr( "title" ), changed );

// 4. Changes to title attribute NOT preserved when attribute has been removed
element.attr( "title", "original title text" ).tooltip()
.tooltip( "open", $.Event( "mouseover", { target: element[0] }) )
.removeAttr( "title" )
.tooltip( "close" );
notEqual( $( "#tooltipped1" ).attr( "title" ), changed );
});

}( jQuery ) );
14 changes: 10 additions & 4 deletions ui/jquery.ui.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ $.widget( "ui.tooltip", {
close: function( event ) {
var that = this,
target = $( event ? event.currentTarget : this.element ),
tooltip = this._find( target );
tooltip = this._find( target ),
title = target.attr( "title" );

// disabling closes the tooltip, so we need to track when we're closing
// to avoid an infinite loop in case the tooltip becomes disabled on close
Expand All @@ -317,7 +318,8 @@ $.widget( "ui.tooltip", {
clearInterval( this.delayedShow );

// only set title if we had one before (see comment in _open())
if ( target.data( "ui-tooltip-title" ) ) {
// if the title attribute has changed since open(), don't restore
if ( target.data( "ui-tooltip-title" ) && ( title === "" || title === undefined ) ) {
target.attr( "title", target.data( "ui-tooltip-title" ) );
}

Expand Down Expand Up @@ -380,7 +382,7 @@ $.widget( "ui.tooltip", {
// close open tooltips
$.each( this.tooltips, function( id, element ) {
// Delegate to close method to handle common cleanup
var event = $.Event( "blur" );
var event = $.Event( "blur" ), title;
event.target = event.currentTarget = element[0];
that.close( event, true );

Expand All @@ -390,7 +392,11 @@ $.widget( "ui.tooltip", {

// Restore the title
if ( element.data( "ui-tooltip-title" ) ) {
element.attr( "title", element.data( "ui-tooltip-title" ) );
// if the title attribute has changed since open(), don't restore
title = element.attr( "title" );
if ( title === "" || title === undefined ) {
element.attr( "title", element.data( "ui-tooltip-title" ) );
}
element.removeData( "ui-tooltip-title" );
}
});
Expand Down