Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / accessibility / AXARIAGrid.cpp
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 "core/accessibility/AXARIAGrid.h"
31
32 #include "core/accessibility/AXObjectCache.h"
33 #include "core/accessibility/AXTableColumn.h"
34 #include "core/accessibility/AXTableRow.h"
35 #include "core/rendering/RenderObject.h"
36
37
38 namespace blink {
39
40 AXARIAGrid::AXARIAGrid(RenderObject* renderer)
41     : AXTable(renderer)
42 {
43 }
44
45 AXARIAGrid::~AXARIAGrid()
46 {
47 }
48
49 PassRefPtr<AXARIAGrid> AXARIAGrid::create(RenderObject* renderer)
50 {
51     return adoptRef(new AXARIAGrid(renderer));
52 }
53
54 bool AXARIAGrid::addTableCellChild(AXObject* child, HashSet<AXObject*>& appendedRows, unsigned& columnCount)
55 {
56     if (!child || !child->isTableRow() || child->ariaRoleAttribute() != RowRole)
57         return false;
58
59     AXTableRow* row = toAXTableRow(child);
60     if (appendedRows.contains(row))
61         return false;
62
63     // store the maximum number of columns
64     unsigned rowCellCount = row->children().size();
65     if (rowCellCount > columnCount)
66         columnCount = rowCellCount;
67
68     row->setRowIndex((int)m_rows.size());
69     m_rows.append(row);
70
71     // Try adding the row if it's not ignoring accessibility,
72     // otherwise add its children (the cells) as the grid's children.
73     if (!row->accessibilityIsIgnored())
74         m_children.append(row);
75     else
76         m_children.appendVector(row->children());
77
78     appendedRows.add(row);
79     return true;
80 }
81
82 void AXARIAGrid::addChildren()
83 {
84     ASSERT(!m_haveChildren);
85
86     if (!isAXTable()) {
87         AXRenderObject::addChildren();
88         return;
89     }
90
91     m_haveChildren = true;
92     if (!m_renderer)
93         return;
94
95     AXObjectCache* axCache = m_renderer->document().axObjectCache();
96
97     // add only rows that are labeled as aria rows
98     HashSet<AXObject*> appendedRows;
99     unsigned columnCount = 0;
100     for (RefPtr<AXObject> child = firstChild(); child; child = child->nextSibling()) {
101
102         if (!addTableCellChild(child.get(), appendedRows, columnCount)) {
103
104             // in case the render tree doesn't match the expected ARIA hierarchy, look at the children
105             if (!child->hasChildren())
106                 child->addChildren();
107
108             // The children of this non-row will contain all non-ignored elements (recursing to find them).
109             // This allows the table to dive arbitrarily deep to find the rows.
110             AccessibilityChildrenVector children = child->children();
111             size_t length = children.size();
112             for (size_t i = 0; i < length; ++i)
113                 addTableCellChild(children[i].get(), appendedRows, columnCount);
114         }
115     }
116
117     // make the columns based on the number of columns in the first body
118     for (unsigned i = 0; i < columnCount; ++i) {
119         AXTableColumn* column = toAXTableColumn(axCache->getOrCreate(ColumnRole));
120         column->setColumnIndex((int)i);
121         column->setParent(this);
122         m_columns.append(column);
123         if (!column->accessibilityIsIgnored())
124             m_children.append(column);
125     }
126
127     AXObject* headerContainerObject = headerContainer();
128     if (headerContainerObject && !headerContainerObject->accessibilityIsIgnored())
129         m_children.append(headerContainerObject);
130 }
131
132 } // namespace blink