1/*
2 * Copyright (C) 2009 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 "AccessibilityARIAGrid.h"
31
32#include "AXObjectCache.h"
33#include "AccessibilityTableCell.h"
34#include "AccessibilityTableColumn.h"
35#include "AccessibilityTableHeaderContainer.h"
36#include "AccessibilityTableRow.h"
37#include "RenderObject.h"
38
39using namespace std;
40
41namespace WebCore {
42
43AccessibilityARIAGrid::AccessibilityARIAGrid(RenderObject* renderer)
44    : AccessibilityTable(renderer)
45{
46}
47
48AccessibilityARIAGrid::~AccessibilityARIAGrid()
49{
50}
51
52PassRefPtr<AccessibilityARIAGrid> AccessibilityARIAGrid::create(RenderObject* renderer)
53{
54    return adoptRef(new AccessibilityARIAGrid(renderer));
55}
56
57bool AccessibilityARIAGrid::addTableCellChild(AccessibilityObject* child, HashSet<AccessibilityObject*>& appendedRows, unsigned& columnCount)
58{
59    if (!child || !child->isTableRow() || child->ariaRoleAttribute() != RowRole)
60        return false;
61
62    AccessibilityTableRow* row = static_cast<AccessibilityTableRow*>(child);
63    if (appendedRows.contains(row))
64        return false;
65
66    // store the maximum number of columns
67    unsigned rowCellCount = row->children().size();
68    if (rowCellCount > columnCount)
69        columnCount = rowCellCount;
70
71    row->setRowIndex((int)m_rows.size());
72    m_rows.append(row);
73
74    // Try adding the row if it's not ignoring accessibility,
75    // otherwise add its children (the cells) as the grid's children.
76    if (!row->accessibilityIsIgnored())
77        m_children.append(row);
78    else
79        m_children.appendVector(row->children());
80
81    appendedRows.add(row);
82    return true;
83}
84
85void AccessibilityARIAGrid::addRowDescendant(AccessibilityObject* rowChild, HashSet<AccessibilityObject*>& appendedRows, unsigned& columnCount)
86{
87    if (!rowChild)
88        return;
89
90    if (!rowChild->isTableRow()) {
91        // Although a "grid" should have rows as its direct descendants, if this is not a table row,
92        // dive deeper into the descendants to try to find a valid row.
93        AccessibilityChildrenVector children = rowChild->children();
94        size_t length = children.size();
95        for (size_t i = 0; i < length; ++i)
96            addRowDescendant(children[i].get(), appendedRows, columnCount);
97    } else
98        addTableCellChild(rowChild, appendedRows, columnCount);
99}
100
101void AccessibilityARIAGrid::addChildren()
102{
103    ASSERT(!m_haveChildren);
104
105    if (!isAccessibilityTable()) {
106        AccessibilityRenderObject::addChildren();
107        return;
108    }
109
110    m_haveChildren = true;
111    if (!m_renderer)
112        return;
113
114    AXObjectCache* axCache = m_renderer->document()->axObjectCache();
115
116    // add only rows that are labeled as aria rows
117    HashSet<AccessibilityObject*> appendedRows;
118    unsigned columnCount = 0;
119    for (RefPtr<AccessibilityObject> child = firstChild(); child; child = child->nextSibling())
120        addRowDescendant(child.get(), appendedRows, columnCount);
121
122    // make the columns based on the number of columns in the first body
123    for (unsigned i = 0; i < columnCount; ++i) {
124        AccessibilityTableColumn* column = static_cast<AccessibilityTableColumn*>(axCache->getOrCreate(ColumnRole));
125        column->setColumnIndex((int)i);
126        column->setParent(this);
127        m_columns.append(column);
128        if (!column->accessibilityIsIgnored())
129            m_children.append(column);
130    }
131
132    AccessibilityObject* headerContainerObject = headerContainer();
133    if (headerContainerObject && !headerContainerObject->accessibilityIsIgnored())
134        m_children.append(headerContainerObject);
135}
136
137} // namespace WebCore
138