Bug 665612 - More cleanup around mInputData and IsSingleLineTextControl(). f=ehsan r=bz
authorMounir Lamouri <mounir.lamouri@gmail.com>
Wed, 22 Jun 2011 12:41:00 +0200
changeset 71513 ff8bd5d40a57621efc07de0ddcd46e155e63ad4b
parent 71512 c2c6a3a55aafe471e216891e9f8ac78c665632ae
child 71514 00290f0912fd991a5ef6ae0015989460236e7796
child 71516 7853e5cf72f7700cfe63e4c9e25315800e2fb52d
push id20557
push usermlamouri@mozilla.com
push dateWed, 22 Jun 2011 14:18:06 +0000
treeherdermozilla-central@00290f0912fd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs665612
milestone7.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 665612 - More cleanup around mInputData and IsSingleLineTextControl(). f=ehsan r=bz
content/html/content/src/nsHTMLInputElement.cpp
--- a/content/html/content/src/nsHTMLInputElement.cpp
+++ b/content/html/content/src/nsHTMLInputElement.cpp
@@ -993,47 +993,52 @@ NS_IMETHODIMP
 nsHTMLInputElement::GetValue(nsAString& aValue)
 {
   return GetValueInternal(aValue);
 }
 
 nsresult
 nsHTMLInputElement::GetValueInternal(nsAString& aValue) const
 {
-  nsTextEditorState* state = GetEditorState();
-  if (state) {
-    state->GetValue(aValue, PR_TRUE);
-    return NS_OK;
-  }
-
-  if (mType == NS_FORM_INPUT_FILE) {
-    if (nsContentUtils::IsCallerTrustedForCapability("UniversalFileRead")) {
-      if (mFiles.Count()) {
-        return mFiles[0]->GetMozFullPath(aValue);
-      }
-      else {
-        aValue.Truncate();
+  switch (GetValueMode()) {
+    case VALUE_MODE_VALUE:
+      mInputData.mState->GetValue(aValue, PR_TRUE);
+      return NS_OK;
+
+    case VALUE_MODE_FILENAME:
+      if (nsContentUtils::IsCallerTrustedForCapability("UniversalFileRead")) {
+        if (mFiles.Count()) {
+          return mFiles[0]->GetMozFullPath(aValue);
+        }
+        else {
+          aValue.Truncate();
+        }
+      } else {
+        // Just return the leaf name
+        if (mFiles.Count() == 0 || NS_FAILED(mFiles[0]->GetName(aValue))) {
+          aValue.Truncate();
+        }
       }
-    } else {
-      // Just return the leaf name
-      if (mFiles.Count() == 0 || NS_FAILED(mFiles[0]->GetName(aValue))) {
-        aValue.Truncate();
+
+      return NS_OK;
+
+    case VALUE_MODE_DEFAULT:
+      // Treat defaultValue as value.
+      GetAttr(kNameSpaceID_None, nsGkAtoms::value, aValue);
+      return NS_OK;
+
+    case VALUE_MODE_DEFAULT_ON:
+      // Treat default value as value and returns "on" if no value.
+      if (!GetAttr(kNameSpaceID_None, nsGkAtoms::value, aValue)) {
+        aValue.AssignLiteral("on");
       }
-    }
-    
-    return NS_OK;
+      return NS_OK;
   }
 
-  // Treat value == defaultValue for other input elements
-  if (!GetAttr(kNameSpaceID_None, nsGkAtoms::value, aValue) &&
-      (mType == NS_FORM_INPUT_RADIO || mType == NS_FORM_INPUT_CHECKBOX)) {
-    // The default value of a radio or checkbox input is "on".
-    aValue.AssignLiteral("on");
-  }
-
+  // This return statement is required for some compilers.
   return NS_OK;
 }
 
 bool
 nsHTMLInputElement::IsValueEmpty() const
 {
   nsAutoString value;
   GetValueInternal(value);
@@ -1388,73 +1393,80 @@ nsHTMLInputElement::UpdateFileList()
   return NS_OK;
 }
 
 nsresult
 nsHTMLInputElement::SetValueInternal(const nsAString& aValue,
                                      PRBool aUserInput,
                                      PRBool aSetValueChanged)
 {
-  NS_PRECONDITION(mType != NS_FORM_INPUT_FILE,
+  NS_PRECONDITION(GetValueMode() != VALUE_MODE_FILENAME,
                   "Don't call SetValueInternal for file inputs");
 
-  if (mType == NS_FORM_INPUT_FILE) {
-    return NS_ERROR_UNEXPECTED;
-  }
-
-  if (IsSingleLineTextControl(PR_FALSE)) {
-    // At the moment, only single line text control have to sanitize their value
-    // Because we have to create a new string for that, we should prevent doing
-    // it if it's useless.
-    nsAutoString value(aValue);
-    if (!GET_BOOLBIT(mBitField, BF_PARSER_CREATING)) {
-      SanitizeValue(value);
-    }
-
-    if (aSetValueChanged) {
-      SetValueChanged(PR_TRUE);
+  switch (GetValueMode()) {
+    case VALUE_MODE_VALUE:
+    {
+      // At the moment, only single line text control have to sanitize their value
+      // Because we have to create a new string for that, we should prevent doing
+      // it if it's useless.
+      nsAutoString value(aValue);
+
+      if (!GET_BOOLBIT(mBitField, BF_PARSER_CREATING)) {
+        SanitizeValue(value);
+      }
+
+      if (aSetValueChanged) {
+        SetValueChanged(PR_TRUE);
+      }
+
+      mInputData.mState->SetValue(value, aUserInput);
+
+      // This call might be useless in some situations because if the element is
+      // a single line text control, nsTextEditorState::SetValue will call
+      // nsHTMLInputElement::OnValueChanged which is going to call UpdateState()
+      // if the element is focused. This bug 665547.
+      if (PlaceholderApplies() &&
+          HasAttr(kNameSpaceID_None, nsGkAtoms::placeholder)) {
+        UpdateState(true);
+      }
+
+      return NS_OK;
     }
-    mInputData.mState->SetValue(value, aUserInput);
-
-    if (PlaceholderApplies() &&
-        HasAttr(kNameSpaceID_None, nsGkAtoms::placeholder)) {
-      UpdateState(true);
-    }
-
-    return NS_OK;
+
+    case VALUE_MODE_DEFAULT:
+    case VALUE_MODE_DEFAULT_ON:
+      // If the value of a hidden input was changed, we mark it changed so that we
+      // will know we need to save / restore the value.  Yes, we are overloading
+      // the meaning of ValueChanged just a teensy bit to save a measly byte of
+      // storage space in nsHTMLInputElement.  Yes, you are free to make a new flag,
+      // NEED_TO_SAVE_VALUE, at such time as mBitField becomes a 16-bit value.
+      if (mType == NS_FORM_INPUT_HIDDEN) {
+        SetValueChanged(PR_TRUE);
+      }
+
+      // Treat value == defaultValue for other input elements.
+      return nsGenericHTMLFormElement::SetAttr(kNameSpaceID_None,
+                                               nsGkAtoms::value, aValue,
+                                               PR_TRUE);
+
+    case VALUE_MODE_FILENAME:
+      return NS_ERROR_UNEXPECTED;
   }
 
-  // If the value of a hidden input was changed, we mark it changed so that we
-  // will know we need to save / restore the value.  Yes, we are overloading
-  // the meaning of ValueChanged just a teensy bit to save a measly byte of
-  // storage space in nsHTMLInputElement.  Yes, you are free to make a new flag,
-  // NEED_TO_SAVE_VALUE, at such time as mBitField becomes a 16-bit value.
-  if (mType == NS_FORM_INPUT_HIDDEN) {
-    SetValueChanged(PR_TRUE);
-  }
-
-  // Treat value == defaultValue for other input elements.
-  return nsGenericHTMLFormElement::SetAttr(kNameSpaceID_None,
-                                           nsGkAtoms::value, aValue,
-                                           PR_TRUE);
+  // This return statement is required for some compilers.
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 nsHTMLInputElement::SetValueChanged(PRBool aValueChanged)
 {
   PRBool valueChangedBefore = GetValueChanged();
 
   SET_BOOLBIT(mBitField, BF_VALUE_CHANGED, aValueChanged);
 
-  if (!aValueChanged) {
-    if (!IsSingleLineTextControl(PR_FALSE)) {
-      FreeData();
-    }
-  }
-
   if (valueChangedBefore != aValueChanged) {
     UpdateState(true);
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP