Bug 899931 - Part h: Cleanup nsListControlFrame::GetOptions; r=dzbarsky
authorMs2ger <ms2ger@gmail.com>
Fri, 02 Aug 2013 09:08:19 +0200
changeset 148538 35078177354436dc791ab5d5122d9e389f71a4f4
parent 148537 3e2c0996c8202f263f069bfc62d0800f970ee3de
child 148539 f81a119405640561ed4a576a29d17638b83ca883
push id4085
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 20:29:25 +0000
treeherdermozilla-aurora@ede8780a15bc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdzbarsky
bugs899931
milestone25.0a1
Bug 899931 - Part h: Cleanup nsListControlFrame::GetOptions; r=dzbarsky
layout/forms/nsListControlFrame.cpp
layout/forms/nsListControlFrame.h
--- a/layout/forms/nsListControlFrame.cpp
+++ b/layout/forms/nsListControlFrame.cpp
@@ -8,17 +8,16 @@
 #include "nsReadableUtils.h"
 #include "nsUnicharUtils.h"
 #include "nsListControlFrame.h"
 #include "nsFormControlFrame.h" // for COMPARE macro
 #include "nsGkAtoms.h"
 #include "nsIFormControl.h"
 #include "nsIDocument.h"
 #include "nsIDOMHTMLCollection.h"
-#include "nsIDOMHTMLOptionsCollection.h"
 #include "nsIDOMHTMLSelectElement.h"
 #include "nsIDOMHTMLOptionElement.h"
 #include "nsComboboxControlFrame.h"
 #include "nsViewManager.h"
 #include "nsIDOMHTMLOptGroupElement.h"
 #include "nsWidgetsCID.h"
 #include "nsIPresShell.h"
 #include "nsHTMLParts.h"
@@ -37,16 +36,17 @@
 #include "nsINodeInfo.h"
 #include "nsCSSRendering.h"
 #include "nsITheme.h"
 #include "nsIDOMEventListener.h"
 #include "nsLayoutUtils.h"
 #include "nsDisplayList.h"
 #include "nsContentUtils.h"
 #include "mozilla/Attributes.h"
+#include "mozilla/dom/HTMLOptionsCollection.h"
 #include "mozilla/dom/HTMLSelectElement.h"
 #include "mozilla/LookAndFeel.h"
 #include <algorithm>
 
 using namespace mozilla;
 
 // Constants
 const uint32_t kMaxDropDownRows         = 20; // This matches the setting for 4.x browsers
@@ -679,27 +679,23 @@ nsListControlFrame::InitSelectionRange(i
   // clicked >= selectedIndex, return selectedIndex.
   //
   // This makes it so that shift click works properly when you first click
   // in a multiple select.
   //
   int32_t selectedIndex = GetSelectedIndex();
   if (selectedIndex >= 0) {
     // Get the end of the contiguous selection
-    nsCOMPtr<nsIDOMHTMLOptionsCollection> options = GetOptions(mContent);
+    nsRefPtr<dom::HTMLOptionsCollection> options = GetOptions();
     NS_ASSERTION(options, "Collection of options is null!");
-    uint32_t numOptions;
-    options->GetLength(&numOptions);
+    uint32_t numOptions = options->Length();
+    // Push i to one past the last selected index in the group.
     uint32_t i;
-    // Push i to one past the last selected index in the group
-    for (i=selectedIndex+1; i < numOptions; i++) {
-      bool selected;
-      nsCOMPtr<nsIDOMHTMLOptionElement> option = GetOption(options, i);
-      option->GetSelected(&selected);
-      if (!selected) {
+    for (i = selectedIndex + 1; i < numOptions; i++) {
+      if (!options->ItemAsOption(i)->Selected()) {
         break;
       }
     }
 
     if (aClickedIndex < selectedIndex) {
       // User clicked before selection, so start selection at end of
       // contiguous selection
       mStartSelectionIndex = i-1;
@@ -979,57 +975,36 @@ nsListControlFrame::Init(nsIContent*    
 
   mLastDropdownBackstopColor = PresContext()->DefaultBackgroundColor();
 
   if (IsInDropDownMode()) {
     AddStateBits(NS_FRAME_IN_POPUP);
   }
 }
 
-already_AddRefed<nsIDOMHTMLOptionsCollection>
-nsListControlFrame::GetOptions(nsIContent * aContent)
+dom::HTMLOptionsCollection*
+nsListControlFrame::GetOptions() const
 {
-  nsCOMPtr<nsIDOMHTMLOptionsCollection> options;
-  nsCOMPtr<nsIDOMHTMLSelectElement> selectElement = do_QueryInterface(aContent);
-  if (selectElement) {
-    selectElement->GetOptions(getter_AddRefs(options));
-  }
+  dom::HTMLSelectElement* select =
+    dom::HTMLSelectElement::FromContentOrNull(mContent);
+  NS_ENSURE_TRUE(select, nullptr);
 
-  return options.forget();
+  return select->Options();
 }
 
 dom::HTMLOptionElement*
 nsListControlFrame::GetOption(uint32_t aIndex) const
 {
   dom::HTMLSelectElement* select =
     dom::HTMLSelectElement::FromContentOrNull(mContent);
   NS_ENSURE_TRUE(select, nullptr);
 
   return select->Item(aIndex);
 }
 
-already_AddRefed<nsIDOMHTMLOptionElement>
-nsListControlFrame::GetOption(nsIDOMHTMLOptionsCollection* aCollection,
-                              int32_t aIndex)
-{
-  nsCOMPtr<nsIDOMNode> node;
-  if (NS_SUCCEEDED(aCollection->Item(aIndex, getter_AddRefs(node)))) {
-    NS_ASSERTION(node,
-                 "Item was successful, but node from collection was null!");
-    if (node) {
-      nsCOMPtr<nsIDOMHTMLOptionElement> option = do_QueryInterface(node);
-
-      return option.forget();
-    }
-  } else {
-    NS_ERROR("Couldn't get option by index from collection!");
-  }
-  return nullptr;
-}
-
 NS_IMETHODIMP
 nsListControlFrame::OnOptionSelected(int32_t aIndex, bool aSelected)
 {
   if (aSelected) {
     ScrollToIndex(aIndex);
   }
   return NS_OK;
 }
@@ -1151,28 +1126,22 @@ bool
 nsListControlFrame::IsInDropDownMode() const
 {
   return (mComboboxFrame != nullptr);
 }
 
 uint32_t
 nsListControlFrame::GetNumberOfOptions()
 {
-  if (!mContent) {
-    return 0;
-  }
-
-  nsCOMPtr<nsIDOMHTMLOptionsCollection> options = GetOptions(mContent);
+  dom::HTMLOptionsCollection* options = GetOptions();
   if (!options) {
     return 0;
   }
 
-  uint32_t length = 0;
-  options->GetLength(&length);
-  return length;
+  return options->Length();
 }
 
 //----------------------------------------------------------------------
 // nsISelectControlFrame
 //----------------------------------------------------------------------
 bool nsListControlFrame::CheckIfAllFramesHere()
 {
   // Get the number of optgroups and options
@@ -2103,21 +2072,20 @@ nsListControlFrame::KeyDown(nsIDOMEvent*
   if (keyEvent->IsAlt()) {
     if (keyEvent->keyCode == NS_VK_UP || keyEvent->keyCode == NS_VK_DOWN) {
       DropDownToggleKey(aKeyEvent);
     }
     return NS_OK;
   }
 
   // now make sure there are options or we are wasting our time
-  nsCOMPtr<nsIDOMHTMLOptionsCollection> options = GetOptions(mContent);
+  nsRefPtr<dom::HTMLOptionsCollection> options = GetOptions();
   NS_ENSURE_TRUE(options, NS_ERROR_FAILURE);
 
-  uint32_t numOptions = 0;
-  options->GetLength(&numOptions);
+  uint32_t numOptions = options->Length();
 
   // this is the new index to set
   int32_t newIndex = kNothingSelected;
 
   bool isControlOrMeta = (keyEvent->IsControl() || keyEvent->IsMeta());
   if (isControlOrMeta && (keyEvent->keyCode == NS_VK_UP ||
                           keyEvent->keyCode == NS_VK_LEFT ||
                           keyEvent->keyCode == NS_VK_DOWN ||
@@ -2319,25 +2287,25 @@ nsListControlFrame::KeyPress(nsIDOMEvent
   int32_t startIndex = GetSelectedIndex();
   if (startIndex == kNothingSelected) {
     startIndex = 0;
   } else if (stringLength == 1) {
     startIndex++;
   }
 
   // now make sure there are options or we are wasting our time
-  nsCOMPtr<nsIDOMHTMLOptionsCollection> options = GetOptions(mContent);
+  nsRefPtr<dom::HTMLOptionsCollection> options = GetOptions();
   NS_ENSURE_TRUE(options, NS_ERROR_FAILURE);
 
-  uint32_t numOptions = 0;
-  options->GetLength(&numOptions);
+  uint32_t numOptions = options->Length();
 
   for (uint32_t i = 0; i < numOptions; ++i) {
     uint32_t index = (i + startIndex) % numOptions;
-    nsCOMPtr<nsIDOMHTMLOptionElement> optionElement = GetOption(options, index);
+    nsRefPtr<dom::HTMLOptionElement> optionElement =
+      options->ItemAsOption(index);
     if (!optionElement) {
       continue;
     }
 
     nsAutoString text;
     if (NS_FAILED(optionElement->GetText(text)) ||
         !StringBeginsWith(
            nsContentUtils::TrimWhitespace<
--- a/layout/forms/nsListControlFrame.h
+++ b/layout/forms/nsListControlFrame.h
@@ -31,16 +31,17 @@ class nsIDOMHTMLOptionsCollection;
 class nsIDOMHTMLOptionElement;
 class nsIComboboxControlFrame;
 class nsPresContext;
 class nsListEventListener;
 
 namespace mozilla {
 namespace dom {
 class HTMLOptionElement;
+class HTMLOptionsCollection;
 } // namespace dom
 } // namespace mozilla
 
 /**
  * Frame-based listbox.
  */
 
 class nsListControlFrame : public nsHTMLScrollFrame,
@@ -165,32 +166,24 @@ public:
   nsresult MouseDown(nsIDOMEvent* aMouseEvent); // might destroy |this|
   nsresult MouseUp(nsIDOMEvent* aMouseEvent);   // might destroy |this|
   nsresult MouseMove(nsIDOMEvent* aMouseEvent);
   nsresult DragMove(nsIDOMEvent* aMouseEvent);
   nsresult KeyDown(nsIDOMEvent* aKeyEvent);     // might destroy |this|
   nsresult KeyPress(nsIDOMEvent* aKeyEvent);    // might destroy |this|
 
   /**
-   * Returns the options collection for aContent, if any.
+   * Returns the options collection for mContent, if any.
    */
-  static already_AddRefed<nsIDOMHTMLOptionsCollection>
-    GetOptions(nsIContent * aContent);
+  mozilla::dom::HTMLOptionsCollection* GetOptions() const;
   /**
    * Returns the HTMLOptionElement for a given index in mContent's collection.
    */
   mozilla::dom::HTMLOptionElement* GetOption(uint32_t aIndex) const;
 
-  /**
-   * Returns the nsIDOMHTMLOptionElement for a given index 
-   * in the select's collection.
-   */
-  static already_AddRefed<nsIDOMHTMLOptionElement>
-    GetOption(nsIDOMHTMLOptionsCollection* aOptions, int32_t aIndex);
-
   static void ComboboxFocusSet();
 
   // Helper
   bool IsFocused() { return this == mFocused; }
 
   /**
    * Function to paint the focus rect when our nsSelectsAreaFrame is painting.
    * @param aPt the offset of this frame, relative to the rendering reference