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)
, m_axColIndexFromRow(-1)
{
}
AccessibilityTableCell::~AccessibilityTableCell() = default;
Ref<AccessibilityTableCell> AccessibilityTableCell::create(RenderObject* renderer)
{
return adoptRef(*new AccessibilityTableCell(renderer));
}
bool AccessibilityTableCell::computeAccessibilityIsIgnored() const
{
AccessibilityObjectInclusion decision = defaultObjectInclusion();
if (decision == AccessibilityObjectInclusion::IncludeObject)
return false;
if (decision == AccessibilityObjectInclusion::IgnoreObject)
return true;
RenderObject* renderTable = is<RenderTableCell>(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>(renderer()))
return nullptr;
if (!axObjectCache())
return nullptr;
AccessibilityObject* parentTable = axObjectCache()->get(downcast<RenderTableCell>(*m_renderer).table());
if (!is<AccessibilityTable>(parentTable))
return nullptr;
if (!parentTable->node()) {
for (AccessibilityObject* parent = parentObject(); parent; parent = parent->parentObject()) {
if (is<AccessibilityTable>(*parent)) {
auto& parentTable = downcast<AccessibilityTable>(*parent);
if (parentTable.isExposableThroughAccessibility())
return &parentTable;
if (parentTable.node())
break;
}
}
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 == AccessibilityRole::ColumnHeader || defaultRole == AccessibilityRole::RowHeader || defaultRole == AccessibilityRole::Cell || defaultRole == AccessibilityRole::GridCell)
return defaultRole;
if (!isTableCell())
return defaultRole;
if (isColumnHeaderCell())
return AccessibilityRole::ColumnHeader;
if (isRowHeaderCell())
return AccessibilityRole::RowHeader;
return AccessibilityRole::Cell;
}
bool AccessibilityTableCell::isTableHeaderCell() const
{
return node() && node()->hasTagName(thTag);
}
bool AccessibilityTableCell::isColumnHeaderCell() const
{
const AtomString& 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 AtomString& 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 AtomString& 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 AtomString& scope = tableCell->getAttribute(scopeAttr);
if (scope == "rowgroup" && isTableCellInSameRowGroup(tableCell))
headers.append(tableCell);
else if (tableCell->isRowHeaderCell())
headers.append(tableCell);
}
}
AccessibilityTableRow* AccessibilityTableCell::parentRow() const
{
AXCoreObject* parent = parentObjectUnignored();
if (!is<AccessibilityTableRow>(*parent))
return nullptr;
return downcast<AccessibilityTableRow>(parent);
}
void AccessibilityTableCell::rowIndexRange(std::pair<unsigned, unsigned>& rowRange) const
{
if (!is<RenderTableCell>(renderer()))
return;
RenderTableCell& renderCell = downcast<RenderTableCell>(*m_renderer);
rowRange.second = axRowSpan();
if (static_cast<int>(rowRange.second) == -1)
rowRange.second = renderCell.rowSpan();
if (AccessibilityTableRow* parentRow = this->parentRow())
rowRange.first = parentRow->rowIndex();
}
void AccessibilityTableCell::columnIndexRange(std::pair<unsigned, unsigned>& columnRange) const
{
if (!is<RenderTableCell>(renderer()))
return;
const RenderTableCell& cell = downcast<RenderTableCell>(*m_renderer);
columnRange.first = cell.table()->colToEffCol(cell.col());
columnRange.second = axColumnSpan();
if (static_cast<int>(columnRange.second) != -1)
return;
columnRange.second = cell.table()->colToEffCol(cell.col() + cell.colSpan()) - columnRange.first;
}
AccessibilityObject* AccessibilityTableCell::titleUIElement() const
{
if (isTableCell() || !is<RenderTableCell>(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);
}
int AccessibilityTableCell::axColumnIndex() const
{
const AtomString& colIndexValue = getAttribute(aria_colindexAttr);
if (colIndexValue.toInt() >= 1)
return colIndexValue.toInt();
AccessibilityTableRow* parentRow = this->parentRow();
if (parentRow && m_axColIndexFromRow != -1)
return m_axColIndexFromRow;
return -1;
}
int AccessibilityTableCell::axRowIndex() const
{
const AtomString& rowIndexValue = getAttribute(aria_rowindexAttr);
if (rowIndexValue.toInt() >= 1)
return rowIndexValue.toInt();
if (AccessibilityTableRow* parentRow = this->parentRow())
return parentRow->axRowIndex();
return -1;
}
int AccessibilityTableCell::axColumnSpan() const
{
if (hasAttribute(colspanAttr))
return -1;
const AtomString& colSpanValue = getAttribute(aria_colspanAttr);
if (colSpanValue.toInt() >= 1)
return colSpanValue.toInt();
return -1;
}
int AccessibilityTableCell::axRowSpan() const
{
if (hasAttribute(rowspanAttr))
return -1;
const AtomString& rowSpanValue = getAttribute(aria_rowspanAttr);
if (rowSpanValue == "0")
return 0;
if (rowSpanValue.toInt() >= 1)
return rowSpanValue.toInt();
return -1;
}
}