1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "CachedTableAccessible.h"
#include "AccIterator.h"
#include "DocAccessibleParent.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/UniquePtr.h"
#include "nsAccUtils.h"
#include "nsIAccessiblePivot.h"
#include "Pivot.h"
#include "RemoteAccessible.h"
#include "TableAccessible.h"
#include "TableCellAccessible.h"
namespace mozilla::a11y {
// Used to search for table descendants relevant to table structure.
class TablePartRule : public PivotRule {
public:
virtual uint16_t Match(Accessible* aAcc) override {
role accRole = aAcc->Role();
if (accRole == roles::CAPTION || aAcc->IsTableCell()) {
return nsIAccessibleTraversalRule::FILTER_MATCH |
nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
}
if (aAcc->IsTableRow()) {
return nsIAccessibleTraversalRule::FILTER_MATCH;
}
if (aAcc->IsTable() ||
// Generic containers.
accRole == roles::TEXT || accRole == roles::TEXT_CONTAINER ||
accRole == roles::SECTION ||
// Row groups.
accRole == roles::GROUPING) {
// Walk inside these, but don't match them.
return nsIAccessibleTraversalRule::FILTER_IGNORE;
}
return nsIAccessibleTraversalRule::FILTER_IGNORE |
nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
}
};
// The Accessible* keys should only be used for lookup. They should not be
// dereferenced.
using CachedTablesMap = nsTHashMap<Accessible*, CachedTableAccessible>;
// We use a global map rather than a map in each document for three reasons:
// 1. We don't have a common base class for local and remote documents.
// 2. It avoids wasting memory in a document that doesn't have any tables.
// 3. It allows the cache management to be encapsulated here in
// CachedTableAccessible.
static StaticAutoPtr<CachedTablesMap> sCachedTables;
/* static */
CachedTableAccessible* CachedTableAccessible::GetFrom(Accessible* aAcc) {
MOZ_ASSERT(aAcc->IsTable());
if (!sCachedTables) {
sCachedTables = new CachedTablesMap();
ClearOnShutdown(&sCachedTables);
}
return &sCachedTables->LookupOrInsertWith(
aAcc, [&] { return CachedTableAccessible(aAcc); });
}
/* static */
void CachedTableAccessible::Invalidate(Accessible* aAcc) {
if (!sCachedTables) {
return;
}
if (Accessible* table = nsAccUtils::TableFor(aAcc)) {
// Destroy the instance (if any). We'll create a new one the next time it
// is requested.
sCachedTables->Remove(table);
}
}
CachedTableAccessible::CachedTableAccessible(Accessible* aAcc) : mAcc(aAcc) {
MOZ_ASSERT(mAcc);
// Build the cache. The cache can only be built once per instance. When it's
// invalidated, we just throw away the instance and create a new one when
// the cache is next needed.
int32_t rowIdx = -1;
uint32_t colIdx = 0;
// Maps a column index to the cell index of its previous implicit column
// header.
nsTHashMap<uint32_t, uint32_t> prevColHeaders;
Pivot pivot(mAcc);
TablePartRule rule;
for (Accessible* part = pivot.Next(mAcc, rule); part;
part = pivot.Next(part, rule)) {
role partRole = part->Role();
if (partRole == roles::CAPTION) {
// If there are multiple captions, use the first.
if (!mCaptionAccID) {
mCaptionAccID = part->ID();
}
continue;
}
if (part->IsTableRow()) {
++rowIdx;
colIdx = 0;
// This might be an empty row, so ensure a row here, as our row count is
// based on the length of mRowColToCellIdx.
EnsureRow(rowIdx);
continue;
}
MOZ_ASSERT(part->IsTableCell());
if (rowIdx == -1) {
// We haven't created a row yet, so this cell must be outside a row.
continue;
}
// Check for a cell spanning multiple rows which already occupies this
// position. Keep incrementing until we find a vacant position.
for (;;) {
EnsureRowCol(rowIdx, colIdx);
if (mRowColToCellIdx[rowIdx][colIdx] == kNoCellIdx) {
// This position is not occupied.
break;
}
// This position is occupied.
++colIdx;
}
// Create the cell.
uint32_t cellIdx = mCells.Length();
auto prevColHeader = prevColHeaders.MaybeGet(colIdx);
auto cell = mCells.AppendElement(
CachedTableCellAccessible(part->ID(), part, rowIdx, colIdx,
prevColHeader ? *prevColHeader : kNoCellIdx));
mAccToCellIdx.InsertOrUpdate(part, cellIdx);
// Update our row/col map.
// This cell might span multiple rows and/or columns. In that case, we need
// to occupy multiple coordinates in the row/col map.
uint32_t lastRowForCell =
static_cast<uint32_t>(rowIdx) + cell->RowExtent() - 1;
MOZ_ASSERT(lastRowForCell >= static_cast<uint32_t>(rowIdx));
uint32_t lastColForCell = colIdx + cell->ColExtent() - 1;
MOZ_ASSERT(lastColForCell >= colIdx);
for (uint32_t spannedRow = static_cast<uint32_t>(rowIdx);
spannedRow <= lastRowForCell; ++spannedRow) {
for (uint32_t spannedCol = colIdx; spannedCol <= lastColForCell;
++spannedCol) {
EnsureRowCol(spannedRow, spannedCol);
auto& rowCol = mRowColToCellIdx[spannedRow][spannedCol];
// If a cell already occupies this position, it overlaps with this one;
// e.g. r1..2c2 and r2c1..2. In that case, we want to prefer the first
// cell.
if (rowCol == kNoCellIdx) {
rowCol = cellIdx;
}
}
}
if (partRole == roles::COLUMNHEADER) {
for (uint32_t spannedCol = colIdx; spannedCol <= lastColForCell;
++spannedCol) {
prevColHeaders.InsertOrUpdate(spannedCol, cellIdx);
}
}
// Increment for the next cell.
colIdx = lastColForCell + 1;
}
}
void CachedTableAccessible::EnsureRow(uint32_t aRowIdx) {
if (mRowColToCellIdx.Length() <= aRowIdx) {
mRowColToCellIdx.AppendElements(aRowIdx - mRowColToCellIdx.Length() + 1);
}
MOZ_ASSERT(mRowColToCellIdx.Length() > aRowIdx);
}
void CachedTableAccessible::EnsureRowCol(uint32_t aRowIdx, uint32_t aColIdx) {
EnsureRow(aRowIdx);
auto& row = mRowColToCellIdx[aRowIdx];
if (mColCount <= aColIdx) {
mColCount = aColIdx + 1;
}
row.SetCapacity(mColCount);
for (uint32_t newCol = row.Length(); newCol <= aColIdx; ++newCol) {
// An entry doesn't yet exist for this column in this row.
row.AppendElement(kNoCellIdx);
}
MOZ_ASSERT(row.Length() > aColIdx);
}
Accessible* CachedTableAccessible::Caption() const {
if (mCaptionAccID) {
Accessible* caption = nsAccUtils::GetAccessibleByID(
nsAccUtils::DocumentFor(mAcc), mCaptionAccID);
MOZ_ASSERT(caption, "Dead caption Accessible!");
MOZ_ASSERT(caption->Role() == roles::CAPTION, "Caption has wrong role");
return caption;
}
return nullptr;
}
void CachedTableAccessible::Summary(nsString& aSummary) {
if (Caption()) {
// If there's a caption, we map caption to Name and summary to Description.
mAcc->Description(aSummary);
} else {
// If there's no caption, we map summary to Name.
mAcc->Name(aSummary);
}
}
Accessible* CachedTableAccessible::CellAt(uint32_t aRowIdx, uint32_t aColIdx) {
int32_t cellIdx = CellIndexAt(aRowIdx, aColIdx);
if (cellIdx == -1) {
return nullptr;
}
return mCells[cellIdx].Acc(mAcc);
}
void CachedTableAccessible::SelectCol(uint32_t aColIdx) {
if (LocalAccessible* localAcc = mAcc->AsLocal()) {
TableAccessible* table = localAcc->AsTable();
table->SelectCol(aColIdx);
}
// XXX Implement support for RemoteAccessible.
}
void CachedTableAccessible::UnselectCol(uint32_t aColIdx) {
if (LocalAccessible* localAcc = mAcc->AsLocal()) {
TableAccessible* table = localAcc->AsTable();
table->UnselectCol(aColIdx);
}
// XXX Implement support for RemoteAccessible.
}
void CachedTableAccessible::SelectRow(uint32_t aRowIdx) {
if (LocalAccessible* localAcc = mAcc->AsLocal()) {
TableAccessible* table = localAcc->AsTable();
table->SelectRow(aRowIdx);
}
// XXX Implement support for RemoteAccessible.
}
void CachedTableAccessible::UnselectRow(uint32_t aRowIdx) {
if (LocalAccessible* localAcc = mAcc->AsLocal()) {
TableAccessible* table = localAcc->AsTable();
table->UnselectRow(aRowIdx);
}
// XXX Implement support for RemoteAccessible.
}
bool CachedTableAccessible::IsProbablyLayoutTable() {
if (RemoteAccessible* remoteAcc = mAcc->AsRemote()) {
return remoteAcc->TableIsProbablyForLayout();
}
TableAccessible* localTable = mAcc->AsLocal()->AsTable();
return localTable->IsProbablyLayoutTable();
}
/* static */
CachedTableCellAccessible* CachedTableCellAccessible::GetFrom(
Accessible* aAcc) {
MOZ_ASSERT(aAcc->IsTableCell());
for (Accessible* parent = aAcc; parent; parent = parent->Parent()) {
if (auto* table =
static_cast<CachedTableAccessible*>(parent->AsTableBase())) {
if (auto cellIdx = table->mAccToCellIdx.Lookup(aAcc)) {
return &table->mCells[*cellIdx];
}
}
}
return nullptr;
}
Accessible* CachedTableCellAccessible::Acc(Accessible* aTableAcc) const {
Accessible* acc =
nsAccUtils::GetAccessibleByID(nsAccUtils::DocumentFor(aTableAcc), mAccID);
MOZ_DIAGNOSTIC_ASSERT(acc == mAcc, "Cell's cached mAcc is dead!");
return acc;
}
TableAccessibleBase* CachedTableCellAccessible::Table() const {
for (const Accessible* acc = mAcc; acc; acc = acc->Parent()) {
// Since the caller has this cell, the table is already created, so it's
// okay to ignore the const restriction here.
if (TableAccessibleBase* table =
const_cast<Accessible*>(acc)->AsTableBase()) {
return table;
}
}
return nullptr;
}
uint32_t CachedTableCellAccessible::ColExtent() const {
if (RemoteAccessible* remoteAcc = mAcc->AsRemote()) {
if (remoteAcc->mCachedFields) {
if (auto colSpan = remoteAcc->mCachedFields->GetAttribute<int32_t>(
nsGkAtoms::colspan)) {
MOZ_ASSERT(*colSpan > 0);
return *colSpan;
}
}
} else if (LocalAccessible* localAcc = mAcc->AsLocal()) {
// For HTML table cells, we must use the HTMLTableCellAccessible
// GetColExtent method rather than using the DOM attributes directly.
// This is because of things like rowspan="0" which depend on knowing
// about thead, tbody, etc., which is info we don't have in the a11y tree.
TableCellAccessible* cell = localAcc->AsTableCell();
MOZ_ASSERT(cell);
uint32_t colExtent = cell->ColExtent();
MOZ_ASSERT(colExtent > 0);
if (colExtent > 0) {
return colExtent;
}
}
return 1;
}
uint32_t CachedTableCellAccessible::RowExtent() const {
if (RemoteAccessible* remoteAcc = mAcc->AsRemote()) {
if (remoteAcc->mCachedFields) {
if (auto rowSpan = remoteAcc->mCachedFields->GetAttribute<int32_t>(
nsGkAtoms::rowspan)) {
MOZ_ASSERT(*rowSpan > 0);
return *rowSpan;
}
}
} else if (LocalAccessible* localAcc = mAcc->AsLocal()) {
// For HTML table cells, we must use the HTMLTableCellAccessible
// GetRowExtent method rather than using the DOM attributes directly.
// This is because of things like rowspan="0" which depend on knowing
// about thead, tbody, etc., which is info we don't have in the a11y tree.
TableCellAccessible* cell = localAcc->AsTableCell();
MOZ_ASSERT(cell);
uint32_t rowExtent = cell->RowExtent();
MOZ_ASSERT(rowExtent > 0);
if (rowExtent > 0) {
return rowExtent;
}
}
return 1;
}
UniquePtr<AccIterable> CachedTableCellAccessible::GetExplicitHeadersIterator() {
if (RemoteAccessible* remoteAcc = mAcc->AsRemote()) {
if (remoteAcc->mCachedFields) {
if (auto headers =
remoteAcc->mCachedFields->GetAttribute<nsTArray<uint64_t>>(
nsGkAtoms::headers)) {
return MakeUnique<RemoteAccIterator>(*headers, remoteAcc->Document());
}
}
} else if (LocalAccessible* localAcc = mAcc->AsLocal()) {
return MakeUnique<IDRefsIterator>(
localAcc->Document(), localAcc->GetContent(), nsGkAtoms::headers);
}
return nullptr;
}
void CachedTableCellAccessible::ColHeaderCells(nsTArray<Accessible*>* aCells) {
auto* table = static_cast<CachedTableAccessible*>(Table());
if (!table) {
return;
}
if (auto iter = GetExplicitHeadersIterator()) {
while (Accessible* header = iter->Next()) {
role headerRole = header->Role();
if (headerRole == roles::COLUMNHEADER) {
aCells->AppendElement(header);
} else if (headerRole != roles::ROWHEADER) {
// Treat this cell as a column header only if it's in the same column.
if (auto cellIdx = table->mAccToCellIdx.Lookup(header)) {
CachedTableCellAccessible& cell = table->mCells[*cellIdx];
if (cell.ColIdx() == ColIdx()) {
aCells->AppendElement(header);
}
}
}
}
if (!aCells->IsEmpty()) {
return;
}
}
Accessible* doc = nsAccUtils::DocumentFor(table->AsAccessible());
// Each cell stores its previous implicit column header, effectively forming a
// linked list. We traverse that to get all the headers.
CachedTableCellAccessible* cell = this;
for (;;) {
if (cell->mPrevColHeaderCellIdx == kNoCellIdx) {
break; // No more headers.
}
cell = &table->mCells[cell->mPrevColHeaderCellIdx];
Accessible* cellAcc = nsAccUtils::GetAccessibleByID(doc, cell->mAccID);
aCells->AppendElement(cellAcc);
}
}
void CachedTableCellAccessible::RowHeaderCells(nsTArray<Accessible*>* aCells) {
auto* table = static_cast<CachedTableAccessible*>(Table());
if (!table) {
return;
}
if (auto iter = GetExplicitHeadersIterator()) {
while (Accessible* header = iter->Next()) {
role headerRole = header->Role();
if (headerRole == roles::ROWHEADER) {
aCells->AppendElement(header);
} else if (headerRole != roles::COLUMNHEADER) {
// Treat this cell as a row header only if it's in the same row.
if (auto cellIdx = table->mAccToCellIdx.Lookup(header)) {
CachedTableCellAccessible& cell = table->mCells[*cellIdx];
if (cell.RowIdx() == RowIdx()) {
aCells->AppendElement(header);
}
}
}
}
if (!aCells->IsEmpty()) {
return;
}
}
Accessible* doc = nsAccUtils::DocumentFor(table->AsAccessible());
// We don't cache implicit row headers because there are usually not that many
// cells per row. Get all the row headers on the row before this cell.
uint32_t row = RowIdx();
uint32_t thisCol = ColIdx();
for (uint32_t col = thisCol - 1; col < thisCol; --col) {
int32_t cellIdx = table->CellIndexAt(row, col);
if (cellIdx == -1) {
continue;
}
CachedTableCellAccessible& cell = table->mCells[cellIdx];
Accessible* cellAcc = nsAccUtils::GetAccessibleByID(doc, cell.mAccID);
MOZ_ASSERT(cellAcc);
// cell might span multiple columns. We don't want to visit it multiple
// times, so ensure col is set to cell's starting column.
col = cell.ColIdx();
if (cellAcc->Role() != roles::ROWHEADER) {
continue;
}
aCells->AppendElement(cellAcc);
}
}
bool CachedTableCellAccessible::Selected() {
return mAcc->State() & states::SELECTED;
}
} // namespace mozilla::a11y
|