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
|
/* clang-format off */
/* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* clang-format on */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "HyperTextAccessibleWrap.h"
#include "LocalAccessible-inl.h"
#include "HTMLListAccessible.h"
#include "nsAccUtils.h"
#include "nsFrameSelection.h"
#include "TextRange.h"
#include "TreeWalker.h"
using namespace mozilla;
using namespace mozilla::a11y;
// HyperTextIterator
class HyperTextIterator {
public:
HyperTextIterator(HyperTextAccessible* aStartContainer, int32_t aStartOffset,
HyperTextAccessible* aEndContainer, int32_t aEndOffset)
: mCurrentContainer(aStartContainer),
mCurrentStartOffset(0),
mCurrentEndOffset(0),
mEndContainer(aEndContainer),
mEndOffset(0) {
mCurrentStartOffset =
std::min(aStartOffset,
static_cast<int32_t>(mCurrentContainer->CharacterCount()));
mCurrentEndOffset = mCurrentStartOffset;
mEndOffset = std::min(
aEndOffset, static_cast<int32_t>(mEndContainer->CharacterCount()));
}
bool Next();
int32_t SegmentLength();
// If offset is set to a child hyperlink, adjust it so it set on the first
// offset in the deepest link. Or, if the offset to the last character, set it
// to the outermost end offset in an ancestor. Returns true if iterator was
// mutated.
bool NormalizeForward();
// If offset is set right after child hyperlink, adjust it so it set on the
// last offset in the deepest link. Or, if the offset is on the first
// character of a link, set it to the outermost start offset in an ancestor.
// Returns true if iterator was mutated.
bool NormalizeBackward();
HyperTextAccessible* mCurrentContainer;
int32_t mCurrentStartOffset;
int32_t mCurrentEndOffset;
private:
int32_t NextLinkOffset();
HyperTextAccessible* mEndContainer;
int32_t mEndOffset;
};
bool HyperTextIterator::NormalizeForward() {
if (mCurrentStartOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT ||
mCurrentStartOffset >=
static_cast<int32_t>(mCurrentContainer->CharacterCount())) {
// If this is the end of the current container, mutate to its parent's
// end offset.
if (!mCurrentContainer->IsLink()) {
// If we are not a link, it is a root hypertext accessible.
return false;
}
if (!mCurrentContainer->LocalParent() ||
!mCurrentContainer->LocalParent()->IsHyperText()) {
// If we are a link, but our parent is not a hypertext accessible
// treat the current container as the root hypertext accessible.
// This can be the case with some XUL containers that are not
// hypertext accessibles.
return false;
}
uint32_t endOffset = mCurrentContainer->EndOffset();
if (endOffset != 0) {
mCurrentContainer = mCurrentContainer->LocalParent()->AsHyperText();
mCurrentStartOffset = endOffset;
if (mCurrentContainer == mEndContainer &&
mCurrentStartOffset >= mEndOffset) {
// Reached end boundary.
return false;
}
// Call NormalizeForward recursively to get top-most link if at the end of
// one, or innermost link if at the beginning.
NormalizeForward();
return true;
}
} else {
LocalAccessible* link = mCurrentContainer->LinkAt(
mCurrentContainer->LinkIndexAtOffset(mCurrentStartOffset));
// If there is a link at this offset, mutate into it.
if (link && link->IsHyperText()) {
if (mCurrentStartOffset > 0 &&
mCurrentContainer->LinkIndexAtOffset(mCurrentStartOffset) ==
mCurrentContainer->LinkIndexAtOffset(mCurrentStartOffset - 1)) {
MOZ_ASSERT_UNREACHABLE("Same link for previous offset");
return false;
}
mCurrentContainer = link->AsHyperText();
if (link->IsHTMLListItem()) {
LocalAccessible* bullet = link->AsHTMLListItem()->Bullet();
mCurrentStartOffset = bullet ? nsAccUtils::TextLength(bullet) : 0;
} else {
mCurrentStartOffset = 0;
}
if (mCurrentContainer == mEndContainer &&
mCurrentStartOffset >= mEndOffset) {
// Reached end boundary.
return false;
}
// Call NormalizeForward recursively to get top-most embedding ancestor
// if at the end of one, or innermost link if at the beginning.
NormalizeForward();
return true;
}
}
return false;
}
bool HyperTextIterator::NormalizeBackward() {
if (mCurrentStartOffset == 0) {
// If this is the start of the current container, mutate to its parent's
// start offset.
if (!mCurrentContainer->IsLink()) {
// If we are not a link, it is a root hypertext accessible.
return false;
}
if (!mCurrentContainer->LocalParent() ||
!mCurrentContainer->LocalParent()->IsHyperText()) {
// If we are a link, but our parent is not a hypertext accessible
// treat the current container as the root hypertext accessible.
// This can be the case with some XUL containers that are not
// hypertext accessibles.
return false;
}
uint32_t startOffset = mCurrentContainer->StartOffset();
mCurrentContainer = mCurrentContainer->LocalParent()->AsHyperText();
mCurrentStartOffset = startOffset;
// Call NormalizeBackward recursively to get top-most link if at the
// beginning of one, or innermost link if at the end.
NormalizeBackward();
return true;
} else {
LocalAccessible* link =
mCurrentContainer->GetChildAtOffset(mCurrentStartOffset - 1);
// If there is a link before this offset, mutate into it,
// and set the offset to its last character.
if (link && link->IsHyperText()) {
mCurrentContainer = link->AsHyperText();
mCurrentStartOffset = mCurrentContainer->CharacterCount();
// Call NormalizeBackward recursively to get top-most top-most embedding
// ancestor if at the beginning of one, or innermost link if at the end.
NormalizeBackward();
return true;
}
if (mCurrentContainer->IsHTMLListItem() &&
mCurrentContainer->AsHTMLListItem()->Bullet() == link) {
mCurrentStartOffset = 0;
NormalizeBackward();
return true;
}
}
return false;
}
int32_t HyperTextIterator::SegmentLength() {
int32_t endOffset = mCurrentEndOffset < 0
? mCurrentContainer->CharacterCount()
: mCurrentEndOffset;
return endOffset - mCurrentStartOffset;
}
int32_t HyperTextIterator::NextLinkOffset() {
int32_t linkCount = mCurrentContainer->LinkCount();
for (int32_t i = 0; i < linkCount; i++) {
LocalAccessible* link = mCurrentContainer->LinkAt(i);
MOZ_ASSERT(link);
int32_t linkStartOffset = link->StartOffset();
if (mCurrentStartOffset < linkStartOffset) {
return linkStartOffset;
}
}
return -1;
}
bool HyperTextIterator::Next() {
if (!mCurrentContainer->Document()->HasLoadState(
DocAccessible::eTreeConstructed)) {
// If the accessible tree is still being constructed the text tree
// is not in a traversable state yet.
return false;
}
if (mCurrentContainer == mEndContainer &&
(mCurrentEndOffset == -1 || mEndOffset <= mCurrentEndOffset)) {
return false;
} else {
mCurrentStartOffset = mCurrentEndOffset;
NormalizeForward();
}
int32_t nextLinkOffset = NextLinkOffset();
if (mCurrentContainer == mEndContainer &&
(nextLinkOffset == -1 || nextLinkOffset > mEndOffset)) {
mCurrentEndOffset =
mEndOffset < 0 ? mEndContainer->CharacterCount() : mEndOffset;
} else {
mCurrentEndOffset = nextLinkOffset < 0 ? mCurrentContainer->CharacterCount()
: nextLinkOffset;
}
return mCurrentStartOffset != mCurrentEndOffset;
}
void HyperTextAccessibleWrap::TextForRange(nsAString& aText,
int32_t aStartOffset,
HyperTextAccessible* aEndContainer,
int32_t aEndOffset) {
if (IsHTMLListItem()) {
LocalAccessible* maybeBullet = GetChildAtOffset(aStartOffset - 1);
if (maybeBullet) {
LocalAccessible* bullet = AsHTMLListItem()->Bullet();
if (maybeBullet == bullet) {
TextSubstring(0, nsAccUtils::TextLength(bullet), aText);
}
}
}
HyperTextIterator iter(this, aStartOffset, aEndContainer, aEndOffset);
while (iter.Next()) {
nsAutoString text;
iter.mCurrentContainer->TextSubstring(iter.mCurrentStartOffset,
iter.mCurrentEndOffset, text);
aText.Append(text);
}
}
void HyperTextAccessibleWrap::AttributedTextForRange(
nsTArray<nsString>& aStrings, nsTArray<RefPtr<AccAttributes>>& aProperties,
nsTArray<LocalAccessible*>& aContainers, int32_t aStartOffset,
HyperTextAccessible* aEndContainer, int32_t aEndOffset) {
if (IsHTMLListItem()) {
LocalAccessible* maybeBullet = GetChildAtOffset(aStartOffset - 1);
if (maybeBullet) {
LocalAccessible* bullet = AsHTMLListItem()->Bullet();
if (maybeBullet == bullet) {
nsAutoString text;
TextSubstring(0, nsAccUtils::TextLength(bullet), text);
int32_t unusedAttrStartOffset, unusedAttrEndOffset;
RefPtr<AccAttributes> props =
TextAttributes(true, aStartOffset - 1, &unusedAttrStartOffset,
&unusedAttrEndOffset);
aStrings.AppendElement(text);
aProperties.AppendElement(props);
aContainers.AppendElement(this);
}
}
}
HyperTextIterator iter(this, aStartOffset, aEndContainer, aEndOffset);
while (iter.Next()) {
int32_t attrStartOffset = 0;
int32_t attrEndOffset = iter.mCurrentStartOffset;
do {
int32_t oldEndOffset = attrEndOffset;
RefPtr<AccAttributes> props = iter.mCurrentContainer->TextAttributes(
true, attrEndOffset, &attrStartOffset, &attrEndOffset);
if (oldEndOffset == attrEndOffset) {
MOZ_ASSERT_UNREACHABLE("new attribute end offset should be different");
break;
}
nsAutoString text;
iter.mCurrentContainer->TextSubstring(
attrStartOffset < iter.mCurrentStartOffset ? iter.mCurrentStartOffset
: attrStartOffset,
attrEndOffset < iter.mCurrentEndOffset ? attrEndOffset
: iter.mCurrentEndOffset,
text);
aStrings.AppendElement(text);
aProperties.AppendElement(props);
aContainers.AppendElement(iter.mCurrentContainer);
} while (attrEndOffset < iter.mCurrentEndOffset);
}
}
LayoutDeviceIntRect HyperTextAccessibleWrap::BoundsForRange(
int32_t aStartOffset, HyperTextAccessible* aEndContainer,
int32_t aEndOffset) {
LayoutDeviceIntRect rect;
HyperTextIterator iter(this, aStartOffset, aEndContainer, aEndOffset);
while (iter.Next()) {
LayoutDeviceIntRect stringRect = iter.mCurrentContainer->TextBounds(
iter.mCurrentStartOffset, iter.mCurrentEndOffset);
rect.UnionRect(rect, stringRect);
}
return rect;
}
int32_t HyperTextAccessibleWrap::LengthForRange(
int32_t aStartOffset, HyperTextAccessible* aEndContainer,
int32_t aEndOffset) {
int32_t length = 0;
HyperTextIterator iter(this, aStartOffset, aEndContainer, aEndOffset);
while (iter.Next()) {
length += iter.SegmentLength();
}
return length;
}
void HyperTextAccessibleWrap::OffsetAtIndex(int32_t aIndex,
HyperTextAccessible** aContainer,
int32_t* aOffset) {
int32_t index = aIndex;
HyperTextIterator iter(this, 0, this, CharacterCount());
while (iter.Next()) {
int32_t segmentLength = iter.SegmentLength();
if (index <= segmentLength) {
*aContainer = iter.mCurrentContainer;
*aOffset = iter.mCurrentStartOffset + index;
break;
}
index -= segmentLength;
}
}
void HyperTextAccessibleWrap::RangeAt(int32_t aOffset, EWhichRange aRangeType,
HyperTextAccessible** aStartContainer,
int32_t* aStartOffset,
HyperTextAccessible** aEndContainer,
int32_t* aEndOffset) {
switch (aRangeType) {
case EWhichRange::eLeftWord:
LeftWordAt(aOffset, aStartContainer, aStartOffset, aEndContainer,
aEndOffset);
break;
case EWhichRange::eRightWord:
RightWordAt(aOffset, aStartContainer, aStartOffset, aEndContainer,
aEndOffset);
break;
case EWhichRange::eLine:
case EWhichRange::eLeftLine:
LineAt(aOffset, false, aStartContainer, aStartOffset, aEndContainer,
aEndOffset);
break;
case EWhichRange::eRightLine:
LineAt(aOffset, true, aStartContainer, aStartOffset, aEndContainer,
aEndOffset);
break;
case EWhichRange::eParagraph:
ParagraphAt(aOffset, aStartContainer, aStartOffset, aEndContainer,
aEndOffset);
break;
case EWhichRange::eStyle:
StyleAt(aOffset, aStartContainer, aStartOffset, aEndContainer,
aEndOffset);
break;
default:
break;
}
}
void HyperTextAccessibleWrap::LeftWordAt(int32_t aOffset,
HyperTextAccessible** aStartContainer,
int32_t* aStartOffset,
HyperTextAccessible** aEndContainer,
int32_t* aEndOffset) {
TextPoint here(this, aOffset);
TextPoint start =
FindTextPoint(aOffset, eDirPrevious, eSelectWord, eStartWord);
if (!start.mContainer) {
return;
}
auto* startContainer = static_cast<HyperTextAccessibleWrap*>(
start.mContainer->AsLocal()->AsHyperText());
if ((NativeState() & states::EDITABLE) &&
!(startContainer->NativeState() & states::EDITABLE)) {
// The word search crossed an editable boundary. Return the first word of
// the editable root.
return EditableRoot()->RightWordAt(0, aStartContainer, aStartOffset,
aEndContainer, aEndOffset);
}
TextPoint end = startContainer->FindTextPoint(start.mOffset, eDirNext,
eSelectWord, eEndWord);
if (end < here) {
*aStartContainer = end.mContainer->AsLocal()->AsHyperText();
*aEndContainer = here.mContainer->AsLocal()->AsHyperText();
*aStartOffset = end.mOffset;
*aEndOffset = here.mOffset;
} else {
*aStartContainer = startContainer;
*aEndContainer = end.mContainer->AsLocal()->AsHyperText();
*aStartOffset = start.mOffset;
*aEndOffset = end.mOffset;
}
}
void HyperTextAccessibleWrap::RightWordAt(int32_t aOffset,
HyperTextAccessible** aStartContainer,
int32_t* aStartOffset,
HyperTextAccessible** aEndContainer,
int32_t* aEndOffset) {
TextPoint here(this, aOffset);
TextPoint end = FindTextPoint(aOffset, eDirNext, eSelectWord, eEndWord);
if (!end.mContainer || end < here || here == end) {
// If we didn't find a word end, or if we wrapped around (bug 1652833),
// return with no result.
return;
}
auto* endContainer = static_cast<HyperTextAccessibleWrap*>(
end.mContainer->AsLocal()->AsHyperText());
if ((NativeState() & states::EDITABLE) &&
!(endContainer->NativeState() & states::EDITABLE)) {
// The word search crossed an editable boundary. Return with no result.
return;
}
TextPoint start = endContainer->FindTextPoint(end.mOffset, eDirPrevious,
eSelectWord, eStartWord);
if (here < start) {
*aStartContainer = here.mContainer->AsLocal()->AsHyperText();
*aEndContainer = start.mContainer->AsLocal()->AsHyperText();
*aStartOffset = here.mOffset;
*aEndOffset = start.mOffset;
} else {
*aStartContainer = start.mContainer->AsLocal()->AsHyperText();
*aEndContainer = endContainer;
*aStartOffset = start.mOffset;
*aEndOffset = end.mOffset;
}
}
void HyperTextAccessibleWrap::LineAt(int32_t aOffset, bool aNextLine,
HyperTextAccessible** aStartContainer,
int32_t* aStartOffset,
HyperTextAccessible** aEndContainer,
int32_t* aEndOffset) {
TextPoint here(this, aOffset);
TextPoint end =
FindTextPoint(aOffset, eDirNext, eSelectEndLine, eDefaultBehavior);
if (!end.mContainer || end < here) {
// If we didn't find a word end, or if we wrapped around (bug 1652833),
// return with no result.
return;
}
auto* endContainer = static_cast<HyperTextAccessibleWrap*>(
end.mContainer->AsLocal()->AsHyperText());
TextPoint start = endContainer->FindTextPoint(
end.mOffset, eDirPrevious, eSelectBeginLine, eDefaultBehavior);
if (!aNextLine && here < start) {
start = FindTextPoint(aOffset, eDirPrevious, eSelectBeginLine,
eDefaultBehavior);
if (!start.mContainer) {
return;
}
auto* startContainer = static_cast<HyperTextAccessibleWrap*>(
start.mContainer->AsLocal()->AsHyperText());
end = startContainer->FindTextPoint(start.mOffset, eDirNext, eSelectEndLine,
eDefaultBehavior);
}
*aStartContainer = start.mContainer->AsLocal()->AsHyperText();
*aEndContainer = end.mContainer->AsLocal()->AsHyperText();
*aStartOffset = start.mOffset;
*aEndOffset = end.mOffset;
}
void HyperTextAccessibleWrap::ParagraphAt(int32_t aOffset,
HyperTextAccessible** aStartContainer,
int32_t* aStartOffset,
HyperTextAccessible** aEndContainer,
int32_t* aEndOffset) {
TextPoint here(this, aOffset);
TextPoint end =
FindTextPoint(aOffset, eDirNext, eSelectParagraph, eDefaultBehavior);
if (!end.mContainer || end < here) {
// If we didn't find a word end, or if we wrapped around (bug 1652833),
// return with no result.
return;
}
if (end.mOffset == -1 && LocalParent() && LocalParent()->IsHyperText()) {
// If end offset is -1 we didn't find a paragraph boundary.
// This must be an inline container, go to its parent to
// retrieve paragraph boundaries.
static_cast<HyperTextAccessibleWrap*>(LocalParent()->AsHyperText())
->ParagraphAt(StartOffset(), aStartContainer, aStartOffset,
aEndContainer, aEndOffset);
return;
}
auto* endContainer = static_cast<HyperTextAccessibleWrap*>(
end.mContainer->AsLocal()->AsHyperText());
TextPoint start = static_cast<HyperTextAccessibleWrap*>(endContainer)
->FindTextPoint(end.mOffset, eDirPrevious,
eSelectParagraph, eDefaultBehavior);
*aStartContainer = start.mContainer->AsLocal()->AsHyperText();
*aEndContainer = endContainer;
*aStartOffset = start.mOffset;
*aEndOffset = end.mOffset;
}
void HyperTextAccessibleWrap::StyleAt(int32_t aOffset,
HyperTextAccessible** aStartContainer,
int32_t* aStartOffset,
HyperTextAccessible** aEndContainer,
int32_t* aEndOffset) {
// Get the range of the text leaf at this offset.
// A text leaf represents a stretch of like-styled text.
auto leaf = LeafAtOffset(aOffset);
if (!leaf) {
return;
}
MOZ_ASSERT(leaf->LocalParent()->IsHyperText());
HyperTextAccessibleWrap* container =
static_cast<HyperTextAccessibleWrap*>(leaf->LocalParent()->AsHyperText());
if (!container) {
return;
}
*aStartContainer = *aEndContainer = container;
container->RangeOfChild(leaf, aStartOffset, aEndOffset);
}
void HyperTextAccessibleWrap::NextClusterAt(
int32_t aOffset, HyperTextAccessible** aNextContainer,
int32_t* aNextOffset) {
TextPoint here(this, aOffset);
TextPoint next =
FindTextPoint(aOffset, eDirNext, eSelectCluster, eDefaultBehavior);
if ((next.mOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT &&
next.mContainer == Document()) ||
(next < here)) {
// If we reached the end of the doc, or if we wrapped to the start of the
// doc return given offset as-is.
*aNextContainer = this;
*aNextOffset = aOffset;
} else {
*aNextContainer = next.mContainer->AsLocal()->AsHyperText();
*aNextOffset = next.mOffset;
}
}
void HyperTextAccessibleWrap::PreviousClusterAt(
int32_t aOffset, HyperTextAccessible** aPrevContainer,
int32_t* aPrevOffset) {
TextPoint prev =
FindTextPoint(aOffset, eDirPrevious, eSelectCluster, eDefaultBehavior);
*aPrevContainer = prev.mContainer->AsLocal()->AsHyperText();
*aPrevOffset = prev.mOffset;
}
void HyperTextAccessibleWrap::RangeOfChild(LocalAccessible* aChild,
int32_t* aStartOffset,
int32_t* aEndOffset) {
MOZ_ASSERT(aChild->LocalParent() == this);
*aStartOffset = *aEndOffset = -1;
int32_t index = GetIndexOf(aChild);
if (index != -1) {
*aStartOffset = GetChildOffset(index);
// If this is the last child index + 1 will return the total
// chracter count.
*aEndOffset = GetChildOffset(index + 1);
}
}
LocalAccessible* HyperTextAccessibleWrap::LeafAtOffset(int32_t aOffset) {
HyperTextAccessible* text = this;
LocalAccessible* child = nullptr;
// The offset needed should "attach" the previous accessible if
// in between two accessibles.
int32_t innerOffset = aOffset > 0 ? aOffset - 1 : aOffset;
do {
int32_t childIdx = text->GetChildIndexAtOffset(innerOffset);
if (childIdx == -1) {
return text;
}
child = text->LocalChildAt(childIdx);
if (!child || nsAccUtils::MustPrune(text)) {
return text;
}
innerOffset -= text->GetChildOffset(childIdx);
text = child->AsHyperText();
} while (text);
return child;
}
void HyperTextAccessibleWrap::SelectRange(int32_t aStartOffset,
HyperTextAccessible* aEndContainer,
int32_t aEndOffset) {
TextRange range(this, this, aStartOffset, aEndContainer, aEndOffset);
range.SetSelectionAt(0);
}
TextPoint HyperTextAccessibleWrap::FindTextPoint(
int32_t aOffset, nsDirection aDirection, nsSelectionAmount aAmount,
EWordMovementType aWordMovementType) {
// Layout can remain trapped in an editable. We normalize out of
// it if we are in its last offset.
HyperTextIterator iter(this, aOffset, this, CharacterCount());
if (aDirection == eDirNext) {
iter.NormalizeForward();
} else {
iter.NormalizeBackward();
}
// Find a leaf accessible frame to start with. PeekOffset wants this.
HyperTextAccessible* text = iter.mCurrentContainer;
LocalAccessible* child = nullptr;
int32_t innerOffset = iter.mCurrentStartOffset;
do {
int32_t childIdx = text->GetChildIndexAtOffset(innerOffset);
// We can have an empty text leaf as our only child. Since empty text
// leaves are not accessible we then have no children, but 0 is a valid
// innerOffset.
if (childIdx == -1) {
NS_ASSERTION(innerOffset == 0 && !text->ChildCount(), "No childIdx?");
return TextPoint(text, 0);
}
child = text->LocalChildAt(childIdx);
if (child->IsHyperText() && !child->ChildCount()) {
// If this is a childless hypertext, jump to its
// previous or next sibling, depending on
// direction.
if (aDirection == eDirPrevious && childIdx > 0) {
child = text->LocalChildAt(--childIdx);
} else if (aDirection == eDirNext &&
childIdx + 1 < static_cast<int32_t>(text->ChildCount())) {
child = text->LocalChildAt(++childIdx);
}
}
int32_t childOffset = text->GetChildOffset(childIdx);
if (child->IsHyperText() && aDirection == eDirPrevious && childIdx > 0 &&
innerOffset - childOffset == 0) {
// If we are searching backwards, and this is the begining of a
// segment, get the previous sibling so that layout will start
// its search there.
childIdx--;
innerOffset -= text->GetChildOffset(childIdx);
child = text->LocalChildAt(childIdx);
} else {
innerOffset -= childOffset;
}
text = child->AsHyperText();
} while (text);
nsIFrame* childFrame = child->GetFrame();
if (!childFrame) {
NS_ERROR("No child frame");
return TextPoint(this, aOffset);
}
int32_t innerContentOffset = innerOffset;
if (child->IsTextLeaf()) {
NS_ASSERTION(childFrame->IsTextFrame(), "Wrong frame!");
RenderedToContentOffset(childFrame, innerOffset, &innerContentOffset);
}
nsIFrame* frameAtOffset = childFrame;
int32_t offsetInFrame = 0;
childFrame->GetChildFrameContainingOffset(innerContentOffset, true,
&offsetInFrame, &frameAtOffset);
if (aDirection == eDirPrevious && offsetInFrame == 0) {
// If we are searching backwards, and we are at the start of a frame,
// get the previous continuation frame.
if (nsIFrame* prevInContinuation = frameAtOffset->GetPrevContinuation()) {
frameAtOffset = prevInContinuation;
}
}
const bool kIsJumpLinesOk = true; // okay to jump lines
const bool kIsScrollViewAStop = false; // do not stop at scroll views
const bool kIsKeyboardSelect = true; // is keyboard selection
const bool kIsVisualBidi = false; // use visual order for bidi text
nsPeekOffsetStruct pos(
aAmount, aDirection, innerContentOffset, nsPoint(0, 0), kIsJumpLinesOk,
kIsScrollViewAStop, kIsKeyboardSelect, kIsVisualBidi, false,
nsPeekOffsetStruct::ForceEditableRegion::No, aWordMovementType, false);
nsresult rv = frameAtOffset->PeekOffset(&pos);
// PeekOffset fails on last/first lines of the text in certain cases.
if (NS_FAILED(rv) && aAmount == eSelectLine) {
pos.mAmount = aDirection == eDirNext ? eSelectEndLine : eSelectBeginLine;
frameAtOffset->PeekOffset(&pos);
}
if (!pos.mResultContent) {
NS_ERROR("No result content!");
return TextPoint(this, aOffset);
}
if (aDirection == eDirNext &&
nsContentUtils::PositionIsBefore(pos.mResultContent, mContent, nullptr,
nullptr)) {
// Bug 1652833 makes us sometimes return the first element on the doc.
return TextPoint(Document(), nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT);
}
HyperTextAccessible* container =
nsAccUtils::GetTextContainer(pos.mResultContent);
int32_t offset = container ? container->DOMPointToOffset(
pos.mResultContent, pos.mContentOffset,
aDirection == eDirNext)
: 0;
return TextPoint(container, offset);
}
HyperTextAccessibleWrap* HyperTextAccessibleWrap::EditableRoot() {
LocalAccessible* editable = nullptr;
for (LocalAccessible* acc = this; acc && acc != Document();
acc = acc->LocalParent()) {
if (acc->NativeState() & states::EDITABLE) {
editable = acc;
} else {
break;
}
}
return static_cast<HyperTextAccessibleWrap*>(editable->AsHyperText());
}
|