Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(537)

Unified Diff: third_party/WebKit/Source/core/html/HTMLSelectElement.cpp

Issue 2152643002: SELECT element: Introduce HTMLSelectElement::optionAtListIndex(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/core/html/HTMLSelectElement.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/core/html/HTMLSelectElement.cpp
diff --git a/third_party/WebKit/Source/core/html/HTMLSelectElement.cpp b/third_party/WebKit/Source/core/html/HTMLSelectElement.cpp
index d3c46052dc707340038853ccb3fcb20d01e8b351..edfa1c61e7b2bbb641948c0cfd806718ebb33027 100644
--- a/third_party/WebKit/Source/core/html/HTMLSelectElement.cpp
+++ b/third_party/WebKit/Source/core/html/HTMLSelectElement.cpp
@@ -189,9 +189,8 @@ void HTMLSelectElement::selectMultipleOptionsByPopup(const Vector<int>& listIndi
DCHECK(!multiple());
for (size_t i = 0; i < listIndices.size(); ++i) {
bool addSelectionIfNotFirst = i > 0;
- HTMLElement* element = listItems()[listIndices[i]];
- if (isHTMLOptionElement(element))
- updateSelectedState(toHTMLOptionElement(element), addSelectionIfNotFirst, false);
+ if (HTMLOptionElement* option = optionAtListIndex(listIndices[i]))
+ updateSelectedState(option, addSelectionIfNotFirst, false);
}
setNeedsValidityCheck();
// TODO(tkent): Using listBoxOnChange() is very confusing.
@@ -481,6 +480,16 @@ bool HTMLSelectElement::isRequiredFormControl() const
return isRequired();
}
+HTMLOptionElement* HTMLSelectElement::optionAtListIndex(int listIndex) const
+{
+ if (listIndex < 0)
+ return nullptr;
+ const ListItems& items = listItems();
+ if (static_cast<size_t>(listIndex) >= items.size() || !isHTMLOptionElement(items[listIndex]))
+ return nullptr;
+ return toHTMLOptionElement(items[listIndex]);
+}
+
// Returns the 1st valid OPTION |skip| items from |listIndex| in direction
// |direction| if there is one.
// Otherwise, it returns the valid OPTION closest to that boundary which is past
@@ -1058,7 +1067,7 @@ int HTMLSelectElement::optionToListIndex(int optionIndex) const
int HTMLSelectElement::listToOptionIndex(int listIndex) const
{
const ListItems& items = listItems();
- if (listIndex < 0 || listIndex >= static_cast<int>(items.size()) || !isHTMLOptionElement(*items[listIndex]))
+ if (!optionAtListIndex(listIndex))
return -1;
// Actual index of option not counting OPTGROUP entries that may be in list.
@@ -1674,9 +1683,10 @@ HTMLOptionElement* HTMLSelectElement::lastSelectedOption() const
{
const ListItems& items = listItems();
for (size_t i = items.size(); i;) {
- HTMLElement* element = items[--i];
- if (isHTMLOptionElement(*element) && toHTMLOptionElement(element)->selected())
- return toHTMLOptionElement(element);
+ if (HTMLOptionElement* option = optionAtListIndex(--i)) {
+ if (option->selected())
+ return option;
+ }
}
return nullptr;
}
@@ -1693,11 +1703,11 @@ int HTMLSelectElement::optionCount() const
String HTMLSelectElement::optionAtIndex(int index) const
{
- const ListItems& items = listItems();
- HTMLElement* element = items[index];
- if (!isHTMLOptionElement(*element) || toHTMLOptionElement(element)->isDisabledFormControl())
- return String();
- return toHTMLOptionElement(element)->displayLabel();
+ if (HTMLOptionElement* option = optionAtListIndex(index)) {
+ if (!option->isDisabledFormControl())
+ return option->displayLabel();
+ }
+ return String();
}
void HTMLSelectElement::typeAheadFind(KeyboardEvent* event)
@@ -1705,10 +1715,7 @@ void HTMLSelectElement::typeAheadFind(KeyboardEvent* event)
int index = m_typeAhead.handleEvent(event, TypeAhead::MatchPrefix | TypeAhead::CycleFirstChar);
if (index < 0)
return;
- HTMLOptionElement* option = nullptr;
- if (static_cast<size_t>(index) < listItems().size() && isHTMLOptionElement(listItems()[index]))
- option = toHTMLOptionElement(listItems()[index]);
- selectOption(option, DeselectOtherOptions | MakeOptionDirty | DispatchInputAndChangeEvent);
+ selectOption(optionAtListIndex(index), DeselectOtherOptions | MakeOptionDirty | DispatchInputAndChangeEvent);
if (!usesMenuList())
listBoxOnChange();
}
@@ -1888,8 +1895,8 @@ void HTMLSelectElement::setIndexToSelectOnCancel(int listIndex)
HTMLOptionElement* HTMLSelectElement::optionToBeShown() const
{
- if (m_indexToSelectOnCancel >= 0 && static_cast<size_t>(m_indexToSelectOnCancel) < listItems().size() && isHTMLOptionElement(listItems()[m_indexToSelectOnCancel]))
- return toHTMLOptionElement(listItems()[m_indexToSelectOnCancel]);
+ if (HTMLOptionElement* option = optionAtListIndex(m_indexToSelectOnCancel))
+ return option;
if (m_suggestedOption)
return m_suggestedOption;
// TODO(tkent): We should not call optionToBeShown() in multiple() case.
@@ -1910,9 +1917,7 @@ void HTMLSelectElement::selectOptionByPopup(int listIndex)
setIndexToSelectOnCancel(-1);
- HTMLOptionElement* option = nullptr;
- if (listIndex >= 0 && isHTMLOptionElement(listItems()[listIndex]))
- option = toHTMLOptionElement(listItems()[listIndex]);
+ HTMLOptionElement* option = optionAtListIndex(listIndex);
// Bail out if this index is already the selected one, to avoid running
// unnecessary JavaScript that can mess up autofill when there is no actual
// change (see https://bugs.webkit.org/show_bug.cgi?id=35256 and
« no previous file with comments | « third_party/WebKit/Source/core/html/HTMLSelectElement.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698