Skip to content

Commit 816b89f

Browse files
author
Bob Leers
committed
Allow labels to be jQuery objects as well as plain strings, for increased flexibility
1 parent ca0b4b8 commit 816b89f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

tests/unit/autocomplete/autocomplete_options.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,28 @@ test( "source, custom", function() {
248248
});
249249
});
250250

251+
test( "custom source with jQuery label object", function() {
252+
expect( 4 );
253+
var element = $( "#autocomplete" ).autocomplete({
254+
source: function( request, response ) {
255+
equal( request.term, "er" );
256+
response([
257+
{ label: $("<strong>er</strong><span>lang</span>"), value: "erlang" },
258+
{ label: $("<span>p</span><strong>er</strong><span>l</span>"), value: "perl" },
259+
// Intentional string, must not be interpreted as HTML
260+
{ label: "<span>ob</span><strong>er</strong><span>on</span>", value: "oberon" }
261+
]);
262+
}
263+
});
264+
element.val( "er" ).autocomplete( "search" );
265+
var menu = $( "#autocomplete" ).autocomplete( "widget" ),
266+
perl = menu.find( ".ui-menu-item span+strong+span" ),
267+
oberon = menu.find( ".ui-menu-item:last-child" );
268+
equal( perl.length, 1 );
269+
equal( perl.parent().text(), "perl" );
270+
equal( oberon.text(), "<span>ob</span><strong>er</strong><span>on</span>" );
271+
});
272+
251273
test( "source, update after init", function() {
252274
expect( 2 );
253275
var element = $( "#autocomplete" ).autocomplete({

ui/jquery.ui.autocomplete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ $.widget( "ui.autocomplete", {
529529

530530
_renderItem: function( ul, item ) {
531531
return $( "<li>" )
532-
.append( $( "<a>" ).text( item.label ) )
532+
.append( $( "<a>" )[item.label instanceof $ ? 'append' : 'text']( item.label ) )
533533
.appendTo( ul );
534534
},
535535

0 commit comments

Comments
 (0)