Permalink
Browse files

Autocomplete: Fall back to .ui-front searching for empty jQuery objects

Fixes #9755
  • Loading branch information...
scottgonzalez committed Jan 20, 2014
1 parent 0bb807b commit 2ef1b16e4d3aa8766084e50f4a1d806c434e7e43
Showing with 27 additions and 12 deletions.
  1. +26 −11 tests/unit/autocomplete/autocomplete_options.js
  2. +1 −1 ui/jquery.ui.autocomplete.js
@@ -5,13 +5,18 @@ module( "autocomplete: options" );
var data = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby",
"python", "c", "scala", "groovy", "haskell", "perl" ];
-test( "appendTo", function() {
- expect( 8 );
- var detached = $( "<div>" ),
- element = $( "#autocomplete" ).autocomplete();
+test( "appendTo: null", function() {
+ expect( 1 );
+ var element = $( "#autocomplete" ).autocomplete();
equal( element.autocomplete( "widget" ).parent()[ 0 ], document.body,
"defaults to body" );
element.autocomplete( "destroy" );
+});
+
+test( "appendTo: explicit", function() {
+ expect( 6 );
+ var detached = $( "<div>" ),
+ element = $( "#autocomplete" );
element.autocomplete({
appendTo: ".autocomplete-wrap"
@@ -22,13 +27,6 @@ test( "appendTo", function() {
"only appends to one element" );
element.autocomplete( "destroy" );
- $( "#autocomplete-wrap2" ).addClass( "ui-front" );
- element.autocomplete();
- equal( element.autocomplete( "widget" ).parent()[ 0 ],
- $( "#autocomplete-wrap2" )[ 0 ], "null, inside .ui-front" );
- element.autocomplete( "destroy" );
- $( "#autocomlete-wrap2" ).removeClass( "ui-front" );
-
element.autocomplete().autocomplete( "option", "appendTo", "#autocomplete-wrap1" );
equal( element.autocomplete( "widget" ).parent()[ 0 ],
$( "#autocomplete-wrap1" )[ 0 ], "modified after init" );
@@ -54,6 +52,23 @@ test( "appendTo", function() {
element.autocomplete( "destroy" );
});
+test( "appendTo: ui-front", function() {
+ expect( 2 );
+ var element = $( "#autocomplete" );
+
+ $( "#autocomplete-wrap2" ).addClass( "ui-front" );
+ element.autocomplete();
+ equal( element.autocomplete( "widget" ).parent()[ 0 ],
+ $( "#autocomplete-wrap2" )[ 0 ], "null, inside .ui-front" );
+ element.autocomplete( "destroy" );
+
+ element.autocomplete({
+ appendTo: $()
+ });
+ equal( element.autocomplete( "widget" ).parent()[ 0 ],
+ $( "#autocomplete-wrap2" )[ 0 ], "null, inside .ui-front" );
+});
+
function autoFocusTest( afValue, focusedLength ) {
var element = $( "#autocomplete" ).autocomplete({
autoFocus: afValue,
@@ -339,7 +339,7 @@ $.widget( "ui.autocomplete", {
this.document.find( element ).eq( 0 );
}
- if ( !element ) {
+ if ( !element || !element[ 0 ] ) {
element = this.element.closest( ".ui-front" );
}

5 comments on commit 2ef1b16

Contributor

MoonScript replied Jan 20, 2014

@scottgonzalez thanks for the quick fix! I noticed a similar thing in the dialog widget:
https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.dialog.js#L133

And there may be other widgets that use the same appendTo option, too.

Owner

scottgonzalez replied Jan 20, 2014

Dialog doesn't use the .ui-front walking. You can only be explicit.

Contributor

MoonScript replied Jan 20, 2014

Yes, but you could end up with a case where the dialog wouldn't be appended to anything if an empty jQuery collection was passed in.

Owner

scottgonzalez replied Jan 20, 2014

We don't tend to write defensive code like this. The only reason to use dialog's appendTo option is because of forms or the like. If you pass an invalid object, the page is going to be broken anyway.

Contributor

MoonScript replied Jan 21, 2014

Please sign in to comment.