ax_table_info.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_ACCESSIBILITY_AX_TABLE_INFO_H_
  5. #define UI_ACCESSIBILITY_AX_TABLE_INFO_H_
  6. #include <map>
  7. #include <set>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "build/build_config.h"
  11. #include "ui/accessibility/ax_export.h"
  12. #include "ui/accessibility/ax_node_data.h"
  13. namespace ui {
  14. class AXTree;
  15. class AXNode;
  16. #if BUILDFLAG(IS_MAC)
  17. #define AX_EXTRA_MAC_NODES 1
  18. #endif
  19. // This helper class computes info about tables and grids in AXTrees.
  20. class AX_EXPORT AXTableInfo {
  21. public:
  22. struct CellData {
  23. AXNode* cell;
  24. AXNodeID cell_id;
  25. size_t col_index;
  26. size_t row_index;
  27. size_t col_span;
  28. size_t row_span;
  29. size_t aria_col_index;
  30. size_t aria_row_index;
  31. };
  32. // Returns nullptr if the node is not a valid table or grid node.
  33. static AXTableInfo* Create(AXTree* tree, AXNode* table_node);
  34. ~AXTableInfo();
  35. // Called automatically on Create(), but must be called again any time
  36. // the table is invalidated. Returns true if this is still a table.
  37. bool Update();
  38. // Whether the data is valid. Whenever the tree is updated in any way,
  39. // every AXTableInfo is invalidated and needs to be recomputed, just
  40. // to be safe.
  41. bool valid() const { return valid_; }
  42. void Invalidate();
  43. // The real row count, guaranteed to be at least as large as the
  44. // maximum row index of any cell.
  45. size_t row_count = 0;
  46. // The real column count, guaranteed to be at least as large as the
  47. // maximum column index of any cell.
  48. size_t col_count = 0;
  49. // List of column header nodes IDs for each column index.
  50. std::vector<std::vector<AXNodeID>> col_headers;
  51. // List of row header node IDs for each row index.
  52. std::vector<std::vector<AXNodeID>> row_headers;
  53. // All header cells.
  54. std::vector<AXNodeID> all_headers;
  55. // The id of the element with the caption tag or ARIA role.
  56. AXNodeID caption_id;
  57. // 2-D array of [row][column] -> cell node ID.
  58. // This may contain duplicates if there is a rowspan or
  59. // colspan. The entry is empty (zero) only if the cell
  60. // really is missing from the table.
  61. std::vector<std::vector<AXNodeID>> cell_ids;
  62. // Array of cell data for every unique cell in the table.
  63. std::vector<CellData> cell_data_vector;
  64. // Set of all unique cell node IDs in the table.
  65. std::vector<AXNodeID> unique_cell_ids;
  66. // Extra computed nodes for the accessibility tree for macOS:
  67. // one column node for each table column, followed by one
  68. // table header container node.
  69. std::vector<AXNode*> extra_mac_nodes;
  70. // Map from each cell's node ID to its index in unique_cell_ids.
  71. std::map<AXNodeID, size_t> cell_id_to_index;
  72. // Map from each row's node ID to its row index.
  73. std::map<AXNodeID, size_t> row_id_to_index;
  74. // List of ax nodes that represent the rows of the table.
  75. std::vector<AXNode*> row_nodes;
  76. // The ARIA row count and column count, if any ARIA table or grid
  77. // attributes are used in the table at all.
  78. int aria_row_count = 0;
  79. int aria_col_count = 0;
  80. std::string ToString() const;
  81. private:
  82. AXTableInfo(AXTree* tree, AXNode* table_node);
  83. void ClearVectors();
  84. void BuildCellDataVectorFromRowAndCellNodes(
  85. const std::vector<AXNode*>& row_node_list,
  86. const std::vector<std::vector<AXNode*>>& cell_nodes_per_row);
  87. void BuildCellAndHeaderVectorsFromCellData();
  88. void UpdateExtraMacNodes();
  89. void ClearExtraMacNodes();
  90. AXNode* CreateExtraMacColumnNode(size_t col_index);
  91. AXNode* CreateExtraMacTableHeaderNode();
  92. void UpdateExtraMacColumnNodeAttributes(size_t col_index);
  93. raw_ptr<AXTree> tree_ = nullptr;
  94. raw_ptr<AXNode> table_node_ = nullptr;
  95. bool valid_ = false;
  96. std::map<int, std::map<int, CellData>> incremental_row_col_map_;
  97. };
  98. } // namespace ui
  99. #endif // UI_ACCESSIBILITY_AX_TABLE_INFO_H_