forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAccessible.cpp
2729 lines (2271 loc) · 91.9 KB
/
Accessible.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 "Accessible-inl.h"
#include "nsIXBLAccessible.h"
#include "EmbeddedObjCollector.h"
#include "AccGroupInfo.h"
#include "AccIterator.h"
#include "nsAccUtils.h"
#include "nsAccessibilityService.h"
#include "ApplicationAccessible.h"
#include "nsAccessiblePivot.h"
#include "nsGenericHTMLElement.h"
#include "NotificationController.h"
#include "nsEventShell.h"
#include "nsTextEquivUtils.h"
#include "DocAccessibleChild.h"
#include "EventTree.h"
#include "GeckoProfiler.h"
#include "Relation.h"
#include "Role.h"
#include "RootAccessible.h"
#include "States.h"
#include "StyleInfo.h"
#include "TableAccessible.h"
#include "TableCellAccessible.h"
#include "TreeWalker.h"
#include "XULDocument.h"
#include "nsIDOMXULButtonElement.h"
#include "nsIDOMXULSelectCntrlEl.h"
#include "nsIDOMXULSelectCntrlItemEl.h"
#include "nsINodeList.h"
#include "nsPIDOMWindow.h"
#include "mozilla/dom/Document.h"
#include "nsIContent.h"
#include "nsIForm.h"
#include "nsIFormControl.h"
#include "nsDeckFrame.h"
#include "nsLayoutUtils.h"
#include "nsIStringBundle.h"
#include "nsPresContext.h"
#include "nsIFrame.h"
#include "nsView.h"
#include "nsIDocShellTreeItem.h"
#include "nsIScrollableFrame.h"
#include "nsFocusManager.h"
#include "nsString.h"
#include "nsUnicharUtils.h"
#include "nsReadableUtils.h"
#include "prdtoa.h"
#include "nsAtom.h"
#include "nsIURI.h"
#include "nsArrayUtils.h"
#include "nsIMutableArray.h"
#include "nsIObserverService.h"
#include "nsIServiceManager.h"
#include "nsWhitespaceTokenizer.h"
#include "nsAttrName.h"
#include "nsPersistentProperties.h"
#include "mozilla/Assertions.h"
#include "mozilla/BasicEvents.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/EventStates.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/PresShell.h"
#include "mozilla/Unused.h"
#include "mozilla/Preferences.h"
#include "mozilla/dom/CanvasRenderingContext2D.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLCanvasElement.h"
#include "mozilla/dom/HTMLBodyElement.h"
#include "mozilla/dom/KeyboardEventBinding.h"
#include "mozilla/dom/TreeWalker.h"
using namespace mozilla;
using namespace mozilla::a11y;
////////////////////////////////////////////////////////////////////////////////
// Accessible: nsISupports and cycle collection
NS_IMPL_CYCLE_COLLECTION_CLASS(Accessible)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Accessible)
tmp->Shutdown();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Accessible)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContent, mDoc)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Accessible)
NS_INTERFACE_MAP_ENTRY_CONCRETE(Accessible)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, Accessible)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(Accessible)
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_DESTROY(Accessible, LastRelease())
Accessible::Accessible(nsIContent* aContent, DocAccessible* aDoc)
: mContent(aContent),
mDoc(aDoc),
mParent(nullptr),
mIndexInParent(-1),
mRoleMapEntryIndex(aria::NO_ROLE_MAP_ENTRY_INDEX),
mStateFlags(0),
mContextFlags(0),
mType(0),
mGenericTypes(0),
mReorderEventTarget(false),
mShowEventTarget(false),
mHideEventTarget(false) {
mBits.groupInfo = nullptr;
mInt.mIndexOfEmbeddedChild = -1;
// Assign an ID to this Accessible for use in UniqueID().
recordreplay::RegisterThing(this);
}
Accessible::~Accessible() {
NS_ASSERTION(!mDoc, "LastRelease was never called!?!");
recordreplay::UnregisterThing(this);
}
ENameValueFlag Accessible::Name(nsString& aName) const {
aName.Truncate();
if (!HasOwnContent()) return eNameOK;
ARIAName(aName);
if (!aName.IsEmpty()) return eNameOK;
nsCOMPtr<nsIXBLAccessible> xblAccessible(do_QueryInterface(mContent));
if (xblAccessible) {
xblAccessible->GetAccessibleName(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(kNameSpaceID_None, nsGkAtoms::title,
aName)) {
aName.CompressWhitespace();
return eNameFromTooltip;
}
} else if (mContent->IsXULElement()) {
if (mContent->AsElement()->GetAttr(kNameSpaceID_None,
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;
}
}
}
if (nameFlag != eNoNameOnPurpose) aName.SetIsVoid(true);
return nameFlag;
}
void Accessible::Description(nsString& aDescription) {
// There are 4 conditions that make an accessible have no accDescription:
// 1. it's a text node; or
// 2. It has no DHTML describedby 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;
nsTextEquivUtils::GetTextEquivFromIDRefs(this, nsGkAtoms::aria_describedby,
aDescription);
if (aDescription.IsEmpty()) {
NativeDescription(aDescription);
if (aDescription.IsEmpty()) {
// Keep the Name() method logic.
if (mContent->IsHTMLElement()) {
mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::title,
aDescription);
} else if (mContent->IsXULElement()) {
mContent->AsElement()->GetAttr(kNameSpaceID_None,
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 Accessible::AccessKey() const {
if (!HasOwnContent()) return KeyBinding();
uint32_t key = nsCoreUtils::GetAccessKeyFor(mContent);
if (!key && mContent->IsElement()) {
Accessible* 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();
} else if (mContent->IsXULElement()) {
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 (Preferences::GetInt("ui.key.generalAccessKey", -1)) {
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->GetUncomposedDoc();
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:
rv = Preferences::GetInt("ui.key.chromeAccess", &modifierMask);
break;
case nsIDocShellTreeItem::typeContent:
rv = Preferences::GetInt("ui.key.contentAccess", &modifierMask);
break;
}
return NS_SUCCEEDED(rv) ? KeyBinding(key, modifierMask) : KeyBinding();
}
KeyBinding Accessible::KeyboardShortcut() const { return KeyBinding(); }
void Accessible::TranslateString(const nsString& aKey, nsAString& aStringOut) {
nsCOMPtr<nsIStringBundleService> stringBundleService =
services::GetStringBundleService();
if (!stringBundleService) return;
nsCOMPtr<nsIStringBundle> stringBundle;
stringBundleService->CreateBundle(
"chrome://global-platform/locale/accessible.properties",
getter_AddRefs(stringBundle));
if (!stringBundle) return;
nsAutoString xsValue;
nsresult rv = stringBundle->GetStringFromName(
NS_ConvertUTF16toUTF8(aKey).get(), xsValue);
if (NS_SUCCEEDED(rv)) aStringOut.Assign(xsValue);
}
uint64_t Accessible::VisibilityState() const {
nsIFrame* frame = GetFrame();
if (!frame) {
// Element having display:contents is considered visible semantically,
// despite it doesn't have a visually visible box.
if (mContent->IsElement() && mContent->AsElement()->IsDisplayContents()) {
return states::OFFSCREEN;
}
return states::INVISIBLE;
}
// Walk the parent frame chain to see if there's invisible parent or the frame
// is in background tab.
if (!frame->StyleVisibility()->IsVisible()) return states::INVISIBLE;
// Offscreen state if the document's visibility state is not visible.
if (Document()->IsHidden()) return states::OFFSCREEN;
nsIFrame* curFrame = frame;
do {
nsView* view = curFrame->GetView();
if (view && view->GetVisibility() == nsViewVisibility_kHide)
return states::INVISIBLE;
if (nsLayoutUtils::IsPopup(curFrame)) return 0;
// Offscreen state for background tab content and invisible for not selected
// deck panel.
nsIFrame* parentFrame = curFrame->GetParent();
nsDeckFrame* deckFrame = do_QueryFrame(parentFrame);
if (deckFrame && deckFrame->GetSelectedBox() != curFrame) {
#if defined(ANDROID)
// In Fennec instead of a <tabpanels> container there is a <deck>
// with direct <browser> children.
if (curFrame->GetContent()->IsXULElement(nsGkAtoms::browser))
return states::OFFSCREEN;
#else
if (deckFrame->GetContent()->IsXULElement(nsGkAtoms::tabpanels))
return states::OFFSCREEN;
#endif
MOZ_ASSERT_UNREACHABLE(
"Children of not selected deck panel are not accessible.");
return states::INVISIBLE;
}
// 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);
if (scrollableFrame) {
nsRect scrollPortRect = scrollableFrame->GetScrollPortRect();
nsRect frameRect = nsLayoutUtils::TransformFrameRectToAncestor(
frame, frame->GetRectRelativeToSelf(), parentFrame);
if (!scrollPortRect.Contains(frameRect)) {
const nscoord kMinPixels = nsPresContext::CSSPixelsToAppUnits(12);
scrollPortRect.Deflate(kMinPixels, kMinPixels);
if (!scrollPortRect.Intersects(frameRect)) return states::OFFSCREEN;
}
}
if (!parentFrame) {
parentFrame = nsLayoutUtils::GetCrossDocParentFrame(curFrame);
if (parentFrame && !parentFrame->StyleVisibility()->IsVisible())
return states::INVISIBLE;
}
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->GetStateBits() & 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 Accessible::NativeState() const {
uint64_t state = 0;
if (!IsInDocument()) state |= states::STALE;
if (HasOwnContent() && mContent->IsElement()) {
EventStates elementState = mContent->AsElement()->State();
if (elementState.HasState(NS_EVENT_STATE_INVALID)) state |= states::INVALID;
if (elementState.HasState(NS_EVENT_STATE_REQUIRED))
state |= states::REQUIRED;
state |= NativeInteractiveState();
if (FocusMgr()->IsFocused(this)) state |= states::FOCUSED;
}
// Gather states::INVISIBLE and states::OFFSCREEN flags for this object.
state |= VisibilityState();
nsIFrame* frame = GetFrame();
if (frame) {
if (frame->GetStateBits() & NS_FRAME_OUT_OF_FLOW) state |= states::FLOATING;
// XXX we should look at layout for non XUL box frames, but need to decide
// how that interacts with ARIA.
if (HasOwnContent() && mContent->IsXULElement() && frame->IsXULBoxFrame()) {
const nsStyleXUL* xulStyle = frame->StyleXUL();
if (xulStyle && frame->IsXULBoxFrame()) {
// In XUL all boxes are either vertical or horizontal
if (xulStyle->mBoxOrient == StyleBoxOrient::Vertical)
state |= states::VERTICAL;
else
state |= states::HORIZONTAL;
}
}
}
// Check if a XUL element has the popup attribute (an attached popup menu).
if (HasOwnContent() && mContent->IsXULElement() &&
mContent->AsElement()->HasAttr(kNameSpaceID_None, 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 Accessible::NativeInteractiveState() const {
if (!mContent->IsElement()) return 0;
if (NativelyUnavailable()) return states::UNAVAILABLE;
nsIFrame* frame = GetFrame();
if (frame && frame->IsFocusable()) return states::FOCUSABLE;
return 0;
}
uint64_t Accessible::NativeLinkState() const { return 0; }
bool Accessible::NativelyUnavailable() const {
if (mContent->IsHTMLElement())
return mContent->AsElement()->State().HasState(NS_EVENT_STATE_DISABLED);
return mContent->IsElement() && mContent->AsElement()->AttrValueIs(
kNameSpaceID_None, nsGkAtoms::disabled,
nsGkAtoms::_true, eCaseMatters);
}
Accessible* Accessible::FocusedChild() {
Accessible* focus = FocusMgr()->FocusedAccessible();
if (focus && (focus == this || focus->Parent() == this)) return focus;
return nullptr;
}
Accessible* Accessible::ChildAtPoint(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.
Accessible* fallbackAnswer = nullptr;
nsIntRect 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();
WidgetMouseEvent dummyEvent(true, eMouseMove, rootWidget,
WidgetMouseEvent::eSynthesized);
dummyEvent.mRefPoint =
LayoutDeviceIntPoint(aX - rootRect.X(), aY - rootRect.Y());
nsIFrame* popupFrame = nsLayoutUtils::GetPopupFrameForEventCoordinates(
accDocument->PresContext()->GetRootPresContext(), &dummyEvent);
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());
Accessible* popupAcc =
popupDoc->GetAccessibleOrContainer(popupFrame->GetContent());
Accessible* popupChild = this;
while (popupChild && !popupChild->IsDoc() && popupChild != popupAcc)
popupChild = popupChild->Parent();
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());
// We need to take into account a non-1 resolution set on the presshell.
// This happens in mobile platforms with async pinch zooming.
offset = offset.RemoveResolution(presContext->PresShell()->GetResolution());
nsIFrame* foundFrame = nsLayoutUtils::GetFrameForPoint(startFrame, 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;
Accessible* 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.
Accessible* child = accessible;
while (child != this) {
Accessible* parent = child->Parent();
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 == eDirectChild) 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();
for (uint32_t childIdx = 0; childIdx < childCount; childIdx++) {
Accessible* child = accessible->GetChildAt(childIdx);
nsIntRect childRect = child->Bounds();
if (childRect.Contains(aX, aY) &&
(child->State() & states::INVISIBLE) == 0) {
if (aWhichChild == eDeepestChild)
return child->ChildAtPoint(aX, aY, eDeepestChild);
return child;
}
}
return accessible;
}
nsRect Accessible::RelativeBounds(nsIFrame** aBoundingFrame) const {
nsIFrame* frame = GetFrame();
if (frame && mContent) {
bool* pHasHitRegionRect =
static_cast<bool*>(mContent->GetProperty(nsGkAtoms::hitregion));
MOZ_ASSERT(pHasHitRegionRect == nullptr || *pHasHitRegionRect,
"hitregion property is always null or true");
bool hasHitRegionRect = pHasHitRegionRect != nullptr && *pHasHitRegionRect;
if (hasHitRegionRect && mContent->IsElement()) {
// This is for canvas fallback content
// Find a canvas frame the found hit region is relative to.
nsIFrame* canvasFrame = frame->GetParent();
if (canvasFrame) {
canvasFrame = nsLayoutUtils::GetClosestFrameOfType(
canvasFrame, LayoutFrameType::HTMLCanvas);
}
// make the canvas the bounding frame
if (canvasFrame) {
*aBoundingFrame = canvasFrame;
dom::HTMLCanvasElement* canvas =
dom::HTMLCanvasElement::FromNode(canvasFrame->GetContent());
// get the bounding rect of the hit region
nsRect bounds;
if (canvas && canvas->CountContexts() &&
canvas->GetContextAtIndex(0)->GetHitRegionRect(
mContent->AsElement(), bounds)) {
return bounds;
}
}
}
*aBoundingFrame = nsLayoutUtils::GetContainingBlockForClientRect(frame);
return nsLayoutUtils::GetAllInFlowRectsUnion(
frame, *aBoundingFrame, nsLayoutUtils::RECTS_ACCOUNT_FOR_TRANSFORMS);
}
return nsRect();
}
nsRect Accessible::BoundsInAppUnits() const {
nsIFrame* boundingFrame = nullptr;
nsRect unionRectTwips = RelativeBounds(&boundingFrame);
if (!boundingFrame) {
return nsRect();
}
// We need to take into account a non-1 resolution set on the presshell.
// This happens in mobile platforms with async pinch zooming. Here we
// scale the bounds before adding the screen-relative offset.
unionRectTwips.ScaleRoundOut(
mDoc->PresContext()->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;
}
nsIntRect Accessible::Bounds() const {
return BoundsInAppUnits().ToNearestPixels(
mDoc->PresContext()->AppUnitsPerDevPixel());
}
nsIntRect Accessible::BoundsInCSSPixels() const {
return BoundsInAppUnits().ToNearestPixels(AppUnitsPerCSSPixel());
}
void Accessible::SetSelected(bool aSelect) {
if (!HasOwnContent()) return;
Accessible* 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,
NS_LITERAL_STRING("true"), true);
} else {
mContent->AsElement()->UnsetAttr(kNameSpaceID_None,
nsGkAtoms::aria_selected, true);
}
}
return;
}
if (aSelect) TakeFocus();
}
}
void Accessible::TakeSelection() {
Accessible* select = nsAccUtils::GetSelectableContainer(this, State());
if (select) {
if (select->State() & states::MULTISELECTABLE) select->UnselectAll();
SetSelected(true);
}
}
void Accessible::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()) {
Accessible* widget = ContainerWidget();
if (widget && widget->AreItemsOperable()) {
nsIContent* widgetElm = widget->GetContent();
nsIFrame* widgetFrame = widgetElm->GetPrimaryFrame();
if (widgetFrame && widgetFrame->IsFocusable()) {
focusContent = widgetElm;
widget->SetCurrentItem(this);
}
}
}
nsFocusManager* fm = nsFocusManager::GetFocusManager();
if (fm) {
AutoHandlingUserInputStatePusher inputStatePusher(true, nullptr,
focusContent->OwnerDoc());
// XXXbz: Can we actually have a non-element content here?
RefPtr<Element> element =
focusContent->IsElement() ? focusContent->AsElement() : nullptr;
fm->SetFocus(element, 0);
}
}
void Accessible::XULElmName(DocAccessible* aDocument, nsIContent* aElm,
nsString& aName) {
/**
* 3 main cases for XUL Controls to be labeled
* 1 - control contains label="foo"
* 2 - control has, as a child, a label element
* - label has either value="foo" or children
* 3 - non-child label contains control="controlID"
* - label has either value="foo" or children
* Once a label is found, the search is discontinued, so a control
* that has a label child as well as having a label external to
* the control that uses the control="controlID" syntax will use
* the child label for its Name.
*/
// CASE #1 (via label attribute) -- great majority of the cases
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemEl =
aElm->AsElement()->AsXULSelectControlItem();
if (itemEl) {
itemEl->GetLabel(aName);
} else {
// Use @label 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(kNameSpaceID_None, nsGkAtoms::label, aName);
}
}
// CASES #2 and #3 ------ label as a child or <label control="id" ... >
// </label>
if (aName.IsEmpty()) {
Accessible* label = nullptr;
XULLabelIterator iter(aDocument, aElm);
while ((label = iter.Next())) {
// Check if label's value attribute is used
label->Elm()->GetAttr(kNameSpaceID_None, 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();
if (!aName.IsEmpty()) return;
// Can get text from title of <toolbaritem> if we're a child of a
// <toolbaritem>
nsIContent* bindingParent = aElm->GetBindingParent();
nsIContent* parent =
bindingParent ? bindingParent->GetParent() : aElm->GetParent();
nsAutoString ancestorTitle;
while (parent) {
if (parent->IsXULElement(nsGkAtoms::toolbaritem) &&
parent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::title,
ancestorTitle)) {
// Before returning this, check if the element itself has a tooltip:
if (aElm->IsElement() &&
aElm->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::tooltiptext,
aName)) {
aName.CompressWhitespace();
return;
}
aName.Assign(ancestorTitle);
aName.CompressWhitespace();
return;
}
parent = parent->GetParent();
}
}
nsresult Accessible::HandleAccEvent(AccEvent* aEvent) {
NS_ENSURE_ARG_POINTER(aEvent);
#ifdef MOZ_GECKO_PROFILER
if (profiler_thread_is_being_profiled()) {
nsAutoCString strEventType;
GetAccService()->GetStringEventType(aEvent->GetEventType(), strEventType);
nsAutoCString strMarker;
strMarker.AppendLiteral("A11y Event - ");
strMarker.Append(strEventType);
profiler_add_marker(strMarker.get(), JS::ProfilingCategoryPair::OTHER);
}
#endif
if (IPCAccessibilityActive() && Document()) {
DocAccessibleChild* ipcDoc = mDoc->IPCDoc();
MOZ_ASSERT(ipcDoc);
if (ipcDoc) {
uint64_t id = aEvent->GetAccessible()->IsDoc()
? 0
: reinterpret_cast<uintptr_t>(
aEvent->GetAccessible()->UniqueID());
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_REORDER:
// 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());
break;
}
case nsIAccessibleEvent::EVENT_TEXT_INSERTED:
case nsIAccessibleEvent::EVENT_TEXT_REMOVED: {
AccTextChangeEvent* event = downcast_accEvent(aEvent);
const nsString& text = event->ModifiedText();
#if defined(XP_WIN)
// On Windows, events for live region updates containing embedded
// objects require us to dispatch synchronous events.
bool sync = text.Contains(L'\xfffc') &&
nsAccUtils::IsARIALive(aEvent->GetAccessible());
#endif
ipcDoc->SendTextChangeEvent(id, text, event->GetStartOffset(),
event->GetLength(),
event->IsTextInserted(),
event->IsFromUserInput()
#if defined(XP_WIN)
// This parameter only exists on Windows.
,
sync
#endif
);
break;
}
case nsIAccessibleEvent::EVENT_SELECTION:
case nsIAccessibleEvent::EVENT_SELECTION_ADD:
case nsIAccessibleEvent::EVENT_SELECTION_REMOVE: {
AccSelChangeEvent* selEvent = downcast_accEvent(aEvent);
uint64_t widgetID =
selEvent->Widget()->IsDoc()
? 0
: reinterpret_cast<uintptr_t>(selEvent->Widget()->UniqueID());
ipcDoc->SendSelectionEvent(id, widgetID, aEvent->GetEventType());
break;
}
case nsIAccessibleEvent::EVENT_VIRTUALCURSOR_CHANGED: {
AccVCChangeEvent* vcEvent = downcast_accEvent(aEvent);
Accessible* position = vcEvent->NewAccessible();
Accessible* oldPosition = vcEvent->OldAccessible();
ipcDoc->SendVirtualCursorChangeEvent(
id,
oldPosition ? reinterpret_cast<uintptr_t>(oldPosition->UniqueID())
: 0,
vcEvent->OldStartOffset(), vcEvent->OldEndOffset(),
position ? reinterpret_cast<uintptr_t>(position->UniqueID()) : 0,
vcEvent->NewStartOffset(), vcEvent->NewEndOffset(),
vcEvent->Reason(), vcEvent->BoundaryType(),
vcEvent->IsFromUserInput());
break;
}
#if defined(XP_WIN)
case nsIAccessibleEvent::EVENT_FOCUS: {
ipcDoc->SendFocusEvent(id);
break;
}
#endif
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
default:
ipcDoc->SendEvent(id, aEvent->GetEventType());
}
}
}
if (nsCoreUtils::AccEventObserversExist()) {
nsCoreUtils::DispatchAccEvent(MakeXPCEvent(aEvent));
}
return NS_OK;
}
already_AddRefed<nsIPersistentProperties> Accessible::Attributes() {
nsCOMPtr<nsIPersistentProperties> attributes = NativeAttributes();
if (!HasOwnContent() || !mContent->IsElement()) return attributes.forget();
// 'xml-roles' attribute for landmark.
nsAtom* landmark = LandmarkRole();
if (landmark) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::xmlroles, landmark);
} else {
// 'xml-roles' attribute coming from ARIA.
nsAutoString xmlRoles;
if (mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::role,
xmlRoles))
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::xmlroles, xmlRoles);
}
// Expose object attributes from ARIA attributes.
nsAutoString unused;
aria::AttrIterator attribIter(mContent);
nsAutoString name, value;
while (attribIter.Next(name, value))
attributes->SetStringProperty(NS_ConvertUTF16toUTF8(name), value, unused);
// If there is no aria-live attribute then expose default value of 'live'
// object attribute used for ARIA role of this accessible.
const nsRoleMapEntry* roleMapEntry = ARIARoleMap();
if (roleMapEntry) {
if (roleMapEntry->Is(nsGkAtoms::searchbox)) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::textInputType,
NS_LITERAL_STRING("search"));
}
nsAutoString live;
nsAccUtils::GetAccAttr(attributes, nsGkAtoms::live, live);
if (live.IsEmpty()) {
if (nsAccUtils::GetLiveAttrValue(roleMapEntry->liveAttRule, live))
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::live, live);
}
}
return attributes.forget();
}
already_AddRefed<nsIPersistentProperties> Accessible::NativeAttributes() {
RefPtr<nsPersistentProperties> attributes = new nsPersistentProperties();
nsAutoString unused;
// We support values, so expose the string value as well, via the valuetext
// object attribute. We test for the value interface because we don't want
// to expose traditional Value() information such as URL's on links and
// documents, or text in an input.
if (HasNumericValue()) {
nsAutoString valuetext;
Value(valuetext);
attributes->SetStringProperty(NS_LITERAL_CSTRING("valuetext"), valuetext,
unused);
}
// Expose checkable object attribute if the accessible has checkable state
if (State() & states::CHECKABLE) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::checkable,
NS_LITERAL_STRING("true"));