forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathax_node_data.cc
2095 lines (1902 loc) · 78 KB
/
ax_node_data.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 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/ax_node_data.h"
#include <stddef.h>
#include <set>
#include <type_traits>
#include <utility>
#include "base/containers/cxx20_erase.h"
#include "base/no_destructor.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/accessibility/ax_enum_util.h"
#include "ui/accessibility/ax_enums.mojom-shared.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/accessibility/ax_tree_id.h"
#include "ui/gfx/geometry/transform.h"
namespace ui {
namespace {
bool IsFlagSet(uint32_t bitfield, uint32_t flag) {
return (bitfield & (1U << flag)) != 0;
}
bool IsFlagSet(uint64_t bitfield, uint32_t flag) {
return (bitfield & (1ULL << flag)) != 0;
}
uint32_t ModifyFlag(uint32_t bitfield, uint32_t flag, bool set) {
return set ? (bitfield |= (1U << flag)) : (bitfield &= ~(1U << flag));
}
uint64_t ModifyFlag(uint64_t bitfield, uint32_t flag, bool set) {
return set ? (bitfield |= (1ULL << flag)) : (bitfield &= ~(1ULL << flag));
}
std::string StateBitfieldToString(uint32_t state_enum) {
std::string str;
for (uint32_t i = static_cast<uint32_t>(ax::mojom::State::kNone) + 1;
i <= static_cast<uint32_t>(ax::mojom::State::kMaxValue); ++i) {
if (IsFlagSet(state_enum, i))
str += " " +
base::ToUpperASCII(ui::ToString(static_cast<ax::mojom::State>(i)));
}
return str;
}
std::string ActionsBitfieldToString(uint64_t actions) {
std::string str;
for (uint32_t i = static_cast<uint32_t>(ax::mojom::Action::kNone) + 1;
i <= static_cast<uint32_t>(ax::mojom::Action::kMaxValue); ++i) {
if (IsFlagSet(actions, i)) {
str += ui::ToString(static_cast<ax::mojom::Action>(i));
actions = ModifyFlag(actions, i, false);
str += actions ? "," : "";
}
}
return str;
}
template <typename ItemType, typename ItemToStringFunction>
std::string VectorToString(const std::vector<ItemType>& items,
ItemToStringFunction itemToStringFunction) {
std::string str;
for (size_t i = 0; i < items.size(); ++i) {
std::string item_str = itemToStringFunction(items[i]);
if (item_str.empty())
continue;
if (i > 0)
str += ",";
str += itemToStringFunction(items[i]);
}
return str;
}
std::string IntVectorToString(const std::vector<int32_t>& items) {
return VectorToString(
items, [](const int32_t item) { return base::NumberToString(item); });
}
// Helper function that finds a key in a vector of pairs by matching on the
// first value, and returns an iterator.
template <typename FirstType, typename SecondType>
typename std::vector<std::pair<FirstType, SecondType>>::const_iterator
FindInVectorOfPairs(
FirstType first,
const std::vector<std::pair<FirstType, SecondType>>& vector) {
return base::ranges::find(vector, first,
&std::pair<FirstType, SecondType>::first);
}
} // namespace
// Return true if |attr| is a node ID that would need to be mapped when
// renumbering the ids in a combined tree.
bool IsNodeIdIntAttribute(ax::mojom::IntAttribute attr) {
switch (attr) {
case ax::mojom::IntAttribute::kActivedescendantId:
case ax::mojom::IntAttribute::kErrormessageIdDeprecated:
case ax::mojom::IntAttribute::kInPageLinkTargetId:
case ax::mojom::IntAttribute::kMemberOfId:
case ax::mojom::IntAttribute::kNextOnLineId:
case ax::mojom::IntAttribute::kPopupForId:
case ax::mojom::IntAttribute::kPreviousOnLineId:
case ax::mojom::IntAttribute::kTableHeaderId:
case ax::mojom::IntAttribute::kTableColumnHeaderId:
case ax::mojom::IntAttribute::kTableRowHeaderId:
case ax::mojom::IntAttribute::kNextFocusId:
case ax::mojom::IntAttribute::kPreviousFocusId:
case ax::mojom::IntAttribute::kNextWindowFocusId:
case ax::mojom::IntAttribute::kPreviousWindowFocusId:
return true;
// Note: all of the attributes are included here explicitly,
// rather than using "default:", so that it's a compiler error to
// add a new attribute without explicitly considering whether it's
// a node id attribute or not.
case ax::mojom::IntAttribute::kNone:
case ax::mojom::IntAttribute::kDefaultActionVerb:
case ax::mojom::IntAttribute::kScrollX:
case ax::mojom::IntAttribute::kScrollXMin:
case ax::mojom::IntAttribute::kScrollXMax:
case ax::mojom::IntAttribute::kScrollY:
case ax::mojom::IntAttribute::kScrollYMin:
case ax::mojom::IntAttribute::kScrollYMax:
case ax::mojom::IntAttribute::kTextSelStart:
case ax::mojom::IntAttribute::kTextSelEnd:
case ax::mojom::IntAttribute::kTableRowCount:
case ax::mojom::IntAttribute::kTableColumnCount:
case ax::mojom::IntAttribute::kTableRowIndex:
case ax::mojom::IntAttribute::kTableColumnIndex:
case ax::mojom::IntAttribute::kTableCellColumnIndex:
case ax::mojom::IntAttribute::kTableCellColumnSpan:
case ax::mojom::IntAttribute::kTableCellRowIndex:
case ax::mojom::IntAttribute::kTableCellRowSpan:
case ax::mojom::IntAttribute::kSortDirection:
case ax::mojom::IntAttribute::kHierarchicalLevel:
case ax::mojom::IntAttribute::kNameFrom:
case ax::mojom::IntAttribute::kDescriptionFrom:
case ax::mojom::IntAttribute::kSetSize:
case ax::mojom::IntAttribute::kPosInSet:
case ax::mojom::IntAttribute::kColorValue:
case ax::mojom::IntAttribute::kAriaCurrentState:
case ax::mojom::IntAttribute::kHasPopup:
case ax::mojom::IntAttribute::kIsPopup:
case ax::mojom::IntAttribute::kBackgroundColor:
case ax::mojom::IntAttribute::kColor:
case ax::mojom::IntAttribute::kInvalidState:
case ax::mojom::IntAttribute::kCheckedState:
case ax::mojom::IntAttribute::kRestriction:
case ax::mojom::IntAttribute::kListStyle:
case ax::mojom::IntAttribute::kTextAlign:
case ax::mojom::IntAttribute::kTextDirection:
case ax::mojom::IntAttribute::kTextPosition:
case ax::mojom::IntAttribute::kTextStyle:
case ax::mojom::IntAttribute::kTextOverlineStyle:
case ax::mojom::IntAttribute::kTextStrikethroughStyle:
case ax::mojom::IntAttribute::kTextUnderlineStyle:
case ax::mojom::IntAttribute::kAriaColumnCount:
case ax::mojom::IntAttribute::kAriaCellColumnIndex:
case ax::mojom::IntAttribute::kAriaCellColumnSpan:
case ax::mojom::IntAttribute::kAriaRowCount:
case ax::mojom::IntAttribute::kAriaCellRowIndex:
case ax::mojom::IntAttribute::kAriaCellRowSpan:
case ax::mojom::IntAttribute::kImageAnnotationStatus:
case ax::mojom::IntAttribute::kDropeffectDeprecated:
case ax::mojom::IntAttribute::kDOMNodeId:
return false;
}
NOTREACHED();
return false;
}
// Returns true if |attr| contains a vector of node ids that would need
// to be mapped when renumbering the ids in a combined tree.
bool IsNodeIdIntListAttribute(ax::mojom::IntListAttribute attr) {
switch (attr) {
case ax::mojom::IntListAttribute::kIndirectChildIds:
case ax::mojom::IntListAttribute::kControlsIds:
case ax::mojom::IntListAttribute::kDetailsIds:
case ax::mojom::IntListAttribute::kDescribedbyIds:
case ax::mojom::IntListAttribute::kErrormessageIds:
case ax::mojom::IntListAttribute::kFlowtoIds:
case ax::mojom::IntListAttribute::kLabelledbyIds:
case ax::mojom::IntListAttribute::kRadioGroupIds:
return true;
// Note: all of the attributes are included here explicitly,
// rather than using "default:", so that it's a compiler error to
// add a new attribute without explicitly considering whether it's
// a node id attribute or not.
case ax::mojom::IntListAttribute::kNone:
case ax::mojom::IntListAttribute::kMarkerTypes:
case ax::mojom::IntListAttribute::kMarkerStarts:
case ax::mojom::IntListAttribute::kMarkerEnds:
case ax::mojom::IntListAttribute::kHighlightTypes:
case ax::mojom::IntListAttribute::kCaretBounds:
case ax::mojom::IntListAttribute::kCharacterOffsets:
case ax::mojom::IntListAttribute::kLineStarts:
case ax::mojom::IntListAttribute::kLineEnds:
case ax::mojom::IntListAttribute::kSentenceStarts:
case ax::mojom::IntListAttribute::kSentenceEnds:
case ax::mojom::IntListAttribute::kWordStarts:
case ax::mojom::IntListAttribute::kWordEnds:
case ax::mojom::IntListAttribute::kCustomActionIds:
case ax::mojom::IntListAttribute::kTextOperationStartOffsets:
case ax::mojom::IntListAttribute::kTextOperationEndOffsets:
case ax::mojom::IntListAttribute::kTextOperationEndAnchorIds:
case ax::mojom::IntListAttribute::kTextOperationStartAnchorIds:
case ax::mojom::IntListAttribute::kTextOperations:
return false;
}
}
AXNodeData::AXNodeData() : role(ax::mojom::Role::kUnknown) {}
AXNodeData::~AXNodeData() = default;
AXNodeData::AXNodeData(const AXNodeData& other) {
id = other.id;
role = other.role;
state = other.state;
actions = other.actions;
string_attributes = other.string_attributes;
int_attributes = other.int_attributes;
float_attributes = other.float_attributes;
bool_attributes = other.bool_attributes;
intlist_attributes = other.intlist_attributes;
stringlist_attributes = other.stringlist_attributes;
html_attributes = other.html_attributes;
child_ids = other.child_ids;
relative_bounds = other.relative_bounds;
}
AXNodeData::AXNodeData(AXNodeData&& other) {
id = other.id;
role = other.role;
state = other.state;
actions = other.actions;
string_attributes.swap(other.string_attributes);
int_attributes.swap(other.int_attributes);
float_attributes.swap(other.float_attributes);
bool_attributes.swap(other.bool_attributes);
intlist_attributes.swap(other.intlist_attributes);
stringlist_attributes.swap(other.stringlist_attributes);
html_attributes.swap(other.html_attributes);
child_ids.swap(other.child_ids);
relative_bounds = other.relative_bounds;
other.id = kInvalidAXNodeID;
other.role = ax::mojom::Role::kUnknown;
other.state = 0U;
other.actions = 0ULL;
}
AXNodeData& AXNodeData::operator=(const AXNodeData& other) {
id = other.id;
role = other.role;
state = other.state;
actions = other.actions;
string_attributes = other.string_attributes;
int_attributes = other.int_attributes;
float_attributes = other.float_attributes;
bool_attributes = other.bool_attributes;
intlist_attributes = other.intlist_attributes;
stringlist_attributes = other.stringlist_attributes;
html_attributes = other.html_attributes;
child_ids = other.child_ids;
relative_bounds = other.relative_bounds;
return *this;
}
bool AXNodeData::HasBoolAttribute(ax::mojom::BoolAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, bool_attributes);
return iter != bool_attributes.end();
}
bool AXNodeData::GetBoolAttribute(ax::mojom::BoolAttribute attribute) const {
bool result;
if (GetBoolAttribute(attribute, &result))
return result;
return false;
}
bool AXNodeData::GetBoolAttribute(ax::mojom::BoolAttribute attribute,
bool* value) const {
auto iter = FindInVectorOfPairs(attribute, bool_attributes);
if (iter != bool_attributes.end()) {
*value = iter->second;
return true;
}
return false;
}
bool AXNodeData::HasFloatAttribute(ax::mojom::FloatAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, float_attributes);
return iter != float_attributes.end();
}
float AXNodeData::GetFloatAttribute(ax::mojom::FloatAttribute attribute) const {
float result;
if (GetFloatAttribute(attribute, &result))
return result;
return 0.0;
}
bool AXNodeData::GetFloatAttribute(ax::mojom::FloatAttribute attribute,
float* value) const {
auto iter = FindInVectorOfPairs(attribute, float_attributes);
if (iter != float_attributes.end()) {
*value = iter->second;
return true;
}
return false;
}
bool AXNodeData::HasIntAttribute(ax::mojom::IntAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, int_attributes);
return iter != int_attributes.end();
}
int AXNodeData::GetIntAttribute(ax::mojom::IntAttribute attribute) const {
int result;
if (GetIntAttribute(attribute, &result))
return result;
return 0;
}
bool AXNodeData::GetIntAttribute(ax::mojom::IntAttribute attribute,
int* value) const {
auto iter = FindInVectorOfPairs(attribute, int_attributes);
if (iter != int_attributes.end()) {
*value = int{iter->second};
return true;
}
return false;
}
bool AXNodeData::HasStringAttribute(
ax::mojom::StringAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, string_attributes);
return iter != string_attributes.end();
}
const std::string& AXNodeData::GetStringAttribute(
ax::mojom::StringAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, string_attributes);
return iter != string_attributes.end() ? iter->second : base::EmptyString();
}
bool AXNodeData::GetStringAttribute(ax::mojom::StringAttribute attribute,
std::string* value) const {
auto iter = FindInVectorOfPairs(attribute, string_attributes);
if (iter != string_attributes.end()) {
*value = iter->second;
return true;
}
return false;
}
std::u16string AXNodeData::GetString16Attribute(
ax::mojom::StringAttribute attribute) const {
std::string value_utf8;
if (!GetStringAttribute(attribute, &value_utf8))
return std::u16string();
return base::UTF8ToUTF16(value_utf8);
}
bool AXNodeData::GetString16Attribute(ax::mojom::StringAttribute attribute,
std::u16string* value) const {
std::string value_utf8;
if (!GetStringAttribute(attribute, &value_utf8))
return false;
*value = base::UTF8ToUTF16(value_utf8);
return true;
}
bool AXNodeData::HasIntListAttribute(
ax::mojom::IntListAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, intlist_attributes);
return iter != intlist_attributes.end();
}
const std::vector<int32_t>& AXNodeData::GetIntListAttribute(
ax::mojom::IntListAttribute attribute) const {
static const base::NoDestructor<std::vector<int32_t>> empty_vector;
auto iter = FindInVectorOfPairs(attribute, intlist_attributes);
if (iter != intlist_attributes.end())
return iter->second;
return *empty_vector;
}
bool AXNodeData::GetIntListAttribute(ax::mojom::IntListAttribute attribute,
std::vector<int32_t>* value) const {
auto iter = FindInVectorOfPairs(attribute, intlist_attributes);
if (iter != intlist_attributes.end()) {
*value = iter->second;
return true;
}
return false;
}
bool AXNodeData::HasStringListAttribute(
ax::mojom::StringListAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, stringlist_attributes);
return iter != stringlist_attributes.end();
}
const std::vector<std::string>& AXNodeData::GetStringListAttribute(
ax::mojom::StringListAttribute attribute) const {
static const base::NoDestructor<std::vector<std::string>> empty_vector;
auto iter = FindInVectorOfPairs(attribute, stringlist_attributes);
if (iter != stringlist_attributes.end())
return iter->second;
return *empty_vector;
}
bool AXNodeData::GetStringListAttribute(
ax::mojom::StringListAttribute attribute,
std::vector<std::string>* value) const {
auto iter = FindInVectorOfPairs(attribute, stringlist_attributes);
if (iter != stringlist_attributes.end()) {
*value = iter->second;
return true;
}
return false;
}
bool AXNodeData::HasHtmlAttribute(const char* attribute) const {
std::string value;
if (!GetHtmlAttribute(attribute, &value))
return false;
return true;
}
bool AXNodeData::GetHtmlAttribute(const char* attribute,
std::string* value) const {
for (const std::pair<std::string, std::string>& html_attribute :
html_attributes) {
const std::string& attr = html_attribute.first;
if (base::EqualsCaseInsensitiveASCII(attr, attribute)) {
*value = html_attribute.second;
return true;
}
}
return false;
}
std::u16string AXNodeData::GetHtmlAttribute(const char* attribute) const {
std::u16string value_utf16;
GetHtmlAttribute(attribute, &value_utf16);
return value_utf16;
}
bool AXNodeData::GetHtmlAttribute(const char* attribute,
std::u16string* value) const {
std::string value_utf8;
if (!GetHtmlAttribute(attribute, &value_utf8))
return false;
*value = base::UTF8ToUTF16(value_utf8);
return true;
}
void AXNodeData::AddChildTreeId(const AXTreeID& tree_id) {
ax::mojom::StringAttribute attribute =
ax::mojom::StringAttribute::kChildTreeId;
if (HasStringAttribute(attribute))
RemoveStringAttribute(attribute);
string_attributes.emplace_back(attribute, tree_id.ToString());
}
void AXNodeData::AddBoolAttribute(ax::mojom::BoolAttribute attribute,
bool value) {
DCHECK_NE(attribute, ax::mojom::BoolAttribute::kNone);
if (HasBoolAttribute(attribute))
RemoveBoolAttribute(attribute);
bool_attributes.emplace_back(attribute, value);
}
void AXNodeData::AddIntAttribute(ax::mojom::IntAttribute attribute, int value) {
DCHECK_NE(attribute, ax::mojom::IntAttribute::kNone);
if (HasIntAttribute(attribute))
RemoveIntAttribute(attribute);
int_attributes.emplace_back(attribute, value);
}
void AXNodeData::AddFloatAttribute(ax::mojom::FloatAttribute attribute,
float value) {
DCHECK_NE(attribute, ax::mojom::FloatAttribute::kNone);
if (HasFloatAttribute(attribute))
RemoveFloatAttribute(attribute);
float_attributes.emplace_back(attribute, value);
}
void AXNodeData::AddStringAttribute(ax::mojom::StringAttribute attribute,
const std::string& value) {
DCHECK_NE(attribute, ax::mojom::StringAttribute::kNone);
DCHECK_NE(attribute, ax::mojom::StringAttribute::kChildTreeId)
<< "Use AddChildTreeId.";
if (HasStringAttribute(attribute))
RemoveStringAttribute(attribute);
string_attributes.emplace_back(attribute, value);
}
void AXNodeData::AddIntListAttribute(ax::mojom::IntListAttribute attribute,
const std::vector<int32_t>& value) {
DCHECK_NE(attribute, ax::mojom::IntListAttribute::kNone);
if (HasIntListAttribute(attribute))
RemoveIntListAttribute(attribute);
intlist_attributes.emplace_back(attribute, value);
}
void AXNodeData::AddStringListAttribute(
ax::mojom::StringListAttribute attribute,
const std::vector<std::string>& value) {
DCHECK_NE(attribute, ax::mojom::StringListAttribute::kNone);
if (HasStringListAttribute(attribute))
RemoveStringListAttribute(attribute);
stringlist_attributes.emplace_back(attribute, value);
}
void AXNodeData::RemoveBoolAttribute(ax::mojom::BoolAttribute attribute) {
DCHECK_NE(attribute, ax::mojom::BoolAttribute::kNone);
base::EraseIf(bool_attributes, [attribute](const auto& bool_attribute) {
return bool_attribute.first == attribute;
});
}
void AXNodeData::RemoveIntAttribute(ax::mojom::IntAttribute attribute) {
DCHECK_NE(attribute, ax::mojom::IntAttribute::kNone);
base::EraseIf(int_attributes, [attribute](const auto& int_attribute) {
return int_attribute.first == attribute;
});
}
void AXNodeData::RemoveFloatAttribute(ax::mojom::FloatAttribute attribute) {
DCHECK_NE(attribute, ax::mojom::FloatAttribute::kNone);
base::EraseIf(float_attributes, [attribute](const auto& float_attribute) {
return float_attribute.first == attribute;
});
}
void AXNodeData::RemoveStringAttribute(ax::mojom::StringAttribute attribute) {
DCHECK_NE(attribute, ax::mojom::StringAttribute::kNone);
base::EraseIf(string_attributes, [attribute](const auto& string_attribute) {
return string_attribute.first == attribute;
});
}
void AXNodeData::RemoveIntListAttribute(ax::mojom::IntListAttribute attribute) {
DCHECK_NE(attribute, ax::mojom::IntListAttribute::kNone);
base::EraseIf(intlist_attributes, [attribute](const auto& intlist_attribute) {
return intlist_attribute.first == attribute;
});
}
void AXNodeData::RemoveStringListAttribute(
ax::mojom::StringListAttribute attribute) {
DCHECK_NE(attribute, ax::mojom::StringListAttribute::kNone);
base::EraseIf(stringlist_attributes,
[attribute](const auto& stringlist_attribute) {
return stringlist_attribute.first == attribute;
});
}
AXTextAttributes AXNodeData::GetTextAttributes() const {
AXTextAttributes text_attributes;
// This overload of `GetIntAttribute` does not set the return value to 0 if
// the attribute is not present, hence maintaining the corresponding member in
// `AXTextAttributes` as `AXTextAttributes::kUnsetValue`.
GetIntAttribute(ax::mojom::IntAttribute::kBackgroundColor,
&text_attributes.background_color);
GetIntAttribute(ax::mojom::IntAttribute::kColor, &text_attributes.color);
GetIntAttribute(ax::mojom::IntAttribute::kInvalidState,
&text_attributes.invalid_state);
GetIntAttribute(ax::mojom::IntAttribute::kTextOverlineStyle,
&text_attributes.overline_style);
GetIntAttribute(ax::mojom::IntAttribute::kTextDirection,
&text_attributes.text_direction);
GetIntAttribute(ax::mojom::IntAttribute::kTextPosition,
&text_attributes.text_position);
GetIntAttribute(ax::mojom::IntAttribute::kTextStrikethroughStyle,
&text_attributes.strikethrough_style);
GetIntAttribute(ax::mojom::IntAttribute::kTextStyle,
&text_attributes.text_style);
GetIntAttribute(ax::mojom::IntAttribute::kTextUnderlineStyle,
&text_attributes.underline_style);
GetFloatAttribute(ax::mojom::FloatAttribute::kFontSize,
&text_attributes.font_size);
GetFloatAttribute(ax::mojom::FloatAttribute::kFontWeight,
&text_attributes.font_weight);
GetStringAttribute(ax::mojom::StringAttribute::kFontFamily,
&text_attributes.font_family);
GetIntListAttribute(ax::mojom::IntListAttribute::kMarkerTypes,
&text_attributes.marker_types);
GetIntListAttribute(ax::mojom::IntListAttribute::kHighlightTypes,
&text_attributes.highlight_types);
return text_attributes;
}
void AXNodeData::SetName(const std::string& name) {
// Elements with role='presentation' have Role::kNone. They should not be
// named. Objects with Role::kUnknown were never given a role. This check
// is only relevant if the name is not empty.
// TODO(crbug.com/1361972): It would be nice to have a means to set the name
// and role at the same time to avoid this ordering requirement.
DCHECK(name.empty() ||
(role != ax::mojom::Role::kNone && role != ax::mojom::Role::kUnknown))
<< "Cannot set name to '" << name << "' on class: '"
<< GetStringAttribute(ax::mojom::StringAttribute::kClassName)
<< "' because a valid role is needed to set the default NameFrom "
"attribute. Set the role first.";
auto iter = base::ranges::find(
string_attributes, ax::mojom::StringAttribute::kName,
&std::pair<ax::mojom::StringAttribute, std::string>::first);
if (iter == string_attributes.end()) {
string_attributes.emplace_back(ax::mojom::StringAttribute::kName, name);
} else {
iter->second = name;
}
// It is possible for `SetName`/`SetNameChecked` to be called after
// `SetNameExplicitlyEmpty`.
if (!name.empty() &&
GetNameFrom() == ax::mojom::NameFrom::kAttributeExplicitlyEmpty) {
RemoveIntAttribute(ax::mojom::IntAttribute::kNameFrom);
}
if (HasIntAttribute(ax::mojom::IntAttribute::kNameFrom))
return;
// Since this method is mostly used by tests which don't always set the
// "NameFrom" attribute, we need to set it here to the most likely value if
// not set, otherwise code that tries to calculate the node's inner text, its
// hypertext, or even its value, might not know whether to include the name in
// the result or not.
//
// For example, if there is a text field, but it is empty, i.e. it has no
// value, its value could be its name if "NameFrom" is set to "kPlaceholder"
// or to "kContents" but not if it's set to "kAttribute". Similarly, if there
// is a button without any unignored children, it's name can only be
// equivalent to its inner text if "NameFrom" is set to "kContents" or to
// "kValue", but not if it is set to "kAttribute".
if (IsText(role)) {
SetNameFrom(ax::mojom::NameFrom::kContents);
} else {
SetNameFrom(ax::mojom::NameFrom::kAttribute);
}
}
void AXNodeData::SetName(const std::u16string& name) {
SetName(base::UTF16ToUTF8(name));
}
void AXNodeData::SetNameChecked(const std::string& name) {
SetName(name);
// We do this check after calling `SetName` because `SetName` handles the
// case where it is called after `SetNameExplicitlyEmpty` by removing the
// existing `NameFrom::kAttributeExplicitlyEmpty`.
DCHECK_EQ(name.empty(),
GetNameFrom() == ax::mojom::NameFrom::kAttributeExplicitlyEmpty)
<< "If the accessible name of Role::" << role << " class: '"
<< GetStringAttribute(ax::mojom::StringAttribute::kClassName)
<< "' is being set to an empty string to improve the "
"user experience, call `SetNameExplicitlyEmpty` instead of `SetName`.";
}
void AXNodeData::SetNameChecked(const std::u16string& name) {
SetNameChecked(base::UTF16ToUTF8(name));
}
void AXNodeData::SetNameExplicitlyEmpty() {
SetNameFrom(ax::mojom::NameFrom::kAttributeExplicitlyEmpty);
SetName(std::string());
}
void AXNodeData::SetDescription(const std::string& description) {
AddStringAttribute(ax::mojom::StringAttribute::kDescription, description);
// It is possible for `SetDescription` to be called after
// `SetDescriptionExplicitlyEmpty`.
if (!description.empty() &&
GetDescriptionFrom() ==
ax::mojom::DescriptionFrom::kAttributeExplicitlyEmpty) {
RemoveIntAttribute(ax::mojom::IntAttribute::kDescriptionFrom);
}
DCHECK_EQ(description.empty(),
GetDescriptionFrom() ==
ax::mojom::DescriptionFrom::kAttributeExplicitlyEmpty)
<< "If the accessible description of Role::" << role << " class: '"
<< GetStringAttribute(ax::mojom::StringAttribute::kClassName)
<< "' is being set to an empty string to improve the user experience, "
"call SetDescriptionExplicitlyEmpty instead of SetDescription.";
if (HasIntAttribute(ax::mojom::IntAttribute::kDescriptionFrom))
return;
SetDescriptionFrom(ax::mojom::DescriptionFrom::kAriaDescription);
}
void AXNodeData::SetDescription(const std::u16string& description) {
SetDescription(base::UTF16ToUTF8(description));
}
void AXNodeData::SetDescriptionExplicitlyEmpty() {
SetDescriptionFrom(ax::mojom::DescriptionFrom::kAttributeExplicitlyEmpty);
SetDescription(std::string());
}
void AXNodeData::SetValue(const std::string& value) {
AddStringAttribute(ax::mojom::StringAttribute::kValue, value);
}
void AXNodeData::SetValue(const std::u16string& value) {
SetValue(base::UTF16ToUTF8(value));
}
bool AXNodeData::HasState(ax::mojom::State state_enum) const {
return IsFlagSet(state, static_cast<uint32_t>(state_enum));
}
bool AXNodeData::HasAction(ax::mojom::Action action) const {
return IsFlagSet(actions, static_cast<uint32_t>(action));
}
bool AXNodeData::HasTextStyle(ax::mojom::TextStyle text_style_enum) const {
int32_t style = GetIntAttribute(ax::mojom::IntAttribute::kTextStyle);
return IsFlagSet(static_cast<uint32_t>(style),
static_cast<uint32_t>(text_style_enum));
}
bool AXNodeData::HasDropeffect(ax::mojom::Dropeffect dropeffect_enum) const {
int32_t dropeffect =
GetIntAttribute(ax::mojom::IntAttribute::kDropeffectDeprecated);
return IsFlagSet(static_cast<uint32_t>(dropeffect),
static_cast<uint32_t>(dropeffect_enum));
}
void AXNodeData::AddState(ax::mojom::State state_enum) {
DCHECK_GT(static_cast<int>(state_enum),
static_cast<int>(ax::mojom::State::kNone));
DCHECK_LE(static_cast<int>(state_enum),
static_cast<int>(ax::mojom::State::kMaxValue));
state = ModifyFlag(state, static_cast<uint32_t>(state_enum), true);
}
void AXNodeData::RemoveState(ax::mojom::State state_enum) {
DCHECK_GT(static_cast<int>(state_enum),
static_cast<int>(ax::mojom::State::kNone));
DCHECK_LE(static_cast<int>(state_enum),
static_cast<int>(ax::mojom::State::kMaxValue));
state = ModifyFlag(state, static_cast<uint32_t>(state_enum), false);
}
void AXNodeData::AddAction(ax::mojom::Action action_enum) {
switch (action_enum) {
case ax::mojom::Action::kNone:
NOTREACHED();
break;
// Note: all of the attributes are included here explicitly, rather than
// using "default:", so that it's a compiler error to add a new action
// without explicitly considering whether there are mutually exclusive
// actions that can be performed on a UI control at the same time.
case ax::mojom::Action::kBlur:
case ax::mojom::Action::kFocus: {
ax::mojom::Action excluded_action =
(action_enum == ax::mojom::Action::kBlur) ? ax::mojom::Action::kFocus
: ax::mojom::Action::kBlur;
DCHECK(!HasAction(excluded_action)) << excluded_action;
break;
}
case ax::mojom::Action::kClearAccessibilityFocus:
case ax::mojom::Action::kCollapse:
case ax::mojom::Action::kCustomAction:
case ax::mojom::Action::kDecrement:
case ax::mojom::Action::kDoDefault:
case ax::mojom::Action::kExpand:
case ax::mojom::Action::kGetImageData:
case ax::mojom::Action::kHitTest:
case ax::mojom::Action::kIncrement:
case ax::mojom::Action::kInternalInvalidateTree:
case ax::mojom::Action::kLoadInlineTextBoxes:
case ax::mojom::Action::kReplaceSelectedText:
case ax::mojom::Action::kScrollToMakeVisible:
case ax::mojom::Action::kScrollToPoint:
case ax::mojom::Action::kScrollToPositionAtRowColumn:
case ax::mojom::Action::kSetAccessibilityFocus:
case ax::mojom::Action::kSetScrollOffset:
case ax::mojom::Action::kSetSelection:
case ax::mojom::Action::kSetSequentialFocusNavigationStartingPoint:
case ax::mojom::Action::kSetValue:
case ax::mojom::Action::kShowContextMenu:
case ax::mojom::Action::kScrollBackward:
case ax::mojom::Action::kScrollForward:
case ax::mojom::Action::kScrollUp:
case ax::mojom::Action::kScrollDown:
case ax::mojom::Action::kScrollLeft:
case ax::mojom::Action::kScrollRight:
case ax::mojom::Action::kGetTextLocation:
case ax::mojom::Action::kAnnotatePageImages:
case ax::mojom::Action::kSignalEndOfTest:
case ax::mojom::Action::kHideTooltip:
case ax::mojom::Action::kShowTooltip:
case ax::mojom::Action::kStitchChildTree:
case ax::mojom::Action::kResumeMedia:
case ax::mojom::Action::kStartDuckingMedia:
case ax::mojom::Action::kStopDuckingMedia:
case ax::mojom::Action::kSuspendMedia:
case ax::mojom::Action::kLongClick:
break;
}
actions = ModifyFlag(actions, static_cast<uint32_t>(action_enum), true);
}
void AXNodeData::AddTextStyle(ax::mojom::TextStyle text_style_enum) {
DCHECK_GE(static_cast<int>(text_style_enum),
static_cast<int>(ax::mojom::TextStyle::kMinValue));
DCHECK_LE(static_cast<int>(text_style_enum),
static_cast<int>(ax::mojom::TextStyle::kMaxValue));
int32_t style = GetIntAttribute(ax::mojom::IntAttribute::kTextStyle);
style = ModifyFlag(static_cast<uint32_t>(style),
static_cast<uint32_t>(text_style_enum), true);
RemoveIntAttribute(ax::mojom::IntAttribute::kTextStyle);
AddIntAttribute(ax::mojom::IntAttribute::kTextStyle, style);
}
ax::mojom::CheckedState AXNodeData::GetCheckedState() const {
return static_cast<ax::mojom::CheckedState>(
GetIntAttribute(ax::mojom::IntAttribute::kCheckedState));
}
void AXNodeData::SetCheckedState(ax::mojom::CheckedState checked_state) {
if (HasCheckedState())
RemoveIntAttribute(ax::mojom::IntAttribute::kCheckedState);
if (checked_state != ax::mojom::CheckedState::kNone) {
AddIntAttribute(ax::mojom::IntAttribute::kCheckedState,
static_cast<int32_t>(checked_state));
}
}
bool AXNodeData::HasCheckedState() const {
return HasIntAttribute(ax::mojom::IntAttribute::kCheckedState);
}
ax::mojom::DefaultActionVerb AXNodeData::GetDefaultActionVerb() const {
return static_cast<ax::mojom::DefaultActionVerb>(
GetIntAttribute(ax::mojom::IntAttribute::kDefaultActionVerb));
}
void AXNodeData::SetDefaultActionVerb(
ax::mojom::DefaultActionVerb default_action_verb) {
if (HasIntAttribute(ax::mojom::IntAttribute::kDefaultActionVerb))
RemoveIntAttribute(ax::mojom::IntAttribute::kDefaultActionVerb);
if (default_action_verb != ax::mojom::DefaultActionVerb::kNone) {
AddIntAttribute(ax::mojom::IntAttribute::kDefaultActionVerb,
static_cast<int32_t>(default_action_verb));
}
}
ax::mojom::HasPopup AXNodeData::GetHasPopup() const {
return static_cast<ax::mojom::HasPopup>(
GetIntAttribute(ax::mojom::IntAttribute::kHasPopup));
}
void AXNodeData::SetHasPopup(ax::mojom::HasPopup has_popup) {
if (HasIntAttribute(ax::mojom::IntAttribute::kHasPopup))
RemoveIntAttribute(ax::mojom::IntAttribute::kHasPopup);
if (has_popup != ax::mojom::HasPopup::kFalse) {
AddIntAttribute(ax::mojom::IntAttribute::kHasPopup,
static_cast<int32_t>(has_popup));
}
}
ax::mojom::IsPopup AXNodeData::GetIsPopup() const {
return static_cast<ax::mojom::IsPopup>(
GetIntAttribute(ax::mojom::IntAttribute::kIsPopup));
}
void AXNodeData::SetIsPopup(ax::mojom::IsPopup is_popup) {
if (HasIntAttribute(ax::mojom::IntAttribute::kIsPopup)) {
RemoveIntAttribute(ax::mojom::IntAttribute::kIsPopup);
}
if (is_popup != ax::mojom::IsPopup::kNone) {
AddIntAttribute(ax::mojom::IntAttribute::kIsPopup,
static_cast<int32_t>(is_popup));
}
}
ax::mojom::InvalidState AXNodeData::GetInvalidState() const {
return static_cast<ax::mojom::InvalidState>(
GetIntAttribute(ax::mojom::IntAttribute::kInvalidState));
}
void AXNodeData::SetInvalidState(ax::mojom::InvalidState invalid_state) {
if (HasIntAttribute(ax::mojom::IntAttribute::kInvalidState))
RemoveIntAttribute(ax::mojom::IntAttribute::kInvalidState);
if (invalid_state != ax::mojom::InvalidState::kNone) {
AddIntAttribute(ax::mojom::IntAttribute::kInvalidState,
static_cast<int32_t>(invalid_state));
}
}
ax::mojom::NameFrom AXNodeData::GetNameFrom() const {
return static_cast<ax::mojom::NameFrom>(
GetIntAttribute(ax::mojom::IntAttribute::kNameFrom));
}
void AXNodeData::SetNameFrom(ax::mojom::NameFrom name_from) {
if (HasIntAttribute(ax::mojom::IntAttribute::kNameFrom))
RemoveIntAttribute(ax::mojom::IntAttribute::kNameFrom);
if (name_from != ax::mojom::NameFrom::kNone) {
AddIntAttribute(ax::mojom::IntAttribute::kNameFrom,
static_cast<int32_t>(name_from));
}
}
ax::mojom::DescriptionFrom AXNodeData::GetDescriptionFrom() const {
return static_cast<ax::mojom::DescriptionFrom>(
GetIntAttribute(ax::mojom::IntAttribute::kDescriptionFrom));
}
void AXNodeData::SetDescriptionFrom(
ax::mojom::DescriptionFrom description_from) {
if (HasIntAttribute(ax::mojom::IntAttribute::kDescriptionFrom))
RemoveIntAttribute(ax::mojom::IntAttribute::kDescriptionFrom);
if (description_from != ax::mojom::DescriptionFrom::kNone) {
AddIntAttribute(ax::mojom::IntAttribute::kDescriptionFrom,
static_cast<int32_t>(description_from));
}
}
ax::mojom::TextPosition AXNodeData::GetTextPosition() const {
return static_cast<ax::mojom::TextPosition>(
GetIntAttribute(ax::mojom::IntAttribute::kTextPosition));
}
void AXNodeData::SetTextPosition(ax::mojom::TextPosition text_position) {
if (HasIntAttribute(ax::mojom::IntAttribute::kTextPosition))
RemoveIntAttribute(ax::mojom::IntAttribute::kTextPosition);
if (text_position != ax::mojom::TextPosition::kNone) {
AddIntAttribute(ax::mojom::IntAttribute::kTextPosition,
static_cast<int32_t>(text_position));
}
}
ax::mojom::ImageAnnotationStatus AXNodeData::GetImageAnnotationStatus() const {
return static_cast<ax::mojom::ImageAnnotationStatus>(
GetIntAttribute(ax::mojom::IntAttribute::kImageAnnotationStatus));
}
void AXNodeData::SetImageAnnotationStatus(
ax::mojom::ImageAnnotationStatus status) {
if (HasIntAttribute(ax::mojom::IntAttribute::kImageAnnotationStatus))
RemoveIntAttribute(ax::mojom::IntAttribute::kImageAnnotationStatus);
if (status != ax::mojom::ImageAnnotationStatus::kNone) {
AddIntAttribute(ax::mojom::IntAttribute::kImageAnnotationStatus,
static_cast<int32_t>(status));
}
}
ax::mojom::Restriction AXNodeData::GetRestriction() const {
return static_cast<ax::mojom::Restriction>(
GetIntAttribute(ax::mojom::IntAttribute::kRestriction));
}
void AXNodeData::SetRestriction(ax::mojom::Restriction restriction) {
if (HasIntAttribute(ax::mojom::IntAttribute::kRestriction))
RemoveIntAttribute(ax::mojom::IntAttribute::kRestriction);
if (restriction != ax::mojom::Restriction::kNone) {
AddIntAttribute(ax::mojom::IntAttribute::kRestriction,
static_cast<int32_t>(restriction));
}
}
ax::mojom::ListStyle AXNodeData::GetListStyle() const {