Commit c97868e4 authored by tkent's avatar tkent Committed by Commit bot

SELECT element: Add HTMLSelectElement::selectedListIndex().

We had multiple instances of |optionToListIndex(selectedIndex())|, which causes
two iterations over OPTIONs. selectedListIndex() returns the same value, but is
more efficient.

Review-Url: https://codereview.chromium.org/2149763003
Cr-Commit-Position: refs/heads/master@{#405508}
parent 5897d75b
......@@ -873,6 +873,17 @@ void HTMLSelectElement::setSelectedIndex(int index)
selectOption(item(index), DeselectOtherOptions | MakeOptionDirty);
}
int HTMLSelectElement::selectedListIndex() const
{
int index = 0;
for (const auto& item : listItems()) {
if (isHTMLOptionElement(item) && toHTMLOptionElement(item)->selected())
return index;
++index;
}
return -1;
}
void HTMLSelectElement::setSuggestedOption(HTMLOptionElement* option)
{
if (m_suggestedOption == option)
......@@ -1674,7 +1685,7 @@ HTMLOptionElement* HTMLSelectElement::lastSelectedOption() const
int HTMLSelectElement::indexOfSelectedOption() const
{
return optionToListIndex(selectedIndex());
return selectedListIndex();
}
int HTMLSelectElement::optionCount() const
......
......@@ -55,6 +55,8 @@ public:
int selectedIndex() const;
void setSelectedIndex(int);
// `listIndex' version of |selectedIndex|.
int selectedListIndex() const;
// For ValidityState
String validationMessage() const override;
......
......@@ -264,7 +264,7 @@ void ExternalPopupMenu::getPopupMenuInfo(WebPopupMenuInfo& info, HTMLSelectEleme
const ComputedStyle& menuStyle = ownerElement.computedStyle() ? *ownerElement.computedStyle() : *ownerElement.ensureComputedStyle();
info.itemHeight = menuStyle.font().getFontMetrics().height();
info.itemFontSize = static_cast<int>(menuStyle.font().getFontDescription().computedSize());
info.selectedIndex = toExternalPopupMenuItemIndex(ownerElement.optionToListIndex(ownerElement.selectedIndex()), ownerElement);
info.selectedIndex = toExternalPopupMenuItemIndex(ownerElement.selectedListIndex(), ownerElement);
info.rightAligned = menuStyle.direction() == RTL;
info.allowMultipleSelection = ownerElement.multiple();
if (count < itemCount)
......
......@@ -253,7 +253,7 @@ void PopupMenuImpl::writeDocument(SharedBuffer* data)
data->append(Platform::current()->loadResource("listPicker.css"));
PagePopupClient::addString("</style></head><body><div id=main>Loading...</div><script>\n"
"window.dialogArguments = {\n", data);
addProperty("selectedIndex", ownerElement.optionToListIndex(ownerElement.selectedIndex()), data);
addProperty("selectedIndex", ownerElement.selectedListIndex(), data);
const ComputedStyle* ownerStyle = ownerElement.computedStyle();
ItemIterationContext context(*ownerStyle, data);
context.serializeBaseStyle();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment