AccessibilityTableCell.cpp [plain text]
#include "config.h"
#include "AccessibilityTableCell.h"
#include "AXObjectCache.h"
#include "AccessibilityTable.h"
#include "AccessibilityTableRow.h"
#include "ElementIterator.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "RenderObject.h"
#include "RenderTableCell.h"
namespace WebCore {
using namespace HTMLNames;
AccessibilityTableCell::AccessibilityTableCell(RenderObject* renderer)
: AccessibilityRenderObject(renderer)
{
}
AccessibilityTableCell::~AccessibilityTableCell()
{
}
Ref<AccessibilityTableCell> AccessibilityTableCell::create(RenderObject* renderer)
{
return adoptRef(*new AccessibilityTableCell(renderer));
}
bool AccessibilityTableCell::computeAccessibilityIsIgnored() const
{
AccessibilityObjectInclusion decision = defaultObjectInclusion();
if (decision == IncludeObject)
return false;
if (decision == IgnoreObject)
return true;
RenderObject* renderTable = is<RenderTableCell>(m_renderer) ? downcast<RenderTableCell>(*m_renderer).table() : nullptr;
bool inTable = renderTable && renderTable->node() && (renderTable->node()->hasTagName(tableTag) || nodeHasRole(renderTable->node(), "grid"));
if (!node() && !inTable)
return true;
if (!isTableCell())
return AccessibilityRenderObject::computeAccessibilityIsIgnored();
return false;
}
AccessibilityTable* AccessibilityTableCell::parentTable() const
{
if (!is<RenderTableCell>(m_renderer))
return nullptr;
if (!axObjectCache())
return nullptr;
AccessibilityObject* parentTable = axObjectCache()->get(downcast<RenderTableCell>(*m_renderer).table());
if (!is<AccessibilityTable>(parentTable))
return nullptr;
return downcast<AccessibilityTable>(parentTable);
}
bool AccessibilityTableCell::isTableCell() const
{
AccessibilityObject* parentTable = this->parentTable();
return is<AccessibilityTable>(parentTable) && downcast<AccessibilityTable>(*parentTable).isExposableThroughAccessibility();
}
AccessibilityRole AccessibilityTableCell::determineAccessibilityRole()
{
AccessibilityRole defaultRole = AccessibilityRenderObject::determineAccessibilityRole();
if (defaultRole == ColumnHeaderRole || defaultRole == RowHeaderRole || defaultRole == CellRole)
return defaultRole;
if (!isTableCell())
return defaultRole;
if (isColumnHeaderCell())
return ColumnHeaderRole;
if (isRowHeaderCell())
return RowHeaderRole;
return CellRole;
}
bool AccessibilityTableCell::isTableHeaderCell() const
{
return node() && node()->hasTagName(thTag);
}
bool AccessibilityTableCell::isColumnHeaderCell() const
{
const AtomicString& scope = getAttribute(scopeAttr);
if (scope == "col" || scope == "colgroup")
return true;
if (scope == "row" || scope == "rowgroup")
return false;
if (!isTableHeaderCell())
return false;
for (Node* parentNode = node(); parentNode; parentNode = parentNode->parentNode()) {
if (parentNode->hasTagName(theadTag))
return true;
if (parentNode->hasTagName(tfootTag))
return false;
if (parentNode->hasTagName(tableTag) || parentNode->hasTagName(tbodyTag)) {
std::pair<unsigned, unsigned> rowRange;
rowIndexRange(rowRange);
if (!rowRange.first)
return true;
return false;
}
}
return false;
}
bool AccessibilityTableCell::isRowHeaderCell() const
{
const AtomicString& scope = getAttribute(scopeAttr);
if (scope == "row" || scope == "rowgroup")
return true;
if (scope == "col" || scope == "colgroup")
return false;
if (!isTableHeaderCell())
return false;
for (Node* parentNode = node(); parentNode; parentNode = parentNode->parentNode()) {
if (parentNode->hasTagName(tfootTag) || parentNode->hasTagName(tbodyTag) || parentNode->hasTagName(tableTag)) {
std::pair<unsigned, unsigned> colRange;
columnIndexRange(colRange);
if (!colRange.first)
return true;
return false;
}
if (parentNode->hasTagName(theadTag))
return false;
}
return false;
}
bool AccessibilityTableCell::isTableCellInSameRowGroup(AccessibilityTableCell* otherTableCell)
{
Node* parentNode = node();
for ( ; parentNode; parentNode = parentNode->parentNode()) {
if (parentNode->hasTagName(theadTag) || parentNode->hasTagName(tbodyTag) || parentNode->hasTagName(tfootTag))
break;
}
Node* otherParentNode = otherTableCell->node();
for ( ; otherParentNode; otherParentNode = otherParentNode->parentNode()) {
if (otherParentNode->hasTagName(theadTag) || otherParentNode->hasTagName(tbodyTag) || otherParentNode->hasTagName(tfootTag))
break;
}
return otherParentNode == parentNode;
}
bool AccessibilityTableCell::isTableCellInSameColGroup(AccessibilityTableCell* tableCell)
{
std::pair<unsigned, unsigned> colRange;
columnIndexRange(colRange);
std::pair<unsigned, unsigned> otherColRange;
tableCell->columnIndexRange(otherColRange);
if (colRange.first <= (otherColRange.first + otherColRange.second))
return true;
return false;
}
String AccessibilityTableCell::expandedTextValue() const
{
return getAttribute(abbrAttr);
}
bool AccessibilityTableCell::supportsExpandedTextValue() const
{
return isTableHeaderCell() && hasAttribute(abbrAttr);
}
void AccessibilityTableCell::columnHeaders(AccessibilityChildrenVector& headers)
{
AccessibilityTable* parent = parentTable();
if (!parent)
return;
ariaElementsFromAttribute(headers, headersAttr);
if (!headers.isEmpty())
return;
std::pair<unsigned, unsigned> rowRange;
rowIndexRange(rowRange);
std::pair<unsigned, unsigned> colRange;
columnIndexRange(colRange);
for (unsigned row = 0; row < rowRange.first; row++) {
AccessibilityTableCell* tableCell = parent->cellForColumnAndRow(colRange.first, row);
if (!tableCell || tableCell == this || headers.contains(tableCell))
continue;
std::pair<unsigned, unsigned> childRowRange;
tableCell->rowIndexRange(childRowRange);
const AtomicString& scope = tableCell->getAttribute(scopeAttr);
if (scope == "colgroup" && isTableCellInSameColGroup(tableCell))
headers.append(tableCell);
else if (tableCell->isColumnHeaderCell())
headers.append(tableCell);
}
}
void AccessibilityTableCell::rowHeaders(AccessibilityChildrenVector& headers)
{
AccessibilityTable* parent = parentTable();
if (!parent)
return;
std::pair<unsigned, unsigned> rowRange;
rowIndexRange(rowRange);
std::pair<unsigned, unsigned> colRange;
columnIndexRange(colRange);
for (unsigned column = 0; column < colRange.first; column++) {
AccessibilityTableCell* tableCell = parent->cellForColumnAndRow(column, rowRange.first);
if (!tableCell || tableCell == this || headers.contains(tableCell))
continue;
const AtomicString& scope = tableCell->getAttribute(scopeAttr);
if (scope == "rowgroup" && isTableCellInSameRowGroup(tableCell))
headers.append(tableCell);
else if (tableCell->isRowHeaderCell())
headers.append(tableCell);
}
}
void AccessibilityTableCell::rowIndexRange(std::pair<unsigned, unsigned>& rowRange) const
{
if (!is<RenderTableCell>(m_renderer))
return;
RenderTableCell& renderCell = downcast<RenderTableCell>(*m_renderer);
rowRange.first = renderCell.rowIndex();
rowRange.second = renderCell.rowSpan();
RenderTableSection* section = renderCell.section();
RenderTable* table = renderCell.table();
if (!table || !section)
return;
RenderTableSection* footerSection = table->footer();
unsigned rowOffset = 0;
for (RenderTableSection* tableSection = table->topSection(); tableSection; tableSection = table->sectionBelow(tableSection, SkipEmptySections)) {
if (tableSection == footerSection)
continue;
if (tableSection == section)
break;
rowOffset += tableSection->numRows();
}
rowRange.first += rowOffset;
}
void AccessibilityTableCell::columnIndexRange(std::pair<unsigned, unsigned>& columnRange) const
{
if (!is<RenderTableCell>(m_renderer))
return;
const RenderTableCell& cell = downcast<RenderTableCell>(*m_renderer);
columnRange.first = cell.table()->colToEffCol(cell.col());
columnRange.second = cell.table()->colToEffCol(cell.col() + cell.colSpan()) - columnRange.first;
}
AccessibilityObject* AccessibilityTableCell::titleUIElement() const
{
if (isTableCell() || !is<RenderTableCell>(m_renderer))
return nullptr;
Node* node = m_renderer->node();
if (node && node->hasTagName(thTag))
return nullptr;
RenderTableCell& renderCell = downcast<RenderTableCell>(*m_renderer);
int col = renderCell.col();
if (!col)
return nullptr;
int row = renderCell.rowIndex();
RenderTableSection* section = renderCell.section();
if (!section)
return nullptr;
RenderTableCell* headerCell = section->primaryCellAt(row, 0);
if (!headerCell || headerCell == &renderCell)
return nullptr;
if (!headerCell->element() || !headerCell->element()->hasTagName(thTag))
return nullptr;
return axObjectCache()->getOrCreate(headerCell);
}
}