-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
ax_platform_node_cocoa.mm
2697 lines (2325 loc) · 87.9 KB
/
ax_platform_node_cocoa.mm
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ui/accessibility/platform/ax_platform_node_cocoa.h"
#import <Cocoa/Cocoa.h>
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/mac_util.h"
#include "base/no_destructor.h"
#include "base/strings/sys_string_conversions.h"
#include "base/trace_event/trace_event.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_range.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/accessibility/platform/ax_platform_node_mac.h"
#include "ui/accessibility/platform/ax_private_attributes_mac.h"
#include "ui/accessibility/platform/ax_private_roles_mac.h"
#include "ui/accessibility/platform/ax_utils_mac.h"
#include "ui/base/l10n/l10n_util.h"
#import "ui/gfx/mac/coordinate_conversion.h"
#include "ui/strings/grit/ax_strings.h"
using AXRange = ui::AXPlatformNodeDelegate::AXRange;
namespace ui {
AXAnnouncementSpec::AXAnnouncementSpec() = default;
AXAnnouncementSpec::~AXAnnouncementSpec() = default;
} // namespace ui
namespace {
// Same length as web content/WebKit.
static int kLiveRegionDebounceMillis = 20;
using RoleMap = std::map<ax::mojom::Role, NSString*>;
using EventMap = std::map<ax::mojom::Event, NSString*>;
using ActionList = std::vector<std::pair<ax::mojom::Action, NSString*>>;
RoleMap BuildSubroleMap() {
const RoleMap::value_type subroles[] = {
{ax::mojom::Role::kAlert, @"AXApplicationAlert"},
{ax::mojom::Role::kAlertDialog, @"AXApplicationAlertDialog"},
{ax::mojom::Role::kApplication, @"AXLandmarkApplication"},
{ax::mojom::Role::kArticle, @"AXDocumentArticle"},
{ax::mojom::Role::kBanner, @"AXLandmarkBanner"},
{ax::mojom::Role::kCode, @"AXCodeStyleGroup"},
{ax::mojom::Role::kComplementary, @"AXLandmarkComplementary"},
{ax::mojom::Role::kContentDeletion, @"AXDeleteStyleGroup"},
{ax::mojom::Role::kContentInsertion, @"AXInsertStyleGroup"},
{ax::mojom::Role::kContentInfo, @"AXLandmarkContentInfo"},
{ax::mojom::Role::kDefinition, @"AXDefinition"},
{ax::mojom::Role::kDescriptionListDetail, @"AXDefinition"},
{ax::mojom::Role::kDescriptionListTerm, @"AXTerm"},
{ax::mojom::Role::kDialog, @"AXApplicationDialog"},
{ax::mojom::Role::kDocument, @"AXDocument"},
{ax::mojom::Role::kEmphasis, @"AXEmphasisStyleGroup"},
{ax::mojom::Role::kFeed, @"AXApplicationGroup"},
{ax::mojom::Role::kFooter, @"AXLandmarkContentInfo"},
{ax::mojom::Role::kForm, @"AXLandmarkForm"},
{ax::mojom::Role::kGraphicsDocument, @"AXDocument"},
{ax::mojom::Role::kGroup, @"AXApplicationGroup"},
{ax::mojom::Role::kHeader, @"AXLandmarkBanner"},
{ax::mojom::Role::kLog, @"AXApplicationLog"},
{ax::mojom::Role::kMain, @"AXLandmarkMain"},
{ax::mojom::Role::kMarquee, @"AXApplicationMarquee"},
// https://w3c.github.io/mathml-aam/#mathml-element-mappings
{ax::mojom::Role::kMath, @"AXDocumentMath"},
{ax::mojom::Role::kMathMLFraction, @"AXMathFraction"},
{ax::mojom::Role::kMathMLIdentifier, @"AXMathIdentifier"},
{ax::mojom::Role::kMathMLMath, @"AXDocumentMath"},
{ax::mojom::Role::kMathMLMultiscripts, @"AXMathMultiscript"},
{ax::mojom::Role::kMathMLNoneScript, @"AXMathRow"},
{ax::mojom::Role::kMathMLNumber, @"AXMathNumber"},
{ax::mojom::Role::kMathMLOperator, @"AXMathOperator"},
{ax::mojom::Role::kMathMLOver, @"AXMathUnderOver"},
{ax::mojom::Role::kMathMLPrescriptDelimiter, @"AXMathRow"},
{ax::mojom::Role::kMathMLRoot, @"AXMathRoot"},
{ax::mojom::Role::kMathMLRow, @"AXMathRow"},
{ax::mojom::Role::kMathMLSquareRoot, @"AXMathSquareRoot"},
{ax::mojom::Role::kMathMLSub, @"AXMathSubscriptSuperscript"},
{ax::mojom::Role::kMathMLSubSup, @"AXMathSubscriptSuperscript"},
{ax::mojom::Role::kMathMLSup, @"AXMathSubscriptSuperscript"},
{ax::mojom::Role::kMathMLTable, @"AXMathTable"},
{ax::mojom::Role::kMathMLTableCell, @"AXMathTableCell"},
{ax::mojom::Role::kMathMLTableRow, @"AXMathTableRow"},
{ax::mojom::Role::kMathMLText, @"AXMathText"},
{ax::mojom::Role::kMathMLUnder, @"AXMathUnderOver"},
{ax::mojom::Role::kMathMLUnderOver, @"AXMathUnderOver"},
{ax::mojom::Role::kNavigation, @"AXLandmarkNavigation"},
{ax::mojom::Role::kNote, @"AXDocumentNote"},
{ax::mojom::Role::kRegion, @"AXLandmarkRegion"},
{ax::mojom::Role::kSearch, @"AXLandmarkSearch"},
{ax::mojom::Role::kSearchBox, @"AXSearchField"},
{ax::mojom::Role::kStatus, @"AXApplicationStatus"},
{ax::mojom::Role::kStrong, @"AXStrongStyleGroup"},
{ax::mojom::Role::kSubscript, @"AXSubscriptStyleGroup"},
{ax::mojom::Role::kSuperscript, @"AXSuperscriptStyleGroup"},
{ax::mojom::Role::kSwitch, @"AXSwitch"},
{ax::mojom::Role::kTab, @"AXTabButton"},
{ax::mojom::Role::kTabPanel, @"AXTabPanel"},
{ax::mojom::Role::kTerm, @"AXTerm"},
{ax::mojom::Role::kTime, @"AXTimeGroup"},
{ax::mojom::Role::kTimer, @"AXApplicationTimer"},
{ax::mojom::Role::kToggleButton, @"AXToggleButton"},
{ax::mojom::Role::kTooltip, @"AXUserInterfaceTooltip"},
{ax::mojom::Role::kTreeItem, NSAccessibilityOutlineRowSubrole},
};
return RoleMap(begin(subroles), end(subroles));
}
EventMap BuildEventMap() {
const EventMap::value_type events[] = {
{ax::mojom::Event::kCheckedStateChanged,
NSAccessibilityValueChangedNotification},
{ax::mojom::Event::kFocus,
NSAccessibilityFocusedUIElementChangedNotification},
{ax::mojom::Event::kFocusContext,
NSAccessibilityFocusedUIElementChangedNotification},
// Do not map kMenuStart/End to the Mac's opened/closed notifications.
// kMenuStart/End are fired at the start/end of menu interaction on the
// container of the menu; not the menu itself. All newly-opened/closed
// menus should fire kMenuPopupStart/End. See SubmenuView::ShowAt and
// SubmenuView::Hide.
{ax::mojom::Event::kMenuPopupStart, (NSString*)kAXMenuOpenedNotification},
{ax::mojom::Event::kMenuPopupEnd, (NSString*)kAXMenuClosedNotification},
{ax::mojom::Event::kTextChanged, NSAccessibilityTitleChangedNotification},
{ax::mojom::Event::kValueChanged,
NSAccessibilityValueChangedNotification},
{ax::mojom::Event::kTextSelectionChanged,
NSAccessibilitySelectedTextChangedNotification},
// TODO(patricialor): Add more events.
};
return EventMap(begin(events), end(events));
}
ActionList BuildActionList() {
const ActionList::value_type entries[] = {
// NSAccessibilityPressAction must come first in this list.
{ax::mojom::Action::kDoDefault, NSAccessibilityPressAction},
{ax::mojom::Action::kDecrement, NSAccessibilityDecrementAction},
{ax::mojom::Action::kIncrement, NSAccessibilityIncrementAction},
{ax::mojom::Action::kShowContextMenu, NSAccessibilityShowMenuAction},
};
return ActionList(begin(entries), end(entries));
}
const ActionList& GetActionList() {
static const base::NoDestructor<ActionList> action_map(BuildActionList());
return *action_map;
}
void PostAnnouncementNotification(NSString* announcement,
NSWindow* window,
bool is_polite) {
NSAccessibilityPriorityLevel priority =
is_polite ? NSAccessibilityPriorityMedium : NSAccessibilityPriorityHigh;
NSDictionary* notification_info = @{
NSAccessibilityAnnouncementKey : announcement,
NSAccessibilityPriorityKey : @(priority)
};
// On Mojave, announcements from an inactive window aren't spoken.
NSAccessibilityPostNotificationWithUserInfo(
window, NSAccessibilityAnnouncementRequestedNotification,
notification_info);
}
// Returns true if |action| should be added implicitly for |data|.
bool HasImplicitAction(const ui::AXPlatformNodeBase& node,
ax::mojom::Action action) {
return action == ax::mojom::Action::kDoDefault &&
node.GetData().IsClickable();
}
// For roles that show a menu for the default action, ensure "show menu" also
// appears in available actions, but only if that's not already used for a
// context menu. It will be mapped back to the default action when performed.
bool AlsoUseShowMenuActionForDefaultAction(const ui::AXPlatformNodeBase& node) {
return HasImplicitAction(node, ax::mojom::Action::kDoDefault) &&
!node.HasAction(ax::mojom::Action::kShowContextMenu) &&
(node.GetRole() == ax::mojom::Role::kPopUpButton ||
node.GetRole() == ax::mojom::Role::kComboBoxSelect);
}
// Check whether |selector| is an accessibility setter. This is a heuristic but
// seems to be a pretty good one.
bool IsAXSetter(SEL selector) {
return [NSStringFromSelector(selector) hasPrefix:@"setAccessibility"];
}
void CollectAncestorRoles(
const ui::AXNode& node,
std::map<ui::AXNodeID, std::set<ax::mojom::Role>>& out_ancestor_roles) {
if (out_ancestor_roles.contains(node.id()))
return;
out_ancestor_roles[node.id()] = {node.GetRole()};
if (!node.GetParent())
return;
CollectAncestorRoles(*node.GetParent(), out_ancestor_roles);
out_ancestor_roles[node.id()].insert(
out_ancestor_roles[node.GetParent()->id()].begin(),
out_ancestor_roles[node.GetParent()->id()].end());
}
} // namespace
@interface AXPlatformNodeCocoa (Private)
// Helper function for string attributes that don't require extra processing.
- (NSString*)getStringAttribute:(ax::mojom::StringAttribute)attribute;
// Returns AXValue, or nil if AXValue isn't an NSString.
- (NSString*)getAXValueAsString;
// Returns the native wrapper for the given node id.
- (AXPlatformNodeCocoa*)fromNodeID:(ui::AXNodeID)id;
// Returns true if this object is an image.
- (BOOL)isImage;
@end
@implementation AXPlatformNodeCocoa {
ui::AXPlatformNodeBase* _node; // Weak. Retains us.
std::unique_ptr<ui::AXAnnouncementSpec> _pendingAnnouncement;
}
@synthesize node = _node;
// Required for AXCustomContentProvider, which defines the property.
@synthesize accessibilityCustomContent = _accessibilityCustomContent;
- (ui::AXPlatformNodeDelegate*)nodeDelegate {
return _node ? _node->GetDelegate() : nil;
}
- (BOOL)instanceActive {
return _node != nullptr;
}
- (BOOL)isIncludedInPlatformTree {
// TODO(accessibility): Do we really need to have invisible objects in
// the platform tree?
return [self instanceActive] &&
![[self AXRole] isEqualToString:NSAccessibilityUnknownRole] &&
!_node->IsInvisibleOrIgnored();
}
- (id)titleUIElement {
// True only if it's a control, if there's a single label, and the label has
// nonempty text.
// VoiceOver ignores TitleUIElement if the element isn't a control.
if (!ui::IsControl(_node->GetRole()))
return nil;
if (!_node->HasNameFromOtherElement())
return nil;
std::vector<int32_t> labelledby_ids =
_node->GetIntListAttribute(ax::mojom::IntListAttribute::kLabelledbyIds);
if (labelledby_ids.size() != 1)
return nil;
ui::AXPlatformNode* label =
_node->GetDelegate()->GetFromNodeID(labelledby_ids[0]);
if (!label)
return nil;
// No title UI element if the label's name is empty.
std::string labelName = label->GetDelegate()->GetName();
if (labelName.empty())
return nil;
return label->GetNativeViewAccessible();
}
- (BOOL)isNameFromLabel {
// Image annotations are not visible text, so they should be exposed
// as a description and not a title.
switch (_node->GetData().GetImageAnnotationStatus()) {
case ax::mojom::ImageAnnotationStatus::kEligibleForAnnotation:
case ax::mojom::ImageAnnotationStatus::kAnnotationPending:
case ax::mojom::ImageAnnotationStatus::kAnnotationEmpty:
case ax::mojom::ImageAnnotationStatus::kAnnotationAdult:
case ax::mojom::ImageAnnotationStatus::kAnnotationProcessFailed:
case ax::mojom::ImageAnnotationStatus::kAnnotationSucceeded:
return true;
case ax::mojom::ImageAnnotationStatus::kNone:
case ax::mojom::ImageAnnotationStatus::kWillNotAnnotateDueToScheme:
case ax::mojom::ImageAnnotationStatus::kIneligibleForAnnotation:
case ax::mojom::ImageAnnotationStatus::kSilentlyEligibleForAnnotation:
break;
}
// No label for windows or native dialogs.
ax::mojom::Role role = _node->GetRole();
if (ui::IsWindow(role) ||
(ui::IsDialog(role) && !_node->GetDelegate()->IsWebContent()))
return false;
// VoiceOver computes the wrong description for a link.
if (ui::IsLink(role))
return true;
// VoiceOver will not read the label of these roles unless it is
// exposed in the description instead of the title.
switch (role) {
case ax::mojom::Role::kGenericContainer:
case ax::mojom::Role::kGroup:
case ax::mojom::Role::kRadioGroup:
case ax::mojom::Role::kTabPanel:
return true;
default:
break;
}
// On Mac OS X, the accessible name of an object is exposed as its
// title if it comes from visible text, and as its description
// otherwise, but never both.
// Note: a placeholder is often visible text, but since it aids in data entry
// it is similar to accessibilityValue, and thus cannot be exposed either in
// accessibilityTitle or in accessibilityLabel.
ax::mojom::NameFrom nameFrom = _node->GetNameFrom();
if (nameFrom == ax::mojom::NameFrom::kCaption ||
nameFrom == ax::mojom::NameFrom::kContents ||
nameFrom == ax::mojom::NameFrom::kPlaceholder ||
nameFrom == ax::mojom::NameFrom::kRelatedElement ||
nameFrom == ax::mojom::NameFrom::kValue) {
return false;
}
return true;
}
+ (NSString*)nativeRoleFromAXRole:(ax::mojom::Role)role {
switch (role) {
case ax::mojom::Role::kAbbr:
case ax::mojom::Role::kAlert:
case ax::mojom::Role::kAlertDialog:
case ax::mojom::Role::kApplication:
case ax::mojom::Role::kArticle:
case ax::mojom::Role::kAudio:
case ax::mojom::Role::kBanner:
case ax::mojom::Role::kBlockquote:
case ax::mojom::Role::kCaption:
case ax::mojom::Role::kClient:
case ax::mojom::Role::kCode:
case ax::mojom::Role::kComment:
case ax::mojom::Role::kComplementary:
case ax::mojom::Role::kContentDeletion:
case ax::mojom::Role::kContentInsertion:
case ax::mojom::Role::kContentInfo:
case ax::mojom::Role::kDefinition:
case ax::mojom::Role::kDescriptionListDetail:
case ax::mojom::Role::kDescriptionListTerm:
case ax::mojom::Role::kDesktop:
case ax::mojom::Role::kDialog:
case ax::mojom::Role::kDetails:
case ax::mojom::Role::kDocAbstract:
case ax::mojom::Role::kDocAcknowledgments:
case ax::mojom::Role::kDocAfterword:
case ax::mojom::Role::kDocAppendix:
case ax::mojom::Role::kDocBiblioEntry:
case ax::mojom::Role::kDocBibliography:
case ax::mojom::Role::kDocChapter:
case ax::mojom::Role::kDocColophon:
case ax::mojom::Role::kDocConclusion:
case ax::mojom::Role::kDocCredit:
case ax::mojom::Role::kDocCredits:
case ax::mojom::Role::kDocDedication:
case ax::mojom::Role::kDocEndnote:
case ax::mojom::Role::kDocEndnotes:
case ax::mojom::Role::kDocEpigraph:
case ax::mojom::Role::kDocEpilogue:
case ax::mojom::Role::kDocErrata:
case ax::mojom::Role::kDocExample:
case ax::mojom::Role::kDocFootnote:
case ax::mojom::Role::kDocForeword:
case ax::mojom::Role::kDocGlossary:
case ax::mojom::Role::kDocIndex:
case ax::mojom::Role::kDocIntroduction:
case ax::mojom::Role::kDocNotice:
case ax::mojom::Role::kDocPageFooter:
case ax::mojom::Role::kDocPageHeader:
case ax::mojom::Role::kDocPageList:
case ax::mojom::Role::kDocPart:
case ax::mojom::Role::kDocPreface:
case ax::mojom::Role::kDocPrologue:
case ax::mojom::Role::kDocPullquote:
case ax::mojom::Role::kDocQna:
case ax::mojom::Role::kDocTip:
case ax::mojom::Role::kDocToc:
case ax::mojom::Role::kDocument:
case ax::mojom::Role::kEmbeddedObject:
case ax::mojom::Role::kEmphasis:
case ax::mojom::Role::kFeed:
case ax::mojom::Role::kFigcaption:
case ax::mojom::Role::kFigure:
case ax::mojom::Role::kFooter:
case ax::mojom::Role::kFooterAsNonLandmark:
case ax::mojom::Role::kForm:
case ax::mojom::Role::kGenericContainer:
case ax::mojom::Role::kGraphicsDocument:
case ax::mojom::Role::kGraphicsObject:
case ax::mojom::Role::kGroup:
case ax::mojom::Role::kHeader:
case ax::mojom::Role::kHeaderAsNonLandmark:
case ax::mojom::Role::kIframe:
case ax::mojom::Role::kIframePresentational:
case ax::mojom::Role::kLabelText:
case ax::mojom::Role::kLayoutTable:
case ax::mojom::Role::kLayoutTableCell:
case ax::mojom::Role::kLayoutTableRow:
case ax::mojom::Role::kLegend:
case ax::mojom::Role::kLineBreak:
case ax::mojom::Role::kListItem:
case ax::mojom::Role::kLog:
case ax::mojom::Role::kMain:
case ax::mojom::Role::kMark:
case ax::mojom::Role::kMarquee:
case ax::mojom::Role::kMath:
case ax::mojom::Role::kMathMLFraction:
case ax::mojom::Role::kMathMLIdentifier:
case ax::mojom::Role::kMathMLMath:
case ax::mojom::Role::kMathMLMultiscripts:
case ax::mojom::Role::kMathMLNoneScript:
case ax::mojom::Role::kMathMLNumber:
case ax::mojom::Role::kMathMLOperator:
case ax::mojom::Role::kMathMLOver:
case ax::mojom::Role::kMathMLPrescriptDelimiter:
case ax::mojom::Role::kMathMLRoot:
case ax::mojom::Role::kMathMLRow:
case ax::mojom::Role::kMathMLSquareRoot:
case ax::mojom::Role::kMathMLStringLiteral:
case ax::mojom::Role::kMathMLSub:
case ax::mojom::Role::kMathMLSubSup:
case ax::mojom::Role::kMathMLSup:
case ax::mojom::Role::kMathMLTable:
case ax::mojom::Role::kMathMLTableCell:
case ax::mojom::Role::kMathMLTableRow:
case ax::mojom::Role::kMathMLText:
case ax::mojom::Role::kMathMLUnder:
case ax::mojom::Role::kMathMLUnderOver:
case ax::mojom::Role::kNavigation:
case ax::mojom::Role::kNone:
case ax::mojom::Role::kNote:
case ax::mojom::Role::kPane:
case ax::mojom::Role::kParagraph:
case ax::mojom::Role::kPdfRoot:
case ax::mojom::Role::kPluginObject:
case ax::mojom::Role::kPre:
case ax::mojom::Role::kRegion:
case ax::mojom::Role::kRowGroup:
case ax::mojom::Role::kRuby:
case ax::mojom::Role::kSearch:
case ax::mojom::Role::kSection:
case ax::mojom::Role::kStatus:
case ax::mojom::Role::kSubscript:
case ax::mojom::Role::kSuggestion:
case ax::mojom::Role::kSuperscript:
return NSAccessibilityGroupRole;
case ax::mojom::Role::kSvgRoot:
return NSAccessibilityImageRole;
case ax::mojom::Role::kStrong:
case ax::mojom::Role::kTableHeaderContainer:
case ax::mojom::Role::kTabPanel:
case ax::mojom::Role::kTerm:
case ax::mojom::Role::kTime:
case ax::mojom::Role::kTimer:
case ax::mojom::Role::kTooltip:
case ax::mojom::Role::kVideo:
case ax::mojom::Role::kWebView:
return NSAccessibilityGroupRole;
case ax::mojom::Role::kButton:
return NSAccessibilityButtonRole;
case ax::mojom::Role::kCanvas:
return NSAccessibilityImageRole;
case ax::mojom::Role::kCaret:
return NSAccessibilityUnknownRole;
case ax::mojom::Role::kCell:
return @"AXCell";
case ax::mojom::Role::kCheckBox:
return NSAccessibilityCheckBoxRole;
case ax::mojom::Role::kColorWell:
return NSAccessibilityColorWellRole;
case ax::mojom::Role::kColumn:
return NSAccessibilityColumnRole;
case ax::mojom::Role::kColumnHeader:
return @"AXCell";
case ax::mojom::Role::kComboBoxGrouping:
return NSAccessibilityComboBoxRole;
case ax::mojom::Role::kComboBoxMenuButton:
return NSAccessibilityComboBoxRole;
case ax::mojom::Role::kComboBoxSelect:
// TODO(crbug.com/1362834): Can this be NSAccessibilityComboBoxRole?
return NSAccessibilityPopUpButtonRole;
case ax::mojom::Role::kDate:
return @"AXDateField";
case ax::mojom::Role::kDateTime:
return @"AXDateField";
case ax::mojom::Role::kDescriptionList:
return NSAccessibilityListRole;
case ax::mojom::Role::kDirectory:
return NSAccessibilityListRole;
case ax::mojom::Role::kDisclosureTriangle:
// If Mac supports AXExpandedChanged event with
// NSAccessibilityDisclosureTriangleRole, We should update
// ax::mojom::Role::kDisclosureTriangle mapping to
// NSAccessibilityDisclosureTriangleRole. http://crbug.com/558324
return NSAccessibilityButtonRole;
case ax::mojom::Role::kDocBackLink:
case ax::mojom::Role::kDocBiblioRef:
case ax::mojom::Role::kDocGlossRef:
case ax::mojom::Role::kDocNoteRef:
return NSAccessibilityLinkRole;
case ax::mojom::Role::kDocCover:
return NSAccessibilityImageRole;
case ax::mojom::Role::kDocPageBreak:
return NSAccessibilitySplitterRole;
case ax::mojom::Role::kDocSubtitle:
return @"AXHeading";
case ax::mojom::Role::kGraphicsSymbol:
return NSAccessibilityImageRole;
case ax::mojom::Role::kGrid:
// Should be NSAccessibilityGridRole but VoiceOver treating it like
// a list as of 10.12.6, so following WebKit and using table role:
// crbug.com/753925
return NSAccessibilityTableRole;
case ax::mojom::Role::kHeading:
return @"AXHeading";
case ax::mojom::Role::kImage:
return NSAccessibilityImageRole;
case ax::mojom::Role::kImeCandidate:
return NSAccessibilityUnknownRole;
case ax::mojom::Role::kInlineTextBox:
return NSAccessibilityStaticTextRole;
case ax::mojom::Role::kInputTime:
return @"AXTimeField";
case ax::mojom::Role::kKeyboard:
return NSAccessibilityUnknownRole;
case ax::mojom::Role::kLink:
return NSAccessibilityLinkRole;
case ax::mojom::Role::kList:
return NSAccessibilityListRole;
case ax::mojom::Role::kListBox:
return NSAccessibilityListRole;
case ax::mojom::Role::kListBoxOption:
return NSAccessibilityStaticTextRole;
case ax::mojom::Role::kListGrid:
return NSAccessibilityTableRole;
case ax::mojom::Role::kListMarker:
return @"AXListMarker";
case ax::mojom::Role::kMenu:
return NSAccessibilityMenuRole;
case ax::mojom::Role::kMenuBar:
return NSAccessibilityMenuBarRole;
case ax::mojom::Role::kMenuItem:
return NSAccessibilityMenuItemRole;
case ax::mojom::Role::kMenuItemCheckBox:
return NSAccessibilityMenuItemRole;
case ax::mojom::Role::kMenuItemRadio:
return NSAccessibilityMenuItemRole;
case ax::mojom::Role::kMenuListOption:
return NSAccessibilityMenuItemRole;
case ax::mojom::Role::kMenuListPopup:
return NSAccessibilityMenuRole;
case ax::mojom::Role::kMeter:
return NSAccessibilityLevelIndicatorRole;
case ax::mojom::Role::kPdfActionableHighlight:
return NSAccessibilityButtonRole;
case ax::mojom::Role::kPopUpButton:
return NSAccessibilityPopUpButtonRole;
case ax::mojom::Role::kPortal:
return NSAccessibilityButtonRole;
case ax::mojom::Role::kProgressIndicator:
return NSAccessibilityProgressIndicatorRole;
case ax::mojom::Role::kRadioButton:
return NSAccessibilityRadioButtonRole;
case ax::mojom::Role::kRadioGroup:
return NSAccessibilityRadioGroupRole;
case ax::mojom::Role::kRootWebArea:
return NSAccessibilityWebAreaRole;
case ax::mojom::Role::kRow:
return NSAccessibilityRowRole;
case ax::mojom::Role::kRowHeader:
return @"AXCell";
case ax::mojom::Role::kRubyAnnotation:
return NSAccessibilityUnknownRole;
case ax::mojom::Role::kScrollBar:
return NSAccessibilityScrollBarRole;
case ax::mojom::Role::kScrollView:
return NSAccessibilityScrollAreaRole;
case ax::mojom::Role::kSearchBox:
return NSAccessibilityTextFieldRole;
case ax::mojom::Role::kSlider:
return NSAccessibilitySliderRole;
case ax::mojom::Role::kSpinButton:
return NSAccessibilityIncrementorRole;
case ax::mojom::Role::kSplitter:
return NSAccessibilitySplitterRole;
case ax::mojom::Role::kStaticText:
return NSAccessibilityStaticTextRole;
case ax::mojom::Role::kSwitch:
return NSAccessibilityCheckBoxRole;
case ax::mojom::Role::kTab:
return NSAccessibilityRadioButtonRole;
case ax::mojom::Role::kTable:
return NSAccessibilityTableRole;
case ax::mojom::Role::kTabList:
return NSAccessibilityTabGroupRole;
case ax::mojom::Role::kTextField:
return NSAccessibilityTextFieldRole;
case ax::mojom::Role::kTextFieldWithComboBox:
return NSAccessibilityComboBoxRole;
case ax::mojom::Role::kTitleBar:
return NSAccessibilityStaticTextRole;
case ax::mojom::Role::kToggleButton:
return NSAccessibilityCheckBoxRole;
case ax::mojom::Role::kToolbar:
return NSAccessibilityToolbarRole;
case ax::mojom::Role::kTree:
return NSAccessibilityOutlineRole;
case ax::mojom::Role::kTreeGrid:
return NSAccessibilityTableRole;
case ax::mojom::Role::kTreeItem:
return NSAccessibilityRowRole;
case ax::mojom::Role::kUnknown:
return NSAccessibilityUnknownRole;
case ax::mojom::Role::kWindow:
// Use the group role as the BrowserNativeWidgetWindow already provides
// a kWindow role, and having extra window roles, which are treated
// specially by screen readers, can break their ability to find the
// content window. See http://crbug.com/875843 for more information.
return NSAccessibilityGroupRole;
}
}
+ (NSString*)nativeSubroleFromAXRole:(ax::mojom::Role)role {
static const base::NoDestructor<RoleMap> subrole_map(BuildSubroleMap());
RoleMap::const_iterator it = subrole_map->find(role);
return it != subrole_map->end() ? it->second : nil;
}
+ (NSString*)nativeNotificationFromAXEvent:(ax::mojom::Event)event {
static const base::NoDestructor<EventMap> event_map(BuildEventMap());
EventMap::const_iterator it = event_map->find(event);
return it != event_map->end() ? it->second : nil;
}
- (instancetype)initWithNode:(ui::AXPlatformNodeBase*)node {
if ((self = [super init])) {
_node = node;
}
return self;
}
- (void)detach {
if (!_node)
return;
_node = nil;
NSAccessibilityPostNotification(
self, NSAccessibilityUIElementDestroyedNotification);
}
- (NSRect)boundsInScreen {
if (!_node || !_node->GetDelegate())
return NSZeroRect;
return gfx::ScreenRectToNSRect(_node->GetDelegate()->GetBoundsRect(
ui::AXCoordinateSystem::kScreenDIPs, ui::AXClippingBehavior::kClipped));
}
- (NSString*)getStringAttribute:(ax::mojom::StringAttribute)attribute {
std::string attributeValue;
if (_node->GetStringAttribute(attribute, &attributeValue))
return base::SysUTF8ToNSString(attributeValue);
return nil;
}
- (NSString*)getAXValueAsString {
id value = [self AXValue];
return [value isKindOfClass:[NSString class]] ? value : nil;
}
- (ax::mojom::Role)internalRole {
if ([self instanceActive]) {
ax::mojom::Role role = static_cast<ax::mojom::Role>(_node->GetRole());
// Make sure to use Role::kPopupButton instead of Role::kButton for all
// values of kHasPopup. This is normally already true, but the default
// implementation does not use kPopupButton if aria-haspopup="dialog".
if (role == ax::mojom::Role::kButton &&
_node->HasIntAttribute(ax::mojom::IntAttribute::kHasPopup)) {
return ax::mojom::Role::kPopUpButton;
}
return role;
}
return ax::mojom::Role::kUnknown;
}
- (AXPlatformNodeCocoa*)fromNodeID:(ui::AXNodeID)id {
ui::AXPlatformNode* cell = _node->GetDelegate()->GetFromNodeID(id);
if (cell)
return cell->GetNativeViewAccessible();
return nil;
}
- (BOOL)isImage {
bool has_image_semantics =
ui::IsImage(_node->GetRole()) &&
!_node->GetBoolAttribute(ax::mojom::BoolAttribute::kCanvasHasFallback) &&
!_node->GetChildCount() &&
_node->GetNameFrom() != ax::mojom::NameFrom::kAttributeExplicitlyEmpty;
#if DCHECK_IS_ON()
bool is_native_image =
[[self accessibilityRole] isEqualToString:NSAccessibilityImageRole];
DCHECK_EQ(is_native_image, has_image_semantics)
<< "\nPresence/lack of native image role do not match the expected "
"internal semantics:"
<< "\n* Chrome role: " << ui::ToString(_node->GetRole())
<< "\n* NSAccessibility role: " << [self accessibilityRole]
<< "\n* AXNode: " << *_node;
#endif
return has_image_semantics;
}
- (void)addTextAnnotationsIn:(const AXRange*)axRange
to:(NSMutableAttributedString*)attributedString {
int anchorStartOffset = 0;
std::map<ui::AXNodeID, std::set<ax::mojom::Role>> ancestor_roles;
[attributedString beginEditing];
for (const AXRange& leafTextRange : *axRange) {
DCHECK(!leafTextRange.IsNull());
DCHECK_EQ(leafTextRange.anchor()->GetAnchor(),
leafTextRange.focus()->GetAnchor())
<< "An anchor range should only span a single object.";
ui::AXNode* anchor = leafTextRange.focus()->GetAnchor();
DCHECK(anchor) << "A non-null position should have a non-null anchor node.";
// Add misspelling information
const std::vector<int32_t>& markerTypes =
anchor->GetIntListAttribute(ax::mojom::IntListAttribute::kMarkerTypes);
const std::vector<int>& markerStarts =
anchor->GetIntListAttribute(ax::mojom::IntListAttribute::kMarkerStarts);
const std::vector<int>& markerEnds =
anchor->GetIntListAttribute(ax::mojom::IntListAttribute::kMarkerEnds);
DCHECK_EQ(markerTypes.size(), markerStarts.size());
DCHECK_EQ(markerTypes.size(), markerEnds.size());
for (size_t i = 0; i < markerTypes.size(); ++i) {
if (!(markerTypes[i] &
static_cast<int32_t>(ax::mojom::MarkerType::kSpelling))) {
continue;
}
int misspellingStart = anchorStartOffset + markerStarts[i];
int misspellingEnd = anchorStartOffset + markerEnds[i];
int misspellingLength = misspellingEnd - misspellingStart;
DCHECK_LE(static_cast<unsigned long>(misspellingEnd),
[attributedString length]);
DCHECK_GT(misspellingLength, 0);
[attributedString
addAttribute:NSAccessibilityMarkedMisspelledTextAttribute
value:@YES
range:NSMakeRange(misspellingStart, misspellingLength)];
}
// Add annotation information
int leafTextLength = leafTextRange.GetText().length();
DCHECK_LE(static_cast<unsigned long>(anchorStartOffset + leafTextLength),
[attributedString length]);
NSRange leafRange = NSMakeRange(anchorStartOffset, leafTextLength);
CollectAncestorRoles(*anchor, ancestor_roles);
if (ancestor_roles[anchor->id()].contains(ax::mojom::Role::kMark)) {
[attributedString addAttribute:@"AXHighlight" value:@YES range:leafRange];
}
if (ancestor_roles[anchor->id()].contains(ax::mojom::Role::kSuggestion)) {
[attributedString addAttribute:@"AXIsSuggestion"
value:@YES
range:leafRange];
}
if (ancestor_roles[anchor->id()].contains(
ax::mojom::Role::kContentDeletion)) {
[attributedString addAttribute:@"AXIsSuggestedDeletion"
value:@YES
range:leafRange];
}
if (ancestor_roles[anchor->id()].contains(
ax::mojom::Role::kContentInsertion)) {
[attributedString addAttribute:@"AXIsSuggestedInsertion"
value:@YES
range:leafRange];
}
anchorStartOffset += leafTextLength;
}
[attributedString endEditing];
}
- (NSString*)descriptionIfFromAriaDescription {
// TODO(crbug.com/1373178): Eventually this should include the DescriptionFrom
// for content from aria-describedby, kRelatedContent.
ax::mojom::DescriptionFrom descFrom = static_cast<ax::mojom::DescriptionFrom>(
_node->GetIntAttribute(ax::mojom::IntAttribute::kDescriptionFrom));
if (descFrom == ax::mojom::DescriptionFrom::kAriaDescription) {
return [self getStringAttribute:ax::mojom::StringAttribute::kDescription];
}
return nil;
}
- (NSString*)getName {
return base::SysUTF8ToNSString(_node->GetName());
}
- (std::unique_ptr<ui::AXAnnouncementSpec>)announcementForEvent:
(ax::mojom::Event)eventType {
// Only alerts and live region changes should be announced.
DCHECK(eventType == ax::mojom::Event::kAlert ||
eventType == ax::mojom::Event::kLiveRegionChanged);
std::string liveStatus =
_node->GetStringAttribute(ax::mojom::StringAttribute::kLiveStatus);
// If live status is explicitly set to off, don't announce.
if (liveStatus == "off")
return nullptr;
NSString* name = [self getName];
NSString* announcementText =
[name length] > 0
? name
: base::SysUTF16ToNSString(_node->GetTextContentUTF16());
if ([announcementText length] == 0)
return nullptr;
const std::string& description =
_node->GetStringAttribute(ax::mojom::StringAttribute::kDescription);
if (!description.empty()) {
// Concatenating name and description, with a newline in between to create a
// pause to avoid treating the concatenation as a single sentence.
announcementText =
[NSString stringWithFormat:@"%@\n%@", announcementText,
base::SysUTF8ToNSString(description)];
}
auto announcement = std::make_unique<ui::AXAnnouncementSpec>();
announcement->announcement =
base::scoped_nsobject<NSString>([announcementText retain]);
announcement->window =
base::scoped_nsobject<NSWindow>([[self AXWindow] retain]);
announcement->is_polite = liveStatus != "assertive";
return announcement;
}
- (void)scheduleLiveRegionAnnouncement:
(std::unique_ptr<ui::AXAnnouncementSpec>)announcement {
if (_pendingAnnouncement) {
// An announcement is already in flight, so just reset the contents. This is
// threadsafe because the dispatch is on the main queue.
_pendingAnnouncement = std::move(announcement);
return;
}
_pendingAnnouncement = std::move(announcement);
dispatch_after(
kLiveRegionDebounceMillis * NSEC_PER_MSEC, dispatch_get_main_queue(), ^{
if (!_pendingAnnouncement) {
return;
}
PostAnnouncementNotification(_pendingAnnouncement->announcement,
_pendingAnnouncement->window,
_pendingAnnouncement->is_polite);
_pendingAnnouncement.reset();
});
}
//
// NSAccessibility legacy informal protocol implementation (deprecated).
//
- (BOOL)accessibilityIsIgnored {
return ![self isAccessibilityElement];
}
- (id)accessibilityHitTest:(NSPoint)point {
if (!NSPointInRect(point, [self boundsInScreen]))
return nil;
for (id child in [[self AXChildren] reverseObjectEnumerator]) {
if (!NSPointInRect(point, [child accessibilityFrame]))
continue;
if (id foundChild = [child accessibilityHitTest:point])
return foundChild;
}
// Hit self, but not any child.
return NSAccessibilityUnignoredAncestor(self);
}
- (BOOL)accessibilityNotifiesWhenDestroyed {
return YES;
}
- (id)accessibilityFocusedUIElement {
return _node ? _node->GetDelegate()->GetFocus() : nil;
}
// This function and accessibilityPerformAction:, while deprecated, are a) still
// called by AppKit internally and b) not implemented by NSAccessibilityElement,
// so this class needs its own implementations.
- (NSArray*)accessibilityActionNames {
if (!_node)
return @[];
base::scoped_nsobject<NSMutableArray> axActions(
[[NSMutableArray alloc] init]);
const ActionList& action_list = GetActionList();
// VoiceOver expects the "press" action to be first. Note that some roles
// should be given a press action implicitly.
DCHECK([action_list[0].second isEqualToString:NSAccessibilityPressAction]);
for (const auto& item : action_list) {
if (_node->HasAction(item.first) || HasImplicitAction(*_node, item.first))
[axActions addObject:item.second];
}
if (AlsoUseShowMenuActionForDefaultAction(*_node))
[axActions addObject:NSAccessibilityShowMenuAction];
return axActions.autorelease();
}
- (void)accessibilityPerformAction:(NSString*)action {
// Actions are performed asynchronously, so it's always possible for an object
// to change its mind after previously reporting an action as available.
if (![[self accessibilityActionNames] containsObject:action])
return;
ui::AXActionData data;
if ([action isEqualToString:NSAccessibilityShowMenuAction] &&
AlsoUseShowMenuActionForDefaultAction(*_node)) {
data.action = ax::mojom::Action::kDoDefault;
} else {
for (const ActionList::value_type& entry : GetActionList()) {
if ([action isEqualToString:entry.second]) {
data.action = entry.first;
break;
}
}
}
// Note ui::AX_ACTIONs which are just overwriting an accessibility attribute
// are already implemented in -accessibilitySetValue:forAttribute:, so ignore
// those here.
if (data.action != ax::mojom::Action::kNone)
_node->GetDelegate()->AccessibilityPerformAction(data);
}
// This method, while deprecated, is still called internally by AppKit.
- (NSArray*)accessibilityAttributeNames {
if (!_node)
return @[];
// These attributes are required on all accessibility objects.
NSArray* const kAllRoleAttributes = @[
NSAccessibilityBlockQuoteLevelAttribute, NSAccessibilityChildrenAttribute,
NSAccessibilityDOMClassList, NSAccessibilityDOMIdentifierAttribute,
NSAccessibilityDescriptionAttribute, NSAccessibilityElementBusyAttribute,
NSAccessibilityParentAttribute, NSAccessibilityPositionAttribute,
NSAccessibilityRoleAttribute, NSAccessibilitySizeAttribute,
NSAccessibilitySelectedAttribute, NSAccessibilitySizeAttribute,
NSAccessibilitySubroleAttribute,
// Title is required for most elements. Cocoa asks for the value even if it
// is omitted here, but won't present it to accessibility APIs without this.
NSAccessibilityTitleAttribute,
// Attributes which are not required, but are general to all roles.
NSAccessibilityRoleDescriptionAttribute, NSAccessibilityEnabledAttribute,
NSAccessibilityFocusedAttribute, NSAccessibilityHelpAttribute,
NSAccessibilityTopLevelUIElementAttribute, NSAccessibilityVisitedAttribute,
NSAccessibilityWindowAttribute, NSAccessibilityChromeAXNodeIdAttribute
];
// Attributes required for user-editable controls.
NSArray* const kValueAttributes = @[ NSAccessibilityValueAttribute ];
// Attributes required for unprotected textfields and labels.
NSArray* const kUnprotectedTextAttributes = @[
NSAccessibilityInsertionPointLineNumberAttribute,
NSAccessibilityNumberOfCharactersAttribute,