]> source.dussan.org Git - jquery-ui.git/commitdiff
Button: find associated label even without common ancestor. Fixes #7092 - button...
authorddstreet <ddstreet@ieee.org>
Fri, 11 Mar 2011 15:52:13 +0000 (10:52 -0500)
committerScott González <scott.gonzalez@gmail.com>
Fri, 11 Mar 2011 15:54:18 +0000 (10:54 -0500)
(cherry picked from commit 0b30a1d450f6b7d529732d6a39c5f4057efaaaf4)

tests/unit/button/button_tickets.js
ui/jquery.ui.button.js

index 5c5053ded550e2c50ba60d99f327636dd1847208..79bbfeb5c0a731ad965ad435fbc00e07c7d47447 100644 (file)
@@ -20,4 +20,26 @@ test( "#6262 - buttonset not applying ui-corner to invisible elements", function
        ok( set.find( "label:eq(2)" ).is( ".ui-button.ui-corner-right" ) );
 });
 
+test( "#7092 - button creation that requires a matching label does not find label in all cases", function() {
+       var group = $( "<span><label for='t7092a'/><input type='checkbox' id='t7092a'/></span>" );
+       group.find( "input:checkbox" ).button();
+       ok( group.find( "label" ).is( ".ui-button" ) );
+
+       group = $( "<input type='checkbox' id='t7092b'/><label for='t7092b'/>" );
+       group.filter( "input:checkbox" ).button();
+       ok( group.filter( "label" ).is( ".ui-button" ) );
+
+       group = $( "<span><input type='checkbox' id='t7092c'/></span><label for='t7092c'/>" );
+       group.find( "input:checkbox" ).button();
+       ok( group.filter( "label" ).is( ".ui-button" ) );
+
+       group = $( "<span><input type='checkbox' id='t7092d'/></span><span><label for='t7092d'/></span>" );
+       group.find( "input:checkbox" ).button();
+       ok( group.find( "label" ).is( ".ui-button" ) );
+
+       group = $( "<input type='checkbox' id='t7092e'/><span><label for='t7092e'/></span>" );
+       group.filter( "input:checkbox" ).button();
+       ok( group.find( "label" ).is( ".ui-button" ) );
+});
+
 })( jQuery );
index fc4c9aa6c3c468c91a0c5d09e2ded8b0824d21df..56e0d2d223c1b53ef89d097099492cdb7b5cd1fe 100644 (file)
@@ -200,8 +200,16 @@ $.widget( "ui.button", {
                if ( this.type === "checkbox" || this.type === "radio" ) {
                        // we don't search against the document in case the element
                        // is disconnected from the DOM
-                       this.buttonElement = this.element.parents().last()
-                               .find( "label[for=" + this.element.attr("id") + "]" );
+                       var ancestor = this.element.parents().last(),
+                               labelSelector = "label[for=" + this.element.attr("id") + "]";
+                       this.buttonElement = ancestor.find( labelSelector );
+                       if ( !this.buttonElement.length ) {
+                               ancestor = ancestor.length ? ancestor.siblings() : this.element.siblings();
+                               this.buttonElement = ancestor.filter( labelSelector );
+                               if ( !this.buttonElement.length ) {
+                                       this.buttonElement = ancestor.find( labelSelector );
+                               }
+                       }
                        this.element.addClass( "ui-helper-hidden-accessible" );
 
                        var checked = this.element.is( ":checked" );