summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/accessibility/ax_menu_list_option.cc
blob: 9489ed52acf8848dbb83aaccf1cef1885b7087c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (C) 2010 Apple Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/modules/accessibility/ax_menu_list_option.h"

#include "third_party/blink/renderer/core/aom/accessible_node.h"
#include "third_party/blink/renderer/core/html/forms/html_select_element.h"
#include "third_party/blink/renderer/modules/accessibility/ax_menu_list.h"
#include "third_party/blink/renderer/modules/accessibility/ax_menu_list_popup.h"
#include "third_party/blink/renderer/modules/accessibility/ax_object_cache_impl.h"
#include "third_party/skia/include/core/SkMatrix44.h"

namespace blink {

AXMenuListOption::AXMenuListOption(HTMLOptionElement* element,
                                   AXObjectCacheImpl& ax_object_cache)
    : AXNodeObject(element, ax_object_cache), element_(element) {}

AXMenuListOption::~AXMenuListOption() {
  DCHECK(!element_);
}

void AXMenuListOption::Detach() {
  element_ = nullptr;
  AXNodeObject::Detach();
}

LocalFrameView* AXMenuListOption::DocumentFrameView() const {
  if (IsDetached())
    return nullptr;
  return element_->GetDocument().View();
}

ax::mojom::Role AXMenuListOption::RoleValue() const {
  const AtomicString& aria_role =
      GetAOMPropertyOrARIAAttribute(AOMStringProperty::kRole);
  if (aria_role.IsEmpty())
    return ax::mojom::Role::kMenuListOption;

  ax::mojom::Role role = AriaRoleToWebCoreRole(aria_role);
  if (role != ax::mojom::Role::kUnknown)
    return role;
  return ax::mojom::Role::kMenuListOption;
}

Element* AXMenuListOption::ActionElement() const {
  return element_;
}

AXObject* AXMenuListOption::ComputeParent() const {
  Node* node = GetNode();
  if (!node)
    return nullptr;
  auto* select = To<HTMLOptionElement>(node)->OwnerSelectElement();
  if (!select)
    return nullptr;
  AXObject* select_ax_object = AXObjectCache().GetOrCreate(select);
  if (!select_ax_object)
    return nullptr;

  // This happens if the <select> is not rendered. Return it and move on.
  auto* menu_list = DynamicTo<AXMenuList>(select_ax_object);
  if (!menu_list)
    return select_ax_object;

  if (menu_list->HasChildren()) {
    const auto& child_objects = menu_list->Children();
    if (child_objects.IsEmpty())
      return nullptr;
    DCHECK_EQ(child_objects.size(), 1UL);
    DCHECK(IsA<AXMenuListPopup>(child_objects[0].Get()));
    To<AXMenuListPopup>(child_objects[0].Get())->UpdateChildrenIfNecessary();
  } else {
    menu_list->UpdateChildrenIfNecessary();
  }
  return parent_.Get();
}

bool AXMenuListOption::IsVisible() const {
  if (!parent_)
    return false;

  // In a single-option select with the popup collapsed, only the selected
  // item is considered visible.
  return !parent_->IsOffScreen() ||
         ((IsSelected() == kSelectedStateTrue) ? true : false);
}

bool AXMenuListOption::IsOffScreen() const {
  // Invisible list options are considered to be offscreen.
  return !IsVisible();
}

int AXMenuListOption::PosInSet() const {
  // Value should be 1-based. 0 means not supported.
  return SetSize() ? element_->index() + 1 : 0;
}

int AXMenuListOption::SetSize() const {
  // Return 0 if not supported.
  if (!element_)
    return 0;
  HTMLSelectElement* select = element_->OwnerSelectElement();
  if (!select)
    return 0;
  return select->length();
}

AccessibilitySelectedState AXMenuListOption::IsSelected() const {
  if (!GetNode() || !CanSetSelectedAttribute())
    return kSelectedStateUndefined;

  AXMenuListPopup* parent = static_cast<AXMenuListPopup*>(ParentObject());
  if (parent && !parent->IsOffScreen()) {
    return ((parent->ActiveDescendant() == this) ? kSelectedStateTrue
                                                 : kSelectedStateFalse);
  }
  return ((element_ && element_->Selected()) ? kSelectedStateTrue
                                             : kSelectedStateFalse);
}

bool AXMenuListOption::OnNativeClickAction() {
  if (!element_)
    return false;

  // Clicking on an option within a menu list should first select that item,
  // then toggle whether the menu list is showing.
  element_->SetSelected(true);

  // Calling OnNativeClickAction on the parent select element will toggle
  // it open or closed.
  if (IsA<AXMenuListPopup>(ParentObject()))
    return ParentObject()->OnNativeClickAction();

  return AXNodeObject::OnNativeClickAction();
}

bool AXMenuListOption::OnNativeSetSelectedAction(bool b) {
  if (!element_ || !CanSetSelectedAttribute())
    return false;

  element_->SetSelected(b);
  return true;
}

bool AXMenuListOption::ComputeAccessibilityIsIgnored(
    IgnoredReasons* ignored_reasons) const {
  return AccessibilityIsIgnoredByDefault(ignored_reasons);
}

void AXMenuListOption::GetRelativeBounds(AXObject** out_container,
                                         FloatRect& out_bounds_in_container,
                                         SkMatrix44& out_container_transform,
                                         bool* clips_children) const {
  *out_container = nullptr;
  out_bounds_in_container = FloatRect();
  out_container_transform.setIdentity();

  AXObject* parent = ParentObject();
  if (!parent)
    return;
  DCHECK(IsA<AXMenuListPopup>(parent));

  AXObject* grandparent = parent->ParentObject();
  if (!grandparent)
    return;
  DCHECK(grandparent->IsMenuList());
  grandparent->GetRelativeBounds(out_container, out_bounds_in_container,
                                 out_container_transform, clips_children);
}

String AXMenuListOption::TextAlternative(bool recursive,
                                         bool in_aria_labelled_by_traversal,
                                         AXObjectSet& visited,
                                         ax::mojom::NameFrom& name_from,
                                         AXRelatedObjectVector* related_objects,
                                         NameSources* name_sources) const {
  // If nameSources is non-null, relatedObjects is used in filling it in, so it
  // must be non-null as well.
  if (name_sources)
    DCHECK(related_objects);

  if (!GetNode())
    return String();

  bool found_text_alternative = false;
  String text_alternative = AriaTextAlternative(
      recursive, in_aria_labelled_by_traversal, visited, name_from,
      related_objects, name_sources, &found_text_alternative);
  if (found_text_alternative && !name_sources)
    return text_alternative;

  name_from = ax::mojom::NameFrom::kContents;
  text_alternative = element_->DisplayLabel();
  if (name_sources) {
    name_sources->push_back(NameSource(found_text_alternative));
    name_sources->back().type = name_from;
    name_sources->back().text = text_alternative;
    found_text_alternative = true;
  }

  return text_alternative;
}

HTMLSelectElement* AXMenuListOption::ParentSelectNode() const {
  if (!GetNode())
    return nullptr;

  if (auto* option = DynamicTo<HTMLOptionElement>(GetNode()))
    return option->OwnerSelectElement();

  return nullptr;
}

void AXMenuListOption::Trace(Visitor* visitor) {
  visitor->Trace(element_);
  AXNodeObject::Trace(visitor);
}

}  // namespace blink