-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathAXCoreObject.cpp
More file actions
2200 lines (1914 loc) · 76.4 KB
/
AXCoreObject.cpp
File metadata and controls
2200 lines (1914 loc) · 76.4 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) 2023-2025 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 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 "config.h"
#include "AXCoreObject.h"
#include "AXLoggerBase.h"
#include "AXObjectCache.h"
#include "AXSearchManager.h"
#include "AXTreeStoreInlines.h"
#include "AXUtilities.h"
#include "DocumentView.h"
#include "HTMLAreaElement.h"
#include "LocalFrameView.h"
#include "Logging.h"
#include "RenderObjectStyle.h"
#include "RenderStyle+GettersInlines.h"
#include "Settings.h"
#include "TextDecorationPainter.h"
#include <wtf/Deque.h>
#include <wtf/text/MakeString.h>
namespace WebCore {
bool AXCoreObject::isList() const
{
auto role = this->role();
return role == AccessibilityRole::List || role == AccessibilityRole::DescriptionList;
}
bool AXCoreObject::isFileUploadButton() const
{
std::optional type = inputType();
return type ? *type == InputType::Type::File : false;
}
bool AXCoreObject::isMenuRelated() const
{
switch (role()) {
case AccessibilityRole::Menu:
case AccessibilityRole::MenuBar:
case AccessibilityRole::MenuItem:
case AccessibilityRole::MenuItemCheckbox:
case AccessibilityRole::MenuItemRadio:
return true;
default:
return false;
}
}
bool AXCoreObject::isMenuItem() const
{
switch (role()) {
case AccessibilityRole::MenuItem:
case AccessibilityRole::MenuItemRadio:
case AccessibilityRole::MenuItemCheckbox:
return true;
default:
return false;
}
}
bool AXCoreObject::isInputImage() const
{
if (role() != AccessibilityRole::Button)
return false;
std::optional type = inputType();
return type ? *type == InputType::Type::Image : false;
}
bool AXCoreObject::isControl() const
{
switch (role()) {
case AccessibilityRole::Button:
case AccessibilityRole::Checkbox:
case AccessibilityRole::ColorWell:
case AccessibilityRole::ComboBox:
case AccessibilityRole::DateTime:
case AccessibilityRole::LandmarkSearch:
case AccessibilityRole::ListBox:
case AccessibilityRole::PopUpButton:
case AccessibilityRole::RadioButton:
case AccessibilityRole::SearchField:
case AccessibilityRole::Slider:
case AccessibilityRole::SliderThumb:
case AccessibilityRole::SpinButton:
case AccessibilityRole::Switch:
case AccessibilityRole::TextArea:
case AccessibilityRole::TextField:
case AccessibilityRole::ToggleButton:
return true;
default:
// A focusable splitter (separator with tabindex) is considered a control,
// since it can be interacted with to adjust the value.
return isFieldset() || isFocusableSplitter();
}
}
bool AXCoreObject::isImplicitlyInteractive() const
{
switch (role()) {
case AccessibilityRole::Button:
case AccessibilityRole::Checkbox:
case AccessibilityRole::ColorWell:
case AccessibilityRole::ComboBox:
case AccessibilityRole::DateTime:
case AccessibilityRole::Details:
case AccessibilityRole::LandmarkSearch:
case AccessibilityRole::Link:
case AccessibilityRole::ListBox:
case AccessibilityRole::ListBoxOption:
case AccessibilityRole::MenuItemCheckbox:
case AccessibilityRole::MenuItemRadio:
case AccessibilityRole::MenuListOption:
case AccessibilityRole::MenuListPopup:
case AccessibilityRole::PopUpButton:
case AccessibilityRole::RadioButton:
case AccessibilityRole::SearchField:
case AccessibilityRole::Slider:
case AccessibilityRole::SliderThumb:
case AccessibilityRole::SpinButton:
case AccessibilityRole::SpinButtonPart:
case AccessibilityRole::Switch:
case AccessibilityRole::Tab:
case AccessibilityRole::TextArea:
case AccessibilityRole::TextField:
case AccessibilityRole::ToggleButton:
return true;
default:
return false;
}
}
bool AXCoreObject::isLandmark() const
{
switch (role()) {
case AccessibilityRole::Form:
case AccessibilityRole::LandmarkBanner:
case AccessibilityRole::LandmarkComplementary:
case AccessibilityRole::LandmarkContentInfo:
case AccessibilityRole::LandmarkDocRegion:
case AccessibilityRole::LandmarkMain:
case AccessibilityRole::LandmarkNavigation:
case AccessibilityRole::LandmarkRegion:
case AccessibilityRole::LandmarkSearch:
return true;
default:
return false;
}
}
bool AXCoreObject::isGroup() const
{
switch (role()) {
case AccessibilityRole::Group:
case AccessibilityRole::TextGroup:
return true;
default:
return false;
}
}
bool AXCoreObject::isImageMapLink() const
{
RefPtr element = this->element();
return element && is<HTMLAreaElement>(*element);
}
bool AXCoreObject::hasHighlighting() const
{
for (RefPtr ancestor = this; ancestor; ancestor = ancestor->parentObject()) {
if (ancestor->hasMarkTag())
return true;
}
return false;
}
bool AXCoreObject::hasGridRole() const
{
auto role = this->role();
return role == AccessibilityRole::Grid || role == AccessibilityRole::TreeGrid;
}
bool AXCoreObject::hasCellRole() const
{
auto role = this->role();
return role == AccessibilityRole::Cell || role == AccessibilityRole::GridCell || role == AccessibilityRole::ColumnHeader || role == AccessibilityRole::RowHeader;
}
bool AXCoreObject::hasCellOrRowRole() const
{
return hasCellRole() || role() == AccessibilityRole::Row;
}
bool AXCoreObject::isButton() const
{
switch (role()) {
case AccessibilityRole::Button:
case AccessibilityRole::PopUpButton:
case AccessibilityRole::ToggleButton:
return true;
default:
return false;
}
}
bool AXCoreObject::isTextControl() const
{
switch (role()) {
case AccessibilityRole::ComboBox:
case AccessibilityRole::SearchField:
case AccessibilityRole::TextArea:
case AccessibilityRole::TextField:
return true;
default:
return false;
}
}
ListBoxInterpretation AXCoreObject::listBoxInterpretation() const
{
if (role() != AccessibilityRole::ListBox)
return ListBoxInterpretation::NotListBox;
Deque<Ref<AXCoreObject>, /* inlineCapacity */ 100> queue;
for (Ref child : const_cast<AXCoreObject*>(this)->childrenIncludingIgnored())
queue.append(WTF::move(child));
unsigned iterations = 0;
bool foundListItem = false;
while (!queue.isEmpty()) {
Ref current = queue.takeFirst();
// Technically, per ARIA, the only valid children of listboxes are options, or groups containing options.
// But be permissive and call this listbox valid if it has at least one option.
if (current->isListBoxOption())
return ListBoxInterpretation::ActuallyListBox;
if (current->isListItem())
foundListItem = true;
// If we've checked 10 children and found a list item but no options, treat it as a static list.
if (iterations > 10 && foundListItem)
return ListBoxInterpretation::ActuallyStaticList;
// Don't iterate forever in case someone added role="listbox" to some high-level element.
// If we haven't found an option after checking 200 objects, this probably isn't valid anyways.
if (iterations >= 250)
break;
++iterations;
if (current->isGroup() || current->isIgnored()) {
for (Ref child : current->childrenIncludingIgnored())
queue.append(WTF::move(child));
}
}
return foundListItem ? ListBoxInterpretation::ActuallyStaticList : ListBoxInterpretation::InvalidListBox;
}
AXCoreObject::AccessibilityChildrenVector AXCoreObject::tabChildren()
{
if (role() != AccessibilityRole::TabList)
return { };
AXCoreObject::AccessibilityChildrenVector result;
for (const auto& child : unignoredChildren()) {
if (child->isTabItem())
result.append(child);
}
return result;
}
#if ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
static bool NODELETE isValidChildForTable(AXCoreObject& object)
{
auto role = object.role();
// Tables can only have these roles as exposed-to-AT children.
return role == AccessibilityRole::Row || role == AccessibilityRole::Column || role == AccessibilityRole::TableHeaderContainer || role == AccessibilityRole::Caption;
}
AXCoreObject::AccessibilityChildrenVector AXCoreObject::unignoredChildren(bool updateChildrenIfNeeded)
{
if (onlyAddsUnignoredChildren())
return children(updateChildrenIfNeeded);
// The unignored children of this object are generated by iterating over its children, ignored or not,
// and finding the first unignored descendant object of that child (or itself, if unignored).
RefPtr<AXCoreObject> parent = nullptr;
const AXCoreObject::AccessibilityChildrenVector* siblings = nullptr;
bool isExposedTable = isExposableTable();
AXCoreObject::AccessibilityChildrenVector unignoredChildren;
const auto& children = childrenIncludingIgnored(updateChildrenIfNeeded);
RefPtr descendant = children.size() ? children[0].ptr() : nullptr;
while (descendant && descendant != this) {
bool childIsValid = !isExposedTable || isValidChildForTable(*descendant);
if (!childIsValid || descendant->isIgnored()) {
descendant = descendant->nextInPreOrder(updateChildrenIfNeeded, /* stayWithin */ this);
parent = nullptr;
continue;
}
unignoredChildren.append(*descendant);
constexpr size_t maxChildrenFailsafe = 100000;
if (unignoredChildren.size() >= maxChildrenFailsafe) [[unlikely]] {
// This should never happen in a well-formed accessibility tree, so we must
// be looping infinitely.
ASSERT_NOT_REACHED();
return unignoredChildren;
}
while (descendant && descendant != this) {
if (!parent) {
parent = descendant->parentObject();
if (!parent) {
siblings = nullptr;
break;
}
siblings = &parent->childrenIncludingIgnored();
}
unsigned nextSiblingIndex = descendant->indexInParent() + 1;
if (RefPtr nextSibling = nextSiblingIndex < siblings->size() ? (*siblings)[nextSiblingIndex].ptr() : nullptr) {
descendant = WTF::move(nextSibling);
break;
}
// The descendant didn't have a next sibling, so ascend to its parent.
descendant = WTF::move(parent);
parent = nullptr;
}
}
return unignoredChildren;
}
bool AXCoreObject::hasUnignoredChild()
{
const auto& children = childrenIncludingIgnored(/* updateChildrenIfNeeded */ true);
RefPtr descendant = children.size() ? children[0].ptr() : nullptr;
if (onlyAddsUnignoredChildren())
return descendant;
bool isExposedTable = isExposableTable();
while (descendant && descendant != this) {
bool childIsValid = !isExposedTable || isValidChildForTable(*descendant);
if (childIsValid && !descendant->isIgnored())
return true;
descendant = descendant->nextInPreOrder(/* updateChildrenIfNeeded */ true, /* stayWithin */ this);
}
return false;
}
#endif // ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
static AXCoreObject::AccessibilityChildrenVector childrenAfterStitching(AXCoreObject::AccessibilityChildrenVector&& children)
{
children.removeAllMatching([] (const auto& child) {
if (!child->hasStitchableRole())
return false;
std::optional stitchedIntoID = child->stitchedIntoID();
return stitchedIntoID && *stitchedIntoID != child->objectID();
});
return children;
}
#if !ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
static AXCoreObject::AccessibilityChildrenVector childrenAfterStitching(const AXCoreObject::AccessibilityChildrenVector& children)
{
auto childrenCopy = children;
return childrenAfterStitching(WTF::move(childrenCopy));
}
#endif // !ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
AXCoreObject::AccessibilityChildrenVector AXCoreObject::stitchedUnignoredChildren()
{
return childrenAfterStitching(unignoredChildren());
}
std::optional<AXStitchGroup> AXCoreObject::stitchGroupIfRepresentative() const
{
std::optional stitchGroup = this->stitchGroup();
if (!stitchGroup || stitchGroup->representativeID() != objectID() || stitchGroup->isEmpty())
return { };
return stitchGroup;
}
AXCoreObject* AXCoreObject::blockFlowAncestor() const
{
return Accessibility::findAncestor(*this, /* includeSelf */ false, [] (const auto& ancestor) {
return ancestor.isBlockFlow();
});
}
// ARIA component of hidden definition.
// https://www.w3.org/TR/wai-aria/#dfn-hidden
bool AXCoreObject::isAXHidden() const
{
if (isFocused())
return false;
if (std::optional cachedIsIgnored = this->cachedIsIgnored()) {
if (!*cachedIsIgnored) {
// aria-hidden="true" makes itself and all descendants ignored, so try to early-exit
// before the ancestry traversal if we can cheaply determine we aren't ignored.
return false;
}
}
return Accessibility::findAncestor<AXCoreObject>(*this, /* includeSelf */ true, [] (const auto& object) {
return object.isARIAHidden();
}) != nullptr;
}
std::optional<AXStitchGroup> AXCoreObject::stitchGroupFromGroups(const Vector<AXStitchGroup>* groups, IncludeGroupMembers includeGroupMembers) const
{
if (!groups)
return { };
AXID thisAXID = objectID();
for (const auto& group : *groups) {
// Stitching zero or one elements doesn't make sense, so ensure our group is two or larger.
AX_ASSERT(group.members().size() >= 2);
if (group.members().contains(thisAXID)) {
if (includeGroupMembers == IncludeGroupMembers::No) {
// If the caller doesn't need the group we belong to, don't bother doing the copy.
return std::optional(AXStitchGroup { { }, group.representativeID() });
}
return std::optional(group);
}
}
return { };
}
AXCoreObject::AccessibilityChildrenVector AXCoreObject::crossFrameUnignoredChildren()
{
AXCoreObject::AccessibilityChildrenVector result = stitchedUnignoredChildren();
#if ENABLE_ACCESSIBILITY_LOCAL_FRAME
if (result.isEmpty()) {
if (RefPtr crossFrameChild = crossFrameChildObject())
result.append(*crossFrameChild);
} else {
for (size_t i = 0; i < result.size(); i++) {
if (RefPtr crossFrameChild = protect(result[i])->crossFrameChildObject())
result[i] = crossFrameChild.releaseNonNull();
}
}
#endif
return result;
}
AXCoreObject* AXCoreObject::crossFrameParentObjectUnignored() const
{
if (SUPPRESS_UNCOUNTED_LOCAL auto* result = parentObjectUnignored())
return result;
#if ENABLE_ACCESSIBILITY_LOCAL_FRAME
return crossFrameParentObject();
#endif
return nullptr;
}
AXCoreObject::AccessibilityChildrenVector AXCoreObject::crossFrameChildrenIncludingIgnored(bool updateChildrenIfNeeded)
{
AXCoreObject::AccessibilityChildrenVector result = childrenIncludingIgnored(updateChildrenIfNeeded);
#if ENABLE(ACCESSIBILITY_LOCAL_FRAME)
if (result.isEmpty()) {
if (RefPtr crossFrameChild = crossFrameChildObject())
result.append(crossFrameChild.releaseNonNull());
}
#endif // ENABLE(ACCESSIBILITY_LOCAL_FRAME)
return result;
}
bool AXCoreObject::crossFrameIsAncestorOfObject(const AXCoreObject& axObject) const
{
return this == &axObject || axObject.crossFrameIsDescendantOfObject(*this);
}
bool AXCoreObject::crossFrameIsDescendantOfObject(const AXCoreObject& axObject) const
{
return Accessibility::crossFrameFindAncestor<AXCoreObject>(*this, false, [&axObject] (const AXCoreObject& object) {
return &object == &axObject;
}) != nullptr;
}
AXCoreObject* AXCoreObject::parentObjectIncludingCrossFrame() const
{
if (SUPPRESS_UNCOUNTED_LOCAL auto* parent = parentObject())
return parent;
#if ENABLE_ACCESSIBILITY_LOCAL_FRAME
return crossFrameParentObject();
#else
return nullptr;
#endif
}
AXCoreObject* AXCoreObject::nextSiblingUnignored() const
{
RefPtr parent = parentObjectIncludingCrossFrame();
if (!parent)
return nullptr;
const auto& siblings = parent->children();
size_t index = siblings.findIf([this](const Ref<AXCoreObject>& child) {
return child.ptr() == this;
});
if (index == notFound)
return nullptr;
for (size_t i = index + 1; i < siblings.size(); ++i) {
auto& sibling = siblings[i];
if (sibling->isIgnored())
continue;
// Skip children that have been stitched into another object,
// as they don't appear in the exposed accessibility tree.
if (sibling->hasStitchableRole()) {
if (auto stitchedInto = sibling->stitchedIntoID(); stitchedInto && *stitchedInto != sibling->objectID())
continue;
}
return sibling.unsafePtr();
}
return nullptr;
}
AXCoreObject* AXCoreObject::previousSiblingUnignored() const
{
RefPtr parent = parentObjectIncludingCrossFrame();
if (!parent)
return nullptr;
const auto& siblings = parent->children();
size_t index = siblings.findIf([this](const Ref<AXCoreObject>& child) {
return child.ptr() == this;
});
if (index == notFound || !index)
return nullptr;
for (size_t i = index; i > 0; --i) {
auto& sibling = siblings[i - 1];
if (sibling->isIgnored())
continue;
// Skip children that have been stitched into another object,
// as they don't appear in the exposed accessibility tree.
if (sibling->hasStitchableRole()) {
if (auto stitchedInto = sibling->stitchedIntoID(); stitchedInto && *stitchedInto != sibling->objectID())
continue;
}
return sibling.unsafePtr();
}
return nullptr;
}
#ifndef NDEBUG
void AXCoreObject::verifyChildrenIndexInParent(const AccessibilityChildrenVector& children) const
{
if (!shouldSetChildIndexInParent()) {
// Due to known irregularities in how the accessibility tree is built, we don't want to
// do this verification for some types of objects, as it will always fail. At the time this
// was written, this is specifically table columns and table header containers, which insert
// cells as their children despite not being their "true" parent.
return;
}
for (unsigned i = 0; i < children.size(); i++)
AX_ASSERT(children[i]->indexInParent() == i);
}
#endif
RefPtr<AXCoreObject> AXCoreObject::nextInPreOrder(bool updateChildrenIfNeeded, AXCoreObject* stayWithin)
{
return nextInPreOrder(updateChildrenIfNeeded, stayWithin, false);
}
RefPtr<AXCoreObject> AXCoreObject::nextInPreOrder(bool updateChildrenIfNeeded , AXCoreObject* stayWithin, bool includeCrossFrame)
{
const auto& children = includeCrossFrame ? crossFrameChildrenIncludingIgnored(updateChildrenIfNeeded) : childrenIncludingIgnored(updateChildrenIfNeeded);
if (!children.isEmpty()) {
auto role = this->role();
if (role != AccessibilityRole::Column && role != AccessibilityRole::TableHeaderContainer) {
// Table columns and header containers add cells despite not being their "true" parent (which are the rows).
// Don't allow a pre-order traversal of these object types to return cells to avoid an infinite loop.
return children[0].copyRef();
}
}
if (stayWithin == this)
return nullptr;
RefPtr current = this;
RefPtr next = nextSiblingIncludingIgnored(updateChildrenIfNeeded, includeCrossFrame);
for (; !next; next = current->nextSiblingIncludingIgnored(updateChildrenIfNeeded, includeCrossFrame)) {
#if ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
current = includeCrossFrame ? current->parentObjectIncludingCrossFrame() : current->parentObject();
#else
current = includeCrossFrame ? current->crossFrameParentObjectUnignored() : current->parentObjectUnignored();
#endif
if (!current || stayWithin == current)
return nullptr;
}
return next;
}
RefPtr<AXCoreObject> AXCoreObject::previousInPreOrder(bool updateChildrenIfNeeded, AXCoreObject* stayWithin)
{
if (stayWithin == this)
return nullptr;
if (RefPtr sibling = previousSiblingIncludingIgnored(updateChildrenIfNeeded)) {
const auto& children = sibling->childrenIncludingIgnored(updateChildrenIfNeeded);
if (children.size())
return sibling->deepestLastChildIncludingIgnored(updateChildrenIfNeeded);
return sibling;
}
return parentObject();
}
AXCoreObject* AXCoreObject::deepestLastChildIncludingIgnored(bool updateChildrenIfNeeded)
{
const auto& children = childrenIncludingIgnored(updateChildrenIfNeeded);
if (children.isEmpty())
return nullptr;
Ref deepestChild = children[children.size() - 1];
while (true) {
const auto& descendants = deepestChild->childrenIncludingIgnored(updateChildrenIfNeeded);
if (descendants.isEmpty())
break;
deepestChild = descendants[descendants.size() - 1];
}
return deepestChild.unsafePtr();
}
size_t AXCoreObject::indexInSiblings(const AccessibilityChildrenVector& siblings) const
{
unsigned indexOfThis = indexInParent();
if (indexOfThis >= siblings.size() || siblings[indexOfThis]->objectID() != objectID()) [[unlikely]] {
// If this happens, the accessibility tree is an incorrect state.
AX_ASSERT_NOT_REACHED();
return siblings.findIf([this] (const Ref<AXCoreObject>& object) {
return object.ptr() == this;
});
}
return indexOfThis;
}
AXCoreObject* AXCoreObject::nextSiblingIncludingIgnored(bool updateChildrenIfNeeded) const
{
return nextSiblingIncludingIgnored(updateChildrenIfNeeded, /* crossFrame = */ false);
}
AXCoreObject* AXCoreObject::nextSiblingIncludingIgnored(bool updateChildrenIfNeeded, bool includeCrossFrame) const
{
#if ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
RefPtr parent = parentObject();
#else
RefPtr parent = parentObjectUnignored();
#endif
if (!parent)
return nullptr;
const auto& siblings = includeCrossFrame ? parent->crossFrameChildrenIncludingIgnored(updateChildrenIfNeeded) : parent->childrenIncludingIgnored(updateChildrenIfNeeded);
size_t indexOfThis = indexInSiblings(siblings);
if (indexOfThis == notFound)
return nullptr;
return indexOfThis + 1 < siblings.size() ? siblings[indexOfThis + 1].unsafePtr() : nullptr;
}
RefPtr<AXCoreObject> AXCoreObject::previousSiblingIncludingIgnored(bool updateChildrenIfNeeded)
{
RefPtr parent = parentObject();
if (!parent)
return nullptr;
const auto& siblings = parent->childrenIncludingIgnored(updateChildrenIfNeeded);
size_t indexOfThis = indexInSiblings(siblings);
if (indexOfThis == notFound || indexOfThis < 1)
return nullptr;
return siblings[indexOfThis - 1].copyRef();
}
AXCoreObject* AXCoreObject::nextUnignoredSibling(bool updateChildrenIfNeeded, AXCoreObject* unignoredParent) const
{
// In some contexts, we may have already computed the `unignoredParent`, which is what this parameter is.
// Ensure this is actually our parent.
AX_ASSERT(unignoredParent == parentObjectUnignored());
RefPtr parent = unignoredParent ? unignoredParent : parentObjectUnignored();
if (!parent)
return nullptr;
const auto& siblings = parent->unignoredChildren(updateChildrenIfNeeded);
size_t indexOfThis = siblings.findIf([this] (const Ref<AXCoreObject>& object) {
return object.ptr() == this;
});
if (indexOfThis == notFound)
return nullptr;
return indexOfThis + 1 < siblings.size() ? siblings[indexOfThis + 1].unsafePtr() : nullptr;
}
AXCoreObject* AXCoreObject::nextSiblingIncludingIgnoredOrParent() const
{
if (auto* nextSibling = nextSiblingIncludingIgnored(/* updateChildrenIfNeeded */ true))
return nextSibling;
return parentObject();
}
String AXCoreObject::autoCompleteValue() const
{
String explicitValue = explicitAutoCompleteValue();
return explicitValue.isEmpty() ? "none"_s : explicitValue;
}
String AXCoreObject::invalidStatus() const
{
auto explicitValue = explicitInvalidStatus();
// "false" is the default if no invalid status is explicitly provided (e.g. via aria-invalid).
return explicitValue.isEmpty() ? "false"_s : explicitValue;
}
AXCoreObject::AccessibilityChildrenVector AXCoreObject::contents()
{
if (isTabList())
return tabChildren();
if (isScrollArea()) {
// A scroll view's contents are everything except the scroll bars.
AccessibilityChildrenVector nonScrollbarChildren;
for (const auto& child : stitchedUnignoredChildren()) {
if (!child->isScrollbar())
nonScrollbarChildren.append(child);
}
return nonScrollbarChildren;
}
return { };
}
AXCoreObject::AccessibilityChildrenVector AXCoreObject::ariaTreeItemContent()
{
AccessibilityChildrenVector result;
// The content of a treeitem excludes other treeitems or their containing groups.
for (const auto& child : unignoredChildren()) {
if (!child->isGroup() && child->role() != AccessibilityRole::TreeItem)
result.append(child);
}
return result;
}
String AXCoreObject::currentValue() const
{
switch (currentState()) {
case AccessibilityCurrentState::False:
return "false"_s;
case AccessibilityCurrentState::Page:
return "page"_s;
case AccessibilityCurrentState::Step:
return "step"_s;
case AccessibilityCurrentState::Location:
return "location"_s;
case AccessibilityCurrentState::Time:
return "time"_s;
case AccessibilityCurrentState::Date:
return "date"_s;
default:
case AccessibilityCurrentState::True:
return "true"_s;
}
}
AXCoreObject::AXValue AXCoreObject::value()
{
if (supportsRangeValue())
return valueForRange();
if (role() == AccessibilityRole::SliderThumb) {
RefPtr parent = parentObject();
return parent ? parent->valueForRange() : 0.0f;
}
if (isHeading())
return headingLevel();
if (supportsCheckedState())
return checkboxOrRadioValue();
if (role() == AccessibilityRole::Summary)
return isExpanded();
// Radio groups return the selected radio button as the AXValue.
if (isRadioGroup())
return selectedRadioButton();
if (isTabList())
return selectedTabItem();
if (isTabItem())
return isSelected();
if (isDateTime())
return dateTimeValue();
if (isColorWell()) {
auto color = convertColor<SRGBA<float>>(colorValue()).resolved();
auto channel = [](float number) {
return FormattedNumber::fixedPrecision(number, 6, TrailingZerosPolicy::Keep);
};
return color.alpha == 1
? makeString("rgb "_s, channel(color.red), ' ', channel(color.green), ' ', channel(color.blue), " 1"_s)
: makeString("rgb "_s, channel(color.red), ' ', channel(color.green), ' ', channel(color.blue), ' ', channel(color.alpha));
}
return stringValue();
}
AXCoreObject* AXCoreObject::selectedRadioButton()
{
if (!isRadioGroup())
return nullptr;
// Find the child radio button that is selected (ie. the intValue == 1).
for (const auto& child : unignoredChildren()) {
if (child->role() == AccessibilityRole::RadioButton && child->checkboxOrRadioValue() == AccessibilityButtonState::On)
return child.ptr();
}
return nullptr;
}
AXCoreObject* AXCoreObject::selectedTabItem()
{
if (!isTabList())
return nullptr;
// FIXME: Is this valid? ARIA tab items support aria-selected; not aria-checked.
// Find the child tab item that is selected (ie. the intValue == 1).
for (const auto& child : unignoredChildren()) {
if (child->isTabItem() && (child->isChecked() || child->isSelected()))
return child.ptr();
}
return nullptr;
}
bool AXCoreObject::canHaveSelectedChildren() const
{
switch (role()) {
// These roles are containers whose children support aria-selected:
case AccessibilityRole::Grid:
case AccessibilityRole::ListBox:
case AccessibilityRole::TabList:
case AccessibilityRole::Tree:
case AccessibilityRole::TreeGrid:
case AccessibilityRole::List:
// These roles are containers whose children are treated as selected by assistive
// technologies. We can get the "selected" item via aria-activedescendant or the
// focused element.
case AccessibilityRole::Menu:
case AccessibilityRole::MenuBar:
case AccessibilityRole::ComboBox:
#if USE(ATSPI)
case AccessibilityRole::MenuListPopup:
#endif
return true;
default:
return false;
}
}
AXCoreObject::AccessibilityChildrenVector AXCoreObject::selectedChildren()
{
if (!canHaveSelectedChildren())
return { };
switch (role()) {
case AccessibilityRole::ComboBox:
if (RefPtr descendant = activeDescendant())
return { { descendant.releaseNonNull() } };
break;
case AccessibilityRole::ListBox:
return listboxSelectedChildren();
case AccessibilityRole::Grid:
case AccessibilityRole::Tree:
case AccessibilityRole::TreeGrid:
return selectedRows();
case AccessibilityRole::TabList:
if (RefPtr selectedTab = selectedTabItem())
return { { selectedTab.releaseNonNull() } };
break;
case AccessibilityRole::List:
return selectedListItems();
case AccessibilityRole::Menu:
case AccessibilityRole::MenuBar:
if (RefPtr descendant = activeDescendant())
return { { descendant.releaseNonNull() } };
if (RefPtr focusedElement = focusedUIElement())
return { { focusedElement.releaseNonNull() } };
break;
case AccessibilityRole::MenuListPopup: {
AccessibilityChildrenVector selectedItems;
for (const auto& child : unignoredChildren()) {
if (child->isSelected())
selectedItems.append(child);
}
return selectedItems;
}
default:
AX_ASSERT_NOT_REACHED();
break;
}
return { };
}
AXCoreObject::AccessibilityChildrenVector AXCoreObject::listboxSelectedChildren()
{
AX_ASSERT(role() == AccessibilityRole::ListBox);
AccessibilityChildrenVector result;
bool isMulti = isMultiSelectable();
for (const auto& child : unignoredChildren()) {
if (!child->isListBoxOption() || !child->isSelected())
continue;
result.append(child);
if (!isMulti)
return result;
}
return result;
}
AXCoreObject::AccessibilityChildrenVector AXCoreObject::selectedRows()
{
AX_ASSERT(role() == AccessibilityRole::Grid || role() == AccessibilityRole::Tree || role() == AccessibilityRole::TreeGrid);
bool isMulti = isMultiSelectable();
AccessibilityChildrenVector result;
// Prefer active descendant over aria-selected.
RefPtr activeDescendant = this->activeDescendant();
if (activeDescendant && (activeDescendant->isTreeItem() || activeDescendant->isExposedTableRow())) {
result.append(*activeDescendant);
if (!isMulti)
return result;
}
auto rowsIteration = [&](const auto& rows) {
for (auto& row : rows) {
if (row->isSelected() || row->isActiveDescendantOfFocusedContainer()) {
result.append(row);
if (!isMulti)
break;
}
}
};
if (isTree())
rowsIteration(ariaTreeRows());
else if (isExposableTable() && supportsSelectedRows())
rowsIteration(rows());
return result;
}
AXCoreObject::AccessibilityChildrenVector AXCoreObject::selectedListItems()
{
AX_ASSERT(role() == AccessibilityRole::List);
AccessibilityChildrenVector selectedListItems;
for (const auto& child : unignoredChildren()) {