[Forms] Default selection of select(menulist) should not be disabled
https://bugs.webkit.org/show_bug.cgi?id=74270

Patch by Yosifumi Inoue <yosin@chromium.org> on 2011-12-13
Reviewed by Kent Tamura.

Source/WebCore:

This patch changes default selection of select(menulist) element to
  1. Selected option element. If there are multiple options which have
     selected state, we pick the largest index option up. (same as current)
  2. Non-disabled option element (new behavior)
  3. The first option if all options are disabled. (new behavior)

Tests: fast/forms/select/menulist-disabled-option-expected.html
       fast/forms/select/menulist-disabled-option.html

* html/HTMLSelectElement.cpp:
(WebCore::HTMLSelectElement::recalcListItems): Implement new logic for selection.

LayoutTests:

basic-selects.html will fail because select element displays disabled
option element "foo" at index 0 as default selection. This should be
non-disabled option "bar" at index 1.

menulist-disabled-option-expected.html checks default selection of
select(menulist) with disabled option element.

* fast/forms/select/menulist-disabled-option-expected.html: Added.
* fast/forms/select/menulist-disabled-option.html: Added.
* platform/chromium/test_expectations.txt: Add fast/forms/basic-selects.html
* platform/gtk/test_expectations.txt: Add fast/forms/basic-selects.html
* platform/mac/test_expectations.txt: Add fast/forms/basic-selects.html
* platform/qt/test_expectations.txt: Add fast/forms/basic-selects.html
* platform/win/test_expectations.txt: Add fast/forms/basic-selects.html

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@102741 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index 8d62805..8c16239 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,25 @@
+2011-12-13  Yosifumi Inoue  <yosin@chromium.org>
+
+        [Forms] Default selection of select(menulist) should not be disabled
+        https://bugs.webkit.org/show_bug.cgi?id=74270
+
+        Reviewed by Kent Tamura.
+
+        basic-selects.html will fail because select element displays disabled
+        option element "foo" at index 0 as default selection. This should be
+        non-disabled option "bar" at index 1.
+
+        menulist-disabled-option-expected.html checks default selection of
+        select(menulist) with disabled option element.
+
+        * fast/forms/select/menulist-disabled-option-expected.html: Added.
+        * fast/forms/select/menulist-disabled-option.html: Added.
+        * platform/chromium/test_expectations.txt: Add fast/forms/basic-selects.html
+        * platform/gtk/test_expectations.txt: Add fast/forms/basic-selects.html
+        * platform/mac/test_expectations.txt: Add fast/forms/basic-selects.html
+        * platform/qt/test_expectations.txt: Add fast/forms/basic-selects.html
+        * platform/win/test_expectations.txt: Add fast/forms/basic-selects.html
+
 2011-12-13  Hajime Morrita  <morrita@chromium.org>
 
         HTML details summary not working with form controls
diff --git a/LayoutTests/fast/forms/select/menulist-disabled-option-expected.html b/LayoutTests/fast/forms/select/menulist-disabled-option-expected.html
new file mode 100644
index 0000000..4d80d8b
--- /dev/null
+++ b/LayoutTests/fast/forms/select/menulist-disabled-option-expected.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html>
+<body>
+<h1>Webkit Bug <a href="https://bugs.webkit.org/show_bug.cgi?id=74270">74270</a></h1>
+<select name="three">
+<option value="3">Three</option>
+<option value="4">Four</option>
+</select>
+<br />
+<select name="two">
+<option value="1">One</option>
+<option value="2" selected>Two</option>
+<option value="3">Three</option>
+<option value="4">Four</option>
+</select>
+</body>
+</html>
diff --git a/LayoutTests/fast/forms/select/menulist-disabled-option.html b/LayoutTests/fast/forms/select/menulist-disabled-option.html
new file mode 100644
index 0000000..dd9ae6b
--- /dev/null
+++ b/LayoutTests/fast/forms/select/menulist-disabled-option.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<body>
+<h1>Webkit Bug <a href="https://bugs.webkit.org/show_bug.cgi?id=74270">74270</a></h1>
+<select name="three">
+<option value="1" disabled>One</option>
+<option value="2" disabled>Two</option>
+<option value="3">Three</option>
+<option value="4">Four</option>
+</select>
+<br />
+<select name="two">
+<option value="1" disabled>One</option>
+<option value="2" disabled selected>Two</option>
+<option value="3">Three</option>
+<option value="4">Four</option>
+</select>
+</body>
+</html>
diff --git a/LayoutTests/platform/chromium/test_expectations.txt b/LayoutTests/platform/chromium/test_expectations.txt
index 82d909a..d764c1d 100644
--- a/LayoutTests/platform/chromium/test_expectations.txt
+++ b/LayoutTests/platform/chromium/test_expectations.txt
@@ -1106,7 +1106,6 @@
 BUGWK70748 CPU : fast/css/box-shadow-and-border-radius.html = IMAGE
 BUGWK70748 CPU : fast/css/nested-rounded-corners.html = IMAGE
 BUGWK70748 CPU : fast/css/text-input-with-webkit-border-radius.html = IMAGE
-BUGWK70748 CPU : fast/forms/basic-selects.html = IMAGE
 BUGWK70748 CPU : fast/forms/validation-message-appearance.html = IMAGE
 BUGWK70748 CPU : fast/repaint/shadow-multiple-horizontal.html = IMAGE
 BUGWK70748 CPU : fast/repaint/shadow-multiple-strict-horizontal.html = IMAGE
@@ -3954,6 +3953,8 @@
 
 BUGWK73838 LEOPARD DEBUG : animations/animation-add-events-in-handler.html = CRASH
 
+BUGWK74270 : fast/forms/basic-selects.html = FAIL
+
 // Need rebaseline on Linux and Win GPU
 BUGWK73905 GPU LINUX : media/video-layer-crash.html = IMAGE+TEXT
 BUGWK73905 GPU LINUX : media/video-transformed.html = IMAGE
diff --git a/LayoutTests/platform/gtk/test_expectations.txt b/LayoutTests/platform/gtk/test_expectations.txt
index 1d0579d..effafb9 100644
--- a/LayoutTests/platform/gtk/test_expectations.txt
+++ b/LayoutTests/platform/gtk/test_expectations.txt
@@ -89,3 +89,6 @@
 // End of Flaky tests
 
 BUGWK73766 : css3/unicode-bidi-isolate-aharon-failing.html = FAIL
+
+BUGWK74270 : fast/forms/basic-selects.html = FAIL
+
diff --git a/LayoutTests/platform/mac/test_expectations.txt b/LayoutTests/platform/mac/test_expectations.txt
index b98437c..76063b2 100644
--- a/LayoutTests/platform/mac/test_expectations.txt
+++ b/LayoutTests/platform/mac/test_expectations.txt
@@ -36,3 +36,6 @@
 BUGWK69999 RELEASE : http/tests/security/contentSecurityPolicy/media-src-allowed.html = TEXT PASS
 
 BUGWK73766 : css3/unicode-bidi-isolate-aharon-failing.html = FAIL
+
+BUGWK74270 : fast/forms/basic-selects.html = FAIL
+
diff --git a/LayoutTests/platform/qt/test_expectations.txt b/LayoutTests/platform/qt/test_expectations.txt
index 084c8af..67c060e 100644
--- a/LayoutTests/platform/qt/test_expectations.txt
+++ b/LayoutTests/platform/qt/test_expectations.txt
@@ -19,3 +19,6 @@
 BUGWK62662 DEBUG : inspector/cookie-parser.html = CRASH PASS
 
 BUGWK73766 : css3/unicode-bidi-isolate-aharon-failing.html = FAIL
+
+BUGWK74270 : fast/forms/basic-selects.html = FAIL
+
diff --git a/LayoutTests/platform/win/test_expectations.txt b/LayoutTests/platform/win/test_expectations.txt
index 9ccfee4..46bda86 100644
--- a/LayoutTests/platform/win/test_expectations.txt
+++ b/LayoutTests/platform/win/test_expectations.txt
@@ -14,3 +14,6 @@
 BUGWK65361 : fast/forms/input-disabled-color.html = IMAGE+TEXT
 
 BUGWK73766 : css3/unicode-bidi-isolate-aharon-failing.html = FAIL
+
+BUGWK74270 : fast/forms/basic-selects.html = FAIL
+
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index bda9a51..bf6241a 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,22 @@
+2011-12-13  Yosifumi Inoue  <yosin@chromium.org>
+
+        [Forms] Default selection of select(menulist) should not be disabled
+        https://bugs.webkit.org/show_bug.cgi?id=74270
+
+        Reviewed by Kent Tamura.
+
+        This patch changes default selection of select(menulist) element to
+          1. Selected option element. If there are multiple options which have
+             selected state, we pick the largest index option up. (same as current)
+          2. Non-disabled option element (new behavior)
+          3. The first option if all options are disabled. (new behavior)
+
+        Tests: fast/forms/select/menulist-disabled-option-expected.html
+               fast/forms/select/menulist-disabled-option.html
+
+        * html/HTMLSelectElement.cpp:
+        (WebCore::HTMLSelectElement::recalcListItems): Implement new logic for selection.
+
 2011-12-13  Simon Fraser  <simon.fraser@apple.com>
 
         Share blend progress code
diff --git a/Source/WebCore/html/HTMLSelectElement.cpp b/Source/WebCore/html/HTMLSelectElement.cpp
index 593bd85..d6523c1c 100644
--- a/Source/WebCore/html/HTMLSelectElement.cpp
+++ b/Source/WebCore/html/HTMLSelectElement.cpp
@@ -691,6 +691,7 @@
     m_shouldRecalcListItems = false;
 
     HTMLOptionElement* foundSelected = 0;
+    HTMLOptionElement* firstOption = 0;
     for (Node* currentNode = this->firstChild(); currentNode;) {
         if (!currentNode->isHTMLElement()) {
             currentNode = currentNode->traverseNextSibling(this);
@@ -714,12 +715,16 @@
             m_listItems.append(current);
 
             if (updateSelectedStates && !m_multiple) {
-                if (!foundSelected && (m_size <= 1 || toHTMLOptionElement(current)->selected())) {
-                    foundSelected = toHTMLOptionElement(current);
+                HTMLOptionElement* option = toHTMLOptionElement(current);
+                if (!firstOption)
+                    firstOption = option;
+                if (option->selected()) {
+                    if (foundSelected)
+                        foundSelected->setSelectedState(false);
+                    foundSelected = option;
+                } else if (m_size <= 1 && !foundSelected && !option->disabled()) {
+                    foundSelected = option;
                     foundSelected->setSelectedState(true);
-                } else if (foundSelected && toHTMLOptionElement(current)->selected()) {
-                    foundSelected->setSelectedState(false);
-                    foundSelected = toHTMLOptionElement(current);
                 }
             }
         }
@@ -735,6 +740,9 @@
         // <select>'s subtree at this point.
         currentNode = currentNode->traverseNextSibling(this);
     }
+
+    if (!foundSelected && m_size <= 1 && firstOption && !firstOption->selected())
+        firstOption->setSelectedState(true);
 }
 
 int HTMLSelectElement::selectedIndex() const