1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_ACCESSIBILITY_AX_TABLE_INFO_H_
6 #define UI_ACCESSIBILITY_AX_TABLE_INFO_H_
12 #include "base/memory/raw_ptr.h"
13 #include "build/build_config.h"
14 #include "ui/accessibility/ax_export.h"
15 #include "ui/accessibility/ax_node_data.h"
23 #define AX_EXTRA_MAC_NODES 1
26 // This helper class computes info about tables and grids in AXTrees.
27 class AX_EXPORT AXTableInfo {
30 raw_ptr<AXNode, DanglingUntriaged> cell;
36 size_t aria_col_index;
37 size_t aria_row_index;
40 // Returns nullptr if the node is not a valid table or grid node.
41 static AXTableInfo* Create(AXTree* tree, AXNode* table_node);
45 // Called automatically on Create(), but must be called again any time
46 // the table is invalidated. Returns true if this is still a table.
49 // Whether the data is valid. Whenever the tree is updated in any way,
50 // every AXTableInfo is invalidated and needs to be recomputed, just
52 bool valid() const { return valid_; }
55 const AXNode* GetFirstCellInRow(const AXNode*) const;
57 // The real row count, guaranteed to be at least as large as the
58 // maximum row index of any cell.
61 // The real column count, guaranteed to be at least as large as the
62 // maximum column index of any cell.
65 // List of column header nodes IDs for each column index.
66 std::vector<std::vector<AXNodeID>> col_headers;
68 // All column header cells in a one dimensional array.
69 std::vector<AXNodeID> all_col_headers;
71 // List of row header node IDs for each row index.
72 std::vector<std::vector<AXNodeID>> row_headers;
74 // The id of the element with the caption tag or ARIA role.
77 // 2-D array of [row][column] -> cell node ID.
78 // This may contain duplicates if there is a rowspan or
79 // colspan. The entry is empty (zero) only if the cell
80 // really is missing from the table.
81 std::vector<std::vector<AXNodeID>> cell_ids;
83 // Array of cell data for every unique cell in the table.
84 std::vector<CellData> cell_data_vector;
86 // Set of all unique cell node IDs in the table.
87 std::vector<AXNodeID> unique_cell_ids;
89 // Extra computed nodes for the accessibility tree for macOS:
90 // one column node for each table column, followed by one
91 // table header container node.
92 std::vector<AXNode*> extra_mac_nodes;
94 // Map from each cell's node ID to its index in unique_cell_ids.
95 std::map<AXNodeID, size_t> cell_id_to_index;
97 // Map from each row's node ID to its row index.
98 std::map<AXNodeID, size_t> row_id_to_index;
100 // List of ax nodes that represent the rows of the table.
101 std::vector<AXNode*> row_nodes;
103 // The ARIA row count and column count, if any ARIA table or grid
104 // attributes are used in the table at all.
105 int aria_row_count = 0;
106 int aria_col_count = 0;
108 std::string ToString() const;
111 struct CellBuildState {
114 size_t current_col_index;
115 size_t current_row_index;
116 size_t spanned_col_index;
117 size_t current_aria_row_index;
118 size_t current_aria_col_index;
119 bool is_first_cell_in_row;
122 AXTableInfo(AXTree* tree, AXNode* table_node);
125 void BuildCellDataVectorFromRowAndCellNodes(
126 const std::vector<AXNode*>& row_node_list,
127 const std::vector<std::vector<AXNode*>>& cell_nodes_per_row);
128 void BuildCellDataVectorFromCellNodes(
129 const std::vector<std::vector<AXNode*>>& cell_nodes_per_row);
130 void BuildCellData(AXNode* cell,
131 AXNode* row_or_first_cell,
132 CellBuildState& state);
133 void BuildCellAndHeaderVectorsFromCellData();
134 void UpdateExtraMacNodes();
135 void ClearExtraMacNodes();
137 AXNode* CreateExtraMacColumnNode(size_t col_index);
138 AXNode* CreateExtraMacTableHeaderNode();
139 void UpdateExtraMacColumnNodeAttributes(size_t col_index);
141 raw_ptr<AXTree> tree_ = nullptr;
142 raw_ptr<AXNode> table_node_ = nullptr;
144 std::map<int, std::map<int, CellData>> incremental_row_col_map_;
149 #endif // UI_ACCESSIBILITY_AX_TABLE_INFO_H_