Bug 1352799 - Always set maxlength on initializing editor. r?masayuki draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Thu, 20 Apr 2017 10:23:01 +0900
changeset 574080 797f318ad8d42ffd058b225ff327004060d5cc1c
parent 573891 c3e5497cff1c995821b1c9320fa71f1ef9a8c30e
child 574081 33724d81c9a969eba7d23b3cf9ac7ad0a1d5a8a8
child 574085 ba09b6577f043ffd7dcec028d6aa667b8be8f6a6
push id57580
push userbmo:m_kato@ga2.so-net.ne.jp
push dateMon, 08 May 2017 10:01:54 +0000
reviewersmasayuki
bugs1352799
milestone55.0a1
Bug 1352799 - Always set maxlength on initializing editor. r?masayuki maxlength will be set by nsTextControlFrame::AttributeChanged via RestyleManager. If element is display:none, RestyleManager won't call Frame's AttributeChanged. So we should always initialize maxlength when setting focus. Also, wrap attribute for textarea element will be updated by HTMLTextAreaElement even if display:none. So this issue doesn't occur. maxlength might have to be updated by HTMLInputElement. But it is unnecessary to update editor's maxlength on display:none since this is used on focused editor. MozReview-Commit-ID: JHODOBTv62v
dom/html/nsTextEditorState.cpp
dom/html/nsTextEditorState.h
--- a/dom/html/nsTextEditorState.cpp
+++ b/dom/html/nsTextEditorState.cpp
@@ -1457,29 +1457,26 @@ nsTextEditorState::PrepareEditor(const n
           }
         }
       }
       if (!found)
         rv = NS_ERROR_FAILURE;
     }
   }
 
-  if (shouldInitializeEditor) {
-    // Initialize the plaintext editor
-    nsCOMPtr<nsIPlaintextEditor> textEditor(do_QueryInterface(newEditor));
-    if (textEditor) {
+  // Initialize the plaintext editor
+  nsCOMPtr<nsIPlaintextEditor> textEditor = do_QueryInterface(newEditor);
+  if (textEditor) {
+    if (shouldInitializeEditor) {
       // Set up wrapping
       textEditor->SetWrapColumn(GetWrapCols());
-
-      // Set max text field length
-      int32_t maxLength;
-      if (GetMaxLength(&maxLength)) { 
-        textEditor->SetMaxTextLength(maxLength);
-      }
     }
+
+    // Set max text field length
+    textEditor->SetMaxTextLength(GetMaxLength());
   }
 
   nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
   if (content) {
     rv = newEditor->GetFlags(&editorFlags);
     NS_ENSURE_SUCCESS(rv, rv);
 
     // Check if the readonly attribute is set.
@@ -2328,32 +2325,32 @@ nsTextEditorState::CreatePreviewNode()
   mPreviewDiv = CreateEmptyDivNode();
 
   mPreviewDiv->SetAttr(kNameSpaceID_None, nsGkAtoms::_class,
                        NS_LITERAL_STRING("preview-div"), false);
 
   return NS_OK;
 }
 
-bool
-nsTextEditorState::GetMaxLength(int32_t* aMaxLength)
+int32_t
+nsTextEditorState::GetMaxLength()
 {
   nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
   nsGenericHTMLElement* element =
     nsGenericHTMLElement::FromContentOrNull(content);
-  NS_ENSURE_TRUE(element, false);
+  if (NS_WARN_IF(!element)) {
+    return -1;
+  }
 
   const nsAttrValue* attr = element->GetParsedAttr(nsGkAtoms::maxlength);
   if (attr && attr->Type() == nsAttrValue::eInteger) {
-    *aMaxLength = attr->GetIntegerValue();
-
-    return true;
+    return attr->GetIntegerValue();
   }
 
-  return false;
+  return -1;
 }
 
 void
 nsTextEditorState::GetValue(nsAString& aValue, bool aIgnoreWrap) const
 {
   // While SetValue() is being called and requesting to commit composition to
   // IME, GetValue() may be called for appending text or something.  Then, we
   // need to return the latest aValue of SetValue() since the value hasn't
--- a/dom/html/nsTextEditorState.h
+++ b/dom/html/nsTextEditorState.h
@@ -235,17 +235,17 @@ public:
     return mPreviewVisibility;
   }
 
   /**
    * Get the maxlength attribute
    * @param aMaxLength the value of the max length attr
    * @returns false if attr not defined
    */
-  bool GetMaxLength(int32_t* aMaxLength);
+  int32_t GetMaxLength();
 
   void ClearValueCache() { mCachedValue.Truncate(); }
 
   void HideSelectionIfBlurred();
 
   struct SelectionProperties {
     public:
       SelectionProperties() : mStart(0), mEnd(0),