Make sure that selected works in Safari on options in optgroups. Fixes #5701.
authorjeresig <jeresig@gmail.com>
Tue, 22 Dec 2009 20:02:52 +0000 (15:02 -0500)
committerjeresig <jeresig@gmail.com>
Tue, 22 Dec 2009 20:02:52 +0000 (15:02 -0500)
src/attributes.js
test/unit/attributes.js

index d3afa89..30b3205 100644 (file)
@@ -261,12 +261,17 @@ jQuery.extend({
                // Only do all the following if this is a node (faster for style)
                if ( elem.nodeType === 1 ) {
                        // These attributes require special treatment
-                       var special = rspecialurl.test( name );
+                       var special = rspecialurl.test( name ), parent = elem.parentNode;
 
                        // Safari mis-reports the default selected property of a hidden option
                        // Accessing the parent's selectedIndex property fixes it
-                       if ( name === "selected" && elem.parentNode ) {
-                               elem.parentNode.selectedIndex;
+                       if ( name === "selected" && parent ) {
+                               parent.selectedIndex;
+
+                               // Make sure that it also works with optgroups, see #5701
+                               if ( parent.parentNode ) {
+                                       parent.parentNode.selectedIndex;
+                               }
                        }
 
                        // If applicable, access the attribute via the DOM 0 way
index a837428..0692076 100644 (file)
@@ -4,7 +4,7 @@ var bareObj = function(value) { return value; };
 var functionReturningObj = function(value) { return (function() { return value; }); };
 
 test("attr(String)", function() {
-       expect(27);
+       expect(28);
 
        // This one sometimes fails randomly ?!
        equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
@@ -55,6 +55,12 @@ test("attr(String)", function() {
        ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
 
        body.removeAttribute('foo'); // Cleanup
+
+       var select = document.createElement("select"), optgroup = document.createElement("optgroup"), option = document.createElement("option");
+       optgroup.appendChild( option );
+       select.appendChild( optgroup );
+
+       equals( jQuery(option).attr("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
 });
 
 if ( !isLocal ) {