-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathax_object.cc
More file actions
7749 lines (6728 loc) · 279 KB
/
Copy pathax_object.cc
File metadata and controls
7749 lines (6728 loc) · 279 KB
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 (C) 2008, 2009, 2011 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/modules/accessibility/ax_object.h"
#include <algorithm>
#include <ostream>
#include "base/auto_reset.h"
#include "base/numerics/safe_conversions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/input/web_keyboard_event.h"
#include "third_party/blink/public/common/input/web_menu_source_type.h"
#include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom-blink.h"
#include "third_party/blink/public/mojom/input/focus_type.mojom-blink.h"
#include "third_party/blink/public/mojom/scroll/scroll_into_view_params.mojom-blink.h"
#include "third_party/blink/renderer/core/aom/accessible_node.h"
#include "third_party/blink/renderer/core/aom/accessible_node_list.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver.h"
#include "third_party/blink/renderer/core/display_lock/display_lock_utilities.h"
#include "third_party/blink/renderer/core/dom/dom_node_ids.h"
#include "third_party/blink/renderer/core/dom/events/simulated_click_options.h"
#include "third_party/blink/renderer/core/dom/focus_params.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/dom/slot_assignment_engine.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/events/keyboard_event.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/frame/visual_viewport.h"
#include "third_party/blink/renderer/core/fullscreen/fullscreen.h"
#include "third_party/blink/renderer/core/html/canvas/html_canvas_element.h"
#include "third_party/blink/renderer/core/html/custom/element_internals.h"
#include "third_party/blink/renderer/core/html/fenced_frame/html_fenced_frame_element.h"
#include "third_party/blink/renderer/core/html/forms/html_form_control_element.h"
#include "third_party/blink/renderer/core/html/forms/html_input_element.h"
#include "third_party/blink/renderer/core/html/forms/html_opt_group_element.h"
#include "third_party/blink/renderer/core/html/forms/html_select_element.h"
#include "third_party/blink/renderer/core/html/forms/html_text_area_element.h"
#include "third_party/blink/renderer/core/html/forms/text_control_element.h"
#include "third_party/blink/renderer/core/html/html_dialog_element.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/html/html_frame_owner_element.h"
#include "third_party/blink/renderer/core/html/html_head_element.h"
#include "third_party/blink/renderer/core/html/html_image_element.h"
#include "third_party/blink/renderer/core/html/html_map_element.h"
#include "third_party/blink/renderer/core/html/html_script_element.h"
#include "third_party/blink/renderer/core/html/html_slot_element.h"
#include "third_party/blink/renderer/core/html/html_style_element.h"
#include "third_party/blink/renderer/core/html/html_table_cell_element.h"
#include "third_party/blink/renderer/core/html/html_table_element.h"
#include "third_party/blink/renderer/core/html/html_table_row_element.h"
#include "third_party/blink/renderer/core/html/html_table_section_element.h"
#include "third_party/blink/renderer/core/html/html_title_element.h"
#include "third_party/blink/renderer/core/html/media/html_media_element.h"
#include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h"
#include "third_party/blink/renderer/core/html/portal/html_portal_element.h"
#include "third_party/blink/renderer/core/html/shadow/shadow_element_names.h"
#include "third_party/blink/renderer/core/input/context_menu_allowed_scope.h"
#include "third_party/blink/renderer/core/input/event_handler.h"
#include "third_party/blink/renderer/core/input_type_names.h"
#include "third_party/blink/renderer/core/layout/layout_box.h"
#include "third_party/blink/renderer/core/layout/layout_box_model_object.h"
#include "third_party/blink/renderer/core/layout/layout_image.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/page/chrome_client.h"
#include "third_party/blink/renderer/core/page/focus_controller.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller.h"
#include "third_party/blink/renderer/core/scroll/scroll_into_view_util.h"
#include "third_party/blink/renderer/core/svg/svg_element.h"
#include "third_party/blink/renderer/core/svg/svg_g_element.h"
#include "third_party/blink/renderer/core/svg/svg_style_element.h"
#if DCHECK_IS_ON()
#include "third_party/blink/renderer/modules/accessibility/ax_debug_utils.h"
#endif
#include "third_party/blink/renderer/modules/accessibility/ax_image_map_link.h"
#include "third_party/blink/renderer/modules/accessibility/ax_inline_text_box.h"
#include "third_party/blink/renderer/modules/accessibility/ax_menu_list.h"
#include "third_party/blink/renderer/modules/accessibility/ax_menu_list_option.h"
#include "third_party/blink/renderer/modules/accessibility/ax_menu_list_popup.h"
#include "third_party/blink/renderer/modules/accessibility/ax_object_cache_impl.h"
#include "third_party/blink/renderer/modules/accessibility/ax_range.h"
#include "third_party/blink/renderer/modules/accessibility/ax_selection.h"
#include "third_party/blink/renderer/modules/accessibility/ax_sparse_attribute_setter.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/keyboard_codes.h"
#include "third_party/blink/renderer/platform/language.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/text/platform_locale.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/wtf_size_t.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_common.h"
#include "ui/accessibility/ax_enums.mojom-blink-forward.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/gfx/geometry/transform.h"
namespace blink {
namespace {
#if defined(AX_FAIL_FAST_BUILD)
// TODO(accessibility) Move this out of DEBUG by having a new enum in
// ax_enums.mojom, and a matching ToString() in ax_enum_utils, as well as move
// out duplicate code of String IgnoredReasonName(AXIgnoredReason reason) in
// inspector_type_builder_helper.cc.
String IgnoredReasonName(AXIgnoredReason reason) {
switch (reason) {
case kAXActiveFullscreenElement:
return "activeFullscreenElement";
case kAXActiveModalDialog:
return "activeModalDialog";
case kAXAriaModalDialog:
return "activeAriaModalDialog";
case kAXAriaHiddenElement:
return "ariaHiddenElement";
case kAXAriaHiddenSubtree:
return "ariaHiddenSubtree";
case kAXEmptyAlt:
return "emptyAlt";
case kAXEmptyText:
return "emptyText";
case kAXInertElement:
return "inertElement";
case kAXInertSubtree:
return "inertSubtree";
case kAXLabelContainer:
return "labelContainer";
case kAXLabelFor:
return "labelFor";
case kAXNotRendered:
return "notRendered";
case kAXNotVisible:
return "notVisible";
case kAXPresentational:
return "presentationalRole";
case kAXProbablyPresentational:
return "probablyPresentational";
case kAXUninteresting:
return "uninteresting";
}
NOTREACHED();
return "";
}
String GetIgnoredReasonsDebugString(AXObject::IgnoredReasons& reasons) {
if (reasons.size() == 0)
return "";
String string_builder = "(";
for (wtf_size_t count = 0; count < reasons.size(); count++) {
if (count > 0)
string_builder = string_builder + ',';
string_builder = string_builder + IgnoredReasonName(reasons[count].reason);
}
string_builder = string_builder + ")";
return string_builder;
}
#endif
String GetNodeString(Node* node) {
if (node->IsTextNode()) {
String string_builder = "\"";
string_builder = string_builder + node->nodeValue();
string_builder = string_builder + "\"";
return string_builder;
}
Element* element = DynamicTo<Element>(node);
if (!element) {
return To<Document>(node)->IsLoadCompleted() ? "#document"
: "#document (loading)";
}
String string_builder = "<";
string_builder = string_builder + element->tagName().LowerASCII();
// Cannot safely get @class from SVG elements.
if (!element->IsSVGElement() &&
element->FastHasAttribute(html_names::kClassAttr)) {
string_builder = string_builder + "." +
element->FastGetAttribute(html_names::kClassAttr);
}
if (element->FastHasAttribute(html_names::kIdAttr)) {
string_builder =
string_builder + "#" + element->FastGetAttribute(html_names::kIdAttr);
}
return string_builder + ">";
}
#if DCHECK_IS_ON()
bool IsValidRole(ax::mojom::blink::Role role) {
// Check for illegal roles that should not be assigned in Blink.
switch (role) {
case ax::mojom::blink::Role::kCaret:
case ax::mojom::blink::Role::kClient:
case ax::mojom::blink::Role::kColumn:
case ax::mojom::blink::Role::kDesktop:
case ax::mojom::blink::Role::kKeyboard:
case ax::mojom::blink::Role::kImeCandidate:
case ax::mojom::blink::Role::kListGrid:
case ax::mojom::blink::Role::kPane:
case ax::mojom::blink::Role::kPdfActionableHighlight:
case ax::mojom::blink::Role::kPdfRoot:
case ax::mojom::blink::Role::kTableHeaderContainer:
case ax::mojom::blink::Role::kTitleBar:
case ax::mojom::blink::Role::kUnknown:
case ax::mojom::blink::Role::kWebView:
case ax::mojom::blink::Role::kWindow:
return false;
default:
return true;
}
}
#endif
using RoleHashTraits =
EnumHashTraits<ax::mojom::blink::Role, ax::mojom::blink::Role::kUnknown>;
constexpr wtf_size_t kNumRoles =
static_cast<wtf_size_t>(ax::mojom::blink::Role::kMaxValue) + 1;
using ARIARoleMap = HashMap<String,
ax::mojom::blink::Role,
CaseFoldingHashTraits<String>,
RoleHashTraits>;
struct RoleEntry {
const char* role_name;
ax::mojom::blink::Role role;
};
// Mapping of ARIA role name to internal role name.
// This is used for the following:
// 1. Map from an ARIA role to the internal role when building tree.
// 2. Map from an internal role to an ARIA role name, for debugging, the
// xml-roles object attribute and element.computedRole.
const RoleEntry kAriaRoles[] = {
{"alert", ax::mojom::blink::Role::kAlert},
{"alertdialog", ax::mojom::blink::Role::kAlertDialog},
{"application", ax::mojom::blink::Role::kApplication},
{"article", ax::mojom::blink::Role::kArticle},
{"banner", ax::mojom::blink::Role::kBanner},
{"blockquote", ax::mojom::blink::Role::kBlockquote},
{"button", ax::mojom::blink::Role::kButton},
{"caption", ax::mojom::blink::Role::kCaption},
{"cell", ax::mojom::blink::Role::kCell},
{"code", ax::mojom::blink::Role::kCode},
{"checkbox", ax::mojom::blink::Role::kCheckBox},
{"columnheader", ax::mojom::blink::Role::kColumnHeader},
{"combobox", ax::mojom::blink::Role::kComboBoxGrouping},
{"comment", ax::mojom::blink::Role::kComment},
{"complementary", ax::mojom::blink::Role::kComplementary},
{"contentinfo", ax::mojom::blink::Role::kContentInfo},
{"definition", ax::mojom::blink::Role::kDefinition},
{"deletion", ax::mojom::blink::Role::kContentDeletion},
{"dialog", ax::mojom::blink::Role::kDialog},
{"directory", ax::mojom::blink::Role::kDirectory},
// -------------------------------------------------
// DPub Roles:
// www.w3.org/TR/dpub-aam-1.0/#mapping_role_table
{"doc-abstract", ax::mojom::blink::Role::kDocAbstract},
{"doc-acknowledgments", ax::mojom::blink::Role::kDocAcknowledgments},
{"doc-afterword", ax::mojom::blink::Role::kDocAfterword},
{"doc-appendix", ax::mojom::blink::Role::kDocAppendix},
{"doc-backlink", ax::mojom::blink::Role::kDocBackLink},
// Deprecated in DPUB-ARIA 1.1. Use a listitem inside of a doc-bibliography.
{"doc-biblioentry", ax::mojom::blink::Role::kDocBiblioEntry},
{"doc-bibliography", ax::mojom::blink::Role::kDocBibliography},
{"doc-biblioref", ax::mojom::blink::Role::kDocBiblioRef},
{"doc-chapter", ax::mojom::blink::Role::kDocChapter},
{"doc-colophon", ax::mojom::blink::Role::kDocColophon},
{"doc-conclusion", ax::mojom::blink::Role::kDocConclusion},
{"doc-cover", ax::mojom::blink::Role::kDocCover},
{"doc-credit", ax::mojom::blink::Role::kDocCredit},
{"doc-credits", ax::mojom::blink::Role::kDocCredits},
{"doc-dedication", ax::mojom::blink::Role::kDocDedication},
// Deprecated in DPUB-ARIA 1.1. Use a listitem inside of a doc-endnotes.
{"doc-endnote", ax::mojom::blink::Role::kDocEndnote},
{"doc-endnotes", ax::mojom::blink::Role::kDocEndnotes},
{"doc-epigraph", ax::mojom::blink::Role::kDocEpigraph},
{"doc-epilogue", ax::mojom::blink::Role::kDocEpilogue},
{"doc-errata", ax::mojom::blink::Role::kDocErrata},
{"doc-example", ax::mojom::blink::Role::kDocExample},
{"doc-footnote", ax::mojom::blink::Role::kDocFootnote},
{"doc-foreword", ax::mojom::blink::Role::kDocForeword},
{"doc-glossary", ax::mojom::blink::Role::kDocGlossary},
{"doc-glossref", ax::mojom::blink::Role::kDocGlossRef},
{"doc-index", ax::mojom::blink::Role::kDocIndex},
{"doc-introduction", ax::mojom::blink::Role::kDocIntroduction},
{"doc-noteref", ax::mojom::blink::Role::kDocNoteRef},
{"doc-notice", ax::mojom::blink::Role::kDocNotice},
{"doc-pagebreak", ax::mojom::blink::Role::kDocPageBreak},
{"doc-pagefooter", ax::mojom::blink::Role::kDocPageFooter},
{"doc-pageheader", ax::mojom::blink::Role::kDocPageHeader},
{"doc-pagelist", ax::mojom::blink::Role::kDocPageList},
{"doc-part", ax::mojom::blink::Role::kDocPart},
{"doc-preface", ax::mojom::blink::Role::kDocPreface},
{"doc-prologue", ax::mojom::blink::Role::kDocPrologue},
{"doc-pullquote", ax::mojom::blink::Role::kDocPullquote},
{"doc-qna", ax::mojom::blink::Role::kDocQna},
{"doc-subtitle", ax::mojom::blink::Role::kDocSubtitle},
{"doc-tip", ax::mojom::blink::Role::kDocTip},
{"doc-toc", ax::mojom::blink::Role::kDocToc},
// End DPub roles.
// -------------------------------------------------
{"document", ax::mojom::blink::Role::kDocument},
{"emphasis", ax::mojom::blink::Role::kEmphasis},
{"feed", ax::mojom::blink::Role::kFeed},
{"figure", ax::mojom::blink::Role::kFigure},
{"form", ax::mojom::blink::Role::kForm},
{"generic", ax::mojom::blink::Role::kGenericContainer},
// -------------------------------------------------
// ARIA Graphics module roles:
// https://rawgit.com/w3c/graphics-aam/master/
{"graphics-document", ax::mojom::blink::Role::kGraphicsDocument},
{"graphics-object", ax::mojom::blink::Role::kGraphicsObject},
{"graphics-symbol", ax::mojom::blink::Role::kGraphicsSymbol},
// End ARIA Graphics module roles.
// -------------------------------------------------
{"grid", ax::mojom::blink::Role::kGrid},
{"gridcell", ax::mojom::blink::Role::kCell},
{"group", ax::mojom::blink::Role::kGroup},
{"heading", ax::mojom::blink::Role::kHeading},
{"img", ax::mojom::blink::Role::kImage},
// role="image" is listed after role="img" to treat the synonym img
// as a computed name image
{"image", ax::mojom::blink::Role::kImage},
{"insertion", ax::mojom::blink::Role::kContentInsertion},
{"link", ax::mojom::blink::Role::kLink},
{"list", ax::mojom::blink::Role::kList},
{"listbox", ax::mojom::blink::Role::kListBox},
{"listitem", ax::mojom::blink::Role::kListItem},
{"log", ax::mojom::blink::Role::kLog},
{"main", ax::mojom::blink::Role::kMain},
{"marquee", ax::mojom::blink::Role::kMarquee},
{"math", ax::mojom::blink::Role::kMath},
{"menu", ax::mojom::blink::Role::kMenu},
{"menubar", ax::mojom::blink::Role::kMenuBar},
{"menuitem", ax::mojom::blink::Role::kMenuItem},
{"menuitemcheckbox", ax::mojom::blink::Role::kMenuItemCheckBox},
{"menuitemradio", ax::mojom::blink::Role::kMenuItemRadio},
{"mark", ax::mojom::blink::Role::kMark},
{"meter", ax::mojom::blink::Role::kMeter},
{"navigation", ax::mojom::blink::Role::kNavigation},
// role="presentation" is the same as role="none".
{"presentation", ax::mojom::blink::Role::kNone},
// role="none" is listed after role="presentation", so that it is the
// canonical name in devtools and tests.
{"none", ax::mojom::blink::Role::kNone},
{"note", ax::mojom::blink::Role::kNote},
{"option", ax::mojom::blink::Role::kListBoxOption},
{"paragraph", ax::mojom::blink::Role::kParagraph},
{"progressbar", ax::mojom::blink::Role::kProgressIndicator},
{"radio", ax::mojom::blink::Role::kRadioButton},
{"radiogroup", ax::mojom::blink::Role::kRadioGroup},
{"region", ax::mojom::blink::Role::kRegion},
{"row", ax::mojom::blink::Role::kRow},
{"rowgroup", ax::mojom::blink::Role::kRowGroup},
{"rowheader", ax::mojom::blink::Role::kRowHeader},
{"scrollbar", ax::mojom::blink::Role::kScrollBar},
{"search", ax::mojom::blink::Role::kSearch},
{"searchbox", ax::mojom::blink::Role::kSearchBox},
{"separator", ax::mojom::blink::Role::kSplitter},
{"slider", ax::mojom::blink::Role::kSlider},
{"spinbutton", ax::mojom::blink::Role::kSpinButton},
{"status", ax::mojom::blink::Role::kStatus},
{"strong", ax::mojom::blink::Role::kStrong},
{"subscript", ax::mojom::blink::Role::kSubscript},
{"suggestion", ax::mojom::blink::Role::kSuggestion},
{"superscript", ax::mojom::blink::Role::kSuperscript},
{"switch", ax::mojom::blink::Role::kSwitch},
{"tab", ax::mojom::blink::Role::kTab},
{"table", ax::mojom::blink::Role::kTable},
{"tablist", ax::mojom::blink::Role::kTabList},
{"tabpanel", ax::mojom::blink::Role::kTabPanel},
{"term", ax::mojom::blink::Role::kTerm},
{"textbox", ax::mojom::blink::Role::kTextField},
{"time", ax::mojom::blink::Role::kTime},
{"timer", ax::mojom::blink::Role::kTimer},
{"toolbar", ax::mojom::blink::Role::kToolbar},
{"tooltip", ax::mojom::blink::Role::kTooltip},
{"tree", ax::mojom::blink::Role::kTree},
{"treegrid", ax::mojom::blink::Role::kTreeGrid},
{"treeitem", ax::mojom::blink::Role::kTreeItem}};
// More friendly names for debugging. These are roles which don't map from
// the ARIA role name to the internal role when building the tree, but when
// debugging, we want to show the ARIA role name, since it is close in meaning.
const RoleEntry kReverseRoles[] = {
{"banner", ax::mojom::blink::Role::kHeader},
{"button", ax::mojom::blink::Role::kToggleButton},
{"button", ax::mojom::blink::Role::kPopUpButton},
{"contentinfo", ax::mojom::blink::Role::kFooter},
{"menuitem", ax::mojom::blink::Role::kMenuListOption},
{"combobox", ax::mojom::blink::Role::kComboBoxMenuButton},
{"combobox", ax::mojom::blink::Role::kComboBoxSelect},
{"combobox", ax::mojom::blink::Role::kTextFieldWithComboBox}};
static ARIARoleMap* CreateARIARoleMap() {
ARIARoleMap* role_map = new ARIARoleMap;
for (auto aria_role : kAriaRoles)
role_map->Set(String(aria_role.role_name), aria_role.role);
return role_map;
}
// The role name vector contains only ARIA roles, and no internal roles.
static Vector<AtomicString>* CreateARIARoleNameVector() {
Vector<AtomicString>* role_name_vector = new Vector<AtomicString>(kNumRoles);
role_name_vector->Fill(g_null_atom, kNumRoles);
for (auto aria_role : kAriaRoles) {
(*role_name_vector)[static_cast<wtf_size_t>(aria_role.role)] =
AtomicString(aria_role.role_name);
}
for (auto reverse_role : kReverseRoles) {
(*role_name_vector)[static_cast<wtf_size_t>(reverse_role.role)] =
AtomicString(reverse_role.role_name);
}
return role_name_vector;
}
void AddIntListAttributeFromObjects(ax::mojom::blink::IntListAttribute attr,
const AXObject::AXObjectVector& objects,
ui::AXNodeData* node_data) {
DCHECK(node_data);
std::vector<int32_t> ids;
for (const auto& obj : objects) {
if (!obj->AccessibilityIsIgnored())
ids.push_back(obj->AXObjectID());
}
if (!ids.empty())
node_data->AddIntListAttribute(attr, ids);
}
// Max length for attributes such as aria-label.
static constexpr uint32_t kMaxStringAttributeLength = 10000;
// Max length for a static text name.
// Length of War and Peace (http://www.gutenberg.org/files/2600/2600-0.txt).
static constexpr uint32_t kMaxStaticTextLength = 3227574;
void TruncateAndAddStringAttribute(
ui::AXNodeData* dst,
ax::mojom::blink::StringAttribute attribute,
const String& value,
uint32_t max_len = kMaxStringAttributeLength) {
if (value.empty())
return;
std::string value_utf8 = value.Utf8(kStrictUTF8Conversion);
if (value_utf8.size() > max_len) {
std::string truncated;
base::TruncateUTF8ToByteSize(value_utf8, max_len, &truncated);
dst->AddStringAttribute(attribute, truncated);
} else {
dst->AddStringAttribute(attribute, value_utf8);
}
}
void AddIntListAttributeFromOffsetVector(
ax::mojom::blink::IntListAttribute attr,
const Vector<int> offsets,
ui::AXNodeData* node_data) {
std::vector<int32_t> offset_values;
for (int offset : offsets)
offset_values.push_back(static_cast<int32_t>(offset));
DCHECK(node_data);
if (!offset_values.empty())
node_data->AddIntListAttribute(attr, offset_values);
}
} // namespace
int32_t ToAXMarkerType(DocumentMarker::MarkerType marker_type) {
ax::mojom::blink::MarkerType result;
switch (marker_type) {
case DocumentMarker::kSpelling:
result = ax::mojom::blink::MarkerType::kSpelling;
break;
case DocumentMarker::kGrammar:
result = ax::mojom::blink::MarkerType::kGrammar;
break;
case DocumentMarker::kTextFragment:
case DocumentMarker::kTextMatch:
result = ax::mojom::blink::MarkerType::kTextMatch;
break;
case DocumentMarker::kActiveSuggestion:
result = ax::mojom::blink::MarkerType::kActiveSuggestion;
break;
case DocumentMarker::kSuggestion:
result = ax::mojom::blink::MarkerType::kSuggestion;
break;
case DocumentMarker::kCustomHighlight:
result = ax::mojom::blink::MarkerType::kHighlight;
break;
default:
result = ax::mojom::blink::MarkerType::kNone;
break;
}
return static_cast<int32_t>(result);
}
int32_t ToAXHighlightType(const AtomicString& highlight_type) {
DEFINE_STATIC_LOCAL(const AtomicString, type_highlight, ("highlight"));
DEFINE_STATIC_LOCAL(const AtomicString, type_spelling_error,
("spelling-error"));
DEFINE_STATIC_LOCAL(const AtomicString, type_grammar_error,
("grammar-error"));
ax::mojom::blink::HighlightType result =
ax::mojom::blink::HighlightType::kNone;
if (highlight_type == type_highlight)
result = ax::mojom::blink::HighlightType::kHighlight;
else if (highlight_type == type_spelling_error)
result = ax::mojom::blink::HighlightType::kSpellingError;
else if (highlight_type == type_grammar_error)
result = ax::mojom::blink::HighlightType::kGrammarError;
// Check that |highlight_type| is one of the static AtomicStrings defined
// above or "none", so if there are more HighlightTypes added, they should
// also be taken into account in this function.
DCHECK(result != ax::mojom::blink::HighlightType::kNone ||
highlight_type == "none");
return static_cast<int32_t>(result);
}
const AXObject* FindAncestorWithAriaHidden(const AXObject* start) {
for (const AXObject* object = start; object && !object->IsWebArea();
object = object->ParentObject()) {
if (object->AOMPropertyOrARIAAttributeIsTrue(AOMBooleanProperty::kHidden))
return object;
}
return nullptr;
}
// static
unsigned AXObject::number_of_live_ax_objects_ = 0;
AXObject::AXObject(AXObjectCacheImpl& ax_object_cache)
: id_(0),
parent_(nullptr),
role_(ax::mojom::blink::Role::kUnknown),
explicit_container_id_(0),
cached_values_need_update_(true),
cached_is_ignored_(false),
cached_is_ignored_but_included_in_tree_(false),
cached_is_inert_(false),
cached_is_aria_hidden_(false),
cached_is_descendant_of_disabled_node_(false),
cached_can_set_focus_attribute_(false),
cached_live_region_root_(nullptr),
ax_object_cache_(&ax_object_cache) {
++number_of_live_ax_objects_;
}
AXObject::~AXObject() {
DCHECK(IsDetached());
--number_of_live_ax_objects_;
}
void AXObject::SetHasDirtyDescendants(bool dirty) const {
CHECK(!dirty || LastKnownIsIncludedInTreeValue())
<< "Only included nodes can be marked as having dirty descendants: "
<< ToString(true, true);
has_dirty_descendants_ = dirty;
}
void AXObject::SetAncestorsHaveDirtyDescendants() const {
DCHECK(!IsDetached());
DCHECK(!AXObjectCache().HasBeenDisposed());
DCHECK(!AXObjectCache().IsFrozen());
DCHECK(!AXObjectCache().UpdatingTree());
if (!RuntimeEnabledFeatures::AccessibilityEagerAXTreeUpdateEnabled()) {
return;
}
// Set the dirty bit for the root AX object when created. For all other
// objects, this is set by a descendant needing to be updated, and
// AXObjectCacheImpl::UpdateTreeIfNeeded will therefore process an object
// if its parent has has_dirty_descendants_ set. The root, however, has no
// parent, so there is no parent to mark in order to cause the root to update
// itself. Therefore this bit serves a second purpose of determining
// whether AXObjectCacheImpl::UpdateTreeIfNeeded needs to update the root
// object.
if (IsRoot()) {
// Need at least the root object to be flagged in order for
// UpdateTreeIfNeeded() to do anything.
SetHasDirtyDescendants(true);
return;
}
const AXObject* ancestor = this;
bool can_repair_parents = AXObjectCache().IsProcessingDeferredEvents();
while (true) {
if (can_repair_parents && ancestor->IsMissingParent()) {
// RepairMissingParent() will finish setting the
// "has dirty descendants" flag the rest of the way of the parent chain
// by calling back into this method.
ancestor->RepairMissingParent();
break;
}
ancestor = ancestor->CachedParentObject();
if (!ancestor) {
break;
}
DCHECK(!ancestor->IsDetached());
// We need to to continue setting bits through AX objects for which
// LastKnownIsIncludedInTreeValue is false, since those objects are omitted
// from the generated tree. However, don't set the bit on unincluded
// objects, during the clearing phase in
// AXObjectCacheImpl::UpdateTreeIfNeeded(), only included nodes are
// visited.
if (!ancestor->LastKnownIsIncludedInTreeValue()) {
continue;
}
if (ancestor->has_dirty_descendants_) {
break;
}
ancestor->SetHasDirtyDescendants(true);
}
#if DCHECK_IS_ON()
// Walk up the tree looking for dirty bits that failed to be set. If any
// are found, this is a bug.
if (!AXObjectCache().UpdatingTree()) {
bool fail = false;
for (auto* obj = CachedParentObject(); obj;
obj = obj->CachedParentObject()) {
if (obj->LastKnownIsIncludedInTreeValue() &&
!obj->has_dirty_descendants_) {
fail = true;
break;
}
}
if (fail) {
LOG(ERROR) << "Failed to set dirty bits on some objects in the ancestor"
"chain. Bits set: ";
for (auto* obj = this; obj; obj = obj->CachedParentObject()) {
LOG(ERROR) << "* has_dirty_descendants_: "
<< obj->has_dirty_descendants_
<< " object: " << obj->ToString(true, true);
}
DCHECK(false);
}
}
#endif
}
void AXObject::Init(AXObject* parent) {
#if DCHECK_IS_ON()
DCHECK(!parent_) << "Should not already have a cached parent:"
<< "\n* Child = " << GetNode() << " / " << GetLayoutObject()
<< "\n* Parent = " << parent_->ToString(true, true)
<< "\n* Equal to passed-in parent? " << (parent == parent_);
DCHECK(!is_initializing_);
base::AutoReset<bool> reentrancy_protector(&is_initializing_, true);
#endif // DCHECK_IS_ON()
// The role must be determined immediately.
// Note: in order to avoid reentrancy, the role computation cannot use the
// ParentObject(), although it can use the DOM parent.
role_ = DetermineAccessibilityRole();
#if DCHECK_IS_ON()
DCHECK(IsValidRole(role_)) << "Illegal " << role_ << " for\n"
<< GetNode() << '\n'
<< GetLayoutObject();
HTMLOptGroupElement* optgroup = DynamicTo<HTMLOptGroupElement>(GetNode());
if (optgroup && optgroup->OwnerSelectElement()) {
// We do not currently create accessible objects for an <optgroup> inside of
// a <select size=1>.
// TODO(accessibility) Remove this once we refactor HTML <select> to use
// the shadow DOM and AXNodeObject instead of AXMenuList* classes.
DCHECK(!optgroup->OwnerSelectElement()->UsesMenuList());
}
#endif // DCHECK_IS_ON()
// Determine the parent as soon as possible.
// Every AXObject must have a parent unless it's the root.
SetParent(parent);
DCHECK(parent_ || IsRoot())
<< "The following node should have a parent: " << GetNode();
// The parent cannot have children. This object must be destroyed.
DCHECK(!parent_ || parent_->CanHaveChildren())
<< "Tried to set a parent that cannot have children:"
<< "\n* Parent = " << parent_->ToString(true, true)
<< "\n* Child = " << ToString(true, true);
children_dirty_ = true;
UpdateCachedAttributeValuesIfNeeded(false);
DCHECK(GetDocument()) << "All AXObjects must have a document: "
<< ToString(true, true);
// Set the dirty bit for the root AX object when created. For all other
// objects, this is set by a descendant needing to be updated, and
// AXObjectCacheImpl::UpdateTreeIfNeeded will therefore process an object
// if its parent has has_dirty_descendants_ set. The root, however, has no
// parent, so there is no parent to mark in order to cause the root to update
// itself. Therefore this bit serves a second purpose of determining
// whether AXObjectCacheImpl::UpdateTreeIfNeeded needs to update the root
// object.
if (IsRoot()) {
SetHasDirtyDescendants(true);
} else if (AXObjectCache().UpdatingTree()) {
if (children_dirty_ && LastKnownIsIncludedInTreeValue()) {
// If we're in the updating tree loop, and a new object is created, we
// need to make sure to fill out all descendants of the new object.
SetHasDirtyDescendants(true);
// We also need to tell the new object's parent to iterate through
// all of its children to look for the has dirty descendants flag.
// However, we do not set the flag on higher ancestors since
// they have already been walked by the tree update loop.
if (AXObject* ax_included_parent = ParentObjectIncludedInTree()) {
ax_included_parent->SetHasDirtyDescendants(true);
}
}
} else {
DCHECK(ParentObjectIncludedInTree()->HasDirtyDescendants())
<< "When adding a new child to a parent, and not updating the entire "
"tree from the top down, the included parent must be "
"flagged as having dirty descendants:"
<< "\n* Object: " << ToString(true, true) << "* Included parent: "
<< ParentObjectIncludedInTree()->ToString(true, true);
}
}
void AXObject::Detach() {
// Prevents LastKnown*() methods from returning the wrong values.
cached_is_ignored_ = true;
cached_is_ignored_but_included_in_tree_ = false;
if (IsDetached()) {
// Only mock objects can end up being detached twice, because their owner
// may have needed to detach them when they were detached, but couldn't
// remove them from the object cache yet.
DCHECK(IsMockObject()) << "Object detached twice: " << RoleValue();
return;
}
#if defined(AX_FAIL_FAST_BUILD)
SANITIZER_CHECK(ax_object_cache_);
SANITIZER_CHECK(!ax_object_cache_->IsFrozen())
<< "Do not detach children while the tree is frozen, in order to avoid "
"an object detaching itself in the middle of computing its own "
"accessibility properties.";
SANITIZER_CHECK(!is_adding_children_) << ToString(true, true);
#endif
#if !defined(NDEBUG)
// Facilitates debugging of detached objects by providing info on what it was.
if (!ax_object_cache_->HasBeenDisposed()) {
detached_object_debug_info_ = ToString(true, true);
}
#endif
// Clear any children and call DetachFromParent() on them so that
// no children are left with dangling pointers to their parent.
ClearChildren();
parent_ = nullptr;
ax_object_cache_ = nullptr;
children_dirty_ = false;
has_dirty_descendants_ = false;
id_ = 0;
}
bool AXObject::IsDetached() const {
return !ax_object_cache_;
}
bool AXObject::IsRoot() const {
return GetNode() && GetNode() == &AXObjectCache().GetDocument();
}
void AXObject::SetParent(AXObject* new_parent) const {
// TODO(crbug.com/1353205): Re-enable DCHECK for all platforms.
#if DCHECK_IS_ON() && !BUILDFLAG(IS_CHROMEOS_ASH)
if (!new_parent && !IsRoot()) {
std::ostringstream message;
message << "Parent cannot be null, except at the root."
<< "\nThis: " << ToString(true, true)
<< "\nDOM parent chain , starting at |this->GetNode()|:";
int count = 0;
for (Node* node = GetNode(); node;
node = GetParentNodeForComputeParent(AXObjectCache(), node)) {
message << "\n"
<< (++count) << ". " << node
<< "\n LayoutObject=" << node->GetLayoutObject();
if (AXObject* obj = AXObjectCache().Get(node))
message << "\n " << obj->ToString(true, true);
if (!node->isConnected()) {
break;
}
}
NOTREACHED() << message.str();
}
if (new_parent) {
DCHECK(!new_parent->IsDetached())
<< "Cannot set parent to a detached object:"
<< "\n* Child: " << ToString(true, true)
<< "\n* New parent: " << new_parent->ToString(true, true);
DCHECK(!IsAXInlineTextBox() ||
ui::CanHaveInlineTextBoxChildren(new_parent->RoleValue()))
<< "Unexpected parent of inline text box: " << new_parent->RoleValue();
}
// Check to ensure that if the parent is changing from a previous parent,
// that |this| is not still a child of that one.
// This is similar to the IsParentUnignoredOf() check in
// BlinkAXTreeSource, but closer to where the problem would occur.
if (parent_ && new_parent != parent_ && !parent_->NeedsToUpdateChildren() &&
!parent_->IsDetached()) {
for (const auto& child : parent_->ChildrenIncludingIgnored()) {
DCHECK(child != this) << "Previous parent still has |this| child:\n"
<< ToString(true, true) << " should be a child of "
<< new_parent->ToString(true, true) << " not of "
<< parent_->ToString(true, true);
}
// TODO(accessibility) This should not be reached unless this method is
// called on an AXObject of role kRootWebArea or when the parent's
// children are dirty, aka parent_->NeedsToUpdateChildren());
// Ideally we will also ensure |this| is in the parent's children now, so
// that ClearChildren() can later find the child to detach from the parent.
}
#endif
parent_ = new_parent;
// TODO(accessibility) Is it necessary to do this while updating the tree?
if (AXObjectCache().UpdatingTree()) {
// If updating tree, tell the newly included parent to iterate through
// all of its children to look for the has dirty descendants flag.
// However, we do not set the flag on higher ancestors since
// they have already been walked by the tree update loop.
if (AXObject* ax_included_parent = ParentObjectIncludedInTree()) {
ax_included_parent->SetHasDirtyDescendants(true);
}
} else {
SetAncestorsHaveDirtyDescendants();
}
}
bool AXObject::IsMissingParent() const {
if (!parent_) {
// Do not attempt to repair the ParentObject() of a validation message
// object, because hidden ones are purposely kept around without being in
// the tree, and without a parent, for potential later reuse.
// TODO(accessibility) This is ugly. Consider destroying validation message
// objects between uses instead. See GetOrCreateValidationMessageObject().
return !IsRoot() && !IsValidationMessage();
}
if (parent_->IsDetached())
return true;
return false;
}
void AXObject::RepairMissingParent() const {
DCHECK(IsMissingParent());
DCHECK(!AXObjectCache().HasBeenDisposed());
SetParent(ComputeParent());
SANITIZER_CHECK(!parent_ ||
parent_->RoleValue() != ax::mojom::blink::Role::kIframe ||
RoleValue() == ax::mojom::blink::Role::kDocument)
<< "An iframe can only have a document child."
<< "\n* Child = " << ToString(true, true)
<< "\n* Parent = " << parent_->ToString(true, true);
}
// In many cases, ComputeParent() is not called, because the parent adding
// the parent adding the child will pass itself into AXObjectCacheImpl.
// ComputeParent() is still necessary because some parts of the code,
// especially web tests, result in AXObjects being created in the middle of
// the tree before their parents are created.
// TODO(accessibility) Consider forcing all ax objects to be created from
// the top down, eliminating the need for ComputeParent().
AXObject* AXObject::ComputeParent() const {
AXObject* ax_parent = ComputeParentOrNull();
CHECK(!ax_parent || !ax_parent->IsDetached())
<< "Computed parent should never be detached:"
<< "\n* Child: " << GetNode()
<< "\n* Parent: " << ax_parent->ToString(true, true);
return ax_parent;
}
// Same as ComputeParent, but without the extra check for valid parent in the
// end. This is for use in RestoreParentOrPrune.
AXObject* AXObject::ComputeParentOrNull() const {
#if defined(AX_FAIL_FAST_BUILD)
SANITIZER_CHECK(!IsDetached());
SANITIZER_CHECK(!IsMockObject())
<< "A mock object must have a parent, and cannot exist without one. "
"The parent is set when the object is constructed.";
SANITIZER_CHECK(GetNode() || GetLayoutObject() || IsVirtualObject())
<< "Can't compute parent on AXObjects without a backing Node "
"LayoutObject, "
" or AccessibleNode. Objects without those must set the "
"parent in Init(), |this| = "
<< RoleValue();
#endif
AXObject* ax_parent = nullptr;
if (IsAXInlineTextBox()) {
NOTREACHED()
<< "AXInlineTextBox box tried to compute a new parent, but they are "
"not allowed to exist even temporarily without a parent, as their "
"existence depends on the parent text object. Parent text = "
<< (AXObjectCache().SafeGet(GetNode())
? AXObjectCache().SafeGet(GetNode())->ToString(true, true)
: "");
} else if (AXObjectCache().IsAriaOwned(this)) {
ax_parent = AXObjectCache().ValidatedAriaOwner(this);
} else if (IsVirtualObject()) {
ax_parent =
ComputeAccessibleNodeParent(AXObjectCache(), *GetAccessibleNode());
}
if (!ax_parent) {
ax_parent = ComputeNonARIAParent(AXObjectCache(), GetNode());
}
return ax_parent;
}
// static
Node* AXObject::GetParentNodeForComputeParent(AXObjectCacheImpl& cache,
Node* node) {
if (!node) {
return nullptr;
}
DCHECK(node->isConnected())
<< "Should not call with disconnected node: " << node;
// A document's parent should be the page popup owner, if any, otherwise null.
if (auto* document = DynamicTo<Document>(node)) {
LocalFrame* frame = document->GetFrame();
DCHECK(frame);
Node* popup_owner = frame->PagePopupOwner();
if (!popup_owner) {
return nullptr;
}
// TODO(accessibility) Remove this rule once we stop using AXMenuList*.
if (IsA<HTMLSelectElement>(popup_owner) &&
AXObjectCacheImpl::ShouldCreateAXMenuListFor(
popup_owner->GetLayoutObject())) {
return nullptr;
}