Bug 899931 - Part d: Cleanup nsListControlFrame::GetCurrentOption; r=dzbarsky

This commit is contained in:
Ms2ger 2013-08-02 09:07:22 +02:00
parent 29bcd8621d
commit 2a836c0301
3 changed files with 17 additions and 30 deletions

View File

@ -12,6 +12,12 @@
class nsAString;
class nsIContent;
namespace mozilla {
namespace dom {
class HTMLOptionElement;
} // namespace dom
} // namespace mozilla
/**
* nsIListControlFrame is the interface for frame-based listboxes.
*/
@ -41,7 +47,7 @@ public:
* Return current option. The current option is the option displaying
* the focus ring when the listbox is focused.
*/
virtual already_AddRefed<nsIContent> GetCurrentOption() = 0;
virtual mozilla::dom::HTMLOptionElement* GetCurrentOption() = 0;
/**
* Initiates mouse capture for the listbox

View File

@ -1177,7 +1177,7 @@ nsListControlFrame::GetSelectedIndex()
return aIndex;
}
already_AddRefed<nsIContent>
dom::HTMLOptionElement*
nsListControlFrame::GetCurrentOption()
{
// The mEndSelectionIndex is what is currently being selected. Use
@ -1186,43 +1186,24 @@ nsListControlFrame::GetCurrentOption()
GetSelectedIndex() : mEndSelectionIndex;
if (focusedIndex != kNothingSelected) {
return GetOptionContent(focusedIndex);
return GetOption(SafeCast<uint32_t>(focusedIndex));
}
// There is no selected item. Return the first non-disabled item.
nsRefPtr<dom::HTMLSelectElement> selectElement =
dom::HTMLSelectElement::FromContent(mContent);
NS_ASSERTION(selectElement, "Can't be null");
// There is no a selected item return the first non-disabled item and skip all
// the option group elements.
nsCOMPtr<nsIDOMNode> node;
uint32_t length;
selectElement->GetLength(&length);
if (length) {
bool isDisabled = true;
for (uint32_t i = 0; i < length && isDisabled; i++) {
if (NS_FAILED(selectElement->Item(i, getter_AddRefs(node))) || !node) {
break;
}
if (NS_FAILED(selectElement->IsOptionDisabled(i, &isDisabled))) {
break;
}
if (isDisabled) {
node = nullptr;
} else {
break;
}
}
for (uint32_t i = 0, length = selectElement->Length(); i < length; ++i) {
dom::HTMLOptionElement* node = selectElement->Item(i);
if (!node) {
return nullptr;
}
if (!selectElement->IsOptionDisabled(node)) {
return node;
}
}
if (node) {
nsCOMPtr<nsIContent> focusedOption = do_QueryInterface(node);
return focusedOption.forget();
}
return nullptr;
}

View File

@ -117,7 +117,7 @@ public:
// nsIListControlFrame
virtual void SetComboboxFrame(nsIFrame* aComboboxFrame) MOZ_OVERRIDE;
virtual int32_t GetSelectedIndex() MOZ_OVERRIDE;
virtual already_AddRefed<nsIContent> GetCurrentOption() MOZ_OVERRIDE;
virtual mozilla::dom::HTMLOptionElement* GetCurrentOption() MOZ_OVERRIDE;
/**
* Gets the text of the currently selected item.