Bug 1385788 - Move the ownership of preview value from PresState to HTMLSelectElement. r=bz draft
authorRay Lin <ralin@mozilla.com>
Wed, 25 Oct 2017 11:57:57 +0800
changeset 686881 ce4e0213bdad9195eda2cbfa279820200a53cdb5
parent 686801 0d1e55d87931fe70ec1d007e886bcd58015ff770
child 737512 b9d763954dae27c8c7ffad1c1e0dbe2bc6a75f50
push id86339
push userbmo:ralin@mozilla.com
push dateThu, 26 Oct 2017 15:19:50 +0000
reviewersbz
bugs1385788
milestone58.0a1
Bug 1385788 - Move the ownership of preview value from PresState to HTMLSelectElement. r=bz MozReview-Commit-ID: 9NC8h6SyWdh
dom/html/HTMLSelectElement.cpp
dom/html/HTMLSelectElement.h
layout/base/nsPresState.h
layout/forms/nsComboboxControlFrame.cpp
layout/forms/nsComboboxControlFrame.h
layout/forms/nsIComboboxControlFrame.h
--- a/dom/html/HTMLSelectElement.cpp
+++ b/dom/html/HTMLSelectElement.cpp
@@ -1927,32 +1927,24 @@ HTMLSelectElement::SetOpenInParentProces
   nsIFormControlFrame* formControlFrame = GetFormControlFrame(false);
   nsIComboboxControlFrame* comboFrame = do_QueryFrame(formControlFrame);
   if (comboFrame) {
     comboFrame->SetOpenInParentProcess(aVal);
   }
 }
 
 void
-HTMLSelectElement::GetPreviewValue(nsAString& aValue)
+HTMLSelectElement::SetPreviewValue(const nsAString& aValue)
 {
+  mPreviewValue = aValue;
+  nsContentUtils::RemoveNewlines(mPreviewValue);
   nsIFormControlFrame* formControlFrame = GetFormControlFrame(false);
   nsIComboboxControlFrame* comboFrame = do_QueryFrame(formControlFrame);
   if (comboFrame) {
-    comboFrame->GetPreviewText(aValue);
-  }
-}
-
-void
-HTMLSelectElement::SetPreviewValue(const nsAString& aValue)
-{
-  nsIFormControlFrame* formControlFrame = GetFormControlFrame(false);
-  nsIComboboxControlFrame* comboFrame = do_QueryFrame(formControlFrame);
-  if (comboFrame) {
-    comboFrame->SetPreviewText(aValue);
+    comboFrame->RedisplaySelectedText();
   }
 }
 
 JSObject*
 HTMLSelectElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return HTMLSelectElementBinding::Wrap(aCx, this, aGivenProto);
 }
--- a/dom/html/HTMLSelectElement.h
+++ b/dom/html/HTMLSelectElement.h
@@ -439,17 +439,20 @@ public:
   bool IsCombobox() const
   {
     return !Multiple() && Size() <= 1;
   }
 
   bool OpenInParentProcess();
   void SetOpenInParentProcess(bool aVal);
 
-  void GetPreviewValue(nsAString& aValue);
+  void GetPreviewValue(nsAString& aValue)
+  {
+    aValue = mPreviewValue;
+  }
   void SetPreviewValue(const nsAString& aValue);
 
 protected:
   virtual ~HTMLSelectElement();
 
   friend class SafeOptionListMutation;
 
   // Helper Methods
@@ -656,16 +659,21 @@ protected:
    */
   nsCOMPtr<SelectState> mRestoreState;
 
   /**
    * The live list of selected options.
   */
   RefPtr<nsContentList> mSelectedOptions;
 
+  /**
+   * The current displayed preview text.
+  */
+  nsString  mPreviewValue;
+
 private:
   static void MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
                                     GenericSpecifiedValues* aGenericData);
 };
 
 } // namespace dom
 } // namespace mozilla
 
--- a/layout/base/nsPresState.h
+++ b/layout/base/nsPresState.h
@@ -106,32 +106,21 @@ public:
     mDroppedDown = aDroppedDown;
   }
 
   bool GetDroppedDown() const
   {
     return mDroppedDown;
   }
 
-  void SetPreviewText(const nsAString& aValue)
-  {
-    mPreviewText = aValue;
-  }
-
-  void GetPreviewText(nsAString& aValue)
-  {
-    aValue = mPreviewText;
-  }
-
 // MEMBER VARIABLES
 protected:
   nsCOMPtr<nsISupports> mContentData;
   nsPoint mScrollState;
   bool mAllowScrollOriginDowngrade;
   float mResolution;
   bool mScaleToResolution;
   bool mDisabledSet;
   bool mDisabled;
   bool mDroppedDown;
-  nsString mPreviewText;
 };
 
 #endif /* nsPresState_h_ */
--- a/layout/forms/nsComboboxControlFrame.cpp
+++ b/layout/forms/nsComboboxControlFrame.cpp
@@ -22,16 +22,17 @@
 #include "nsIPresShell.h"
 #include "nsPresState.h"
 #include "nsView.h"
 #include "nsViewManager.h"
 #include "nsIDOMEventListener.h"
 #include "nsIDOMNode.h"
 #include "nsISelectControlFrame.h"
 #include "nsContentUtils.h"
+#include "mozilla/dom/HTMLSelectElement.h"
 #include "nsIDocument.h"
 #include "nsIScrollableFrame.h"
 #include "nsListControlFrame.h"
 #include "mozilla/StyleSetHandle.h"
 #include "mozilla/StyleSetHandleInlines.h"
 #include "nsNodeInfoManager.h"
 #include "nsContentCreatorFunctions.h"
 #include "nsLayoutUtils.h"
@@ -963,42 +964,37 @@ nsComboboxControlFrame::SetDropDown(nsIF
 nsIFrame*
 nsComboboxControlFrame::GetDropDown()
 {
   return mDropdownFrame;
 }
 
 ///////////////////////////////////////////////////////////////
 
-void
-nsComboboxControlFrame::SetPreviewText(const nsAString& aValue)
-{
-  nsAutoString previewValue(aValue);
-  nsContentUtils::RemoveNewlines(previewValue);
-
-  mPreviewText = previewValue;
-  RedisplayText();
-}
 
 NS_IMETHODIMP
 nsComboboxControlFrame::RedisplaySelectedText()
 {
   nsAutoScriptBlocker scriptBlocker;
   mDisplayedIndex = mListControlFrame->GetSelectedIndex();
   return RedisplayText();
 }
 
 
 nsresult
 nsComboboxControlFrame::RedisplayText()
 {
+  nsString previewValue;
   nsString previousText(mDisplayedOptionTextOrPreview);
+
+  auto* selectElement = static_cast<dom::HTMLSelectElement*>(GetContent());
+  selectElement->GetPreviewValue(previewValue);
   // Get the text to display
-  if (!mPreviewText.IsEmpty()) {
-    mDisplayedOptionTextOrPreview = mPreviewText;
+  if (!previewValue.IsEmpty()) {
+    mDisplayedOptionTextOrPreview = previewValue;
   } else if (mDisplayedIndex != -1) {
     mListControlFrame->GetOptionText(mDisplayedIndex, mDisplayedOptionTextOrPreview);
   } else {
     mDisplayedOptionTextOrPreview.Truncate();
   }
 
   REFLOW_DEBUG_MSG2("RedisplayText \"%s\"\n",
                     NS_LossyConvertUTF16toASCII(mDisplayedOptionTextOrPreview).get());
@@ -1669,27 +1665,25 @@ nsComboboxControlFrame::OnContentReset()
 // nsIStatefulFrame
 //--------------------------------------------------------
 NS_IMETHODIMP
 nsComboboxControlFrame::SaveState(nsPresState** aState)
 {
   MOZ_ASSERT(!(*aState));
   (*aState) = new nsPresState();
   (*aState)->SetDroppedDown(mDroppedDown);
-  (*aState)->SetPreviewText(mPreviewText);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsComboboxControlFrame::RestoreState(nsPresState* aState)
 {
   if (!aState) {
     return NS_ERROR_FAILURE;
   }
-  aState->GetPreviewText(mPreviewText);
   ShowList(aState->GetDroppedDown()); // might destroy us
   return NS_OK;
 }
 
 // Append a suffix so that the state key for the combobox is different
 // from the state key the list control uses to sometimes save the scroll
 // position for the same Element
 NS_IMETHODIMP
--- a/layout/forms/nsComboboxControlFrame.h
+++ b/layout/forms/nsComboboxControlFrame.h
@@ -168,22 +168,16 @@ public:
     return mIsOpenInParentProcess;
   }
 
   void SetOpenInParentProcess(bool aVal) override
   {
     mIsOpenInParentProcess = aVal;
   }
 
-  void GetPreviewText(nsAString& aValue) override
-  {
-    aValue = mPreviewText;
-  }
-  void SetPreviewText(const nsAString& aValue) override;
-
   // nsISelectControlFrame
   NS_IMETHOD AddOption(int32_t index) override;
   NS_IMETHOD RemoveOption(int32_t index) override;
   NS_IMETHOD DoneAddingChildren(bool aIsDone) override;
   NS_IMETHOD OnOptionSelected(int32_t aIndex, bool aSelected) override;
   NS_IMETHOD OnSetSelectedIndex(int32_t aOldIndex, int32_t aNewIndex) override;
 
   //nsIRollupListener
@@ -299,17 +293,16 @@ protected:
   // to size to the full inline size except the drop-marker.
   nscoord mDisplayISize;
 
   nsRevocableEventPtr<RedisplayTextEvent> mRedisplayTextEvent;
 
   int32_t               mRecentSelectedIndex;
   int32_t               mDisplayedIndex;
   nsString              mDisplayedOptionTextOrPreview;
-  nsString              mPreviewText;
 
   // make someone to listen to the button. If its programmatically pressed by someone like Accessibility
   // then open or close the combo box.
   nsCOMPtr<nsIDOMEventListener> mButtonListener;
 
   // The last y-positions used for estimating available space before and
   // after for the dropdown list in GetAvailableDropdownSpace.  These are
   // reset to nscoord_MIN in AbsolutelyPositionDropDown when placing the
--- a/layout/forms/nsIComboboxControlFrame.h
+++ b/layout/forms/nsIComboboxControlFrame.h
@@ -48,26 +48,16 @@ public:
 
   /**
    * Redisplay the selected text (will do nothing if text has not changed).
    * This method might destroy this frame or any others that happen to be
    * around.  It might even run script.
    */
   NS_IMETHOD RedisplaySelectedText() = 0;
 
-  /*
-   * Update preview text for the select control.
-   */
-  virtual void SetPreviewText(const nsAString& aValue) = 0;
-
-  /**
-   * Get the current preview text for select control.
-   */
-  virtual void GetPreviewText(nsAString& aValue) = 0;
-
   /**
    * Method for the listbox to set and get the recent index
    */
   virtual int32_t UpdateRecentIndex(int32_t aIndex) = 0;
 
   /**
    * Notification that the content has been reset
    */