1 /*
2 * Copyright (C) 2008, 2009, 2010 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 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.
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 "third_party/blink/renderer/core/accessibility/ax_object_cache.h"
30
31 #include <memory>
32
33 #include "base/memory/ptr_util.h"
34 #include "base/stl_util.h"
35 #include "third_party/blink/public/web/web_ax_enums.h"
36 #include "third_party/blink/renderer/core/dom/element.h"
37 #include "third_party/blink/renderer/core/dom/node.h"
38 #include "third_party/blink/renderer/core/html_element_type_helpers.h"
39 #include "third_party/blink/renderer/platform/wtf/assertions.h"
40 #include "third_party/blink/renderer/platform/wtf/hash_set.h"
41 #include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
42 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
43
44 namespace blink {
45
46 AXObjectCache::AXObjectCacheCreateFunction AXObjectCache::create_function_ =
47 nullptr;
48
Init(AXObjectCacheCreateFunction function)49 void AXObjectCache::Init(AXObjectCacheCreateFunction function) {
50 DCHECK(!create_function_);
51 create_function_ = function;
52 }
53
Create(Document & document)54 AXObjectCache* AXObjectCache::Create(Document& document) {
55 DCHECK(create_function_);
56 return create_function_(document);
57 }
58
59 namespace {
60
61 typedef HashSet<String, CaseFoldingHash> ARIAWidgetSet;
62
63 const char* g_aria_widgets[] = {
64 // From http://www.w3.org/TR/wai-aria/roles#widget_roles
65 "alert", "alertdialog", "button", "checkbox", "dialog", "gridcell", "link",
66 "log", "marquee", "menuitem", "menuitemcheckbox", "menuitemradio", "option",
67 "progressbar", "radio", "scrollbar", "slider", "spinbutton", "status",
68 "tab", "tabpanel", "textbox", "timer", "tooltip", "treeitem",
69 // Composite user interface widgets.
70 // This list is also from the w3.org site referenced above.
71 "combobox", "grid", "listbox", "menu", "menubar", "radiogroup", "tablist",
72 "tree", "treegrid"};
73
CreateARIARoleWidgetSet()74 static ARIAWidgetSet* CreateARIARoleWidgetSet() {
75 ARIAWidgetSet* widget_set = new HashSet<String, CaseFoldingHash>();
76 for (size_t i = 0; i < base::size(g_aria_widgets); ++i)
77 widget_set->insert(String(g_aria_widgets[i]));
78 return widget_set;
79 }
80
IncludesARIAWidgetRole(const String & role)81 bool IncludesARIAWidgetRole(const String& role) {
82 static const HashSet<String, CaseFoldingHash>* role_set =
83 CreateARIARoleWidgetSet();
84
85 Vector<String> role_vector;
86 role.Split(' ', role_vector);
87 for (const auto& child : role_vector) {
88 if (role_set->Contains(child))
89 return true;
90 }
91 return false;
92 }
93
94 const char* g_aria_interactive_widget_attributes[] = {
95 // These attributes implicitly indicate the given widget is interactive.
96 // From http://www.w3.org/TR/wai-aria/states_and_properties#attrs_widgets
97 // clang-format off
98 "aria-activedescendant",
99 "aria-checked",
100 "aria-controls",
101 "aria-disabled", // If it's disabled, it can be made interactive.
102 "aria-haspopup",
103 "aria-multiselectable",
104 "aria-required",
105 "aria-selected"
106 // clang-format on
107 };
108
HasInteractiveARIAAttribute(const Element & element)109 bool HasInteractiveARIAAttribute(const Element& element) {
110 for (size_t i = 0; i < base::size(g_aria_interactive_widget_attributes);
111 ++i) {
112 const char* attribute = g_aria_interactive_widget_attributes[i];
113 if (element.hasAttribute(attribute)) {
114 return true;
115 }
116 }
117 return false;
118 }
119
120 } // namespace
121
IsInsideFocusableElementOrARIAWidget(const Node & node)122 bool AXObjectCache::IsInsideFocusableElementOrARIAWidget(const Node& node) {
123 const Node* cur_node = &node;
124 do {
125 if (const auto* element = DynamicTo<Element>(cur_node)) {
126 if (element->IsFocusable())
127 return true;
128 String role = element->getAttribute("role");
129 if (!role.IsEmpty() && IncludesARIAWidgetRole(role))
130 return true;
131 if (HasInteractiveARIAAttribute(*element))
132 return true;
133 }
134 cur_node = cur_node->parentNode();
135 } while (cur_node && !IsA<HTMLBodyElement>(node));
136 return false;
137 }
138
139 } // namespace blink
140