cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 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 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 "config.h" |
| 30 | #include "AccessibilityListBox.h" |
| 31 | |
| 32 | #include "AXObjectCache.h" |
| 33 | #include "AccessibilityListBoxOption.h" |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 34 | #include "HTMLNames.h" |
| 35 | #include "HTMLSelectElement.h" |
cfleizach@apple.com | f534eb2 | 2009-11-03 07:01:20 +0000 | [diff] [blame] | 36 | #include "HitTestResult.h" |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 37 | #include "RenderListBox.h" |
| 38 | #include "RenderObject.h" |
| 39 | |
| 40 | using namespace std; |
| 41 | |
| 42 | namespace WebCore { |
| 43 | |
| 44 | using namespace HTMLNames; |
| 45 | |
| 46 | AccessibilityListBox::AccessibilityListBox(RenderObject* renderer) |
| 47 | : AccessibilityRenderObject(renderer) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | AccessibilityListBox::~AccessibilityListBox() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | PassRefPtr<AccessibilityListBox> AccessibilityListBox::create(RenderObject* renderer) |
| 56 | { |
dmazzoni@google.com | 3601906 | 2013-01-24 08:16:00 +0000 | [diff] [blame] | 57 | return adoptRef(new AccessibilityListBox(renderer)); |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 58 | } |
| 59 | |
cfleizach@apple.com | 39027b7 | 2008-06-05 22:17:56 +0000 | [diff] [blame] | 60 | bool AccessibilityListBox::canSetSelectedChildrenAttribute() const |
| 61 | { |
| 62 | Node* selectNode = m_renderer->node(); |
| 63 | if (!selectNode) |
| 64 | return false; |
| 65 | |
darin@apple.com | 318f889 | 2011-10-15 00:30:43 +0000 | [diff] [blame] | 66 | return !toHTMLSelectElement(selectNode)->disabled(); |
cfleizach@apple.com | 39027b7 | 2008-06-05 22:17:56 +0000 | [diff] [blame] | 67 | } |
| 68 | |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 69 | void AccessibilityListBox::addChildren() |
| 70 | { |
| 71 | Node* selectNode = m_renderer->node(); |
| 72 | if (!selectNode) |
| 73 | return; |
| 74 | |
| 75 | m_haveChildren = true; |
| 76 | |
darin@apple.com | 318f889 | 2011-10-15 00:30:43 +0000 | [diff] [blame] | 77 | const Vector<HTMLElement*>& listItems = toHTMLSelectElement(selectNode)->listItems(); |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 78 | unsigned length = listItems.size(); |
| 79 | for (unsigned i = 0; i < length; i++) { |
zimmermann@webkit.org | de5f1ac | 2009-05-23 21:25:22 +0000 | [diff] [blame] | 80 | // The cast to HTMLElement below is safe because the only other possible listItem type |
dbates@webkit.org | ef42d38 | 2010-01-25 19:20:06 +0000 | [diff] [blame] | 81 | // would be a WMLElement, but WML builds don't use accessibility features at all. |
darin@apple.com | 318f889 | 2011-10-15 00:30:43 +0000 | [diff] [blame] | 82 | AccessibilityObject* listOption = listBoxOptionAccessibilityObject(listItems[i]); |
cfleizach@apple.com | 6f3a569 | 2010-03-11 00:36:02 +0000 | [diff] [blame] | 83 | if (listOption && !listOption->accessibilityIsIgnored()) |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 84 | m_children.append(listOption); |
| 85 | } |
| 86 | } |
| 87 | |
cfleizach@apple.com | 39027b7 | 2008-06-05 22:17:56 +0000 | [diff] [blame] | 88 | void AccessibilityListBox::setSelectedChildren(AccessibilityChildrenVector& children) |
| 89 | { |
| 90 | if (!canSetSelectedChildrenAttribute()) |
| 91 | return; |
| 92 | |
| 93 | Node* selectNode = m_renderer->node(); |
| 94 | if (!selectNode) |
| 95 | return; |
| 96 | |
| 97 | // disable any selected options |
| 98 | unsigned length = m_children.size(); |
| 99 | for (unsigned i = 0; i < length; i++) { |
| 100 | AccessibilityListBoxOption* listBoxOption = static_cast<AccessibilityListBoxOption*>(m_children[i].get()); |
| 101 | if (listBoxOption->isSelected()) |
| 102 | listBoxOption->setSelected(false); |
| 103 | } |
| 104 | |
| 105 | length = children.size(); |
| 106 | for (unsigned i = 0; i < length; i++) { |
| 107 | AccessibilityObject* obj = children[i].get(); |
| 108 | if (obj->roleValue() != ListBoxOptionRole) |
| 109 | continue; |
| 110 | |
| 111 | static_cast<AccessibilityListBoxOption*>(obj)->setSelected(true); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void AccessibilityListBox::selectedChildren(AccessibilityChildrenVector& result) |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 116 | { |
alice.liu@apple.com | cac21ca | 2008-05-16 03:27:24 +0000 | [diff] [blame] | 117 | ASSERT(result.isEmpty()); |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 118 | |
| 119 | if (!hasChildren()) |
| 120 | addChildren(); |
| 121 | |
| 122 | unsigned length = m_children.size(); |
| 123 | for (unsigned i = 0; i < length; i++) { |
| 124 | if (static_cast<AccessibilityListBoxOption*>(m_children[i].get())->isSelected()) |
alice.liu@apple.com | cac21ca | 2008-05-16 03:27:24 +0000 | [diff] [blame] | 125 | result.append(m_children[i]); |
| 126 | } |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 127 | } |
| 128 | |
cfleizach@apple.com | 39027b7 | 2008-06-05 22:17:56 +0000 | [diff] [blame] | 129 | void AccessibilityListBox::visibleChildren(AccessibilityChildrenVector& result) |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 130 | { |
alice.liu@apple.com | cac21ca | 2008-05-16 03:27:24 +0000 | [diff] [blame] | 131 | ASSERT(result.isEmpty()); |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 132 | |
| 133 | if (!hasChildren()) |
| 134 | addChildren(); |
| 135 | |
| 136 | unsigned length = m_children.size(); |
| 137 | for (unsigned i = 0; i < length; i++) { |
darin@apple.com | b6cb256 | 2009-08-05 21:25:09 +0000 | [diff] [blame] | 138 | if (toRenderListBox(m_renderer)->listIndexIsVisible(i)) |
alice.liu@apple.com | cac21ca | 2008-05-16 03:27:24 +0000 | [diff] [blame] | 139 | result.append(m_children[i]); |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 140 | } |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | AccessibilityObject* AccessibilityListBox::listBoxOptionAccessibilityObject(HTMLElement* element) const |
| 144 | { |
| 145 | // skip hr elements |
| 146 | if (!element || element->hasTagName(hrTag)) |
| 147 | return 0; |
| 148 | |
cfleizach@apple.com | 70b144c | 2009-02-26 00:27:38 +0000 | [diff] [blame] | 149 | AccessibilityObject* listBoxObject = m_renderer->document()->axObjectCache()->getOrCreate(ListBoxOptionRole); |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 150 | static_cast<AccessibilityListBoxOption*>(listBoxObject)->setHTMLElement(element); |
| 151 | |
| 152 | return listBoxObject; |
| 153 | } |
cfleizach@apple.com | 6f3a569 | 2010-03-11 00:36:02 +0000 | [diff] [blame] | 154 | |
dmazzoni@google.com | 3b5179d | 2013-02-09 23:06:00 +0000 | [diff] [blame] | 155 | bool AccessibilityListBox::computeAccessibilityIsIgnored() const |
cfleizach@apple.com | 6f3a569 | 2010-03-11 00:36:02 +0000 | [diff] [blame] | 156 | { |
cfleizach@apple.com | 14f57ce | 2010-03-11 22:24:33 +0000 | [diff] [blame] | 157 | AccessibilityObjectInclusion decision = accessibilityIsIgnoredBase(); |
| 158 | if (decision == IncludeObject) |
| 159 | return false; |
| 160 | if (decision == IgnoreObject) |
cfleizach@apple.com | 6f3a569 | 2010-03-11 00:36:02 +0000 | [diff] [blame] | 161 | return true; |
| 162 | |
| 163 | return false; |
| 164 | } |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 165 | |
leviw@chromium.org | 3be076e | 2012-03-22 15:07:02 +0000 | [diff] [blame] | 166 | AccessibilityObject* AccessibilityListBox::elementAccessibilityHitTest(const IntPoint& point) const |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 167 | { |
| 168 | // the internal HTMLSelectElement methods for returning a listbox option at a point |
| 169 | // ignore optgroup elements. |
| 170 | if (!m_renderer) |
| 171 | return 0; |
| 172 | |
hyatt@apple.com | 7e03253 | 2009-02-11 22:06:32 +0000 | [diff] [blame] | 173 | Node* node = m_renderer->node(); |
| 174 | if (!node) |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 175 | return 0; |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 176 | |
leviw@chromium.org | c3f41a0 | 2011-08-18 01:18:55 +0000 | [diff] [blame] | 177 | LayoutRect parentRect = boundingBoxRect(); |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 178 | |
cfleizach@apple.com | 6f3a569 | 2010-03-11 00:36:02 +0000 | [diff] [blame] | 179 | AccessibilityObject* listBoxOption = 0; |
| 180 | unsigned length = m_children.size(); |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 181 | for (unsigned i = 0; i < length; i++) { |
leviw@chromium.org | c3f41a0 | 2011-08-18 01:18:55 +0000 | [diff] [blame] | 182 | LayoutRect rect = toRenderListBox(m_renderer)->itemBoundingBoxRect(parentRect.location(), i); |
zimmermann@webkit.org | de5f1ac | 2009-05-23 21:25:22 +0000 | [diff] [blame] | 183 | // The cast to HTMLElement below is safe because the only other possible listItem type |
dbates@webkit.org | ef42d38 | 2010-01-25 19:20:06 +0000 | [diff] [blame] | 184 | // would be a WMLElement, but WML builds don't use accessibility features at all. |
cfleizach@apple.com | 6f3a569 | 2010-03-11 00:36:02 +0000 | [diff] [blame] | 185 | if (rect.contains(point)) { |
| 186 | listBoxOption = m_children[i].get(); |
| 187 | break; |
| 188 | } |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 189 | } |
| 190 | |
cfleizach@apple.com | 6f3a569 | 2010-03-11 00:36:02 +0000 | [diff] [blame] | 191 | if (listBoxOption && !listBoxOption->accessibilityIsIgnored()) |
| 192 | return listBoxOption; |
| 193 | |
cfleizach@apple.com | 70b144c | 2009-02-26 00:27:38 +0000 | [diff] [blame] | 194 | return axObjectCache()->getOrCreate(m_renderer); |
cfleizach@apple.com | e9dbd31 | 2008-05-06 23:27:40 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | } // namespace WebCore |