1/*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
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 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.
16 *
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.
27 */
28
29#include "config.h"
30#include "AccessibilityListBoxOption.h"
31
32#include "AXObjectCache.h"
33#include "AccessibilityListBox.h"
34#include "Element.h"
35#include "HTMLElement.h"
36#include "HTMLNames.h"
37#include "HTMLOptGroupElement.h"
38#include "HTMLOptionElement.h"
39#include "HTMLSelectElement.h"
40#include "IntRect.h"
41#include "RenderListBox.h"
42#include "RenderObject.h"
43
44namespace WebCore {
45
46using namespace HTMLNames;
47
48AccessibilityListBoxOption::AccessibilityListBoxOption()
49 : m_optionElement(nullptr)
50{
51}
52
53AccessibilityListBoxOption::~AccessibilityListBoxOption() = default;
54
55Ref<AccessibilityListBoxOption> AccessibilityListBoxOption::create()
56{
57 return adoptRef(*new AccessibilityListBoxOption());
58}
59
60bool AccessibilityListBoxOption::isEnabled() const
61{
62 if (is<HTMLOptGroupElement>(m_optionElement))
63 return false;
64
65 if (equalLettersIgnoringASCIICase(getAttribute(aria_disabledAttr), "true"))
66 return false;
67
68 if (m_optionElement->hasAttributeWithoutSynchronization(disabledAttr))
69 return false;
70
71 return true;
72}
73
74bool AccessibilityListBoxOption::isSelected() const
75{
76 if (!is<HTMLOptionElement>(m_optionElement))
77 return false;
78
79 return downcast<HTMLOptionElement>(*m_optionElement).selected();
80}
81
82bool AccessibilityListBoxOption::isSelectedOptionActive() const
83{
84 HTMLSelectElement* listBoxParentNode = listBoxOptionParentNode();
85 if (!listBoxParentNode)
86 return false;
87
88 return listBoxParentNode->activeSelectionEndListIndex() == listBoxOptionIndex();
89}
90
91LayoutRect AccessibilityListBoxOption::elementRect() const
92{
93 LayoutRect rect;
94 if (!m_optionElement)
95 return rect;
96
97 HTMLSelectElement* listBoxParentNode = listBoxOptionParentNode();
98 if (!listBoxParentNode)
99 return rect;
100
101 RenderElement* listBoxRenderer = listBoxParentNode->renderer();
102 if (!listBoxRenderer)
103 return rect;
104
105 LayoutRect parentRect = listBoxRenderer->document().axObjectCache()->getOrCreate(listBoxRenderer)->boundingBoxRect();
106 int index = listBoxOptionIndex();
107 if (index != -1)
108 rect = downcast<RenderListBox>(*listBoxRenderer).itemBoundingBoxRect(parentRect.location(), index);
109
110 return rect;
111}
112
113bool AccessibilityListBoxOption::computeAccessibilityIsIgnored() const
114{
115 if (!m_optionElement)
116 return true;
117
118 if (accessibilityIsIgnoredByDefault())
119 return true;
120
121 return parentObject()->accessibilityIsIgnored();
122}
123
124bool AccessibilityListBoxOption::canSetSelectedAttribute() const
125{
126 if (!is<HTMLOptionElement>(m_optionElement))
127 return false;
128
129 if (m_optionElement->isDisabledFormControl())
130 return false;
131
132 HTMLSelectElement* selectElement = listBoxOptionParentNode();
133 if (selectElement && selectElement->isDisabledFormControl())
134 return false;
135
136 return true;
137}
138
139String AccessibilityListBoxOption::stringValue() const
140{
141 if (!m_optionElement)
142 return String();
143
144 const AtomicString& ariaLabel = getAttribute(aria_labelAttr);
145 if (!ariaLabel.isNull())
146 return ariaLabel;
147
148 if (is<HTMLOptionElement>(*m_optionElement))
149 return downcast<HTMLOptionElement>(*m_optionElement).label();
150
151 if (is<HTMLOptGroupElement>(*m_optionElement))
152 return downcast<HTMLOptGroupElement>(*m_optionElement).groupLabelText();
153
154 return String();
155}
156
157Element* AccessibilityListBoxOption::actionElement() const
158{
159 return m_optionElement;
160}
161
162AccessibilityObject* AccessibilityListBoxOption::parentObject() const
163{
164 HTMLSelectElement* parentNode = listBoxOptionParentNode();
165 if (!parentNode)
166 return nullptr;
167
168 return m_optionElement->document().axObjectCache()->getOrCreate(parentNode);
169}
170
171void AccessibilityListBoxOption::setSelected(bool selected)
172{
173 HTMLSelectElement* selectElement = listBoxOptionParentNode();
174 if (!selectElement)
175 return;
176
177 if (!canSetSelectedAttribute())
178 return;
179
180 bool isOptionSelected = isSelected();
181 if ((isOptionSelected && selected) || (!isOptionSelected && !selected))
182 return;
183
184 // Convert from the entire list index to the option index.
185 int optionIndex = selectElement->listToOptionIndex(listBoxOptionIndex());
186 selectElement->accessKeySetSelectedIndex(optionIndex);
187}
188
189HTMLSelectElement* AccessibilityListBoxOption::listBoxOptionParentNode() const
190{
191 if (!m_optionElement)
192 return nullptr;
193
194 if (is<HTMLOptionElement>(*m_optionElement))
195 return downcast<HTMLOptionElement>(*m_optionElement).ownerSelectElement();
196
197 if (is<HTMLOptGroupElement>(*m_optionElement))
198 return downcast<HTMLOptGroupElement>(*m_optionElement).ownerSelectElement();
199
200 return nullptr;
201}
202
203int AccessibilityListBoxOption::listBoxOptionIndex() const
204{
205 if (!m_optionElement)
206 return -1;
207
208 HTMLSelectElement* selectElement = listBoxOptionParentNode();
209 if (!selectElement)
210 return -1;
211
212 const auto& listItems = selectElement->listItems();
213 unsigned length = listItems.size();
214 for (unsigned i = 0; i < length; i++)
215 if (listItems[i] == m_optionElement)
216 return i;
217
218 return -1;
219}
220
221} // namespace WebCore
222