From 6e35ea5ed5a255384ac750ee953f8159d6f6da77 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Fri, 2 Aug 2013 09:08:19 +0200 Subject: [PATCH] Bug 899931 - Part h: Cleanup nsListControlFrame::GetOptions; r=dzbarsky --- layout/forms/nsListControlFrame.cpp | 72 ++++++++--------------------- layout/forms/nsListControlFrame.h | 13 ++---- 2 files changed, 23 insertions(+), 62 deletions(-) diff --git a/layout/forms/nsListControlFrame.cpp b/layout/forms/nsListControlFrame.cpp index e7e59892d4a1..1d5e420d0399 100644 --- a/layout/forms/nsListControlFrame.cpp +++ b/layout/forms/nsListControlFrame.cpp @@ -13,7 +13,6 @@ #include "nsIFormControl.h" #include "nsIDocument.h" #include "nsIDOMHTMLCollection.h" -#include "nsIDOMHTMLOptionsCollection.h" #include "nsIDOMHTMLSelectElement.h" #include "nsIDOMHTMLOptionElement.h" #include "nsComboboxControlFrame.h" @@ -42,6 +41,7 @@ #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 @@ -684,17 +684,13 @@ nsListControlFrame::InitSelectionRange(int32_t aClickedIndex) int32_t selectedIndex = GetSelectedIndex(); if (selectedIndex >= 0) { // Get the end of the contiguous selection - nsCOMPtr options = GetOptions(mContent); + nsRefPtr 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 option = GetOption(options, i); - option->GetSelected(&selected); - if (!selected) { + for (i = selectedIndex + 1; i < numOptions; i++) { + if (!options->ItemAsOption(i)->Selected()) { break; } } @@ -984,16 +980,14 @@ nsListControlFrame::Init(nsIContent* aContent, } } -already_AddRefed -nsListControlFrame::GetOptions(nsIContent * aContent) +dom::HTMLOptionsCollection* +nsListControlFrame::GetOptions() const { - nsCOMPtr options; - nsCOMPtr 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* @@ -1006,25 +1000,6 @@ nsListControlFrame::GetOption(uint32_t aIndex) const return select->Item(aIndex); } -already_AddRefed -nsListControlFrame::GetOption(nsIDOMHTMLOptionsCollection* aCollection, - int32_t aIndex) -{ - nsCOMPtr 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 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) { @@ -1156,18 +1131,12 @@ nsListControlFrame::IsInDropDownMode() const uint32_t nsListControlFrame::GetNumberOfOptions() { - if (!mContent) { - return 0; - } - - nsCOMPtr options = GetOptions(mContent); + dom::HTMLOptionsCollection* options = GetOptions(); if (!options) { return 0; } - uint32_t length = 0; - options->GetLength(&length); - return length; + return options->Length(); } //---------------------------------------------------------------------- @@ -2108,11 +2077,10 @@ nsListControlFrame::KeyDown(nsIDOMEvent* aKeyEvent) } // now make sure there are options or we are wasting our time - nsCOMPtr options = GetOptions(mContent); + nsRefPtr 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; @@ -2324,15 +2292,15 @@ nsListControlFrame::KeyPress(nsIDOMEvent* aKeyEvent) } // now make sure there are options or we are wasting our time - nsCOMPtr options = GetOptions(mContent); + nsRefPtr 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 optionElement = GetOption(options, index); + nsRefPtr optionElement = + options->ItemAsOption(index); if (!optionElement) { continue; } diff --git a/layout/forms/nsListControlFrame.h b/layout/forms/nsListControlFrame.h index 90b321988ae3..2f86d6fd92ca 100644 --- a/layout/forms/nsListControlFrame.h +++ b/layout/forms/nsListControlFrame.h @@ -36,6 +36,7 @@ class nsListEventListener; namespace mozilla { namespace dom { class HTMLOptionElement; +class HTMLOptionsCollection; } // namespace dom } // namespace mozilla @@ -170,22 +171,14 @@ public: 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 - 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 - GetOption(nsIDOMHTMLOptionsCollection* aOptions, int32_t aIndex); - static void ComboboxFocusSet(); // Helper