b423736804dafbf056f8084941b687b6d3371480
[WebKit-https.git] / Source / WebCore / accessibility / mac / WebAccessibilityObjectWrapperBase.mm
1 /*
2  * Copyright (C) 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import "config.h"
30 #import "WebAccessibilityObjectWrapperBase.h"
31
32 #if ENABLE(ACCESSIBILITY)
33
34 #import "AXObjectCache.h"
35 #import "AccessibilityARIAGridRow.h"
36 #import "AccessibilityList.h"
37 #import "AccessibilityListBox.h"
38 #import "AccessibilityObjectInterface.h"
39 #import "AccessibilityRenderObject.h"
40 #import "AccessibilityScrollView.h"
41 #import "AccessibilitySpinButton.h"
42 #import "AccessibilityTable.h"
43 #import "AccessibilityTableCell.h"
44 #import "AccessibilityTableColumn.h"
45 #import "AccessibilityTableRow.h"
46 #import "Chrome.h"
47 #import "ChromeClient.h"
48 #import "ColorMac.h"
49 #import "ContextMenuController.h"
50 #import "Editing.h"
51 #import "Font.h"
52 #import "FontCascade.h"
53 #import "Frame.h"
54 #import "FrameLoaderClient.h"
55 #import "FrameSelection.h"
56 #import "HTMLNames.h"
57 #import "LayoutRect.h"
58 #import "LocalizedStrings.h"
59 #import "Page.h"
60 #import "RenderTextControl.h"
61 #import "RenderView.h"
62 #import "RenderWidget.h"
63 #import "ScrollView.h"
64 #import "TextCheckerClient.h"
65 #import "TextCheckingHelper.h"
66 #import "VisibleUnits.h"
67 #import "WebCoreFrameView.h"
68
69 #if PLATFORM(MAC)
70 #import <pal/spi/mac/HIServicesSPI.h>
71 #else
72 #import "WAKView.h"
73 #import "WAKWindow.h"
74 #endif
75
76 using namespace WebCore;
77 using namespace HTMLNames;
78
79 // Search Keys
80 #ifndef NSAccessibilityAnyTypeSearchKey
81 #define NSAccessibilityAnyTypeSearchKey @"AXAnyTypeSearchKey"
82 #endif
83
84 #ifndef NSAccessibilityArticleSearchKey
85 #define NSAccessibilityArticleSearchKey @"AXArticleSearchKey"
86 #endif
87
88 #ifndef NSAccessibilityBlockquoteSameLevelSearchKey
89 #define NSAccessibilityBlockquoteSameLevelSearchKey @"AXBlockquoteSameLevelSearchKey"
90 #endif
91
92 #ifndef NSAccessibilityBlockquoteSearchKey
93 #define NSAccessibilityBlockquoteSearchKey @"AXBlockquoteSearchKey"
94 #endif
95
96 #ifndef NSAccessibilityBoldFontSearchKey
97 #define NSAccessibilityBoldFontSearchKey @"AXBoldFontSearchKey"
98 #endif
99
100 #ifndef NSAccessibilityButtonSearchKey
101 #define NSAccessibilityButtonSearchKey @"AXButtonSearchKey"
102 #endif
103
104 #ifndef NSAccessibilityCheckBoxSearchKey
105 #define NSAccessibilityCheckBoxSearchKey @"AXCheckBoxSearchKey"
106 #endif
107
108 #ifndef NSAccessibilityControlSearchKey
109 #define NSAccessibilityControlSearchKey @"AXControlSearchKey"
110 #endif
111
112 #ifndef NSAccessibilityDifferentTypeSearchKey
113 #define NSAccessibilityDifferentTypeSearchKey @"AXDifferentTypeSearchKey"
114 #endif
115
116 #ifndef NSAccessibilityFontChangeSearchKey
117 #define NSAccessibilityFontChangeSearchKey @"AXFontChangeSearchKey"
118 #endif
119
120 #ifndef NSAccessibilityFontColorChangeSearchKey
121 #define NSAccessibilityFontColorChangeSearchKey @"AXFontColorChangeSearchKey"
122 #endif
123
124 #ifndef NSAccessibilityFrameSearchKey
125 #define NSAccessibilityFrameSearchKey @"AXFrameSearchKey"
126 #endif
127
128 #ifndef NSAccessibilityGraphicSearchKey
129 #define NSAccessibilityGraphicSearchKey @"AXGraphicSearchKey"
130 #endif
131
132 #ifndef NSAccessibilityHeadingLevel1SearchKey
133 #define NSAccessibilityHeadingLevel1SearchKey @"AXHeadingLevel1SearchKey"
134 #endif
135
136 #ifndef NSAccessibilityHeadingLevel2SearchKey
137 #define NSAccessibilityHeadingLevel2SearchKey @"AXHeadingLevel2SearchKey"
138 #endif
139
140 #ifndef NSAccessibilityHeadingLevel3SearchKey
141 #define NSAccessibilityHeadingLevel3SearchKey @"AXHeadingLevel3SearchKey"
142 #endif
143
144 #ifndef NSAccessibilityHeadingLevel4SearchKey
145 #define NSAccessibilityHeadingLevel4SearchKey @"AXHeadingLevel4SearchKey"
146 #endif
147
148 #ifndef NSAccessibilityHeadingLevel5SearchKey
149 #define NSAccessibilityHeadingLevel5SearchKey @"AXHeadingLevel5SearchKey"
150 #endif
151
152 #ifndef NSAccessibilityHeadingLevel6SearchKey
153 #define NSAccessibilityHeadingLevel6SearchKey @"AXHeadingLevel6SearchKey"
154 #endif
155
156 #ifndef NSAccessibilityHeadingSameLevelSearchKey
157 #define NSAccessibilityHeadingSameLevelSearchKey @"AXHeadingSameLevelSearchKey"
158 #endif
159
160 #ifndef NSAccessibilityHeadingSearchKey
161 #define NSAccessibilityHeadingSearchKey @"AXHeadingSearchKey"
162 #endif
163
164 #ifndef NSAccessibilityHighlightedSearchKey
165 #define NSAccessibilityHighlightedSearchKey @"AXHighlightedSearchKey"
166 #endif
167
168 #ifndef NSAccessibilityKeyboardFocusableSearchKey
169 #define NSAccessibilityKeyboardFocusableSearchKey @"AXKeyboardFocusableSearchKey"
170 #endif
171
172 #ifndef NSAccessibilityItalicFontSearchKey
173 #define NSAccessibilityItalicFontSearchKey @"AXItalicFontSearchKey"
174 #endif
175
176 #ifndef NSAccessibilityLandmarkSearchKey
177 #define NSAccessibilityLandmarkSearchKey @"AXLandmarkSearchKey"
178 #endif
179
180 #ifndef NSAccessibilityLinkSearchKey
181 #define NSAccessibilityLinkSearchKey @"AXLinkSearchKey"
182 #endif
183
184 #ifndef NSAccessibilityListSearchKey
185 #define NSAccessibilityListSearchKey @"AXListSearchKey"
186 #endif
187
188 #ifndef NSAccessibilityLiveRegionSearchKey
189 #define NSAccessibilityLiveRegionSearchKey @"AXLiveRegionSearchKey"
190 #endif
191
192 #ifndef NSAccessibilityMisspelledWordSearchKey
193 #define NSAccessibilityMisspelledWordSearchKey @"AXMisspelledWordSearchKey"
194 #endif
195
196 #ifndef NSAccessibilityOutlineSearchKey
197 #define NSAccessibilityOutlineSearchKey @"AXOutlineSearchKey"
198 #endif
199
200 #ifndef NSAccessibilityPlainTextSearchKey
201 #define NSAccessibilityPlainTextSearchKey @"AXPlainTextSearchKey"
202 #endif
203
204 #ifndef NSAccessibilityRadioGroupSearchKey
205 #define NSAccessibilityRadioGroupSearchKey @"AXRadioGroupSearchKey"
206 #endif
207
208 #ifndef NSAccessibilitySameTypeSearchKey
209 #define NSAccessibilitySameTypeSearchKey @"AXSameTypeSearchKey"
210 #endif
211
212 #ifndef NSAccessibilityStaticTextSearchKey
213 #define NSAccessibilityStaticTextSearchKey @"AXStaticTextSearchKey"
214 #endif
215
216 #ifndef NSAccessibilityStyleChangeSearchKey
217 #define NSAccessibilityStyleChangeSearchKey @"AXStyleChangeSearchKey"
218 #endif
219
220 #ifndef NSAccessibilityTableSameLevelSearchKey
221 #define NSAccessibilityTableSameLevelSearchKey @"AXTableSameLevelSearchKey"
222 #endif
223
224 #ifndef NSAccessibilityTableSearchKey
225 #define NSAccessibilityTableSearchKey @"AXTableSearchKey"
226 #endif
227
228 #ifndef NSAccessibilityTextFieldSearchKey
229 #define NSAccessibilityTextFieldSearchKey @"AXTextFieldSearchKey"
230 #endif
231
232 #ifndef NSAccessibilityUnderlineSearchKey
233 #define NSAccessibilityUnderlineSearchKey @"AXUnderlineSearchKey"
234 #endif
235
236 #ifndef NSAccessibilityUnvisitedLinkSearchKey
237 #define NSAccessibilityUnvisitedLinkSearchKey @"AXUnvisitedLinkSearchKey"
238 #endif
239
240 #ifndef NSAccessibilityVisitedLinkSearchKey
241 #define NSAccessibilityVisitedLinkSearchKey @"AXVisitedLinkSearchKey"
242 #endif
243
244 // Search
245 #ifndef NSAccessibilityImmediateDescendantsOnly
246 #define NSAccessibilityImmediateDescendantsOnly @"AXImmediateDescendantsOnly"
247 #endif
248
249 static NSArray *convertMathPairsToNSArray(const AccessibilityObject::AccessibilityMathMultiscriptPairs& pairs, NSString *subscriptKey, NSString *superscriptKey)
250 {
251     NSMutableArray *array = [NSMutableArray arrayWithCapacity:pairs.size()];
252     for (const auto& pair : pairs) {
253         NSMutableDictionary *pairDictionary = [NSMutableDictionary dictionary];
254         if (pair.first && pair.first->wrapper() && !pair.first->accessibilityIsIgnored())
255             [pairDictionary setObject:pair.first->wrapper() forKey:subscriptKey];
256         if (pair.second && pair.second->wrapper() && !pair.second->accessibilityIsIgnored())
257             [pairDictionary setObject:pair.second->wrapper() forKey:superscriptKey];
258         [array addObject:pairDictionary];
259     }
260     return array;
261 }
262
263 static void addChildToArray(AXCoreObject& child, RetainPtr<NSMutableArray> array)
264 {
265     WebAccessibilityObjectWrapper *wrapper = child.wrapper();
266     // We want to return the attachment view instead of the object representing the attachment,
267     // otherwise, we get palindrome errors in the AX hierarchy.
268     if (child.isAttachment() && [wrapper attachmentView])
269         [array.get() addObject:[wrapper attachmentView]];
270     else if (wrapper)
271         [array.get() addObject:wrapper];
272 }
273
274 NSArray *convertToNSArray(const WebCore::AXCoreObject::AccessibilityChildrenVector& children)
275 {
276     NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:children.size()];
277     for (const auto& child : children)
278         addChildToArray(*child, result);
279     return [result autorelease];
280 }
281
282 @implementation WebAccessibilityObjectWrapperBase
283
284 @synthesize identifier=_identifier;
285
286 - (id)initWithAccessibilityObject:(AXCoreObject*)axObject
287 {
288     if (!(self = [super init]))
289         return nil;
290
291     [self attachAXObject:axObject];
292     return self;
293 }
294
295 - (void)attachAXObject:(AXCoreObject*)axObject
296 {
297     ASSERT(axObject && (_identifier == InvalidAXID || _identifier == axObject->objectID()));
298     m_axObject = axObject;
299     if (_identifier == InvalidAXID)
300         _identifier = m_axObject->objectID();
301 }
302
303 #if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
304 - (void)attachIsolatedObject:(AXCoreObject*)isolatedObject
305 {
306     ASSERT(isolatedObject && (_identifier == InvalidAXID || _identifier == isolatedObject->objectID()));
307     m_isolatedObject = isolatedObject;
308     if (_identifier == InvalidAXID)
309         _identifier = m_isolatedObject->objectID();
310 }
311 #endif
312
313 - (void)detach
314 {
315     m_axObject = nullptr;
316 #if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
317     m_isolatedObject = nullptr;
318 #endif
319     _identifier = InvalidAXID;
320 }
321
322 - (BOOL)updateObjectBackingStore
323 {
324     // Calling updateBackingStore() can invalidate this element so self must be retained.
325     // If it does become invalidated, self.axBackingObject will be nil.
326     CFRetain((__bridge CFTypeRef)self);
327     CFAutorelease((__bridge CFTypeRef)self);
328
329     if (!self.axBackingObject)
330         return NO;
331     
332     self.axBackingObject->updateBackingStore();
333     if (!self.axBackingObject)
334         return NO;
335     
336     return YES;
337 }
338
339 - (id)attachmentView
340 {
341     return nil;
342 }
343
344 // This should be the "visible" text that's actually on the screen if possible.
345 // If there's alternative text, that can override the title.
346 - (NSString *)baseAccessibilityTitle
347 {
348     return self.axBackingObject->titleAttributeValue();
349 }
350
351 - (WebCore::AXCoreObject*)axBackingObject
352 {
353 #if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
354     if (!isMainThread()) {
355         ASSERT(AXObjectCache::clientSupportsIsolatedTree());
356         return m_isolatedObject;
357     }
358 #endif
359     return m_axObject;
360 }
361
362 - (NSString *)baseAccessibilityDescription
363 {
364     return self.axBackingObject->descriptionAttributeValue();
365 }
366
367 - (NSArray<NSString *> *)baseAccessibilitySpeechHint
368 {
369     return [(NSString *)self.axBackingObject->speechHintAttributeValue() componentsSeparatedByString:@" "];
370 }
371
372 - (NSString *)baseAccessibilityHelpText
373 {
374     return self.axBackingObject->helpTextAttributeValue();
375 }
376
377 struct PathConversionInfo {
378     WebAccessibilityObjectWrapperBase *wrapper;
379     CGMutablePathRef path;
380 };
381
382 static void convertPathToScreenSpaceFunction(PathConversionInfo& conversion, const PathElement& element)
383 {
384     WebAccessibilityObjectWrapperBase *wrapper = conversion.wrapper;
385     CGMutablePathRef newPath = conversion.path;
386     FloatRect rect;
387     switch (element.type) {
388     case PathElement::Type::MoveToPoint:
389     {
390         rect = FloatRect(element.points[0], FloatSize());
391         CGPoint newPoint = [wrapper convertRectToSpace:rect space:AccessibilityConversionSpace::Screen].origin;
392         CGPathMoveToPoint(newPath, nil, newPoint.x, newPoint.y);
393         break;
394     }
395     case PathElement::Type::AddLineToPoint:
396     {
397         rect = FloatRect(element.points[0], FloatSize());
398         CGPoint newPoint = [wrapper convertRectToSpace:rect space:AccessibilityConversionSpace::Screen].origin;
399         CGPathAddLineToPoint(newPath, nil, newPoint.x, newPoint.y);
400         break;
401     }
402     case PathElement::Type::AddQuadCurveToPoint:
403     {
404         rect = FloatRect(element.points[0], FloatSize());
405         CGPoint newPoint1 = [wrapper convertRectToSpace:rect space:AccessibilityConversionSpace::Screen].origin;
406
407         rect = FloatRect(element.points[1], FloatSize());
408         CGPoint newPoint2 = [wrapper convertRectToSpace:rect space:AccessibilityConversionSpace::Screen].origin;
409         CGPathAddQuadCurveToPoint(newPath, nil, newPoint1.x, newPoint1.y, newPoint2.x, newPoint2.y);
410         break;
411     }
412     case PathElement::Type::AddCurveToPoint:
413     {
414         rect = FloatRect(element.points[0], FloatSize());
415         CGPoint newPoint1 = [wrapper convertRectToSpace:rect space:AccessibilityConversionSpace::Screen].origin;
416
417         rect = FloatRect(element.points[1], FloatSize());
418         CGPoint newPoint2 = [wrapper convertRectToSpace:rect space:AccessibilityConversionSpace::Screen].origin;
419
420         rect = FloatRect(element.points[2], FloatSize());
421         CGPoint newPoint3 = [wrapper convertRectToSpace:rect space:AccessibilityConversionSpace::Screen].origin;
422         CGPathAddCurveToPoint(newPath, nil, newPoint1.x, newPoint1.y, newPoint2.x, newPoint2.y, newPoint3.x, newPoint3.y);
423         break;
424     }
425     case PathElement::Type::CloseSubpath:
426     {
427         CGPathCloseSubpath(newPath);
428         break;
429     }
430     }
431 }
432
433 - (CGPathRef)convertPathToScreenSpace:(Path &)path
434 {
435     PathConversionInfo conversion = { self, CGPathCreateMutable() };
436     path.apply([&conversion](const PathElement& pathElement) {
437         convertPathToScreenSpaceFunction(conversion, pathElement);
438     });
439     CFAutorelease(conversion.path);
440     return conversion.path;
441 }
442
443 - (id)_accessibilityWebDocumentView
444 {
445     ASSERT_NOT_REACHED();
446     // Overridden by sub-classes
447     return nil;
448 }
449
450 - (CGRect)convertRectToSpace:(WebCore::FloatRect &)rect space:(AccessibilityConversionSpace)space
451 {
452     if (!self.axBackingObject)
453         return CGRectZero;
454     
455     CGSize size = CGSizeMake(rect.size().width(), rect.size().height());
456     CGPoint point = CGPointMake(rect.x(), rect.y());
457     
458     CGRect cgRect = CGRectMake(point.x, point.y, size.width, size.height);
459
460     // WebKit1 code path... platformWidget() exists.
461     FrameView* frameView = self.axBackingObject->documentFrameView();
462 #if PLATFORM(IOS_FAMILY)
463     WAKView* documentView = frameView ? frameView->documentView() : nullptr;
464     if (documentView) {
465         cgRect = [documentView convertRect:cgRect toView:nil];
466         
467         // we need the web document view to give us our final screen coordinates
468         // because that can take account of the scroller
469         id webDocument = [self _accessibilityWebDocumentView];
470         if (webDocument)
471             cgRect = [webDocument convertRect:cgRect toView:nil];
472         return cgRect;
473     }
474 #else
475     if (frameView && frameView->platformWidget()) {
476         NSRect nsRect = NSRectFromCGRect(cgRect);
477         NSView* view = frameView->documentView();
478         ALLOW_DEPRECATED_DECLARATIONS_BEGIN
479         nsRect = [[view window] convertRectToScreen:[view convertRect:nsRect toView:nil]];
480         ALLOW_DEPRECATED_DECLARATIONS_END
481         return NSRectToCGRect(nsRect);
482     }
483 #endif
484     else
485         return static_cast<CGRect>(self.axBackingObject->convertFrameToSpace(rect, space));
486 }
487
488 - (NSString *)ariaLandmarkRoleDescription
489 {
490     return self.axBackingObject->ariaLandmarkRoleDescription();
491 }
492
493 - (void)baseAccessibilitySetFocus:(BOOL)focus
494 {
495     // If focus is just set without making the view the first responder, then keyboard focus won't move to the right place.
496     if (focus && !self.axBackingObject->document()->frame()->selection().isFocusedAndActive()) {
497         FrameView* frameView = self.axBackingObject->documentFrameView();
498         Page* page = self.axBackingObject->page();
499         if (page && frameView) {
500             ChromeClient& chromeClient = page->chrome().client();
501             chromeClient.focus();
502
503             // Legacy WebKit1 case.
504             if (frameView->platformWidget())
505                 chromeClient.makeFirstResponder(frameView->platformWidget());
506             else
507                 chromeClient.assistiveTechnologyMakeFirstResponder();
508         }
509     }
510
511     self.axBackingObject->setFocused(focus);
512 }
513
514 - (NSString *)accessibilityPlatformMathSubscriptKey
515 {
516     ASSERT_NOT_REACHED();
517     return nil;
518 }
519
520 - (NSString *)accessibilityPlatformMathSuperscriptKey
521 {
522     ASSERT_NOT_REACHED();
523     return nil;    
524 }
525
526 - (NSArray *)accessibilityMathPostscriptPairs
527 {
528     AccessibilityObject::AccessibilityMathMultiscriptPairs pairs;
529     self.axBackingObject->mathPostscripts(pairs);
530     return convertMathPairsToNSArray(pairs, [self accessibilityPlatformMathSubscriptKey], [self accessibilityPlatformMathSuperscriptKey]);
531 }
532
533 - (NSArray *)accessibilityMathPrescriptPairs
534 {
535     AccessibilityObject::AccessibilityMathMultiscriptPairs pairs;
536     self.axBackingObject->mathPrescripts(pairs);
537     return convertMathPairsToNSArray(pairs, [self accessibilityPlatformMathSubscriptKey], [self accessibilityPlatformMathSuperscriptKey]);
538 }
539
540 // This is set by DRT when it wants to listen for notifications.
541 static BOOL accessibilityShouldRepostNotifications;
542 + (void)accessibilitySetShouldRepostNotifications:(BOOL)repost
543 {
544     accessibilityShouldRepostNotifications = repost;
545 #if PLATFORM(MAC)
546     AXObjectCache::setShouldRepostNotificationsForTests(repost);
547 #endif
548 }
549
550 - (void)accessibilityPostedNotification:(NSString *)notificationName
551 {
552     if (accessibilityShouldRepostNotifications)
553         [self accessibilityPostedNotification:notificationName userInfo:nil];
554 }
555
556 static bool isValueTypeSupported(id value)
557 {
558     return [value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[WebAccessibilityObjectWrapperBase class]];
559 }
560
561 static NSArray *arrayRemovingNonSupportedTypes(NSArray *array)
562 {
563     ASSERT([array isKindOfClass:[NSArray class]]);
564     NSMutableArray *mutableArray = [array mutableCopy];
565     for (NSUInteger i = 0; i < [mutableArray count];) {
566         id value = [mutableArray objectAtIndex:i];
567         if ([value isKindOfClass:[NSDictionary class]])
568             [mutableArray replaceObjectAtIndex:i withObject:dictionaryRemovingNonSupportedTypes(value)];
569         else if ([value isKindOfClass:[NSArray class]])
570             [mutableArray replaceObjectAtIndex:i withObject:arrayRemovingNonSupportedTypes(value)];
571         else if (!isValueTypeSupported(value)) {
572             [mutableArray removeObjectAtIndex:i];
573             continue;
574         }
575         i++;
576     }
577     return [mutableArray autorelease];
578 }
579
580 static NSDictionary *dictionaryRemovingNonSupportedTypes(NSDictionary *dictionary)
581 {
582     if (!dictionary)
583         return nil;
584     ASSERT([dictionary isKindOfClass:[NSDictionary class]]);
585     NSMutableDictionary *mutableDictionary = [dictionary mutableCopy];
586     for (NSString *key in dictionary) {
587         id value = [dictionary objectForKey:key];
588         if ([value isKindOfClass:[NSDictionary class]])
589             [mutableDictionary setObject:dictionaryRemovingNonSupportedTypes(value) forKey:key];
590         else if ([value isKindOfClass:[NSArray class]])
591             [mutableDictionary setObject:arrayRemovingNonSupportedTypes(value) forKey:key];
592         else if (!isValueTypeSupported(value))
593             [mutableDictionary removeObjectForKey:key];
594     }
595     return [mutableDictionary autorelease];
596 }
597
598 - (void)accessibilityPostedNotification:(NSString *)notificationName userInfo:(NSDictionary *)userInfo
599 {
600     if (accessibilityShouldRepostNotifications) {
601         ASSERT(notificationName);
602         userInfo = dictionaryRemovingNonSupportedTypes(userInfo);
603         NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:notificationName, @"notificationName", userInfo, @"userInfo", nil];
604         [[NSNotificationCenter defaultCenter] postNotificationName:@"AXDRTNotification" object:self userInfo:info];
605     }
606 }
607
608 #pragma mark Search helpers
609
610 typedef HashMap<String, AccessibilitySearchKey> AccessibilitySearchKeyMap;
611
612 struct SearchKeyEntry {
613     String key;
614     AccessibilitySearchKey value;
615 };
616
617 static AccessibilitySearchKeyMap* createAccessibilitySearchKeyMap()
618 {
619     const SearchKeyEntry searchKeys[] = {
620         { NSAccessibilityAnyTypeSearchKey, AccessibilitySearchKey::AnyType },
621         { NSAccessibilityArticleSearchKey, AccessibilitySearchKey::Article },
622         { NSAccessibilityBlockquoteSameLevelSearchKey, AccessibilitySearchKey::BlockquoteSameLevel },
623         { NSAccessibilityBlockquoteSearchKey, AccessibilitySearchKey::Blockquote },
624         { NSAccessibilityBoldFontSearchKey, AccessibilitySearchKey::BoldFont },
625         { NSAccessibilityButtonSearchKey, AccessibilitySearchKey::Button },
626         { NSAccessibilityCheckBoxSearchKey, AccessibilitySearchKey::CheckBox },
627         { NSAccessibilityControlSearchKey, AccessibilitySearchKey::Control },
628         { NSAccessibilityDifferentTypeSearchKey, AccessibilitySearchKey::DifferentType },
629         { NSAccessibilityFontChangeSearchKey, AccessibilitySearchKey::FontChange },
630         { NSAccessibilityFontColorChangeSearchKey, AccessibilitySearchKey::FontColorChange },
631         { NSAccessibilityFrameSearchKey, AccessibilitySearchKey::Frame },
632         { NSAccessibilityGraphicSearchKey, AccessibilitySearchKey::Graphic },
633         { NSAccessibilityHeadingLevel1SearchKey, AccessibilitySearchKey::HeadingLevel1 },
634         { NSAccessibilityHeadingLevel2SearchKey, AccessibilitySearchKey::HeadingLevel2 },
635         { NSAccessibilityHeadingLevel3SearchKey, AccessibilitySearchKey::HeadingLevel3 },
636         { NSAccessibilityHeadingLevel4SearchKey, AccessibilitySearchKey::HeadingLevel4 },
637         { NSAccessibilityHeadingLevel5SearchKey, AccessibilitySearchKey::HeadingLevel5 },
638         { NSAccessibilityHeadingLevel6SearchKey, AccessibilitySearchKey::HeadingLevel6 },
639         { NSAccessibilityHeadingSameLevelSearchKey, AccessibilitySearchKey::HeadingSameLevel },
640         { NSAccessibilityHeadingSearchKey, AccessibilitySearchKey::Heading },
641         { NSAccessibilityHighlightedSearchKey, AccessibilitySearchKey::Highlighted },
642         { NSAccessibilityKeyboardFocusableSearchKey, AccessibilitySearchKey::KeyboardFocusable },
643         { NSAccessibilityItalicFontSearchKey, AccessibilitySearchKey::ItalicFont },
644         { NSAccessibilityLandmarkSearchKey, AccessibilitySearchKey::Landmark },
645         { NSAccessibilityLinkSearchKey, AccessibilitySearchKey::Link },
646         { NSAccessibilityListSearchKey, AccessibilitySearchKey::List },
647         { NSAccessibilityLiveRegionSearchKey, AccessibilitySearchKey::LiveRegion },
648         { NSAccessibilityMisspelledWordSearchKey, AccessibilitySearchKey::MisspelledWord },
649         { NSAccessibilityOutlineSearchKey, AccessibilitySearchKey::Outline },
650         { NSAccessibilityPlainTextSearchKey, AccessibilitySearchKey::PlainText },
651         { NSAccessibilityRadioGroupSearchKey, AccessibilitySearchKey::RadioGroup },
652         { NSAccessibilitySameTypeSearchKey, AccessibilitySearchKey::SameType },
653         { NSAccessibilityStaticTextSearchKey, AccessibilitySearchKey::StaticText },
654         { NSAccessibilityStyleChangeSearchKey, AccessibilitySearchKey::StyleChange },
655         { NSAccessibilityTableSameLevelSearchKey, AccessibilitySearchKey::TableSameLevel },
656         { NSAccessibilityTableSearchKey, AccessibilitySearchKey::Table },
657         { NSAccessibilityTextFieldSearchKey, AccessibilitySearchKey::TextField },
658         { NSAccessibilityUnderlineSearchKey, AccessibilitySearchKey::Underline },
659         { NSAccessibilityUnvisitedLinkSearchKey, AccessibilitySearchKey::UnvisitedLink },
660         { NSAccessibilityVisitedLinkSearchKey, AccessibilitySearchKey::VisitedLink }
661     };
662     
663     AccessibilitySearchKeyMap* searchKeyMap = new AccessibilitySearchKeyMap;
664     for (size_t i = 0; i < WTF_ARRAY_LENGTH(searchKeys); i++)
665         searchKeyMap->set(searchKeys[i].key, searchKeys[i].value);
666     
667     return searchKeyMap;
668 }
669
670 static AccessibilitySearchKey accessibilitySearchKeyForString(const String& value)
671 {
672     if (value.isEmpty())
673         return AccessibilitySearchKey::AnyType;
674     
675     static const AccessibilitySearchKeyMap* searchKeyMap = createAccessibilitySearchKeyMap();
676     AccessibilitySearchKey searchKey = searchKeyMap->get(value);    
677     return static_cast<int>(searchKey) ? searchKey : AccessibilitySearchKey::AnyType;
678 }
679
680 AccessibilitySearchCriteria accessibilitySearchCriteriaForSearchPredicateParameterizedAttribute(const NSDictionary *parameterizedAttribute)
681 {
682     NSString *directionParameter = [parameterizedAttribute objectForKey:@"AXDirection"];
683     NSNumber *immediateDescendantsOnlyParameter = [parameterizedAttribute objectForKey:NSAccessibilityImmediateDescendantsOnly];
684     NSNumber *resultsLimitParameter = [parameterizedAttribute objectForKey:@"AXResultsLimit"];
685     NSString *searchTextParameter = [parameterizedAttribute objectForKey:@"AXSearchText"];
686     WebAccessibilityObjectWrapperBase *startElementParameter = [parameterizedAttribute objectForKey:@"AXStartElement"];
687     NSNumber *visibleOnlyParameter = [parameterizedAttribute objectForKey:@"AXVisibleOnly"];
688     id searchKeyParameter = [parameterizedAttribute objectForKey:@"AXSearchKey"];
689     
690     AccessibilitySearchDirection direction = AccessibilitySearchDirection::Next;
691     if ([directionParameter isKindOfClass:[NSString class]])
692         direction = [directionParameter isEqualToString:@"AXDirectionNext"] ? AccessibilitySearchDirection::Next : AccessibilitySearchDirection::Previous;
693     
694     bool immediateDescendantsOnly = false;
695     if ([immediateDescendantsOnlyParameter isKindOfClass:[NSNumber class]])
696         immediateDescendantsOnly = [immediateDescendantsOnlyParameter boolValue];
697     
698     unsigned resultsLimit = 0;
699     if ([resultsLimitParameter isKindOfClass:[NSNumber class]])
700         resultsLimit = [resultsLimitParameter unsignedIntValue];
701     
702     String searchText;
703     if ([searchTextParameter isKindOfClass:[NSString class]])
704         searchText = searchTextParameter;
705     
706     AXCoreObject* startElement = nullptr;
707     if ([startElementParameter isKindOfClass:[WebAccessibilityObjectWrapperBase class]])
708         startElement = [startElementParameter axBackingObject];
709
710     bool visibleOnly = false;
711     if ([visibleOnlyParameter isKindOfClass:[NSNumber class]])
712         visibleOnly = [visibleOnlyParameter boolValue];
713     
714     AccessibilitySearchCriteria criteria = AccessibilitySearchCriteria(startElement, direction, searchText, resultsLimit, visibleOnly, immediateDescendantsOnly);
715     
716     if ([searchKeyParameter isKindOfClass:[NSString class]])
717         criteria.searchKeys.append(accessibilitySearchKeyForString(searchKeyParameter));
718     else if ([searchKeyParameter isKindOfClass:[NSArray class]]) {
719         size_t searchKeyCount = static_cast<size_t>([searchKeyParameter count]);
720         criteria.searchKeys.reserveInitialCapacity(searchKeyCount);
721         for (size_t i = 0; i < searchKeyCount; ++i) {
722             NSString *searchKey = [searchKeyParameter objectAtIndex:i];
723             if ([searchKey isKindOfClass:[NSString class]])
724                 criteria.searchKeys.uncheckedAppend(accessibilitySearchKeyForString(searchKey));
725         }
726     }
727     
728     return criteria;
729 }
730
731 @end
732
733 #endif // ENABLE(ACCESSIBILITY)