aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorScott González <scott.gonzalez@gmail.com>2012-11-28 10:33:44 -0500
committerScott González <scott.gonzalez@gmail.com>2012-11-28 10:33:59 -0500
commitdec844570fae5edf56876b760b9358fde2ecb5e7 (patch)
treef26cba55944b538152ffb7754cc697f76fe27432
parente77edc60991dc0d9908e056aa05adc49b9daa7cd (diff)
downloadjquery-ui-dec844570fae5edf56876b760b9358fde2ecb5e7.tar.gz
jquery-ui-dec844570fae5edf56876b760b9358fde2ecb5e7.zip
Autocomplete: When appendTo is a jQuery object or a DOM element, don't search against the document. Fixes #8858 - Autocomplete: Fails when appendTo is detached from the DOM.
-rw-r--r--tests/unit/autocomplete/autocomplete_options.js17
-rw-r--r--ui/jquery.ui.autocomplete.js10
2 files changed, 24 insertions, 3 deletions
diff --git a/tests/unit/autocomplete/autocomplete_options.js b/tests/unit/autocomplete/autocomplete_options.js
index 8331c53d5..d3a25d2fc 100644
--- a/tests/unit/autocomplete/autocomplete_options.js
+++ b/tests/unit/autocomplete/autocomplete_options.js
@@ -5,8 +5,9 @@ module( "autocomplete: options" );
var data = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl" ];
test( "appendTo", function() {
- expect( 5 );
- var element = $( "#autocomplete" ).autocomplete();
+ expect( 7 );
+ var detached = $( "<div>" ),
+ element = $( "#autocomplete" ).autocomplete();
equal( element.autocomplete( "widget" ).parent()[0], document.body, "defaults to body" );
element.autocomplete( "destroy" );
@@ -26,6 +27,18 @@ test( "appendTo", function() {
element.autocomplete().autocomplete( "option", "appendTo", "#ac-wrap1" );
equal( element.autocomplete( "widget" ).parent()[0], $( "#ac-wrap1" )[0], "modified after init" );
element.autocomplete( "destroy" );
+
+ element.autocomplete({
+ appendTo: detached
+ });
+ equal( element.autocomplete( "widget" ).parent()[0], detached[0], "detached jQuery object" );
+ element.autocomplete( "destroy" );
+
+ element.autocomplete({
+ appendTo: detached[0]
+ });
+ equal( element.autocomplete( "widget" ).parent()[0], detached[0], "detached DOM element" );
+ element.autocomplete( "destroy" );
});
function autoFocusTest( afValue, focusedLength ) {
diff --git a/ui/jquery.ui.autocomplete.js b/ui/jquery.ui.autocomplete.js
index f2a7089c4..5edb84d44 100644
--- a/ui/jquery.ui.autocomplete.js
+++ b/ui/jquery.ui.autocomplete.js
@@ -181,7 +181,7 @@ $.widget( "ui.autocomplete", {
this._initSource();
this.menu = $( "<ul>" )
.addClass( "ui-autocomplete" )
- .appendTo( this.document.find( this.options.appendTo || "body" )[ 0 ] )
+ .appendTo( this._appendTo() )
.menu({
// custom key handling for now
input: $(),
@@ -320,6 +320,14 @@ $.widget( "ui.autocomplete", {
}
},
+ _appendTo: function() {
+ var element = this.options.appendTo;
+ if ( element && (element.jquery || element.nodeType) ) {
+ return $( element );
+ }
+ return this.document.find( element || "body" ).eq( 0 );
+ },
+
_isMultiLine: function() {
// Textareas are always multi-line
if ( this.element.is( "textarea" ) ) {