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 "AccessibilityTableColumn.h"
31
32#include "AXObjectCache.h"
33#include "AccessibilityTableCell.h"
34#include "HTMLElement.h"
35#include "HTMLNames.h"
36#include "RenderTable.h"
37#include "RenderTableCell.h"
38#include "RenderTableSection.h"
39
40namespace WebCore {
41
42using namespace HTMLNames;
43
44AccessibilityTableColumn::AccessibilityTableColumn()
45{
46}
47
48AccessibilityTableColumn::~AccessibilityTableColumn()
49{
50}
51
52PassRefPtr<AccessibilityTableColumn> AccessibilityTableColumn::create()
53{
54    return adoptRef(new AccessibilityTableColumn());
55}
56
57void AccessibilityTableColumn::setParent(AccessibilityObject* parent)
58{
59    AccessibilityMockObject::setParent(parent);
60
61    clearChildren();
62}
63
64LayoutRect AccessibilityTableColumn::elementRect() const
65{
66    // this will be filled in when addChildren is called
67    return m_columnRect;
68}
69
70AccessibilityObject* AccessibilityTableColumn::headerObject()
71{
72    if (!m_parent)
73        return 0;
74
75    RenderObject* renderer = m_parent->renderer();
76    if (!renderer)
77        return 0;
78
79    if (!m_parent->isAccessibilityTable())
80        return 0;
81
82    AccessibilityTable* parentTable = toAccessibilityTable(m_parent);
83    if (parentTable->isAriaTable()) {
84        for (const auto& cell : children()) {
85            if (cell->ariaRoleAttribute() == ColumnHeaderRole)
86                return cell.get();
87        }
88
89        return 0;
90    }
91
92    if (!renderer->isTable())
93        return 0;
94
95    RenderTable* table = toRenderTable(renderer);
96
97    AccessibilityObject* headerObject = 0;
98
99    // try the <thead> section first. this doesn't require th tags
100    headerObject = headerObjectForSection(table->header(), false);
101
102    if (headerObject)
103        return headerObject;
104
105    // now try for <th> tags in the first body
106    headerObject = headerObjectForSection(table->firstBody(), true);
107
108    return headerObject;
109}
110
111AccessibilityObject* AccessibilityTableColumn::headerObjectForSection(RenderTableSection* section, bool thTagRequired)
112{
113    if (!section)
114        return 0;
115
116    unsigned numCols = section->numColumns();
117    if (m_columnIndex >= numCols)
118        return 0;
119
120    if (!section->numRows())
121        return 0;
122
123    RenderTableCell* cell = 0;
124    // also account for cells that have a span
125    for (int testCol = m_columnIndex; testCol >= 0; --testCol) {
126        RenderTableCell* testCell = section->primaryCellAt(0, testCol);
127        if (!testCell)
128            continue;
129
130        // we've reached a cell that doesn't even overlap our column
131        // it can't be our header
132        if ((testCell->col() + (testCell->colSpan()-1)) < m_columnIndex)
133            break;
134
135        if (!testCell->element())
136            continue;
137
138        if (thTagRequired && !testCell->element()->hasTagName(thTag))
139            continue;
140
141        cell = testCell;
142    }
143
144    if (!cell)
145        return 0;
146
147    return axObjectCache()->getOrCreate(cell);
148}
149
150bool AccessibilityTableColumn::computeAccessibilityIsIgnored() const
151{
152    if (!m_parent)
153        return true;
154
155#if PLATFORM(IOS) || PLATFORM(GTK) || PLATFORM(EFL)
156    return true;
157#endif
158
159    return m_parent->accessibilityIsIgnored();
160}
161
162void AccessibilityTableColumn::addChildren()
163{
164    ASSERT(!m_haveChildren);
165
166    m_haveChildren = true;
167    if (!m_parent || !m_parent->isAccessibilityTable())
168        return;
169
170    AccessibilityTable* parentTable = toAccessibilityTable(m_parent);
171    int numRows = parentTable->rowCount();
172
173    for (int i = 0; i < numRows; i++) {
174        AccessibilityTableCell* cell = parentTable->cellForColumnAndRow(m_columnIndex, i);
175        if (!cell)
176            continue;
177
178        // make sure the last one isn't the same as this one (rowspan cells)
179        if (m_children.size() > 0 && m_children.last() == cell)
180            continue;
181
182        m_children.append(cell);
183        m_columnRect.unite(cell->elementRect());
184    }
185}
186
187} // namespace WebCore
188