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 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 "AccessibilityTableRow.h"
31
32#include "AXObjectCache.h"
33#include "AccessibilityTable.h"
34#include "AccessibilityTableCell.h"
35#include "HTMLNames.h"
36#include "HTMLTableRowElement.h"
37#include "RenderObject.h"
38#include "RenderTableCell.h"
39#include "RenderTableRow.h"
40
41namespace WebCore {
42
43using namespace HTMLNames;
44
45AccessibilityTableRow::AccessibilityTableRow(RenderObject* renderer)
46 : AccessibilityRenderObject(renderer)
47{
48}
49
50AccessibilityTableRow::~AccessibilityTableRow() = default;
51
52Ref<AccessibilityTableRow> AccessibilityTableRow::create(RenderObject* renderer)
53{
54 return adoptRef(*new AccessibilityTableRow(renderer));
55}
56
57AccessibilityRole AccessibilityTableRow::determineAccessibilityRole()
58{
59 if (!isTableRow())
60 return AccessibilityRenderObject::determineAccessibilityRole();
61
62 if ((m_ariaRole = determineAriaRoleAttribute()) != AccessibilityRole::Unknown)
63 return m_ariaRole;
64
65 return AccessibilityRole::Row;
66}
67
68bool AccessibilityTableRow::isTableRow() const
69{
70 AccessibilityObject* table = parentTable();
71 return is<AccessibilityTable>(table) && downcast<AccessibilityTable>(*table).isExposableThroughAccessibility();
72}
73
74AccessibilityObject* AccessibilityTableRow::observableObject() const
75{
76 // This allows the table to be the one who sends notifications about tables.
77 return parentTable();
78}
79
80bool AccessibilityTableRow::computeAccessibilityIsIgnored() const
81{
82 AccessibilityObjectInclusion decision = defaultObjectInclusion();
83 if (decision == AccessibilityObjectInclusion::IncludeObject)
84 return false;
85 if (decision == AccessibilityObjectInclusion::IgnoreObject)
86 return true;
87
88 if (!isTableRow())
89 return AccessibilityRenderObject::computeAccessibilityIsIgnored();
90
91 return false;
92}
93
94AccessibilityTable* AccessibilityTableRow::parentTable() const
95{
96 // The parent table might not be the direct ancestor of the row unfortunately. ARIA states that role="grid" should
97 // only have "row" elements, but if not, we still should handle it gracefully by finding the right table.
98 for (AccessibilityObject* parent = parentObject(); parent; parent = parent->parentObject()) {
99 // If this is a non-anonymous table object, but not an accessibility table, we should stop because we don't want to
100 // choose another ancestor table as this row's table.
101 if (is<AccessibilityTable>(*parent)) {
102 auto& parentTable = downcast<AccessibilityTable>(*parent);
103 if (parentTable.isExposableThroughAccessibility())
104 return &parentTable;
105 if (parentTable.node())
106 break;
107 }
108 }
109
110 return nullptr;
111}
112
113AccessibilityObject* AccessibilityTableRow::headerObject()
114{
115 if (!m_renderer || !m_renderer->isTableRow())
116 return nullptr;
117
118 const auto& rowChildren = children();
119 if (!rowChildren.size())
120 return nullptr;
121
122 // check the first element in the row to see if it is a TH element
123 AccessibilityObject* cell = rowChildren[0].get();
124 if (!is<AccessibilityTableCell>(*cell))
125 return nullptr;
126
127 RenderObject* cellRenderer = downcast<AccessibilityTableCell>(*cell).renderer();
128 if (!cellRenderer)
129 return nullptr;
130
131 Node* cellNode = cellRenderer->node();
132 if (!cellNode || !cellNode->hasTagName(thTag))
133 return nullptr;
134
135 // Verify that the row header is not part of an entire row of headers.
136 // In that case, it is unlikely this is a row header.
137 bool allHeadersInRow = true;
138 for (const auto& cell : rowChildren) {
139 if (cell->node() && !cell->node()->hasTagName(thTag)) {
140 allHeadersInRow = false;
141 break;
142 }
143 }
144 if (allHeadersInRow)
145 return nullptr;
146
147 return cell;
148}
149
150void AccessibilityTableRow::addChildren()
151{
152 AccessibilityRenderObject::addChildren();
153
154 // "ARIA 1.1, If the set of columns which is present in the DOM is contiguous, and if there are no cells which span more than one row or
155 // column in that set, then authors may place aria-colindex on each row, setting the value to the index of the first column of the set."
156 // Update child cells' axColIndex if there's an aria-colindex value set for the row. So the cell doesn't have to go through the siblings
157 // to calculate the index.
158 int colIndex = axColumnIndex();
159 if (colIndex == -1)
160 return;
161
162 unsigned index = 0;
163 for (const auto& cell : children()) {
164 if (is<AccessibilityTableCell>(*cell))
165 downcast<AccessibilityTableCell>(*cell).setAXColIndexFromRow(colIndex + index);
166 index++;
167 }
168}
169
170int AccessibilityTableRow::axColumnIndex() const
171{
172 const AtomicString& colIndexValue = getAttribute(aria_colindexAttr);
173 if (colIndexValue.toInt() >= 1)
174 return colIndexValue.toInt();
175
176 return -1;
177}
178
179int AccessibilityTableRow::axRowIndex() const
180{
181 const AtomicString& rowIndexValue = getAttribute(aria_rowindexAttr);
182 if (rowIndexValue.toInt() >= 1)
183 return rowIndexValue.toInt();
184
185 return -1;
186}
187
188} // namespace WebCore
189