forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAXObjectCache.cpp
More file actions
4102 lines (3453 loc) · 153 KB
/
AXObjectCache.cpp
File metadata and controls
4102 lines (3453 loc) · 153 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if ENABLE(ACCESSIBILITY)
#include "AXObjectCache.h"
#include "AXImage.h"
#include "AXIsolatedObject.h"
#include "AXIsolatedTree.h"
#include "AXLogger.h"
#include "AccessibilityARIAGrid.h"
#include "AccessibilityARIAGridCell.h"
#include "AccessibilityARIAGridRow.h"
#include "AccessibilityAttachment.h"
#include "AccessibilityImageMapLink.h"
#include "AccessibilityLabel.h"
#include "AccessibilityList.h"
#include "AccessibilityListBox.h"
#include "AccessibilityListBoxOption.h"
#include "AccessibilityMathMLElement.h"
#include "AccessibilityMediaObject.h"
#include "AccessibilityMenuList.h"
#include "AccessibilityMenuListOption.h"
#include "AccessibilityMenuListPopup.h"
#include "AccessibilityProgressIndicator.h"
#include "AccessibilityRenderObject.h"
#include "AccessibilitySVGElement.h"
#include "AccessibilitySVGRoot.h"
#include "AccessibilityScrollView.h"
#include "AccessibilityScrollbar.h"
#include "AccessibilitySlider.h"
#include "AccessibilitySpinButton.h"
#include "AccessibilityTable.h"
#include "AccessibilityTableCell.h"
#include "AccessibilityTableColumn.h"
#include "AccessibilityTableHeaderContainer.h"
#include "AccessibilityTableRow.h"
#include "AccessibilityTree.h"
#include "AccessibilityTreeItem.h"
#include "CaretRectComputation.h"
#include "Document.h"
#include "Editing.h"
#include "Editor.h"
#include "ElementIterator.h"
#include "FocusController.h"
#include "Frame.h"
#include "HTMLAreaElement.h"
#include "HTMLCanvasElement.h"
#include "HTMLDialogElement.h"
#include "HTMLImageElement.h"
#include "HTMLInputElement.h"
#include "HTMLLabelElement.h"
#include "HTMLMediaElement.h"
#include "HTMLMeterElement.h"
#include "HTMLNames.h"
#include "HTMLOptGroupElement.h"
#include "HTMLOptionElement.h"
#include "HTMLParserIdioms.h"
#include "HTMLSelectElement.h"
#include "HTMLTableElement.h"
#include "HTMLTablePartElement.h"
#include "HTMLTableSectionElement.h"
#include "HTMLTextFormControlElement.h"
#include "InlineRunAndOffset.h"
#include "MathMLElement.h"
#include "Page.h"
#include "ProgressTracker.h"
#include "Range.h"
#include "RenderAttachment.h"
#include "RenderImage.h"
#include "RenderLayer.h"
#include "RenderLineBreak.h"
#include "RenderListBox.h"
#include "RenderMathMLOperator.h"
#include "RenderMenuList.h"
#include "RenderMeter.h"
#include "RenderProgress.h"
#include "RenderSlider.h"
#include "RenderTable.h"
#include "RenderTableCell.h"
#include "RenderTableRow.h"
#include "RenderView.h"
#include "SVGElement.h"
#include "ScriptDisallowedScope.h"
#include "ScrollView.h"
#include "TextBoundaries.h"
#include "TextControlInnerElements.h"
#include "TextIterator.h"
#include <utility>
#include <wtf/DataLog.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/SetForScope.h>
#include <wtf/text/AtomString.h>
#if COMPILER(MSVC)
// See https://msdn.microsoft.com/en-us/library/1wea5zwe.aspx
#pragma warning(disable: 4701)
#endif
namespace WebCore {
using namespace HTMLNames;
// Post value change notifications for password fields or elements contained in password fields at a 40hz interval to thwart analysis of typing cadence
static const Seconds accessibilityPasswordValueChangeNotificationInterval { 25_ms };
static bool rendererNeedsDeferredUpdate(const RenderObject& renderer)
{
ASSERT(!renderer.beingDestroyed());
auto& document = renderer.document();
return renderer.needsLayout() || document.needsStyleRecalc() || document.inRenderTreeUpdate() || (document.view() && document.view()->layoutContext().isInRenderTreeLayout());
}
static bool nodeAndRendererAreValid(Node* node)
{
if (!node)
return false;
auto* renderer = node->renderer();
return renderer && !renderer->beingDestroyed();
}
AccessibilityObjectInclusion AXComputedObjectAttributeCache::getIgnored(AXID id) const
{
auto it = m_idMapping.find(id);
return it != m_idMapping.end() ? it->value.ignored : AccessibilityObjectInclusion::DefaultBehavior;
}
void AXComputedObjectAttributeCache::setIgnored(AXID id, AccessibilityObjectInclusion inclusion)
{
HashMap<AXID, CachedAXObjectAttributes>::iterator it = m_idMapping.find(id);
if (it != m_idMapping.end())
it->value.ignored = inclusion;
else {
CachedAXObjectAttributes attributes;
attributes.ignored = inclusion;
m_idMapping.set(id, attributes);
}
}
AccessibilityReplacedText::AccessibilityReplacedText(const VisibleSelection& selection)
{
if (AXObjectCache::accessibilityEnabled()) {
m_replacedRange.startIndex.value = indexForVisiblePosition(selection.visibleStart(), m_replacedRange.startIndex.scope);
if (selection.isRange()) {
m_replacedText = AccessibilityObject::stringForVisiblePositionRange(selection);
m_replacedRange.endIndex.value = indexForVisiblePosition(selection.visibleEnd(), m_replacedRange.endIndex.scope);
} else
m_replacedRange.endIndex = m_replacedRange.startIndex;
}
}
void AccessibilityReplacedText::postTextStateChangeNotification(AXObjectCache* cache, AXTextEditType type, const String& text, const VisibleSelection& selection)
{
if (!cache)
return;
if (!AXObjectCache::accessibilityEnabled())
return;
VisiblePosition position = selection.start();
auto* node = highestEditableRoot(position.deepEquivalent(), HasEditableAXRole);
if (m_replacedText.length())
cache->postTextReplacementNotification(node, AXTextEditTypeDelete, m_replacedText, type, text, position);
else
cache->postTextStateChangeNotification(node, type, text, position);
}
bool AXObjectCache::gAccessibilityEnabled = false;
bool AXObjectCache::gAccessibilityEnhancedUserInterfaceEnabled = false;
void AXObjectCache::enableAccessibility()
{
ASSERT(isMainThread());
gAccessibilityEnabled = true;
}
void AXObjectCache::disableAccessibility()
{
gAccessibilityEnabled = false;
}
void AXObjectCache::setEnhancedUserInterfaceAccessibility(bool flag)
{
gAccessibilityEnhancedUserInterfaceEnabled = flag;
#if PLATFORM(MAC)
if (flag)
enableAccessibility();
#endif
}
AXObjectCache::AXObjectCache(Document& document)
: m_document(document)
, m_pageID(document.pageID())
, m_notificationPostTimer(*this, &AXObjectCache::notificationPostTimerFired)
, m_passwordNotificationPostTimer(*this, &AXObjectCache::passwordNotificationPostTimerFired)
, m_liveRegionChangedPostTimer(*this, &AXObjectCache::liveRegionChangedNotificationPostTimerFired)
, m_currentModalElement(nullptr)
, m_performCacheUpdateTimer(*this, &AXObjectCache::performCacheUpdateTimerFired)
{
AXTRACE(makeString("AXObjectCache::AXObjectCache 0x"_s, hex(reinterpret_cast<uintptr_t>(this))));
#ifndef NDEBUG
if (m_pageID)
AXLOG(makeString("pageID ", m_pageID->loggingString()));
else
AXLOG("No pageID.");
#endif
ASSERT(isMainThread());
// If loading completed before the cache was created, loading progress will have been reset to zero.
// Consider loading progress to be 100% in this case.
double loadingProgress = document.page() ? document.page()->progress().estimatedProgress() : 1;
if (loadingProgress <= 0)
loadingProgress = 1;
m_loadingProgress = loadingProgress;
}
AXObjectCache::~AXObjectCache()
{
AXTRACE(makeString("AXObjectCache::~AXObjectCache 0x"_s, hex(reinterpret_cast<uintptr_t>(this))));
m_notificationPostTimer.stop();
m_liveRegionChangedPostTimer.stop();
m_performCacheUpdateTimer.stop();
for (const auto& object : m_objects.values())
object->detach(AccessibilityDetachmentType::CacheDestroyed);
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
if (m_pageID)
AXIsolatedTree::removeTreeForPageID(*m_pageID);
#endif
}
bool AXObjectCache::isModalElement(Element& element) const
{
bool hasDialogRole = nodeHasRole(&element, "dialog"_s) || nodeHasRole(&element, "alertdialog"_s);
bool isAriaModal = equalLettersIgnoringASCIICase(element.attributeWithoutSynchronization(aria_modalAttr), "true"_s);
return (hasDialogRole && isAriaModal) || (is<HTMLDialogElement>(element) && downcast<HTMLDialogElement>(element).isModal());
}
void AXObjectCache::findModalNodes()
{
// Traverse the DOM tree to look for the aria-modal=true nodes or modal <dialog> elements.
for (Element* element = ElementTraversal::firstWithin(document().rootNode()); element; element = ElementTraversal::nextIncludingPseudo(*element)) {
if (isModalElement(*element))
m_modalElementsSet.add(element);
}
m_modalNodesInitialized = true;
}
bool AXObjectCache::modalElementHasAccessibleContent(Element& element)
{
// Unless you're trying to compute the new modal node, determining whether an element
// has accessible content is as easy as !getOrCreate(element)->children().isEmpty().
// So don't call this method on anything besides modal elements.
ASSERT(isModalElement(element));
// Because computing any object's children() is dependent on whether a modal is on the page,
// we'll need to walk the DOM and find non-ignored AX objects manually.
Vector<Node*> nodeStack = { element.firstChild() };
while (!nodeStack.isEmpty()) {
for (auto* node = nodeStack.takeLast(); node; node = node->nextSibling()) {
if (auto* axObject = getOrCreate(node)) {
if (!axObject->computeAccessibilityIsIgnored())
return true;
}
// Don't descend into subtrees for non-visible nodes.
if (isNodeVisible(node))
nodeStack.append(node->firstChild());
}
}
return false;
}
void AXObjectCache::updateCurrentModalNode()
{
auto* previousModal = m_currentModalElement.get();
m_currentModalElement = updateCurrentModalNodeInternal();
if (previousModal != m_currentModalElement.get()) {
childrenChanged(rootWebArea());
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
// Because the presence of a modal affects every element on the page,
// regenerate the entire isolated tree with the next cache update.
m_deferredRegenerateIsolatedTree = true;
#endif
}
}
Element* AXObjectCache::updateCurrentModalNodeInternal()
{
// There might be multiple modal dialog nodes.
// We use this function to pick the one we want.
if (m_modalElementsSet.isEmpty())
return nullptr;
// Pick the document active modal <dialog> element if it exists.
if (Element* activeModalDialog = document().activeModalDialog()) {
ASSERT(m_modalElementsSet.contains(activeModalDialog));
return activeModalDialog;
}
SetForScope retrievingCurrentModalNode(m_isRetrievingCurrentModalNode, true);
// If any of the modal nodes contains the keyboard focus, we want to pick that one.
// If not, we want to pick the last visible dialog in the DOM.
RefPtr<Element> focusedElement = document().focusedElement();
RefPtr<Element> lastVisible;
for (auto& element : m_modalElementsSet) {
// Elements in m_modalElementsSet may have become un-modal since we added them, but not yet removed
// as part of the asynchronous m_deferredModalChangedList handling. Skip these.
if (!element || !isModalElement(*element))
continue;
// To avoid trapping users in an empty modal, skip any non-visible element, or any element without accessible content.
if (!isNodeVisible(element) || !modalElementHasAccessibleContent(*element))
continue;
lastVisible = element;
if (focusedElement && focusedElement->isDescendantOf(element))
break;
}
return lastVisible.get();
}
bool AXObjectCache::isNodeVisible(Node* node) const
{
if (!is<Element>(node))
return false;
RenderObject* renderer = node->renderer();
if (!renderer)
return false;
const auto& style = renderer->style();
if (style.display() == DisplayType::None)
return false;
auto* renderLayer = renderer->enclosingLayer();
if (style.visibility() != Visibility::Visible && renderLayer && !renderLayer->hasVisibleContent())
return false;
// Check whether this object or any of its ancestors has opacity 0.
// The resulting opacity of a RenderObject is computed as the multiplication
// of its opacity times the opacities of its ancestors.
for (auto* renderObject = renderer; renderObject; renderObject = renderObject->parent()) {
if (!renderObject->style().opacity())
return false;
}
// We also need to consider aria hidden status.
if (!isNodeAriaVisible(node))
return false;
return true;
}
// This function returns the valid aria modal node.
Node* AXObjectCache::modalNode()
{
if (!m_modalNodesInitialized)
findModalNodes();
if (m_modalElementsSet.isEmpty())
return nullptr;
// Check the cached current valid aria modal node first.
// Usually when one dialog sets aria-modal=true, that dialog is the one we want.
if (isNodeVisible(m_currentModalElement.get()))
return m_currentModalElement.get();
// Recompute the valid aria modal node when m_currentModalElement is null or hidden.
updateCurrentModalNode();
return m_currentModalElement.get();
}
AccessibilityObject* AXObjectCache::focusedImageMapUIElement(HTMLAreaElement* areaElement)
{
// Find the corresponding accessibility object for the HTMLAreaElement. This should be
// in the list of children for its corresponding image.
if (!areaElement)
return nullptr;
HTMLImageElement* imageElement = areaElement->imageElement();
if (!imageElement)
return nullptr;
AccessibilityObject* axRenderImage = areaElement->document().axObjectCache()->getOrCreate(imageElement);
if (!axRenderImage)
return nullptr;
for (const auto& child : axRenderImage->children()) {
if (!is<AccessibilityImageMapLink>(*child))
continue;
if (downcast<AccessibilityImageMapLink>(*child).areaElement() == areaElement)
return downcast<AccessibilityImageMapLink>(child.get());
}
return nullptr;
}
AccessibilityObject* AXObjectCache::focusedObjectForPage(const Page* page)
{
ASSERT(isMainThread());
if (!gAccessibilityEnabled)
return nullptr;
// get the focused node in the page
Document* document = page->focusController().focusedOrMainFrame().document();
if (!document)
return nullptr;
document->updateStyleIfNeeded();
Element* focusedElement = document->focusedElement();
if (is<HTMLAreaElement>(focusedElement))
return focusedImageMapUIElement(downcast<HTMLAreaElement>(focusedElement));
auto* focus = getOrCreate(focusedElement ? focusedElement : static_cast<Node*>(document));
if (!focus)
return nullptr;
if (focus->shouldFocusActiveDescendant()) {
if (auto* descendant = focus->activeDescendant())
focus = descendant;
}
// the HTML element, for example, is focusable but has an AX object that is ignored
if (focus->accessibilityIsIgnored())
focus = focus->parentObjectUnignored();
return focus;
}
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
void AXObjectCache::setIsolatedTreeFocusedObject(Node* focusedNode)
{
ASSERT(isMainThread());
if (!m_pageID)
return;
auto* focus = getOrCreate(focusedNode);
if (auto tree = AXIsolatedTree::treeForPageID(*m_pageID))
tree->setFocusedNodeID(focus ? focus->objectID() : AXID());
}
#endif
AccessibilityObject* AXObjectCache::get(Widget* widget)
{
if (!widget)
return nullptr;
AXID axID = m_widgetObjectMapping.get(widget);
ASSERT(!axID.isHashTableDeletedValue());
if (!axID)
return nullptr;
return m_objects.get(axID);
}
AccessibilityObject* AXObjectCache::get(RenderObject* renderer)
{
if (!renderer)
return nullptr;
AXID axID = m_renderObjectMapping.get(renderer);
ASSERT(!axID.isHashTableDeletedValue());
if (!axID)
return nullptr;
return m_objects.get(axID);
}
AccessibilityObject* AXObjectCache::get(Node* node)
{
if (!node)
return nullptr;
AXID renderID = node->renderer() ? m_renderObjectMapping.get(node->renderer()) : AXID();
ASSERT(!renderID.isHashTableDeletedValue());
AXID nodeID = m_nodeObjectMapping.get(node);
ASSERT(!nodeID.isHashTableDeletedValue());
if (node->renderer() && nodeID && !renderID) {
// This can happen if an AccessibilityNodeObject is created for a node that's not
// rendered, but later something changes and it gets a renderer (like if it's
// reparented).
remove(nodeID);
return nullptr;
}
if (renderID)
return m_objects.get(renderID);
if (!nodeID)
return nullptr;
return m_objects.get(nodeID);
}
// FIXME: This probably belongs on Node.
bool nodeHasRole(Node* node, StringView role)
{
if (!node || !is<Element>(node))
return false;
auto& roleValue = downcast<Element>(*node).attributeWithoutSynchronization(roleAttr);
if (role.isNull())
return roleValue.isEmpty();
if (roleValue.isEmpty())
return false;
return SpaceSplitString::spaceSplitStringContainsValue(roleValue, role, SpaceSplitString::ShouldFoldCase::Yes);
}
static bool isSimpleImage(const RenderObject& renderer)
{
if (!is<RenderImage>(renderer))
return false;
// Exclude ImageButtons because they are treated as buttons, not as images.
auto* node = renderer.node();
if (is<HTMLInputElement>(node))
return false;
// ImageMaps are not simple images.
if (downcast<RenderImage>(renderer).imageMap()
|| (is<HTMLImageElement>(node) && downcast<HTMLImageElement>(node)->hasAttributeWithoutSynchronization(usemapAttr)))
return false;
#if ENABLE(VIDEO)
// Exclude video and audio elements.
if (is<HTMLMediaElement>(node))
return false;
#endif // ENABLE(VIDEO)
return true;
}
static Ref<AccessibilityObject> createFromRenderer(RenderObject* renderer)
{
// FIXME: How could renderer->node() ever not be an Element?
Node* node = renderer->node();
// If the node is aria role="list" or the aria role is empty and its a
// ul/ol/dl type (it shouldn't be a list if aria says otherwise).
if (node && ((nodeHasRole(node, "list"_s) || nodeHasRole(node, "directory"_s))
|| (nodeHasRole(node, nullAtom()) && (node->hasTagName(ulTag) || node->hasTagName(olTag) || node->hasTagName(dlTag)))))
return AccessibilityList::create(renderer);
// aria tables
if (nodeHasRole(node, "grid"_s) || nodeHasRole(node, "treegrid"_s) || nodeHasRole(node, "table"_s))
return AccessibilityARIAGrid::create(renderer);
if (nodeHasRole(node, "row"_s))
return AccessibilityARIAGridRow::create(renderer);
if (nodeHasRole(node, "gridcell"_s) || nodeHasRole(node, "cell"_s) || nodeHasRole(node, "columnheader"_s) || nodeHasRole(node, "rowheader"_s))
return AccessibilityARIAGridCell::create(renderer);
// aria tree
if (nodeHasRole(node, "tree"_s))
return AccessibilityTree::create(renderer);
if (nodeHasRole(node, "treeitem"_s))
return AccessibilityTreeItem::create(renderer);
if (node && is<HTMLLabelElement>(node) && nodeHasRole(node, nullAtom()))
return AccessibilityLabel::create(renderer);
#if PLATFORM(IOS_FAMILY)
if (is<HTMLMediaElement>(node) && nodeHasRole(node, nullAtom()))
return AccessibilityMediaObject::create(renderer);
#endif
if (renderer->isSVGRootOrLegacySVGRoot())
return AccessibilitySVGRoot::create(renderer);
if (is<SVGElement>(node))
return AccessibilitySVGElement::create(renderer);
if (isSimpleImage(*renderer))
return AXImage::create(downcast<RenderImage>(renderer));
#if ENABLE(MATHML)
// The mfenced element creates anonymous RenderMathMLOperators which should be treated
// as MathML elements and assigned the MathElementRole so that platform logic regarding
// inclusion and role mapping is not bypassed.
bool isAnonymousOperator = renderer->isAnonymous() && is<RenderMathMLOperator>(*renderer);
if (isAnonymousOperator || is<MathMLElement>(node))
return AccessibilityMathMLElement::create(renderer, isAnonymousOperator);
#endif
if (is<RenderBoxModelObject>(*renderer)) {
RenderBoxModelObject& cssBox = downcast<RenderBoxModelObject>(*renderer);
if (is<RenderListBox>(cssBox))
return AccessibilityListBox::create(&downcast<RenderListBox>(cssBox));
if (is<RenderMenuList>(cssBox))
return AccessibilityMenuList::create(&downcast<RenderMenuList>(cssBox));
// standard tables
if (is<RenderTable>(cssBox))
return AccessibilityTable::create(&downcast<RenderTable>(cssBox));
if (is<RenderTableRow>(cssBox))
return AccessibilityTableRow::create(&downcast<RenderTableRow>(cssBox));
if (is<RenderTableCell>(cssBox))
return AccessibilityTableCell::create(&downcast<RenderTableCell>(cssBox));
// progress bar
if (is<RenderProgress>(cssBox))
return AccessibilityProgressIndicator::create(&downcast<RenderProgress>(cssBox));
#if ENABLE(ATTACHMENT_ELEMENT)
if (is<RenderAttachment>(cssBox))
return AccessibilityAttachment::create(&downcast<RenderAttachment>(cssBox));
#endif
if (is<RenderMeter>(cssBox))
return AccessibilityProgressIndicator::create(&downcast<RenderMeter>(cssBox));
// input type=range
if (is<RenderSlider>(cssBox))
return AccessibilitySlider::create(&downcast<RenderSlider>(cssBox));
}
return AccessibilityRenderObject::create(renderer);
}
static Ref<AccessibilityObject> createFromNode(Node* node)
{
return AccessibilityNodeObject::create(node);
}
void AXObjectCache::cacheAndInitializeWrapper(AccessibilityObject* newObject, DOMObjectVariant domObject)
{
ASSERT(newObject);
AXID axID = getAXID(newObject);
ASSERT(axID.isValid());
WTF::switchOn(domObject,
[&axID, this] (RenderObject* typedValue) { m_renderObjectMapping.set(typedValue, axID); },
[&axID, this] (Node* typedValue) { m_nodeObjectMapping.set(typedValue, axID); },
[&axID, this] (Widget* typedValue) { m_widgetObjectMapping.set(typedValue, axID); },
[] (auto&) { }
);
m_objects.set(axID, newObject);
newObject->init();
attachWrapper(newObject);
}
AccessibilityObject* AXObjectCache::getOrCreate(Widget* widget)
{
if (!widget)
return nullptr;
if (AccessibilityObject* obj = get(widget))
return obj;
RefPtr<AccessibilityObject> newObj;
if (is<ScrollView>(*widget))
newObj = AccessibilityScrollView::create(downcast<ScrollView>(widget));
else if (is<Scrollbar>(*widget))
newObj = AccessibilityScrollbar::create(downcast<Scrollbar>(widget));
// Will crash later if we have two objects for the same widget.
ASSERT(!get(widget));
// Ensure we weren't given an unsupported widget type.
ASSERT(newObj);
if (!newObj)
return nullptr;
cacheAndInitializeWrapper(newObj.get(), widget);
return newObj.get();
}
AccessibilityObject* AXObjectCache::getOrCreate(Node* node)
{
if (!node)
return nullptr;
if (AccessibilityObject* obj = get(node))
return obj;
if (node->renderer())
return getOrCreate(node->renderer());
if (!node->parentElement())
return nullptr;
bool isOptionElement = is<HTMLOptionElement>(*node);
if (isOptionElement || is<HTMLOptGroupElement>(*node)) {
auto select = isOptionElement
? downcast<HTMLOptionElement>(*node).ownerSelectElement()
: downcast<HTMLOptGroupElement>(*node).ownerSelectElement();
if (!select)
return nullptr;
RefPtr<AccessibilityObject> object;
if (select->usesMenuList()) {
if (!isOptionElement)
return nullptr;
object = AccessibilityMenuListOption::create(downcast<HTMLOptionElement>(*node));
} else
object = AccessibilityListBoxOption::create(downcast<HTMLElement>(*node));
cacheAndInitializeWrapper(object.get(), node);
return object.get();
}
bool inCanvasSubtree = lineageOfType<HTMLCanvasElement>(*node->parentElement()).first();
bool insideMeterElement = is<HTMLMeterElement>(*node->parentElement());
bool hasDisplayContents = is<Element>(*node) && downcast<Element>(*node).hasDisplayContents();
if (!inCanvasSubtree && !insideMeterElement && !hasDisplayContents && !isNodeAriaVisible(node))
return nullptr;
Ref protectedNode { *node };
// Fallback content is only focusable as long as the canvas is displayed and visible.
// Update the style before Element::isFocusable() gets called.
if (inCanvasSubtree)
node->document().updateStyleIfNeeded();
RefPtr<AccessibilityObject> newObj = createFromNode(node);
// Will crash later if we have two objects for the same node.
ASSERT(!get(node));
cacheAndInitializeWrapper(newObj.get(), node);
newObj->setLastKnownIsIgnoredValue(newObj->accessibilityIsIgnored());
// Sometimes asking accessibilityIsIgnored() will cause the newObject to be deallocated, and then
// it will disappear when this function is finished, leading to a use-after-free.
if (newObj->isDetached())
return nullptr;
return newObj.get();
}
AccessibilityObject* AXObjectCache::getOrCreate(RenderObject* renderer)
{
if (!renderer)
return nullptr;
if (AccessibilityObject* obj = get(renderer))
return obj;
// Don't create an object for this renderer if it's being destroyed.
if (renderer->beingDestroyed())
return nullptr;
RefPtr<AccessibilityObject> newObj = createFromRenderer(renderer);
// Will crash later if we have two objects for the same renderer.
ASSERT(!get(renderer));
cacheAndInitializeWrapper(newObj.get(), renderer);
newObj->setLastKnownIsIgnoredValue(newObj->accessibilityIsIgnored());
// Sometimes asking accessibilityIsIgnored() will cause the newObject to be deallocated, and then
// it will disappear when this function is finished, leading to a use-after-free.
if (newObj->isDetached())
return nullptr;
return newObj.get();
}
AXCoreObject* AXObjectCache::rootObject()
{
if (!gAccessibilityEnabled)
return nullptr;
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
if (isIsolatedTreeEnabled())
return isolatedTreeRootObject();
#endif
return getOrCreate(m_document.view());
}
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
RefPtr<AXIsolatedTree> AXObjectCache::getOrCreateIsolatedTree() const
{
if (!m_pageID)
return nullptr;
auto tree = AXIsolatedTree::treeForPageID(*m_pageID);
if (!tree) {
tree = Accessibility::retrieveValueFromMainThread<RefPtr<AXIsolatedTree>>([this] () -> RefPtr<AXIsolatedTree> {
return AXIsolatedTree::create(const_cast<AXObjectCache*>(this));
});
AXObjectCache::initializeSecondaryAXThread();
}
return tree;
}
AXCoreObject* AXObjectCache::isolatedTreeRootObject()
{
if (auto tree = getOrCreateIsolatedTree())
return tree->rootNode().get();
// Should not get here, couldn't create the IsolatedTree.
ASSERT_NOT_REACHED();
return nullptr;
}
#endif
AccessibilityObject* AXObjectCache::rootObjectForFrame(Frame* frame)
{
if (!gAccessibilityEnabled)
return nullptr;
if (!frame)
return nullptr;
return getOrCreate(frame->view());
}
AccessibilityObject* AXObjectCache::create(AccessibilityRole role)
{
RefPtr<AccessibilityObject> obj;
// will be filled in...
switch (role) {
case AccessibilityRole::ImageMapLink:
obj = AccessibilityImageMapLink::create();
break;
case AccessibilityRole::Column:
obj = AccessibilityTableColumn::create();
break;
case AccessibilityRole::TableHeaderContainer:
obj = AccessibilityTableHeaderContainer::create();
break;
case AccessibilityRole::SliderThumb:
obj = AccessibilitySliderThumb::create();
break;
case AccessibilityRole::MenuListPopup:
obj = AccessibilityMenuListPopup::create();
break;
case AccessibilityRole::SpinButton:
obj = AccessibilitySpinButton::create();
break;
case AccessibilityRole::SpinButtonPart:
obj = AccessibilitySpinButtonPart::create();
break;
default:
obj = nullptr;
}
if (!obj)
return nullptr;
cacheAndInitializeWrapper(obj.get());
return obj.get();
}
void AXObjectCache::remove(AXID axID)
{
AXTRACE(makeString("AXObjectCache::remove 0x"_s, hex(reinterpret_cast<uintptr_t>(this))));
AXLOG(makeString("AXID ", axID.loggingString()));
if (!axID)
return;
auto object = m_objects.take(axID);
if (!object)
return;
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
if (m_pageID) {
if (auto tree = AXIsolatedTree::treeForPageID(*m_pageID))
tree->removeNode(*object);
}
#endif
object->detach(AccessibilityDetachmentType::ElementDestroyed);
m_idsInUse.remove(axID);
ASSERT(m_objects.size() >= m_idsInUse.size());
}
void AXObjectCache::remove(RenderObject* renderer)
{
if (!renderer)
return;
remove(m_renderObjectMapping.take(renderer));
}
void AXObjectCache::remove(Node& node)
{
AXTRACE(makeString("AXObjectCache::remove 0x"_s, hex(reinterpret_cast<uintptr_t>(this))));
if (is<Element>(node)) {
m_deferredTextFormControlValue.remove(downcast<Element>(&node));
m_deferredAttributeChange.removeAllMatching([&node] (const auto& entry) {
return entry.first == &node;
});
m_modalElementsSet.remove(downcast<Element>(&node));
m_deferredRecomputeIsIgnoredList.remove(downcast<Element>(node));
m_deferredRecomputeTableIsExposedList.remove(downcast<Element>(node));
m_deferredSelectedChildredChangedList.remove(downcast<Element>(node));
m_deferredModalChangedList.remove(downcast<Element>(node));
m_deferredMenuListChange.remove(downcast<Element>(node));
}
m_deferredNodeAddedOrRemovedList.remove(&node);
m_deferredTextChangedList.remove(&node);
// Remove the entry if the new focused node is being removed.
m_deferredFocusedNodeChange.removeAllMatching([&node](auto& entry) -> bool {
return entry.second == &node;
});
// Set nullptr to the old focused node if it is being removed.
std::for_each(m_deferredFocusedNodeChange.begin(), m_deferredFocusedNodeChange.end(), [&node](auto& entry) {
if (entry.first == &node)
entry.first = nullptr;
});
removeNodeForUse(node);
remove(m_nodeObjectMapping.take(&node));
remove(node.renderer());
}
void AXObjectCache::remove(Widget* view)
{
if (!view)
return;
remove(m_widgetObjectMapping.take(view));
}
#if !PLATFORM(WIN)
AXID AXObjectCache::platformGenerateAXID() const
{
AXID objID;
do {
objID = AXID::generate();
} while (!objID.isValid() || m_idsInUse.contains(objID));
return objID;
}
#endif
Vector<RefPtr<AXCoreObject>> AXObjectCache::objectsForIDs(const Vector<AXID>& axIDs) const
{
ASSERT(isMainThread());
Vector<RefPtr<AXCoreObject>> result;
result.reserveInitialCapacity(axIDs.size());
for (auto& axID : axIDs) {
if (auto* object = objectForID(axID))
result.uncheckedAppend(object);
}
result.shrinkToFit();
return result;
}
AXID AXObjectCache::getAXID(AccessibilityObject* obj)
{
// check for already-assigned ID
AXID objID = obj->objectID();
if (objID) {
ASSERT(m_idsInUse.contains(objID));
return objID;
}
objID = platformGenerateAXID();
m_idsInUse.add(objID);
obj->setObjectID(objID);
return objID;
}
void AXObjectCache::handleTextChanged(AccessibilityObject* object)
{
AXTRACE(makeString("AXObjectCache::handleTextChanged 0x"_s, hex(reinterpret_cast<uintptr_t>(this))));
AXLOG(object);