1 /* 2 * Copyright (C) 2008 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 14 * its contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "config.h" 30 #include "core/accessibility/AccessibilityTableCell.h" 31 32 #include "core/accessibility/AXObjectCache.h" 33 #include "core/rendering/RenderTableCell.h" 34 35 using namespace std; 36 37 namespace WebCore { 38 39 using namespace HTMLNames; 40 41 AccessibilityTableCell::AccessibilityTableCell(RenderObject* renderer) 42 : AccessibilityRenderObject(renderer) 43 { 44 } 45 46 AccessibilityTableCell::~AccessibilityTableCell() 47 { 48 } 49 50 PassRefPtr<AccessibilityTableCell> AccessibilityTableCell::create(RenderObject* renderer) 51 { 52 return adoptRef(new AccessibilityTableCell(renderer)); 53 } 54 55 bool AccessibilityTableCell::computeAccessibilityIsIgnored() const 56 { 57 AccessibilityObjectInclusion decision = defaultObjectInclusion(); 58 if (decision == IncludeObject) 59 return false; 60 if (decision == IgnoreObject) 61 return true; 62 63 if (!isTableCell()) 64 return AccessibilityRenderObject::computeAccessibilityIsIgnored(); 65 66 return false; 67 } 68 69 AccessibilityObject* AccessibilityTableCell::parentTable() const 70 { 71 if (!m_renderer || !m_renderer->isTableCell()) 72 return 0; 73 74 // If the document no longer exists, we might not have an axObjectCache. 75 if (!axObjectCache()) 76 return 0; 77 78 // Do not use getOrCreate. parentTable() can be called while the render tree is being modified 79 // by javascript, and creating a table element may try to access the render tree while in a bad state. 80 // By using only get() implies that the AXTable must be created before AXTableCells. This should 81 // always be the case when AT clients access a table. 82 // https://bugs.webkit.org/show_bug.cgi?id=42652 83 return axObjectCache()->get(toRenderTableCell(m_renderer)->table()); 84 } 85 86 bool AccessibilityTableCell::isTableCell() const 87 { 88 AccessibilityObject* parent = parentObjectUnignored(); 89 if (!parent || !parent->isTableRow()) 90 return false; 91 92 return true; 93 } 94 95 AccessibilityRole AccessibilityTableCell::determineAccessibilityRole() 96 { 97 if (!isTableCell()) 98 return AccessibilityRenderObject::determineAccessibilityRole(); 99 100 return CellRole; 101 } 102 103 void AccessibilityTableCell::rowIndexRange(pair<unsigned, unsigned>& rowRange) 104 { 105 if (!m_renderer || !m_renderer->isTableCell()) 106 return; 107 108 RenderTableCell* renderCell = toRenderTableCell(m_renderer); 109 rowRange.first = renderCell->rowIndex(); 110 rowRange.second = renderCell->rowSpan(); 111 112 // since our table might have multiple sections, we have to offset our row appropriately 113 RenderTableSection* section = renderCell->section(); 114 RenderTable* table = renderCell->table(); 115 if (!table || !section) 116 return; 117 118 RenderTableSection* tableSection = table->topSection(); 119 unsigned rowOffset = 0; 120 while (tableSection) { 121 if (tableSection == section) 122 break; 123 rowOffset += tableSection->numRows(); 124 tableSection = table->sectionBelow(tableSection, SkipEmptySections); 125 } 126 127 rowRange.first += rowOffset; 128 } 129 130 void AccessibilityTableCell::columnIndexRange(pair<unsigned, unsigned>& columnRange) 131 { 132 if (!m_renderer || !m_renderer->isTableCell()) 133 return; 134 135 RenderTableCell* renderCell = toRenderTableCell(m_renderer); 136 columnRange.first = renderCell->col(); 137 columnRange.second = renderCell->colSpan(); 138 } 139 140 AccessibilityObject* AccessibilityTableCell::titleUIElement() const 141 { 142 // Try to find if the first cell in this row is a <th>. If it is, 143 // then it can act as the title ui element. (This is only in the 144 // case when the table is not appearing as an AXTable.) 145 if (isTableCell() || !m_renderer || !m_renderer->isTableCell()) 146 return 0; 147 148 // Table cells that are th cannot have title ui elements, since by definition 149 // they are title ui elements 150 Node* node = m_renderer->node(); 151 if (node && node->hasTagName(thTag)) 152 return 0; 153 154 RenderTableCell* renderCell = toRenderTableCell(m_renderer); 155 156 // If this cell is in the first column, there is no need to continue. 157 int col = renderCell->col(); 158 if (!col) 159 return 0; 160 161 int row = renderCell->rowIndex(); 162 163 RenderTableSection* section = renderCell->section(); 164 if (!section) 165 return 0; 166 167 RenderTableCell* headerCell = section->primaryCellAt(row, 0); 168 if (!headerCell || headerCell == renderCell) 169 return 0; 170 171 Node* cellElement = headerCell->node(); 172 if (!cellElement || !cellElement->hasTagName(thTag)) 173 return 0; 174 175 return axObjectCache()->getOrCreate(headerCell); 176 } 177 178 } // namespace WebCore 179