Bug 605125 (4/5) - :-moz-ui-valid applies if the user tried to submit the form in an invalid state. r=bz a=jst
authorMounir Lamouri <mounir.lamouri@gmail.com>
Wed, 24 Nov 2010 00:55:53 +0100
changeset 58154 8761f71f04b5a0596226c9051d27412b1e1e517d
parent 58153 b47d30240391c05348822bf108245a560b0adb8e
child 58155 c2d742db6304d51cb9a476d487cca530aca85ae7
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersbz, jst
bugs605125
milestone2.0b8pre
Bug 605125 (4/5) - :-moz-ui-valid applies if the user tried to submit the form in an invalid state. r=bz a=jst
content/html/content/src/nsHTMLFormElement.cpp
content/html/content/src/nsHTMLInputElement.cpp
content/html/content/src/nsHTMLTextAreaElement.cpp
content/html/content/test/Makefile.in
content/html/content/test/test_bug605125.html
--- a/content/html/content/src/nsHTMLFormElement.cpp
+++ b/content/html/content/src/nsHTMLFormElement.cpp
@@ -495,19 +495,20 @@ CollectOrphans(nsINode* aRemovalRoot, ns
 #ifdef DEBUG
     PRBool removed = PR_FALSE;
 #endif
     if (node->HasFlag(MAYBE_ORPHAN_FORM_ELEMENT)) {
       node->UnsetFlags(MAYBE_ORPHAN_FORM_ELEMENT);
       if (!nsContentUtils::ContentIsDescendantOf(node, aRemovalRoot)) {
         node->ClearForm(PR_TRUE);
 
-        // When a form control loses its form owner,
-        // NS_EVENT_STATE_MOZ_UI_INVALID might not apply any more.
-        nsEventStates states = NS_EVENT_STATE_MOZ_UI_INVALID;
+        // When a form control loses its form owner, :-moz-ui-invalid and
+        // :-moz-ui-valid might not apply any more.
+        nsEventStates states = NS_EVENT_STATE_MOZ_UI_VALID |
+                               NS_EVENT_STATE_MOZ_UI_INVALID;
 
         // In addition, submit controls shouldn't have
         // NS_EVENT_STATE_MOZ_SUBMITINVALID applying if they do not have a form.
         if (node->IsSubmitControl()) {
           states |= NS_EVENT_STATE_MOZ_SUBMITINVALID;
         }
 
         if (doc) {
@@ -1720,25 +1721,27 @@ nsHTMLFormElement::CheckValidFormSubmiss
            * Submissions shouldn't happen during parsing so it _should_ be safe.
            */
 
           MOZ_AUTO_DOC_UPDATE(doc, UPDATE_CONTENT_STATE, PR_TRUE);
 
           for (PRUint32 i = 0, length = mControls->mElements.Length();
                i < length; ++i) {
             doc->ContentStatesChanged(mControls->mElements[i], nsnull,
+                                      NS_EVENT_STATE_MOZ_UI_VALID |
                                       NS_EVENT_STATE_MOZ_UI_INVALID);
           }
 
           // Because of backward compatibility, <input type='image'> is not in
           // elements but can be invalid.
           // TODO: should probably be removed when bug 606491 will be fixed.
           for (PRUint32 i = 0, length = mControls->mNotInElements.Length();
                i < length; ++i) {
             doc->ContentStatesChanged(mControls->mNotInElements[i], nsnull,
+                                      NS_EVENT_STATE_MOZ_UI_VALID |
                                       NS_EVENT_STATE_MOZ_UI_INVALID);
           }
         }
       }
 
       nsCOMPtr<nsISupports> inst;
       nsCOMPtr<nsIFormSubmitObserver> observer;
       PRBool more = PR_TRUE;
--- a/content/html/content/src/nsHTMLInputElement.cpp
+++ b/content/html/content/src/nsHTMLInputElement.cpp
@@ -3320,17 +3320,18 @@ nsHTMLInputElement::IntrinsicState() con
 
   if (IsCandidateForConstraintValidation()) {
     if (IsValid()) {
       state |= NS_EVENT_STATE_VALID;
 
       // NS_EVENT_STATE_MOZ_UI_VALID applies if the value has been changed.
       // This doesn't apply to elements with value mode default.
       ValueModeType valueMode = GetValueMode();
-      if (valueMode == VALUE_MODE_DEFAULT ||
+      if ((mForm && mForm->HasEverTriedInvalidSubmit()) ||
+          valueMode == VALUE_MODE_DEFAULT ||
           (valueMode == VALUE_MODE_DEFAULT_ON && GetCheckedChanged()) ||
           ((valueMode == VALUE_MODE_VALUE ||
             valueMode == VALUE_MODE_FILENAME) && GetValueChanged())) {
         state |= NS_EVENT_STATE_MOZ_UI_VALID;
       }
     } else {
       state |= NS_EVENT_STATE_INVALID;
 
--- a/content/html/content/src/nsHTMLTextAreaElement.cpp
+++ b/content/html/content/src/nsHTMLTextAreaElement.cpp
@@ -1054,17 +1054,17 @@ nsHTMLTextAreaElement::IntrinsicState() 
     state |= NS_EVENT_STATE_REQUIRED;
   } else {
     state |= NS_EVENT_STATE_OPTIONAL;
   }
 
   if (IsCandidateForConstraintValidation()) {
     if (IsValid()) {
       state |= NS_EVENT_STATE_VALID;
-      if (mValueChanged) {
+      if ((mForm && mForm->HasEverTriedInvalidSubmit()) || mValueChanged) {
         state |= NS_EVENT_STATE_MOZ_UI_VALID;
       }
     } else {
       state |= NS_EVENT_STATE_INVALID;
       // NS_EVENT_STATE_MOZ_UI_INVALID always apply if the element suffers from
       // VALIDITY_STATE_CUSTOM_ERROR.
       // Otherwise, it applies if the value has been modified.
       // NS_EVENT_STATE_MOZ_UI_INVALID always applies if the form submission has
--- a/content/html/content/test/Makefile.in
+++ b/content/html/content/test/Makefile.in
@@ -240,12 +240,13 @@ include $(topsrcdir)/config/rules.mk
 		file_bug297761.html \
 		test_bug607145.html \
 		test_bug601061.html \
 		test_bug596511.html \
 		reflect.js \
 		test_bug613113.html \
 		test_bug605124-1.html \
 		test_bug605124-2.html \
+		test_bug605125.html \
 		$(NULL)
 
 libs:: $(_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
copy from content/html/content/test/test_bug605124-1.html
copy to content/html/content/test/test_bug605125.html
--- a/content/html/content/test/test_bug605124-1.html
+++ b/content/html/content/test/test_bug605125.html
@@ -1,56 +1,66 @@
 <!DOCTYPE HTML>
 <html>
 <!--
-https://bugzilla.mozilla.org/show_bug.cgi?id=605124
+https://bugzilla.mozilla.org/show_bug.cgi?id=605125
 -->
 <head>
-  <title>Test for Bug 605124</title>
+  <title>Test for Bug 605125</title>
   <script type="application/javascript" src="/MochiKit/packed.js"></script>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
-<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=605124">Mozilla Bug 605124</a>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=605125">Mozilla Bug 605125</a>
 <p id="display"></p>
 <div id="content">
   <form>
-    <textarea required></textarea>
-    <input required>
+    <textarea></textarea>
+    <input>
     <button type='submit'></button>
+    <select></select>
   </form>
 
   <table>
     <form>
     <tr>
-      <textarea required></textarea>
-      <input required>
+      <textarea></textarea>
+      <input>
       <button type='submit'></button>
+      <select></select>
     </tr>
     </form>
   </table>
 </div>
 <pre id="test">
 <script type="application/javascript">
 
-/** Test for Bug 605124 **/
+/** Test for Bug 605125 **/
+
+/**
+ * NOTE: this test is very similar to 605124-1.html.
+ */
 
 function checkPseudoClass(aElement, aExpected)
 {
-  is(aElement.mozMatchesSelector(":-moz-ui-invalid"), aExpected,
-     "mozMatchesSelector(':-moz-ui-invalid') should return " + aExpected + " for " + aElement);
+  is(aElement.mozMatchesSelector(":-moz-ui-valid"), aExpected,
+     "mozMatchesSelector(':-moz-ui-valid') should return " + aExpected + " for " + aElement);
 }
 
 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
 var os = Components.classes['@mozilla.org/observer-service;1']
                    .getService(Components.interfaces.nsIObserverService);
 var observers = os.enumerateObservers("invalidformsubmit");
 
 if (observers.hasMoreElements()) {
+  // Make both <select> invalid so the forms will be invalid.
+  document.getElementsByTagName('select')[0].setCustomValidity("foo");
+  document.getElementsByTagName('select')[1].setCustomValidity("foo");
+
   var content = document.getElementById('content');
   var textarea = document.getElementsByTagName('textarea')[0];
   var input = document.getElementsByTagName('input')[0];
   var button = document.getElementsByTagName('button')[0];
   var form = document.forms[0];
 
   checkPseudoClass(textarea, false);
   checkPseudoClass(input, false);