Labels: Handle empty sets

Fixes #15184
This commit is contained in:
Scott González 2017-05-02 12:42:49 -04:00
parent 0d25a36eec
commit adcf9b6f6e
2 changed files with 7 additions and 1 deletions

View File

@ -142,7 +142,7 @@ QUnit.test( "uniqueId / removeUniqueId", function( assert ) {
} ); } );
QUnit.test( "Labels", function( assert ) { QUnit.test( "Labels", function( assert ) {
assert.expect( 2 ); assert.expect( 3 );
var expected = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]; var expected = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ];
var dom = $( "#labels-fragment" ); var dom = $( "#labels-fragment" );
@ -165,6 +165,8 @@ QUnit.test( "Labels", function( assert ) {
// Detach the dom to test on a fragment // Detach the dom to test on a fragment
dom.detach(); dom.detach();
testLabels( "document fragments" ); testLabels( "document fragments" );
assert.equal( $().labels().length, 0, "No element" );
} ); } );
( function() { ( function() {

View File

@ -27,6 +27,10 @@
return $.fn.labels = function() { return $.fn.labels = function() {
var ancestor, selector, id, labels, ancestors; var ancestor, selector, id, labels, ancestors;
if ( !this.length ) {
return this.pushStack( [] );
}
// Check control.labels first // Check control.labels first
if ( this[ 0 ].labels && this[ 0 ].labels.length ) { if ( this[ 0 ].labels && this[ 0 ].labels.length ) {
return this.pushStack( this[ 0 ].labels ); return this.pushStack( this[ 0 ].labels );