Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / accessibility / AXTableCell.cpp
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 "core/accessibility/AXTableCell.h"
31
32 #include "core/accessibility/AXObjectCache.h"
33 #include "core/rendering/RenderTableCell.h"
34
35
36 namespace blink {
37
38 using namespace HTMLNames;
39
40 AXTableCell::AXTableCell(RenderObject* renderer)
41     : AXRenderObject(renderer)
42 {
43 }
44
45 AXTableCell::~AXTableCell()
46 {
47 }
48
49 PassRefPtr<AXTableCell> AXTableCell::create(RenderObject* renderer)
50 {
51     return adoptRef(new AXTableCell(renderer));
52 }
53
54 bool AXTableCell::computeAccessibilityIsIgnored() const
55 {
56     AXObjectInclusion decision = defaultObjectInclusion();
57     if (decision == IncludeObject)
58         return false;
59     if (decision == IgnoreObject)
60         return true;
61
62     if (!isTableCell())
63         return AXRenderObject::computeAccessibilityIsIgnored();
64
65     return false;
66 }
67
68 AXObject* AXTableCell::parentTable() const
69 {
70     if (!m_renderer || !m_renderer->isTableCell())
71         return 0;
72
73     // If the document no longer exists, we might not have an axObjectCache.
74     if (!axObjectCache())
75         return 0;
76
77     // Do not use getOrCreate. parentTable() can be called while the render tree is being modified
78     // by javascript, and creating a table element may try to access the render tree while in a bad state.
79     // By using only get() implies that the AXTable must be created before AXTableCells. This should
80     // always be the case when AT clients access a table.
81     // https://bugs.webkit.org/show_bug.cgi?id=42652
82     return axObjectCache()->get(toRenderTableCell(m_renderer)->table());
83 }
84
85 bool AXTableCell::isTableCell() const
86 {
87     AXObject* parent = parentObjectUnignored();
88     if (!parent || !parent->isTableRow())
89         return false;
90
91     return true;
92 }
93
94 AccessibilityRole AXTableCell::determineAccessibilityRole()
95 {
96     if (!isTableCell())
97         return AXRenderObject::determineAccessibilityRole();
98
99     return CellRole;
100 }
101
102 void AXTableCell::rowIndexRange(pair<unsigned, unsigned>& rowRange)
103 {
104     if (!m_renderer || !m_renderer->isTableCell())
105         return;
106
107     RenderTableCell* renderCell = toRenderTableCell(m_renderer);
108     rowRange.first = renderCell->rowIndex();
109     rowRange.second = renderCell->rowSpan();
110
111     // since our table might have multiple sections, we have to offset our row appropriately
112     RenderTableSection* section = renderCell->section();
113     RenderTable* table = renderCell->table();
114     if (!table || !section)
115         return;
116
117     RenderTableSection* tableSection = table->topSection();
118     unsigned rowOffset = 0;
119     while (tableSection) {
120         if (tableSection == section)
121             break;
122         rowOffset += tableSection->numRows();
123         tableSection = table->sectionBelow(tableSection, SkipEmptySections);
124     }
125
126     rowRange.first += rowOffset;
127 }
128
129 void AXTableCell::columnIndexRange(pair<unsigned, unsigned>& columnRange)
130 {
131     if (!m_renderer || !m_renderer->isTableCell())
132         return;
133
134     RenderTableCell* renderCell = toRenderTableCell(m_renderer);
135     columnRange.first = renderCell->col();
136     columnRange.second = renderCell->colSpan();
137 }
138
139 AXObject* AXTableCell::titleUIElement() const
140 {
141     // Try to find if the first cell in this row is a <th>. If it is,
142     // then it can act as the title ui element. (This is only in the
143     // case when the table is not appearing as an AXTable.)
144     if (isTableCell() || !m_renderer || !m_renderer->isTableCell())
145         return 0;
146
147     // Table cells that are th cannot have title ui elements, since by definition
148     // they are title ui elements
149     Node* node = m_renderer->node();
150     if (node && node->hasTagName(thTag))
151         return 0;
152
153     RenderTableCell* renderCell = toRenderTableCell(m_renderer);
154
155     // If this cell is in the first column, there is no need to continue.
156     int col = renderCell->col();
157     if (!col)
158         return 0;
159
160     int row = renderCell->rowIndex();
161
162     RenderTableSection* section = renderCell->section();
163     if (!section)
164         return 0;
165
166     RenderTableCell* headerCell = section->primaryCellAt(row, 0);
167     if (!headerCell || headerCell == renderCell)
168         return 0;
169
170     Node* cellElement = headerCell->node();
171     if (!cellElement || !cellElement->hasTagName(thTag))
172         return 0;
173
174     return axObjectCache()->getOrCreate(headerCell);
175 }
176
177 } // namespace blink