1/*
2 * Copyright (C) 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "AccessibilityMenuListPopup.h"
28
29#include "AXObjectCache.h"
30#include "AccessibilityMenuList.h"
31#include "AccessibilityMenuListOption.h"
32#include "HTMLNames.h"
33#include "HTMLOptionElement.h"
34#include "HTMLSelectElement.h"
35#include "RenderMenuList.h"
36
37namespace WebCore {
38
39using namespace HTMLNames;
40
41AccessibilityMenuListPopup::AccessibilityMenuListPopup()
42{
43}
44
45bool AccessibilityMenuListPopup::isVisible() const
46{
47 return false;
48}
49
50bool AccessibilityMenuListPopup::isOffScreen() const
51{
52 if (!m_parent)
53 return true;
54
55 return m_parent->isCollapsed();
56}
57
58bool AccessibilityMenuListPopup::isEnabled() const
59{
60 if (!m_parent)
61 return false;
62
63 return m_parent->isEnabled();
64}
65
66bool AccessibilityMenuListPopup::computeAccessibilityIsIgnored() const
67{
68 return accessibilityIsIgnoredByDefault();
69}
70
71AccessibilityMenuListOption* AccessibilityMenuListPopup::menuListOptionAccessibilityObject(HTMLElement* element) const
72{
73 if (!is<HTMLOptionElement>(element) || !element->inRenderedDocument())
74 return nullptr;
75
76 auto& option = downcast<AccessibilityMenuListOption>(*document()->axObjectCache()->getOrCreate(AccessibilityRole::MenuListOption));
77 option.setElement(element);
78
79 return &option;
80}
81
82bool AccessibilityMenuListPopup::press()
83{
84 if (!m_parent)
85 return false;
86
87 m_parent->press();
88 return true;
89}
90
91void AccessibilityMenuListPopup::addChildren()
92{
93 if (!m_parent)
94 return;
95
96 Node* selectNode = m_parent->node();
97 if (!selectNode)
98 return;
99
100 m_haveChildren = true;
101
102 for (const auto& listItem : downcast<HTMLSelectElement>(*selectNode).listItems()) {
103 AccessibilityMenuListOption* option = menuListOptionAccessibilityObject(listItem);
104 if (option) {
105 option->setParent(this);
106 m_children.append(option);
107 }
108 }
109}
110
111void AccessibilityMenuListPopup::childrenChanged()
112{
113 AXObjectCache* cache = axObjectCache();
114 for (size_t i = m_children.size(); i > 0 ; --i) {
115 AccessibilityObject* child = m_children[i - 1].get();
116 if (child->actionElement() && !child->actionElement()->inRenderedDocument()) {
117 child->detachFromParent();
118 cache->remove(child->axObjectID());
119 }
120 }
121
122 m_children.clear();
123 m_haveChildren = false;
124 addChildren();
125}
126
127void AccessibilityMenuListPopup::didUpdateActiveOption(int optionIndex)
128{
129 ASSERT_ARG(optionIndex, optionIndex >= 0);
130 ASSERT_ARG(optionIndex, optionIndex < static_cast<int>(m_children.size()));
131
132 AXObjectCache* cache = axObjectCache();
133 RefPtr<AccessibilityObject> child = m_children[optionIndex].get();
134
135 cache->postNotification(child.get(), document(), AXObjectCache::AXFocusedUIElementChanged, TargetElement, PostSynchronously);
136 cache->postNotification(child.get(), document(), AXObjectCache::AXMenuListItemSelected, TargetElement, PostSynchronously);
137}
138
139} // namespace WebCore
140