~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/webkit/WebCore/page/AccessibilityTable.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "AccessibilityTable.h"
31
 
 
32
 
#include "AccessibilityTableCell.h"
33
 
#include "AccessibilityTableColumn.h"
34
 
#include "AccessibilityTableHeaderContainer.h"
35
 
#include "AccessibilityTableRow.h"
36
 
#include "AXObjectCache.h"
37
 
#include "HTMLNames.h"
38
 
#include "HTMLTableElement.h"
39
 
#include "HTMLTableCaptionElement.h"
40
 
#include "HTMLTableCellElement.h"
41
 
#include "RenderObject.h"
42
 
#include "RenderTable.h"
43
 
#include "RenderTableCell.h"
44
 
#include "RenderTableSection.h"
45
 
 
46
 
using namespace std;
47
 
 
48
 
namespace WebCore {
49
 
 
50
 
using namespace HTMLNames;
51
 
 
52
 
AccessibilityTable::AccessibilityTable(RenderObject* renderer)
53
 
    : AccessibilityRenderObject(renderer),
54
 
    m_headerContainer(0)
55
 
{
56
 
    // FIXME: We need to disable Accessibility Tables entirely on the Mac until <rdar://problem/6372481> is resolved.
57
 
#if PLATFORM(MAC)
58
 
    m_isAccessibilityTable = false;
59
 
#else    
60
 
    m_isAccessibilityTable = isTableExposableThroughAccessibility();
61
 
#endif
62
 
 
63
 
}
64
 
 
65
 
AccessibilityTable::~AccessibilityTable()
66
 
{
67
 
}
68
 
 
69
 
PassRefPtr<AccessibilityTable> AccessibilityTable::create(RenderObject* renderer)
70
 
{
71
 
    return adoptRef(new AccessibilityTable(renderer));
72
 
}
73
 
 
74
 
bool AccessibilityTable::isTableExposableThroughAccessibility()
75
 
{
76
 
    // the following is a heuristic used to determine if a
77
 
    // <table> should be exposed as an AXTable. The goal
78
 
    // is to only show "data" tables
79
 
    
80
 
    if (!m_renderer || !m_renderer->isTable())
81
 
        return false;
82
 
    
83
 
    // if the developer assigned an aria role to this, then we shouldn't 
84
 
    // expose it as a table, unless, of course, the aria role is a table
85
 
    AccessibilityRole ariaRole = ariaRoleAttribute();
86
 
    if (ariaRole == TableRole)
87
 
        return true;
88
 
    if (ariaRole != UnknownRole)
89
 
        return false;
90
 
    
91
 
    RenderTable* table = static_cast<RenderTable*>(m_renderer);
92
 
    
93
 
    // this employs a heuristic to determine if this table should appear. 
94
 
    // Only "data" tables should be exposed as tables. 
95
 
    // Unfortunately, there is no good way to determine the difference
96
 
    // between a "layout" table and a "data" table
97
 
    
98
 
    Node* tableNode = table->element();
99
 
    if (!tableNode || !tableNode->hasTagName(tableTag))
100
 
        return false;
101
 
    
102
 
    // if there is a caption element, summary, THEAD, or TFOOT section, it's most certainly a data table
103
 
    HTMLTableElement* tableElement = static_cast<HTMLTableElement*>(tableNode);
104
 
    if (!tableElement->summary().isEmpty() || tableElement->tHead() || tableElement->tFoot() || tableElement->caption())
105
 
        return true;
106
 
    
107
 
    // if someone used "rules" attribute than the table should appear
108
 
    if (!tableElement->rules().isEmpty())
109
 
        return true;    
110
 
    
111
 
    // go through the cell's and check for tell-tale signs of "data" table status
112
 
    // cells have borders, or use attributes like headers, abbr, scope or axis
113
 
    RenderTableSection* firstBody = table->firstBody();
114
 
    if (!firstBody)
115
 
        return false;
116
 
    
117
 
    int numCols = firstBody->numColumns();
118
 
    int numRows = firstBody->numRows();
119
 
    
120
 
    // if there's only one cell, it's not a good AXTable candidate
121
 
    if (numRows == 1 && numCols == 1)
122
 
        return false;
123
 
    
124
 
    // store the background color of the table to check against cell's background colors
125
 
    RenderStyle* tableStyle = table->style();
126
 
    if (!tableStyle)
127
 
        return false;
128
 
    Color tableBGColor = tableStyle->backgroundColor();
129
 
    
130
 
    // check enough of the cells to find if the table matches our criteria
131
 
    // Criteria: 
132
 
    //   1) must have at least one valid cell (and)
133
 
    //   2) at least half of cells have borders (or)
134
 
    //   3) at least half of cells have different bg colors than the table, and there is cell spacing
135
 
    unsigned validCellCount = 0;
136
 
    unsigned borderedCellCount = 0;
137
 
    unsigned backgroundDifferenceCellCount = 0;
138
 
    
139
 
    for (int row = 0; row < numRows; ++row) {
140
 
        for (int col = 0; col < numCols; ++col) {    
141
 
            RenderTableCell* cell = firstBody->cellAt(row, col).cell;
142
 
            if (!cell)
143
 
                continue;
144
 
            Node* cellNode = cell->element();
145
 
            if (!cellNode)
146
 
                continue;
147
 
            
148
 
            if (cell->width() < 1 || cell->height() < 1)
149
 
                continue;
150
 
            
151
 
            validCellCount++;
152
 
            
153
 
            HTMLTableCellElement* cellElement = static_cast<HTMLTableCellElement*>(cellNode);
154
 
            
155
 
            // in this case, the developer explicitly assigned a "data" table attribute
156
 
            if (!cellElement->headers().isEmpty() || !cellElement->abbr().isEmpty() || 
157
 
                !cellElement->axis().isEmpty() || !cellElement->scope().isEmpty())
158
 
                return true;
159
 
            
160
 
            RenderStyle* renderStyle = cell->style();
161
 
            if (!renderStyle)
162
 
                continue;
163
 
 
164
 
            // a cell needs to have matching bordered sides, before it can be considered a bordered cell.
165
 
            if ((cell->borderTop() > 0 && cell->borderBottom() > 0) ||
166
 
                (cell->borderLeft() > 0 && cell->borderRight() > 0))
167
 
                borderedCellCount++;
168
 
            
169
 
            // if the cell has a different color from the table and there is cell spacing,
170
 
            // then it is probably a data table cell (spacing and colors take the place of borders)
171
 
            Color cellColor = renderStyle->backgroundColor();
172
 
            if (table->hBorderSpacing() > 0 && table->vBorderSpacing() > 0 && 
173
 
                tableBGColor != cellColor && cellColor.alpha() != 1)
174
 
                backgroundDifferenceCellCount++;
175
 
            
176
 
            // if we've found 10 "good" cells, we don't need to keep searching
177
 
            if (borderedCellCount >= 10 || backgroundDifferenceCellCount >= 10)
178
 
                return true;
179
 
        }
180
 
    }
181
 
 
182
 
    // if there is less than two valid cells, it's not a data table
183
 
    if (validCellCount <= 1)
184
 
        return false;
185
 
    
186
 
    // half of the cells had borders, it's a data table
187
 
    unsigned neededCellCount = validCellCount / 2;
188
 
    if (borderedCellCount >= neededCellCount)
189
 
        return true;
190
 
    
191
 
    // half had different background colors, it's a data table
192
 
    if (backgroundDifferenceCellCount >= neededCellCount)
193
 
        return true;
194
 
 
195
 
    return false;
196
 
}
197
 
    
198
 
void AccessibilityTable::clearChildren()
199
 
{
200
 
    m_children.clear();
201
 
    m_rows.clear();
202
 
    m_columns.clear();
203
 
    m_haveChildren = false;
204
 
}
205
 
 
206
 
void AccessibilityTable::addChildren()
207
 
{
208
 
    if (!isDataTable()) {
209
 
        AccessibilityRenderObject::addChildren();
210
 
        return;
211
 
    }
212
 
    
213
 
    ASSERT(!m_haveChildren); 
214
 
    
215
 
    m_haveChildren = true;
216
 
    if (!m_renderer)
217
 
        return;
218
 
    
219
 
    RenderTable* table = static_cast<RenderTable*>(m_renderer);
220
 
    AXObjectCache* axCache = m_renderer->document()->axObjectCache();
221
 
 
222
 
    // go through all the available sections to pull out the rows
223
 
    // and add them as children
224
 
    RenderTableSection* tableSection = table->header();
225
 
    if (!tableSection)
226
 
        tableSection = table->firstBody();
227
 
    
228
 
    if (!tableSection)
229
 
        return;
230
 
    
231
 
    RenderTableSection* initialTableSection = tableSection;
232
 
    
233
 
    while (tableSection) {
234
 
        
235
 
        HashSet<AccessibilityObject*> appendedRows;
236
 
 
237
 
        unsigned numRows = tableSection->numRows();
238
 
        unsigned numCols = tableSection->numColumns();
239
 
        for (unsigned rowIndex = 0; rowIndex < numRows; ++rowIndex) {
240
 
            for (unsigned colIndex = 0; colIndex < numCols; ++colIndex) {
241
 
                
242
 
                RenderTableCell* cell = tableSection->cellAt(rowIndex, colIndex).cell;
243
 
                if (!cell)
244
 
                    continue;
245
 
                
246
 
                AccessibilityObject* rowObject = axCache->get(cell->parent());
247
 
                if (!rowObject->isTableRow())
248
 
                    continue;
249
 
                
250
 
                AccessibilityTableRow* row = static_cast<AccessibilityTableRow*>(rowObject);
251
 
                // we need to check every cell for a new row, because cell spans
252
 
                // can cause us to mess rows if we just check the first column
253
 
                if (appendedRows.contains(row))
254
 
                    continue;
255
 
                
256
 
                row->setRowIndex((int)m_rows.size());        
257
 
                m_rows.append(row);
258
 
                m_children.append(row);
259
 
                appendedRows.add(row);
260
 
            }
261
 
        }
262
 
        
263
 
        tableSection = table->sectionBelow(tableSection, true);
264
 
    }
265
 
    
266
 
    // make the columns based on the number of columns in the first body
267
 
    unsigned length = initialTableSection->numColumns();
268
 
    for (unsigned i = 0; i < length; ++i) {
269
 
        AccessibilityTableColumn* column = static_cast<AccessibilityTableColumn*>(axCache->get(ColumnRole));
270
 
        column->setColumnIndex((int)i);
271
 
        column->setParentTable(this);
272
 
        m_columns.append(column);
273
 
        m_children.append(column);
274
 
    }
275
 
    
276
 
    AccessibilityObject* headerContainerObject = headerContainer();
277
 
    if (headerContainerObject)
278
 
        m_children.append(headerContainerObject);
279
 
}
280
 
    
281
 
AccessibilityObject* AccessibilityTable::headerContainer()
282
 
{
283
 
    if (m_headerContainer)
284
 
        return m_headerContainer;
285
 
    
286
 
    m_headerContainer = static_cast<AccessibilityTableHeaderContainer*>(axObjectCache()->get(TableHeaderContainerRole));
287
 
    m_headerContainer->setParentTable(this);
288
 
    
289
 
    return m_headerContainer;
290
 
}
291
 
 
292
 
AccessibilityObject::AccessibilityChildrenVector& AccessibilityTable::columns()
293
 
{
294
 
    if (!hasChildren())
295
 
        addChildren();
296
 
        
297
 
    return m_columns;
298
 
}
299
 
 
300
 
AccessibilityObject::AccessibilityChildrenVector& AccessibilityTable::rows()
301
 
{
302
 
    if (!hasChildren())
303
 
        addChildren();
304
 
 
305
 
    return m_rows;
306
 
}
307
 
    
308
 
void AccessibilityTable::rowHeaders(AccessibilityChildrenVector& headers)
309
 
{
310
 
    if (!m_renderer)
311
 
        return;
312
 
    
313
 
    if (!hasChildren())
314
 
        addChildren();
315
 
    
316
 
    unsigned rowCount = m_rows.size();
317
 
    for (unsigned k = 0; k < rowCount; ++k) {
318
 
        AccessibilityObject* header = static_cast<AccessibilityTableRow*>(m_rows[k].get())->headerObject();
319
 
        if (!header)
320
 
            continue;
321
 
        headers.append(header);
322
 
    }
323
 
}
324
 
 
325
 
void AccessibilityTable::columnHeaders(AccessibilityChildrenVector& headers)
326
 
{
327
 
    if (!m_renderer)
328
 
        return;
329
 
    
330
 
    if (!hasChildren())
331
 
        addChildren();
332
 
    
333
 
    unsigned colCount = m_columns.size();
334
 
    for (unsigned k = 0; k < colCount; ++k) {
335
 
        AccessibilityObject* header = static_cast<AccessibilityTableColumn*>(m_columns[k].get())->headerObject();
336
 
        if (!header)
337
 
            continue;
338
 
        headers.append(header);
339
 
    }
340
 
}
341
 
    
342
 
void AccessibilityTable::cells(AccessibilityObject::AccessibilityChildrenVector& cells)
343
 
{
344
 
    if (!m_renderer)
345
 
        return;
346
 
    
347
 
    if (!hasChildren())
348
 
        addChildren();
349
 
    
350
 
    int numRows = m_rows.size();
351
 
    for (int row = 0; row < numRows; ++row) {
352
 
        AccessibilityChildrenVector rowChildren = m_rows[row]->children();
353
 
        cells.append(rowChildren);
354
 
    }
355
 
}
356
 
    
357
 
const unsigned AccessibilityTable::columnCount()
358
 
{
359
 
    if (!hasChildren())
360
 
        addChildren();
361
 
    
362
 
    return m_columns.size();    
363
 
}
364
 
    
365
 
const unsigned AccessibilityTable::rowCount()
366
 
{
367
 
    if (!hasChildren())
368
 
        addChildren();
369
 
    
370
 
    return m_rows.size();
371
 
}
372
 
    
373
 
AccessibilityTableCell* AccessibilityTable::cellForColumnAndRow(unsigned column, unsigned row)
374
 
{
375
 
    if (!m_renderer)
376
 
        return 0;
377
 
    
378
 
    if (!hasChildren())
379
 
        addChildren();
380
 
    
381
 
    RenderTable* table = static_cast<RenderTable*>(m_renderer);
382
 
    RenderTableSection* tableSection = table->header();
383
 
    if (!tableSection)
384
 
        tableSection = table->firstBody();
385
 
    
386
 
    RenderTableCell* cell = 0;
387
 
    unsigned rowCount = 0;
388
 
    unsigned rowOffset = 0;
389
 
    while (tableSection) {
390
 
        
391
 
        rowCount += tableSection->numRows();
392
 
        unsigned numCols = tableSection->numColumns();
393
 
        
394
 
        if (row < rowCount && column < numCols) {
395
 
            int sectionSpecificRow = row - rowOffset;
396
 
            cell = tableSection->cellAt(sectionSpecificRow, column).cell;
397
 
            
398
 
            // we didn't find the cell, which means there's spanning happening
399
 
            // search backwards to find the spanning cell
400
 
            if (!cell) {
401
 
                
402
 
                // first try rows
403
 
                for (int testRow = sectionSpecificRow-1; testRow >= 0; --testRow) {
404
 
                    cell = tableSection->cellAt(testRow, column).cell;
405
 
                    // cell overlapped. use this one
406
 
                    if (cell && ((cell->row() + (cell->rowSpan()-1)) >= (int)sectionSpecificRow))
407
 
                        break;
408
 
                    cell = 0;
409
 
                }
410
 
                
411
 
                if (!cell) {
412
 
                    // try cols
413
 
                    for (int testCol = column-1; testCol >= 0; --testCol) {
414
 
                        cell = tableSection->cellAt(sectionSpecificRow, testCol).cell;
415
 
                        // cell overlapped. use this one
416
 
                        if (cell && ((cell->col() + (cell->colSpan()-1)) >= (int)column))
417
 
                            break;
418
 
                        cell = 0;
419
 
                    }
420
 
                }
421
 
            }
422
 
        }
423
 
        
424
 
        if (cell)
425
 
            break;
426
 
        
427
 
        rowOffset += rowCount;
428
 
        // we didn't find anything between the rows we should have
429
 
        if (row < rowOffset)
430
 
            break;
431
 
        tableSection = table->sectionBelow(tableSection, true);        
432
 
    }
433
 
    
434
 
    if (!cell)
435
 
        return 0;
436
 
    
437
 
    AccessibilityObject* cellObject = axObjectCache()->get(cell);
438
 
    ASSERT(cellObject->isTableCell());
439
 
    
440
 
    return static_cast<AccessibilityTableCell*>(cellObject);
441
 
}
442
 
 
443
 
AccessibilityRole AccessibilityTable::roleValue() const
444
 
{
445
 
    if (!isDataTable())
446
 
        return AccessibilityRenderObject::roleValue();
447
 
 
448
 
    return TableRole;
449
 
}
450
 
    
451
 
bool AccessibilityTable::accessibilityIsIgnored() const
452
 
{
453
 
    if (!isDataTable())
454
 
        return AccessibilityRenderObject::accessibilityIsIgnored();
455
 
    
456
 
    return false;
457
 
}
458
 
    
459
 
String AccessibilityTable::title() const
460
 
{
461
 
    if (!isDataTable())
462
 
        return AccessibilityRenderObject::title();
463
 
    
464
 
    String title;
465
 
    if (!m_renderer)
466
 
        return title;
467
 
    
468
 
    // see if there is a caption
469
 
    Node *tableElement = m_renderer->element();
470
 
    if (tableElement) {
471
 
        HTMLTableCaptionElement* caption = static_cast<HTMLTableElement*>(tableElement)->caption();
472
 
        if (caption)
473
 
            title = caption->innerText();
474
 
    }
475
 
    
476
 
    // try the standard 
477
 
    if (title.isEmpty())
478
 
        title = AccessibilityRenderObject::title();
479
 
    
480
 
    return title;
481
 
}
482
 
 
483
 
bool AccessibilityTable::isDataTable() const
484
 
{
485
 
    if (!m_renderer)
486
 
        return false;
487
 
    
488
 
    return m_isAccessibilityTable;
489
 
}
490
 
 
491
 
} // namespace WebCore