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 "ElementIterator.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)
, m_isAccessibilityTable(true)
{
}
AccessibilityTable::~AccessibilityTable()
{
}
void AccessibilityTable::init()
{
AccessibilityRenderObject::init();
m_isAccessibilityTable = isTableExposableThroughAccessibility();
}
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;
}
HTMLTableElement* AccessibilityTable::tableElement() const
{
if (!m_renderer->isTable())
return nullptr;
RenderTable* table = toRenderTable(m_renderer);
if (table->element() && isHTMLTableElement(table->element()))
return toHTMLTableElement(table->element());
RenderTableSection* firstBody = table->firstBody();
if (!firstBody || !firstBody->element())
return nullptr;
Element* actualTable = firstBody->element()->parentElement();
if (!actualTable || !isHTMLTableElement(actualTable))
return nullptr;
return toHTMLTableElement(actualTable);
}
bool AccessibilityTable::isDataTable() const
{
if (!m_renderer)
return false;
if (hasARIARole())
return false;
if (node() && node()->hasEditableStyle())
return true;
if (!m_renderer->isTable())
return false;
RenderTable* table = toRenderTable(m_renderer);
HTMLTableElement* tableElement = this->tableElement();
if (tableElement) {
if (!tableElement->summary().isEmpty() || tableElement->tHead() || tableElement->tFoot() || tableElement->caption())
return true;
if (!tableElement->rules().isEmpty())
return true;
for (const auto& child : childrenOfType<Element>(*tableElement)) {
if (child.hasTagName(colTag) || child.hasTagName(colgroupTag))
return true;
}
}
table->recalcSectionsIfNeeded();
RenderTableSection* firstBody = table->firstBody();
if (!firstBody)
return false;
int numCols = firstBody->numColumns();
int numRows = firstBody->numRows();
if (numRows == 1 && numCols == 1)
return false;
if (numRows >= 20)
return true;
const RenderStyle& tableStyle = table->style();
Color tableBGColor = tableStyle.visitedDependentColor(CSSPropertyBackgroundColor);
unsigned validCellCount = 0;
unsigned borderedCellCount = 0;
unsigned backgroundDifferenceCellCount = 0;
unsigned cellsWithTopBorder = 0;
unsigned cellsWithBottomBorder = 0;
unsigned cellsWithLeftBorder = 0;
unsigned cellsWithRightBorder = 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;
Element* cellElement = cell->element();
if (!cellElement)
continue;
if (cell->width() < 1 || cell->height() < 1)
continue;
validCellCount++;
bool isTHCell = cellElement->hasTagName(thTag);
if (!row && isTHCell)
headersInFirstRowCount++;
if (!col && isTHCell)
headersInFirstColumnCount++;
if (cellElement->hasTagName(tdTag) || cellElement->hasTagName(thTag)) {
HTMLTableCellElement* tableCellElement = toHTMLTableCellElement(cellElement);
if (!tableCellElement->headers().isEmpty() || !tableCellElement->abbr().isEmpty()
|| !tableCellElement->axis().isEmpty() || !tableCellElement->scope().isEmpty())
return true;
}
const RenderStyle& renderStyle = cell->style();
if (renderStyle.emptyCells() == HIDE)
return true;
if ((cell->borderTop() > 0 && cell->borderBottom() > 0)
|| (cell->borderLeft() > 0 && cell->borderRight() > 0))
borderedCellCount++;
if (cell->borderTop() > 0)
cellsWithTopBorder++;
if (cell->borderBottom() > 0)
cellsWithBottomBorder++;
if (cell->borderLeft() > 0)
cellsWithLeftBorder++;
if (cell->borderRight() > 0)
cellsWithRightBorder++;
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;
const RenderStyle& rowRenderStyle = renderRow->style();
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
|| cellsWithTopBorder >= neededCellCount
|| cellsWithBottomBorder >= neededCellCount
|| cellsWithLeftBorder >= neededCellCount
|| cellsWithRightBorder >= 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) || PLATFORM(EFL)
Element* tableNode = toRenderTable(m_renderer)->element();
return tableNode && isHTMLTableElement(tableNode);
#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);
table->recalcSectionsIfNeeded();
unsigned maxColumnCount = 0;
RenderTableSection* footer = table->footer();
for (RenderTableSection* tableSection = table->topSection(); tableSection; tableSection = table->sectionBelow(tableSection, SkipEmptySections)) {
if (tableSection == footer)
continue;
addChildrenFromSection(tableSection, maxColumnCount);
}
if (footer)
addChildrenFromSection(footer, maxColumnCount);
AXObjectCache* axCache = m_renderer->document().axObjectCache();
unsigned length = maxColumnCount;
for (unsigned i = 0; i < length; ++i) {
AccessibilityTableColumn* column = toAccessibilityTableColumn(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);
}
void AccessibilityTable::addChildrenFromSection(RenderTableSection* tableSection, unsigned& maxColumnCount)
{
ASSERT(tableSection);
if (!tableSection)
return;
AXObjectCache* axCache = m_renderer->document().axObjectCache();
HashSet<AccessibilityObject*> appendedRows;
unsigned numRows = tableSection->numRows();
for (unsigned rowIndex = 0; rowIndex < numRows; ++rowIndex) {
RenderTableRow* renderRow = tableSection->rowRendererAt(rowIndex);
if (!renderRow)
continue;
AccessibilityObject* rowObject = axCache->getOrCreate(renderRow);
if (!rowObject->isTableRow())
continue;
AccessibilityTableRow* row = toAccessibilityTableRow(rowObject);
if (appendedRows.contains(row))
continue;
row->setRowIndex(static_cast<int>(m_rows.size()));
m_rows.append(row);
if (!row->accessibilityIsIgnored())
m_children.append(row);
#if PLATFORM(GTK) || PLATFORM(EFL)
else
m_children.appendVector(row->children());
#endif
appendedRows.add(row);
}
maxColumnCount = std::max(tableSection->numColumns(), maxColumnCount);
}
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();
}
const AccessibilityObject::AccessibilityChildrenVector& AccessibilityTable::columns()
{
updateChildrenIfNecessary();
return m_columns;
}
const AccessibilityObject::AccessibilityChildrenVector& AccessibilityTable::rows()
{
updateChildrenIfNecessary();
return m_rows;
}
void AccessibilityTable::columnHeaders(AccessibilityChildrenVector& headers)
{
if (!m_renderer)
return;
updateChildrenIfNecessary();
for (const auto& column : m_columns) {
if (AccessibilityObject* header = toAccessibilityTableColumn(column.get())->headerObject())
headers.append(header);
}
}
void AccessibilityTable::rowHeaders(AccessibilityChildrenVector& headers)
{
if (!m_renderer)
return;
updateChildrenIfNecessary();
for (const auto& row : m_rows) {
if (AccessibilityObject* header = toAccessibilityTableRow(row.get())->headerObject())
headers.append(header);
}
}
void AccessibilityTable::visibleRows(AccessibilityChildrenVector& rows)
{
if (!m_renderer)
return;
updateChildrenIfNecessary();
for (const auto& row : m_rows) {
if (row && !row->isOffScreen())
rows.append(row);
}
}
void AccessibilityTable::cells(AccessibilityObject::AccessibilityChildrenVector& cells)
{
if (!m_renderer)
return;
updateChildrenIfNecessary();
for (const auto& row : m_rows)
cells.appendVector(row->children());
}
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)
{
updateChildrenIfNecessary();
if (column >= columnCount() || row >= rowCount())
return 0;
for (unsigned rowIndexCounter = row + 1; rowIndexCounter > 0; --rowIndexCounter) {
unsigned rowIndex = rowIndexCounter - 1;
const auto& children = m_rows[rowIndex]->children();
for (unsigned colIndexCounter = std::min(static_cast<unsigned>(children.size()), column + 1); colIndexCounter > 0; --colIndexCounter) {
unsigned colIndex = colIndexCounter - 1;
AccessibilityObject* child = children[colIndex].get();
ASSERT(child->isTableCell());
if (!child->isTableCell())
continue;
std::pair<unsigned, unsigned> columnRange;
std::pair<unsigned, unsigned> rowRange;
AccessibilityTableCell* tableCellChild = toAccessibilityTableCell(child);
tableCellChild->columnIndexRange(columnRange);
tableCellChild->rowIndexRange(rowRange);
if ((column >= columnRange.first && column < (columnRange.first + columnRange.second))
&& (row >= rowRange.first && row < (rowRange.first + rowRange.second)))
return tableCellChild;
}
}
return 0;
}
AccessibilityRole AccessibilityTable::roleValue() const
{
if (!isAccessibilityTable())
return AccessibilityRenderObject::roleValue();
return TableRole;
}
bool AccessibilityTable::computeAccessibilityIsIgnored() const
{
AccessibilityObjectInclusion decision = defaultObjectInclusion();
if (decision == IncludeObject)
return false;
if (decision == IgnoreObject)
return true;
if (!isAccessibilityTable())
return AccessibilityRenderObject::computeAccessibilityIsIgnored();
return false;
}
void AccessibilityTable::titleElementText(Vector<AccessibilityText>& textOrder) const
{
String title = this->title();
if (!title.isEmpty())
textOrder.append(AccessibilityText(title, LabelByElementText));
}
String AccessibilityTable::title() const
{
if (!isAccessibilityTable())
return AccessibilityRenderObject::title();
String title;
if (!m_renderer)
return title;
Node* tableElement = m_renderer->node();
if (tableElement && isHTMLTableElement(tableElement)) {
HTMLTableCaptionElement* caption = toHTMLTableElement(tableElement)->caption();
if (caption)
title = caption->innerText();
}
if (title.isEmpty())
title = AccessibilityRenderObject::title();
return title;
}
}