AccessibilityTable.cpp [plain text]
#include "config.h"
#include "AccessibilityTable.h"
#include "AccessibilityTableCell.h"
#include "AccessibilityTableColumn.h"
#include "AccessibilityTableHeaderContainer.h"
#include "AccessibilityTableRow.h"
#include "AXObjectCache.h"
#include "HTMLNames.h"
#include "HTMLTableElement.h"
#include "HTMLTableCaptionElement.h"
#include "HTMLTableCellElement.h"
#include "RenderObject.h"
#include "RenderTable.h"
#include "RenderTableCell.h"
#include "RenderTableSection.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
AccessibilityTable::AccessibilityTable(RenderObject* renderer)
: AccessibilityRenderObject(renderer),
m_headerContainer(0)
{
#if ACCESSIBILITY_TABLES
m_isAccessibilityTable = isTableExposableThroughAccessibility();
#else
m_isAccessibilityTable = false;
#endif
}
AccessibilityTable::~AccessibilityTable()
{
}
PassRefPtr<AccessibilityTable> AccessibilityTable::create(RenderObject* renderer)
{
return adoptRef(new AccessibilityTable(renderer));
}
bool AccessibilityTable::isTableExposableThroughAccessibility()
{
if (!m_renderer || !m_renderer->isTable())
return false;
AccessibilityRole ariaRole = ariaRoleAttribute();
if (ariaRole == TableRole)
return true;
if (ariaRole != UnknownRole)
return false;
RenderTable* table = static_cast<RenderTable*>(m_renderer);
Node* tableNode = table->element();
if (!tableNode || !tableNode->hasTagName(tableTag))
return false;
HTMLTableElement* tableElement = static_cast<HTMLTableElement*>(tableNode);
if (!tableElement->summary().isEmpty() || tableElement->tHead() || tableElement->tFoot() || tableElement->caption())
return true;
if (!tableElement->rules().isEmpty())
return true;
RenderTableSection* firstBody = table->firstBody();
if (!firstBody)
return false;
int numCols = firstBody->numColumns();
int numRows = firstBody->numRows();
if (numRows == 1 && numCols == 1)
return false;
RenderStyle* tableStyle = table->style();
if (!tableStyle)
return false;
Color tableBGColor = tableStyle->backgroundColor();
unsigned validCellCount = 0;
unsigned borderedCellCount = 0;
unsigned backgroundDifferenceCellCount = 0;
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col < numCols; ++col) {
RenderTableCell* cell = firstBody->cellAt(row, col).cell;
if (!cell)
continue;
Node* cellNode = cell->element();
if (!cellNode)
continue;
if (cell->width() < 1 || cell->height() < 1)
continue;
validCellCount++;
HTMLTableCellElement* cellElement = static_cast<HTMLTableCellElement*>(cellNode);
if (!cellElement->headers().isEmpty() || !cellElement->abbr().isEmpty() ||
!cellElement->axis().isEmpty() || !cellElement->scope().isEmpty())
return true;
RenderStyle* renderStyle = cell->style();
if (!renderStyle)
continue;
if ((cell->borderTop() > 0 && cell->borderBottom() > 0) ||
(cell->borderLeft() > 0 && cell->borderRight() > 0))
borderedCellCount++;
Color cellColor = renderStyle->backgroundColor();
if (table->hBorderSpacing() > 0 && table->vBorderSpacing() > 0 &&
tableBGColor != cellColor && cellColor.alpha() != 1)
backgroundDifferenceCellCount++;
if (borderedCellCount >= 10 || backgroundDifferenceCellCount >= 10)
return true;
}
}
if (validCellCount <= 1)
return false;
unsigned neededCellCount = validCellCount / 2;
if (borderedCellCount >= neededCellCount)
return true;
if (backgroundDifferenceCellCount >= neededCellCount)
return true;
return false;
}
void AccessibilityTable::clearChildren()
{
m_children.clear();
m_rows.clear();
m_columns.clear();
m_haveChildren = false;
}
void AccessibilityTable::addChildren()
{
if (!isDataTable()) {
AccessibilityRenderObject::addChildren();
return;
}
ASSERT(!m_haveChildren);
m_haveChildren = true;
if (!m_renderer)
return;
RenderTable* table = static_cast<RenderTable*>(m_renderer);
AXObjectCache* axCache = m_renderer->document()->axObjectCache();
RenderTableSection* tableSection = table->header();
if (!tableSection)
tableSection = table->firstBody();
if (!tableSection)
return;
RenderTableSection* initialTableSection = tableSection;
while (tableSection) {
HashSet<AccessibilityObject*> appendedRows;
unsigned numRows = tableSection->numRows();
unsigned numCols = tableSection->numColumns();
for (unsigned rowIndex = 0; rowIndex < numRows; ++rowIndex) {
for (unsigned colIndex = 0; colIndex < numCols; ++colIndex) {
RenderTableCell* cell = tableSection->cellAt(rowIndex, colIndex).cell;
if (!cell)
continue;
AccessibilityObject* rowObject = axCache->get(cell->parent());
if (!rowObject->isTableRow())
continue;
AccessibilityTableRow* row = static_cast<AccessibilityTableRow*>(rowObject);
if (appendedRows.contains(row))
continue;
row->setRowIndex((int)m_rows.size());
m_rows.append(row);
m_children.append(row);
appendedRows.add(row);
}
}
tableSection = table->sectionBelow(tableSection, true);
}
unsigned length = initialTableSection->numColumns();
for (unsigned i = 0; i < length; ++i) {
AccessibilityTableColumn* column = static_cast<AccessibilityTableColumn*>(axCache->get(ColumnRole));
column->setColumnIndex((int)i);
column->setParentTable(this);
m_columns.append(column);
m_children.append(column);
}
AccessibilityObject* headerContainerObject = headerContainer();
if (headerContainerObject)
m_children.append(headerContainerObject);
}
AccessibilityObject* AccessibilityTable::headerContainer()
{
if (m_headerContainer)
return m_headerContainer;
m_headerContainer = static_cast<AccessibilityTableHeaderContainer*>(axObjectCache()->get(TableHeaderContainerRole));
m_headerContainer->setParentTable(this);
return m_headerContainer;
}
AccessibilityObject::AccessibilityChildrenVector& AccessibilityTable::columns()
{
if (!hasChildren())
addChildren();
return m_columns;
}
AccessibilityObject::AccessibilityChildrenVector& AccessibilityTable::rows()
{
if (!hasChildren())
addChildren();
return m_rows;
}
void AccessibilityTable::rowHeaders(AccessibilityChildrenVector& headers)
{
if (!m_renderer)
return;
if (!hasChildren())
addChildren();
unsigned rowCount = m_rows.size();
for (unsigned k = 0; k < rowCount; ++k) {
AccessibilityObject* header = static_cast<AccessibilityTableRow*>(m_rows[k].get())->headerObject();
if (!header)
continue;
headers.append(header);
}
}
void AccessibilityTable::columnHeaders(AccessibilityChildrenVector& headers)
{
if (!m_renderer)
return;
if (!hasChildren())
addChildren();
unsigned colCount = m_columns.size();
for (unsigned k = 0; k < colCount; ++k) {
AccessibilityObject* header = static_cast<AccessibilityTableColumn*>(m_columns[k].get())->headerObject();
if (!header)
continue;
headers.append(header);
}
}
void AccessibilityTable::cells(AccessibilityObject::AccessibilityChildrenVector& cells)
{
if (!m_renderer)
return;
if (!hasChildren())
addChildren();
int numRows = m_rows.size();
for (int row = 0; row < numRows; ++row) {
AccessibilityChildrenVector rowChildren = m_rows[row]->children();
cells.append(rowChildren);
}
}
const unsigned AccessibilityTable::columnCount()
{
if (!hasChildren())
addChildren();
return m_columns.size();
}
const unsigned AccessibilityTable::rowCount()
{
if (!hasChildren())
addChildren();
return m_rows.size();
}
AccessibilityTableCell* AccessibilityTable::cellForColumnAndRow(unsigned column, unsigned row)
{
if (!m_renderer)
return 0;
if (!hasChildren())
addChildren();
RenderTable* table = static_cast<RenderTable*>(m_renderer);
RenderTableSection* tableSection = table->header();
if (!tableSection)
tableSection = table->firstBody();
RenderTableCell* cell = 0;
unsigned rowCount = 0;
unsigned rowOffset = 0;
while (tableSection) {
rowCount += tableSection->numRows();
unsigned numCols = tableSection->numColumns();
if (row < rowCount && column < numCols) {
int sectionSpecificRow = row - rowOffset;
cell = tableSection->cellAt(sectionSpecificRow, column).cell;
if (!cell) {
for (int testRow = sectionSpecificRow-1; testRow >= 0; --testRow) {
cell = tableSection->cellAt(testRow, column).cell;
if (cell && ((cell->row() + (cell->rowSpan()-1)) >= (int)sectionSpecificRow))
break;
cell = 0;
}
if (!cell) {
for (int testCol = column-1; testCol >= 0; --testCol) {
cell = tableSection->cellAt(sectionSpecificRow, testCol).cell;
if (cell && ((cell->col() + (cell->colSpan()-1)) >= (int)column))
break;
cell = 0;
}
}
}
}
if (cell)
break;
rowOffset += rowCount;
if (row < rowOffset)
break;
tableSection = table->sectionBelow(tableSection, true);
}
if (!cell)
return 0;
AccessibilityObject* cellObject = axObjectCache()->get(cell);
ASSERT(cellObject->isTableCell());
return static_cast<AccessibilityTableCell*>(cellObject);
}
AccessibilityRole AccessibilityTable::roleValue() const
{
if (!isDataTable())
return AccessibilityRenderObject::roleValue();
return TableRole;
}
bool AccessibilityTable::accessibilityIsIgnored() const
{
if (!isDataTable())
return AccessibilityRenderObject::accessibilityIsIgnored();
return false;
}
String AccessibilityTable::title() const
{
if (!isDataTable())
return AccessibilityRenderObject::title();
String title;
if (!m_renderer)
return title;
Node *tableElement = m_renderer->element();
if (tableElement) {
HTMLTableCaptionElement* caption = static_cast<HTMLTableElement*>(tableElement)->caption();
if (caption)
title = caption->innerText();
}
if (title.isEmpty())
title = AccessibilityRenderObject::title();
return title;
}
bool AccessibilityTable::isDataTable() const
{
if (!m_renderer)
return false;
return m_isAccessibilityTable;
}
}