/platform/external/webkit/WebCore/accessibility/AccessibilityTableColumn.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 185 lines · 111 code · 40 blank · 34 comment · 24 complexity · 9f5ddae6017fb6a62dde0b8b2568a7c2 MD5 · raw file

  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. #include "config.h"
  29. #include "AccessibilityTableColumn.h"
  30. #include "AXObjectCache.h"
  31. #include "AccessibilityTableCell.h"
  32. #include "HTMLNames.h"
  33. #include "RenderTable.h"
  34. #include "RenderTableCell.h"
  35. #include "RenderTableSection.h"
  36. using namespace std;
  37. namespace WebCore {
  38. using namespace HTMLNames;
  39. AccessibilityTableColumn::AccessibilityTableColumn()
  40. : m_parentTable(0)
  41. {
  42. }
  43. AccessibilityTableColumn::~AccessibilityTableColumn()
  44. {
  45. }
  46. PassRefPtr<AccessibilityTableColumn> AccessibilityTableColumn::create()
  47. {
  48. return adoptRef(new AccessibilityTableColumn());
  49. }
  50. void AccessibilityTableColumn::setParentTable(AccessibilityTable* table)
  51. {
  52. m_parentTable = table;
  53. clearChildren();
  54. }
  55. IntRect AccessibilityTableColumn::elementRect() const
  56. {
  57. // this will be filled in when addChildren is called
  58. return m_columnRect;
  59. }
  60. IntSize AccessibilityTableColumn::size() const
  61. {
  62. return elementRect().size();
  63. }
  64. const AccessibilityObject::AccessibilityChildrenVector& AccessibilityTableColumn::children()
  65. {
  66. if (!m_haveChildren)
  67. addChildren();
  68. return m_children;
  69. }
  70. AccessibilityObject* AccessibilityTableColumn::headerObject()
  71. {
  72. if (!m_parentTable)
  73. return 0;
  74. RenderObject* renderer = m_parentTable->renderer();
  75. if (!renderer)
  76. return 0;
  77. if (m_parentTable->isAriaTable()) {
  78. AccessibilityChildrenVector rowChildren = children();
  79. unsigned childrenCount = rowChildren.size();
  80. for (unsigned i = 0; i < childrenCount; ++i) {
  81. AccessibilityObject* cell = rowChildren[i].get();
  82. if (cell->ariaRoleAttribute() == ColumnHeaderRole)
  83. return cell;
  84. }
  85. return 0;
  86. }
  87. if (!renderer->isTable())
  88. return 0;
  89. RenderTable* table = toRenderTable(renderer);
  90. AccessibilityObject* headerObject = 0;
  91. // try the <thead> section first. this doesn't require th tags
  92. headerObject = headerObjectForSection(table->header(), false);
  93. if (headerObject)
  94. return headerObject;
  95. // now try for <th> tags in the first body
  96. headerObject = headerObjectForSection(table->firstBody(), true);
  97. return headerObject;
  98. }
  99. AccessibilityObject* AccessibilityTableColumn::headerObjectForSection(RenderTableSection* section, bool thTagRequired)
  100. {
  101. if (!section)
  102. return 0;
  103. int numCols = section->numColumns();
  104. if (m_columnIndex >= numCols)
  105. return 0;
  106. RenderTableCell* cell = 0;
  107. // also account for cells that have a span
  108. for (int testCol = m_columnIndex; testCol >= 0; --testCol) {
  109. RenderTableCell* testCell = section->cellAt(0, testCol).cell;
  110. if (!testCell)
  111. continue;
  112. // we've reached a cell that doesn't even overlap our column
  113. // it can't be our header
  114. if ((testCell->col() + (testCell->colSpan()-1)) < m_columnIndex)
  115. break;
  116. Node* node = testCell->node();
  117. if (!node)
  118. continue;
  119. if (thTagRequired && !node->hasTagName(thTag))
  120. continue;
  121. cell = testCell;
  122. }
  123. if (!cell)
  124. return 0;
  125. return m_parentTable->axObjectCache()->getOrCreate(cell);
  126. }
  127. void AccessibilityTableColumn::addChildren()
  128. {
  129. ASSERT(!m_haveChildren);
  130. m_haveChildren = true;
  131. if (!m_parentTable)
  132. return;
  133. int numRows = m_parentTable->rowCount();
  134. for (int i = 0; i < numRows; i++) {
  135. AccessibilityTableCell* cell = m_parentTable->cellForColumnAndRow(m_columnIndex, i);
  136. if (!cell)
  137. continue;
  138. // make sure the last one isn't the same as this one (rowspan cells)
  139. if (m_children.size() > 0 && m_children.last() == cell)
  140. continue;
  141. m_children.append(cell);
  142. m_columnRect.unite(cell->elementRect());
  143. }
  144. }
  145. } // namespace WebCore