forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocalAccessible.cpp
4167 lines (3616 loc) · 150 KB
/
LocalAccessible.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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "AccEvent.h"
#include "LocalAccessible-inl.h"
#include "EmbeddedObjCollector.h"
#include "AccGroupInfo.h"
#include "AccIterator.h"
#include "CachedTableAccessible.h"
#include "DocAccessible-inl.h"
#include "mozilla/a11y/AccAttributes.h"
#include "mozilla/a11y/DocAccessibleChild.h"
#include "mozilla/a11y/Platform.h"
#include "nsAccUtils.h"
#include "nsAccessibilityService.h"
#include "ApplicationAccessible.h"
#include "nsGenericHTMLElement.h"
#include "NotificationController.h"
#include "nsEventShell.h"
#include "nsTextEquivUtils.h"
#include "EventTree.h"
#include "OuterDocAccessible.h"
#include "Pivot.h"
#include "Relation.h"
#include "mozilla/a11y/Role.h"
#include "RootAccessible.h"
#include "States.h"
#include "TextLeafRange.h"
#include "TextRange.h"
#include "HTMLElementAccessibles.h"
#include "HTMLSelectAccessible.h"
#include "HTMLTableAccessible.h"
#include "ImageAccessible.h"
#include "nsComputedDOMStyle.h"
#include "nsGkAtoms.h"
#include "nsIDOMXULButtonElement.h"
#include "nsIDOMXULSelectCntrlEl.h"
#include "nsIDOMXULSelectCntrlItemEl.h"
#include "nsINodeList.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/HTMLFormElement.h"
#include "mozilla/dom/HTMLAnchorElement.h"
#include "mozilla/gfx/Matrix.h"
#include "nsIContent.h"
#include "nsIFormControl.h"
#include "nsDisplayList.h"
#include "nsLayoutUtils.h"
#include "nsPresContext.h"
#include "nsIFrame.h"
#include "nsTextFrame.h"
#include "nsView.h"
#include "nsIDocShellTreeItem.h"
#include "nsIScrollableFrame.h"
#include "nsStyleStructInlines.h"
#include "nsFocusManager.h"
#include "nsString.h"
#include "nsAtom.h"
#include "nsContainerFrame.h"
#include "mozilla/Assertions.h"
#include "mozilla/BasicEvents.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/PresShell.h"
#include "mozilla/ProfilerMarkers.h"
#include "mozilla/StaticPrefs_ui.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLLabelElement.h"
#include "mozilla/dom/KeyboardEventBinding.h"
#include "mozilla/dom/TreeWalker.h"
#include "mozilla/dom/UserActivation.h"
#include "mozilla/dom/MutationEventBinding.h"
using namespace mozilla;
using namespace mozilla::a11y;
////////////////////////////////////////////////////////////////////////////////
// LocalAccessible: nsISupports and cycle collection
NS_IMPL_CYCLE_COLLECTION_CLASS(LocalAccessible)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(LocalAccessible)
tmp->Shutdown();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(LocalAccessible)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContent, mDoc)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(LocalAccessible)
NS_INTERFACE_MAP_ENTRY_CONCRETE(LocalAccessible)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, LocalAccessible)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(LocalAccessible)
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_DESTROY(LocalAccessible, LastRelease())
LocalAccessible::LocalAccessible(nsIContent* aContent, DocAccessible* aDoc)
: Accessible(),
mContent(aContent),
mDoc(aDoc),
mParent(nullptr),
mIndexInParent(-1),
mBounds(),
mFirstLineStart(-1),
mStateFlags(0),
mContextFlags(0),
mReorderEventTarget(false),
mShowEventTarget(false),
mHideEventTarget(false),
mIndexOfEmbeddedChild(-1),
mGroupInfo(nullptr) {}
LocalAccessible::~LocalAccessible() {
NS_ASSERTION(!mDoc, "LastRelease was never called!?!");
}
ENameValueFlag LocalAccessible::Name(nsString& aName) const {
aName.Truncate();
if (!HasOwnContent()) return eNameOK;
ARIAName(aName);
if (!aName.IsEmpty()) return eNameOK;
ENameValueFlag nameFlag = NativeName(aName);
if (!aName.IsEmpty()) return nameFlag;
// In the end get the name from tooltip.
if (mContent->IsHTMLElement()) {
if (mContent->AsElement()->GetAttr(nsGkAtoms::title, aName)) {
aName.CompressWhitespace();
return eNameFromTooltip;
}
} else if (mContent->IsXULElement()) {
if (mContent->AsElement()->GetAttr(nsGkAtoms::tooltiptext, aName)) {
aName.CompressWhitespace();
return eNameFromTooltip;
}
} else if (mContent->IsSVGElement()) {
// If user agents need to choose among multiple 'desc' or 'title'
// elements for processing, the user agent shall choose the first one.
for (nsIContent* childElm = mContent->GetFirstChild(); childElm;
childElm = childElm->GetNextSibling()) {
if (childElm->IsSVGElement(nsGkAtoms::desc)) {
nsTextEquivUtils::AppendTextEquivFromContent(this, childElm, &aName);
return eNameFromTooltip;
}
}
}
aName.SetIsVoid(true);
return nameFlag;
}
void LocalAccessible::Description(nsString& aDescription) const {
// There are 4 conditions that make an accessible have no accDescription:
// 1. it's a text node; or
// 2. It has no ARIA describedby or description property
// 3. it doesn't have an accName; or
// 4. its title attribute already equals to its accName nsAutoString name;
if (!HasOwnContent() || mContent->IsText()) return;
ARIADescription(aDescription);
if (aDescription.IsEmpty()) {
NativeDescription(aDescription);
if (aDescription.IsEmpty()) {
// Keep the Name() method logic.
if (mContent->IsHTMLElement()) {
mContent->AsElement()->GetAttr(nsGkAtoms::title, aDescription);
} else if (mContent->IsXULElement()) {
mContent->AsElement()->GetAttr(nsGkAtoms::tooltiptext, aDescription);
} else if (mContent->IsSVGElement()) {
for (nsIContent* childElm = mContent->GetFirstChild(); childElm;
childElm = childElm->GetNextSibling()) {
if (childElm->IsSVGElement(nsGkAtoms::desc)) {
nsTextEquivUtils::AppendTextEquivFromContent(this, childElm,
&aDescription);
break;
}
}
}
}
}
if (!aDescription.IsEmpty()) {
aDescription.CompressWhitespace();
nsAutoString name;
Name(name);
// Don't expose a description if it is the same as the name.
if (aDescription.Equals(name)) aDescription.Truncate();
}
}
KeyBinding LocalAccessible::AccessKey() const {
if (!HasOwnContent()) return KeyBinding();
uint32_t key = nsCoreUtils::GetAccessKeyFor(mContent);
if (!key && mContent->IsElement()) {
LocalAccessible* label = nullptr;
// Copy access key from label node.
if (mContent->IsHTMLElement()) {
// Unless it is labeled via an ancestor <label>, in which case that would
// be redundant.
HTMLLabelIterator iter(Document(), this,
HTMLLabelIterator::eSkipAncestorLabel);
label = iter.Next();
}
if (!label) {
XULLabelIterator iter(Document(), mContent);
label = iter.Next();
}
if (label) key = nsCoreUtils::GetAccessKeyFor(label->GetContent());
}
if (!key) return KeyBinding();
// Get modifier mask. Use ui.key.generalAccessKey (unless it is -1).
switch (StaticPrefs::ui_key_generalAccessKey()) {
case -1:
break;
case dom::KeyboardEvent_Binding::DOM_VK_SHIFT:
return KeyBinding(key, KeyBinding::kShift);
case dom::KeyboardEvent_Binding::DOM_VK_CONTROL:
return KeyBinding(key, KeyBinding::kControl);
case dom::KeyboardEvent_Binding::DOM_VK_ALT:
return KeyBinding(key, KeyBinding::kAlt);
case dom::KeyboardEvent_Binding::DOM_VK_META:
return KeyBinding(key, KeyBinding::kMeta);
default:
return KeyBinding();
}
// Determine the access modifier used in this context.
dom::Document* document = mContent->GetComposedDoc();
if (!document) return KeyBinding();
nsCOMPtr<nsIDocShellTreeItem> treeItem(document->GetDocShell());
if (!treeItem) return KeyBinding();
nsresult rv = NS_ERROR_FAILURE;
int32_t modifierMask = 0;
switch (treeItem->ItemType()) {
case nsIDocShellTreeItem::typeChrome:
modifierMask = StaticPrefs::ui_key_chromeAccess();
rv = NS_OK;
break;
case nsIDocShellTreeItem::typeContent:
modifierMask = StaticPrefs::ui_key_contentAccess();
rv = NS_OK;
break;
}
return NS_SUCCEEDED(rv) ? KeyBinding(key, modifierMask) : KeyBinding();
}
KeyBinding LocalAccessible::KeyboardShortcut() const { return KeyBinding(); }
uint64_t LocalAccessible::VisibilityState() const {
if (IPCAccessibilityActive()) {
// Visibility states must be calculated by RemoteAccessible, so there's no
// point calculating them here.
return 0;
}
nsIFrame* frame = GetFrame();
if (!frame) {
// Element having display:contents is considered visible semantically,
// despite it doesn't have a visually visible box.
if (nsCoreUtils::IsDisplayContents(mContent)) {
return states::OFFSCREEN;
}
return states::INVISIBLE;
}
if (!frame->StyleVisibility()->IsVisible()) return states::INVISIBLE;
// It's invisible if the presshell is hidden by a visibility:hidden element in
// an ancestor document.
if (frame->PresShell()->IsUnderHiddenEmbedderElement()) {
return states::INVISIBLE;
}
// Offscreen state if the document's visibility state is not visible.
if (Document()->IsHidden()) return states::OFFSCREEN;
// Walk the parent frame chain to see if the frame is in background tab or
// scrolled out.
nsIFrame* curFrame = frame;
do {
nsView* view = curFrame->GetView();
if (view && view->GetVisibility() == ViewVisibility::Hide) {
return states::INVISIBLE;
}
if (nsLayoutUtils::IsPopup(curFrame)) {
return 0;
}
if (curFrame->StyleUIReset()->mMozSubtreeHiddenOnlyVisually) {
// Offscreen state for background tab content.
return states::OFFSCREEN;
}
nsIFrame* parentFrame = curFrame->GetParent();
// If contained by scrollable frame then check that at least 12 pixels
// around the object is visible, otherwise the object is offscreen.
nsIScrollableFrame* scrollableFrame = do_QueryFrame(parentFrame);
const nscoord kMinPixels = nsPresContext::CSSPixelsToAppUnits(12);
if (scrollableFrame) {
nsRect scrollPortRect = scrollableFrame->GetScrollPortRect();
nsRect frameRect = nsLayoutUtils::TransformFrameRectToAncestor(
frame, frame->GetRectRelativeToSelf(), parentFrame);
if (!scrollPortRect.Contains(frameRect)) {
scrollPortRect.Deflate(kMinPixels, kMinPixels);
if (!scrollPortRect.Intersects(frameRect)) return states::OFFSCREEN;
}
}
if (!parentFrame) {
parentFrame = nsLayoutUtils::GetCrossDocParentFrameInProcess(curFrame);
// Even if we couldn't find the parent frame, it might mean we are in an
// out-of-process iframe, try to see if |frame| is scrolled out in an
// scrollable frame in a cross-process ancestor document.
if (!parentFrame &&
nsLayoutUtils::FrameIsMostlyScrolledOutOfViewInCrossProcess(
frame, kMinPixels)) {
return states::OFFSCREEN;
}
}
curFrame = parentFrame;
} while (curFrame);
// Zero area rects can occur in the first frame of a multi-frame text flow,
// in which case the rendered text is not empty and the frame should not be
// marked invisible.
// XXX Can we just remove this check? Why do we need to mark empty
// text invisible?
if (frame->IsTextFrame() && !frame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) &&
frame->GetRect().IsEmpty()) {
nsIFrame::RenderedText text = frame->GetRenderedText(
0, UINT32_MAX, nsIFrame::TextOffsetType::OffsetsInContentText,
nsIFrame::TrailingWhitespace::DontTrim);
if (text.mString.IsEmpty()) {
return states::INVISIBLE;
}
}
return 0;
}
uint64_t LocalAccessible::NativeState() const {
uint64_t state = 0;
if (!IsInDocument()) state |= states::STALE;
if (HasOwnContent() && mContent->IsElement()) {
dom::ElementState elementState = mContent->AsElement()->State();
if (elementState.HasState(dom::ElementState::INVALID)) {
state |= states::INVALID;
}
if (elementState.HasState(dom::ElementState::REQUIRED)) {
state |= states::REQUIRED;
}
state |= NativeInteractiveState();
}
// Gather states::INVISIBLE and states::OFFSCREEN flags for this object.
state |= VisibilityState();
nsIFrame* frame = GetFrame();
if (frame && frame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
state |= states::FLOATING;
}
// Check if a XUL element has the popup attribute (an attached popup menu).
if (HasOwnContent() && mContent->IsXULElement() &&
mContent->AsElement()->HasAttr(nsGkAtoms::popup)) {
state |= states::HASPOPUP;
}
// Bypass the link states specialization for non links.
const nsRoleMapEntry* roleMapEntry = ARIARoleMap();
if (!roleMapEntry || roleMapEntry->roleRule == kUseNativeRole ||
roleMapEntry->role == roles::LINK) {
state |= NativeLinkState();
}
return state;
}
uint64_t LocalAccessible::NativeInteractiveState() const {
if (!mContent->IsElement()) return 0;
if (NativelyUnavailable()) return states::UNAVAILABLE;
nsIFrame* frame = GetFrame();
// If we're caching this remote document in the parent process, we
// need to cache focusability irrespective of visibility. Otherwise,
// if this document is invisible when it first loads, we'll cache that
// all descendants are unfocusable and this won't get updated when the
// document becomes visible. Even if we did get notified when the
// document becomes visible, it would be wasteful to walk the entire
// tree to figure out what is now focusable and push cache updates.
// Although ignoring visibility means IsFocusable will return true for
// visibility: hidden, etc., this isn't a problem because we don't include
// those hidden elements in the a11y tree anyway.
const bool ignoreVisibility = mDoc->IPCDoc();
if (frame && frame->IsFocusable(
/* aWithMouse */ false,
/* aCheckVisibility */ !ignoreVisibility)) {
return states::FOCUSABLE;
}
return 0;
}
uint64_t LocalAccessible::NativeLinkState() const { return 0; }
bool LocalAccessible::NativelyUnavailable() const {
if (mContent->IsHTMLElement()) return mContent->AsElement()->IsDisabled();
return mContent->IsElement() && mContent->AsElement()->AttrValueIs(
kNameSpaceID_None, nsGkAtoms::disabled,
nsGkAtoms::_true, eCaseMatters);
}
Accessible* LocalAccessible::ChildAtPoint(int32_t aX, int32_t aY,
EWhichChildAtPoint aWhichChild) {
Accessible* child = LocalChildAtPoint(aX, aY, aWhichChild);
if (aWhichChild != EWhichChildAtPoint::DirectChild && child &&
child->IsOuterDoc()) {
child = child->ChildAtPoint(aX, aY, aWhichChild);
}
return child;
}
LocalAccessible* LocalAccessible::LocalChildAtPoint(
int32_t aX, int32_t aY, EWhichChildAtPoint aWhichChild) {
// If we can't find the point in a child, we will return the fallback answer:
// we return |this| if the point is within it, otherwise nullptr.
LocalAccessible* fallbackAnswer = nullptr;
LayoutDeviceIntRect rect = Bounds();
if (rect.Contains(aX, aY)) fallbackAnswer = this;
if (nsAccUtils::MustPrune(this)) { // Do not dig any further
return fallbackAnswer;
}
// Search an accessible at the given point starting from accessible document
// because containing block (see CSS2) for out of flow element (for example,
// absolutely positioned element) may be different from its DOM parent and
// therefore accessible for containing block may be different from accessible
// for DOM parent but GetFrameForPoint() should be called for containing block
// to get an out of flow element.
DocAccessible* accDocument = Document();
NS_ENSURE_TRUE(accDocument, nullptr);
nsIFrame* rootFrame = accDocument->GetFrame();
NS_ENSURE_TRUE(rootFrame, nullptr);
nsIFrame* startFrame = rootFrame;
// Check whether the point is at popup content.
nsIWidget* rootWidget = rootFrame->GetView()->GetNearestWidget(nullptr);
NS_ENSURE_TRUE(rootWidget, nullptr);
LayoutDeviceIntRect rootRect = rootWidget->GetScreenBounds();
auto point = LayoutDeviceIntPoint(aX - rootRect.X(), aY - rootRect.Y());
nsIFrame* popupFrame = nsLayoutUtils::GetPopupFrameForPoint(
accDocument->PresContext()->GetRootPresContext(), rootWidget, point);
if (popupFrame) {
// If 'this' accessible is not inside the popup then ignore the popup when
// searching an accessible at point.
DocAccessible* popupDoc =
GetAccService()->GetDocAccessible(popupFrame->GetContent()->OwnerDoc());
LocalAccessible* popupAcc =
popupDoc->GetAccessibleOrContainer(popupFrame->GetContent());
LocalAccessible* popupChild = this;
while (popupChild && !popupChild->IsDoc() && popupChild != popupAcc) {
popupChild = popupChild->LocalParent();
}
if (popupChild == popupAcc) startFrame = popupFrame;
}
nsPresContext* presContext = startFrame->PresContext();
nsRect screenRect = startFrame->GetScreenRectInAppUnits();
nsPoint offset(presContext->DevPixelsToAppUnits(aX) - screenRect.X(),
presContext->DevPixelsToAppUnits(aY) - screenRect.Y());
nsIFrame* foundFrame = nsLayoutUtils::GetFrameForPoint(
RelativeTo{startFrame, ViewportType::Visual}, offset);
nsIContent* content = nullptr;
if (!foundFrame || !(content = foundFrame->GetContent())) {
return fallbackAnswer;
}
// Get accessible for the node with the point or the first accessible in
// the DOM parent chain.
DocAccessible* contentDocAcc =
GetAccService()->GetDocAccessible(content->OwnerDoc());
// contentDocAcc in some circumstances can be nullptr. See bug 729861
NS_ASSERTION(contentDocAcc, "could not get the document accessible");
if (!contentDocAcc) return fallbackAnswer;
LocalAccessible* accessible =
contentDocAcc->GetAccessibleOrContainer(content);
if (!accessible) return fallbackAnswer;
// Hurray! We have an accessible for the frame that layout gave us.
// Since DOM node of obtained accessible may be out of flow then we should
// ensure obtained accessible is a child of this accessible.
LocalAccessible* child = accessible;
while (child != this) {
LocalAccessible* parent = child->LocalParent();
if (!parent) {
// Reached the top of the hierarchy. These bounds were inside an
// accessible that is not a descendant of this one.
return fallbackAnswer;
}
// If we landed on a legitimate child of |this|, and we want the direct
// child, return it here.
if (parent == this && aWhichChild == EWhichChildAtPoint::DirectChild) {
return child;
}
child = parent;
}
// Manually walk through accessible children and see if the are within this
// point. Skip offscreen or invisible accessibles. This takes care of cases
// where layout won't walk into things for us, such as image map areas and
// sub documents (XXX: subdocuments should be handled by methods of
// OuterDocAccessibles).
uint32_t childCount = accessible->ChildCount();
if (childCount == 1 && accessible->IsOuterDoc() &&
accessible->FirstChild()->IsRemote()) {
// No local children.
return accessible;
}
for (uint32_t childIdx = 0; childIdx < childCount; childIdx++) {
LocalAccessible* child = accessible->LocalChildAt(childIdx);
LayoutDeviceIntRect childRect = child->Bounds();
if (childRect.Contains(aX, aY) &&
(child->State() & states::INVISIBLE) == 0) {
if (aWhichChild == EWhichChildAtPoint::DeepestChild) {
return child->LocalChildAtPoint(aX, aY,
EWhichChildAtPoint::DeepestChild);
}
return child;
}
}
return accessible;
}
nsIFrame* LocalAccessible::FindNearestAccessibleAncestorFrame() {
nsIFrame* frame = GetFrame();
if (frame->StyleDisplay()->mPosition == StylePositionProperty::Fixed &&
nsLayoutUtils::IsReallyFixedPos(frame)) {
return mDoc->PresShellPtr()->GetRootFrame();
}
if (IsDoc()) {
// We bound documents by their own frame, which is their PresShell's root
// frame. We cache the document offset elsewhere in BundleFieldsForCache
// using the nsGkAtoms::crossorigin attribute.
MOZ_ASSERT(frame, "DocAccessibles should always have a frame");
return frame;
}
// Iterate through accessible's ancestors to find one with a frame.
LocalAccessible* ancestor = mParent;
while (ancestor) {
if (nsIFrame* boundingFrame = ancestor->GetFrame()) {
return boundingFrame;
}
ancestor = ancestor->LocalParent();
}
MOZ_ASSERT_UNREACHABLE("No ancestor with frame?");
return nsLayoutUtils::GetContainingBlockForClientRect(frame);
}
nsRect LocalAccessible::ParentRelativeBounds() {
nsIFrame* frame = GetFrame();
if (frame && mContent) {
nsIFrame* boundingFrame = FindNearestAccessibleAncestorFrame();
nsRect result = nsLayoutUtils::GetAllInFlowRectsUnion(frame, boundingFrame);
if (result.IsEmpty()) {
// If we end up with a 0x0 rect from above (or one with negative
// height/width) we should try using the ink overflow rect instead. If we
// use this rect, our relative bounds will match the bounds of what
// appears visually. We do this because some web authors (icloud.com for
// example) employ things like 0x0 buttons with visual overflow. Without
// this, such frames aren't navigable by screen readers.
result = frame->InkOverflowRectRelativeToSelf();
result.MoveBy(frame->GetOffsetTo(boundingFrame));
}
if (boundingFrame->GetRect().IsEmpty()) {
// boundingFrame might be the first in an ib-split-sibling chain. If its
// rect is empty, GetAllInFlowRectsUnion might exclude its origin. For
// example, if boundingFrame is empty with an origin of (0, -840) but
// has a non-empty ib-split-sibling with (0, 0), the union rect will
// originate at (0, 0). This means the bounds returned for our parent
// Accessible might be offset from boundingFrame's rect. Since result is
// currently relative to boundingFrame's rect, we might need to adjust it
// to make it parent relative.
nsRect boundingUnion =
nsLayoutUtils::GetAllInFlowRectsUnion(boundingFrame, boundingFrame);
if (!boundingUnion.IsEmpty()) {
result.MoveBy(-boundingUnion.TopLeft());
} else {
// Since GetAllInFlowRectsUnion returned an empty rect on our parent
// Accessible, we would have used the ink overflow rect. However,
// GetAllInFlowRectsUnion calculates relative to the bounding frame's
// main rect, not its ink overflow rect. We need to adjust for the ink
// overflow offset to make our result parent relative.
nsRect boundingOverflow =
boundingFrame->InkOverflowRectRelativeToSelf();
result.MoveBy(-boundingOverflow.TopLeft());
}
}
if (frame->StyleDisplay()->mPosition == StylePositionProperty::Fixed &&
nsLayoutUtils::IsReallyFixedPos(frame)) {
// If we're dealing with a fixed position frame, we've already made it
// relative to the document which should have gotten rid of its scroll
// offset.
return result;
}
if (nsIScrollableFrame* sf =
mParent == mDoc
? mDoc->PresShellPtr()->GetRootScrollFrameAsScrollable()
: boundingFrame->GetScrollTargetFrame()) {
// If boundingFrame has a scroll position, result is currently relative
// to that. Instead, we want result to remain the same regardless of
// scrolling. We then subtract the scroll position later when
// calculating absolute bounds. We do this because we don't want to push
// cache updates for the bounds of all descendants every time we scroll.
nsPoint scrollPos = sf->GetScrollPosition().ApplyResolution(
mDoc->PresShellPtr()->GetResolution());
result.MoveBy(scrollPos.x, scrollPos.y);
}
return result;
}
return nsRect();
}
nsRect LocalAccessible::RelativeBounds(nsIFrame** aBoundingFrame) const {
nsIFrame* frame = GetFrame();
if (frame && mContent) {
*aBoundingFrame = nsLayoutUtils::GetContainingBlockForClientRect(frame);
nsRect unionRect = nsLayoutUtils::GetAllInFlowRectsUnion(
frame, *aBoundingFrame, nsLayoutUtils::RECTS_ACCOUNT_FOR_TRANSFORMS);
if (unionRect.IsEmpty()) {
// If we end up with a 0x0 rect from above (or one with negative
// height/width) we should try using the ink overflow rect instead. If we
// use this rect, our relative bounds will match the bounds of what
// appears visually. We do this because some web authors (icloud.com for
// example) employ things like 0x0 buttons with visual overflow. Without
// this, such frames aren't navigable by screen readers.
nsRect overflow = frame->InkOverflowRectRelativeToSelf();
nsLayoutUtils::TransformRect(frame, *aBoundingFrame, overflow);
return overflow;
}
return unionRect;
}
return nsRect();
}
nsRect LocalAccessible::BoundsInAppUnits() const {
nsIFrame* boundingFrame = nullptr;
nsRect unionRectTwips = RelativeBounds(&boundingFrame);
if (!boundingFrame) {
return nsRect();
}
PresShell* presShell = mDoc->PresContext()->PresShell();
// We need to inverse translate with the offset of the edge of the visual
// viewport from top edge of the layout viewport.
nsPoint viewportOffset = presShell->GetVisualViewportOffset() -
presShell->GetLayoutViewportOffset();
unionRectTwips.MoveBy(-viewportOffset);
// We need to take into account a non-1 resolution set on the presshell.
// This happens with async pinch zooming. Here we scale the bounds before
// adding the screen-relative offset.
unionRectTwips.ScaleRoundOut(presShell->GetResolution());
// We have the union of the rectangle, now we need to put it in absolute
// screen coords.
nsRect orgRectPixels = boundingFrame->GetScreenRectInAppUnits();
unionRectTwips.MoveBy(orgRectPixels.X(), orgRectPixels.Y());
return unionRectTwips;
}
LayoutDeviceIntRect LocalAccessible::Bounds() const {
return LayoutDeviceIntRect::FromAppUnitsToNearest(
BoundsInAppUnits(), mDoc->PresContext()->AppUnitsPerDevPixel());
}
void LocalAccessible::SetSelected(bool aSelect) {
if (!HasOwnContent()) return;
LocalAccessible* select = nsAccUtils::GetSelectableContainer(this, State());
if (select) {
if (select->State() & states::MULTISELECTABLE) {
if (mContent->IsElement() && ARIARoleMap()) {
if (aSelect) {
mContent->AsElement()->SetAttr(
kNameSpaceID_None, nsGkAtoms::aria_selected, u"true"_ns, true);
} else {
mContent->AsElement()->UnsetAttr(kNameSpaceID_None,
nsGkAtoms::aria_selected, true);
}
}
return;
}
if (aSelect) TakeFocus();
}
}
void LocalAccessible::TakeSelection() {
LocalAccessible* select = nsAccUtils::GetSelectableContainer(this, State());
if (select) {
if (select->State() & states::MULTISELECTABLE) select->UnselectAll();
SetSelected(true);
}
}
void LocalAccessible::TakeFocus() const {
nsIFrame* frame = GetFrame();
if (!frame) return;
nsIContent* focusContent = mContent;
// If the accessible focus is managed by container widget then focus the
// widget and set the accessible as its current item.
if (!frame->IsFocusable()) {
LocalAccessible* widget = ContainerWidget();
if (widget && widget->AreItemsOperable()) {
nsIContent* widgetElm = widget->GetContent();
nsIFrame* widgetFrame = widgetElm->GetPrimaryFrame();
if (widgetFrame && widgetFrame->IsFocusable()) {
focusContent = widgetElm;
widget->SetCurrentItem(this);
}
}
}
if (RefPtr<nsFocusManager> fm = nsFocusManager::GetFocusManager()) {
dom::AutoHandlingUserInputStatePusher inputStatePusher(true);
// XXXbz: Can we actually have a non-element content here?
RefPtr<dom::Element> element = dom::Element::FromNodeOrNull(focusContent);
fm->SetFocus(element, 0);
}
}
void LocalAccessible::NameFromAssociatedXULLabel(DocAccessible* aDocument,
nsIContent* aElm,
nsString& aName) {
LocalAccessible* label = nullptr;
XULLabelIterator iter(aDocument, aElm);
while ((label = iter.Next())) {
// Check if label's value attribute is used
label->Elm()->GetAttr(nsGkAtoms::value, aName);
if (aName.IsEmpty()) {
// If no value attribute, a non-empty label must contain
// children that define its text -- possibly using HTML
nsTextEquivUtils::AppendTextEquivFromContent(label, label->Elm(), &aName);
}
}
aName.CompressWhitespace();
}
void LocalAccessible::XULElmName(DocAccessible* aDocument, nsIContent* aElm,
nsString& aName) {
/**
* 3 main cases for XUL Controls to be labeled
* 1 - control contains label="foo"
* 2 - non-child label contains control="controlID"
* - label has either value="foo" or children
* 3 - name from subtree; e.g. a child label element
* Cases 1 and 2 are handled here.
* Case 3 is handled by GetNameFromSubtree called in NativeName.
* Once a label is found, the search is discontinued, so a control
* that has a label attribute as well as having a label external to
* the control that uses the control="controlID" syntax will use
* the label attribute for its Name.
*/
// CASE #1 (via label attribute) -- great majority of the cases
// Only do this if this is not a select control element, which uses label
// attribute to indicate, which option is selected.
nsCOMPtr<nsIDOMXULSelectControlElement> select =
aElm->AsElement()->AsXULSelectControl();
if (!select) {
aElm->AsElement()->GetAttr(nsGkAtoms::label, aName);
}
// CASE #2 -- label as <label control="id" ... ></label>
if (aName.IsEmpty()) {
NameFromAssociatedXULLabel(aDocument, aElm, aName);
}
aName.CompressWhitespace();
}
nsresult LocalAccessible::HandleAccEvent(AccEvent* aEvent) {
NS_ENSURE_ARG_POINTER(aEvent);
if (profiler_thread_is_being_profiled_for_markers()) {
nsAutoCString strEventType;
GetAccService()->GetStringEventType(aEvent->GetEventType(), strEventType);
nsAutoCString strMarker;
strMarker.AppendLiteral("A11y Event - ");
strMarker.Append(strEventType);
PROFILER_MARKER_UNTYPED(strMarker, A11Y);
}
if (IPCAccessibilityActive() && Document()) {
DocAccessibleChild* ipcDoc = mDoc->IPCDoc();
// If ipcDoc is null, we can't fire the event to the client. We shouldn't
// have fired the event in the first place, since this makes events
// inconsistent for local and remote documents. To avoid this, don't call
// nsEventShell::FireEvent on a DocAccessible for which
// HasLoadState(eTreeConstructed) is false.
MOZ_ASSERT(ipcDoc);
if (ipcDoc) {
uint64_t id = aEvent->GetAccessible()->ID();
switch (aEvent->GetEventType()) {
case nsIAccessibleEvent::EVENT_SHOW:
ipcDoc->ShowEvent(downcast_accEvent(aEvent));
break;
case nsIAccessibleEvent::EVENT_HIDE:
ipcDoc->SendHideEvent(id, aEvent->IsFromUserInput());
break;
case nsIAccessibleEvent::EVENT_INNER_REORDER:
case nsIAccessibleEvent::EVENT_REORDER:
if (IsTable()) {
SendCache(CacheDomain::Table, CacheUpdateType::Update);
}
#if defined(XP_WIN)
if (HasOwnContent() && mContent->IsMathMLElement()) {
// For any change in a MathML subtree, update the innerHTML cache on
// the root math element.
for (LocalAccessible* acc = this; acc; acc = acc->LocalParent()) {
if (acc->HasOwnContent() &&
acc->mContent->IsMathMLElement(nsGkAtoms::math)) {
mDoc->QueueCacheUpdate(acc, CacheDomain::InnerHTML);
}
}
}
#endif // defined(XP_WIN)
// reorder events on the application acc aren't necessary to tell the
// parent about new top level documents.
if (!aEvent->GetAccessible()->IsApplication()) {
ipcDoc->SendEvent(id, aEvent->GetEventType());
}
break;
case nsIAccessibleEvent::EVENT_STATE_CHANGE: {
AccStateChangeEvent* event = downcast_accEvent(aEvent);
ipcDoc->SendStateChangeEvent(id, event->GetState(),
event->IsStateEnabled());
break;
}
case nsIAccessibleEvent::EVENT_TEXT_CARET_MOVED: {
AccCaretMoveEvent* event = downcast_accEvent(aEvent);
ipcDoc->SendCaretMoveEvent(
id, event->GetCaretOffset(), event->IsSelectionCollapsed(),
event->IsAtEndOfLine(), event->GetGranularity());
break;
}
case nsIAccessibleEvent::EVENT_TEXT_INSERTED:
case nsIAccessibleEvent::EVENT_TEXT_REMOVED: {
AccTextChangeEvent* event = downcast_accEvent(aEvent);
const nsString& text = event->ModifiedText();
ipcDoc->SendTextChangeEvent(
id, text, event->GetStartOffset(), event->GetLength(),
event->IsTextInserted(), event->IsFromUserInput());
break;
}
case nsIAccessibleEvent::EVENT_SELECTION:
case nsIAccessibleEvent::EVENT_SELECTION_ADD:
case nsIAccessibleEvent::EVENT_SELECTION_REMOVE: {
AccSelChangeEvent* selEvent = downcast_accEvent(aEvent);
ipcDoc->SendSelectionEvent(id, selEvent->Widget()->ID(),
aEvent->GetEventType());
break;
}
case nsIAccessibleEvent::EVENT_VIRTUALCURSOR_CHANGED: {
AccVCChangeEvent* vcEvent = downcast_accEvent(aEvent);
LocalAccessible* position = vcEvent->NewAccessible();
LocalAccessible* oldPosition = vcEvent->OldAccessible();
ipcDoc->SendVirtualCursorChangeEvent(
id, oldPosition ? oldPosition->ID() : 0,
position ? position->ID() : 0, vcEvent->Reason(),
vcEvent->IsFromUserInput());
break;
}
case nsIAccessibleEvent::EVENT_FOCUS:
ipcDoc->SendFocusEvent(id);
break;
case nsIAccessibleEvent::EVENT_SCROLLING_END:
case nsIAccessibleEvent::EVENT_SCROLLING: {
AccScrollingEvent* scrollingEvent = downcast_accEvent(aEvent);
ipcDoc->SendScrollingEvent(
id, aEvent->GetEventType(), scrollingEvent->ScrollX(),
scrollingEvent->ScrollY(), scrollingEvent->MaxScrollX(),
scrollingEvent->MaxScrollY());
break;
}
#if !defined(XP_WIN)
case nsIAccessibleEvent::EVENT_ANNOUNCEMENT: {
AccAnnouncementEvent* announcementEvent = downcast_accEvent(aEvent);
ipcDoc->SendAnnouncementEvent(id, announcementEvent->Announcement(),
announcementEvent->Priority());
break;
}
#endif // !defined(XP_WIN)
case nsIAccessibleEvent::EVENT_TEXT_SELECTION_CHANGED: {
AccTextSelChangeEvent* textSelChangeEvent = downcast_accEvent(aEvent);
AutoTArray<TextRange, 1> ranges;
textSelChangeEvent->SelectionRanges(&ranges);
nsTArray<TextRangeData> textRangeData(ranges.Length());
for (size_t i = 0; i < ranges.Length(); i++) {
const TextRange& range = ranges.ElementAt(i);
LocalAccessible* start = range.StartContainer()->AsLocal();
LocalAccessible* end = range.EndContainer()->AsLocal();
textRangeData.AppendElement(TextRangeData(start->ID(), end->ID(),
range.StartOffset(),
range.EndOffset()));
}
ipcDoc->SendTextSelectionChangeEvent(id, textRangeData);
break;
}
case nsIAccessibleEvent::EVENT_DESCRIPTION_CHANGE:
case nsIAccessibleEvent::EVENT_NAME_CHANGE: {
SendCache(CacheDomain::NameAndDescription, CacheUpdateType::Update);
ipcDoc->SendEvent(id, aEvent->GetEventType());
break;
}
case nsIAccessibleEvent::EVENT_TEXT_VALUE_CHANGE:
case nsIAccessibleEvent::EVENT_VALUE_CHANGE: {
SendCache(CacheDomain::Value, CacheUpdateType::Update);
ipcDoc->SendEvent(id, aEvent->GetEventType());
break;
}
default:
ipcDoc->SendEvent(id, aEvent->GetEventType());
}
}
}
if (nsCoreUtils::AccEventObserversExist()) {
nsCoreUtils::DispatchAccEvent(MakeXPCEvent(aEvent));
}
if (IPCAccessibilityActive()) {
return NS_OK;
}