Bug 596511 (2/3) - Have :required apply on <select> when appropriate. r=sicking a=blocking-betaN
authorMounir Lamouri <mounir.lamouri@gmail.com>
Tue, 16 Nov 2010 15:40:21 +0100
changeset 57619 ccc6520fee81946a711e6770ea5e579f7fa68bde
parent 57618 96df198710281a5e07cda083716bde54dc19fb60
child 57620 433b919425d6cc473aa3dc3859bd11cda3d9f078
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewerssicking, blocking-betaN
bugs596511
milestone2.0b8pre
Bug 596511 (2/3) - Have :required apply on <select> when appropriate. r=sicking a=blocking-betaN
content/html/content/src/nsHTMLSelectElement.cpp
content/html/content/test/test_bug596511.html
layout/reftests/css-required/css-required-select-ref.html
layout/reftests/css-required/reftest.list
--- a/content/html/content/src/nsHTMLSelectElement.cpp
+++ b/content/html/content/src/nsHTMLSelectElement.cpp
@@ -1495,17 +1495,23 @@ nsEventStates
 nsHTMLSelectElement::IntrinsicState() const
 {
   nsEventStates state = nsGenericHTMLFormElement::IntrinsicState();
 
   if (IsCandidateForConstraintValidation()) {
     state |= IsValid() ? NS_EVENT_STATE_VALID : NS_EVENT_STATE_INVALID;
   }
 
-  return state | NS_EVENT_STATE_OPTIONAL;
+  if (HasAttr(kNameSpaceID_None, nsGkAtoms::required)) {
+    state |= NS_EVENT_STATE_REQUIRED;
+  } else {
+    state |= NS_EVENT_STATE_OPTIONAL;
+  }
+
+  return state;
 }
 
 // nsIFormControl
 
 NS_IMETHODIMP
 nsHTMLSelectElement::SaveState()
 {
   nsRefPtr<nsSelectState> state = new nsSelectState();
--- a/content/html/content/test/test_bug596511.html
+++ b/content/html/content/test/test_bug596511.html
@@ -40,16 +40,32 @@ function checkRequiredAttribute(element)
     "select required attribute should be enabled");
 
   element.removeAttribute('required');
   ok(!element.required, "select required attribute should be disabled");
   is(element.getAttribute('required'), null,
     "select required attribute should be disabled");
 }
 
+function checkRequiredAndOptionalSelectors(element)
+{
+  is(document.querySelector("select:optional"), element, "select should be optional");
+  is(document.querySelector("select:required"), null, "select shouldn't be required");
+
+  element.required = true;
+
+  is(document.querySelector("select:optional"), null, "select shouldn't be optional");
+  is(document.querySelector("select:required"), element, "select should be required");
+
+  element.required = false;
+}
+
 var select = document.createElement("select");
+var content = document.getElementById('content');
+content.appendChild(select);
 
 checkRequiredAttribute(select);
+checkRequiredAndOptionalSelectors(select);
 
 </script>
 </pre>
 </body>
 </html>
new file mode 100644
--- /dev/null
+++ b/layout/reftests/css-required/css-required-select-ref.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<html>
+  <body>
+    <select></select>
+  </body>
+</html>
--- a/layout/reftests/css-required/reftest.list
+++ b/layout/reftests/css-required/reftest.list
@@ -2,16 +2,17 @@
 == css-required-textarea.html css-required-textarea-ref.html
 == css-required-radio.html css-required-radio-ref.html
 == css-required-text.html css-required-text-ref.html
 == css-required-checkbox.html css-required-checkbox-ref.html
 == css-required-file.html css-required-file-ref.html
 == css-required-password.html css-required-password-ref.html
 == css-required-tel.html css-required-tel-ref.html
 == css-required-search.html css-required-search-ref.html
+== css-required-select.html css-required-select-ref.html
 
 # Checking dynamic behavior with <input type='text'>
 == css-required-dyn-1.html css-required-text-ref.html
 == css-required-dyn-2.html about:blank
 == css-required-dyn-3.html css-required-text-ref.html
 == css-required-dyn-4.html about:blank
 == css-required-dyn-5.html css-required-text-ref.html
 == css-required-dyn-6.html about:blank
@@ -19,11 +20,10 @@
 # Following input types do not support :required
 == css-required-hidden.html about:blank
 == css-required-button.html about:blank
 == css-required-submit.html about:blank
 == css-required-image.html about:blank
 == css-required-reset.html about:blank
 
 # Following elements can be optional but can't be required
-== css-required-select.html about:blank
 == css-required-button-element.html about:blank