This source file includes following definitions.
- m_isAXTable
 
- init
 
- create
 
- hasARIARole
 
- isAXTable
 
- isDataTable
 
- isTableExposableThroughAccessibility
 
- clearChildren
 
- addChildren
 
- headerContainer
 
- columns
 
- rows
 
- columnHeaders
 
- cells
 
- columnCount
 
- rowCount
 
- tableLevel
 
- cellForColumnAndRow
 
- roleValue
 
- computeAccessibilityIsIgnored
 
- title
 
#include "config.h"
#include "core/accessibility/AXTable.h"
#include "core/accessibility/AXObjectCache.h"
#include "core/accessibility/AXTableCell.h"
#include "core/accessibility/AXTableColumn.h"
#include "core/accessibility/AXTableRow.h"
#include "core/html/HTMLTableCaptionElement.h"
#include "core/html/HTMLTableCellElement.h"
#include "core/html/HTMLTableColElement.h"
#include "core/html/HTMLTableElement.h"
#include "core/rendering/RenderTableCell.h"
namespace WebCore {
using namespace HTMLNames;
AXTable::AXTable(RenderObject* renderer)
    : AXRenderObject(renderer)
    , m_headerContainer(nullptr)
    , m_isAXTable(true)
{
}
AXTable::~AXTable()
{
}
void AXTable::init()
{
    AXRenderObject::init();
    m_isAXTable = isTableExposableThroughAccessibility();
}
PassRefPtr<AXTable> AXTable::create(RenderObject* renderer)
{
    return adoptRef(new AXTable(renderer));
}
bool AXTable::hasARIARole() const
{
    if (!m_renderer)
        return false;
    AccessibilityRole ariaRole = ariaRoleAttribute();
    if (ariaRole != UnknownRole)
        return true;
    return false;
}
bool AXTable::isAXTable() const
{
    if (!m_renderer)
        return false;
    return m_isAXTable;
}
bool AXTable::isDataTable() const
{
    if (!m_renderer)
        return false;
    
    if (hasARIARole())
        return false;
    
    
    
    if (node() && node()->rendererIsEditable())
        return true;
    
    
    
    
    RenderTable* table = toRenderTable(m_renderer);
    Node* tableNode = table->node();
    if (!isHTMLTableElement(tableNode))
        return false;
    
    HTMLTableElement* tableElement = toHTMLTableElement(tableNode);
    if (!tableElement->summary().isEmpty() || tableElement->tHead() || tableElement->tFoot() || tableElement->caption())
        return true;
    
    if (!tableElement->rules().isEmpty())
        return true;
    
    if (Traversal<HTMLTableColElement>::firstChild(*tableElement))
        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;
    
    RenderStyle* tableStyle = table->style();
    if (!tableStyle)
        return false;
    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;
            Node* cellNode = cell->node();
            if (!cellNode)
                continue;
            if (cell->width() < 1 || cell->height() < 1)
                continue;
            validCellCount++;
            bool isTHCell = cellNode->hasTagName(thTag);
            
            if (!row && isTHCell)
                headersInFirstRowCount++;
            
            if (!col && isTHCell)
                headersInFirstColumnCount++;
            
            if (isHTMLTableCellElement(*cellNode)) {
                HTMLTableCellElement& cellElement = toHTMLTableCellElement(*cellNode);
                if (!cellElement.headers().isEmpty() || !cellElement.abbr().isEmpty()
                    || !cellElement.axis().isEmpty() || !cellElement.scope().isEmpty())
                    return true;
            }
            RenderStyle* renderStyle = cell->style();
            if (!renderStyle)
                continue;
            
            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;
                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
        || 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 AXTable::isTableExposableThroughAccessibility() const
{
    
    
    
    if (!m_renderer)
        return false;
    
    
    
    if (hasARIARole())
        return false;
    return isDataTable();
}
void AXTable::clearChildren()
{
    AXRenderObject::clearChildren();
    m_rows.clear();
    m_columns.clear();
    if (m_headerContainer) {
        m_headerContainer->detachFromParent();
        m_headerContainer = nullptr;
    }
}
void AXTable::addChildren()
{
    if (!isAXTable()) {
        AXRenderObject::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();
    
    table->recalcSectionsIfNeeded();
    RenderTableSection* tableSection = table->topSection();
    if (!tableSection)
        return;
    RenderTableSection* initialTableSection = tableSection;
    while (tableSection) {
        HashSet<AXObject*> appendedRows;
        unsigned numRows = tableSection->numRows();
        for (unsigned rowIndex = 0; rowIndex < numRows; ++rowIndex) {
            RenderTableRow* renderRow = tableSection->rowRendererAt(rowIndex);
            if (!renderRow)
                continue;
            AXObject* rowObject = axCache->getOrCreate(renderRow);
            if (!rowObject->isTableRow())
                continue;
            AXTableRow* row = toAXTableRow(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);
            appendedRows.add(row);
        }
        tableSection = table->sectionBelow(tableSection, SkipEmptySections);
    }
    
    unsigned length = initialTableSection->numColumns();
    for (unsigned i = 0; i < length; ++i) {
        AXTableColumn* column = toAXTableColumn(axCache->getOrCreate(ColumnRole));
        column->setColumnIndex((int)i);
        column->setParent(this);
        m_columns.append(column);
        if (!column->accessibilityIsIgnored())
            m_children.append(column);
    }
    AXObject* headerContainerObject = headerContainer();
    if (headerContainerObject && !headerContainerObject->accessibilityIsIgnored())
        m_children.append(headerContainerObject);
}
AXObject* AXTable::headerContainer()
{
    if (m_headerContainer)
        return m_headerContainer.get();
    AXMockObject* tableHeader = toAXMockObject(axObjectCache()->getOrCreate(TableHeaderContainerRole));
    tableHeader->setParent(this);
    m_headerContainer = tableHeader;
    return m_headerContainer.get();
}
AXObject::AccessibilityChildrenVector& AXTable::columns()
{
    updateChildrenIfNecessary();
    return m_columns;
}
AXObject::AccessibilityChildrenVector& AXTable::rows()
{
    updateChildrenIfNecessary();
    return m_rows;
}
void AXTable::columnHeaders(AccessibilityChildrenVector& headers)
{
    if (!m_renderer)
        return;
    updateChildrenIfNecessary();
    unsigned colCount = m_columns.size();
    for (unsigned k = 0; k < colCount; ++k) {
        AXObject* header = toAXTableColumn(m_columns[k].get())->headerObject();
        if (!header)
            continue;
        headers.append(header);
    }
}
void AXTable::cells(AXObject::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.appendVector(rowChildren);
    }
}
unsigned AXTable::columnCount()
{
    updateChildrenIfNecessary();
    return m_columns.size();
}
unsigned AXTable::rowCount()
{
    updateChildrenIfNecessary();
    return m_rows.size();
}
int AXTable::tableLevel() const
{
    int level = 0;
    for (AXObject* obj = static_cast<AXObject*>(const_cast<AXTable*>(this)); obj; obj = obj->parentObject()) {
        if (obj->isAXTable())
            ++level;
    }
    return level;
}
AXTableCell* AXTable::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;
        AccessibilityChildrenVector children = m_rows[rowIndex]->children();
        
        
        for (unsigned colIndexCounter = std::min(static_cast<unsigned>(children.size()), column + 1); colIndexCounter > 0; --colIndexCounter) {
            unsigned colIndex = colIndexCounter - 1;
            AXObject* child = children[colIndex].get();
            ASSERT(child->isTableCell());
            if (!child->isTableCell())
                continue;
            pair<unsigned, unsigned> columnRange;
            pair<unsigned, unsigned> rowRange;
            AXTableCell* tableCellChild = toAXTableCell(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 AXTable::roleValue() const
{
    if (!isAXTable())
        return AXRenderObject::roleValue();
    return TableRole;
}
bool AXTable::computeAccessibilityIsIgnored() const
{
    AXObjectInclusion decision = defaultObjectInclusion();
    if (decision == IncludeObject)
        return false;
    if (decision == IgnoreObject)
        return true;
    if (!isAXTable())
        return AXRenderObject::computeAccessibilityIsIgnored();
    return false;
}
String AXTable::title() const
{
    if (!isAXTable())
        return AXRenderObject::title();
    String title;
    if (!m_renderer)
        return title;
    
    Node* tableElement = m_renderer->node();
    if (isHTMLTableElement(tableElement)) {
        HTMLTableCaptionElement* caption = toHTMLTableElement(tableElement)->caption();
        if (caption)
            title = caption->innerText();
    }
    
    if (title.isEmpty())
        title = AXRenderObject::title();
    return title;
}
}