Bug 1483738: Part 1 - Change notifyInvalidSubmit to take a plain array. r=mconley
authorKris Maglione <maglione.k@gmail.com>
Thu, 16 Aug 2018 12:20:54 -0700
changeset 487314 6f8bb8f44f2b612c964e1116282917e33168f989
parent 487313 5266762e913c7b8186385023554ac73d324a724a
child 487315 89c81ab0254376e6a7ae4a61fa2607d4ecd7b08b
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley
bugs1483738
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1483738: Part 1 - Change notifyInvalidSubmit to take a plain array. r=mconley This makes things saner for all consumers, which makes it worth doing on its own. But it also gives me an easy nsTArray to work with, which I can use to dispatch DOM events with array properties. Differential Revision: https://phabricator.services.mozilla.com/D3466
browser/modules/FormSubmitObserver.jsm
dom/html/HTMLFormElement.cpp
dom/html/HTMLFormElement.h
dom/html/nsIConstraintValidation.cpp
dom/html/nsIFormSubmitObserver.idl
mobile/android/modules/FormAssistant.jsm
--- a/browser/modules/FormSubmitObserver.jsm
+++ b/browser/modules/FormSubmitObserver.jsm
@@ -74,28 +74,20 @@ FormSubmitObserver.prototype =
     }
   },
 
   /*
    * nsIFormSubmitObserver
    */
 
   notifyInvalidSubmit(aFormElement, aInvalidElements) {
-    // We are going to handle invalid form submission attempt by focusing the
-    // first invalid element and show the corresponding validation message in a
-    // panel attached to the element.
-    if (!aInvalidElements.length) {
-      return;
-    }
-
     // Show a validation message on the first focusable element.
-    for (let i = 0; i < aInvalidElements.length; i++) {
+    for (let element of aInvalidElements) {
       // Insure that this is the FormSubmitObserver associated with the
       // element / window this notification is about.
-      let element = aInvalidElements.queryElementAt(i, Ci.nsISupports);
       if (this._content != element.ownerGlobal.top.document.defaultView) {
         return;
       }
 
       if (!(ChromeUtils.getClassName(element) === "HTMLInputElement" ||
             ChromeUtils.getClassName(element) === "HTMLTextAreaElement" ||
             ChromeUtils.getClassName(element) === "HTMLSelectElement" ||
             ChromeUtils.getClassName(element) === "HTMLButtonElement")) {
--- a/dom/html/HTMLFormElement.cpp
+++ b/dom/html/HTMLFormElement.cpp
@@ -1821,17 +1821,17 @@ HTMLFormElement::ForgetCurrentSubmission
   nsCOMPtr<nsIWebProgress> webProgress = do_QueryReferent(mWebProgress);
   if (webProgress) {
     webProgress->RemoveProgressListener(this);
   }
   mWebProgress = nullptr;
 }
 
 bool
-HTMLFormElement::CheckFormValidity(nsIMutableArray* aInvalidElements) const
+HTMLFormElement::CheckFormValidity(nsTArray<RefPtr<Element>>* aInvalidElements) const
 {
   bool ret = true;
 
   // This shouldn't be called recursively, so use a rather large value
   // for the preallocated buffer.
   AutoTArray<RefPtr<nsGenericHTMLFormElement>, 100> sortedControls;
   if (NS_FAILED(mControls->GetSortedControls(sortedControls))) {
     return false;
@@ -1849,17 +1849,17 @@ HTMLFormElement::CheckFormValidity(nsIMu
                                            static_cast<nsIContent*>(sortedControls[i]),
                                            NS_LITERAL_STRING("invalid"),
                                            CanBubble::eNo, Cancelable::eYes,
                                            &defaultAction);
 
       // Add all unhandled invalid controls to aInvalidElements if the caller
       // requested them.
       if (defaultAction && aInvalidElements) {
-        aInvalidElements->AppendElement(ToSupports(sortedControls[i]));
+        aInvalidElements->AppendElement(sortedControls[i]);
       }
     }
   }
 
   return ret;
 }
 
 bool
@@ -1898,22 +1898,19 @@ HTMLFormElement::CheckValidFormSubmissio
   NS_ENSURE_SUCCESS(rv, true);
 
   bool hasObserver = false;
   rv = theEnum->HasMoreElements(&hasObserver);
 
   // Do not check form validity if there is no observer for
   // NS_INVALIDFORMSUBMIT_SUBJECT.
   if (NS_SUCCEEDED(rv) && hasObserver) {
-    nsCOMPtr<nsIMutableArray> invalidElements =
-      do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
-    // Return true on error here because that's what we always did
-    NS_ENSURE_SUCCESS(rv, true);
+    AutoTArray<RefPtr<Element>, 32> invalidElements;
 
-    if (!CheckFormValidity(invalidElements.get())) {
+    if (!CheckFormValidity(&invalidElements)) {
       // For the first invalid submission, we should update element states.
       // We have to do that _before_ calling the observers so we are sure they
       // will not interfere (like focusing the element).
       if (!mEverTriedInvalidSubmit) {
         mEverTriedInvalidSubmit = true;
 
         /*
          * We are going to call update states assuming elements want to
@@ -1953,18 +1950,17 @@ HTMLFormElement::CheckValidFormSubmissio
       nsCOMPtr<nsISupports> inst;
       nsCOMPtr<nsIFormSubmitObserver> observer;
       bool more = true;
       while (NS_SUCCEEDED(theEnum->HasMoreElements(&more)) && more) {
         theEnum->GetNext(getter_AddRefs(inst));
         observer = do_QueryInterface(inst);
 
         if (observer) {
-          observer->NotifyInvalidSubmit(this,
-                                        static_cast<nsIArray*>(invalidElements));
+          observer->NotifyInvalidSubmit(this, invalidElements);
         }
       }
 
       // The form is invalid. Observers have been alerted. Do not submit.
       return false;
     }
   } else {
     NS_WARNING("There is no observer for \"invalidformsubmit\". \
--- a/dom/html/HTMLFormElement.h
+++ b/dom/html/HTMLFormElement.h
@@ -512,17 +512,17 @@ protected:
    * Check the form validity following this algorithm:
    * http://www.whatwg.org/specs/web-apps/current-work/#statically-validate-the-constraints
    *
    * @param aInvalidElements [out] parameter containing the list of unhandled
    * invalid controls.
    *
    * @return Whether the form is currently valid.
    */
-  bool CheckFormValidity(nsIMutableArray* aInvalidElements) const;
+  bool CheckFormValidity(nsTArray<RefPtr<Element>>* aInvalidElements) const;
 
   // Clear the mImageNameLookupTable and mImageElements.
   void Clear();
 
   // Insert a element into the past names map.
   void AddToPastNamesMap(const nsAString& aName, nsISupports* aChild);
 
   // Remove the given element from the past names map.  The element must be an
--- a/dom/html/nsIConstraintValidation.cpp
+++ b/dom/html/nsIConstraintValidation.cpp
@@ -140,16 +140,19 @@ nsIConstraintValidation::ReportValidity(
                                        NS_LITERAL_STRING("invalid"),
                                        CanBubble::eNo,
                                        Cancelable::eYes,
                                        &defaultAction);
   if (!defaultAction) {
     return false;
   }
 
+  AutoTArray<RefPtr<Element>, 1> invalidElements;
+  invalidElements.AppendElement(element);
+
   nsCOMPtr<nsIObserverService> service =
     mozilla::services::GetObserverService();
   if (!service) {
     NS_WARNING("No observer service available!");
     return true;
   }
 
   nsCOMPtr<nsISimpleEnumerator> theEnum;
@@ -157,20 +160,16 @@ nsIConstraintValidation::ReportValidity(
                                             getter_AddRefs(theEnum));
 
   // Return true on error here because that's what we always did
   NS_ENSURE_SUCCESS(rv, true);
 
   bool hasObserver = false;
   rv = theEnum->HasMoreElements(&hasObserver);
 
-  nsCOMPtr<nsIMutableArray> invalidElements =
-    do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
-  invalidElements->AppendElement(element);
-
   NS_ENSURE_SUCCESS(rv, true);
   nsCOMPtr<nsISupports> inst;
   nsCOMPtr<nsIFormSubmitObserver> observer;
   bool more = true;
   while (NS_SUCCEEDED(theEnum->HasMoreElements(&more)) && more) {
     theEnum->GetNext(getter_AddRefs(inst));
     observer = do_QueryInterface(inst);
 
--- a/dom/html/nsIFormSubmitObserver.idl
+++ b/dom/html/nsIFormSubmitObserver.idl
@@ -6,24 +6,25 @@
 
 #include "nsISupports.idl"
 
 interface mozIDOMWindow;
 interface nsIURI;
 interface nsIArray;
 
 webidl HTMLFormElement;
+webidl Element;
 
 [scriptable, uuid(867cb7e7-835d-408b-9788-d2834d284e03)]
 interface nsIFormSubmitObserver: nsISupports
 {
   void notify(in HTMLFormElement formNode, in mozIDOMWindow window, in nsIURI actionURL, out boolean cancelSubmit);
 
   void notifyInvalidSubmit(in HTMLFormElement formNode,
-                           in nsIArray invalidElements);
+                           in Array<Element> invalidElements);
 };
 
 %{C++
 #define NS_FORMSUBMIT_SUBJECT "formsubmit"
 #define NS_EARLYFORMSUBMIT_SUBJECT "earlyformsubmit"
 #define NS_FIRST_FORMSUBMIT_CATEGORY "firstformsubmit"
 #define NS_PASSWORDMANAGER_CATEGORY "passwordmanager"
 #define NS_INVALIDFORMSUBMIT_SUBJECT "invalidformsubmit"
--- a/mobile/android/modules/FormAssistant.jsm
+++ b/mobile/android/modules/FormAssistant.jsm
@@ -89,17 +89,17 @@ var FormAssistant = {
   },
 
   notifyInvalidSubmit: function(aFormElement, aInvalidElements) {
     if (!aInvalidElements.length) {
         return;
     }
 
     // Ignore this notificaiton if the current tab doesn't contain the invalid element
-    let currentElement = aInvalidElements.queryElementAt(0, Ci.nsISupports);
+    let currentElement = aInvalidElements[0];
     let focused = this.focusedElement;
     if (focused && focused.ownerGlobal.top !== currentElement.ownerGlobal.top) {
         return;
     }
 
     // Our focus listener will show the element's validation message
     currentElement.focus();
   },