Bug 1390342 - Skip pattern matching in HasPatternMismatch() if @pattern is not set. r=ehsan
authorJessica Jong <jjong@mozilla.com>
Thu, 17 Aug 2017 23:57:00 -0400
changeset 375470 9c5af0df60162cf5507febf5bccb6f13dc4747ca
parent 375469 2b57f7b4b3d9513f693419c02fcb1855ffcd117f
child 375471 f192dae121a0721d8a67ed2ea4516c41773a2ab1
push id93928
push userryanvm@gmail.com
push dateFri, 18 Aug 2017 14:10:24 +0000
treeherdermozilla-inbound@f192dae121a0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1390342
milestone57.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 1390342 - Skip pattern matching in HasPatternMismatch() if @pattern is not set. r=ehsan We use a bitfield in HTMLInputElement to cache whether the input element has the @pattern set, and skip pattern mathching in HasPatternMismatch() if the attribute is not set.
dom/html/HTMLInputElement.cpp
dom/html/HTMLInputElement.h
dom/html/input/SingleLineTextInputTypes.cpp
--- a/dom/html/HTMLInputElement.cpp
+++ b/dom/html/HTMLInputElement.cpp
@@ -1144,16 +1144,17 @@ HTMLInputElement::HTMLInputElement(alrea
   , mCanShowInvalidUI(true)
   , mHasRange(false)
   , mIsDraggingRange(false)
   , mNumberControlSpinnerIsSpinning(false)
   , mNumberControlSpinnerSpinsUp(false)
   , mPickerRunning(false)
   , mSelectionCached(true)
   , mIsPreviewEnabled(false)
+  , mHasPatternAttribute(false)
 {
   // If size is above 512, mozjemalloc allocates 1kB, see
   // memory/mozjemalloc/jemalloc.c
   static_assert(sizeof(HTMLInputElement) <= 512,
                 "Keep the size of HTMLInputElement under 512 to avoid "
                 "performance regression!");
 
   // We are in a type=text so we now we currenty need a nsTextEditorState.
@@ -1452,18 +1453,25 @@ HTMLInputElement::AfterSetAttr(int32_t a
       // This *has* to be called *after* validity has changed.
       if (aName == nsGkAtoms::readonly || aName == nsGkAtoms::disabled) {
         UpdateBarredFromConstraintValidation();
       }
     } else if (aName == nsGkAtoms::maxlength) {
       UpdateTooLongValidityState();
     } else if (aName == nsGkAtoms::minlength) {
       UpdateTooShortValidityState();
-    } else if (aName == nsGkAtoms::pattern && mDoneCreating) {
-      UpdatePatternMismatchValidityState();
+    } else if (aName == nsGkAtoms::pattern) {
+      // Although pattern attribute only applies to single line text controls,
+      // we set this flag for all input types to save having to check the type
+      // here.
+      mHasPatternAttribute = !!aValue;
+
+      if (mDoneCreating) {
+        UpdatePatternMismatchValidityState();
+      }
     } else if (aName == nsGkAtoms::multiple) {
       UpdateTypeMismatchValidityState();
     } else if (aName == nsGkAtoms::max) {
       UpdateHasRange();
       nsresult rv = mInputType->MinMaxStepAttrChanged();
       NS_ENSURE_SUCCESS(rv, rv);
       // Validity state must be updated *after* the UpdateValueDueToAttrChange
       // call above or else the following assert will not be valid.
--- a/dom/html/HTMLInputElement.h
+++ b/dom/html/HTMLInputElement.h
@@ -334,16 +334,21 @@ public:
     mSelectionCached = false;
   }
   nsTextEditorState::SelectionProperties& GetSelectionProperties()
   {
     MOZ_ASSERT(mType == NS_FORM_INPUT_NUMBER);
     return mSelectionProperties;
   }
 
+  bool HasPatternAttribute() const
+  {
+    return mHasPatternAttribute;
+  }
+
   // nsIConstraintValidation
   bool     IsTooLong();
   bool     IsTooShort();
   bool     IsValueMissing() const;
   bool     HasTypeMismatch() const;
   bool     HasPatternMismatch() const;
   bool     IsRangeOverflow() const;
   bool     IsRangeUnderflow() const;
@@ -1662,16 +1667,17 @@ protected:
   bool                     mCanShowInvalidUI    : 1;
   bool                     mHasRange            : 1;
   bool                     mIsDraggingRange     : 1;
   bool                     mNumberControlSpinnerIsSpinning : 1;
   bool                     mNumberControlSpinnerSpinsUp : 1;
   bool                     mPickerRunning : 1;
   bool                     mSelectionCached : 1;
   bool                     mIsPreviewEnabled : 1;
+  bool                     mHasPatternAttribute : 1;
 
 private:
   static void MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
                                     GenericSpecifiedValues* aGenericData);
 
   /**
    * Returns true if this input's type will fire a DOM "change" event when it
    * loses focus if its value has changed since it gained focus.
--- a/dom/html/input/SingleLineTextInputTypes.cpp
+++ b/dom/html/input/SingleLineTextInputTypes.cpp
@@ -67,18 +67,22 @@ SingleLineTextInputTypeBase::IsValueMiss
   }
 
   return IsValueEmpty();
 }
 
 bool
 SingleLineTextInputTypeBase::HasPatternMismatch() const
 {
+  if (!mInputElement->HasPatternAttribute()) {
+    return false;
+  }
+
   nsAutoString pattern;
- if (!mInputElement->GetAttr(kNameSpaceID_None, nsGkAtoms::pattern, pattern)) {
+  if (!mInputElement->GetAttr(kNameSpaceID_None, nsGkAtoms::pattern, pattern)) {
     return false;
   }
 
   nsAutoString value;
   GetNonFileValueInternal(value);
 
   if (value.IsEmpty()) {
     return false;