AccessibilityTable.cpp [plain text]
#include "config.h"
#include "AccessibilityTable.h"
#include "AXObjectCache.h"
#include "AccessibilityTableCell.h"
#include "AccessibilityTableColumn.h"
#include "AccessibilityTableHeaderContainer.h"
#include "AccessibilityTableRow.h"
#include "HTMLNames.h"
#include "HTMLTableCaptionElement.h"
#include "HTMLTableCellElement.h"
#include "HTMLTableElement.h"
#include "RenderObject.h"
#include "RenderTable.h"
#include "RenderTableCell.h"
#include "RenderTableSection.h"
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::hasARIARole() const
{
if (!m_renderer)
return false;
AccessibilityRole ariaRole = ariaRoleAttribute();
if (ariaRole != UnknownRole)
return true;
return false;
}
bool AccessibilityTable::isAccessibilityTable() const
{
if (!m_renderer)
return false;
return m_isAccessibilityTable;
}
bool AccessibilityTable::isDataTable() const
{
if (!m_renderer)
return false;
if (hasARIARole())
return false;
RenderTable* table = toRenderTable(m_renderer);
Node* tableNode = table->node();
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->visitedDependentColor(CSSPropertyBackgroundColor);
unsigned validCellCount = 0;
unsigned borderedCellCount = 0;
unsigned backgroundDifferenceCellCount = 0;
Color alternatingRowColors[5];
int alternatingRowColorCount = 0;
int headersInFirstColumnCount = 0;
for (int row = 0; row < numRows; ++row) {
int headersInFirstRowCount = 0;
for (int col = 0; col < numCols; ++col) {
RenderTableCell* cell = firstBody->primaryCellAt(row, col);
if (!cell)
continue;
Node* cellNode = cell->node();
if (!cellNode)
continue;
if (cell->width() < 1 || cell->height() < 1)
continue;
validCellCount++;
HTMLTableCellElement* cellElement = static_cast<HTMLTableCellElement*>(cellNode);
bool isTHCell = cellElement->hasTagName(thTag);
if (!row && isTHCell)
headersInFirstRowCount++;
if (!col && isTHCell)
headersInFirstColumnCount++;
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->visitedDependentColor(CSSPropertyBackgroundColor);
if (table->hBorderSpacing() > 0 && table->vBorderSpacing() > 0
&& tableBGColor != cellColor && cellColor.alpha() != 1)
backgroundDifferenceCellCount++;
if (borderedCellCount >= 10 || backgroundDifferenceCellCount >= 10)
return true;
if (row < 5 && row == alternatingRowColorCount) {
RenderObject* renderRow = cell->parent();
if (!renderRow || !renderRow->isBoxModelObject() || !toRenderBoxModelObject(renderRow)->isTableRow())
continue;
RenderStyle* rowRenderStyle = renderRow->style();
if (!rowRenderStyle)
continue;
Color rowColor = rowRenderStyle->visitedDependentColor(CSSPropertyBackgroundColor);
alternatingRowColors[alternatingRowColorCount] = rowColor;
alternatingRowColorCount++;
}
}
if (!row && headersInFirstRowCount == numCols && numCols > 1)
return true;
}
if (headersInFirstColumnCount == numRows && numRows > 1)
return true;
if (validCellCount <= 1)
return false;
unsigned neededCellCount = validCellCount / 2;
if (borderedCellCount >= neededCellCount)
return true;
if (backgroundDifferenceCellCount >= neededCellCount)
return true;
if (alternatingRowColorCount > 2) {
Color firstColor = alternatingRowColors[0];
for (int k = 1; k < alternatingRowColorCount; k++) {
if (k % 2 == 1 && alternatingRowColors[k] == firstColor)
return false;
if (!(k % 2) && alternatingRowColors[k] != firstColor)
return false;
}
return true;
}
return false;
}
bool AccessibilityTable::isTableExposableThroughAccessibility() const
{
if (!m_renderer)
return false;
if (hasARIARole())
return false;
#if PLATFORM(GTK)
Node* tableNode = toRenderTable(m_renderer)->node();
return tableNode && tableNode->hasTagName(tableTag);
#endif
return isDataTable();
}
void AccessibilityTable::clearChildren()
{
AccessibilityRenderObject::clearChildren();
m_rows.clear();
m_columns.clear();
if (m_headerContainer) {
m_headerContainer->detachFromParent();
m_headerContainer = 0;
}
}
void AccessibilityTable::addChildren()
{
if (!isAccessibilityTable()) {
AccessibilityRenderObject::addChildren();
return;
}
ASSERT(!m_haveChildren);
m_haveChildren = true;
if (!m_renderer || !m_renderer->isTable())
return;
RenderTable* table = toRenderTable(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->primaryCellAt(rowIndex, colIndex);
if (!cell)
continue;
AccessibilityObject* rowObject = axCache->getOrCreate(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);
if (!row->accessibilityIsIgnored())
m_children.append(row);
#if PLATFORM(GTK)
else
m_children.append(row->children());
#endif
appendedRows.add(row);
}
}
tableSection = table->sectionBelow(tableSection, SkipEmptySections);
}
unsigned length = initialTableSection->numColumns();
for (unsigned i = 0; i < length; ++i) {
AccessibilityTableColumn* column = static_cast<AccessibilityTableColumn*>(axCache->getOrCreate(ColumnRole));
column->setColumnIndex((int)i);
column->setParent(this);
m_columns.append(column);
if (!column->accessibilityIsIgnored())
m_children.append(column);
}
AccessibilityObject* headerContainerObject = headerContainer();
if (headerContainerObject && !headerContainerObject->accessibilityIsIgnored())
m_children.append(headerContainerObject);
}
AccessibilityObject* AccessibilityTable::headerContainer()
{
if (m_headerContainer)
return m_headerContainer.get();
AccessibilityMockObject* tableHeader = toAccessibilityMockObject(axObjectCache()->getOrCreate(TableHeaderContainerRole));
tableHeader->setParent(this);
m_headerContainer = tableHeader;
return m_headerContainer.get();
}
AccessibilityObject::AccessibilityChildrenVector& AccessibilityTable::columns()
{
updateChildrenIfNecessary();
return m_columns;
}
AccessibilityObject::AccessibilityChildrenVector& AccessibilityTable::rows()
{
updateChildrenIfNecessary();
return m_rows;
}
void AccessibilityTable::rowHeaders(AccessibilityChildrenVector& headers)
{
if (!m_renderer)
return;
updateChildrenIfNecessary();
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;
updateChildrenIfNecessary();
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;
updateChildrenIfNecessary();
int numRows = m_rows.size();
for (int row = 0; row < numRows; ++row) {
AccessibilityChildrenVector rowChildren = m_rows[row]->children();
cells.append(rowChildren);
}
}
unsigned AccessibilityTable::columnCount()
{
updateChildrenIfNecessary();
return m_columns.size();
}
unsigned AccessibilityTable::rowCount()
{
updateChildrenIfNecessary();
return m_rows.size();
}
int AccessibilityTable::tableLevel() const
{
int level = 0;
for (AccessibilityObject* obj = static_cast<AccessibilityObject*>(const_cast<AccessibilityTable*>(this)); obj; obj = obj->parentObject()) {
if (obj->isAccessibilityTable())
++level;
}
return level;
}
AccessibilityTableCell* AccessibilityTable::cellForColumnAndRow(unsigned column, unsigned row)
{
if (!m_renderer || !m_renderer->isTable())
return 0;
updateChildrenIfNecessary();
RenderTable* table = toRenderTable(m_renderer);
RenderTableSection* tableSection = table->header();
if (!tableSection)
tableSection = table->firstBody();
RenderTableCell* cell = 0;
unsigned rowCount = 0;
unsigned rowOffset = 0;
while (tableSection) {
unsigned numRows = tableSection->numRows();
unsigned numCols = tableSection->numColumns();
rowCount += numRows;
unsigned sectionSpecificRow = row - rowOffset;
if (row < rowCount && column < numCols && sectionSpecificRow < numRows) {
cell = tableSection->primaryCellAt(sectionSpecificRow, column);
if (!cell) {
for (int testRow = sectionSpecificRow - 1; testRow >= 0; --testRow) {
cell = tableSection->primaryCellAt(testRow, column);
ASSERT(cell->rowSpan() >= 1);
if (cell && ((cell->rowIndex() + (cell->rowSpan() - 1)) >= sectionSpecificRow))
break;
cell = 0;
}
if (!cell) {
for (int testCol = column - 1; testCol >= 0; --testCol) {
cell = tableSection->primaryCellAt(sectionSpecificRow, testCol);
ASSERT(cell->rowSpan() >= 1);
if (cell && ((cell->col() + (cell->colSpan() - 1)) >= column))
break;
cell = 0;
}
}
}
}
if (cell)
break;
rowOffset += numRows;
if (row < rowCount)
break;
tableSection = table->sectionBelow(tableSection, SkipEmptySections);
}
if (!cell)
return 0;
AccessibilityObject* cellObject = axObjectCache()->getOrCreate(cell);
ASSERT(cellObject->isTableCell());
return static_cast<AccessibilityTableCell*>(cellObject);
}
AccessibilityRole AccessibilityTable::roleValue() const
{
if (!isAccessibilityTable())
return AccessibilityRenderObject::roleValue();
return TableRole;
}
bool AccessibilityTable::accessibilityIsIgnored() const
{
AccessibilityObjectInclusion decision = accessibilityIsIgnoredBase();
if (decision == IncludeObject)
return false;
if (decision == IgnoreObject)
return true;
if (!isAccessibilityTable())
return AccessibilityRenderObject::accessibilityIsIgnored();
return false;
}
String AccessibilityTable::title() const
{
if (!isAccessibilityTable())
return AccessibilityRenderObject::title();
String title;
if (!m_renderer)
return title;
Node* tableElement = m_renderer->node();
if (tableElement && tableElement->hasTagName(tableTag)) {
HTMLTableCaptionElement* caption = static_cast<HTMLTableElement*>(tableElement)->caption();
if (caption)
title = caption->innerText();
}
if (title.isEmpty())
title = AccessibilityRenderObject::title();
return title;
}
}