Skip to content

Commit 8108ec8

Browse files
committed
Effects: Adding a check to retain focused elements after wrapping and unwrapping in animations - Fixes #7595 - Wrapper-creating jquery-ui animations will discard any focus state during the animation - Thanks @rubyruy
1 parent 8fe87e2 commit 8108ec8

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

tests/unit/effects/effects_core.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,15 @@ asyncTest( "animateClass clears style properties when stopped", function() {
150150
start();
151151
});
152152

153+
test( "createWrapper and removeWrapper retain focused elements (#7595)", function() {
154+
expect( 2 );
155+
var test = $( "div.hidden" ).show(),
156+
input = $( "<input>" ).appendTo( test ).focus();
157+
158+
$.effects.createWrapper( test );
159+
equal( document.activeElement, input[ 0 ], "Active element is still input after createWrapper" );
160+
$.effects.removeWrapper( test );
161+
equal( document.activeElement, input[ 0 ], "Active element is still input after removeWrapper" );
162+
})
163+
153164
})(jQuery);

ui/jquery.effects.core.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,16 @@ $.extend( $.effects, {
415415
size = {
416416
width: element.width(),
417417
height: element.height()
418-
};
418+
},
419+
active = document.activeElement;
419420

420421
element.wrap( wrapper );
422+
423+
// Fixes #7595 - Elements lose focus when wrapped.
424+
if ( element[ 0 ] === active || $.contains( element[ 0 ], active ) ) {
425+
$( active ).focus();
426+
}
427+
421428
wrapper = element.parent(); //Hotfix for jQuery 1.4 since some change in wrap() seems to actually loose the reference to the wrapped element
422429

423430
// transfer positioning properties to the wrapper
@@ -449,8 +456,18 @@ $.extend( $.effects, {
449456
},
450457

451458
removeWrapper: function( element ) {
452-
if ( element.parent().is( ".ui-effects-wrapper" ) )
453-
return element.parent().replaceWith( element );
459+
var active = document.activeElement;
460+
461+
if ( element.parent().is( ".ui-effects-wrapper" ) ) {
462+
element.parent().replaceWith( element );
463+
464+
// Fixes #7595 - Elements lose focus when wrapped.
465+
if ( element[ 0 ] === active || $.contains( element[ 0 ], active ) ) {
466+
$( active ).focus();
467+
}
468+
}
469+
470+
454471
return element;
455472
},
456473

0 commit comments

Comments
 (0)