2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "core/accessibility/AXListBoxOption.h"
32 #include "core/accessibility/AXObjectCache.h"
33 #include "core/html/HTMLOptGroupElement.h"
34 #include "core/html/HTMLOptionElement.h"
35 #include "core/html/HTMLSelectElement.h"
36 #include "core/rendering/RenderListBox.h"
41 using namespace HTMLNames;
43 AXListBoxOption::AXListBoxOption()
48 AXListBoxOption::~AXListBoxOption()
52 PassRefPtr<AXListBoxOption> AXListBoxOption::create()
54 return adoptRef(new AXListBoxOption());
57 bool AXListBoxOption::isEnabled() const
62 if (isHTMLOptGroupElement(*m_optionElement))
65 if (equalIgnoringCase(getAttribute(aria_disabledAttr), "true"))
68 if (m_optionElement->hasAttribute(disabledAttr))
74 bool AXListBoxOption::isSelected() const
76 if (!isHTMLOptionElement(m_optionElement))
79 return toHTMLOptionElement(m_optionElement)->selected();
82 bool AXListBoxOption::isSelectedOptionActive() const
84 HTMLSelectElement* listBoxParentNode = listBoxOptionParentNode();
85 if (!listBoxParentNode)
88 return listBoxParentNode->activeSelectionEndListIndex() == listBoxOptionIndex();
91 LayoutRect AXListBoxOption::elementRect() const
97 HTMLSelectElement* listBoxParentNode = listBoxOptionParentNode();
98 if (!listBoxParentNode)
101 RenderObject* listBoxRenderer = listBoxParentNode->renderer();
102 if (!listBoxRenderer)
105 LayoutRect parentRect = listBoxRenderer->document().axObjectCache()->getOrCreate(listBoxRenderer)->elementRect();
106 int index = listBoxOptionIndex();
108 rect = toRenderListBox(listBoxRenderer)->itemBoundingBoxRect(parentRect.location(), index);
113 bool AXListBoxOption::computeAccessibilityIsIgnored() const
115 if (!m_optionElement)
118 if (accessibilityIsIgnoredByDefault())
121 return parentObject()->accessibilityIsIgnored();
124 bool AXListBoxOption::canSetSelectedAttribute() const
126 if (!isHTMLOptionElement(m_optionElement))
129 if (m_optionElement->isDisabledFormControl())
132 HTMLSelectElement* selectElement = listBoxOptionParentNode();
133 if (selectElement && selectElement->isDisabledFormControl())
139 String AXListBoxOption::stringValue() const
141 if (!m_optionElement)
144 const AtomicString& ariaLabel = getAttribute(aria_labelAttr);
145 if (!ariaLabel.isNull())
148 if (isHTMLOptionElement(*m_optionElement))
149 return toHTMLOptionElement(m_optionElement)->text();
151 if (isHTMLOptGroupElement(*m_optionElement))
152 return toHTMLOptGroupElement(m_optionElement)->groupLabelText();
157 Element* AXListBoxOption::actionElement() const
159 return m_optionElement;
162 AXObject* AXListBoxOption::parentObject() const
164 HTMLSelectElement* parentNode = listBoxOptionParentNode();
168 return m_optionElement->document().axObjectCache()->getOrCreate(parentNode);
171 void AXListBoxOption::setSelected(bool selected)
173 HTMLSelectElement* selectElement = listBoxOptionParentNode();
177 if (!canSetSelectedAttribute())
180 bool isOptionSelected = isSelected();
181 if ((isOptionSelected && selected) || (!isOptionSelected && !selected))
184 // Convert from the entire list index to the option index.
185 int optionIndex = selectElement->listToOptionIndex(listBoxOptionIndex());
186 selectElement->accessKeySetSelectedIndex(optionIndex);
189 HTMLSelectElement* AXListBoxOption::listBoxOptionParentNode() const
191 if (!m_optionElement)
194 if (isHTMLOptionElement(*m_optionElement))
195 return toHTMLOptionElement(m_optionElement)->ownerSelectElement();
197 if (isHTMLOptGroupElement(*m_optionElement))
198 return toHTMLOptGroupElement(m_optionElement)->ownerSelectElement();
203 int AXListBoxOption::listBoxOptionIndex() const
205 if (!m_optionElement)
208 HTMLSelectElement* selectElement = listBoxOptionParentNode();
212 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = selectElement->listItems();
213 unsigned length = listItems.size();
214 for (unsigned i = 0; i < length; i++) {
215 if (listItems[i] == m_optionElement)
222 } // namespace WebCore