-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
WebKitAccessibleInterfaceText.cpp
1276 lines (1039 loc) · 52 KB
/
WebKitAccessibleInterfaceText.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) 2008 Nuanti Ltd.
* Copyright (C) 2009 Jan Alonzo
* Copyright (C) 2009, 2010, 2011, 2012 Igalia S.L.
* Copyright (C) 2013 Samsung Electronics. All rights reserved.
*
* Portions from Mozilla a11y, copyright as follows:
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Sun Microsystems, Inc.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WebKitAccessibleInterfaceText.h"
#if ENABLE(ACCESSIBILITY)
#include "AccessibilityObject.h"
#include "Document.h"
#include "Editing.h"
#include "FontCascade.h"
#include "FrameView.h"
#include "HTMLParserIdioms.h"
#include "HostWindow.h"
#include "InlineTextBox.h"
#include "NotImplemented.h"
#include "RenderListItem.h"
#include "RenderListMarker.h"
#include "RenderText.h"
#include "TextEncoding.h"
#include "TextIterator.h"
#include "VisibleUnits.h"
#include "WebKitAccessible.h"
#include "WebKitAccessibleUtil.h"
#include <wtf/glib/GUniquePtr.h>
#include <wtf/text/CString.h>
using namespace WebCore;
// Text attribute to expose the ARIA 'aria-invalid' attribute. Initially initialized
// to ATK_TEXT_ATTR_INVALID (which means 'invalid' text attribute'), will later on
// hold a reference to the custom registered AtkTextAttribute that we will use.
static AtkTextAttribute atkTextAttributeInvalid = ATK_TEXT_ATTR_INVALID;
static AccessibilityObject* core(AtkText* text)
{
if (!WEBKIT_IS_ACCESSIBLE(text))
return 0;
return &webkitAccessibleGetAccessibilityObject(WEBKIT_ACCESSIBLE(text));
}
static int baselinePositionForRenderObject(RenderObject* renderObject)
{
// FIXME: This implementation of baselinePosition originates from RenderObject.cpp and was
// removed in r70072. The implementation looks incorrect though, because this is not the
// baseline of the underlying RenderObject, but of the AccessibilityRenderObject.
const FontMetrics& fontMetrics = renderObject->firstLineStyle().fontMetrics();
return fontMetrics.ascent() + (renderObject->firstLineStyle().computedLineHeight() - fontMetrics.height()) / 2;
}
static AtkAttributeSet* getAttributeSetForAccessibilityObject(const AccessibilityObject* object)
{
if (!object->isAccessibilityRenderObject())
return 0;
RenderObject* renderer = object->renderer();
auto* style = &renderer->style();
AtkAttributeSet* result = nullptr;
GUniquePtr<gchar> buffer(g_strdup_printf("%i", style->computedFontPixelSize()));
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_SIZE), buffer.get());
Color bgColor = style->visitedDependentColor(CSSPropertyBackgroundColor);
if (bgColor.isValid()) {
buffer.reset(g_strdup_printf("%i,%i,%i", bgColor.red(), bgColor.green(), bgColor.blue()));
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_BG_COLOR), buffer.get());
}
Color fgColor = style->visitedDependentColor(CSSPropertyColor);
if (fgColor.isValid()) {
buffer.reset(g_strdup_printf("%i,%i,%i", fgColor.red(), fgColor.green(), fgColor.blue()));
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_FG_COLOR), buffer.get());
}
int baselinePosition;
bool includeRise = true;
switch (style->verticalAlign()) {
case VerticalAlign::Sub:
baselinePosition = -1 * baselinePositionForRenderObject(renderer);
break;
case VerticalAlign::Super:
baselinePosition = baselinePositionForRenderObject(renderer);
break;
case VerticalAlign::Baseline:
baselinePosition = 0;
break;
default:
includeRise = false;
break;
}
if (includeRise) {
buffer.reset(g_strdup_printf("%i", baselinePosition));
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_RISE), buffer.get());
}
if (!style->textIndent().isUndefined()) {
int indentation = valueForLength(style->textIndent(), object->size().width());
buffer.reset(g_strdup_printf("%i", indentation));
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_INDENT), buffer.get());
}
String fontFamilyName = style->fontCascade().firstFamily();
if (fontFamilyName.left(8) == "-webkit-")
fontFamilyName = fontFamilyName.substring(8);
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_FAMILY_NAME), fontFamilyName.utf8().data());
int fontWeight = static_cast<float>(style->fontCascade().weight());
if (fontWeight > 0) {
buffer.reset(g_strdup_printf("%i", fontWeight));
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_WEIGHT), buffer.get());
}
switch (style->textAlign()) {
case TextAlignMode::Start:
case TextAlignMode::End:
break;
case TextAlignMode::Left:
case TextAlignMode::WebKitLeft:
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_JUSTIFICATION), "left");
break;
case TextAlignMode::Right:
case TextAlignMode::WebKitRight:
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_JUSTIFICATION), "right");
break;
case TextAlignMode::Center:
case TextAlignMode::WebKitCenter:
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_JUSTIFICATION), "center");
break;
case TextAlignMode::Justify:
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_JUSTIFICATION), "fill");
}
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_UNDERLINE), (style->textDecoration() & TextDecoration::Underline) ? "single" : "none");
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_STYLE), style->fontCascade().italic() ? "italic" : "normal");
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_STRIKETHROUGH), (style->textDecoration() & TextDecoration::LineThrough) ? "true" : "false");
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_INVISIBLE), (style->visibility() == Visibility::Hidden) ? "true" : "false");
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_EDITABLE), object->canSetValueAttribute() ? "true" : "false");
String language = object->language();
if (!language.isEmpty())
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(ATK_TEXT_ATTR_LANGUAGE), language.utf8().data());
String invalidStatus = object->invalidStatus();
if (invalidStatus != "false") {
// Register the custom attribute for 'aria-invalid' if not done yet.
if (atkTextAttributeInvalid == ATK_TEXT_ATTR_INVALID)
atkTextAttributeInvalid = atk_text_attribute_register("invalid");
result = addToAtkAttributeSet(result, atk_text_attribute_get_name(atkTextAttributeInvalid), invalidStatus.utf8().data());
}
return result;
}
static gint compareAttribute(const AtkAttribute* a, const AtkAttribute* b)
{
return g_strcmp0(a->name, b->name) || g_strcmp0(a->value, b->value);
}
// Returns an AtkAttributeSet with the elements of attributeSet1 which
// are either not present or different in attributeSet2. Neither
// attributeSet1 nor attributeSet2 should be used after calling this.
static AtkAttributeSet* attributeSetDifference(AtkAttributeSet* attributeSet1, AtkAttributeSet* attributeSet2)
{
if (!attributeSet2)
return attributeSet1;
AtkAttributeSet* currentSet = attributeSet1;
AtkAttributeSet* found;
AtkAttributeSet* toDelete = nullptr;
while (currentSet) {
found = g_slist_find_custom(attributeSet2, currentSet->data, (GCompareFunc)compareAttribute);
if (found) {
AtkAttributeSet* nextSet = currentSet->next;
toDelete = g_slist_prepend(toDelete, currentSet->data);
attributeSet1 = g_slist_delete_link(attributeSet1, currentSet);
currentSet = nextSet;
} else
currentSet = currentSet->next;
}
atk_attribute_set_free(attributeSet2);
atk_attribute_set_free(toDelete);
return attributeSet1;
}
static gchar* webkitAccessibleTextGetText(AtkText*, gint startOffset, gint endOffset);
static guint accessibilityObjectLength(const AccessibilityObject* object)
{
// Non render objects are not taken into account
if (!object->isAccessibilityRenderObject())
return 0;
// For those objects implementing the AtkText interface we use the
// well known API to always get the text in a consistent way
auto* atkObj = ATK_OBJECT(object->wrapper());
if (ATK_IS_TEXT(atkObj)) {
GUniquePtr<gchar> text(webkitAccessibleTextGetText(ATK_TEXT(atkObj), 0, -1));
return g_utf8_strlen(text.get(), -1);
}
// Even if we don't expose list markers to Assistive
// Technologies, we need to have a way to measure their length
// for those cases when it's needed to take it into account
// separately (as in getAccessibilityObjectForOffset)
RenderObject* renderer = object->renderer();
if (is<RenderListMarker>(renderer)) {
auto& marker = downcast<RenderListMarker>(*renderer);
return marker.text().length() + marker.suffix().length();
}
return 0;
}
static const AccessibilityObject* getAccessibilityObjectForOffset(const AccessibilityObject* object, guint offset, gint* startOffset, gint* endOffset)
{
const AccessibilityObject* result;
guint length = accessibilityObjectLength(object);
if (length > offset) {
*startOffset = 0;
*endOffset = length;
result = object;
} else {
*startOffset = -1;
*endOffset = -1;
result = 0;
}
if (!object->firstChild())
return result;
AccessibilityObject* child = object->firstChild();
guint currentOffset = 0;
guint childPosition = 0;
while (child && currentOffset <= offset) {
guint childLength = accessibilityObjectLength(child);
currentOffset = childLength + childPosition;
if (currentOffset > offset) {
gint childStartOffset;
gint childEndOffset;
const AccessibilityObject* grandChild = getAccessibilityObjectForOffset(child, offset-childPosition, &childStartOffset, &childEndOffset);
if (childStartOffset >= 0) {
*startOffset = childStartOffset + childPosition;
*endOffset = childEndOffset + childPosition;
result = grandChild;
}
} else {
childPosition += childLength;
child = child->nextSibling();
}
}
return result;
}
static AtkAttributeSet* getRunAttributesFromAccessibilityObject(const AccessibilityObject* element, gint offset, gint* startOffset, gint* endOffset)
{
const AccessibilityObject* child = getAccessibilityObjectForOffset(element, offset, startOffset, endOffset);
if (!child) {
*startOffset = -1;
*endOffset = -1;
return 0;
}
AtkAttributeSet* defaultAttributes = getAttributeSetForAccessibilityObject(element);
AtkAttributeSet* childAttributes = getAttributeSetForAccessibilityObject(child);
return attributeSetDifference(childAttributes, defaultAttributes);
}
static IntRect textExtents(AtkText* text, gint startOffset, gint length, AtkCoordType coords)
{
GUniquePtr<char> textContent(webkitAccessibleTextGetText(text, startOffset, -1));
gint textLength = g_utf8_strlen(textContent.get(), -1);
// The first case (endOffset of -1) should work, but seems broken for all Gtk+ apps.
gint rangeLength = length;
if (rangeLength < 0 || rangeLength > textLength)
rangeLength = textLength;
AccessibilityObject* coreObject = core(text);
IntRect extents = coreObject->doAXBoundsForRange(PlainTextRange(startOffset, rangeLength));
switch (coords) {
case ATK_XY_SCREEN:
if (Document* document = coreObject->document())
extents = document->view()->contentsToScreen(extents);
break;
case ATK_XY_WINDOW:
// No-op
break;
#if ATK_CHECK_VERSION(2, 30, 0)
case ATK_XY_PARENT:
RELEASE_ASSERT_NOT_REACHED();
#endif
}
return extents;
}
static int offsetAdjustmentForListItem(const AXCoreObject* object)
{
// We need to adjust the offsets for the list item marker in
// Left-To-Right text, since we expose it together with the text.
RenderObject* renderer = object->renderer();
if (is<RenderListItem>(renderer) && renderer->style().direction() == TextDirection::LTR)
return downcast<RenderListItem>(*renderer).markerTextWithSuffix().length();
return 0;
}
static int webCoreOffsetToAtkOffset(const AXCoreObject* object, int offset)
{
if (!object->isListItem())
return offset;
return offset + offsetAdjustmentForListItem(object);
}
static int atkOffsetToWebCoreOffset(AtkText* text, int offset)
{
AccessibilityObject* coreObject = core(text);
if (!coreObject || !coreObject->isListItem())
return offset;
return offset - offsetAdjustmentForListItem(coreObject);
}
static Node* getNodeForAccessibilityObject(AccessibilityObject* coreObject)
{
if (!coreObject->isNativeTextControl())
return coreObject->node();
// For text controls, we get the first visible position on it (which will
// belong to its inner element, unreachable from the DOM) and return its
// parent node, so we have a "bounding node" for the accessibility object.
VisiblePosition positionInTextControlInnerElement = coreObject->visiblePositionForIndex(0, true);
Node* innerMostNode = positionInTextControlInnerElement.deepEquivalent().anchorNode();
if (!innerMostNode)
return 0;
return innerMostNode->parentNode();
}
static void getSelectionOffsetsForObject(AccessibilityObject* coreObject, VisibleSelection& selection, gint& startOffset, gint& endOffset)
{
// Default values, unless the contrary is proved.
startOffset = 0;
endOffset = 0;
Node* node = getNodeForAccessibilityObject(coreObject);
if (!node)
return;
if (selection.isNone())
return;
// We need to limit our search to positions that fall inside the domain of the current object.
Position firstValidPosition = firstPositionInOrBeforeNode(node->firstDescendant());
Position lastValidPosition = lastPositionInOrAfterNode(node->lastDescendant());
// Find the proper range for the selection that falls inside the object.
Position nodeRangeStart = selection.start();
if (comparePositions(nodeRangeStart, firstValidPosition) < 0)
nodeRangeStart = firstValidPosition;
Position nodeRangeEnd = selection.end();
if (comparePositions(nodeRangeEnd, lastValidPosition) > 0)
nodeRangeEnd = lastValidPosition;
// Calculate position of the selected range inside the object.
Position parentFirstPosition = firstPositionInOrBeforeNode(node);
auto rangeInParent = Range::create(node->document(), parentFirstPosition, nodeRangeStart);
// Set values for start offsets and calculate initial range length.
// These values might be adjusted later to cover special cases.
startOffset = webCoreOffsetToAtkOffset(coreObject, TextIterator::rangeLength(rangeInParent.ptr(), true));
auto nodeRange = Range::create(node->document(), nodeRangeStart, nodeRangeEnd);
int rangeLength = TextIterator::rangeLength(nodeRange.ptr(), true);
// Special cases that are only relevant when working with *_END boundaries.
if (selection.affinity() == UPSTREAM) {
VisiblePosition visibleStart(nodeRangeStart, UPSTREAM);
VisiblePosition visibleEnd(nodeRangeEnd, UPSTREAM);
// We need to adjust offsets when finding wrapped lines so the position at the end
// of the line is properly taking into account when calculating the offsets.
if (isEndOfLine(visibleStart) && !lineBreakExistsAtVisiblePosition(visibleStart)) {
if (isStartOfLine(visibleStart.next()))
rangeLength++;
if (!isEndOfBlock(visibleStart))
startOffset = std::max(startOffset - 1, 0);
}
if (isEndOfLine(visibleEnd) && !lineBreakExistsAtVisiblePosition(visibleEnd) && !isEndOfBlock(visibleEnd))
rangeLength--;
}
endOffset = std::min(startOffset + rangeLength, static_cast<int>(accessibilityObjectLength(coreObject)));
}
static gchar* webkitAccessibleTextGetText(AtkText* text, gint startOffset, gint endOffset)
{
g_return_val_if_fail(ATK_TEXT(text), 0);
returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(text), 0);
AccessibilityObject* coreObject = core(text);
#if ENABLE(INPUT_TYPE_COLOR)
if (coreObject->roleValue() == AccessibilityRole::ColorWell) {
int r, g, b;
coreObject->colorValue(r, g, b);
return g_strdup_printf("rgb %7.5f %7.5f %7.5f 1", r / 255., g / 255., b / 255.);
}
#endif
String ret;
if (coreObject->isTextControl())
ret = coreObject->doAXStringForRange(PlainTextRange(0, endOffset));
else {
ret = coreObject->stringValue();
if (!ret)
ret = coreObject->textUnderElement(AccessibilityTextUnderElementMode(AccessibilityTextUnderElementMode::TextUnderElementModeIncludeAllChildren));
}
// Prefix a item number/bullet if needed
int actualEndOffset = endOffset == -1 ? ret.length() : endOffset;
if (coreObject->roleValue() == AccessibilityRole::ListItem) {
RenderObject* objRenderer = coreObject->renderer();
if (is<RenderListItem>(objRenderer)) {
String markerText = downcast<RenderListItem>(*objRenderer).markerTextWithSuffix();
ret = objRenderer->style().direction() == TextDirection::LTR ? markerText + ret : ret + markerText;
if (endOffset == -1)
actualEndOffset = ret.length() + markerText.length();
}
}
ret = ret.substring(startOffset, actualEndOffset - startOffset);
return g_strdup(ret.utf8().data());
}
enum GetTextRelativePosition {
GetTextPositionAt,
GetTextPositionBefore,
GetTextPositionAfter
};
// Convenience function to be used in early returns.
static char* emptyTextSelectionAtOffset(int offset, int* startOffset, int* endOffset)
{
*startOffset = offset;
*endOffset = offset;
return g_strdup("");
}
static char* webkitAccessibleTextGetChar(AtkText* text, int offset, GetTextRelativePosition textPosition, int* startOffset, int* endOffset)
{
int actualOffset = offset;
if (textPosition == GetTextPositionBefore)
actualOffset--;
else if (textPosition == GetTextPositionAfter)
actualOffset++;
GUniquePtr<char> textData(webkitAccessibleTextGetText(text, 0, -1));
int textLength = g_utf8_strlen(textData.get(), -1);
*startOffset = std::max(0, actualOffset);
*startOffset = std::min(*startOffset, textLength);
*endOffset = std::max(0, actualOffset + 1);
*endOffset = std::min(*endOffset, textLength);
if (*startOffset == *endOffset)
return g_strdup("");
return g_utf8_substring(textData.get(), *startOffset, *endOffset);
}
static VisiblePosition nextWordStartPosition(const VisiblePosition &position)
{
VisiblePosition positionAfterCurrentWord = nextWordPosition(position);
// In order to skip spaces when moving right, we advance one word further
// and then move one word back. This will put us at the beginning of the
// word following.
VisiblePosition positionAfterSpacingAndFollowingWord = nextWordPosition(positionAfterCurrentWord);
if (positionAfterSpacingAndFollowingWord != positionAfterCurrentWord)
positionAfterCurrentWord = previousWordPosition(positionAfterSpacingAndFollowingWord);
bool movingBackwardsMovedPositionToStartOfCurrentWord = positionAfterCurrentWord == previousWordPosition(nextWordPosition(position));
if (movingBackwardsMovedPositionToStartOfCurrentWord)
positionAfterCurrentWord = positionAfterSpacingAndFollowingWord;
return positionAfterCurrentWord;
}
static VisiblePosition previousWordEndPosition(const VisiblePosition &position)
{
// We move forward and then backward to position ourselves at the beginning
// of the current word for this boundary, making the most of the semantics
// of previousWordPosition() and nextWordPosition().
VisiblePosition positionAtStartOfCurrentWord = previousWordPosition(nextWordPosition(position));
VisiblePosition positionAtPreviousWord = previousWordPosition(position);
// Need to consider special cases (punctuation) when we are in the last word of a sentence.
if (isStartOfWord(position) && positionAtPreviousWord != position && positionAtPreviousWord == positionAtStartOfCurrentWord)
return nextWordPosition(positionAtStartOfCurrentWord);
// In order to skip spaces when moving left, we advance one word backwards
// and then move one word forward. This will put us at the beginning of
// the word following.
VisiblePosition positionBeforeSpacingAndPreceedingWord = previousWordPosition(positionAtStartOfCurrentWord);
if (positionBeforeSpacingAndPreceedingWord != positionAtStartOfCurrentWord)
positionAtStartOfCurrentWord = nextWordPosition(positionBeforeSpacingAndPreceedingWord);
bool movingForwardMovedPositionToEndOfCurrentWord = nextWordPosition(positionAtStartOfCurrentWord) == previousWordPosition(nextWordPosition(position));
if (movingForwardMovedPositionToEndOfCurrentWord)
positionAtStartOfCurrentWord = positionBeforeSpacingAndPreceedingWord;
return positionAtStartOfCurrentWord;
}
static VisibleSelection wordAtPositionForAtkBoundary(const AccessibilityObject* /*coreObject*/, const VisiblePosition& position, AtkTextBoundary boundaryType)
{
VisiblePosition startPosition;
VisiblePosition endPosition;
switch (boundaryType) {
case ATK_TEXT_BOUNDARY_WORD_START:
// isStartOfWord() returns true both when at the beginning of a "real" word
// as when at the beginning of a whitespace range between two "real" words,
// since that whitespace is considered a "word" as well. And in case we are
// already at the beginning of a "real" word we do not need to look backwards.
if (isStartOfWord(position) && deprecatedIsEditingWhitespace(position.characterBefore()))
startPosition = position;
else
startPosition = previousWordPosition(position);
endPosition = nextWordStartPosition(startPosition);
// We need to make sure that we look for the word in the current line when
// we are at the beginning of a new line, and not look into the previous one
// at all, which might happen when lines belong to different nodes.
if (isStartOfLine(position) && isStartOfLine(endPosition)) {
startPosition = endPosition;
endPosition = nextWordStartPosition(startPosition);
}
break;
case ATK_TEXT_BOUNDARY_WORD_END:
startPosition = previousWordEndPosition(position);
endPosition = nextWordPosition(startPosition);
break;
default:
ASSERT_NOT_REACHED();
}
VisibleSelection selectedWord(startPosition, endPosition);
// We mark the selection as 'upstream' so we can use that information later,
// when finding the actual offsets in getSelectionOffsetsForObject().
if (boundaryType == ATK_TEXT_BOUNDARY_WORD_END)
selectedWord.setAffinity(UPSTREAM);
return selectedWord;
}
static int numberOfReplacedElementsBeforeOffset(AtkText* text, unsigned offset)
{
GUniquePtr<char> textForObject(webkitAccessibleTextGetText(text, 0, offset));
String textBeforeOffset = String::fromUTF8(textForObject.get());
int count = 0;
size_t index = textBeforeOffset.find(objectReplacementCharacter, 0);
while (index < offset && index != WTF::notFound) {
index = textBeforeOffset.find(objectReplacementCharacter, index + 1);
count++;
}
return count;
}
static char* webkitAccessibleTextWordForBoundary(AtkText* text, int offset, AtkTextBoundary boundaryType, GetTextRelativePosition textPosition, int* startOffset, int* endOffset)
{
AccessibilityObject* coreObject = core(text);
Document* document = coreObject->document();
if (!document)
return emptyTextSelectionAtOffset(0, startOffset, endOffset);
Node* node = getNodeForAccessibilityObject(coreObject);
if (!node)
return emptyTextSelectionAtOffset(0, startOffset, endOffset);
int actualOffset = atkOffsetToWebCoreOffset(text, offset);
// Besides of the usual conversion from ATK offsets to WebCore offsets,
// we need to consider the potential embedded objects that might have been
// inserted in the text exposed through AtkText when calculating the offset.
actualOffset -= numberOfReplacedElementsBeforeOffset(text, actualOffset);
VisiblePosition caretPosition = coreObject->visiblePositionForIndex(actualOffset);
VisibleSelection currentWord = wordAtPositionForAtkBoundary(coreObject, caretPosition, boundaryType);
// Take into account other relative positions, if needed, by
// calculating the new position that we would need to consider.
VisiblePosition newPosition = caretPosition;
switch (textPosition) {
case GetTextPositionAt:
break;
case GetTextPositionBefore:
// Early return if asking for the previous word while already at the beginning.
if (isFirstVisiblePositionInNode(currentWord.visibleStart(), node))
return emptyTextSelectionAtOffset(0, startOffset, endOffset);
if (isStartOfLine(currentWord.end()))
newPosition = currentWord.visibleStart().previous();
else
newPosition = startOfWord(currentWord.start(), LeftWordIfOnBoundary);
break;
case GetTextPositionAfter:
// Early return if asking for the following word while already at the end.
if (isLastVisiblePositionInNode(currentWord.visibleEnd(), node))
return emptyTextSelectionAtOffset(accessibilityObjectLength(coreObject), startOffset, endOffset);
if (isEndOfLine(currentWord.end()))
newPosition = currentWord.visibleEnd().next();
else
newPosition = endOfWord(currentWord.end(), RightWordIfOnBoundary);
break;
default:
ASSERT_NOT_REACHED();
}
// Determine the relevant word we are actually interested in
// and calculate the ATK offsets for it, then return everything.
VisibleSelection selectedWord = newPosition != caretPosition ? wordAtPositionForAtkBoundary(coreObject, newPosition, boundaryType) : currentWord;
getSelectionOffsetsForObject(coreObject, selectedWord, *startOffset, *endOffset);
return webkitAccessibleTextGetText(text, *startOffset, *endOffset);
}
static bool isSentenceBoundary(const VisiblePosition &pos)
{
if (pos.isNull())
return false;
// It's definitely a sentence boundary if there's nothing before.
if (pos.previous().isNull())
return true;
// We go backwards and forward to make sure about this.
VisiblePosition startOfPreviousSentence = startOfSentence(pos);
return startOfPreviousSentence.isNotNull() && pos == endOfSentence(startOfPreviousSentence);
}
static bool isWhiteSpaceBetweenSentences(const VisiblePosition& position)
{
if (position.isNull())
return false;
if (!deprecatedIsEditingWhitespace(position.characterAfter()))
return false;
VisiblePosition startOfWhiteSpace = startOfWord(position, RightWordIfOnBoundary);
VisiblePosition endOfWhiteSpace = endOfWord(startOfWhiteSpace, RightWordIfOnBoundary);
if (!isSentenceBoundary(startOfWhiteSpace) && !isSentenceBoundary(endOfWhiteSpace))
return false;
return comparePositions(startOfWhiteSpace, position) <= 0 && comparePositions(endOfWhiteSpace, position) >= 0;
}
static VisibleSelection sentenceAtPositionForAtkBoundary(const AccessibilityObject*, const VisiblePosition& position, AtkTextBoundary boundaryType)
{
VisiblePosition startPosition;
VisiblePosition endPosition;
bool isAtStartOfSentenceForEndBoundary = isWhiteSpaceBetweenSentences(position) || isSentenceBoundary(position);
if (boundaryType == ATK_TEXT_BOUNDARY_SENTENCE_START || !isAtStartOfSentenceForEndBoundary) {
startPosition = isSentenceBoundary(position) ? position : startOfSentence(position);
// startOfSentence might stop at a linebreak in the HTML source code,
// but we don't want to stop there yet, so keep going.
while (!isSentenceBoundary(startPosition) && isHTMLLineBreak(startPosition.characterBefore()))
startPosition = startOfSentence(startPosition);
endPosition = endOfSentence(startPosition);
}
if (boundaryType == ATK_TEXT_BOUNDARY_SENTENCE_END) {
if (isAtStartOfSentenceForEndBoundary) {
startPosition = position;
endPosition = endOfSentence(endOfWord(position, RightWordIfOnBoundary));
}
// startOfSentence returns a position after any white space previous to
// the sentence, so we might need to adjust that offset for this boundary.
if (deprecatedIsEditingWhitespace(startPosition.characterBefore()))
startPosition = startOfWord(startPosition, LeftWordIfOnBoundary);
// endOfSentence returns a position after any white space after the
// sentence, so we might need to adjust that offset for this boundary.
if (deprecatedIsEditingWhitespace(endPosition.characterBefore()))
endPosition = startOfWord(endPosition, LeftWordIfOnBoundary);
// Finally, do some additional adjustments that might be needed if
// positions are at the start or the end of a line.
if (isStartOfLine(startPosition) && !isStartOfBlock(startPosition))
startPosition = startPosition.previous();
if (isStartOfLine(endPosition) && !isStartOfBlock(endPosition))
endPosition = endPosition.previous();
}
VisibleSelection selectedSentence(startPosition, endPosition);
// We mark the selection as 'upstream' so we can use that information later,
// when finding the actual offsets in getSelectionOffsetsForObject().
if (boundaryType == ATK_TEXT_BOUNDARY_SENTENCE_END)
selectedSentence.setAffinity(UPSTREAM);
return selectedSentence;
}
static char* webkitAccessibleTextSentenceForBoundary(AtkText* text, int offset, AtkTextBoundary boundaryType, GetTextRelativePosition textPosition, int* startOffset, int* endOffset)
{
AccessibilityObject* coreObject = core(text);
Document* document = coreObject->document();
if (!document)
return emptyTextSelectionAtOffset(0, startOffset, endOffset);
Node* node = getNodeForAccessibilityObject(coreObject);
if (!node)
return emptyTextSelectionAtOffset(0, startOffset, endOffset);
int actualOffset = atkOffsetToWebCoreOffset(text, offset);
// Besides of the usual conversion from ATK offsets to WebCore offsets,
// we need to consider the potential embedded objects that might have been
// inserted in the text exposed through AtkText when calculating the offset.
actualOffset -= numberOfReplacedElementsBeforeOffset(text, actualOffset);
VisiblePosition caretPosition = coreObject->visiblePositionForIndex(actualOffset);
VisibleSelection currentSentence = sentenceAtPositionForAtkBoundary(coreObject, caretPosition, boundaryType);
// Take into account other relative positions, if needed, by
// calculating the new position that we would need to consider.
VisiblePosition newPosition = caretPosition;
switch (textPosition) {
case GetTextPositionAt:
break;
case GetTextPositionBefore:
// Early return if asking for the previous sentence while already at the beginning.
if (isFirstVisiblePositionInNode(currentSentence.visibleStart(), node))
return emptyTextSelectionAtOffset(0, startOffset, endOffset);
newPosition = currentSentence.visibleStart().previous();
break;
case GetTextPositionAfter:
// Early return if asking for the following word while already at the end.
if (isLastVisiblePositionInNode(currentSentence.visibleEnd(), node))
return emptyTextSelectionAtOffset(accessibilityObjectLength(coreObject), startOffset, endOffset);
newPosition = currentSentence.visibleEnd().next();
break;
default:
ASSERT_NOT_REACHED();
}
// Determine the relevant sentence we are actually interested in
// and calculate the ATK offsets for it, then return everything.
VisibleSelection selectedSentence = newPosition != caretPosition ? sentenceAtPositionForAtkBoundary(coreObject, newPosition, boundaryType) : currentSentence;
getSelectionOffsetsForObject(coreObject, selectedSentence, *startOffset, *endOffset);
return webkitAccessibleTextGetText(text, *startOffset, *endOffset);
}
static VisibleSelection lineAtPositionForAtkBoundary(const AccessibilityObject* coreObject, const VisiblePosition& position, AtkTextBoundary boundaryType)
{
UNUSED_PARAM(coreObject);
VisiblePosition startPosition;
VisiblePosition endPosition;
switch (boundaryType) {
case ATK_TEXT_BOUNDARY_LINE_START:
startPosition = isStartOfLine(position) ? position : logicalStartOfLine(position);
endPosition = logicalEndOfLine(position);
// In addition to checking that we are not at the end of a block, we need
// to check that endPosition has not UPSTREAM affinity, since that would
// cause trouble inside of text controls (we would be advancing too much).
if (!isEndOfBlock(endPosition) && endPosition.affinity() != UPSTREAM)
endPosition = endPosition.next();
break;
case ATK_TEXT_BOUNDARY_LINE_END:
startPosition = isEndOfLine(position) ? position : logicalStartOfLine(position);
if (!isStartOfBlock(startPosition))
startPosition = startPosition.previous();
endPosition = logicalEndOfLine(position);
break;
default:
ASSERT_NOT_REACHED();
}
VisibleSelection selectedLine(startPosition, endPosition);
// We mark the selection as 'upstream' so we can use that information later,
// when finding the actual offsets in getSelectionOffsetsForObject().
if (boundaryType == ATK_TEXT_BOUNDARY_LINE_END)
selectedLine.setAffinity(UPSTREAM);
return selectedLine;
}
static char* webkitAccessibleTextLineForBoundary(AtkText* text, int offset, AtkTextBoundary boundaryType, GetTextRelativePosition textPosition, int* startOffset, int* endOffset)
{
AccessibilityObject* coreObject = core(text);
Document* document = coreObject->document();
if (!document)
return emptyTextSelectionAtOffset(0, startOffset, endOffset);
Node* node = getNodeForAccessibilityObject(coreObject);
if (!node)
return emptyTextSelectionAtOffset(0, startOffset, endOffset);
int actualOffset = atkOffsetToWebCoreOffset(text, offset);
// Besides the usual conversion from ATK offsets to WebCore offsets,
// we need to consider the potential embedded objects that might have been
// inserted in the text exposed through AtkText when calculating the offset.
actualOffset -= numberOfReplacedElementsBeforeOffset(text, actualOffset);
VisiblePosition caretPosition = coreObject->visiblePositionForIndex(actualOffset);
VisibleSelection currentLine = lineAtPositionForAtkBoundary(coreObject, caretPosition, boundaryType);
// Take into account other relative positions, if needed, by
// calculating the new position that we would need to consider.
VisiblePosition newPosition = caretPosition;
switch (textPosition) {
case GetTextPositionAt:
// No need to do additional work if we are using the "at" position, we just
// explicitly list this case option to catch invalid values in the default case.
break;
case GetTextPositionBefore:
// Early return if asking for the previous line while already at the beginning.
if (isFirstVisiblePositionInNode(currentLine.visibleStart(), node))
return emptyTextSelectionAtOffset(0, startOffset, endOffset);
newPosition = currentLine.visibleStart().previous();
break;
case GetTextPositionAfter:
// Early return if asking for the following word while already at the end.
if (isLastVisiblePositionInNode(currentLine.visibleEnd(), node))
return emptyTextSelectionAtOffset(accessibilityObjectLength(coreObject), startOffset, endOffset);
newPosition = currentLine.visibleEnd().next();
break;
default:
ASSERT_NOT_REACHED();
}
// Determine the relevant line we are actually interested in
// and calculate the ATK offsets for it, then return everything.
VisibleSelection selectedLine = newPosition != caretPosition ? lineAtPositionForAtkBoundary(coreObject, newPosition, boundaryType) : currentLine;
getSelectionOffsetsForObject(coreObject, selectedLine, *startOffset, *endOffset);
// We might need to adjust the start or end offset to include the list item marker,
// if present, when printing the first or the last full line for a list item.
RenderObject* renderer = coreObject->renderer();
if (renderer->isListItem()) {
// For Left-to-Right, the list item marker is at the beginning of the exposed text.
if (renderer->style().direction() == TextDirection::LTR && isFirstVisiblePositionInNode(selectedLine.visibleStart(), node))
*startOffset = 0;
// For Right-to-Left, the list item marker is at the end of the exposed text.
if (renderer->style().direction() == TextDirection::RTL && isLastVisiblePositionInNode(selectedLine.visibleEnd(), node))
*endOffset = accessibilityObjectLength(coreObject);
}
return webkitAccessibleTextGetText(text, *startOffset, *endOffset);
}
static gchar* webkitAccessibleTextGetTextForOffset(AtkText* text, gint offset, AtkTextBoundary boundaryType, GetTextRelativePosition textPosition, gint* startOffset, gint* endOffset)
{
AccessibilityObject* coreObject = core(text);
if (!coreObject || !coreObject->isAccessibilityRenderObject())
return emptyTextSelectionAtOffset(0, startOffset, endOffset);
switch (boundaryType) {
case ATK_TEXT_BOUNDARY_CHAR:
return webkitAccessibleTextGetChar(text, offset, textPosition, startOffset, endOffset);
case ATK_TEXT_BOUNDARY_WORD_START:
case ATK_TEXT_BOUNDARY_WORD_END:
return webkitAccessibleTextWordForBoundary(text, offset, boundaryType, textPosition, startOffset, endOffset);
case ATK_TEXT_BOUNDARY_LINE_START:
case ATK_TEXT_BOUNDARY_LINE_END:
return webkitAccessibleTextLineForBoundary(text, offset, boundaryType, textPosition, startOffset, endOffset);
case ATK_TEXT_BOUNDARY_SENTENCE_START:
case ATK_TEXT_BOUNDARY_SENTENCE_END:
return webkitAccessibleTextSentenceForBoundary(text, offset, boundaryType, textPosition, startOffset, endOffset);
default:
ASSERT_NOT_REACHED();
}
// This should never be reached.
return 0;
}
static gchar* webkitAccessibleTextGetTextAfterOffset(AtkText* text, gint offset, AtkTextBoundary boundaryType, gint* startOffset, gint* endOffset)
{
g_return_val_if_fail(ATK_TEXT(text), 0);
returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(text), 0);
return webkitAccessibleTextGetTextForOffset(text, offset, boundaryType, GetTextPositionAfter, startOffset, endOffset);
}
static gchar* webkitAccessibleTextGetTextAtOffset(AtkText* text, gint offset, AtkTextBoundary boundaryType, gint* startOffset, gint* endOffset)
{
g_return_val_if_fail(ATK_TEXT(text), 0);
returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(text), 0);
return webkitAccessibleTextGetTextForOffset(text, offset, boundaryType, GetTextPositionAt, startOffset, endOffset);
}
static gchar* webkitAccessibleTextGetTextBeforeOffset(AtkText* text, gint offset, AtkTextBoundary boundaryType, gint* startOffset, gint* endOffset)
{
g_return_val_if_fail(ATK_TEXT(text), 0);
returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(text), 0);
return webkitAccessibleTextGetTextForOffset(text, offset, boundaryType, GetTextPositionBefore, startOffset, endOffset);
}
static gunichar webkitAccessibleTextGetCharacterAtOffset(AtkText* text, gint)
{
g_return_val_if_fail(ATK_TEXT(text), 0);
returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(text), 0);
notImplemented();
return 0;
}
static gint webkitAccessibleTextGetCaretOffset(AtkText* text)
{
g_return_val_if_fail(ATK_TEXT(text), 0);
returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(text), 0);
// coreObject is the unignored object whose offset the caller is requesting.
// focusedObject is the object with the caret. It is likely ignored -- unless it's a link.
AXCoreObject* coreObject = core(text);
if (!coreObject->isAccessibilityRenderObject())
return 0;
// We need to make sure we pass a valid object as reference.
if (coreObject->accessibilityIsIgnored())
coreObject = coreObject->parentObjectUnignored();
if (!coreObject)