diff --git a/layout/xul/base/src/nsMenuFrame.cpp b/layout/xul/base/src/nsMenuFrame.cpp index 1a3470c46ca..0760db76ce6 100644 --- a/layout/xul/base/src/nsMenuFrame.cpp +++ b/layout/xul/base/src/nsMenuFrame.cpp @@ -1387,27 +1387,10 @@ nsMenuFrame::GetBoxInfo(nsIPresContext* aPresContext, const nsHTMLReflowState& a aSize.prefSize.width = childInfo.prefSize.width; } + // This retrieval guarantess that the selectedItem will + // be set before we lay out. nsCOMPtr element; menulist->GetSelectedItem(getter_AddRefs(element)); - if (!element) { - nsAutoString value; - menulist->GetValue(value); - if (value == "") { - nsCOMPtr child; - GetMenuChildrenElement(getter_AddRefs(child)); - if (child) { - PRInt32 count; - child->ChildCount(count); - if (count > 0) { - nsCOMPtr item; - child->ChildAt(0, *getter_AddRefs(item)); - nsCOMPtr selectedElement(do_QueryInterface(item)); - if (selectedElement) - menulist->SetSelectedItem(selectedElement); - } - } - } - } } return rv; } diff --git a/rdf/content/src/nsXULMenuListElement.cpp b/rdf/content/src/nsXULMenuListElement.cpp index c778522a85f..82352c635c0 100644 --- a/rdf/content/src/nsXULMenuListElement.cpp +++ b/rdf/content/src/nsXULMenuListElement.cpp @@ -29,6 +29,7 @@ #include "nsCOMPtr.h" #include "nsRDFCID.h" #include "nsXULMenuListElement.h" +#include "nsIDOMXULPopupElement.h" #include "nsIContent.h" #include "nsIDocument.h" #include "nsIPresContext.h" @@ -144,9 +145,50 @@ nsXULMenuListElement::SetDisabled(PRBool aDisabled) return NS_OK; } +static void +GetMenuChildrenElement(nsIContent* aParent, nsIContent** aResult) +{ + PRInt32 count; + aParent->ChildCount(count); + + for (PRInt32 i = 0; i < count; i++) { + nsCOMPtr child; + aParent->ChildAt(i, *getter_AddRefs(child)); + nsCOMPtr menuPopup(do_QueryInterface(child)); + if (child) { + *aResult = child.get(); + NS_ADDREF(*aResult); + return; + } + } +} + + NS_IMETHODIMP nsXULMenuListElement::GetSelectedItem(nsIDOMElement** aResult) { + if (!mSelectedItem) { + nsAutoString value; + GetValue(value); + + if (value == "") { + nsCOMPtr parent(do_QueryInterface(mOuter)); + nsCOMPtr child; + GetMenuChildrenElement(parent, getter_AddRefs(child)); + if (child) { + PRInt32 count; + child->ChildCount(count); + if (count > 0) { + nsCOMPtr item; + child->ChildAt(0, *getter_AddRefs(item)); + nsCOMPtr selectedElement(do_QueryInterface(item)); + if (selectedElement) + SetSelectedItem(selectedElement); + } + } + } + } + *aResult = mSelectedItem; NS_IF_ADDREF(*aResult);