summaryrefslogtreecommitdiffstats
path: root/accessible/mac/mozTableAccessible.mm
blob: 07c7e0393d92997922d988b32699a655df54290c (plain)
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/* clang-format off */
/* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* clang-format on */
/* 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/. */

#import "mozTableAccessible.h"
#import "nsCocoaUtils.h"
#import "MacUtils.h"

#include "AccIterator.h"
#include "LocalAccessible.h"
#include "mozilla/a11y/TableAccessible.h"
#include "mozilla/a11y/TableCellAccessible.h"
#include "nsAccessibilityService.h"
#include "XULTreeAccessible.h"
#include "Pivot.h"
#include "nsAccUtils.h"
#include "Relation.h"

using namespace mozilla;
using namespace mozilla::a11y;

@implementation mozColumnContainer

- (id)initWithIndex:(uint32_t)aIndex andParent:(mozAccessible*)aParent {
  self = [super init];
  mIndex = aIndex;
  mParent = aParent;
  return self;
}

- (NSString*)moxRole {
  return NSAccessibilityColumnRole;
}

- (NSString*)moxRoleDescription {
  return NSAccessibilityRoleDescription(NSAccessibilityColumnRole, nil);
}

- (mozAccessible*)moxParent {
  return mParent;
}

- (NSArray*)moxUnignoredChildren {
  if (mChildren) return mChildren;

  mChildren = [[NSMutableArray alloc] init];

  TableAccessible* table = [mParent geckoAccessible]->AsTable();
  MOZ_ASSERT(table, "Got null table when fetching column children!");
  uint32_t numRows = table->RowCount();

  for (uint32_t j = 0; j < numRows; j++) {
    Accessible* cell = table->CellAt(j, mIndex);
    mozAccessible* nativeCell = cell ? GetNativeFromGeckoAccessible(cell) : nil;
    if ([nativeCell isAccessibilityElement]) {
      [mChildren addObject:nativeCell];
    }
  }

  return mChildren;
}

- (void)dealloc {
  NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;

  [self invalidateChildren];
  [super dealloc];

  NS_OBJC_END_TRY_IGNORE_BLOCK;
}

- (void)expire {
  NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;

  [self invalidateChildren];

  mParent = nil;

  [super expire];

  NS_OBJC_END_TRY_IGNORE_BLOCK;
}

- (BOOL)isExpired {
  MOZ_ASSERT((mChildren == nil && mParent == nil) == mIsExpired);

  return [super isExpired];
}

- (void)invalidateChildren {
  NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;

  // make room for new children
  if (mChildren) {
    [mChildren release];
    mChildren = nil;
  }

  NS_OBJC_END_TRY_IGNORE_BLOCK;
}

@end

@implementation mozTablePartAccessible

- (NSString*)moxTitle {
  return @"";
}

- (NSString*)moxRole {
  return [self isLayoutTablePart] ? NSAccessibilityGroupRole : [super moxRole];
}

- (BOOL)isLayoutTablePart {
  mozAccessible* parent = (mozAccessible*)[self moxUnignoredParent];
  if ([parent isKindOfClass:[mozTablePartAccessible class]]) {
    return [(mozTablePartAccessible*)parent isLayoutTablePart];
  } else if ([parent isKindOfClass:[mozOutlineAccessible class]]) {
    return [(mozOutlineAccessible*)parent isLayoutTablePart];
  }

  return NO;
}
@end

@implementation mozTableAccessible

- (BOOL)isLayoutTablePart {
  if (mGeckoAccessible->Role() == roles::TREE_TABLE) {
    // tree tables are never layout tables, and we shouldn't
    // query IsProbablyLayoutTable() on them, so we short
    // circuit here
    return false;
  }

  // For LocalAccessible and cached RemoteAccessible, we could use
  // AsTable()->IsProbablyLayoutTable(). However, if the cache is enabled,
  // that would build the table cache, which is pointless for layout tables on
  // Mac because layout tables are AXGroups and do not expose table properties
  // like AXRows, AXColumns, etc.
  if (LocalAccessible* acc = mGeckoAccessible->AsLocal()) {
    return acc->AsTable()->IsProbablyLayoutTable();
  }
  RemoteAccessible* proxy = mGeckoAccessible->AsRemote();
  return proxy->TableIsProbablyForLayout();
}

- (void)handleAccessibleEvent:(uint32_t)eventType {
  if (eventType == nsIAccessibleEvent::EVENT_REORDER ||
      eventType == nsIAccessibleEvent::EVENT_OBJECT_ATTRIBUTE_CHANGED) {
    [self invalidateColumns];
  }

  [super handleAccessibleEvent:eventType];
}

- (void)dealloc {
  NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;

  [self invalidateColumns];
  [super dealloc];

  NS_OBJC_END_TRY_IGNORE_BLOCK;
}

- (void)expire {
  [self invalidateColumns];
  [super expire];
}

- (NSNumber*)moxRowCount {
  MOZ_ASSERT(mGeckoAccessible);

  return @(mGeckoAccessible->AsTable()->RowCount());
}

- (NSNumber*)moxColumnCount {
  MOZ_ASSERT(mGeckoAccessible);

  return @(mGeckoAccessible->AsTable()->ColCount());
}

- (NSArray*)moxRows {
  // Create a new array with the list of table rows.
  NSArray* children = [self moxChildren];
  NSMutableArray* rows = [[[NSMutableArray alloc] init] autorelease];
  for (mozAccessible* curr : children) {
    if ([curr isKindOfClass:[mozTableRowAccessible class]]) {
      [rows addObject:curr];
    } else if ([[curr moxRole] isEqualToString:@"AXGroup"]) {
      // Plain thead/tbody elements are removed from the core a11y tree and
      // replaced with their subtree, but thead/tbody elements with click
      // handlers are not -- they remain as groups. We need to expose any
      // rows they contain as rows of the parent table.
      [rows
          addObjectsFromArray:[[curr moxChildren]
                                  filteredArrayUsingPredicate:
                                      [NSPredicate predicateWithBlock:^BOOL(
                                                       mozAccessible* child,
                                                       NSDictionary* bindings) {
                                        return [child
                                            isKindOfClass:[mozTableRowAccessible
                                                              class]];
                                      }]]];
    }
  }

  return rows;
}

- (NSArray*)moxColumns {
  MOZ_ASSERT(mGeckoAccessible);

  if (mColContainers) {
    return mColContainers;
  }

  mColContainers = [[NSMutableArray alloc] init];
  uint32_t numCols = 0;

  numCols = mGeckoAccessible->AsTable()->ColCount();
  for (uint32_t i = 0; i < numCols; i++) {
    mozColumnContainer* container =
        [[mozColumnContainer alloc] initWithIndex:i andParent:self];
    [mColContainers addObject:container];
  }

  return mColContainers;
}

- (NSArray*)moxUnignoredChildren {
  if (![self isLayoutTablePart]) {
    return [[super moxUnignoredChildren]
        arrayByAddingObjectsFromArray:[self moxColumns]];
  }

  return [super moxUnignoredChildren];
}

- (NSArray*)moxColumnHeaderUIElements {
  MOZ_ASSERT(mGeckoAccessible);

  uint32_t numCols = 0;
  TableAccessible* table = nullptr;

  table = mGeckoAccessible->AsTable();
  numCols = table->ColCount();
  NSMutableArray* colHeaders =
      [[[NSMutableArray alloc] initWithCapacity:numCols] autorelease];

  for (uint32_t i = 0; i < numCols; i++) {
    Accessible* cell = table->CellAt(0, i);
    if (cell && cell->Role() == roles::COLUMNHEADER) {
      mozAccessible* colHeader = GetNativeFromGeckoAccessible(cell);
      [colHeaders addObject:colHeader];
    }
  }

  return colHeaders;
}

- (id)moxCellForColumnAndRow:(NSArray*)columnAndRow {
  if (columnAndRow == nil || [columnAndRow count] != 2) {
    return nil;
  }

  uint32_t col = [[columnAndRow objectAtIndex:0] unsignedIntValue];
  uint32_t row = [[columnAndRow objectAtIndex:1] unsignedIntValue];

  MOZ_ASSERT(mGeckoAccessible);

  Accessible* cell = mGeckoAccessible->AsTable()->CellAt(row, col);
  if (!cell) {
    return nil;
  }

  return GetNativeFromGeckoAccessible(cell);
}

- (void)invalidateColumns {
  NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
  if (mColContainers) {
    for (mozColumnContainer* col in mColContainers) {
      [col expire];
    }
    [mColContainers release];
    mColContainers = nil;
  }
  NS_OBJC_END_TRY_IGNORE_BLOCK;
}

@end

@interface mozTableRowAccessible ()
- (mozTableAccessible*)getTableParent;
@end

@implementation mozTableRowAccessible

- (mozTableAccessible*)getTableParent {
  id tableParent = static_cast<mozTableAccessible*>(
      [self moxFindAncestor:^BOOL(id curr, BOOL* stop) {
        if ([curr isKindOfClass:[mozOutlineAccessible class]]) {
          // Outline rows are a kind of table row, so it's possible
          // we're trying to call getTableParent on an outline row here.
          // Stop searching.
          *stop = YES;
        }
        return [curr isKindOfClass:[mozTableAccessible class]];
      }]);

  return [tableParent isKindOfClass:[mozTableAccessible class]] ? tableParent
                                                                : nil;
}

- (void)handleAccessibleEvent:(uint32_t)eventType {
  if (eventType == nsIAccessibleEvent::EVENT_REORDER) {
    // It is possible for getTableParent to return nil if we're
    // handling a reorder on an outilne row. Outlines don't have
    // columns, so there's nothing to do here and this will no-op.
    [[self getTableParent] invalidateColumns];
  }

  [super handleAccessibleEvent:eventType];
}

- (NSNumber*)moxIndex {
  return @([[[self getTableParent] moxRows] indexOfObjectIdenticalTo:self]);
}

@end

@implementation mozTableCellAccessible

- (NSValue*)moxRowIndexRange {
  MOZ_ASSERT(mGeckoAccessible);

  TableCellAccessible* cell = mGeckoAccessible->AsTableCell();
  return
      [NSValue valueWithRange:NSMakeRange(cell->RowIdx(), cell->RowExtent())];
}

- (NSValue*)moxColumnIndexRange {
  MOZ_ASSERT(mGeckoAccessible);

  TableCellAccessible* cell = mGeckoAccessible->AsTableCell();
  return
      [NSValue valueWithRange:NSMakeRange(cell->ColIdx(), cell->ColExtent())];
}

- (NSArray*)moxRowHeaderUIElements {
  MOZ_ASSERT(mGeckoAccessible);

  TableCellAccessible* cell = mGeckoAccessible->AsTableCell();
  AutoTArray<Accessible*, 10> headerCells;
  if (cell) {
    cell->RowHeaderCells(&headerCells);
  }
  return utils::ConvertToNSArray(headerCells);
}

- (NSArray*)moxColumnHeaderUIElements {
  MOZ_ASSERT(mGeckoAccessible);

  TableCellAccessible* cell = mGeckoAccessible->AsTableCell();
  AutoTArray<Accessible*, 10> headerCells;
  if (cell) {
    cell->ColHeaderCells(&headerCells);
  }
  return utils::ConvertToNSArray(headerCells);
}

@end

/**
 * This rule matches all accessibles with roles::OUTLINEITEM. If
 * outlines are nested, it ignores the nested subtree and returns
 * only items which are descendants of the primary outline.
 */
class OutlineRule : public PivotRule {
 public:
  uint16_t Match(Accessible* aAcc) override {
    uint16_t result = nsIAccessibleTraversalRule::FILTER_IGNORE;

    if (nsAccUtils::MustPrune(aAcc)) {
      result |= nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
    }

    if (![GetNativeFromGeckoAccessible(aAcc) isAccessibilityElement]) {
      return result;
    }

    if (aAcc->Role() == roles::OUTLINE) {
      // if the accessible is an outline, we ignore all children
      result |= nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
    } else if (aAcc->Role() == roles::OUTLINEITEM) {
      // if the accessible is not an outline item, we match here
      result |= nsIAccessibleTraversalRule::FILTER_MATCH;
    }

    return result;
  }
};

@implementation mozOutlineAccessible

- (BOOL)isLayoutTablePart {
  return NO;
}

- (NSArray*)moxRows {
  // Create a new array with the list of outline rows. We
  // use pivot here to do a deep traversal of all rows nested
  // in this outline, not just those which are direct
  // children, since that's what VO expects.
  NSMutableArray* allRows = [[[NSMutableArray alloc] init] autorelease];
  Pivot p = Pivot(mGeckoAccessible);
  OutlineRule rule = OutlineRule();
  Accessible* firstChild = mGeckoAccessible->FirstChild();
  Accessible* match = p.Next(firstChild, rule, true);
  while (match) {
    [allRows addObject:GetNativeFromGeckoAccessible(match)];
    match = p.Next(match, rule);
  }
  return allRows;
}

- (NSArray*)moxColumns {
  if (LocalAccessible* acc = mGeckoAccessible->AsLocal()) {
    if (acc->IsContent() && acc->GetContent()->IsXULElement(nsGkAtoms::tree)) {
      XULTreeAccessible* treeAcc = (XULTreeAccessible*)acc;
      NSMutableArray* cols = [[[NSMutableArray alloc] init] autorelease];
      // XUL trees store their columns in a group at the tree's first
      // child. Here, we iterate over that group to get each column's
      // native accessible and add it to our col array.
      LocalAccessible* treeColumns = treeAcc->LocalChildAt(0);
      if (treeColumns) {
        uint32_t colCount = treeColumns->ChildCount();
        for (uint32_t i = 0; i < colCount; i++) {
          LocalAccessible* treeColumnItem = treeColumns->LocalChildAt(i);
          [cols addObject:GetNativeFromGeckoAccessible(treeColumnItem)];
        }
        return cols;
      }
    }
  }
  // Webkit says we shouldn't expose any cols for aria-tree
  // so we return an empty array here
  return @[];
}

- (NSArray*)moxSelectedRows {
  NSMutableArray* selectedRows = [[[NSMutableArray alloc] init] autorelease];
  NSArray* allRows = [self moxRows];
  for (mozAccessible* row in allRows) {
    if ([row stateWithMask:states::SELECTED] != 0) {
      [selectedRows addObject:row];
    }
  }

  return selectedRows;
}

- (NSString*)moxOrientation {
  return NSAccessibilityVerticalOrientationValue;
}

@end

@implementation mozOutlineRowAccessible

- (BOOL)isLayoutTablePart {
  return NO;
}

- (NSNumber*)moxDisclosing {
  return @([self stateWithMask:states::EXPANDED] != 0);
}

- (void)moxSetDisclosing:(NSNumber*)disclosing {
  // VoiceOver requires this to be settable, but doesn't
  // require it actually affect our disclosing state.
  // We expose the attr as settable with this method
  // but do nothing to actually set it.
  return;
}

- (NSNumber*)moxExpanded {
  return @([self stateWithMask:states::EXPANDED] != 0);
}

- (id)moxDisclosedByRow {
  // According to webkit: this attr corresponds to the row
  // that contains this row. It should be the same as the
  // first parent that is a treeitem. If the parent is the tree
  // itself, this should be nil. This is tricky for xul trees because
  // all rows are direct children of the outline; they use
  // relations to expose their heirarchy structure.

  // first we check the relations to see if we're in a xul tree
  // with weird row semantics
  NSArray<mozAccessible*>* disclosingRows =
      [self getRelationsByType:RelationType::NODE_CHILD_OF];
  mozAccessible* disclosingRow = [disclosingRows firstObject];

  if (disclosingRow) {
    // if we find a row from our relation check,
    // verify it isn't the outline itself and return
    // appropriately
    if ([[disclosingRow moxRole] isEqualToString:@"AXOutline"]) {
      return nil;
    }

    return disclosingRow;
  }

  mozAccessible* parent = (mozAccessible*)[self moxUnignoredParent];
  // otherwise, its likely we're in an aria tree, so we can use
  // these role and subrole checks
  if ([[parent moxRole] isEqualToString:@"AXOutline"]) {
    return nil;
  }

  if ([[parent moxSubrole] isEqualToString:@"AXOutlineRow"]) {
    disclosingRow = parent;
  }

  return nil;
}

- (NSNumber*)moxDisclosureLevel {
  GroupPos groupPos = mGeckoAccessible->GroupPosition();

  // mac expects 0-indexed levels, but groupPos.level is 1-indexed
  // so we subtract 1 here for levels above 0
  return groupPos.level > 0 ? @(groupPos.level - 1) : @(groupPos.level);
}

- (NSArray*)moxDisclosedRows {
  // According to webkit: this attr corresponds to the rows
  // that are considered inside this row. Again, this is weird for
  // xul trees so we have to use relations first and then fall-back
  // to the children filter for non-xul outlines.

  // first we check the relations to see if we're in a xul tree
  // with weird row semantics
  if (NSArray* disclosedRows =
          [self getRelationsByType:RelationType::NODE_PARENT_OF]) {
    // if we find rows from our relation check, return them here
    return disclosedRows;
  }

  // otherwise, filter our children for outline rows
  return [[self moxChildren]
      filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(
                                                   mozAccessible* child,
                                                   NSDictionary* bindings) {
        return [child isKindOfClass:[mozOutlineRowAccessible class]];
      }]];
}

- (NSNumber*)moxIndex {
  id<MOXAccessible> outline =
      [self moxFindAncestor:^BOOL(id<MOXAccessible> moxAcc, BOOL* stop) {
        return [[moxAcc moxRole] isEqualToString:@"AXOutline"];
      }];

  NSUInteger index = [[outline moxRows] indexOfObjectIdenticalTo:self];
  return index == NSNotFound ? nil : @(index);
}

- (NSString*)moxLabel {
  nsAutoString title;
  mGeckoAccessible->Name(title);

  // XXX: When parsing outlines built with ul/lu's, we
  // include the bullet in this description even
  // though webkit doesn't. Not all outlines are built with
  // ul/lu's so we can't strip the first character here.

  return nsCocoaUtils::ToNSString(title);
}

- (int)checkedValue {
  uint64_t state = [self
      stateWithMask:(states::CHECKABLE | states::CHECKED | states::MIXED)];

  if (state & states::CHECKABLE) {
    if (state & states::CHECKED) {
      return kChecked;
    }

    if (state & states::MIXED) {
      return kMixed;
    }

    return kUnchecked;
  }

  return kUncheckable;
}

- (id)moxValue {
  int checkedValue = [self checkedValue];
  return checkedValue >= 0 ? @(checkedValue) : nil;
}

- (void)stateChanged:(uint64_t)state isEnabled:(BOOL)enabled {
  [super stateChanged:state isEnabled:enabled];

  if (state & states::EXPANDED) {
    // If the EXPANDED state is updated, fire appropriate events on the
    // outline row.
    [self moxPostNotification:(enabled
                                   ? NSAccessibilityRowExpandedNotification
                                   : NSAccessibilityRowCollapsedNotification)];
  }

  if (state & (states::CHECKED | states::CHECKABLE | states::MIXED)) {
    // If the MIXED, CHECKED or CHECKABLE state changes, update the value we
    // expose for the row, which communicates checked status.
    [self moxPostNotification:NSAccessibilityValueChangedNotification];
  }
}

@end