Bug 1398374 - Part 1 - Add helper method to test for the last non-disabled single line form input. r=bz
authorJan Henning <jh+bugzilla@buttercookie.de>
Sun, 01 Oct 2017 15:57:16 +0200
changeset 427062 ca2a39919f2de2cd16ebd3c75d0e6edff54b98da
parent 427061 cfc393773f0ddbb6a498d2f5170b0039e5513489
child 427063 2bde2497dd3f719be2e21cd59e768757775d3885
push id97
push userfmarier@mozilla.com
push dateSat, 14 Oct 2017 01:12:59 +0000
reviewersbz
bugs1398374
milestone58.0a1
Bug 1398374 - Part 1 - Add helper method to test for the last non-disabled single line form input. r=bz We want to treat those specially in IME handling. MozReview-Commit-ID: 8OI2fDQEiGj
dom/html/HTMLFormElement.cpp
dom/html/HTMLFormElement.h
dom/html/nsIFormControl.h
--- a/dom/html/HTMLFormElement.cpp
+++ b/dom/html/HTMLFormElement.cpp
@@ -1823,24 +1823,37 @@ HTMLFormElement::IsDefaultSubmitElement(
 
 bool
 HTMLFormElement::ImplicitSubmissionIsDisabled() const
 {
   // Input text controls are always in the elements list.
   uint32_t numDisablingControlsFound = 0;
   uint32_t length = mControls->mElements.Length();
   for (uint32_t i = 0; i < length && numDisablingControlsFound < 2; ++i) {
-    if (mControls->mElements[i]->IsSingleLineTextControl(false) ||
-        mControls->mElements[i]->ControlType() == NS_FORM_INPUT_NUMBER) {
+    if (mControls->mElements[i]->IsSingleLineTextOrNumberControl(false)) {
       numDisablingControlsFound++;
     }
   }
   return numDisablingControlsFound != 1;
 }
 
+bool
+HTMLFormElement::IsLastActiveElement(const nsIFormControl* aControl) const
+{
+  NS_PRECONDITION(aControl, "Unexpected call");
+
+  for (auto* element : Reversed(mControls->mElements)) {
+    if (element->IsSingleLineTextOrNumberControl(false) &&
+        !element->IsDisabled()) {
+      return element == aControl;
+    }
+  }
+  return false;
+}
+
 NS_IMETHODIMP
 HTMLFormElement::GetEncoding(nsAString& aEncoding)
 {
   return GetEnctype(aEncoding);
 }
 
 NS_IMETHODIMP
 HTMLFormElement::SetEncoding(const nsAString& aEncoding)
--- a/dom/html/HTMLFormElement.h
+++ b/dom/html/HTMLFormElement.h
@@ -216,16 +216,22 @@ public:
     * Returns true if implicit submission of this form is disabled. For more
     * on implicit submission see:
     *
     * http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#implicit-submission
     */
   bool ImplicitSubmissionIsDisabled() const;
 
   /**
+  * Check whether a given nsIFormControl is the last single line input control
+  * that is not disabled. aControl is expected to not be null.
+  */
+  bool IsLastActiveElement(const nsIFormControl* aControl) const;
+
+  /**
    * Check whether a given nsIFormControl is the default submit
    * element.  This is different from just comparing to
    * GetDefaultSubmitElement() in certain situations inside an update
    * when GetDefaultSubmitElement() might not be up to date.  aControl
    * is expected to not be null.
    */
   bool IsDefaultSubmitElement(const nsIFormControl* aControl) const;
 
--- a/dom/html/nsIFormControl.h
+++ b/dom/html/nsIFormControl.h
@@ -197,16 +197,23 @@ public:
   /**
    * Returns whether this is a single line text control.
    * @param  aExcludePassword  to have NS_FORM_INPUT_PASSWORD returning false.
    * @return whether this is a single line text control.
    */
   inline bool IsSingleLineTextControl(bool aExcludePassword) const;
 
   /**
+  * Returns true if this is a single line text control or a number control.
+  * @param  aExcludePassword  to have NS_FORM_INPUT_PASSWORD returning false.
+  * @return true if this is a single line text control or a number control.
+  */
+  inline bool IsSingleLineTextOrNumberControl(bool aExcludePassword) const;
+
+  /**
    * Returns whether this is a submittable form control.
    * @return whether this is a submittable form control.
    */
   inline bool IsSubmittableControl() const;
 
   /**
    * Returns whether this form control can have draggable children.
    * @return whether this form control can have draggable children.
@@ -260,16 +267,23 @@ nsIFormControl::IsTextOrNumberControl(bo
 }
 
 bool
 nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const
 {
   return IsSingleLineTextControl(aExcludePassword, ControlType());
 }
 
+bool
+nsIFormControl::IsSingleLineTextOrNumberControl(bool aExcludePassword) const
+{
+  return IsSingleLineTextControl(aExcludePassword) ||
+         ControlType() == NS_FORM_INPUT_NUMBER;
+}
+
 /*static*/
 bool
 nsIFormControl::IsSingleLineTextControl(bool aExcludePassword, uint32_t aType)
 {
   return aType == NS_FORM_INPUT_TEXT ||
          aType == NS_FORM_INPUT_EMAIL ||
          aType == NS_FORM_INPUT_SEARCH ||
          aType == NS_FORM_INPUT_TEL ||