2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
3 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
4 * (C) 1999 Antti Koivisto (koivisto@kde.org)
5 * (C) 2001 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
7 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
8 * Copyright (C) 2010 Google Inc. All rights reserved.
9 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public License
22 * along with this library; see the file COPYING.LIB. If not, write to
23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
29 #include "core/html/HTMLSelectElement.h"
31 #include "bindings/core/v8/ExceptionMessages.h"
32 #include "bindings/core/v8/ExceptionState.h"
33 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
34 #include "core/HTMLNames.h"
35 #include "core/accessibility/AXObjectCache.h"
36 #include "core/dom/Attribute.h"
37 #include "core/dom/ElementTraversal.h"
38 #include "core/dom/NodeListsNodeData.h"
39 #include "core/dom/NodeRenderStyle.h"
40 #include "core/dom/NodeTraversal.h"
41 #include "core/events/GestureEvent.h"
42 #include "core/events/KeyboardEvent.h"
43 #include "core/events/MouseEvent.h"
44 #include "core/frame/FrameView.h"
45 #include "core/frame/LocalFrame.h"
46 #include "core/html/FormDataList.h"
47 #include "core/html/HTMLFormElement.h"
48 #include "core/html/HTMLOptGroupElement.h"
49 #include "core/html/HTMLOptionElement.h"
50 #include "core/html/forms/FormController.h"
51 #include "core/page/AutoscrollController.h"
52 #include "core/page/EventHandler.h"
53 #include "core/page/Page.h"
54 #include "core/page/SpatialNavigation.h"
55 #include "core/rendering/HitTestRequest.h"
56 #include "core/rendering/HitTestResult.h"
57 #include "core/rendering/RenderListBox.h"
58 #include "core/rendering/RenderMenuList.h"
59 #include "core/rendering/RenderTheme.h"
60 #include "core/rendering/RenderView.h"
61 #include "platform/PlatformMouseEvent.h"
62 #include "platform/text/PlatformLocale.h"
64 using namespace WTF::Unicode;
68 using namespace HTMLNames;
70 // Upper limit agreed upon with representatives of Opera and Mozilla.
71 static const unsigned maxSelectItems = 10000;
73 HTMLSelectElement::HTMLSelectElement(Document& document, HTMLFormElement* form)
74 : HTMLFormControlElementWithState(selectTag, document, form)
77 , m_lastOnChangeIndex(-1)
78 , m_activeSelectionAnchorIndex(-1)
79 , m_activeSelectionEndIndex(-1)
80 , m_isProcessingUserDrivenChange(false)
82 , m_activeSelectionState(false)
83 , m_shouldRecalcListItems(false)
84 , m_suggestedIndex(-1)
86 ScriptWrappable::init(this);
89 PassRefPtrWillBeRawPtr<HTMLSelectElement> HTMLSelectElement::create(Document& document)
91 RefPtrWillBeRawPtr<HTMLSelectElement> select = adoptRefWillBeNoop(new HTMLSelectElement(document, 0));
92 select->ensureUserAgentShadowRoot();
93 return select.release();
96 PassRefPtrWillBeRawPtr<HTMLSelectElement> HTMLSelectElement::create(Document& document, HTMLFormElement* form)
98 RefPtrWillBeRawPtr<HTMLSelectElement> select = adoptRefWillBeNoop(new HTMLSelectElement(document, form));
99 select->ensureUserAgentShadowRoot();
100 return select.release();
103 const AtomicString& HTMLSelectElement::formControlType() const
105 DEFINE_STATIC_LOCAL(const AtomicString, selectMultiple, ("select-multiple", AtomicString::ConstructFromLiteral));
106 DEFINE_STATIC_LOCAL(const AtomicString, selectOne, ("select-one", AtomicString::ConstructFromLiteral));
107 return m_multiple ? selectMultiple : selectOne;
110 void HTMLSelectElement::optionSelectedByUser(int optionIndex, bool fireOnChangeNow, bool allowMultipleSelection)
112 // User interaction such as mousedown events can cause list box select elements to send change events.
113 // This produces that same behavior for changes triggered by other code running on behalf of the user.
114 if (!usesMenuList()) {
115 updateSelectedState(optionToListIndex(optionIndex), allowMultipleSelection, false);
116 setNeedsValidityCheck();
122 // Bail out if this index is already the selected one, to avoid running unnecessary JavaScript that can mess up
123 // autofill when there is no actual change (see https://bugs.webkit.org/show_bug.cgi?id=35256 and <rdar://7467917>).
124 // The selectOption function does not behave this way, possibly because other callers need a change event even
125 // in cases where the selected option is not change.
126 if (optionIndex == selectedIndex())
129 selectOption(optionIndex, DeselectOtherOptions | (fireOnChangeNow ? DispatchInputAndChangeEvent : 0) | UserDriven);
132 bool HTMLSelectElement::hasPlaceholderLabelOption() const
134 // The select element has no placeholder label option if it has an attribute "multiple" specified or a display size of non-1.
136 // The condition "size() > 1" is not compliant with the HTML5 spec as of Dec 3, 2010. "size() != 1" is correct.
137 // Using "size() > 1" here because size() may be 0 in WebKit.
138 // See the discussion at https://bugs.webkit.org/show_bug.cgi?id=43887
140 // "0 size()" happens when an attribute "size" is absent or an invalid size attribute is specified.
141 // In this case, the display size should be assumed as the default.
142 // The default display size is 1 for non-multiple select elements, and 4 for multiple select elements.
144 // Finally, if size() == 0 and non-multiple, the display size can be assumed as 1.
145 if (multiple() || size() > 1)
148 int listIndex = optionToListIndex(0);
149 ASSERT(listIndex >= 0);
152 return !listIndex && toHTMLOptionElement(listItems()[listIndex])->value().isEmpty();
155 String HTMLSelectElement::validationMessage() const
160 return customValidationMessage();
162 return locale().queryString(blink::WebLocalizedString::ValidationValueMissingForSelect);
166 bool HTMLSelectElement::valueMissing() const
174 int firstSelectionIndex = selectedIndex();
176 // If a non-placeholer label option is selected (firstSelectionIndex > 0), it's not value-missing.
177 return firstSelectionIndex < 0 || (!firstSelectionIndex && hasPlaceholderLabelOption());
180 void HTMLSelectElement::listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow)
183 optionSelectedByUser(listToOptionIndex(listIndex), fireOnChangeNow, false);
185 updateSelectedState(listIndex, allowMultiplySelections, shift);
186 setNeedsValidityCheck();
192 bool HTMLSelectElement::usesMenuList() const
194 if (RenderTheme::theme().delegatesMenuListRendering())
197 return !m_multiple && m_size <= 1;
200 int HTMLSelectElement::activeSelectionStartListIndex() const
202 if (m_activeSelectionAnchorIndex >= 0)
203 return m_activeSelectionAnchorIndex;
204 return optionToListIndex(selectedIndex());
207 int HTMLSelectElement::activeSelectionEndListIndex() const
209 if (m_activeSelectionEndIndex >= 0)
210 return m_activeSelectionEndIndex;
211 return lastSelectedListIndex();
214 void HTMLSelectElement::add(HTMLElement* element, HTMLElement* before, ExceptionState& exceptionState)
216 // Make sure the element is ref'd and deref'd so we don't leak it.
217 RefPtrWillBeRawPtr<HTMLElement> protectNewChild(element);
219 if (!element || !(isHTMLOptionElement(element) || isHTMLOptGroupElement(element) || isHTMLHRElement(element)))
222 insertBefore(element, before, exceptionState);
223 setNeedsValidityCheck();
226 void HTMLSelectElement::addBeforeOptionAtIndex(HTMLElement* element, int beforeIndex, ExceptionState& exceptionState)
228 HTMLElement* beforeElement = toHTMLElement(options()->item(beforeIndex));
229 add(element, beforeElement, exceptionState);
232 void HTMLSelectElement::remove(int optionIndex)
234 int listIndex = optionToListIndex(optionIndex);
238 listItems()[listIndex]->remove(IGNORE_EXCEPTION);
241 String HTMLSelectElement::value() const
243 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
244 for (unsigned i = 0; i < items.size(); i++) {
245 if (isHTMLOptionElement(items[i]) && toHTMLOptionElement(items[i])->selected())
246 return toHTMLOptionElement(items[i])->value();
251 void HTMLSelectElement::setValue(const String &value, bool sendEvents)
253 // We clear the previously selected option(s) when needed, to guarantee calling setSelectedIndex() only once.
255 if (value.isNull()) {
258 // Find the option with value() matching the given parameter and make it the current selection.
259 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
260 for (unsigned i = 0; i < items.size(); i++) {
261 if (isHTMLOptionElement(items[i])) {
262 if (toHTMLOptionElement(items[i])->value() == value)
267 if (optionIndex >= static_cast<int>(items.size()))
271 int previousSelectedIndex = selectedIndex();
272 setSuggestedIndex(-1);
273 setSelectedIndex(optionIndex);
275 if (sendEvents && previousSelectedIndex != selectedIndex()) {
277 dispatchInputAndChangeEventForMenuList(false);
283 String HTMLSelectElement::suggestedValue() const
285 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
286 for (unsigned i = 0; i < items.size(); ++i) {
287 if (isHTMLOptionElement(items[i]) && m_suggestedIndex >= 0) {
288 if (i == static_cast<unsigned>(m_suggestedIndex))
289 return toHTMLOptionElement(items[i])->value();
295 void HTMLSelectElement::setSuggestedValue(const String& value)
297 if (value.isNull()) {
298 setSuggestedIndex(-1);
302 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
303 unsigned optionIndex = 0;
304 for (unsigned i = 0; i < items.size(); ++i) {
305 if (isHTMLOptionElement(items[i])) {
306 if (toHTMLOptionElement(items[i])->value() == value) {
307 setSuggestedIndex(optionIndex);
314 setSuggestedIndex(-1);
317 bool HTMLSelectElement::isPresentationAttribute(const QualifiedName& name) const
319 if (name == alignAttr) {
320 // Don't map 'align' attribute. This matches what Firefox, Opera and IE do.
321 // See http://bugs.webkit.org/show_bug.cgi?id=12072
325 return HTMLFormControlElementWithState::isPresentationAttribute(name);
328 void HTMLSelectElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
330 if (name == sizeAttr) {
331 int oldSize = m_size;
332 // Set the attribute value to a number.
333 // This is important since the style rules for this attribute can determine the appearance property.
334 int size = value.toInt();
335 AtomicString attrSize = AtomicString::number(size);
336 if (attrSize != value) {
337 // FIXME: This is horribly factored.
338 if (Attribute* sizeAttribute = ensureUniqueElementData().attributes().find(sizeAttr))
339 sizeAttribute->setValue(attrSize);
341 size = std::max(size, 1);
343 // Ensure that we've determined selectedness of the items at least once prior to changing the size.
345 updateListItemSelectedStates();
348 setNeedsValidityCheck();
349 if (m_size != oldSize && inActiveDocument()) {
350 lazyReattachIfAttached();
351 setRecalcListItems();
353 } else if (name == multipleAttr)
354 parseMultipleAttribute(value);
355 else if (name == accesskeyAttr) {
356 // FIXME: ignore for the moment.
358 } else if (name == disabledAttr) {
359 HTMLFormControlElementWithState::parseAttribute(name, value);
360 if (renderer() && renderer()->isMenuList()) {
361 if (RenderMenuList* menuList = toRenderMenuList(renderer())) {
362 if (menuList->popupIsVisible())
363 menuList->hidePopup();
368 HTMLFormControlElementWithState::parseAttribute(name, value);
371 bool HTMLSelectElement::shouldShowFocusRingOnMouseFocus() const
376 bool HTMLSelectElement::canSelectAll() const
378 return !usesMenuList();
381 RenderObject* HTMLSelectElement::createRenderer(RenderStyle*)
384 return new RenderMenuList(this);
385 return new RenderListBox(this);
388 PassRefPtrWillBeRawPtr<HTMLCollection> HTMLSelectElement::selectedOptions()
390 updateListItemSelectedStates();
391 return ensureCachedCollection<HTMLCollection>(SelectedOptions);
394 PassRefPtrWillBeRawPtr<HTMLOptionsCollection> HTMLSelectElement::options()
396 return ensureCachedCollection<HTMLOptionsCollection>(SelectOptions);
399 void HTMLSelectElement::updateListItemSelectedStates()
401 if (!m_shouldRecalcListItems)
404 setNeedsValidityCheck();
407 void HTMLSelectElement::childrenChanged(const ChildrenChange& change)
409 setRecalcListItems();
410 setNeedsValidityCheck();
411 m_lastOnChangeSelection.clear();
413 HTMLFormControlElementWithState::childrenChanged(change);
416 void HTMLSelectElement::optionElementChildrenChanged()
418 setRecalcListItems();
419 setNeedsValidityCheck();
422 if (AXObjectCache* cache = renderer()->document().existingAXObjectCache())
423 cache->childrenChanged(this);
427 void HTMLSelectElement::accessKeyAction(bool sendMouseEvents)
430 dispatchSimulatedClick(0, sendMouseEvents ? SendMouseUpDownEvents : SendNoEvents);
433 void HTMLSelectElement::setMultiple(bool multiple)
435 bool oldMultiple = this->multiple();
436 int oldSelectedIndex = selectedIndex();
437 setAttribute(multipleAttr, multiple ? emptyAtom : nullAtom);
439 // Restore selectedIndex after changing the multiple flag to preserve
440 // selection as single-line and multi-line has different defaults.
441 if (oldMultiple != this->multiple())
442 setSelectedIndex(oldSelectedIndex);
445 void HTMLSelectElement::setSize(int size)
447 setIntegralAttribute(sizeAttr, size);
450 Element* HTMLSelectElement::namedItem(const AtomicString& name)
452 return options()->namedItem(name);
455 Element* HTMLSelectElement::item(unsigned index)
457 return options()->item(index);
460 void HTMLSelectElement::setOption(unsigned index, HTMLOptionElement* option, ExceptionState& exceptionState)
462 if (index > maxSelectItems - 1)
463 index = maxSelectItems - 1;
464 int diff = index - length();
465 RefPtrWillBeRawPtr<HTMLElement> before = nullptr;
466 // Out of array bounds? First insert empty dummies.
468 setLength(index, exceptionState);
469 // Replace an existing entry?
470 } else if (diff < 0) {
471 before = toHTMLElement(options()->item(index+1));
474 // Finally add the new element.
475 if (!exceptionState.hadException()) {
476 add(option, before.get(), exceptionState);
477 if (diff >= 0 && option->selected())
478 optionSelectionStateChanged(option, true);
482 void HTMLSelectElement::setLength(unsigned newLen, ExceptionState& exceptionState)
484 if (newLen > maxSelectItems)
485 newLen = maxSelectItems;
486 int diff = length() - newLen;
488 if (diff < 0) { // Add dummy elements.
490 RefPtrWillBeRawPtr<Element> option = document().createElement(optionTag, false);
492 add(toHTMLElement(option), 0, exceptionState);
493 if (exceptionState.hadException())
497 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
499 // Removing children fires mutation events, which might mutate the DOM further, so we first copy out a list
500 // of elements that we intend to remove then attempt to remove them one at a time.
501 WillBeHeapVector<RefPtrWillBeMember<Element> > itemsToRemove;
502 size_t optionIndex = 0;
503 for (size_t i = 0; i < items.size(); ++i) {
504 Element* item = items[i];
505 if (isHTMLOptionElement(items[i]) && optionIndex++ >= newLen) {
506 ASSERT(item->parentNode());
507 itemsToRemove.append(item);
511 for (size_t i = 0; i < itemsToRemove.size(); ++i) {
512 Element* item = itemsToRemove[i].get();
513 if (item->parentNode())
514 item->parentNode()->removeChild(item, exceptionState);
517 setNeedsValidityCheck();
520 bool HTMLSelectElement::isRequiredFormControl() const
525 // Returns the 1st valid item |skip| items from |listIndex| in direction |direction| if there is one.
526 // Otherwise, it returns the valid item closest to that boundary which is past |listIndex| if there is one.
527 // Otherwise, it returns |listIndex|.
528 // Valid means that it is enabled and an option element.
529 int HTMLSelectElement::nextValidIndex(int listIndex, SkipDirection direction, int skip) const
531 ASSERT(direction == -1 || direction == 1);
532 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = this->listItems();
533 int lastGoodIndex = listIndex;
534 int size = listItems.size();
535 for (listIndex += direction; listIndex >= 0 && listIndex < size; listIndex += direction) {
537 HTMLElement* element = listItems[listIndex];
538 if (!isHTMLOptionElement(*element))
540 if (toHTMLOptionElement(*element).isDisplayNone())
542 if (element->isDisabledFormControl())
544 if (!usesMenuList() && !element->renderer())
546 lastGoodIndex = listIndex;
550 return lastGoodIndex;
553 int HTMLSelectElement::nextSelectableListIndex(int startIndex) const
555 return nextValidIndex(startIndex, SkipForwards, 1);
558 int HTMLSelectElement::previousSelectableListIndex(int startIndex) const
560 if (startIndex == -1)
561 startIndex = listItems().size();
562 return nextValidIndex(startIndex, SkipBackwards, 1);
565 int HTMLSelectElement::firstSelectableListIndex() const
567 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
568 int index = nextValidIndex(items.size(), SkipBackwards, INT_MAX);
569 if (static_cast<size_t>(index) == items.size())
574 int HTMLSelectElement::lastSelectableListIndex() const
576 return nextValidIndex(-1, SkipForwards, INT_MAX);
579 // Returns the index of the next valid item one page away from |startIndex| in direction |direction|.
580 int HTMLSelectElement::nextSelectableListIndexPageAway(int startIndex, SkipDirection direction) const
582 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
583 // Can't use m_size because renderer forces a minimum size.
585 if (renderer()->isListBox())
586 pageSize = toRenderListBox(renderer())->size() - 1; // -1 so we still show context.
588 // One page away, but not outside valid bounds.
589 // If there is a valid option item one page away, the index is chosen.
590 // If there is no exact one page away valid option, returns startIndex or the most far index.
591 int edgeIndex = (direction == SkipForwards) ? 0 : (items.size() - 1);
592 int skipAmount = pageSize + ((direction == SkipForwards) ? startIndex : (edgeIndex - startIndex));
593 return nextValidIndex(edgeIndex, direction, skipAmount);
596 void HTMLSelectElement::selectAll()
598 ASSERT(!usesMenuList());
599 if (!renderer() || !m_multiple)
602 // Save the selection so it can be compared to the new selectAll selection
603 // when dispatching change events.
606 m_activeSelectionState = true;
607 setActiveSelectionAnchorIndex(nextSelectableListIndex(-1));
608 setActiveSelectionEndIndex(previousSelectableListIndex(-1));
610 updateListBoxSelection(false);
612 setNeedsValidityCheck();
615 void HTMLSelectElement::saveLastSelection()
617 if (usesMenuList()) {
618 m_lastOnChangeIndex = selectedIndex();
622 m_lastOnChangeSelection.clear();
623 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
624 for (unsigned i = 0; i < items.size(); ++i) {
625 HTMLElement* element = items[i];
626 m_lastOnChangeSelection.append(isHTMLOptionElement(*element) && toHTMLOptionElement(element)->selected());
630 void HTMLSelectElement::setActiveSelectionAnchorIndex(int index)
632 m_activeSelectionAnchorIndex = index;
634 // Cache the selection state so we can restore the old selection as the new
635 // selection pivots around this anchor index.
636 m_cachedStateForActiveSelection.clear();
638 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
639 for (unsigned i = 0; i < items.size(); ++i) {
640 HTMLElement* element = items[i];
641 m_cachedStateForActiveSelection.append(isHTMLOptionElement(*element) && toHTMLOptionElement(element)->selected());
645 void HTMLSelectElement::setActiveSelectionEndIndex(int index)
647 if (index == m_activeSelectionEndIndex)
649 m_activeSelectionEndIndex = index;
650 setNeedsStyleRecalc(SubtreeStyleChange);
653 void HTMLSelectElement::updateListBoxSelection(bool deselectOtherOptions)
655 ASSERT(renderer() && (renderer()->isListBox() || m_multiple));
656 ASSERT(!listItems().size() || m_activeSelectionAnchorIndex >= 0);
658 unsigned start = std::min(m_activeSelectionAnchorIndex, m_activeSelectionEndIndex);
659 unsigned end = std::max(m_activeSelectionAnchorIndex, m_activeSelectionEndIndex);
661 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
662 for (unsigned i = 0; i < items.size(); ++i) {
663 HTMLElement* element = items[i];
664 if (!isHTMLOptionElement(*element) || toHTMLOptionElement(element)->isDisabledFormControl() || !toHTMLOptionElement(element)->renderer())
667 if (i >= start && i <= end)
668 toHTMLOptionElement(element)->setSelectedState(m_activeSelectionState);
669 else if (deselectOtherOptions || i >= m_cachedStateForActiveSelection.size())
670 toHTMLOptionElement(element)->setSelectedState(false);
672 toHTMLOptionElement(element)->setSelectedState(m_cachedStateForActiveSelection[i]);
675 setNeedsValidityCheck();
677 notifyFormStateChanged();
680 void HTMLSelectElement::listBoxOnChange()
682 ASSERT(!usesMenuList() || m_multiple);
684 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
686 // If the cached selection list is empty, or the size has changed, then fire
687 // dispatchFormControlChangeEvent, and return early.
688 // FIXME: Why? This looks unreasonable.
689 if (m_lastOnChangeSelection.isEmpty() || m_lastOnChangeSelection.size() != items.size()) {
690 dispatchFormControlChangeEvent();
694 // Update m_lastOnChangeSelection and fire dispatchFormControlChangeEvent.
695 bool fireOnChange = false;
696 for (unsigned i = 0; i < items.size(); ++i) {
697 HTMLElement* element = items[i];
698 bool selected = isHTMLOptionElement(*element) && toHTMLOptionElement(element)->selected();
699 if (selected != m_lastOnChangeSelection[i])
701 m_lastOnChangeSelection[i] = selected;
705 RefPtrWillBeRawPtr<HTMLSelectElement> protector(this);
706 dispatchInputEvent();
707 dispatchFormControlChangeEvent();
711 void HTMLSelectElement::dispatchInputAndChangeEventForMenuList(bool requiresUserGesture)
713 ASSERT(usesMenuList());
715 int selected = selectedIndex();
716 if (m_lastOnChangeIndex != selected && (!requiresUserGesture || m_isProcessingUserDrivenChange)) {
717 m_lastOnChangeIndex = selected;
718 m_isProcessingUserDrivenChange = false;
719 RefPtrWillBeRawPtr<HTMLSelectElement> protector(this);
720 dispatchInputEvent();
721 dispatchFormControlChangeEvent();
725 void HTMLSelectElement::scrollToSelection()
727 if (!isFinishedParsingChildren())
731 scrollTo(activeSelectionEndListIndex());
732 if (AXObjectCache* cache = document().existingAXObjectCache())
733 cache->selectedChildrenChanged(this);
736 void HTMLSelectElement::setOptionsChangedOnRenderer()
738 if (RenderObject* renderer = this->renderer()) {
740 toRenderMenuList(renderer)->setOptionsChanged(true);
744 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& HTMLSelectElement::listItems() const
746 if (m_shouldRecalcListItems)
750 WillBeHeapVector<RawPtrWillBeMember<HTMLElement> > items = m_listItems;
751 recalcListItems(false);
752 ASSERT(items == m_listItems);
759 void HTMLSelectElement::invalidateSelectedItems()
761 if (HTMLCollection* collection = cachedCollection<HTMLCollection>(SelectedOptions))
762 collection->invalidateCache();
765 void HTMLSelectElement::setRecalcListItems()
767 // FIXME: This function does a bunch of confusing things depending on if it
768 // is in the document or not.
770 m_shouldRecalcListItems = true;
771 // Manual selection anchor is reset when manipulating the select programmatically.
772 m_activeSelectionAnchorIndex = -1;
773 setOptionsChangedOnRenderer();
774 setNeedsStyleRecalc(SubtreeStyleChange);
776 if (HTMLOptionsCollection* collection = cachedCollection<HTMLOptionsCollection>(SelectOptions))
777 collection->invalidateCache();
780 invalidateSelectedItems();
783 if (AXObjectCache* cache = renderer()->document().existingAXObjectCache())
784 cache->childrenChanged(this);
788 void HTMLSelectElement::recalcListItems(bool updateSelectedStates) const
792 m_shouldRecalcListItems = false;
794 HTMLOptionElement* foundSelected = 0;
795 HTMLOptionElement* firstOption = 0;
796 for (Element* currentElement = ElementTraversal::firstWithin(*this); currentElement; ) {
797 if (!currentElement->isHTMLElement()) {
798 currentElement = ElementTraversal::nextSkippingChildren(*currentElement, this);
801 HTMLElement& current = toHTMLElement(*currentElement);
803 // optgroup tags may not nest. However, both FireFox and IE will
804 // flatten the tree automatically, so we follow suit.
805 // (http://www.w3.org/TR/html401/interact/forms.html#h-17.6)
806 if (isHTMLOptGroupElement(current)) {
807 m_listItems.append(¤t);
808 if (Element* nextElement = ElementTraversal::firstWithin(current)) {
809 currentElement = nextElement;
814 if (isHTMLOptionElement(current)) {
815 m_listItems.append(¤t);
817 if (updateSelectedStates && !m_multiple) {
818 HTMLOptionElement& option = toHTMLOptionElement(current);
820 firstOption = &option;
821 if (option.selected()) {
823 foundSelected->setSelectedState(false);
824 foundSelected = &option;
825 } else if (m_size <= 1 && !foundSelected && !option.isDisabledFormControl()) {
826 foundSelected = &option;
827 foundSelected->setSelectedState(true);
832 if (isHTMLHRElement(current))
833 m_listItems.append(¤t);
835 // In conforming HTML code, only <optgroup> and <option> will be found
836 // within a <select>. We call NodeTraversal::nextSkippingChildren so that we only step
837 // into those tags that we choose to. For web-compat, we should cope
838 // with the case where odd tags like a <div> have been added but we
839 // handle this because such tags have already been removed from the
840 // <select>'s subtree at this point.
841 currentElement = ElementTraversal::nextSkippingChildren(*currentElement, this);
844 if (!foundSelected && m_size <= 1 && firstOption && !firstOption->selected())
845 firstOption->setSelectedState(true);
848 int HTMLSelectElement::selectedIndex() const
852 // Return the number of the first option selected.
853 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
854 for (size_t i = 0; i < items.size(); ++i) {
855 HTMLElement* element = items[i];
856 if (isHTMLOptionElement(*element)) {
857 if (toHTMLOptionElement(*element).selected())
866 void HTMLSelectElement::setSelectedIndex(int index)
868 selectOption(index, DeselectOtherOptions);
871 int HTMLSelectElement::suggestedIndex() const
873 return m_suggestedIndex;
876 void HTMLSelectElement::setSuggestedIndex(int suggestedIndex)
878 m_suggestedIndex = suggestedIndex;
880 if (RenderObject* renderer = this->renderer()) {
881 renderer->updateFromElement();
882 scrollTo(suggestedIndex);
886 void HTMLSelectElement::scrollTo(int listIndex)
890 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
891 int listSize = static_cast<int>(items.size());
892 if (listIndex >= listSize)
894 items[listIndex]->scrollIntoViewIfNeeded(false);
897 void HTMLSelectElement::optionSelectionStateChanged(HTMLOptionElement* option, bool optionIsSelected)
899 ASSERT(option->ownerSelectElement() == this);
900 if (optionIsSelected)
901 selectOption(option->index());
902 else if (!usesMenuList() || multiple())
905 selectOption(nextSelectableListIndex(-1));
908 void HTMLSelectElement::optionRemoved(const HTMLOptionElement& option)
910 if (m_activeSelectionAnchorIndex < 0 && m_activeSelectionEndIndex < 0)
912 int listIndex = optionToListIndex(option.index());
913 if (listIndex <= m_activeSelectionAnchorIndex)
914 m_activeSelectionAnchorIndex--;
915 if (listIndex <= m_activeSelectionEndIndex)
916 m_activeSelectionEndIndex--;
919 void HTMLSelectElement::selectOption(int optionIndex, SelectOptionFlags flags)
921 bool shouldDeselect = !m_multiple || (flags & DeselectOtherOptions);
923 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
924 int listIndex = optionToListIndex(optionIndex);
926 HTMLElement* element = 0;
927 if (listIndex >= 0) {
928 element = items[listIndex];
929 if (isHTMLOptionElement(*element)) {
930 if (m_activeSelectionAnchorIndex < 0 || shouldDeselect)
931 setActiveSelectionAnchorIndex(listIndex);
932 if (m_activeSelectionEndIndex < 0 || shouldDeselect)
933 setActiveSelectionEndIndex(listIndex);
934 toHTMLOptionElement(*element).setSelectedState(true);
939 deselectItemsWithoutValidation(element);
941 // For the menu list case, this is what makes the selected element appear.
942 if (RenderObject* renderer = this->renderer())
943 renderer->updateFromElement();
947 setNeedsValidityCheck();
949 if (usesMenuList()) {
950 m_isProcessingUserDrivenChange = flags & UserDriven;
951 if (flags & DispatchInputAndChangeEvent)
952 dispatchInputAndChangeEventForMenuList();
953 if (RenderObject* renderer = this->renderer()) {
954 if (usesMenuList()) {
955 toRenderMenuList(renderer)->didSetSelectedIndex(listIndex);
956 } else if (renderer->isListBox()) {
957 if (AXObjectCache* cache = document().existingAXObjectCache())
958 cache->selectedChildrenChanged(this);
963 notifyFormStateChanged();
966 int HTMLSelectElement::optionToListIndex(int optionIndex) const
968 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
969 int listSize = static_cast<int>(items.size());
970 if (optionIndex < 0 || optionIndex >= listSize)
973 int optionIndex2 = -1;
974 for (int listIndex = 0; listIndex < listSize; ++listIndex) {
975 if (isHTMLOptionElement(*items[listIndex])) {
977 if (optionIndex2 == optionIndex)
985 int HTMLSelectElement::listToOptionIndex(int listIndex) const
987 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
988 if (listIndex < 0 || listIndex >= static_cast<int>(items.size()) || !isHTMLOptionElement(*items[listIndex]))
991 // Actual index of option not counting OPTGROUP entries that may be in list.
993 for (int i = 0; i < listIndex; ++i) {
994 if (isHTMLOptionElement(*items[i]))
1001 void HTMLSelectElement::dispatchFocusEvent(Element* oldFocusedElement, FocusType type)
1003 // Save the selection so it can be compared to the new selection when
1004 // dispatching change events during blur event dispatch.
1006 saveLastSelection();
1007 HTMLFormControlElementWithState::dispatchFocusEvent(oldFocusedElement, type);
1010 void HTMLSelectElement::dispatchBlurEvent(Element* newFocusedElement)
1012 // We only need to fire change events here for menu lists, because we fire
1013 // change events for list boxes whenever the selection change is actually made.
1014 // This matches other browsers' behavior.
1016 dispatchInputAndChangeEventForMenuList();
1017 HTMLFormControlElementWithState::dispatchBlurEvent(newFocusedElement);
1020 void HTMLSelectElement::deselectItemsWithoutValidation(HTMLElement* excludeElement)
1022 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
1023 for (unsigned i = 0; i < items.size(); ++i) {
1024 HTMLElement* element = items[i];
1025 if (element != excludeElement && isHTMLOptionElement(*element))
1026 toHTMLOptionElement(element)->setSelectedState(false);
1030 FormControlState HTMLSelectElement::saveFormControlState() const
1032 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
1033 size_t length = items.size();
1034 FormControlState state;
1035 for (unsigned i = 0; i < length; ++i) {
1036 if (!isHTMLOptionElement(*items[i]))
1038 HTMLOptionElement* option = toHTMLOptionElement(items[i]);
1039 if (!option->selected())
1041 state.append(option->value());
1048 size_t HTMLSelectElement::searchOptionsForValue(const String& value, size_t listIndexStart, size_t listIndexEnd) const
1050 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
1051 size_t loopEndIndex = std::min(items.size(), listIndexEnd);
1052 for (size_t i = listIndexStart; i < loopEndIndex; ++i) {
1053 if (!isHTMLOptionElement(items[i]))
1055 if (toHTMLOptionElement(items[i])->value() == value)
1061 void HTMLSelectElement::restoreFormControlState(const FormControlState& state)
1065 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
1066 size_t itemsSize = items.size();
1070 for (size_t i = 0; i < itemsSize; ++i) {
1071 if (!isHTMLOptionElement(items[i]))
1073 toHTMLOptionElement(items[i])->setSelectedState(false);
1077 size_t foundIndex = searchOptionsForValue(state[0], 0, itemsSize);
1078 if (foundIndex != kNotFound)
1079 toHTMLOptionElement(items[foundIndex])->setSelectedState(true);
1081 size_t startIndex = 0;
1082 for (size_t i = 0; i < state.valueSize(); ++i) {
1083 const String& value = state[i];
1084 size_t foundIndex = searchOptionsForValue(value, startIndex, itemsSize);
1085 if (foundIndex == kNotFound)
1086 foundIndex = searchOptionsForValue(value, 0, startIndex);
1087 if (foundIndex == kNotFound)
1089 toHTMLOptionElement(items[foundIndex])->setSelectedState(true);
1090 startIndex = foundIndex + 1;
1094 setOptionsChangedOnRenderer();
1095 setNeedsValidityCheck();
1098 void HTMLSelectElement::parseMultipleAttribute(const AtomicString& value)
1100 m_multiple = !value.isNull();
1101 setNeedsValidityCheck();
1103 lazyReattachIfAttached();
1106 bool HTMLSelectElement::appendFormData(FormDataList& list, bool)
1108 const AtomicString& name = this->name();
1112 bool successful = false;
1113 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
1115 for (unsigned i = 0; i < items.size(); ++i) {
1116 HTMLElement* element = items[i];
1117 if (isHTMLOptionElement(*element) && toHTMLOptionElement(*element).selected() && !toHTMLOptionElement(*element).isDisabledFormControl()) {
1118 list.appendData(name, toHTMLOptionElement(*element).value());
1123 // It's possible that this is a menulist with multiple options and nothing
1124 // will be submitted (!successful). We won't send a unselected non-disabled
1125 // option as fallback. This behavior matches to other browsers.
1129 void HTMLSelectElement::resetImpl()
1131 HTMLOptionElement* firstOption = 0;
1132 HTMLOptionElement* selectedOption = 0;
1134 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
1135 for (unsigned i = 0; i < items.size(); ++i) {
1136 HTMLElement* element = items[i];
1137 if (!isHTMLOptionElement(*element))
1140 if (items[i]->fastHasAttribute(selectedAttr)) {
1141 if (selectedOption && !m_multiple)
1142 selectedOption->setSelectedState(false);
1143 toHTMLOptionElement(element)->setSelectedState(true);
1144 selectedOption = toHTMLOptionElement(element);
1146 toHTMLOptionElement(element)->setSelectedState(false);
1149 firstOption = toHTMLOptionElement(element);
1152 if (!selectedOption && firstOption && !m_multiple && m_size <= 1)
1153 firstOption->setSelectedState(true);
1155 setOptionsChangedOnRenderer();
1156 setNeedsStyleRecalc(SubtreeStyleChange);
1157 setNeedsValidityCheck();
1160 void HTMLSelectElement::handlePopupOpenKeyboardEvent(Event* event)
1163 // Calling focus() may cause us to lose our renderer. Return true so
1164 // that our caller doesn't process the event further, but don't set
1165 // the event as handled.
1166 if (!renderer() || !renderer()->isMenuList() || isDisabledFormControl())
1168 // Save the selection so it can be compared to the new selection
1169 // when dispatching change events during selectOption, which
1170 // gets called from RenderMenuList::valueChanged, which gets called
1171 // after the user makes a selection from the menu.
1172 saveLastSelection();
1173 if (RenderMenuList* menuList = toRenderMenuList(renderer()))
1174 menuList->showPopup();
1175 int index = selectedIndex();
1177 ASSERT_WITH_SECURITY_IMPLICATION(index < static_cast<int>(listItems().size()));
1178 setSelectedIndex(index);
1179 event->setDefaultHandled();
1183 bool HTMLSelectElement::shouldOpenPopupForKeyDownEvent(KeyboardEvent* keyEvent)
1185 const String& keyIdentifier = keyEvent->keyIdentifier();
1186 RenderTheme& renderTheme = RenderTheme::theme();
1188 if (isSpatialNavigationEnabled(document().frame()))
1191 return ((renderTheme.popsMenuByArrowKeys() && (keyIdentifier == "Down" || keyIdentifier == "Up"))
1192 || (renderTheme.popsMenuByAltDownUpOrF4Key() && (keyIdentifier == "Down" || keyIdentifier == "Up") && keyEvent->altKey())
1193 || (renderTheme.popsMenuByAltDownUpOrF4Key() && (!keyEvent->altKey() && !keyEvent->ctrlKey() && keyIdentifier == "F4")));
1196 bool HTMLSelectElement::shouldOpenPopupForKeyPressEvent(KeyboardEvent *event)
1198 RenderTheme& renderTheme = RenderTheme::theme();
1199 int keyCode = event->keyCode();
1201 return ((renderTheme.popsMenuBySpaceKey() && event->keyCode() == ' ')
1202 || (renderTheme.popsMenuByReturnKey() && keyCode == '\r'));
1205 void HTMLSelectElement::menuListDefaultEventHandler(Event* event)
1207 if (event->type() == EventTypeNames::keydown) {
1208 if (!renderer() || !event->isKeyboardEvent())
1211 KeyboardEvent* keyEvent = toKeyboardEvent(event);
1212 if (shouldOpenPopupForKeyDownEvent(keyEvent)) {
1213 handlePopupOpenKeyboardEvent(event);
1217 // When using spatial navigation, we want to be able to navigate away
1218 // from the select element when the user hits any of the arrow keys,
1219 // instead of changing the selection.
1220 if (isSpatialNavigationEnabled(document().frame())) {
1221 if (!m_activeSelectionState)
1225 // The key handling below shouldn't be used for non spatial navigation mode Mac
1226 if (RenderTheme::theme().popsMenuByArrowKeys() && !isSpatialNavigationEnabled(document().frame()))
1229 const String& keyIdentifier = keyEvent->keyIdentifier();
1230 bool handled = true;
1231 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = this->listItems();
1232 int listIndex = optionToListIndex(selectedIndex());
1234 if (keyIdentifier == "Down" || keyIdentifier == "Right")
1235 listIndex = nextValidIndex(listIndex, SkipForwards, 1);
1236 else if (keyIdentifier == "Up" || keyIdentifier == "Left")
1237 listIndex = nextValidIndex(listIndex, SkipBackwards, 1);
1238 else if (keyIdentifier == "PageDown")
1239 listIndex = nextValidIndex(listIndex, SkipForwards, 3);
1240 else if (keyIdentifier == "PageUp")
1241 listIndex = nextValidIndex(listIndex, SkipBackwards, 3);
1242 else if (keyIdentifier == "Home")
1243 listIndex = nextValidIndex(-1, SkipForwards, 1);
1244 else if (keyIdentifier == "End")
1245 listIndex = nextValidIndex(listItems.size(), SkipBackwards, 1);
1249 if (handled && static_cast<size_t>(listIndex) < listItems.size())
1250 selectOption(listToOptionIndex(listIndex), DeselectOtherOptions | DispatchInputAndChangeEvent | UserDriven);
1253 event->setDefaultHandled();
1256 if (event->type() == EventTypeNames::keypress) {
1257 if (!renderer() || !event->isKeyboardEvent())
1260 int keyCode = toKeyboardEvent(event)->keyCode();
1261 if (keyCode == ' ' && isSpatialNavigationEnabled(document().frame())) {
1262 // Use space to toggle arrow key handling for selection change or spatial navigation.
1263 m_activeSelectionState = !m_activeSelectionState;
1264 event->setDefaultHandled();
1268 KeyboardEvent* keyEvent = toKeyboardEvent(event);
1269 if (shouldOpenPopupForKeyPressEvent(keyEvent)) {
1270 handlePopupOpenKeyboardEvent(event);
1274 if (!RenderTheme::theme().popsMenuByReturnKey() && keyCode == '\r') {
1276 form()->submitImplicitly(event, false);
1277 dispatchInputAndChangeEventForMenuList();
1278 event->setDefaultHandled();
1282 if (event->type() == EventTypeNames::mousedown && event->isMouseEvent() && toMouseEvent(event)->button() == LeftButton) {
1284 if (renderer() && renderer()->isMenuList() && !isDisabledFormControl()) {
1285 if (RenderMenuList* menuList = toRenderMenuList(renderer())) {
1286 if (menuList->popupIsVisible())
1287 menuList->hidePopup();
1289 // Save the selection so it can be compared to the new
1290 // selection when we call onChange during selectOption,
1291 // which gets called from RenderMenuList::valueChanged,
1292 // which gets called after the user makes a selection from
1294 saveLastSelection();
1295 menuList->showPopup();
1299 event->setDefaultHandled();
1302 if (event->type() == EventTypeNames::blur) {
1303 if (RenderMenuList* menuList = toRenderMenuList(renderer())) {
1304 if (menuList->popupIsVisible())
1305 menuList->hidePopup();
1310 void HTMLSelectElement::updateSelectedState(int listIndex, bool multi, bool shift)
1312 ASSERT(listIndex >= 0);
1314 HTMLElement* clickedElement = listItems()[listIndex];
1315 ASSERT(clickedElement);
1316 if (isHTMLOptGroupElement(clickedElement))
1319 // Save the selection so it can be compared to the new selection when
1320 // dispatching change events during mouseup, or after autoscroll finishes.
1321 saveLastSelection();
1323 m_activeSelectionState = true;
1325 bool shiftSelect = m_multiple && shift;
1326 bool multiSelect = m_multiple && multi && !shift;
1328 if (isHTMLOptionElement(*clickedElement)) {
1329 // Keep track of whether an active selection (like during drag
1330 // selection), should select or deselect.
1331 if (toHTMLOptionElement(*clickedElement).selected() && multiSelect)
1332 m_activeSelectionState = false;
1333 if (!m_activeSelectionState)
1334 toHTMLOptionElement(*clickedElement).setSelectedState(false);
1337 // If we're not in any special multiple selection mode, then deselect all
1338 // other items, excluding the clicked option. If no option was clicked, then
1339 // this will deselect all items in the list.
1340 if (!shiftSelect && !multiSelect)
1341 deselectItemsWithoutValidation(clickedElement);
1343 // If the anchor hasn't been set, and we're doing a single selection or a
1344 // shift selection, then initialize the anchor to the first selected index.
1345 if (m_activeSelectionAnchorIndex < 0 && !multiSelect)
1346 setActiveSelectionAnchorIndex(selectedIndex());
1348 // Set the selection state of the clicked option.
1349 if (isHTMLOptionElement(*clickedElement) && !toHTMLOptionElement(*clickedElement).isDisabledFormControl())
1350 toHTMLOptionElement(*clickedElement).setSelectedState(true);
1352 // If there was no selectedIndex() for the previous initialization, or If
1353 // we're doing a single selection, or a multiple selection (using cmd or
1354 // ctrl), then initialize the anchor index to the listIndex that just got
1356 if (m_activeSelectionAnchorIndex < 0 || !shiftSelect)
1357 setActiveSelectionAnchorIndex(listIndex);
1359 setActiveSelectionEndIndex(listIndex);
1360 updateListBoxSelection(!multiSelect);
1363 int HTMLSelectElement::listIndexForEventTargetOption(const Event& event)
1365 Node* targetNode = event.target()->toNode();
1366 if (!targetNode || !isHTMLOptionElement(*targetNode))
1368 return listIndexForOption(toHTMLOptionElement(*targetNode));
1371 int HTMLSelectElement::listIndexForOption(const HTMLOptionElement& option)
1373 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = this->listItems();
1374 size_t length = items.size();
1375 for (size_t i = 0; i < length; ++i) {
1376 if (items[i].get() == &option)
1382 AutoscrollController* HTMLSelectElement::autoscrollController() const
1384 if (Page* page = document().page())
1385 return &page->autoscrollController();
1389 void HTMLSelectElement::handleMouseRelease()
1391 // We didn't start this click/drag on any options.
1392 if (m_lastOnChangeSelection.isEmpty())
1397 void HTMLSelectElement::listBoxDefaultEventHandler(Event* event)
1399 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = this->listItems();
1400 if (event->type() == EventTypeNames::gesturetap && event->isGestureEvent()) {
1402 // Calling focus() may cause us to lose our renderer or change the render type, in which case do not want to handle the event.
1403 if (!renderer() || !renderer()->isListBox())
1406 // Convert to coords relative to the list box if needed.
1407 GestureEvent& gestureEvent = toGestureEvent(*event);
1408 int listIndex = listIndexForEventTargetOption(gestureEvent);
1409 if (listIndex >= 0) {
1410 if (!isDisabledFormControl()) {
1411 updateSelectedState(listIndex, true, gestureEvent.shiftKey());
1414 event->setDefaultHandled();
1416 } else if (event->type() == EventTypeNames::mousedown && event->isMouseEvent() && toMouseEvent(event)->button() == LeftButton) {
1418 // Calling focus() may cause us to lose our renderer, in which case do not want to handle the event.
1419 if (!renderer() || !renderer()->isListBox() || isDisabledFormControl())
1422 // Convert to coords relative to the list box if needed.
1423 MouseEvent* mouseEvent = toMouseEvent(event);
1424 int listIndex = listIndexForEventTargetOption(*mouseEvent);
1425 if (listIndex >= 0) {
1426 if (!isDisabledFormControl()) {
1428 updateSelectedState(listIndex, mouseEvent->metaKey(), mouseEvent->shiftKey());
1430 updateSelectedState(listIndex, mouseEvent->ctrlKey(), mouseEvent->shiftKey());
1433 if (LocalFrame* frame = document().frame())
1434 frame->eventHandler().setMouseDownMayStartAutoscroll();
1436 event->setDefaultHandled();
1438 } else if (event->type() == EventTypeNames::mousemove && event->isMouseEvent()) {
1439 MouseEvent* mouseEvent = toMouseEvent(event);
1440 if (mouseEvent->button() != LeftButton || !mouseEvent->buttonDown())
1443 if (Page* page = document().page())
1444 page->autoscrollController().startAutoscrollForSelection(renderer());
1446 int listIndex = listIndexForEventTargetOption(*mouseEvent);
1447 if (listIndex >= 0) {
1448 if (!isDisabledFormControl()) {
1450 // Only extend selection if there is something selected.
1451 if (m_activeSelectionAnchorIndex < 0)
1454 setActiveSelectionEndIndex(listIndex);
1455 updateListBoxSelection(false);
1457 setActiveSelectionAnchorIndex(listIndex);
1458 setActiveSelectionEndIndex(listIndex);
1459 updateListBoxSelection(true);
1463 } else if (event->type() == EventTypeNames::mouseup && event->isMouseEvent() && toMouseEvent(event)->button() == LeftButton && renderer()) {
1464 if (document().page() && document().page()->autoscrollController().autoscrollInProgress(toRenderBox(renderer())))
1465 document().page()->autoscrollController().stopAutoscroll();
1467 handleMouseRelease();
1468 } else if (event->type() == EventTypeNames::keydown) {
1469 if (!event->isKeyboardEvent())
1471 const String& keyIdentifier = toKeyboardEvent(event)->keyIdentifier();
1473 bool handled = false;
1475 if (m_activeSelectionEndIndex < 0) {
1476 // Initialize the end index
1477 if (keyIdentifier == "Down" || keyIdentifier == "PageDown") {
1478 int startIndex = lastSelectedListIndex();
1480 if (keyIdentifier == "Down")
1481 endIndex = nextSelectableListIndex(startIndex);
1483 endIndex = nextSelectableListIndexPageAway(startIndex, SkipForwards);
1484 } else if (keyIdentifier == "Up" || keyIdentifier == "PageUp") {
1485 int startIndex = optionToListIndex(selectedIndex());
1487 if (keyIdentifier == "Up")
1488 endIndex = previousSelectableListIndex(startIndex);
1490 endIndex = nextSelectableListIndexPageAway(startIndex, SkipBackwards);
1493 // Set the end index based on the current end index.
1494 if (keyIdentifier == "Down") {
1495 endIndex = nextSelectableListIndex(m_activeSelectionEndIndex);
1497 } else if (keyIdentifier == "Up") {
1498 endIndex = previousSelectableListIndex(m_activeSelectionEndIndex);
1500 } else if (keyIdentifier == "PageDown") {
1501 endIndex = nextSelectableListIndexPageAway(m_activeSelectionEndIndex, SkipForwards);
1503 } else if (keyIdentifier == "PageUp") {
1504 endIndex = nextSelectableListIndexPageAway(m_activeSelectionEndIndex, SkipBackwards);
1508 if (keyIdentifier == "Home") {
1509 endIndex = firstSelectableListIndex();
1511 } else if (keyIdentifier == "End") {
1512 endIndex = lastSelectableListIndex();
1516 if (isSpatialNavigationEnabled(document().frame()))
1517 // Check if the selection moves to the boundary.
1518 if (keyIdentifier == "Left" || keyIdentifier == "Right" || ((keyIdentifier == "Down" || keyIdentifier == "Up") && endIndex == m_activeSelectionEndIndex))
1521 if (endIndex >= 0 && handled) {
1522 // Save the selection so it can be compared to the new selection
1523 // when dispatching change events immediately after making the new
1525 saveLastSelection();
1527 ASSERT_UNUSED(listItems, !listItems.size() || static_cast<size_t>(endIndex) < listItems.size());
1528 setActiveSelectionEndIndex(endIndex);
1530 bool selectNewItem = !m_multiple || toKeyboardEvent(event)->shiftKey() || !isSpatialNavigationEnabled(document().frame());
1532 m_activeSelectionState = true;
1533 // If the anchor is unitialized, or if we're going to deselect all
1534 // other options, then set the anchor index equal to the end index.
1535 bool deselectOthers = !m_multiple || (!toKeyboardEvent(event)->shiftKey() && selectNewItem);
1536 if (m_activeSelectionAnchorIndex < 0 || deselectOthers) {
1538 deselectItemsWithoutValidation();
1539 setActiveSelectionAnchorIndex(m_activeSelectionEndIndex);
1543 if (selectNewItem) {
1544 updateListBoxSelection(deselectOthers);
1547 scrollToSelection();
1549 event->setDefaultHandled();
1551 } else if (event->type() == EventTypeNames::keypress) {
1552 if (!event->isKeyboardEvent())
1554 int keyCode = toKeyboardEvent(event)->keyCode();
1556 if (keyCode == '\r') {
1558 form()->submitImplicitly(event, false);
1559 event->setDefaultHandled();
1560 } else if (m_multiple && keyCode == ' ' && isSpatialNavigationEnabled(document().frame())) {
1561 // Use space to toggle selection change.
1562 m_activeSelectionState = !m_activeSelectionState;
1563 updateSelectedState(listToOptionIndex(m_activeSelectionEndIndex), true /*multi*/, false /*shift*/);
1565 event->setDefaultHandled();
1570 void HTMLSelectElement::defaultEventHandler(Event* event)
1575 if (isDisabledFormControl()) {
1576 HTMLFormControlElementWithState::defaultEventHandler(event);
1581 menuListDefaultEventHandler(event);
1583 listBoxDefaultEventHandler(event);
1584 if (event->defaultHandled())
1587 if (event->type() == EventTypeNames::keypress && event->isKeyboardEvent()) {
1588 KeyboardEvent* keyboardEvent = toKeyboardEvent(event);
1589 if (!keyboardEvent->ctrlKey() && !keyboardEvent->altKey() && !keyboardEvent->metaKey() && isPrintableChar(keyboardEvent->charCode())) {
1590 typeAheadFind(keyboardEvent);
1591 event->setDefaultHandled();
1595 HTMLFormControlElementWithState::defaultEventHandler(event);
1598 int HTMLSelectElement::lastSelectedListIndex() const
1600 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
1601 for (size_t i = items.size(); i;) {
1602 HTMLElement* element = items[--i];
1603 if (isHTMLOptionElement(*element) && toHTMLOptionElement(element)->selected())
1609 int HTMLSelectElement::indexOfSelectedOption() const
1611 return optionToListIndex(selectedIndex());
1614 int HTMLSelectElement::optionCount() const
1616 return listItems().size();
1619 String HTMLSelectElement::optionAtIndex(int index) const
1621 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
1623 HTMLElement* element = items[index];
1624 if (!isHTMLOptionElement(*element) || toHTMLOptionElement(element)->isDisabledFormControl())
1626 return toHTMLOptionElement(element)->textIndentedToRespectGroupLabel();
1629 void HTMLSelectElement::typeAheadFind(KeyboardEvent* event)
1631 int index = m_typeAhead.handleEvent(event, TypeAhead::MatchPrefix | TypeAhead::CycleFirstChar);
1634 selectOption(listToOptionIndex(index), DeselectOtherOptions | DispatchInputAndChangeEvent | UserDriven);
1635 if (!usesMenuList())
1639 Node::InsertionNotificationRequest HTMLSelectElement::insertedInto(ContainerNode* insertionPoint)
1641 // When the element is created during document parsing, it won't have any
1642 // items yet - but for innerHTML and related methods, this method is called
1643 // after the whole subtree is constructed.
1645 HTMLFormControlElementWithState::insertedInto(insertionPoint);
1646 return InsertionDone;
1649 void HTMLSelectElement::accessKeySetSelectedIndex(int index)
1651 // First bring into focus the list box.
1653 accessKeyAction(false);
1655 // If this index is already selected, unselect. otherwise update the selected index.
1656 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
1657 int listIndex = optionToListIndex(index);
1658 if (listIndex >= 0) {
1659 HTMLElement* element = items[listIndex];
1660 if (isHTMLOptionElement(*element)) {
1661 if (toHTMLOptionElement(*element).selected())
1662 toHTMLOptionElement(*element).setSelectedState(false);
1664 selectOption(index, DispatchInputAndChangeEvent | UserDriven);
1669 dispatchInputAndChangeEventForMenuList();
1673 scrollToSelection();
1676 unsigned HTMLSelectElement::length() const
1678 unsigned options = 0;
1680 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& items = listItems();
1681 for (unsigned i = 0; i < items.size(); ++i) {
1682 if (isHTMLOptionElement(*items[i]))
1689 void HTMLSelectElement::finishParsingChildren()
1691 HTMLFormControlElementWithState::finishParsingChildren();
1692 updateListItemSelectedStates();
1693 scrollToSelection();
1696 bool HTMLSelectElement::anonymousIndexedSetter(unsigned index, PassRefPtrWillBeRawPtr<HTMLOptionElement> value, ExceptionState& exceptionState)
1698 if (!value) { // undefined or null
1702 setOption(index, value.get(), exceptionState);
1706 bool HTMLSelectElement::isInteractiveContent() const
1711 bool HTMLSelectElement::supportsAutofocus() const
1716 void HTMLSelectElement::updateListOnRenderer()
1718 setOptionsChangedOnRenderer();
1721 void HTMLSelectElement::trace(Visitor* visitor)
1724 visitor->trace(m_listItems);
1726 HTMLFormControlElementWithState::trace(visitor);
1729 void HTMLSelectElement::didAddUserAgentShadowRoot(ShadowRoot& root)
1731 RefPtrWillBeRawPtr<HTMLContentElement> content = HTMLContentElement::create(document());
1732 content->setAttribute(selectAttr, "option,optgroup");
1733 root.appendChild(content);
1736 HTMLOptionElement* HTMLSelectElement::spatialNavigationFocusedOption()
1738 if (!isSpatialNavigationEnabled(document().frame()))
1740 int focusedIndex = activeSelectionEndListIndex();
1741 if (focusedIndex < 0)
1742 focusedIndex = firstSelectableListIndex();
1743 if (focusedIndex < 0)
1745 HTMLElement* focused = listItems()[focusedIndex];
1746 return isHTMLOptionElement(focused) ? toHTMLOptionElement(focused) : nullptr;