-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
ax_node_object.cc
6100 lines (5311 loc) · 223 KB
/
ax_node_object.cc
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) 2012, Google 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_node_object.h"
#include <math.h>
#include <algorithm>
#include <memory>
#include <queue>
#include "base/auto_reset.h"
#include "base/containers/fixed_flat_set.h"
#include "base/numerics/safe_conversions.h"
#include "third_party/abseil-cpp/absl/types/optional.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/mojom/frame/user_activation_notification_type.mojom-blink.h"
#include "third_party/blink/public/strings/grit/blink_strings.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_image_bitmap_options.h"
#include "third_party/blink/renderer/core/aom/accessible_node.h"
#include "third_party/blink/renderer/core/css/css_resolution_units.h"
#include "third_party/blink/renderer/core/css/properties/longhands.h"
#include "third_party/blink/renderer/core/display_lock/display_lock_utilities.h"
#include "third_party/blink/renderer/core/dom/css_toggle_inference.h"
#include "third_party/blink/renderer/core/dom/flat_tree_traversal.h"
#include "third_party/blink/renderer/core/dom/layout_tree_builder_traversal.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/dom/node_traversal.h"
#include "third_party/blink/renderer/core/dom/qualified_name.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/dom/text.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/editing/markers/custom_highlight_marker.h"
#include "third_party/blink/renderer/core/editing/markers/document_marker_controller.h"
#include "third_party/blink/renderer/core/editing/position.h"
#include "third_party/blink/renderer/core/events/event_util.h"
#include "third_party/blink/renderer/core/events/keyboard_event.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.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/highlight/highlight.h"
#include "third_party/blink/renderer/core/html/canvas/html_canvas_element.h"
#include "third_party/blink/renderer/core/html/canvas/image_data.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_button_element.h"
#include "third_party/blink/renderer/core/html/forms/html_field_set_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_label_element.h"
#include "third_party/blink/renderer/core/html/forms/html_legend_element.h"
#include "third_party/blink/renderer/core/html/forms/html_opt_group_element.h"
#include "third_party/blink/renderer/core/html/forms/html_option_element.h"
#include "third_party/blink/renderer/core/html/forms/html_select_element.h"
#include "third_party/blink/renderer/core/html/forms/html_select_menu_element.h"
#include "third_party/blink/renderer/core/html/forms/html_text_area_element.h"
#include "third_party/blink/renderer/core/html/forms/labels_node_list.h"
#include "third_party/blink/renderer/core/html/forms/radio_input_type.h"
#include "third_party/blink/renderer/core/html/forms/text_control_element.h"
#include "third_party/blink/renderer/core/html/html_anchor_element.h"
#include "third_party/blink/renderer/core/html/html_div_element.h"
#include "third_party/blink/renderer/core/html/html_dlist_element.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/html/html_frame_element_base.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_meter_element.h"
#include "third_party/blink/renderer/core/html/html_plugin_element.h"
#include "third_party/blink/renderer/core/html/html_slot_element.h"
#include "third_party/blink/renderer/core/html/html_table_caption_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/media/html_media_element.h"
#include "third_party/blink/renderer/core/html/media/html_video_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/html_names.h"
#include "third_party/blink/renderer/core/imagebitmap/image_bitmap.h"
#include "third_party/blink/renderer/core/input_type_names.h"
#include "third_party/blink/renderer/core/layout/layout_block_flow.h"
#include "third_party/blink/renderer/core/layout/layout_box_model_object.h"
#include "third_party/blink/renderer/core/layout/layout_inline.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/layout/ng/inline/ng_abstract_inline_text_box.h"
#include "third_party/blink/renderer/core/layout/ng/inline/ng_inline_node.h"
#include "third_party/blink/renderer/core/layout/ng/inline/ng_offset_mapping.h"
#include "third_party/blink/renderer/core/loader/progress_tracker.h"
#include "third_party/blink/renderer/core/mathml/mathml_element.h"
#include "third_party/blink/renderer/core/mathml_names.h"
#include "third_party/blink/renderer/core/navigation_api/navigation_api.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/style/computed_style_constants.h"
#include "third_party/blink/renderer/core/svg/svg_desc_element.h"
#include "third_party/blink/renderer/core/svg/svg_element.h"
#include "third_party/blink/renderer/core/svg/svg_symbol_element.h"
#include "third_party/blink/renderer/core/svg/svg_text_element.h"
#include "third_party/blink/renderer/core/svg/svg_title_element.h"
#include "third_party/blink/renderer/core/xlink_names.h"
#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_layout_object.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_position.h"
#include "third_party/blink/renderer/modules/accessibility/ax_range.h"
#include "third_party/blink/renderer/modules/accessibility/ax_relation_cache.h"
#include "third_party/blink/renderer/platform/graphics/image_data_buffer.h"
#include "third_party/blink/renderer/platform/keyboard_codes.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/text/text_direction.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/skia/include/core/SkImage.h"
#include "ui/accessibility/ax_common.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 {
// It is not easily possible to find out if an element is the target of an
// in-page link.
// As a workaround, we consider the following to be potential targets:
// - <a name>
// - <foo id> -- an element with an id that is not SVG, a <label> or <optgroup>.
// <label> does not make much sense as an in-page link target.
// Exposing <optgroup> is redundant, as the group is already exposed via a
// child in its shadow DOM, which contains the accessible name.
// #document -- this is always a potential link target via <a name="#">.
// This is a compromise that does not include too many elements, and
// has minimal impact on tests.
bool IsPotentialInPageLinkTarget(blink::Node& node) {
auto* element = blink::DynamicTo<blink::Element>(&node);
if (!element) {
// The document itself is a potential link target, e.g. via <a name="#">.
return blink::IsA<blink::Document>(node);
}
// We exclude elements that are in the shadow DOM. They cannot be linked by a
// document fragment from the main page:as they have their own id namespace.
if (element->ContainingShadowRoot())
return false;
// SVG elements are unlikely link targets, and we want to avoid creating
// a lot of noise in the AX tree or breaking tests unnecessarily.
if (element->IsSVGElement())
return false;
// <a name>
if (auto* anchor = blink::DynamicTo<blink::HTMLAnchorElement>(element)) {
if (anchor->HasName())
return true;
}
// <foo id> not in an <optgroup> or <label>.
if (element->HasID() && !blink::IsA<blink::HTMLLabelElement>(element) &&
!blink::IsA<blink::HTMLOptGroupElement>(element)) {
return true;
}
return false;
}
bool IsNeutralWithinTable(blink::AXObject* obj) {
if (!obj)
return false;
ax::mojom::blink::Role role = obj->RoleValue();
return role == ax::mojom::blink::Role::kGroup ||
role == ax::mojom::blink::Role::kGenericContainer ||
role == ax::mojom::blink::Role::kRowGroup;
}
// Within a table, provide the accessible, semantic parent of |node|,
// by traversing the DOM tree, ignoring elements that are neutral in a table.
// Return the AXObject for the ancestor.
blink::AXObject* GetDOMTableAXAncestor(blink::Node* node,
blink::AXObjectCacheImpl& cache) {
// Used by code to determine roles of elements inside of an HTML table,
// Use DOM to get parent since parent_ is not initialized yet when role is
// being computed, and because HTML table structure should not take into
// account aria-owns.
if (!node)
return nullptr;
while (true) {
node = blink::LayoutTreeBuilderTraversal::Parent(*node);
if (!node)
return nullptr;
blink::AXObject* ax_object = cache.GetOrCreate(node);
if (ax_object && !IsNeutralWithinTable(ax_object))
return ax_object;
}
}
enum class AXAction {
kActionIncrement = 0,
kActionDecrement,
};
blink::KeyboardEvent* CreateKeyboardEvent(
blink::LocalDOMWindow* local_dom_window,
blink::WebInputEvent::Type type,
AXAction action,
blink::AccessibilityOrientation orientation,
ax::mojom::blink::WritingDirection text_direction) {
blink::WebKeyboardEvent key(type,
blink::WebInputEvent::Modifiers::kNoModifiers,
base::TimeTicks::Now());
if (action == AXAction::kActionIncrement) {
if (orientation == blink::kAccessibilityOrientationVertical) {
key.dom_key = ui::DomKey::ARROW_UP;
key.dom_code = static_cast<int>(ui::DomCode::ARROW_UP);
key.native_key_code = key.windows_key_code = blink::VKEY_UP;
} else if (text_direction == ax::mojom::blink::WritingDirection::kRtl) {
key.dom_key = ui::DomKey::ARROW_LEFT;
key.dom_code = static_cast<int>(ui::DomCode::ARROW_LEFT);
key.native_key_code = key.windows_key_code = blink::VKEY_LEFT;
} else { // horizontal and left to right
key.dom_key = ui::DomKey::ARROW_RIGHT;
key.dom_code = static_cast<int>(ui::DomCode::ARROW_RIGHT);
key.native_key_code = key.windows_key_code = blink::VKEY_RIGHT;
}
} else if (action == AXAction::kActionDecrement) {
if (orientation == blink::kAccessibilityOrientationVertical) {
key.dom_key = ui::DomKey::ARROW_DOWN;
key.dom_code = static_cast<int>(ui::DomCode::ARROW_DOWN);
key.native_key_code = key.windows_key_code = blink::VKEY_DOWN;
} else if (text_direction == ax::mojom::blink::WritingDirection::kRtl) {
key.dom_key = ui::DomKey::ARROW_RIGHT;
key.dom_code = static_cast<int>(ui::DomCode::ARROW_RIGHT);
key.native_key_code = key.windows_key_code = blink::VKEY_RIGHT;
} else { // horizontal and left to right
key.dom_key = ui::DomKey::ARROW_LEFT;
key.dom_code = static_cast<int>(ui::DomCode::ARROW_LEFT);
key.native_key_code = key.windows_key_code = blink::VKEY_LEFT;
}
}
return blink::KeyboardEvent::Create(key, local_dom_window, true);
}
unsigned TextStyleFlag(ax::mojom::blink::TextStyle text_style_enum) {
return static_cast<unsigned>(1 << static_cast<int>(text_style_enum));
}
ax::mojom::blink::TextDecorationStyle
TextDecorationStyleToAXTextDecorationStyle(
const blink::ETextDecorationStyle text_decoration_style) {
switch (text_decoration_style) {
case blink::ETextDecorationStyle::kDashed:
return ax::mojom::blink::TextDecorationStyle::kDashed;
case blink::ETextDecorationStyle::kSolid:
return ax::mojom::blink::TextDecorationStyle::kSolid;
case blink::ETextDecorationStyle::kDotted:
return ax::mojom::blink::TextDecorationStyle::kDotted;
case blink::ETextDecorationStyle::kDouble:
return ax::mojom::blink::TextDecorationStyle::kDouble;
case blink::ETextDecorationStyle::kWavy:
return ax::mojom::blink::TextDecorationStyle::kWavy;
}
NOTREACHED();
return ax::mojom::blink::TextDecorationStyle::kNone;
}
String GetTitle(blink::Element* element) {
if (!element)
return String();
if (blink::SVGElement* svg_element =
blink::DynamicTo<blink::SVGElement>(element)) {
// Don't use title() in SVG, as it calls innerText() which updates layout.
// Unfortunately, this must duplicate some logic from SVGElement::title().
if (svg_element->InUseShadowTree()) {
String title = GetTitle(svg_element->OwnerShadowHost());
if (!title.empty())
return title;
}
// If we aren't an instance in a <use> or the <use> title was not found,
// then find the first <title> child of this element. If a title child was
// found, return the text contents.
if (auto* title_element =
blink::Traversal<blink::SVGTitleElement>::FirstChild(*element)) {
return title_element->GetInnerTextWithoutUpdate();
}
return String();
}
return element->title();
}
} // namespace
namespace blink {
using html_names::kAltAttr;
using html_names::kTitleAttr;
using html_names::kTypeAttr;
using html_names::kValueAttr;
// In ARIA 1.1, default value of aria-level was changed to 2.
const int kDefaultHeadingLevel = 2;
AXNodeObject::AXNodeObject(Node* node, AXObjectCacheImpl& ax_object_cache)
: AXObject(ax_object_cache),
native_role_(ax::mojom::blink::Role::kUnknown),
aria_role_(ax::mojom::blink::Role::kUnknown),
node_(node) {}
AXNodeObject::~AXNodeObject() {
DCHECK(!node_);
}
void AXNodeObject::AlterSliderOrSpinButtonValue(bool increase) {
if (!GetNode())
return;
if (!IsSlider() && !IsSpinButton())
return;
float value;
if (!ValueForRange(&value))
return;
if (!RuntimeEnabledFeatures::
SynthesizedKeyboardEventsForAccessibilityActionsEnabled()) {
// If synthesized keyboard events are disabled, we need to set the value
// directly here.
// If no step was provided on the element, use a default value.
float step;
if (!StepValueForRange(&step)) {
if (IsNativeSlider() || IsNativeSpinButton()) {
step = StepRange().Step().ToString().ToFloat();
} else {
return;
}
}
value += increase ? step : -step;
if (native_role_ == ax::mojom::blink::Role::kSlider ||
native_role_ == ax::mojom::blink::Role::kSpinButton) {
OnNativeSetValueAction(String::Number(value));
// Dispatching an event could result in changes to the document, like
// this AXObject becoming detached.
if (IsDetached())
return;
AXObjectCache().HandleValueChanged(GetNode());
return;
}
}
// If we have synthesized keyboard events enabled, we generate a keydown
// event:
// * For a native slider, the dispatch of the event will reach
// RangeInputType::HandleKeydownEvent(), where the value will be set and the
// AXObjectCache notified. The corresponding keydown/up JS events will be
// fired so the website doesn't know it was produced by an AT action.
// * For an ARIA slider, the corresponding keydown/up JS events will be
// fired. It is expected that the handlers for those events manage the
// update of the slider value.
AXAction action =
increase ? AXAction::kActionIncrement : AXAction::kActionDecrement;
LocalDOMWindow* local_dom_window = GetDocument()->domWindow();
AccessibilityOrientation orientation = Orientation();
ax::mojom::blink::WritingDirection text_direction = GetTextDirection();
// A kKeyDown event is kRawKeyDown + kChar events. We cannot synthesize it
// because the KeyboardEvent constructor will prevent it, to force us to
// decide if we must produce both events. In our case, we don't have to
// produce a kChar event because we are synthesizing arrow key presses, and
// only keys that output characters are expected to produce kChar events.
KeyboardEvent* keydown =
CreateKeyboardEvent(local_dom_window, WebInputEvent::Type::kRawKeyDown,
action, orientation, text_direction);
GetNode()->DispatchEvent(*keydown);
// The keydown handler may have caused the node to be removed.
if (!GetNode())
return;
KeyboardEvent* keyup =
CreateKeyboardEvent(local_dom_window, WebInputEvent::Type::kKeyUp, action,
orientation, text_direction);
// Add a 100ms delay between keydown and keyup to make events look less
// evidently synthesized.
GetDocument()
->GetTaskRunner(TaskType::kUserInteraction)
->PostDelayedTask(
FROM_HERE,
WTF::BindOnce(
[](Node* node, KeyboardEvent* evt) {
if (node) {
node->DispatchEvent(*evt);
}
},
WrapWeakPersistent(GetNode()), WrapPersistent(keyup)),
base::Milliseconds(100));
}
AXObject* AXNodeObject::ActiveDescendant() {
Element* element = GetElement();
if (!element)
return nullptr;
Element* descendant =
GetAOMPropertyOrARIAAttribute(AOMRelationProperty::kActiveDescendant);
if (!descendant)
return nullptr;
AXObject* ax_descendant = AXObjectCache().GetOrCreate(descendant);
return ax_descendant && ax_descendant->IsVisible() ? ax_descendant : nullptr;
}
AXObjectInclusion AXNodeObject::ShouldIncludeBasedOnSemantics(
IgnoredReasons* ignored_reasons) const {
DCHECK(GetDocument());
// All nodes must have an unignored parent within their tree under
// the root node of the web area, so force that node to always be unignored.
if (IsA<Document>(GetNode())) {
return kIncludeObject;
}
if (IsPresentational()) {
if (ignored_reasons)
ignored_reasons->push_back(IgnoredReason(kAXPresentational));
return kIgnoreObject;
}
// Objects inside a portal should be ignored. Portals don't directly expose
// their contents as the contents are not focusable (portals do not currently
// support input events). Portals do use their contents to compute a default
// accessible name.
if (GetDocument()->GetPage() && GetDocument()->GetPage()->InsidePortal())
return kIgnoreObject;
Node* node = GetNode();
if (!node) {
// Nodeless pseudo element images are included, even if they don't have CSS
// alt text. This can allow auto alt to be applied to them.
if (IsImage())
return kIncludeObject;
return kDefaultBehavior;
}
// Avoid double speech. The ruby text describes pronunciation of the ruby
// base, and generally produces redundant screen reader output. Expose it only
// as a description on the <ruby> element so that screen reader users can
// toggle it on/off as with other descriptions/annotations.
if (RoleValue() == ax::mojom::blink::Role::kRubyAnnotation ||
(RoleValue() == ax::mojom::blink::Role::kStaticText && ParentObject() &&
ParentObject()->RoleValue() ==
ax::mojom::blink::Role::kRubyAnnotation)) {
return kIgnoreObject;
}
Element* element = GetElement();
if (!element) {
return kDefaultBehavior;
}
if (IsExcludedByFormControlsFilter()) {
if (ignored_reasons) {
ignored_reasons->push_back(IgnoredReason(kAXUninteresting));
}
return kIgnoreObject;
}
if (IsA<SVGElement>(node)) {
// The symbol element is used to define graphical templates which can be
// instantiated by a use element but which are not rendered directly. We
// don't want to include these template objects, or their subtrees, where
// they appear in the DOM. Any associated semantic information (e.g. the
// title child of a symbol) may participate in the text alternative
// computation where it is instantiated by the use element.
// https://svgwg.org/svg2-draft/struct.html#SymbolElement
if (Traversal<SVGSymbolElement>::FirstAncestorOrSelf(*node))
return kIgnoreObject;
// The SVG-AAM states that user agents MUST provide an accessible object
// for rendered SVG elements that have at least one direct child title or
// desc element that is not empty after trimming whitespace. But it also
// says, "User agents MAY include elements with these child elements without
// checking for valid text content." So just check for their existence in
// order to be performant. https://w3c.github.io/svg-aam/#include_elements
if (ElementTraversal::FirstChild(
*To<ContainerNode>(node), [](auto& element) {
return element.HasTagName(svg_names::kTitleTag) ||
element.HasTagName(svg_names::kDescTag);
})) {
return kIncludeObject;
}
// If setting enabled, do not ignore SVG grouping (<g>) elements.
if (IsA<SVGGElement>(node)) {
Settings* settings = GetDocument()->GetSettings();
if (settings->GetAccessibilityIncludeSvgGElement()) {
return kIncludeObject;
}
}
// If we return kDefaultBehavior here, the logic related to inclusion of
// clickable objects, links, controls, etc. will not be reached. We handle
// SVG elements early to ensure properties in a <symbol> subtree do not
// result in inclusion.
}
if (IsTableLikeRole() || IsTableRowLikeRole() || IsTableCellLikeRole())
return kIncludeObject;
if (IsA<HTMLHtmlElement>(node)) {
if (ignored_reasons) {
ignored_reasons->push_back(IgnoredReason(kAXUninteresting));
}
return kIgnoreObject;
}
// All focusable elements except the <body> and <html> are included.
if (!IsA<HTMLBodyElement>(node) && CanSetFocusAttribute())
return kIncludeObject;
if (IsLink())
return kIncludeObject;
// A click handler might be placed on an otherwise ignored non-empty block
// element, e.g. a div. We shouldn't ignore such elements because if an AT
// sees the |ax::mojom::blink::DefaultActionVerb::kClickAncestor|, it will
// look for the clickable ancestor and it expects to find one.
if (IsClickable())
return kIncludeObject;
if (IsHeading())
return kIncludeObject;
// Header and footer tags may also be exposed as landmark roles but not
// always.
if (node->HasTagName(html_names::kHeaderTag) ||
node->HasTagName(html_names::kFooterTag))
return kIncludeObject;
// All controls are accessible.
if (IsControl())
return kIncludeObject;
// Anything with an explicit ARIA role should be included.
if (AriaRoleAttribute() != ax::mojom::blink::Role::kUnknown)
return kIncludeObject;
// Anything with CSS alt should be included.
// Descendants are pruned: IsRelevantPseudoElementDescendant() returns false.
// Note: this is duplicated from AXLayoutObject because CSS alt text may apply
// to both Elements and pseudo-elements.
absl::optional<String> alt_text = GetCSSAltText(GetNode());
if (alt_text && !alt_text->empty())
return kIncludeObject;
// Don't ignored legends, because JAWS uses them to determine redundant text.
if (IsA<HTMLLegendElement>(node))
return kIncludeObject;
// Anything that is an editable root should not be ignored. However, one
// cannot just call `AXObject::IsEditable()` since that will include the
// contents of an editable region too. Only the editable root should always be
// exposed.
if (IsEditableRoot())
return kIncludeObject;
static constexpr auto always_included_computed_roles =
base::MakeFixedFlatSet<ax::mojom::blink::Role>({
ax::mojom::blink::Role::kAbbr,
ax::mojom::blink::Role::kApplication,
ax::mojom::blink::Role::kArticle,
ax::mojom::blink::Role::kAudio,
ax::mojom::blink::Role::kBanner,
ax::mojom::blink::Role::kBlockquote,
ax::mojom::blink::Role::kComplementary,
ax::mojom::blink::Role::kContentDeletion,
ax::mojom::blink::Role::kContentInfo,
ax::mojom::blink::Role::kContentInsertion,
ax::mojom::blink::Role::kDescriptionList,
ax::mojom::blink::Role::kDescriptionListDetail,
ax::mojom::blink::Role::kDescriptionListTerm,
ax::mojom::blink::Role::kDetails,
ax::mojom::blink::Role::kDialog,
ax::mojom::blink::Role::kDocAcknowledgments,
ax::mojom::blink::Role::kDocAfterword,
ax::mojom::blink::Role::kDocAppendix,
ax::mojom::blink::Role::kDocBibliography,
ax::mojom::blink::Role::kDocChapter,
ax::mojom::blink::Role::kDocConclusion,
ax::mojom::blink::Role::kDocCredits,
ax::mojom::blink::Role::kDocEndnotes,
ax::mojom::blink::Role::kDocEpilogue,
ax::mojom::blink::Role::kDocErrata,
ax::mojom::blink::Role::kDocForeword,
ax::mojom::blink::Role::kDocGlossary,
ax::mojom::blink::Role::kDocIntroduction,
ax::mojom::blink::Role::kDocPart,
ax::mojom::blink::Role::kDocPreface,
ax::mojom::blink::Role::kDocPrologue,
ax::mojom::blink::Role::kDocToc,
ax::mojom::blink::Role::kFigcaption,
ax::mojom::blink::Role::kFigure,
ax::mojom::blink::Role::kFooter,
ax::mojom::blink::Role::kForm,
ax::mojom::blink::Role::kHeader,
ax::mojom::blink::Role::kList,
ax::mojom::blink::Role::kListItem,
ax::mojom::blink::Role::kMain,
ax::mojom::blink::Role::kMark,
ax::mojom::blink::Role::kMath,
ax::mojom::blink::Role::kMathMLMath,
// Don't ignore MathML nodes by default, since MathML
// relies on child positions to determine semantics
// (e.g. numerator is the first child of a fraction).
ax::mojom::blink::Role::kMathMLFraction,
ax::mojom::blink::Role::kMathMLIdentifier,
ax::mojom::blink::Role::kMathMLMultiscripts,
ax::mojom::blink::Role::kMathMLNoneScript,
ax::mojom::blink::Role::kMathMLNumber,
ax::mojom::blink::Role::kMathMLOperator,
ax::mojom::blink::Role::kMathMLOver,
ax::mojom::blink::Role::kMathMLPrescriptDelimiter,
ax::mojom::blink::Role::kMathMLRoot,
ax::mojom::blink::Role::kMathMLRow,
ax::mojom::blink::Role::kMathMLSquareRoot,
ax::mojom::blink::Role::kMathMLStringLiteral,
ax::mojom::blink::Role::kMathMLSub,
ax::mojom::blink::Role::kMathMLSubSup,
ax::mojom::blink::Role::kMathMLSup,
ax::mojom::blink::Role::kMathMLTable,
ax::mojom::blink::Role::kMathMLTableCell,
ax::mojom::blink::Role::kMathMLTableRow,
ax::mojom::blink::Role::kMathMLText,
ax::mojom::blink::Role::kMathMLUnder,
ax::mojom::blink::Role::kMathMLUnderOver,
ax::mojom::blink::Role::kMeter,
ax::mojom::blink::Role::kNavigation,
ax::mojom::blink::Role::kPluginObject,
ax::mojom::blink::Role::kProgressIndicator,
ax::mojom::blink::Role::kRegion,
ax::mojom::blink::Role::kRuby,
ax::mojom::blink::Role::kSearch,
ax::mojom::blink::Role::kSection,
ax::mojom::blink::Role::kSplitter,
ax::mojom::blink::Role::kSubscript,
ax::mojom::blink::Role::kSuperscript,
ax::mojom::blink::Role::kTime,
ax::mojom::blink::Role::kVideo,
});
if (always_included_computed_roles.find(RoleValue()) !=
always_included_computed_roles.end())
return kIncludeObject;
// Using the title or accessibility description (so we
// check if there's some kind of accessible name for the element)
// to decide an element's visibility is not as definitive as
// previous checks, so this should remain as one of the last.
if (HasAriaAttribute() || !GetAttribute(kTitleAttr).empty())
return kIncludeObject;
if (IsImage() && !IsA<SVGElement>(node)) {
String alt = GetAttribute(kAltAttr);
// A null alt attribute means the attribute is not present. We assume this
// is a mistake, and expose the image so that it can be repaired.
// In contrast, alt="" is treated as intentional markup to ignore the image.
if (!alt.empty() || alt.IsNull())
return kIncludeObject;
if (ignored_reasons)
ignored_reasons->push_back(IgnoredReason(kAXEmptyAlt));
return kIgnoreObject;
}
// Process potential in-page link targets
if (IsPotentialInPageLinkTarget(*element))
return kIncludeObject;
// <span> tags are inline tags and not meant to convey information if they
// have no other ARIA information on them. If we don't ignore them, they may
// emit signals expected to come from their parent.
if (IsA<HTMLSpanElement>(node)) {
if (ignored_reasons)
ignored_reasons->push_back(IgnoredReason(kAXUninteresting));
return kIgnoreObject;
}
// Ignore labels that are already used to name a control.
// See IsRedundantLabel() for more commentary.
if (HTMLLabelElement* label = DynamicTo<HTMLLabelElement>(node)) {
if (IsRedundantLabel(label)) {
if (ignored_reasons) {
ignored_reasons->push_back(
IgnoredReason(kAXLabelFor, AXObjectCache().Get(label->control())));
}
return kIgnoreObject;
}
return kIncludeObject;
}
// The SVG-AAM says the foreignObject element is normally presentational.
if (IsA<SVGForeignObjectElement>(node)) {
if (ignored_reasons) {
ignored_reasons->push_back(IgnoredReason(kAXPresentational));
}
return kIgnoreObject;
}
return kDefaultBehavior;
}
bool AXNodeObject::ComputeAccessibilityIsIgnored(
IgnoredReasons* ignored_reasons) const {
if (AXObject::ComputeAccessibilityIsIgnored(ignored_reasons)) {
// Fallback elements inside of a <canvas> are invisible, but are not ignored
// if they are semantic and not aria-hidden or hidden via style.
if (IsAriaHidden() || IsHiddenViaStyle() || !GetNode()->parentElement() ||
!GetNode()->parentElement()->IsInCanvasSubtree()) {
return true;
}
}
// Handle content that is either visible or in a canvas subtree.
AXObjectInclusion include = ShouldIncludeBasedOnSemantics(ignored_reasons);
if (include == kIgnoreObject) {
return true;
}
if (include == kDefaultBehavior && !IsA<Text>(GetNode())) {
if (ignored_reasons) {
ignored_reasons->push_back(IgnoredReason(kAXUninteresting));
}
return true;
}
return false;
}
// static
absl::optional<String> AXNodeObject::GetCSSAltText(const Node* node) {
// CSS alt text rules allow text to be assigned to ::before/::after content.
// For example, the following CSS assigns "bullet" text to bullet.png:
// .something::before {
// content: url(bullet.png) / "bullet";
// }
if (!node || !node->GetComputedStyle() ||
node->GetComputedStyle()->ContentBehavesAsNormal()) {
return absl::nullopt;
}
const ComputedStyle* style = node->GetComputedStyle();
if (node->IsPseudoElement()) {
for (const ContentData* content_data = style->GetContentData();
content_data; content_data = content_data->Next()) {
if (content_data->IsAltText())
return To<AltTextContentData>(content_data)->GetText();
}
return absl::nullopt;
}
// If the content property is used on a non-pseudo element, match the
// behaviour of LayoutObject::CreateObject and only honour the style if
// there is exactly one piece of content, which is an image.
const ContentData* content_data = style->GetContentData();
if (content_data && content_data->IsImage() && content_data->Next() &&
content_data->Next()->IsAltText()) {
return To<AltTextContentData>(content_data->Next())->GetText();
}
return absl::nullopt;
}
// The following lists are for deciding whether the tags aside,
// header and footer can be interpreted as roles complementary, banner and
// contentInfo or if they should be interpreted as generic. This list
// includes all roles that correspond to the html sectioning content elements.
static HashSet<ax::mojom::blink::Role>& GetLandmarkIsNotAllowedAncestorRoles(
ax::mojom::blink::Role landmark) {
// clang-format off
DEFINE_STATIC_LOCAL(
HashSet<ax::mojom::blink::Role>, sectioning_content_roles,
({
ax::mojom::blink::Role::kArticle,
ax::mojom::blink::Role::kComplementary,
ax::mojom::blink::Role::kMain,
ax::mojom::blink::Role::kNavigation,
ax::mojom::blink::Role::kSection
}));
// clang-format on
DEFINE_STATIC_LOCAL(HashSet<ax::mojom::blink::Role>,
aside_is_not_allowed_roles, ());
// Main can contain complementary element but not header or footer.
if (landmark == ax::mojom::blink::Role::kComplementary) {
if (aside_is_not_allowed_roles.empty()) {
for (const auto& role : sectioning_content_roles) {
if (role != ax::mojom::blink::Role::kMain) {
aside_is_not_allowed_roles.insert(role);
}
}
}
return aside_is_not_allowed_roles;
}
return sectioning_content_roles;
}
bool AXNodeObject::IsDescendantOfLandmarkDisallowedElement() const {
if (!GetNode())
return false;
auto role_names = GetLandmarkIsNotAllowedAncestorRoles(RoleValue());
for (AXObject* parent = ParentObjectUnignored(); parent;
parent = parent->ParentObjectUnignored()) {
if (role_names.Contains(parent->RoleValue())) {
return true;
}
}
return false;
}
static bool IsNonEmptyNonHeaderCell(const Node* cell) {
return cell && cell->hasChildren() && cell->HasTagName(html_names::kTdTag);
}
static bool IsHeaderCell(const Node* cell) {
return cell && cell->HasTagName(html_names::kThTag);
}
static ax::mojom::blink::Role DecideRoleFromSiblings(Element* cell) {
// If this header is only cell in its row, it is a column header.
// It is also a column header if it has a header on either side of it.
// If instead it has a non-empty td element next to it, it is a row header.
const Node* next_cell = LayoutTreeBuilderTraversal::NextSibling(*cell);
const Node* previous_cell =
LayoutTreeBuilderTraversal::PreviousSibling(*cell);
if (!next_cell && !previous_cell)
return ax::mojom::blink::Role::kColumnHeader;
if (IsHeaderCell(next_cell) && IsHeaderCell(previous_cell))
return ax::mojom::blink::Role::kColumnHeader;
if (IsNonEmptyNonHeaderCell(next_cell) ||
IsNonEmptyNonHeaderCell(previous_cell))
return ax::mojom::blink::Role::kRowHeader;
const auto* row = DynamicTo<HTMLTableRowElement>(cell->parentNode());
if (!row)
return ax::mojom::blink::Role::kColumnHeader;
// If this row's first or last cell is a non-empty td, this is a row header.
// Do the same check for the second and second-to-last cells because tables
// often have an empty cell at the intersection of the row and column headers.
const Element* first_cell = ElementTraversal::FirstChild(*row);
DCHECK(first_cell);
const Element* last_cell = ElementTraversal::LastChild(*row);
DCHECK(last_cell);
if (IsNonEmptyNonHeaderCell(first_cell) || IsNonEmptyNonHeaderCell(last_cell))
return ax::mojom::blink::Role::kRowHeader;
if (IsNonEmptyNonHeaderCell(ElementTraversal::NextSibling(*first_cell)) ||
IsNonEmptyNonHeaderCell(ElementTraversal::PreviousSibling(*last_cell)))
return ax::mojom::blink::Role::kRowHeader;
// We have no evidence that this is not a column header.
return ax::mojom::blink::Role::kColumnHeader;
}
ax::mojom::blink::Role AXNodeObject::DetermineTableSectionRole() const {
if (!GetElement())
return ax::mojom::blink::Role::kGenericContainer;
AXObject* parent = GetDOMTableAXAncestor(GetNode(), AXObjectCache());
if (!parent || !parent->IsTableLikeRole())
return ax::mojom::blink::Role::kGenericContainer;
if (parent->RoleValue() == ax::mojom::blink::Role::kLayoutTable)
return ax::mojom::blink::Role::kGenericContainer;
return ax::mojom::blink::Role::kRowGroup;
}
ax::mojom::blink::Role AXNodeObject::DetermineTableRowRole() const {
AXObject* parent = GetDOMTableAXAncestor(GetNode(), AXObjectCache());
if (!parent || !parent->IsTableLikeRole())
return ax::mojom::blink::Role::kGenericContainer;
if (parent->RoleValue() == ax::mojom::blink::Role::kLayoutTable)
return ax::mojom::blink::Role::kLayoutTableRow;
return ax::mojom::blink::Role::kRow;
}
ax::mojom::blink::Role AXNodeObject::DetermineTableCellRole() const {
AXObject* parent = GetDOMTableAXAncestor(GetNode(), AXObjectCache());
if (!parent || !parent->IsTableRowLikeRole())
return ax::mojom::blink::Role::kGenericContainer;
// Ensure table container.
AXObject* grandparent =
GetDOMTableAXAncestor(parent->GetNode(), AXObjectCache());
if (!grandparent || !grandparent->IsTableLikeRole())
return ax::mojom::blink::Role::kGenericContainer;
if (parent->RoleValue() == ax::mojom::blink::Role::kLayoutTableRow)
return ax::mojom::blink::Role::kLayoutTableCell;
if (!GetElement() || !GetNode()->HasTagName(html_names::kThTag))
return ax::mojom::blink::Role::kCell;
const AtomicString& scope = GetAttribute(html_names::kScopeAttr);
if (EqualIgnoringASCIICase(scope, "row") ||
EqualIgnoringASCIICase(scope, "rowgroup"))
return ax::mojom::blink::Role::kRowHeader;
if (EqualIgnoringASCIICase(scope, "col") ||
EqualIgnoringASCIICase(scope, "colgroup"))
return ax::mojom::blink::Role::kColumnHeader;
return DecideRoleFromSiblings(GetElement());
}
ax::mojom::blink::Role AXNodeObject::RoleFromLayoutObjectOrNode() const {
return ax::mojom::blink::Role::kGenericContainer;
}
// Does not check ARIA role, but does check some ARIA properties, specifically
// @aria-haspopup/aria-pressed via ButtonType().
// TODO(accessibility) Ensure that if the native role needs to change, that the
// object is destroyed and a new one is created. Examples are changes to
// IsClickable(), DataList(), aria-pressed, the parent's tag, @role.
ax::mojom::blink::Role AXNodeObject::NativeRoleIgnoringAria() const {
if (!GetNode()) {
// Can be null in the case of pseudo content.
return RoleFromLayoutObjectOrNode();
}
if (GetNode()->IsPseudoElement() && GetCSSAltText(GetNode())) {
const ComputedStyle* style = GetNode()->GetComputedStyle();
ContentData* content_data = style->GetContentData();
// We just check the first item of the content list to determine the
// appropriate role, should only ever be image or text.
// TODO(accessibility) Is it possible to use CSS alt text on an HTML tag
// with strong semantics? If so, why are we overriding the role here?
// We only need to ensure the accessible name gets the CSS alt text.
// Note: by doing this, we are often hiding child pseudo element content
// because IsRelevantPseudoElementDescendant() returns false when an
// ancestor has CSS alt text.
if (content_data->IsImage())
return ax::mojom::blink::Role::kImage;
return ax::mojom::blink::Role::kStaticText;
}
if (GetNode()->IsTextNode())