Bug 899931 - Part f: Cleanup nsListControlFrame::GetIndexFromDOMEvent; r=dzbarsky
authorMs2ger <ms2ger@gmail.com>
Fri, 02 Aug 2013 09:07:56 +0200
changeset 153242 1a76ce7c35fdabbea92db633f1a9679db7f745c1
parent 153241 fa76408d51da3434960c20bfebbfe61afdfe7410
child 153243 3e2c0996c8202f263f069bfc62d0800f970ee3de
push id2859
push userakeybl@mozilla.com
push dateMon, 16 Sep 2013 19:14:59 +0000
treeherdermozilla-beta@87d3c51cd2bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdzbarsky
bugs899931
milestone25.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 899931 - Part f: Cleanup nsListControlFrame::GetIndexFromDOMEvent; r=dzbarsky
layout/forms/nsListControlFrame.cpp
layout/forms/nsListControlFrame.h
--- a/layout/forms/nsListControlFrame.cpp
+++ b/layout/forms/nsListControlFrame.cpp
@@ -615,51 +615,16 @@ nsListControlFrame::ShouldPropagateCompu
 
 //---------------------------------------------------------
 nsIFrame*
 nsListControlFrame::GetContentInsertionFrame() {
   return GetOptionsContainer()->GetContentInsertionFrame();
 }
 
 //---------------------------------------------------------
-// Starts at the passed in content object and walks up the 
-// parent heierarchy looking for the nsIDOMHTMLOptionElement
-//---------------------------------------------------------
-nsIContent *
-nsListControlFrame::GetOptionFromContent(nsIContent *aContent) 
-{
-  for (nsIContent* content = aContent; content; content = content->GetParent()) {
-    if (content->IsHTML(nsGkAtoms::option)) {
-      return content;
-    }
-  }
-
-  return nullptr;
-}
-
-//---------------------------------------------------------
-// Finds the index of the hit frame's content in the list
-// of option elements
-//---------------------------------------------------------
-int32_t 
-nsListControlFrame::GetIndexFromContent(nsIContent *aContent)
-{
-  nsCOMPtr<nsIDOMHTMLOptionElement> option;
-  option = do_QueryInterface(aContent);
-  if (option) {
-    int32_t retval;
-    option->GetIndex(&retval);
-    if (retval >= 0) {
-      return retval;
-    }
-  }
-  return kNothingSelected;
-}
-
-//---------------------------------------------------------
 bool
 nsListControlFrame::ExtendedSelection(int32_t aStartIndex,
                                       int32_t aEndIndex,
                                       bool aClearAll)
 {
   return SetOptionsSelectedFromFrame(aStartIndex, aEndIndex,
                                      true, aClearAll);
 }
@@ -1797,46 +1762,51 @@ nsListControlFrame::GetIndexFromDOMEvent
     // If we're not capturing, then ignore movement in the border
     nsPoint pt = nsLayoutUtils::GetDOMEventCoordinatesRelativeTo(aMouseEvent, this);
     nsRect borderInnerEdge = GetScrollPortRect();
     if (!borderInnerEdge.Contains(pt)) {
       return NS_ERROR_FAILURE;
     }
   }
 
-  nsCOMPtr<nsIContent> content = PresContext()->EventStateManager()->
-    GetEventTargetContent(nullptr);
+  nsRefPtr<dom::HTMLOptionElement> option;
+  for (nsCOMPtr<nsIContent> content =
+         PresContext()->EventStateManager()->GetEventTargetContent(nullptr);
+       content && !option;
+       content = content->GetParent()) {
+    option = dom::HTMLOptionElement::FromContent(content);
+  }
 
-  nsCOMPtr<nsIContent> optionContent = GetOptionFromContent(content);
-  if (optionContent) {
-    aCurIndex = GetIndexFromContent(optionContent);
+  if (option) {
+    option->GetIndex(&aCurIndex);
+    MOZ_ASSERT(aCurIndex >= 0);
     return NS_OK;
   }
 
   int32_t numOptions = GetNumberOfOptions();
   if (numOptions < 1)
     return NS_ERROR_FAILURE;
 
   nsPoint pt = nsLayoutUtils::GetDOMEventCoordinatesRelativeTo(aMouseEvent, this);
 
   // If the event coordinate is above the first option frame, then target the
   // first option frame
-  nsCOMPtr<nsIContent> firstOption = GetOptionContent(0);
+  nsRefPtr<dom::HTMLOptionElement> firstOption = GetOption(0);
   NS_ASSERTION(firstOption, "Can't find first option that's supposed to be there");
   nsIFrame* optionFrame = firstOption->GetPrimaryFrame();
   if (optionFrame) {
     nsPoint ptInOptionFrame = pt - optionFrame->GetOffsetTo(this);
     if (ptInOptionFrame.y < 0 && ptInOptionFrame.x >= 0 &&
         ptInOptionFrame.x < optionFrame->GetSize().width) {
       aCurIndex = 0;
       return NS_OK;
     }
   }
 
-  nsCOMPtr<nsIContent> lastOption = GetOptionContent(numOptions - 1);
+  nsRefPtr<dom::HTMLOptionElement> lastOption = GetOption(numOptions - 1);
   // If the event coordinate is below the last option frame, then target the
   // last option frame
   NS_ASSERTION(lastOption, "Can't find last option that's supposed to be there");
   optionFrame = lastOption->GetPrimaryFrame();
   if (optionFrame) {
     nsPoint ptInOptionFrame = pt - optionFrame->GetOffsetTo(this);
     if (ptInOptionFrame.y >= optionFrame->GetSize().height && ptInOptionFrame.x >= 0 &&
         ptInOptionFrame.x < optionFrame->GetSize().width) {
--- a/layout/forms/nsListControlFrame.h
+++ b/layout/forms/nsListControlFrame.h
@@ -316,34 +316,32 @@ protected:
    */
   virtual void ResetList(bool aAllowScrolling);
 
   nsListControlFrame(nsIPresShell* aShell, nsIDocument* aDocument, nsStyleContext* aContext);
   virtual ~nsListControlFrame();
 
   // Utility methods
   nsresult GetSizeAttribute(uint32_t *aSize);
-  nsIContent* GetOptionFromContent(nsIContent *aContent);
 
   /**
    * Sets the mSelectedIndex and mOldSelectedIndex from figuring out what 
    * item was selected using content
    * @param aPoint the event point, in listcontrolframe coordinates
    * @return NS_OK if it successfully found the selection
    */
   nsresult GetIndexFromDOMEvent(nsIDOMEvent* aMouseEvent, int32_t& aCurIndex);
 
   /**
    * For a given index it returns the nsIContent object 
    * from the select.
    */
   already_AddRefed<nsIContent> GetOptionContent(int32_t aIndex) const;
 
   bool     CheckIfAllFramesHere();
-  int32_t  GetIndexFromContent(nsIContent *aContent);
   bool     IsLeftButton(nsIDOMEvent* aMouseEvent);
 
   // guess at a row height based on our own style.
   nscoord  CalcFallbackRowHeight(float aFontSizeInflation);
 
   // CalcIntrinsicHeight computes our intrinsic height (taking the "size"
   // attribute into account).  This should only be called in non-dropdown mode.
   nscoord CalcIntrinsicHeight(nscoord aHeightOfARow, int32_t aNumberOfOptions);