/
AccessibilityUIElementAtk.cpp
2360 lines (1961 loc) · 76.3 KB
/
AccessibilityUIElementAtk.cpp
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) 2011 Apple Inc. All Rights Reserved.
* Copyright (C) 2012 Igalia S.L.
* Copyright (C) 2013 Samsung Electronics. 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 INC. OR
* 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 "AccessibilityUIElement.h"
#if HAVE(ACCESSIBILITY)
#include "InjectedBundle.h"
#include "InjectedBundlePage.h"
#include "NotImplemented.h"
#include <JavaScriptCore/JSStringRef.h>
#include <JavaScriptCore/OpaqueJSString.h>
#if ATK_CHECK_VERSION(2,11,90)
#include <WebKit/WKBundleFrame.h>
#endif
#include <atk/atk.h>
#include <wtf/Assertions.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/GUniquePtr.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/WTFString.h>
#include <wtf/unicode/CharacterNames.h>
namespace WTR {
namespace {
#if ATK_CHECK_VERSION(2,11,92)
enum RangeLimit {
RangeLimitMinimum,
RangeLimitMaximum
};
#endif
enum AtkAttributeType {
ObjectAttributeType,
TextAttributeType
};
enum AttributeDomain {
CoreDomain = 0,
AtkDomain
};
enum AttributesIndex {
// Attribute names.
InvalidNameIndex = 0,
ColumnCount,
ColumnIndex,
ColumnSpan,
RowCount,
RowIndex,
RowSpan,
PosInSetIndex,
SetSizeIndex,
PlaceholderNameIndex,
SortNameIndex,
CurrentNameIndex,
AriaLiveNameIndex,
AriaAtomicNameIndex,
AriaRelevantNameIndex,
BusyNameIndex,
// Attribute values.
SortAscendingValueIndex,
SortDescendingValueIndex,
SortUnknownValueIndex,
NumberOfAttributes
};
// Attribute names & Values (keep on sync with enum AttributesIndex).
const String attributesMap[][2] = {
// Attribute names.
{ "AXInvalid", "invalid" },
{ "AXARIAColumnCount", "colcount" },
{ "AXARIAColumnIndex", "colindex" },
{ "AXARIAColumnSpan", "colspan" },
{ "AXARIARowCount", "rowcount" },
{ "AXARIARowIndex", "rowindex" },
{ "AXARIARowSpan", "rowspan" },
{ "AXARIAPosInSet", "posinset" },
{ "AXARIASetSize", "setsize" },
{ "AXPlaceholderValue", "placeholder-text" } ,
{ "AXSortDirection", "sort" },
{ "AXARIACurrent", "current" },
{ "AXARIALive", "live" },
{ "AXARIAAtomic", "atomic" },
{ "AXARIARelevant", "relevant" },
{ "AXElementBusy", "busy" },
// Attribute values.
{ "AXAscendingSortDirection", "ascending" },
{ "AXDescendingSortDirection", "descending" },
{ "AXUnknownSortDirection", "unknown" }
};
#if ATK_CHECK_VERSION(2, 11, 3)
const char* landmarkStringBanner = "AXLandmarkBanner";
const char* landmarkStringComplementary = "AXLandmarkComplementary";
const char* landmarkStringContentinfo = "AXLandmarkContentInfo";
const char* landmarkStringMain = "AXLandmarkMain";
const char* landmarkStringNavigation = "AXLandmarkNavigation";
const char* landmarkStringRegion = "AXLandmarkRegion";
const char* landmarkStringSearch = "AXLandmarkSearch";
#endif
String jsStringToWTFString(JSStringRef attribute)
{
size_t bufferSize = JSStringGetMaximumUTF8CStringSize(attribute);
GUniquePtr<gchar> buffer(static_cast<gchar*>(g_malloc(bufferSize)));
JSStringGetUTF8CString(attribute, buffer.get(), bufferSize);
return String::fromUTF8(buffer.get());
}
String coreAttributeToAtkAttribute(JSStringRef attribute)
{
String attributeString = jsStringToWTFString(attribute);
for (int i = 0; i < NumberOfAttributes; ++i) {
if (attributesMap[i][CoreDomain] == attributeString)
return attributesMap[i][AtkDomain];
}
return attributeString;
}
String atkAttributeValueToCoreAttributeValue(AtkAttributeType type, const String& id, const String& value)
{
if (type == ObjectAttributeType) {
// We don't expose the "current" attribute if there is no author-provided value.
if (id == attributesMap[CurrentNameIndex][AtkDomain] && value.isEmpty())
return "false";
// We need to translate ATK values exposed for 'aria-sort' (e.g. 'ascending')
// into those expected by the layout tests (e.g. 'AXAscendingSortDirection').
if (id == attributesMap[SortNameIndex][AtkDomain] && !value.isEmpty()) {
if (value == attributesMap[SortAscendingValueIndex][AtkDomain])
return attributesMap[SortAscendingValueIndex][CoreDomain];
if (value == attributesMap[SortDescendingValueIndex][AtkDomain])
return attributesMap[SortDescendingValueIndex][CoreDomain];
return attributesMap[SortUnknownValueIndex][CoreDomain];
}
} else if (type == TextAttributeType) {
// In case of 'aria-invalid' when the attribute empty or has "false" for ATK
// it should not be mapped at all, but layout tests will expect 'false'.
if (id == attributesMap[InvalidNameIndex][AtkDomain] && value.isEmpty())
return "false";
}
return value;
}
AtkAttributeSet* getAttributeSet(AtkObject* accessible, AtkAttributeType type)
{
if (type == ObjectAttributeType)
return atk_object_get_attributes(accessible);
if (type == TextAttributeType) {
if (!ATK_IS_TEXT(accessible))
return nullptr;
return atk_text_get_default_attributes(ATK_TEXT(accessible));
}
ASSERT_NOT_REACHED();
return nullptr;
}
String getAttributeSetValueForId(AtkObject* accessible, AtkAttributeType type, String id)
{
AtkAttributeSet* attributeSet = getAttributeSet(accessible, type);
if (!attributeSet)
return String();
String attributeValue;
for (AtkAttributeSet* attributes = attributeSet; attributes; attributes = attributes->next) {
AtkAttribute* atkAttribute = static_cast<AtkAttribute*>(attributes->data);
if (id == atkAttribute->name) {
attributeValue = String::fromUTF8(atkAttribute->value);
break;
}
}
atk_attribute_set_free(attributeSet);
return atkAttributeValueToCoreAttributeValue(type, id, attributeValue);
}
String attributeSetToString(AtkAttributeSet* attributeSet, String separator=", ")
{
if (!attributeSet)
return String();
StringBuilder builder;
for (AtkAttributeSet* attributes = attributeSet; attributes; attributes = attributes->next) {
AtkAttribute* attribute = static_cast<AtkAttribute*>(attributes->data);
GUniquePtr<gchar> attributeData(g_strconcat(attribute->name, ":", attribute->value, NULL));
builder.append(attributeData.get());
if (attributes->next)
builder.append(separator);
}
atk_attribute_set_free(attributeSet);
return builder.toString();
}
String getAtkAttributeSetAsString(AtkObject* accessible, AtkAttributeType type, String separator=", ")
{
return attributeSetToString(getAttributeSet(accessible, type), separator);
}
bool checkElementState(PlatformUIElement element, AtkStateType stateType)
{
if (!ATK_IS_OBJECT(element.get()))
return false;
GRefPtr<AtkStateSet> stateSet = adoptGRef(atk_object_ref_state_set(ATK_OBJECT(element.get())));
return atk_state_set_contains_state(stateSet.get(), stateType);
}
JSStringRef indexRangeInTable(PlatformUIElement element, bool isRowRange)
{
GUniquePtr<gchar> rangeString(g_strdup("{0, 0}"));
#if ATK_CHECK_VERSION(2,11,90)
if (!ATK_IS_TABLE_CELL(element.get()))
return JSStringCreateWithUTF8CString(rangeString.get());
#else
if (!ATK_IS_OBJECT(element.get()))
return JSStringCreateWithUTF8CString(rangeString.get());
AtkObject* axTable = atk_object_get_parent(ATK_OBJECT(element.get()));
if (!axTable || !ATK_IS_TABLE(axTable))
return JSStringCreateWithUTF8CString(rangeString.get());
// Look for the cell in the table.
gint indexInParent = atk_object_get_index_in_parent(ATK_OBJECT(element.get()));
if (indexInParent == -1)
return JSStringCreateWithUTF8CString(rangeString.get());
#endif
gint row = -1;
gint column = -1;
gint rowSpan = -1;
gint columnSpan = -1;
#if ATK_CHECK_VERSION(2,11,90)
atk_table_cell_get_row_column_span(ATK_TABLE_CELL(element.get()), &row, &column, &rowSpan, &columnSpan);
#else
row = atk_table_get_row_at_index(ATK_TABLE(axTable), indexInParent);
column = atk_table_get_column_at_index(ATK_TABLE(axTable), indexInParent);
rowSpan = atk_table_get_row_extent_at(ATK_TABLE(axTable), row, column);
columnSpan = atk_table_get_column_extent_at(ATK_TABLE(axTable), row, column);
#endif
// Get the actual values, if row and columns are valid values.
if (row != -1 && column != -1) {
int base = 0;
int length = 0;
if (isRowRange) {
base = row;
length = rowSpan;
} else {
base = column;
length = columnSpan;
}
rangeString.reset(g_strdup_printf("{%d, %d}", base, length));
}
return JSStringCreateWithUTF8CString(rangeString.get());
}
void alterCurrentValue(PlatformUIElement element, int factor)
{
if (!ATK_IS_VALUE(element.get()))
return;
#if ATK_CHECK_VERSION(2,11,92)
double currentValue;
atk_value_get_value_and_text(ATK_VALUE(element.get()), ¤tValue, nullptr);
double increment = atk_value_get_increment(ATK_VALUE(element.get()));
atk_value_set_value(ATK_VALUE(element.get()), currentValue + factor * increment);
#else
GValue currentValue = G_VALUE_INIT;
atk_value_get_current_value(ATK_VALUE(element.get()), ¤tValue);
GValue increment = G_VALUE_INIT;
atk_value_get_minimum_increment(ATK_VALUE(element.get()), &increment);
GValue newValue = G_VALUE_INIT;
g_value_init(&newValue, G_TYPE_FLOAT);
g_value_set_float(&newValue, g_value_get_float(¤tValue) + factor * g_value_get_float(&increment));
atk_value_set_current_value(ATK_VALUE(element.get()), &newValue);
g_value_unset(&newValue);
g_value_unset(&increment);
g_value_unset(¤tValue);
#endif
}
gchar* replaceCharactersForResults(gchar* str)
{
WTF::String uString = WTF::String::fromUTF8(str);
// The object replacement character is passed along to ATs so we need to be
// able to test for their presence and do so without causing test failures.
uString.replace(objectReplacementCharacter, "<obj>");
// The presence of newline characters in accessible text of a single object
// is appropriate, but it makes test results (especially the accessible tree)
// harder to read.
uString.replace("\n", "<\\n>");
return g_strdup(uString.utf8().data());
}
const gchar* roleToString(AtkObject* object)
{
AtkRole role = atk_object_get_role(object);
#if ATK_CHECK_VERSION(2, 11, 3)
if (role == ATK_ROLE_LANDMARK) {
String xmlRolesValue = getAttributeSetValueForId(object, ObjectAttributeType, "xml-roles");
if (equalLettersIgnoringASCIICase(xmlRolesValue, "banner"))
return landmarkStringBanner;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "complementary"))
return landmarkStringComplementary;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "contentinfo"))
return landmarkStringContentinfo;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-acknowledgments"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-afterword"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-appendix"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-bibliography"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-chapter"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-conclusion"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-credits"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-endnotes"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-epilogue"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-errata"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-foreword"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-glossary"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-glossref"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-index"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-introduction"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-pagelist"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-part"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-preface"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-prologue"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "doc-toc"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "main"))
return landmarkStringMain;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "navigation"))
return landmarkStringNavigation;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "region"))
return landmarkStringRegion;
if (equalLettersIgnoringASCIICase(xmlRolesValue, "search"))
return landmarkStringSearch;
}
#endif
switch (role) {
case ATK_ROLE_ALERT:
return "AXAlert";
case ATK_ROLE_DIALOG:
return "AXDialog";
case ATK_ROLE_CANVAS:
return "AXCanvas";
case ATK_ROLE_CAPTION:
return "AXCaption";
case ATK_ROLE_CHECK_BOX:
return "AXCheckBox";
case ATK_ROLE_COLOR_CHOOSER:
return "AXColorWell";
case ATK_ROLE_COLUMN_HEADER:
return "AXColumnHeader";
case ATK_ROLE_COMBO_BOX:
return "AXComboBox";
case ATK_ROLE_COMMENT:
return "AXComment";
case ATK_ROLE_DOCUMENT_FRAME:
return "AXDocument";
case ATK_ROLE_DOCUMENT_WEB:
return "AXWebArea";
case ATK_ROLE_EMBEDDED:
return "AXEmbedded";
case ATK_ROLE_ENTRY:
return "AXTextField";
case ATK_ROLE_FOOTER:
return "AXFooter";
case ATK_ROLE_FORM:
return "AXForm";
case ATK_ROLE_GROUPING:
return "AXGroup";
case ATK_ROLE_HEADING:
return "AXHeading";
case ATK_ROLE_IMAGE:
return "AXImage";
case ATK_ROLE_IMAGE_MAP:
return "AXImageMap";
case ATK_ROLE_INVALID:
return "AXInvalid";
case ATK_ROLE_LABEL:
return "AXLabel";
case ATK_ROLE_LEVEL_BAR:
return "AXProgressIndicator";
case ATK_ROLE_LINK:
return "AXLink";
case ATK_ROLE_LIST:
return "AXList";
case ATK_ROLE_LIST_BOX:
return "AXListBox";
case ATK_ROLE_LIST_ITEM:
return "AXListItem";
case ATK_ROLE_MENU:
return "AXMenu";
case ATK_ROLE_MENU_BAR:
return "AXMenuBar";
case ATK_ROLE_MENU_ITEM:
return "AXMenuItem";
case ATK_ROLE_PAGE_TAB:
return "AXTab";
case ATK_ROLE_PAGE_TAB_LIST:
return "AXTabGroup";
case ATK_ROLE_PANEL:
return "AXGroup";
case ATK_ROLE_PARAGRAPH:
return "AXParagraph";
case ATK_ROLE_PASSWORD_TEXT:
return "AXPasswordField";
case ATK_ROLE_PROGRESS_BAR:
return "AXProgressIndicator";
case ATK_ROLE_PUSH_BUTTON:
return "AXButton";
case ATK_ROLE_RADIO_BUTTON:
return "AXRadioButton";
case ATK_ROLE_RADIO_MENU_ITEM:
return "AXRadioMenuItem";
case ATK_ROLE_ROW_HEADER:
return "AXRowHeader";
case ATK_ROLE_CHECK_MENU_ITEM:
return "AXCheckMenuItem";
case ATK_ROLE_RULER:
return "AXRuler";
case ATK_ROLE_SCROLL_BAR:
return "AXScrollBar";
case ATK_ROLE_SCROLL_PANE:
return "AXScrollArea";
case ATK_ROLE_SECTION:
return "AXSection";
case ATK_ROLE_SEPARATOR:
return "AXSeparator";
case ATK_ROLE_SLIDER:
return "AXSlider";
case ATK_ROLE_SPIN_BUTTON:
return "AXSpinButton";
case ATK_ROLE_STATUSBAR:
return "AXStatusBar";
case ATK_ROLE_TABLE:
return "AXTable";
case ATK_ROLE_TABLE_CELL:
return "AXCell";
case ATK_ROLE_TABLE_COLUMN_HEADER:
return "AXColumnHeader";
case ATK_ROLE_TABLE_ROW:
return "AXRow";
case ATK_ROLE_TABLE_ROW_HEADER:
return "AXRowHeader";
case ATK_ROLE_TOGGLE_BUTTON:
return "AXToggleButton";
case ATK_ROLE_TOOL_BAR:
return "AXToolbar";
case ATK_ROLE_TOOL_TIP:
return "AXUserInterfaceTooltip";
case ATK_ROLE_TREE:
return "AXTree";
case ATK_ROLE_TREE_TABLE:
return "AXTreeGrid";
case ATK_ROLE_TREE_ITEM:
return "AXTreeItem";
case ATK_ROLE_WINDOW:
return "AXWindow";
case ATK_ROLE_UNKNOWN:
return "AXUnknown";
#if ATK_CHECK_VERSION(2, 11, 3)
case ATK_ROLE_ARTICLE:
return "AXArticle";
case ATK_ROLE_AUDIO:
return "AXAudio";
case ATK_ROLE_BLOCK_QUOTE:
return "AXBlockquote";
case ATK_ROLE_DEFINITION:
return "AXDefinition";
case ATK_ROLE_LOG:
return "AXLog";
case ATK_ROLE_MARQUEE:
return "AXMarquee";
case ATK_ROLE_MATH:
return "AXMath";
case ATK_ROLE_TIMER:
return "AXTimer";
case ATK_ROLE_VIDEO:
return "AXVideo";
#endif
#if ATK_CHECK_VERSION(2, 11, 4)
case ATK_ROLE_DESCRIPTION_LIST:
return "AXDescriptionList";
case ATK_ROLE_DESCRIPTION_TERM:
return "AXDescriptionTerm";
case ATK_ROLE_DESCRIPTION_VALUE:
return "AXDescriptionValue";
#endif
#if ATK_CHECK_VERSION(2, 15, 2)
case ATK_ROLE_STATIC:
return "AXStatic";
#endif
#if ATK_CHECK_VERSION(2, 15, 4)
case ATK_ROLE_MATH_FRACTION:
return "AXMathFraction";
case ATK_ROLE_MATH_ROOT:
return "AXMathRoot";
case ATK_ROLE_SUBSCRIPT:
return "AXSubscript";
case ATK_ROLE_SUPERSCRIPT:
return "AXSuperscript";
#endif
#if ATK_CHECK_VERSION(2, 25, 2)
case ATK_ROLE_FOOTNOTE:
return "AXFootnote";
#endif
default:
// We want to distinguish ATK_ROLE_UNKNOWN from a known AtkRole which
// our DRT isn't properly handling.
return "FIXME not identified";
}
}
String selectedText(AtkObject* accessible)
{
if (!ATK_IS_TEXT(accessible))
return String();
AtkText* text = ATK_TEXT(accessible);
gint start, end;
g_free(atk_text_get_selection(text, 0, &start, &end));
return atk_text_get_text(text, start, end);
}
String attributesOfElement(AccessibilityUIElement* element)
{
StringBuilder builder;
builder.append(String::format("%s\n", element->role()->string().utf8().data()));
// For the parent we print its role and its name, if available.
builder.append("AXParent: ");
RefPtr<AccessibilityUIElement> parent = element->parentElement();
AtkObject* atkParent = parent ? parent->platformUIElement().get() : nullptr;
if (atkParent) {
builder.append(roleToString(atkParent));
const char* parentName = atk_object_get_name(atkParent);
if (parentName && g_utf8_strlen(parentName, -1))
builder.append(String::format(": %s", parentName));
} else
builder.append("(null)");
builder.append("\n");
builder.append(String::format("AXChildren: %d\n", element->childrenCount()));
builder.append(String::format("AXPosition: { %f, %f }\n", element->x(), element->y()));
builder.append(String::format("AXSize: { %f, %f }\n", element->width(), element->height()));
String title = element->title()->string();
if (!title.isEmpty())
builder.append(String::format("%s\n", title.utf8().data()));
String description = element->description()->string();
if (!description.isEmpty())
builder.append(String::format("%s\n", description.utf8().data()));
String value = element->stringValue()->string();
if (!value.isEmpty())
builder.append(String::format("%s\n", value.utf8().data()));
builder.append(String::format("AXFocusable: %d\n", element->isFocusable()));
builder.append(String::format("AXFocused: %d\n", element->isFocused()));
builder.append(String::format("AXSelectable: %d\n", element->isSelectable()));
builder.append(String::format("AXSelected: %d\n", element->isSelected()));
builder.append(String::format("AXMultiSelectable: %d\n", element->isMultiSelectable()));
builder.append(String::format("AXEnabled: %d\n", element->isEnabled()));
builder.append(String::format("AXExpanded: %d\n", element->isExpanded()));
builder.append(String::format("AXRequired: %d\n", element->isRequired()));
builder.append(String::format("AXChecked: %d\n", element->isChecked()));
String url = element->url()->string();
if (!url.isEmpty())
builder.append(String::format("%s\n", url.utf8().data()));
// We append the ATK specific attributes as a single line at the end.
builder.append("AXPlatformAttributes: ");
builder.append(getAtkAttributeSetAsString(element->platformUIElement().get(), ObjectAttributeType));
return builder.toString();
}
static JSRetainPtr<JSStringRef> createStringWithAttributes(const Vector<RefPtr<AccessibilityUIElement> >& elements)
{
StringBuilder builder;
for (Vector<RefPtr<AccessibilityUIElement> >::const_iterator it = elements.begin(); it != elements.end(); ++it) {
builder.append(attributesOfElement(const_cast<AccessibilityUIElement*>(it->get())));
builder.append("\n------------\n");
}
return JSStringCreateWithUTF8CString(builder.toString().utf8().data());
}
static Vector<RefPtr<AccessibilityUIElement> > getRowHeaders(AtkTable* accessible)
{
Vector<RefPtr<AccessibilityUIElement> > rowHeaders;
int rowsCount = atk_table_get_n_rows(accessible);
for (int row = 0; row < rowsCount; ++row)
rowHeaders.append(AccessibilityUIElement::create(atk_table_get_row_header(accessible, row)));
return rowHeaders;
}
static Vector<RefPtr<AccessibilityUIElement> > getColumnHeaders(AtkTable* accessible)
{
Vector<RefPtr<AccessibilityUIElement> > columnHeaders;
int columnsCount = atk_table_get_n_columns(accessible);
for (int column = 0; column < columnsCount; ++column)
columnHeaders.append(AccessibilityUIElement::create(atk_table_get_column_header(accessible, column)));
return columnHeaders;
}
static Vector<RefPtr<AccessibilityUIElement> > getVisibleCells(AccessibilityUIElement* element)
{
Vector<RefPtr<AccessibilityUIElement> > visibleCells;
AtkTable* accessible = ATK_TABLE(element->platformUIElement().get());
int rowsCount = atk_table_get_n_rows(accessible);
int columnsCount = atk_table_get_n_columns(accessible);
for (int row = 0; row < rowsCount; ++row) {
for (int column = 0; column < columnsCount; ++column)
visibleCells.append(element->cellForColumnAndRow(column, row));
}
return visibleCells;
}
#if ATK_CHECK_VERSION(2,11,90)
static Vector<RefPtr<AccessibilityUIElement>> convertGPtrArrayToVector(const GPtrArray* array)
{
Vector<RefPtr<AccessibilityUIElement>> cells;
for (guint i = 0; i < array->len; i++) {
if (AtkObject* atkObject = static_cast<AtkObject*>(g_ptr_array_index(array, i)))
cells.append(AccessibilityUIElement::create(atkObject));
}
return cells;
}
static JSValueRef convertToJSObjectArray(const Vector<RefPtr<AccessibilityUIElement>>& children)
{
WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::singleton().page()->page());
JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
size_t elementCount = children.size();
auto valueElements = std::make_unique<JSValueRef[]>(elementCount);
for (size_t i = 0; i < elementCount; i++)
valueElements[i] = JSObjectMake(context, children[i]->wrapperClass(), children[i].get());
return JSObjectMakeArray(context, elementCount, valueElements.get(), nullptr);
}
#endif
#if ATK_CHECK_VERSION(2,11,92)
static double rangeMinMaxValue(AtkValue* atkValue, RangeLimit rangeLimit)
{
AtkRange* range = atk_value_get_range(atkValue);
if (!range)
return 0;
double rangeValue = 0;
switch (rangeLimit) {
case RangeLimitMinimum:
rangeValue = atk_range_get_lower_limit(range);
break;
case RangeLimitMaximum:
rangeValue = atk_range_get_upper_limit(range);
break;
};
atk_range_free(range);
return rangeValue;
}
#endif
} // namespace
AccessibilityUIElement::AccessibilityUIElement(PlatformUIElement element)
: m_element(element)
{
}
AccessibilityUIElement::AccessibilityUIElement(const AccessibilityUIElement& other)
: JSWrappable()
, m_element(other.m_element)
{
}
AccessibilityUIElement::~AccessibilityUIElement()
{
}
bool AccessibilityUIElement::isEqual(AccessibilityUIElement* otherElement)
{
return m_element == otherElement->platformUIElement();
}
void AccessibilityUIElement::getChildren(Vector<RefPtr<AccessibilityUIElement> >& children)
{
if (!ATK_IS_OBJECT(m_element.get()))
return;
int count = childrenCount();
for (int i = 0; i < count; i++) {
GRefPtr<AtkObject> child = adoptGRef(atk_object_ref_accessible_child(ATK_OBJECT(m_element.get()), i));
children.append(AccessibilityUIElement::create(child.get()));
}
}
void AccessibilityUIElement::getChildrenWithRange(Vector<RefPtr<AccessibilityUIElement> >& children, unsigned location, unsigned length)
{
if (!ATK_IS_OBJECT(m_element.get()))
return;
unsigned end = location + length;
for (unsigned i = location; i < end; i++) {
GRefPtr<AtkObject> child = adoptGRef(atk_object_ref_accessible_child(ATK_OBJECT(m_element.get()), i));
children.append(AccessibilityUIElement::create(child.get()));
}
}
int AccessibilityUIElement::childrenCount()
{
if (!ATK_IS_OBJECT(m_element.get()))
return 0;
return atk_object_get_n_accessible_children(ATK_OBJECT(m_element.get()));
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::elementAtPoint(int x, int y)
{
if (!ATK_IS_COMPONENT(m_element.get()))
return nullptr;
GRefPtr<AtkObject> objectAtPoint = adoptGRef(atk_component_ref_accessible_at_point(ATK_COMPONENT(m_element.get()), x, y, ATK_XY_WINDOW));
return AccessibilityUIElement::create(objectAtPoint ? objectAtPoint.get() : m_element.get());
}
unsigned AccessibilityUIElement::indexOfChild(AccessibilityUIElement* element)
{
if (!ATK_IS_OBJECT(m_element.get()))
return 0;
Vector<RefPtr<AccessibilityUIElement> > children;
getChildren(children);
unsigned count = children.size();
for (unsigned i = 0; i < count; i++)
if (children[i]->isEqual(element))
return i;
return 0;
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::childAtIndex(unsigned index)
{
if (!ATK_IS_OBJECT(m_element.get()))
return nullptr;
Vector<RefPtr<AccessibilityUIElement> > children;
getChildrenWithRange(children, index, 1);
if (children.size() == 1)
return children[0];
return nullptr;
}
static RefPtr<AccessibilityUIElement> accessibilityElementAtIndex(AtkObject* element, AtkRelationType relationType, unsigned index)
{
if (!ATK_IS_OBJECT(element))
return nullptr;
AtkRelationSet* relationSet = atk_object_ref_relation_set(element);
if (!relationSet)
return nullptr;
AtkRelation* relation = atk_relation_set_get_relation_by_type(relationSet, relationType);
if (!relation)
return nullptr;
GPtrArray* targetList = atk_relation_get_target(relation);
if (!targetList || !targetList->len || index >= targetList->len)
return nullptr;
AtkObject* target = static_cast<AtkObject*>(g_ptr_array_index(targetList, index));
g_object_unref(relationSet);
return target ? AccessibilityUIElement::create(target).ptr() : nullptr;
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::linkedUIElementAtIndex(unsigned index)
{
// FIXME: implement
return nullptr;
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaOwnsElementAtIndex(unsigned index)
{
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_NODE_PARENT_OF, index);
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaOwnsReferencingElementAtIndex(unsigned index)
{
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_NODE_CHILD_OF, index);
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaFlowToElementAtIndex(unsigned index)
{
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_FLOWS_TO, index);
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaFlowToReferencingElementAtIndex(unsigned index)
{
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_FLOWS_FROM, index);
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaControlsElementAtIndex(unsigned index)
{
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_CONTROLLER_FOR, index);
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaControlsReferencingElementAtIndex(unsigned index)
{
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_CONTROLLED_BY, index);
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaLabelledByElementAtIndex(unsigned index)
{
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_LABELLED_BY, index);
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaLabelledByReferencingElementAtIndex(unsigned index)
{
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_LABEL_FOR, index);
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaDescribedByElementAtIndex(unsigned index)
{
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_DESCRIBED_BY, index);
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaDescribedByReferencingElementAtIndex(unsigned index)
{
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_DESCRIPTION_FOR, index);
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaDetailsElementAtIndex(unsigned index)
{
#if ATK_CHECK_VERSION(2, 25, 2)
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_DETAILS, index);
#endif
return nullptr;
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaDetailsReferencingElementAtIndex(unsigned index)
{
#if ATK_CHECK_VERSION(2, 25, 2)
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_DETAILS_FOR, index);
#endif
return nullptr;
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaErrorMessageElementAtIndex(unsigned index)
{
#if ATK_CHECK_VERSION(2, 25, 2)
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_ERROR_MESSAGE, index);
#endif
return nullptr;
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::ariaErrorMessageReferencingElementAtIndex(unsigned index)
{
#if ATK_CHECK_VERSION(2, 25, 2)
return accessibilityElementAtIndex(m_element.get(), ATK_RELATION_ERROR_FOR, index);
#endif
return nullptr;
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::disclosedRowAtIndex(unsigned index)
{
// FIXME: implement
return nullptr;
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::rowAtIndex(unsigned index)
{
// ATK doesn't have API to get an accessible row by index directly. It does, however, have
// API to get cells in the row specified by index. The parent of a cell should be the row.
AtkTable* axTable = ATK_TABLE(m_element.get());
unsigned nColumns = columnCount();
for (unsigned col = 0; col < nColumns; col++) {
// Find the first cell in this row that only spans one row.
if (atk_table_get_row_extent_at(axTable, index, col) == 1) {
AtkObject* cell = atk_table_ref_at(axTable, index, col);
return cell ? AccessibilityUIElement::create(atk_object_get_parent(cell)).ptr() : nullptr;
}
}
return nullptr;
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::selectedChildAtIndex(unsigned index) const
{
if (!ATK_SELECTION(m_element.get()))
return nullptr;
GRefPtr<AtkObject> child = adoptGRef(atk_selection_ref_selection(ATK_SELECTION(m_element.get()), index));
return child ? AccessibilityUIElement::create(child.get()).ptr() : nullptr;
}
unsigned AccessibilityUIElement::selectedChildrenCount() const
{
if (!ATK_IS_SELECTION(m_element.get()))
return 0;
return atk_selection_get_selection_count(ATK_SELECTION(m_element.get()));
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::selectedRowAtIndex(unsigned index)
{
// FIXME: implement
return nullptr;
}
RefPtr<AccessibilityUIElement> AccessibilityUIElement::titleUIElement()
{
if (!ATK_IS_OBJECT(m_element.get()))