2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "AccessibilityARIAGrid.h"
32 #include "AXObjectCache.h"
33 #include "AccessibilityTableCell.h"
34 #include "AccessibilityTableColumn.h"
35 #include "AccessibilityTableHeaderContainer.h"
36 #include "AccessibilityTableRow.h"
37 #include "RenderObject.h"
43 AccessibilityARIAGrid::AccessibilityARIAGrid(RenderObject* renderer)
44 : AccessibilityTable(renderer)
46 #if ACCESSIBILITY_TABLES
47 m_isAccessibilityTable = true;
49 m_isAccessibilityTable = false;
53 AccessibilityARIAGrid::~AccessibilityARIAGrid()
57 PassRefPtr<AccessibilityARIAGrid> AccessibilityARIAGrid::create(RenderObject* renderer)
59 return adoptRef(new AccessibilityARIAGrid(renderer));
62 bool AccessibilityARIAGrid::addChild(AccessibilityObject* child, HashSet<AccessibilityObject*>& appendedRows, unsigned& columnCount)
64 if (!child || !child->isTableRow() || child->ariaRoleAttribute() != RowRole)
67 AccessibilityTableRow* row = static_cast<AccessibilityTableRow*>(child);
68 if (appendedRows.contains(row))
71 // store the maximum number of columns
72 unsigned rowCellCount = row->children().size();
73 if (rowCellCount > columnCount)
74 columnCount = rowCellCount;
76 row->setRowIndex((int)m_rows.size());
79 // Try adding the row if it's not ignoring accessibility,
80 // otherwise add its children (the cells) as the grid's children.
81 if (!row->accessibilityIsIgnored())
82 m_children.append(row);
84 m_children.append(row->children());
86 appendedRows.add(row);
90 void AccessibilityARIAGrid::addChildren()
92 ASSERT(!m_haveChildren);
94 if (!isAccessibilityTable()) {
95 AccessibilityRenderObject::addChildren();
99 m_haveChildren = true;
103 AXObjectCache* axCache = m_renderer->document()->axObjectCache();
105 // add only rows that are labeled as aria rows
106 HashSet<AccessibilityObject*> appendedRows;
107 unsigned columnCount = 0;
108 for (RefPtr<AccessibilityObject> child = firstChild(); child; child = child->nextSibling()) {
110 if (!addChild(child.get(), appendedRows, columnCount)) {
112 // in case the render tree doesn't match the expected ARIA hierarchy, look at the children
113 if (!child->hasChildren())
114 child->addChildren();
116 // The children of this non-row will contain all non-ignored elements (recursing to find them).
117 // This allows the table to dive arbitrarily deep to find the rows.
118 AccessibilityChildrenVector children = child->children();
119 size_t length = children.size();
120 for (size_t i = 0; i < length; ++i)
121 addChild(children[i].get(), appendedRows, columnCount);
125 // make the columns based on the number of columns in the first body
126 for (unsigned i = 0; i < columnCount; ++i) {
127 AccessibilityTableColumn* column = static_cast<AccessibilityTableColumn*>(axCache->getOrCreate(ColumnRole));
128 column->setColumnIndex((int)i);
129 column->setParentTable(this);
130 m_columns.append(column);
131 if (!column->accessibilityIsIgnored())
132 m_children.append(column);
135 AccessibilityObject* headerContainerObject = headerContainer();
136 if (headerContainerObject && !headerContainerObject->accessibilityIsIgnored())
137 m_children.append(headerContainerObject);
140 AccessibilityTableCell* AccessibilityARIAGrid::cellForColumnAndRow(unsigned column, unsigned row)
145 updateChildrenIfNecessary();
147 if (column >= columnCount() || row >= rowCount())
150 int intRow = (int)row;
151 int intColumn = (int)column;
153 pair<int, int> columnRange;
154 pair<int, int> rowRange;
156 // Iterate backwards through the rows in case the desired cell has a rowspan and exists
157 // in a previous row.
158 for (; intRow >= 0; --intRow) {
159 AccessibilityObject* tableRow = m_rows[intRow].get();
163 AccessibilityChildrenVector children = tableRow->children();
164 unsigned childrenLength = children.size();
166 // Since some cells may have colspans, we have to check the actual range of each
167 // cell to determine which is the right one.
168 for (unsigned k = 0; k < childrenLength; ++k) {
169 AccessibilityObject* child = children[k].get();
170 if (!child->isTableCell())
173 AccessibilityTableCell* tableCellChild = static_cast<AccessibilityTableCell*>(child);
174 tableCellChild->columnIndexRange(columnRange);
175 tableCellChild->rowIndexRange(rowRange);
177 if ((intColumn >= columnRange.first && intColumn < (columnRange.first + columnRange.second))
178 && (intRow >= rowRange.first && intRow < (rowRange.first + rowRange.second)))
179 return tableCellChild;
186 } // namespace WebCore