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 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 "AccessibilityARIAGridRow.h"
31
32#include "AccessibilityObject.h"
33#include "AccessibilityTable.h"
34
35namespace WebCore {
36
37AccessibilityARIAGridRow::AccessibilityARIAGridRow(RenderObject* renderer)
38 : AccessibilityTableRow(renderer)
39{
40}
41
42AccessibilityARIAGridRow::~AccessibilityARIAGridRow() = default;
43
44Ref<AccessibilityARIAGridRow> AccessibilityARIAGridRow::create(RenderObject* renderer)
45{
46 return adoptRef(*new AccessibilityARIAGridRow(renderer));
47}
48
49bool AccessibilityARIAGridRow::isARIATreeGridRow() const
50{
51 AccessibilityObject* parent = parentTable();
52 if (!parent)
53 return false;
54
55 return parent->isTreeGrid();
56}
57
58void AccessibilityARIAGridRow::disclosedRows(AccessibilityChildrenVector& disclosedRows)
59{
60 // The contiguous disclosed rows will be the rows in the table that
61 // have an aria-level of plus 1 from this row.
62 AccessibilityObject* parent = parentObjectUnignored();
63 if (!is<AccessibilityTable>(*parent) || !downcast<AccessibilityTable>(*parent).isExposableThroughAccessibility())
64 return;
65
66 // Search for rows that match the correct level.
67 // Only take the subsequent rows from this one that are +1 from this row's level.
68 int index = rowIndex();
69 if (index < 0)
70 return;
71
72 unsigned level = hierarchicalLevel();
73 auto& allRows = downcast<AccessibilityTable>(*parent).rows();
74 int rowCount = allRows.size();
75 for (int k = index + 1; k < rowCount; ++k) {
76 AccessibilityObject* row = allRows[k].get();
77 // Stop at the first row that doesn't match the correct level.
78 if (row->hierarchicalLevel() != level + 1)
79 break;
80
81 disclosedRows.append(row);
82 }
83}
84
85AccessibilityObject* AccessibilityARIAGridRow::disclosedByRow() const
86{
87 // The row that discloses this one is the row in the table
88 // that is aria-level subtract 1 from this row.
89 AccessibilityObject* parent = parentObjectUnignored();
90 if (!is<AccessibilityTable>(*parent) || !downcast<AccessibilityTable>(*parent).isExposableThroughAccessibility())
91 return nullptr;
92
93 // If the level is 1 or less, than nothing discloses this row.
94 unsigned level = hierarchicalLevel();
95 if (level <= 1)
96 return nullptr;
97
98 // Search for the previous row that matches the correct level.
99 int index = rowIndex();
100 auto& allRows = downcast<AccessibilityTable>(parent)->rows();
101 int rowCount = allRows.size();
102 if (index >= rowCount)
103 return nullptr;
104
105 for (int k = index - 1; k >= 0; --k) {
106 AccessibilityObject* row = allRows[k].get();
107 if (row->hierarchicalLevel() == level - 1)
108 return row;
109 }
110
111 return nullptr;
112}
113
114AccessibilityObject* AccessibilityARIAGridRow::parentObjectUnignored() const
115{
116 return parentTable();
117}
118
119AccessibilityTable* AccessibilityARIAGridRow::parentTable() const
120{
121 // The parent table might not be the direct ancestor of the row unfortunately. ARIA states that role="grid" should
122 // only have "row" elements, but if not, we still should handle it gracefully by finding the right table.
123 for (AccessibilityObject* parent = parentObject(); parent; parent = parent->parentObject()) {
124 // The parent table for an ARIA grid row should be an ARIA table.
125 // Unless the row is a native tr element.
126 if (is<AccessibilityTable>(*parent)) {
127 AccessibilityTable& tableParent = downcast<AccessibilityTable>(*parent);
128 if (tableParent.isExposableThroughAccessibility() && (tableParent.isAriaTable() || node()->hasTagName(HTMLNames::trTag)))
129 return &tableParent;
130 }
131 }
132
133 return nullptr;
134}
135
136AccessibilityObject* AccessibilityARIAGridRow::headerObject()
137{
138 for (const auto& child : children()) {
139 if (child->ariaRoleAttribute() == AccessibilityRole::RowHeader)
140 return child.get();
141 }
142
143 return nullptr;
144}
145
146} // namespace WebCore
147