Autocomplete: Normalize falsy values, not just missing values

Fixes #9762
This commit is contained in:
Scott González 2014-01-22 10:40:19 -05:00
parent 2ef1b16e4d
commit 113e9d0c2c
5 changed files with 52 additions and 25 deletions

View File

@ -220,7 +220,20 @@ function sourceTest( source, async ) {
}), }),
menu = element.autocomplete( "widget" ); menu = element.autocomplete( "widget" );
function result() { function result() {
equal( menu.find( ".ui-menu-item" ).text(), "javajavascript" ); var items = menu.find( ".ui-menu-item" );
equal( items.length, 3, "Should find three results." );
deepEqual( items.eq( 0 ).data( "ui-autocomplete-item" ), {
label: "java",
value: "java"
});
deepEqual( items.eq( 1 ).data( "ui-autocomplete-item" ), {
label: "javascript",
value: "javascript"
});
deepEqual( items.eq( 2 ).data( "ui-autocomplete-item" ), {
label: "clojure",
value: "clojure"
});
element.autocomplete( "destroy" ); element.autocomplete( "destroy" );
if ( async ) { if ( async ) {
start(); start();
@ -230,52 +243,58 @@ function sourceTest( source, async ) {
stop(); stop();
$( document ).one( "ajaxStop", result ); $( document ).one( "ajaxStop", result );
} }
element.val( "ja" ).autocomplete( "search" ); element.val( "j" ).autocomplete( "search" );
if ( !async ) { if ( !async ) {
result(); result();
} }
} }
test( "source, local object array, only label property", function() { test( "source, local object array, only labels", function() {
expect( 1 ); expect( 4 );
sourceTest([ sourceTest([
{ label: "java" }, { label: "java", value: null },
{ label: "php" }, { label: "php", value: null },
{ label: "coldfusion" }, { label: "coldfusion", value: "" },
{ label: "javascript" } { label: "javascript", value: "" },
{ label: "clojure" }
]); ]);
}); });
test( "source, local object array, only value property", function() { test( "source, local object array, only values", function() {
expect( 1 ); expect( 4 );
sourceTest([ sourceTest([
{ value: "java" }, { value: "java", label: null },
{ value: "php" }, { value: "php", label: null },
{ value: "coldfusion" }, { value: "coldfusion", label: "" },
{ value: "javascript" } { value: "javascript", label: "" },
{ value: "clojure" }
]); ]);
}); });
test( "source, url string with remote json string array", function() { test( "source, url string with remote json string array", function() {
expect( 1 ); expect( 4 );
sourceTest( "remote_string_array.txt", true ); sourceTest( "remote_string_array.txt", true );
}); });
test( "source, url string with remote json object array, only value properties", function() { test( "source, url string with remote json object array, only value properties", function() {
expect( 1 ); expect( 4 );
sourceTest( "remote_object_array_values.txt", true ); sourceTest( "remote_object_array_values.txt", true );
}); });
test( "source, url string with remote json object array, only label properties", function() { test( "source, url string with remote json object array, only label properties", function() {
expect( 1 ); expect( 4 );
sourceTest( "remote_object_array_labels.txt", true ); sourceTest( "remote_object_array_labels.txt", true );
}); });
test( "source, custom", function() { test( "source, custom", function() {
expect( 2 ); expect( 5 );
sourceTest(function( request, response ) { sourceTest(function( request, response ) {
equal( request.term, "ja" ); equal( request.term, "j" );
response( ["java", "javascript"] ); response([
"java",
{ label: "javascript", value: null },
{ value: "clojure", label: null }
]);
}); });
}); });

View File

@ -1 +1,5 @@
[{"label":"java"},{"label":"javascript"}] [
{ "label": "java", "value": null },
{ "label": "javascript", "value": "" },
{ "label": "clojure" }
]

View File

@ -1 +1,5 @@
[{"value":"java"},{"value":"javascript"}] [
{ "value": "java", "label": null },
{ "value": "javascript", "label": "" },
{ "value": "clojure" }
]

View File

@ -1 +1 @@
["java", "javascript"] [ "java", "javascript", "clojure" ]

View File

@ -478,10 +478,10 @@ $.widget( "ui.autocomplete", {
value: item value: item
}; };
} }
return $.extend({ return $.extend( {}, item, {
label: item.label || item.value, label: item.label || item.value,
value: item.value || item.label value: item.value || item.label
}, item ); });
}); });
}, },