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 "core/accessibility/AXMenuListPopup.h"
28
29 #include "core/accessibility/AXMenuListOption.h"
30 #include "core/accessibility/AXObjectCache.h"
31 #include "core/html/HTMLSelectElement.h"
32
33 namespace WebCore {
34
35 using namespace HTMLNames;
36
AXMenuListPopup()37 AXMenuListPopup::AXMenuListPopup()
38 {
39 }
40
isVisible() const41 bool AXMenuListPopup::isVisible() const
42 {
43 return false;
44 }
45
isOffScreen() const46 bool AXMenuListPopup::isOffScreen() const
47 {
48 if (!m_parent)
49 return true;
50
51 return m_parent->isCollapsed();
52 }
53
isEnabled() const54 bool AXMenuListPopup::isEnabled() const
55 {
56 if (!m_parent)
57 return false;
58
59 return m_parent->isEnabled();
60 }
61
computeAccessibilityIsIgnored() const62 bool AXMenuListPopup::computeAccessibilityIsIgnored() const
63 {
64 return accessibilityIsIgnoredByDefault();
65 }
66
menuListOptionAXObject(HTMLElement * element) const67 AXMenuListOption* AXMenuListPopup::menuListOptionAXObject(HTMLElement* element) const
68 {
69 if (!element->hasTagName(optionTag))
70 return 0;
71
72 AXObject* object = document()->axObjectCache()->getOrCreate(MenuListOptionRole);
73 ASSERT_WITH_SECURITY_IMPLICATION(object->isMenuListOption());
74
75 AXMenuListOption* option = toAXMenuListOption(object);
76 option->setElement(element);
77
78 return option;
79 }
80
press() const81 bool AXMenuListPopup::press() const
82 {
83 if (!m_parent)
84 return false;
85
86 m_parent->press();
87 return true;
88 }
89
addChildren()90 void AXMenuListPopup::addChildren()
91 {
92 if (!m_parent)
93 return;
94
95 Node* selectNode = m_parent->node();
96 if (!selectNode)
97 return;
98
99 m_haveChildren = true;
100
101 const Vector<HTMLElement*>& listItems = toHTMLSelectElement(selectNode)->listItems();
102 unsigned length = listItems.size();
103 for (unsigned i = 0; i < length; i++) {
104 AXMenuListOption* option = menuListOptionAXObject(listItems[i]);
105 if (option) {
106 option->setParent(this);
107 m_children.append(option);
108 }
109 }
110 }
111
childrenChanged()112 void AXMenuListPopup::childrenChanged()
113 {
114 AXObjectCache* cache = axObjectCache();
115 for (size_t i = m_children.size(); i > 0 ; --i) {
116 AXObject* child = m_children[i - 1].get();
117 // FIXME: How could children end up in here that have no actionElement(), the check
118 // in menuListOptionAXObject would seem to prevent that.
119 if (child->actionElement()) {
120 child->detachFromParent();
121 cache->remove(child->axObjectID());
122 }
123 }
124
125 m_children.clear();
126 m_haveChildren = false;
127 }
128
didUpdateActiveOption(int optionIndex)129 void AXMenuListPopup::didUpdateActiveOption(int optionIndex)
130 {
131 // We defer creation of the children until updating the active option so that we don't
132 // create AXObjects for <option> elements while they're in the middle of removal.
133 if (!m_haveChildren)
134 addChildren();
135
136 ASSERT_ARG(optionIndex, optionIndex >= 0);
137 ASSERT_ARG(optionIndex, optionIndex < static_cast<int>(m_children.size()));
138
139 AXObjectCache* cache = axObjectCache();
140 RefPtr<AXObject> child = m_children[optionIndex].get();
141
142 cache->postNotification(child.get(), document(), AXObjectCache::AXFocusedUIElementChanged, true, PostSynchronously);
143 cache->postNotification(child.get(), document(), AXObjectCache::AXMenuListItemSelected, true, PostSynchronously);
144 }
145
146 } // namespace WebCore
147