AccessibilityObject* AccessibilityTableCell::titleUIElement() const { // Try to find if the first cell in this row is a <th>. If it is, // then it can act as the title ui element. (This is only in the // case when the table is not appearing as an AXTable.) if (!m_renderer || isTableCell()) return 0; RenderTableCell* renderCell = static_cast<RenderTableCell*>(m_renderer); // If this cell is in the first column, there is no need to continue. int col = renderCell->col(); if (!col) return 0; int row = renderCell->row(); RenderTableSection* section = renderCell->section(); if (!section) return 0; RenderTableCell* headerCell = section->cellAt(row, 0).cell; if (!headerCell || headerCell == renderCell) return 0; Node* cellElement = headerCell->element(); if (!cellElement || !cellElement->hasTagName(thTag)) return 0; return axObjectCache()->get(headerCell); }
AccessibilityObject* AccessibilityTableColumn::headerObjectForSection(RenderTableSection* section, bool thTagRequired) { if (!section) return nullptr; unsigned numCols = section->numColumns(); if (m_columnIndex >= numCols) return nullptr; if (!section->numRows()) return nullptr; RenderTableCell* cell = nullptr; // also account for cells that have a span for (int testCol = m_columnIndex; testCol >= 0; --testCol) { // Run down the rows in case initial rows are invalid (like when a <caption> is used). unsigned rowCount = section->numRows(); for (unsigned testRow = 0; testRow < rowCount; testRow++) { RenderTableCell* testCell = section->primaryCellAt(testRow, testCol); // No cell at this index, keep checking more rows and columns. if (!testCell) continue; // If we've reached a cell that doesn't even overlap our column it can't be the header. if ((testCell->col() + (testCell->colSpan()-1)) < m_columnIndex) break; // If this does not have an element (like a <caption>) then check the next row if (!testCell->element()) continue; // If th is required, but we found an element that doesn't have a th tag, we can stop looking. if (thTagRequired && !testCell->element()->hasTagName(thTag)) break; cell = testCell; break; } } if (!cell) return nullptr; return axObjectCache()->getOrCreate(cell); }
AccessibilityObject* AccessibilityTableColumn::headerObjectForSection(RenderTableSection* section, bool thTagRequired) { if (!section) return 0; unsigned numCols = section->numColumns(); if (m_columnIndex >= numCols) return 0; if (!section->numRows()) return 0; RenderTableCell* cell = 0; // also account for cells that have a span for (int testCol = m_columnIndex; testCol >= 0; --testCol) { RenderTableCell* testCell = section->primaryCellAt(0, testCol); if (!testCell) continue; // we've reached a cell that doesn't even overlap our column // it can't be our header if ((testCell->col() + (testCell->colSpan()-1)) < m_columnIndex) break; if (!testCell->element()) continue; if (thTagRequired && !testCell->element()->hasTagName(thTag)) continue; cell = testCell; } if (!cell) return 0; return axObjectCache()->getOrCreate(cell); }
AccessibilityObject* AccessibilityTableCell::titleUIElement() const { // Try to find if the first cell in this row is a <th>. If it is, // then it can act as the title ui element. (This is only in the // case when the table is not appearing as an AXTable.) if (isTableCell() || !m_renderer || !m_renderer->isTableCell()) return 0; // Table cells that are th cannot have title ui elements, since by definition // they are title ui elements Node* node = m_renderer->node(); if (node && node->hasTagName(thTag)) return 0; RenderTableCell* renderCell = toRenderTableCell(m_renderer); // If this cell is in the first column, there is no need to continue. int col = renderCell->col(); if (!col) return 0; int row = renderCell->rowIndex(); RenderTableSection* section = renderCell->section(); if (!section) return 0; RenderTableCell* headerCell = section->primaryCellAt(row, 0); if (!headerCell || headerCell == renderCell) return 0; if (!headerCell->element() || !headerCell->element()->hasTagName(thTag)) return 0; return axObjectCache()->getOrCreate(headerCell); }
HTMLTableCellElement* HTMLTableCellElement::cellAbove() const { RenderObject* cellRenderer = renderer(); if (!cellRenderer) return 0; if (!cellRenderer->isTableCell()) return 0; RenderTableCell* tableCellRenderer = toRenderTableCell(cellRenderer); RenderTableCell* cellAboveRenderer = tableCellRenderer->table()->cellAbove(tableCellRenderer); if (!cellAboveRenderer) return 0; return static_cast<HTMLTableCellElement*>(cellAboveRenderer->element()); }
bool AccessibilityTable::isDataTable() const { if (!m_renderer) return false; // Do not consider it a data table is it has an ARIA role. if (hasARIARole()) return false; // When a section of the document is contentEditable, all tables should be // treated as data tables, otherwise users may not be able to work with rich // text editors that allow creating and editing tables. if (node() && node()->hasEditableStyle()) return true; // This employs a heuristic to determine if this table should appear. // Only "data" tables should be exposed as tables. // Unfortunately, there is no good way to determine the difference // between a "layout" table and a "data" table. RenderTable* table = toRenderTable(m_renderer); if (!table->element() || !isHTMLTableElement(table->element())) return false; // if there is a caption element, summary, THEAD, or TFOOT section, it's most certainly a data table HTMLTableElement* tableElement = toHTMLTableElement(table->element()); if (!tableElement->summary().isEmpty() || tableElement->tHead() || tableElement->tFoot() || tableElement->caption()) return true; // if someone used "rules" attribute than the table should appear if (!tableElement->rules().isEmpty()) return true; // if there's a colgroup or col element, it's probably a data table. for (const auto& child : childrenOfType<Element>(*tableElement)) { if (child.hasTagName(colTag) || child.hasTagName(colgroupTag)) return true; } // go through the cell's and check for tell-tale signs of "data" table status // cells have borders, or use attributes like headers, abbr, scope or axis table->recalcSectionsIfNeeded(); RenderTableSection* firstBody = table->firstBody(); if (!firstBody) return false; int numCols = firstBody->numColumns(); int numRows = firstBody->numRows(); // If there's only one cell, it's not a good AXTable candidate. if (numRows == 1 && numCols == 1) return false; // If there are at least 20 rows, we'll call it a data table. if (numRows >= 20) return true; // Store the background color of the table to check against cell's background colors. const RenderStyle& tableStyle = table->style(); Color tableBGColor = tableStyle.visitedDependentColor(CSSPropertyBackgroundColor); // check enough of the cells to find if the table matches our criteria // Criteria: // 1) must have at least one valid cell (and) // 2) at least half of cells have borders (or) // 3) at least half of cells have different bg colors than the table, and there is cell spacing 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 the first row is comprised of all <th> tags, assume it is a data table. if (!row && isTHCell) headersInFirstRowCount++; // If the first column is comprised of all <th> tags, assume it is a data table. if (!col && isTHCell) headersInFirstColumnCount++; // In this case, the developer explicitly assigned a "data" table attribute. 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 the empty-cells style is set, we'll call it a data table. if (renderStyle.emptyCells() == HIDE) return true; // If a cell has matching bordered sides, call it a (fully) bordered cell. if ((cell->borderTop() > 0 && cell->borderBottom() > 0) || (cell->borderLeft() > 0 && cell->borderRight() > 0)) borderedCellCount++; // Also keep track of each individual border, so we can catch tables where most // cells have a bottom border, for example. if (cell->borderTop() > 0) cellsWithTopBorder++; if (cell->borderBottom() > 0) cellsWithBottomBorder++; if (cell->borderLeft() > 0) cellsWithLeftBorder++; if (cell->borderRight() > 0) cellsWithRightBorder++; // If the cell has a different color from the table and there is cell spacing, // then it is probably a data table cell (spacing and colors take the place of borders). Color cellColor = renderStyle.visitedDependentColor(CSSPropertyBackgroundColor); if (table->hBorderSpacing() > 0 && table->vBorderSpacing() > 0 && tableBGColor != cellColor && cellColor.alpha() != 1) backgroundDifferenceCellCount++; // If we've found 10 "good" cells, we don't need to keep searching. if (borderedCellCount >= 10 || backgroundDifferenceCellCount >= 10) return true; // For the first 5 rows, cache the background color so we can check if this table has zebra-striped rows. 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 there is less than two valid cells, it's not a data table if (validCellCount <= 1) return false; // half of the cells had borders, it's a data table unsigned neededCellCount = validCellCount / 2; if (borderedCellCount >= neededCellCount || cellsWithTopBorder >= neededCellCount || cellsWithBottomBorder >= neededCellCount || cellsWithLeftBorder >= neededCellCount || cellsWithRightBorder >= neededCellCount) return true; // half had different background colors, it's a data table if (backgroundDifferenceCellCount >= neededCellCount) return true; // Check if there is an alternating row background color indicating a zebra striped style pattern. if (alternatingRowColorCount > 2) { Color firstColor = alternatingRowColors[0]; for (int k = 1; k < alternatingRowColorCount; k++) { // If an odd row was the same color as the first row, its not alternating. if (k % 2 == 1 && alternatingRowColors[k] == firstColor) return false; // If an even row is not the same as the first row, its not alternating. if (!(k % 2) && alternatingRowColors[k] != firstColor) return false; } return true; } return false; }
bool AccessibilityTable::isTableExposableThroughAccessibility() { // the following is a heuristic used to determine if a // <table> should be exposed as an AXTable. The goal // is to only show "data" tables if (!m_renderer || !m_renderer->isTable()) return false; // if the developer assigned an aria role to this, then we shouldn't // expose it as a table, unless, of course, the aria role is a table AccessibilityRole ariaRole = ariaRoleAttribute(); if (ariaRole == TableRole) return true; if (ariaRole != UnknownRole) return false; RenderTable* table = static_cast<RenderTable*>(m_renderer); // this employs a heuristic to determine if this table should appear. // Only "data" tables should be exposed as tables. // Unfortunately, there is no good way to determine the difference // between a "layout" table and a "data" table Node* tableNode = table->element(); if (!tableNode || !tableNode->hasTagName(tableTag)) return false; // if there is a caption element, summary, THEAD, or TFOOT section, it's most certainly a data table HTMLTableElement* tableElement = static_cast<HTMLTableElement*>(tableNode); if (!tableElement->summary().isEmpty() || tableElement->tHead() || tableElement->tFoot() || tableElement->caption()) return true; // if someone used "rules" attribute than the table should appear if (!tableElement->rules().isEmpty()) return true; // go through the cell's and check for tell-tale signs of "data" table status // cells have borders, or use attributes like headers, abbr, scope or axis RenderTableSection* firstBody = table->firstBody(); if (!firstBody) return false; int numCols = firstBody->numColumns(); int numRows = firstBody->numRows(); // if there's only one cell, it's not a good AXTable candidate if (numRows == 1 && numCols == 1) return false; // store the background color of the table to check against cell's background colors RenderStyle* tableStyle = table->style(); if (!tableStyle) return false; Color tableBGColor = tableStyle->backgroundColor(); // check enough of the cells to find if the table matches our criteria // Criteria: // 1) must have at least one valid cell (and) // 2) at least half of cells have borders (or) // 3) at least half of cells have different bg colors than the table, and there is cell spacing 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); // in this case, the developer explicitly assigned a "data" table attribute if (!cellElement->headers().isEmpty() || !cellElement->abbr().isEmpty() || !cellElement->axis().isEmpty() || !cellElement->scope().isEmpty()) return true; RenderStyle* renderStyle = cell->style(); if (!renderStyle) continue; // a cell needs to have matching bordered sides, before it can be considered a bordered cell. if ((cell->borderTop() > 0 && cell->borderBottom() > 0) || (cell->borderLeft() > 0 && cell->borderRight() > 0)) borderedCellCount++; // if the cell has a different color from the table and there is cell spacing, // then it is probably a data table cell (spacing and colors take the place of borders) Color cellColor = renderStyle->backgroundColor(); if (table->hBorderSpacing() > 0 && table->vBorderSpacing() > 0 && tableBGColor != cellColor && cellColor.alpha() != 1) backgroundDifferenceCellCount++; // if we've found 10 "good" cells, we don't need to keep searching if (borderedCellCount >= 10 || backgroundDifferenceCellCount >= 10) return true; } } // if there is less than two valid cells, it's not a data table if (validCellCount <= 1) return false; // half of the cells had borders, it's a data table unsigned neededCellCount = validCellCount / 2; if (borderedCellCount >= neededCellCount) return true; // half had different background colors, it's a data table if (backgroundDifferenceCellCount >= neededCellCount) return true; return false; }