WebCore:
[WebKit-https.git] / WebCore / page / mac / AccessibilityObjectWrapper.mm
1 /*
2  * Copyright (C) 2008 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 Computer, 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 "AccessibilityObjectWrapper.h"
31
32 #import "AXObjectCache.h"
33 #import "AccessibilityListBox.h"
34 #import "AccessibilityRenderObject.h"
35 #import "ColorMac.h"
36 #import "Frame.h"
37 #import "HTMLAnchorElement.h"
38 #import "HTMLAreaElement.h"
39 #import "HTMLImageElement.h"
40 #import "HTMLInputElement.h"
41 #import "HTMLTextAreaElement.h"
42 #import "LocalizedStrings.h"
43 #import "RenderTextControl.h"
44 #import "RenderView.h"
45 #import "RenderWidget.h"
46 #import "SelectionController.h"
47 #import "SimpleFontData.h"
48 #import "TextIterator.h"
49 #import "WebCoreFrameView.h"
50 #import "WebCoreObjCExtras.h"
51 #import "WebCoreViewFactory.h"
52 #import "htmlediting.h"
53 #import "visible_units.h"
54
55 using namespace WebCore;
56 using namespace HTMLNames;
57
58 @implementation AccessibilityObjectWrapper
59
60 #ifndef BUILDING_ON_TIGER
61 + (void)initialize
62 {
63     WebCoreObjCFinalizeOnMainThread(self);
64 }
65 #endif
66
67 - (id)initWithAccessibilityObject:(AccessibilityObject*)axObject
68 {
69     [super init];
70
71     m_object = axObject;
72     return self;
73 }
74
75 - (void)unregisterUniqueIdForUIElement
76 {
77     [[WebCoreViewFactory sharedFactory] unregisterUniqueIdForUIElement:self];
78 }
79
80 - (void)detach
81 {
82     // Send unregisterUniqueIdForUIElement unconditionally because if it is
83     // ever accidently not done (via other bugs in our AX implementation) you
84     // end up with a crash like <rdar://problem/4273149>.  It is safe and not
85     // expensive to send even if the object is not registered.
86     [self unregisterUniqueIdForUIElement];
87     m_object = 0;
88 }
89
90 - (AccessibilityObject*)accessibilityObject
91 {
92     return m_object;
93 }
94
95 - (NSView*)attachmentView
96 {
97     ASSERT(m_object->isAttachment());
98     Widget* widget = m_object->widgetForAttachmentView();
99     if (!widget)
100         return nil;
101     return widget->getView();
102 }
103
104 static WebCoreTextMarker* textMarkerForVisiblePosition(const VisiblePosition& visiblePos)
105 {
106     if (visiblePos.isNull())
107         return nil;
108
109     Position deepPos = visiblePos.deepEquivalent();
110     Node* domNode = deepPos.node();
111     ASSERT(domNode);
112     if (!domNode)
113         return nil;
114
115     if (domNode->isHTMLElement())
116         if (static_cast<HTMLElement*>(domNode)->isPasswordField())
117             return nil;
118
119     // locate the renderer, which must exist for a visible dom node
120     RenderObject* renderer = domNode->renderer();
121     ASSERT(renderer);
122
123     // find or create an accessibility object for this renderer
124     AXObjectCache* cache = renderer->document()->axObjectCache();
125     RefPtr<AccessibilityObject> obj = cache->get(renderer);
126
127     // create a text marker, adding an ID for the AccessibilityObject if needed
128     TextMarkerData textMarkerData;
129     textMarkerData.axID = obj.get()->axObjectID();
130     textMarkerData.node = domNode;
131     textMarkerData.offset = deepPos.offset();
132     textMarkerData.affinity = visiblePos.affinity();
133     return [[WebCoreViewFactory sharedFactory] textMarkerWithBytes:&textMarkerData length:sizeof(textMarkerData)];
134 }
135
136 static VisiblePosition visiblePositionForTextMarker(WebCoreTextMarker* textMarker)
137 {
138     TextMarkerData textMarkerData;
139     
140     if (![[WebCoreViewFactory sharedFactory] getBytes:&textMarkerData fromTextMarker:textMarker length:sizeof(textMarkerData)])
141         return VisiblePosition();
142
143     VisiblePosition visiblePos = VisiblePosition(textMarkerData.node, textMarkerData.offset, textMarkerData.affinity);
144     Position deepPos = visiblePos.deepEquivalent();
145     AXObjectCache* cache = deepPos.node()->renderer()->document()->axObjectCache();
146     if (!cache->isIDinUse(textMarkerData.axID))
147         return VisiblePosition();
148
149     if (deepPos.node() != textMarkerData.node || deepPos.offset() != textMarkerData.offset)
150         return VisiblePosition();
151     
152     return visiblePos;
153 }
154
155 static VisiblePosition visiblePositionForStartOfTextMarkerRange(WebCoreTextMarkerRange* textMarkerRange)
156 {
157     return visiblePositionForTextMarker([[WebCoreViewFactory sharedFactory] startOfTextMarkerRange:textMarkerRange]);
158 }
159
160 static VisiblePosition visiblePositionForEndOfTextMarkerRange(WebCoreTextMarkerRange* textMarkerRange)
161 {
162     return visiblePositionForTextMarker([[WebCoreViewFactory sharedFactory] endOfTextMarkerRange:textMarkerRange]);
163 }
164
165 static WebCoreTextMarkerRange* textMarkerRangeFromMarkers(WebCoreTextMarker* textMarker1, WebCoreTextMarker* textMarker2)
166 {
167     return [[WebCoreViewFactory sharedFactory] textMarkerRangeWithStart:textMarker1 end:textMarker2];
168 }
169
170 static void AXAttributeStringSetFont(NSMutableAttributedString* attrString, NSString* attribute, NSFont* font, NSRange range)
171 {
172     NSDictionary* dict;
173     
174     if (font) {
175         dict = [NSDictionary dictionaryWithObjectsAndKeys:
176             [font fontName]                             , NSAccessibilityFontNameKey,
177             [font familyName]                           , NSAccessibilityFontFamilyKey,
178             [font displayName]                          , NSAccessibilityVisibleNameKey,
179             [NSNumber numberWithFloat:[font pointSize]] , NSAccessibilityFontSizeKey,
180         nil];
181
182         [attrString addAttribute:attribute value:dict range:range];
183     } else
184         [attrString removeAttribute:attribute range:range];
185     
186 }
187
188 static CGColorRef CreateCGColorIfDifferent(NSColor* nsColor, CGColorRef existingColor)
189 {
190     // get color information assuming NSDeviceRGBColorSpace 
191     NSColor* rgbColor = [nsColor colorUsingColorSpaceName:NSDeviceRGBColorSpace];
192     if (rgbColor == nil)
193         rgbColor = [NSColor blackColor];
194     CGFloat components[4];
195     [rgbColor getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]];
196     
197     // create a new CGColorRef to return
198     CGColorSpaceRef cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
199     CGColorRef cgColor = CGColorCreate(cgColorSpace, components);
200     CGColorSpaceRelease(cgColorSpace);
201     CFMakeCollectable(cgColor);
202     
203     // check for match with existing color
204     if (existingColor && CGColorEqualToColor(cgColor, existingColor))
205         cgColor = nil;
206
207     return cgColor;
208 }
209
210 static void AXAttributeStringSetColor(NSMutableAttributedString* attrString, NSString* attribute, NSColor* color, NSRange range)
211 {
212     if (color) {
213         CGColorRef existingColor = (CGColorRef) [attrString attribute:attribute atIndex:range.location effectiveRange:nil];
214         CGColorRef cgColor = CreateCGColorIfDifferent(color, existingColor);
215         if (cgColor) {
216             [attrString addAttribute:attribute value:(id)cgColor range:range];
217             CGColorRelease(cgColor);
218         }
219     } else
220         [attrString removeAttribute:attribute range:range];
221 }
222
223 static void AXAttributeStringSetNumber(NSMutableAttributedString* attrString, NSString* attribute, NSNumber* number, NSRange range)
224 {
225     if (number)
226         [attrString addAttribute:attribute value:number range:range];
227     else
228         [attrString removeAttribute:attribute range:range];
229 }
230
231 static void AXAttributeStringSetStyle(NSMutableAttributedString* attrString, RenderObject* renderer, NSRange range)
232 {
233     RenderStyle* style = renderer->style();
234
235     // set basic font info
236     AXAttributeStringSetFont(attrString, NSAccessibilityFontTextAttribute, style->font().primaryFont()->getNSFont(), range);
237
238     // set basic colors
239     AXAttributeStringSetColor(attrString, NSAccessibilityForegroundColorTextAttribute, nsColor(style->color()), range);
240     AXAttributeStringSetColor(attrString, NSAccessibilityBackgroundColorTextAttribute, nsColor(style->backgroundColor()), range);
241
242     // set super/sub scripting
243     EVerticalAlign alignment = style->verticalAlign();
244     if (alignment == SUB)
245         AXAttributeStringSetNumber(attrString, NSAccessibilitySuperscriptTextAttribute, [NSNumber numberWithInt:(-1)], range);
246     else if (alignment == SUPER)
247         AXAttributeStringSetNumber(attrString, NSAccessibilitySuperscriptTextAttribute, [NSNumber numberWithInt:1], range);
248     else
249         [attrString removeAttribute:NSAccessibilitySuperscriptTextAttribute range:range];
250     
251     // set shadow
252     if (style->textShadow())
253         AXAttributeStringSetNumber(attrString, NSAccessibilityShadowTextAttribute, [NSNumber numberWithBool:YES], range);
254     else
255         [attrString removeAttribute:NSAccessibilityShadowTextAttribute range:range];
256     
257     // set underline and strikethrough
258     int decor = style->textDecorationsInEffect();
259     if ((decor & UNDERLINE) == 0) {
260         [attrString removeAttribute:NSAccessibilityUnderlineTextAttribute range:range];
261         [attrString removeAttribute:NSAccessibilityUnderlineColorTextAttribute range:range];
262     }
263     
264     if ((decor & LINE_THROUGH) == 0) {
265         [attrString removeAttribute:NSAccessibilityStrikethroughTextAttribute range:range];
266         [attrString removeAttribute:NSAccessibilityStrikethroughColorTextAttribute range:range];
267     }
268
269     if ((decor & (UNDERLINE | LINE_THROUGH)) != 0) {
270         // find colors using quirk mode approach (strict mode would use current
271         // color for all but the root line box, which would use getTextDecorationColors)
272         Color underline, overline, linethrough;
273         renderer->getTextDecorationColors(decor, underline, overline, linethrough);
274         
275         if ((decor & UNDERLINE) != 0) {
276             AXAttributeStringSetNumber(attrString, NSAccessibilityUnderlineTextAttribute, [NSNumber numberWithBool:YES], range);
277             AXAttributeStringSetColor(attrString, NSAccessibilityUnderlineColorTextAttribute, nsColor(underline), range);
278         }
279
280         if ((decor & LINE_THROUGH) != 0) {
281             AXAttributeStringSetNumber(attrString, NSAccessibilityStrikethroughTextAttribute, [NSNumber numberWithBool:YES], range);
282             AXAttributeStringSetColor(attrString, NSAccessibilityStrikethroughColorTextAttribute, nsColor(linethrough), range);
283         }
284     }
285 }
286
287 static int blockquoteLevel(RenderObject* renderer)
288 {
289     int result = 0;
290     for (Node* node = renderer->element(); node; node = node->parent()) {
291         if (node->hasTagName(blockquoteTag))
292             result += 1;
293     }
294     
295     return result;
296 }
297
298 static void AXAttributeStringSetBlockquoteLevel(NSMutableAttributedString* attrString, RenderObject* renderer, NSRange range)
299 {
300     int quoteLevel = blockquoteLevel(renderer);
301     
302     if (quoteLevel)
303         [attrString addAttribute:@"AXBlockQuoteLevel" value:[NSNumber numberWithInt:quoteLevel] range:range];
304     else
305         [attrString removeAttribute:@"AXBlockQuoteLevel" range:range];
306 }
307
308 static void AXAttributeStringSetSpelling(NSMutableAttributedString* attrString, Node* node, int offset, NSRange range)
309 {
310     Vector<DocumentMarker> markers = node->renderer()->document()->markersForNode(node);
311     Vector<DocumentMarker>::iterator markerIt = markers.begin();
312
313     unsigned endOffset = (unsigned)offset + range.length;
314     for ( ; markerIt != markers.end(); markerIt++) {
315         DocumentMarker marker = *markerIt;
316         
317         if (marker.type != DocumentMarker::Spelling)
318             continue;
319         
320         if (marker.endOffset <= (unsigned)offset)
321             continue;
322         
323         if (marker.startOffset > endOffset)
324             break;
325         
326         // add misspelling attribute for the intersection of the marker and the range
327         int rStart = range.location + (marker.startOffset - offset);
328         int rLength = MIN(marker.endOffset, endOffset) - marker.startOffset;
329         NSRange spellRange = NSMakeRange(rStart, rLength);
330         AXAttributeStringSetNumber(attrString, NSAccessibilityMisspelledTextAttribute, [NSNumber numberWithBool:YES], spellRange);
331         
332         if (marker.endOffset > endOffset + 1)
333             break;
334     }
335 }
336
337 static void AXAttributeStringSetHeadingLevel(AccessibilityObject* object, NSMutableAttributedString* attrString, RenderObject* renderer, NSRange range)
338 {
339     int parentHeadingLevel = AccessibilityRenderObject::headingLevel(renderer->parent()->element());
340     
341     if (parentHeadingLevel)
342         [attrString addAttribute:@"AXHeadingLevel" value:[NSNumber numberWithInt:parentHeadingLevel] range:range];
343     else
344         [attrString removeAttribute:@"AXHeadingLevel" range:range];
345 }
346
347 static AccessibilityObject* AXLinkElementForNode(Node* node)
348 {
349     RenderObject* obj = node->renderer();
350     if (!obj)
351         return 0;
352
353     RefPtr<AccessibilityObject> axObj = obj->document()->axObjectCache()->get(obj);
354     HTMLAnchorElement* anchor = axObj->anchorElement();
355     if (!anchor)
356         return 0;
357
358     RenderObject* anchorRenderer = anchor->renderer();
359     if (!anchorRenderer)
360         return 0;
361     
362     return anchorRenderer->document()->axObjectCache()->get(anchorRenderer);
363 }
364
365 static void AXAttributeStringSetElement(NSMutableAttributedString* attrString, NSString* attribute, AccessibilityObject* object, NSRange range)
366 {
367     if (object && object->isAccessibilityRenderObject()) {
368         // make a serialiazable AX object
369         
370         RenderObject* renderer = static_cast<AccessibilityRenderObject*>(object)->renderer();
371         if (!renderer)
372             return;
373         
374         Document* doc = renderer->document();
375         if (!doc)
376             return;
377         
378         AXObjectCache* cache = doc->axObjectCache();
379         if (!cache)
380             return;
381
382         AXUIElementRef axElement = [[WebCoreViewFactory sharedFactory] AXUIElementForElement:object->wrapper()];
383         if (axElement) {
384             [attrString addAttribute:attribute value:(id)axElement range:range];
385             CFRelease(axElement);
386         }
387     } else
388         [attrString removeAttribute:attribute range:range];
389 }
390
391 static void AXAttributedStringAppendText(AccessibilityObject* object, NSMutableAttributedString* attrString, Node* node, int offset, const UChar* chars, int length)
392 {
393     // skip invisible text
394     if (!node->renderer())
395         return;
396
397     // easier to calculate the range before appending the string
398     NSRange attrStringRange = NSMakeRange([attrString length], length);
399     
400     // append the string from this node
401     [[attrString mutableString] appendString:[NSString stringWithCharacters:chars length:length]];
402
403     // add new attributes and remove irrelevant inherited ones
404     // NOTE: color attributes are handled specially because -[NSMutableAttributedString addAttribute: value: range:] does not merge
405     // identical colors.  Workaround is to not replace an existing color attribute if it matches what we are adding.  This also means
406     // we cannot just pre-remove all inherited attributes on the appended string, so we have to remove the irrelevant ones individually.
407
408     // remove inherited attachment from prior AXAttributedStringAppendReplaced
409     [attrString removeAttribute:NSAccessibilityAttachmentTextAttribute range:attrStringRange];
410     
411     // set new attributes
412     AXAttributeStringSetStyle(attrString, node->renderer(), attrStringRange);
413     AXAttributeStringSetHeadingLevel(object, attrString, node->renderer(), attrStringRange);
414     AXAttributeStringSetBlockquoteLevel(attrString, node->renderer(), attrStringRange);
415     AXAttributeStringSetElement(attrString, NSAccessibilityLinkTextAttribute, AXLinkElementForNode(node), attrStringRange);
416     
417     // do spelling last because it tends to break up the range
418     AXAttributeStringSetSpelling(attrString, node, offset, attrStringRange);
419 }
420
421 static NSString* nsStringForReplacedNode(Node* replacedNode)
422 {
423     // we should always be given a rendered node and a replaced node, but be safe
424     // replaced nodes are either attachments (widgets) or images
425     if (!replacedNode || !replacedNode->renderer() || !replacedNode->renderer()->isReplaced() || replacedNode->isTextNode()) {
426         ASSERT_NOT_REACHED();
427         return nil;
428     }
429
430     // create an AX object, but skip it if it is not supposed to be seen
431     RefPtr<AccessibilityObject> obj = replacedNode->renderer()->document()->axObjectCache()->get(replacedNode->renderer());
432     if (obj->accessibilityIsIgnored())
433         return nil;
434     
435     // use the attachmentCharacter to represent the replaced node
436     const UniChar attachmentChar = NSAttachmentCharacter;
437     return [NSString stringWithCharacters:&attachmentChar length:1];
438 }
439
440 - (NSAttributedString*)doAXAttributedStringForTextMarkerRange:(WebCoreTextMarkerRange*)textMarkerRange
441 {
442     // extract the start and end VisiblePosition
443     VisiblePosition startVisiblePosition = visiblePositionForStartOfTextMarkerRange(textMarkerRange);
444     if (startVisiblePosition.isNull())
445         return nil;
446
447     VisiblePosition endVisiblePosition = visiblePositionForEndOfTextMarkerRange(textMarkerRange);
448     if (endVisiblePosition.isNull())
449         return nil;
450
451     // iterate over the range to build the AX attributed string
452     NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] init];
453     TextIterator it(makeRange(startVisiblePosition, endVisiblePosition).get());
454     while (!it.atEnd()) {
455         // locate the node and starting offset for this range
456         int exception = 0;
457         Node* node = it.range()->startContainer(exception);
458         ASSERT(node == it.range()->endContainer(exception));
459         int offset = it.range()->startOffset(exception);
460
461         // non-zero length means textual node, zero length means replaced node (AKA "attachments" in AX)
462         if (it.length() != 0) {
463             AXAttributedStringAppendText(m_object, attrString, node, offset, it.characters(), it.length());
464         } else {
465             Node* replacedNode = node->childNode(offset);
466             NSString *attachmentString = nsStringForReplacedNode(replacedNode);
467             if (attachmentString) {
468                 NSRange attrStringRange = NSMakeRange([attrString length], [attachmentString length]);
469
470                 // append the placeholder string
471                 [[attrString mutableString] appendString:attachmentString];
472
473                 // remove all inherited attributes
474                 [attrString setAttributes:nil range:attrStringRange];
475
476                 // add the attachment attribute
477                 AccessibilityObject* obj = replacedNode->renderer()->document()->axObjectCache()->get(replacedNode->renderer());
478                 AXAttributeStringSetElement(attrString, NSAccessibilityAttachmentTextAttribute, obj, attrStringRange);
479             }
480         }
481         it.advance();
482     }
483
484     return [attrString autorelease];
485 }
486
487 static WebCoreTextMarkerRange* textMarkerRangeFromVisiblePositions(VisiblePosition startPosition, VisiblePosition endPosition)
488 {
489     WebCoreTextMarker* startTextMarker = textMarkerForVisiblePosition(startPosition);
490     WebCoreTextMarker* endTextMarker   = textMarkerForVisiblePosition(endPosition);
491     return textMarkerRangeFromMarkers(startTextMarker, endTextMarker);
492 }
493
494 - (NSArray*)accessibilityActionNames
495 {
496     static NSArray* actions = nil;
497     
498     if (actions == nil) {
499         if (m_object->actionElement()) 
500             actions = [[NSArray alloc] initWithObjects: NSAccessibilityPressAction, nil];
501         else if (m_object->isAttachment())
502             actions = [[[self attachmentView] accessibilityActionNames] retain];
503     }
504
505     return actions;
506 }
507
508 - (NSArray*)accessibilityAttributeNames
509 {
510     if (m_object->isAttachment())
511         return [[self attachmentView] accessibilityAttributeNames];
512
513     static NSArray* attributes = nil;
514     static NSArray* anchorAttrs = nil;
515     static NSArray* webAreaAttrs = nil;
516     static NSArray* textAttrs = nil;
517     static NSArray* listBoxAttrs = nil;
518     NSMutableArray* tempArray;
519     if (attributes == nil) {
520         attributes = [[NSArray alloc] initWithObjects: NSAccessibilityRoleAttribute,
521                       NSAccessibilitySubroleAttribute,
522                       NSAccessibilityRoleDescriptionAttribute,
523                       NSAccessibilityChildrenAttribute,
524                       NSAccessibilityHelpAttribute,
525                       NSAccessibilityParentAttribute,
526                       NSAccessibilityPositionAttribute,
527                       NSAccessibilitySizeAttribute,
528                       NSAccessibilityTitleAttribute,
529                       NSAccessibilityDescriptionAttribute,
530                       NSAccessibilityValueAttribute,
531                       NSAccessibilityFocusedAttribute,
532                       NSAccessibilityEnabledAttribute,
533                       NSAccessibilityWindowAttribute,
534                       @"AXSelectedTextMarkerRange",
535                       @"AXStartTextMarker",
536                       @"AXEndTextMarker",
537                       @"AXVisited",
538                       NSAccessibilityLinkedUIElementsAttribute,
539                       NSAccessibilitySelectedAttribute,
540                       nil];
541     }
542     if (anchorAttrs == nil) {
543         tempArray = [[NSMutableArray alloc] initWithArray:attributes];
544         [tempArray addObject:NSAccessibilityURLAttribute];
545         anchorAttrs = [[NSArray alloc] initWithArray:tempArray];
546         [tempArray release];
547     }
548     if (webAreaAttrs == nil) {
549         tempArray = [[NSMutableArray alloc] initWithArray:attributes];
550         [tempArray addObject:@"AXLinkUIElements"];
551         [tempArray addObject:@"AXLoaded"];
552         [tempArray addObject:@"AXLayoutCount"];
553         [tempArray addObject:NSAccessibilityURLAttribute];
554         webAreaAttrs = [[NSArray alloc] initWithArray:tempArray];
555         [tempArray release];
556     }
557     if (textAttrs == nil) {
558         tempArray = [[NSMutableArray alloc] initWithArray:attributes];
559         [tempArray addObject:NSAccessibilityNumberOfCharactersAttribute];
560         [tempArray addObject:NSAccessibilitySelectedTextAttribute];
561         [tempArray addObject:NSAccessibilitySelectedTextRangeAttribute];
562         [tempArray addObject:NSAccessibilityVisibleCharacterRangeAttribute];
563         [tempArray addObject:NSAccessibilityInsertionPointLineNumberAttribute];
564         textAttrs = [[NSArray alloc] initWithArray:tempArray];
565         [tempArray release];
566     }
567     if (listBoxAttrs == nil) {
568         tempArray = [[NSMutableArray alloc] initWithArray:attributes];
569         [tempArray addObject:NSAccessibilitySelectedChildrenAttribute];
570         [tempArray addObject:NSAccessibilityVisibleChildrenAttribute];
571         [tempArray addObject:NSAccessibilityOrientationAttribute];
572         listBoxAttrs = [[NSArray alloc] initWithArray:tempArray];
573         [tempArray release];
574     }
575     
576     if (m_object->isPasswordField())
577         return attributes;
578
579     if (m_object->isWebArea())
580         return webAreaAttrs;
581     
582     if (m_object->isTextControl())
583         return textAttrs;
584
585     if (m_object->isAnchor() || m_object->isImage())
586         return anchorAttrs;
587
588     if (m_object->isListBox())
589         return listBoxAttrs;
590
591     return attributes;
592 }
593
594 - (VisiblePositionRange)visiblePositionRangeForTextMarkerRange:(WebCoreTextMarkerRange*) textMarkerRange
595 {
596     return VisiblePositionRange(visiblePositionForStartOfTextMarkerRange(textMarkerRange), visiblePositionForEndOfTextMarkerRange(textMarkerRange));
597 }
598
599 - (NSArray*)renderWidgetChildren
600 {
601     Widget* widget = m_object->widget();
602     if (!widget)
603         return nil;
604     return [(widget->getOuterView()) accessibilityAttributeValue: NSAccessibilityChildrenAttribute];
605 }
606
607 static NSMutableArray* convertToNSArray(const Vector<RefPtr<AccessibilityObject> >& vector)
608 {
609     unsigned length = vector.size();
610     NSMutableArray* array = [NSMutableArray arrayWithCapacity: length];
611     for (unsigned i = 0; i < length; ++i)
612         [array addObject:vector[i]->wrapper()];
613     return array;
614 }
615
616 - (WebCoreTextMarkerRange*)textMarkerRangeForSelection
617 {
618     Selection selection = m_object->selection();
619     if (selection.isNone())
620         return nil;
621     return textMarkerRangeFromVisiblePositions(selection.visibleStart(), selection.visibleEnd());
622 }
623
624 - (NSValue*)position
625 {
626     IntRect rect = m_object->elementRect();
627     
628     // The Cocoa accessibility API wants the lower-left corner.
629     NSPoint point = NSMakePoint(rect.x(), rect.bottom());
630     FrameView* frameView = m_object->documentFrameView();
631     if (frameView) {
632         NSView* view = frameView->documentView();
633         point = [[view window] convertBaseToScreen: [view convertPoint: point toView:nil]];
634     }
635
636     return [NSValue valueWithPoint: point];
637 }
638
639 typedef HashMap<int, NSString*> AccessibilityRoleMap;
640
641 static const AccessibilityRoleMap& createAccessibilityRoleMap()
642 {
643     struct RoleEntry {
644         AccessibilityRole value;
645         NSString* string;
646     };
647     
648     static const RoleEntry roles[] = {
649         { UnknownRole, NSAccessibilityUnknownRole },
650         { ButtonRole, NSAccessibilityButtonRole },
651         { RadioButtonRole, NSAccessibilityRadioButtonRole },
652         { CheckBoxRole, NSAccessibilityCheckBoxRole },
653         { SliderRole, NSAccessibilitySliderRole },
654         { TabGroupRole, NSAccessibilityTabGroupRole },
655         { TextFieldRole, NSAccessibilityTextFieldRole },
656         { StaticTextRole, NSAccessibilityStaticTextRole },
657         { TextAreaRole, NSAccessibilityTextAreaRole },
658         { ScrollAreaRole, NSAccessibilityScrollAreaRole },
659         { PopUpButtonRole, NSAccessibilityPopUpButtonRole },
660         { MenuButtonRole, NSAccessibilityMenuButtonRole },
661         { TableRole, NSAccessibilityTableRole },
662         { ApplicationRole, NSAccessibilityApplicationRole },
663         { GroupRole, NSAccessibilityGroupRole },
664         { RadioGroupRole, NSAccessibilityRadioGroupRole },
665         { ListRole, NSAccessibilityListRole },
666         { ScrollBarRole, NSAccessibilityScrollBarRole },
667         { ValueIndicatorRole, NSAccessibilityValueIndicatorRole },
668         { ImageRole, NSAccessibilityImageRole },
669         { MenuBarRole, NSAccessibilityMenuBarRole },
670         { MenuRole, NSAccessibilityMenuRole },
671         { MenuItemRole, NSAccessibilityMenuItemRole },
672         { ColumnRole, NSAccessibilityColumnRole },
673         { RowRole, NSAccessibilityRowRole },
674         { ToolbarRole, NSAccessibilityToolbarRole },
675         { BusyIndicatorRole, NSAccessibilityBusyIndicatorRole },
676         { ProgressIndicatorRole, NSAccessibilityProgressIndicatorRole },
677         { WindowRole, NSAccessibilityWindowRole },
678         { DrawerRole, NSAccessibilityDrawerRole },
679         { SystemWideRole, NSAccessibilitySystemWideRole },
680         { OutlineRole, NSAccessibilityOutlineRole },
681         { IncrementorRole, NSAccessibilityIncrementorRole },
682         { BrowserRole, NSAccessibilityBrowserRole },
683         { ComboBoxRole, NSAccessibilityComboBoxRole },
684         { SplitGroupRole, NSAccessibilitySplitGroupRole },
685         { SplitterRole, NSAccessibilitySplitterRole },
686         { ColorWellRole, NSAccessibilityColorWellRole },
687         { GrowAreaRole, NSAccessibilityGrowAreaRole },
688         { SheetRole, NSAccessibilitySheetRole },
689         { HelpTagRole, NSAccessibilityHelpTagRole },
690         { MatteRole, NSAccessibilityMatteRole }, 
691         { RulerRole, NSAccessibilityRulerRole },
692         { RulerMarkerRole, NSAccessibilityRulerMarkerRole },
693         { LinkRole, NSAccessibilityLinkRole },
694 #ifndef BUILDING_ON_TIGER        
695         { DisclosureTriangleRole, NSAccessibilityDisclosureTriangleRole },
696         { GridRole, NSAccessibilityGridRole },
697 #endif
698         { WebCoreLinkRole, @"AXLink" }, // why isn't this just NSAccessibilityLinkRole ?
699         { ImageMapRole, @"AXImageMap" },
700         { ListMarkerRole, @"AXListMarker" },
701         { WebAreaRole, @"AXWebArea" },
702         { HeadingRole, @"AXHeading" },
703         { ListBoxRole, NSAccessibilityListRole },
704         { ListBoxOptionRole, NSAccessibilityStaticTextRole }
705     };
706     AccessibilityRoleMap& roleMap = *new AccessibilityRoleMap;
707     
708     const unsigned numRoles = sizeof(roles) / sizeof(roles[0]);
709     for (unsigned i = 0; i < numRoles; ++i)
710         roleMap.set(roles[i].value, roles[i].string);
711     return roleMap;
712 }
713
714 static NSString* roleValueToNSString(AccessibilityRole value)
715 {
716     ASSERT(value);
717     static const AccessibilityRoleMap& roleMap = createAccessibilityRoleMap();
718     return roleMap.get(value);
719 }
720
721 - (NSString*)role
722 {
723     if (m_object->isAttachment())
724         return [[self attachmentView] accessibilityAttributeValue:NSAccessibilityRoleAttribute];
725     NSString* string = roleValueToNSString(m_object->roleValue());
726     if (string != nil)
727         return string;
728     return NSAccessibilityUnknownRole;
729 }
730
731 - (NSString*)subrole
732 {
733     if (m_object->isPasswordField())
734         return NSAccessibilitySecureTextFieldSubrole;
735     
736     if (m_object->isAttachment()) {
737         NSView* attachView = [self attachmentView];
738         if ([[attachView accessibilityAttributeNames] containsObject:NSAccessibilitySubroleAttribute]) {
739             return [attachView accessibilityAttributeValue:NSAccessibilitySubroleAttribute];
740         }
741     }
742
743     return nil;
744 }
745
746 - (NSString*)roleDescription
747 {
748     if (!m_object)
749         return nil;
750
751     // attachments have the AXImage role, but a different subrole
752     if (m_object->isAttachment())
753         return [[self attachmentView] accessibilityAttributeValue:NSAccessibilityRoleDescriptionAttribute];
754     
755     // FIXME 3447564: It would be better to call some AppKit API to get these strings
756     // (which would be the best way to localize them)
757     
758     NSString* axRole = [self role];
759     if ([axRole isEqualToString:NSAccessibilityButtonRole])
760         return NSAccessibilityRoleDescription(NSAccessibilityButtonRole, [self subrole]);
761     
762     if ([axRole isEqualToString:NSAccessibilityPopUpButtonRole])
763         return NSAccessibilityRoleDescription(NSAccessibilityPopUpButtonRole, [self subrole]);
764
765     if ([axRole isEqualToString:NSAccessibilityStaticTextRole])
766         return NSAccessibilityRoleDescription(NSAccessibilityStaticTextRole, [self subrole]);
767
768     if ([axRole isEqualToString:NSAccessibilityImageRole])
769         return NSAccessibilityRoleDescription(NSAccessibilityImageRole, [self subrole]);
770     
771     if ([axRole isEqualToString:NSAccessibilityGroupRole])
772         return NSAccessibilityRoleDescription(NSAccessibilityGroupRole, [self subrole]);
773     
774     if ([axRole isEqualToString:NSAccessibilityCheckBoxRole])
775         return NSAccessibilityRoleDescription(NSAccessibilityCheckBoxRole, [self subrole]);
776
777     if ([axRole isEqualToString:NSAccessibilityRadioButtonRole])
778         return NSAccessibilityRoleDescription(NSAccessibilityRadioButtonRole, [self subrole]);
779
780     if ([axRole isEqualToString:NSAccessibilityTextFieldRole])
781         return NSAccessibilityRoleDescription(NSAccessibilityTextFieldRole, [self subrole]);
782
783     if ([axRole isEqualToString:NSAccessibilityTextAreaRole])
784         return NSAccessibilityRoleDescription(NSAccessibilityTextAreaRole, [self subrole]);
785
786     if ([axRole isEqualToString:NSAccessibilityListRole])
787         return NSAccessibilityRoleDescription(NSAccessibilityListRole, [self subrole]);
788     
789     if ([axRole isEqualToString:@"AXWebArea"])
790         return AXWebAreaText();
791     
792     if ([axRole isEqualToString:@"AXLink"])
793         return AXLinkText();
794     
795     if ([axRole isEqualToString:@"AXListMarker"])
796         return AXListMarkerText();
797     
798     if ([axRole isEqualToString:@"AXImageMap"])
799         return AXImageMapText();
800
801     if ([axRole isEqualToString:@"AXHeading"])
802         return AXHeadingText();
803     
804     return NSAccessibilityRoleDescription(NSAccessibilityUnknownRole, nil);
805 }
806
807 // FIXME: split up this function in a better way.  
808 // suggestions: Use a hash table that maps attribute names to function calls,
809 // or maybe pointers to member functions
810 - (id)accessibilityAttributeValue:(NSString*)attributeName
811 {
812     if (!m_object)
813         return nil;
814
815     if ([attributeName isEqualToString: NSAccessibilityRoleAttribute])
816         return [self role];
817
818     if ([attributeName isEqualToString: NSAccessibilitySubroleAttribute])
819         return [self subrole];
820
821     if ([attributeName isEqualToString: NSAccessibilityRoleDescriptionAttribute])
822         return [self roleDescription];
823
824     if ([attributeName isEqualToString: NSAccessibilityParentAttribute]) {
825         if (m_object->isAccessibilityRenderObject()) {
826             FrameView* fv = static_cast<AccessibilityRenderObject*>(m_object)->frameViewIfRenderView();
827             if (fv)
828                 return fv->getView();
829         }
830         
831         return m_object->parentObjectUnignored()->wrapper();
832     }
833
834     if ([attributeName isEqualToString: NSAccessibilityChildrenAttribute]) {
835         if (m_object->children().isEmpty()) {
836             NSArray* children = [self renderWidgetChildren];
837             if (children != nil)
838                 return children;
839         }
840         return convertToNSArray(m_object->children());
841     }
842     
843     if ([attributeName isEqualToString: NSAccessibilitySelectedChildrenAttribute] && m_object->isListBox())
844         return convertToNSArray(static_cast<AccessibilityListBox*>(m_object)->selectedChildren());
845     
846     if ([attributeName isEqualToString: NSAccessibilityVisibleChildrenAttribute] && m_object->isListBox())
847         return convertToNSArray(static_cast<AccessibilityListBox*>(m_object)->visibleChildren());
848     
849     
850     if (m_object->isWebArea()) {
851         if ([attributeName isEqualToString: @"AXLinkUIElements"]) {
852             Vector<RefPtr<AccessibilityObject> > links;
853             m_object->getDocumentLinks(links);
854             return convertToNSArray(links);
855         }
856         if ([attributeName isEqualToString: @"AXLoaded"])
857             return [NSNumber numberWithBool: m_object->isLoaded()];
858         if ([attributeName isEqualToString: @"AXLayoutCount"])
859             return [NSNumber numberWithInt: m_object->layoutCount()];
860     }
861     
862     if (m_object->isTextControl()) {
863         if ([attributeName isEqualToString: NSAccessibilityNumberOfCharactersAttribute]) {
864             int length = m_object->textLength();
865             if (length < 0)
866                 return nil;
867             return [NSNumber numberWithUnsignedInt:length];
868         }
869         if ([attributeName isEqualToString: NSAccessibilitySelectedTextAttribute]) {
870             String selectedText = m_object->selectedText();
871             if (selectedText.isNull())
872                 return nil;
873             return (NSString*)selectedText;
874         }
875         if ([attributeName isEqualToString: NSAccessibilitySelectedTextRangeAttribute]) {
876             PlainTextRange textRange = m_object->selectedTextRange();
877             if (textRange.isNull())
878                 return nil;
879             return [NSValue valueWithRange:NSMakeRange(textRange.start, textRange.length)];
880         }
881         // TODO: Get actual visible range. <rdar://problem/4712101>
882         if ([attributeName isEqualToString: NSAccessibilityVisibleCharacterRangeAttribute])
883             return m_object->isPasswordField() ? nil : [NSValue valueWithRange: NSMakeRange(0, m_object->textLength())];
884         if ([attributeName isEqualToString: NSAccessibilityInsertionPointLineNumberAttribute]) {
885             if (m_object->isPasswordField() || m_object->selectionStart() != m_object->selectionEnd())
886                 return nil;
887             return [NSNumber numberWithInt:m_object->doAXLineForTextMarker(m_object->textMarkerForIndex(m_object->selectionStart(), true))];
888         }
889     }
890     
891     if ([attributeName isEqualToString: NSAccessibilityURLAttribute]) {
892         KURL url = m_object->url();
893         if (url.isNull())
894             return nil;
895         return (NSURL*)url;
896     }
897
898     if ([attributeName isEqualToString: @"AXVisited"])
899         return [NSNumber numberWithBool: m_object->isVisited()];
900     
901     if ([attributeName isEqualToString: NSAccessibilityTitleAttribute]) {
902         if (m_object->isAttachment()) {
903             if ([[[self attachmentView] accessibilityAttributeNames] containsObject:NSAccessibilityTitleAttribute]) 
904                 return [[self attachmentView] accessibilityAttributeValue:NSAccessibilityTitleAttribute];
905         }
906         return m_object->title();
907     }
908     
909     if ([attributeName isEqualToString: NSAccessibilityDescriptionAttribute]) {
910         if (m_object->isAttachment()) {
911             if ([[[self attachmentView] accessibilityAttributeNames] containsObject:NSAccessibilityDescriptionAttribute])
912                 return [[self attachmentView] accessibilityAttributeValue:NSAccessibilityDescriptionAttribute];
913         }
914         return m_object->accessibilityDescription();
915     }
916     
917     if ([attributeName isEqualToString: NSAccessibilityValueAttribute]) {
918         if (m_object->isAttachment()) {
919             if ([[[self attachmentView] accessibilityAttributeNames] containsObject:NSAccessibilityValueAttribute]) 
920                 return [[self attachmentView] accessibilityAttributeValue:NSAccessibilityValueAttribute];
921         }    
922         if (m_object->hasIntValue())
923             return [NSNumber numberWithInt:m_object->intValue()];
924         return m_object->stringValue();
925     }
926
927     if ([attributeName isEqualToString: NSAccessibilityHelpAttribute])
928         return m_object->helpText();
929
930     if ([attributeName isEqualToString: NSAccessibilityFocusedAttribute])
931         return [NSNumber numberWithBool: m_object->isFocused()];
932
933     if ([attributeName isEqualToString: NSAccessibilityEnabledAttribute])
934         return [NSNumber numberWithBool: m_object->isEnabled()];
935
936     if ([attributeName isEqualToString: NSAccessibilitySizeAttribute]) {
937         IntSize s = m_object->size();
938         return [NSValue valueWithSize: NSMakeSize(s.width(), s.height())];
939     }
940
941     if ([attributeName isEqualToString: NSAccessibilityPositionAttribute])
942         return [self position];
943
944     if ([attributeName isEqualToString: NSAccessibilityWindowAttribute]) {
945         FrameView* fv = m_object->documentFrameView();
946         if (fv)
947             return [fv->getView() window];
948         return nil;
949     }
950     
951     if (m_object->isListBox() && [attributeName isEqualToString:NSAccessibilityOrientationAttribute])
952         return NSAccessibilityVerticalOrientationValue;
953
954     if ([attributeName isEqualToString: @"AXSelectedTextMarkerRange"])
955         return [self textMarkerRangeForSelection];
956     
957     if (m_object->isAccessibilityRenderObject()) {
958         RenderObject* renderer = static_cast<AccessibilityRenderObject*>(m_object)->renderer();
959         if (!renderer)
960             return nil;
961         
962         if ([attributeName isEqualToString: @"AXStartTextMarker"])
963             return textMarkerForVisiblePosition(startOfDocument(renderer->document()));
964         if ([attributeName isEqualToString: @"AXEndTextMarker"])
965             return textMarkerForVisiblePosition(endOfDocument(renderer->document()));
966     }
967     
968     if ([attributeName isEqualToString: NSAccessibilityLinkedUIElementsAttribute]) {
969         AccessibilityObject* obj = m_object->linkedUIElement();
970         if (obj)
971             return (id) obj->wrapper();
972         return nil;
973     }
974
975     if ([attributeName isEqualToString: NSAccessibilitySelectedAttribute])
976         return [NSNumber numberWithBool:m_object->isSelected()];
977     
978     return nil;
979 }
980
981 - (id)accessibilityFocusedUIElement
982 {
983     RefPtr<AccessibilityObject> focusedObj = m_object->focusedUIElement();
984
985     if (!focusedObj)
986         return nil;
987     
988     return focusedObj->wrapper();
989 }
990
991 - (id)accessibilityHitTest:(NSPoint)point
992 {
993     RefPtr<AccessibilityObject> axObject = m_object->doAccessibilityHitTest(IntPoint(point));
994     if (axObject)
995         return NSAccessibilityUnignoredAncestor(axObject->wrapper());
996     return NSAccessibilityUnignoredAncestor(self);
997 }
998
999 - (BOOL)accessibilityIsAttributeSettable:(NSString*)attributeName
1000 {
1001     if ([attributeName isEqualToString: @"AXSelectedTextMarkerRange"])
1002         return YES;
1003
1004     if ([attributeName isEqualToString: NSAccessibilityFocusedAttribute])
1005         return m_object->canSetFocusAttribute();
1006
1007     if ([attributeName isEqualToString: NSAccessibilityValueAttribute])
1008         return m_object->canSetValueAttribute();
1009
1010     if ([attributeName isEqualToString: NSAccessibilitySelectedTextAttribute] ||
1011         [attributeName isEqualToString: NSAccessibilitySelectedTextRangeAttribute] ||
1012         [attributeName isEqualToString: NSAccessibilityVisibleCharacterRangeAttribute])
1013         return m_object->canSetTextRangeAttributes();
1014     
1015     return NO;
1016 }
1017
1018 // accessibilityShouldUseUniqueId is an AppKit method we override so that
1019 // objects will be given a unique ID, and therefore allow AppKit to know when they
1020 // become obsolete (e.g. when the user navigates to a new web page, making this one
1021 // unrendered but not deallocated because it is in the back/forward cache).
1022 // It is important to call NSAccessibilityUnregisterUniqueIdForUIElement in the
1023 // appropriate place (e.g. dealloc) to remove these non-retained references from
1024 // AppKit's id mapping tables. We do this in detach by calling unregisterUniqueIdForUIElement.
1025 //
1026 // Registering an object is also required for observing notifications. Only registered objects can be observed.
1027 - (BOOL)accessibilityIsIgnored
1028 {
1029     if (m_object->isAttachment())
1030         return [[self attachmentView] accessibilityIsIgnored];
1031     return m_object->accessibilityIsIgnored();
1032 }
1033
1034 - (NSArray* )accessibilityParameterizedAttributeNames
1035 {
1036     if (m_object->isAttachment()) 
1037         return nil;
1038
1039     static NSArray* paramAttrs = nil;
1040     static NSArray* textParamAttrs = nil;
1041     if (paramAttrs == nil) {
1042         paramAttrs = [[NSArray alloc] initWithObjects:
1043                       @"AXUIElementForTextMarker",
1044                       @"AXTextMarkerRangeForUIElement",
1045                       @"AXLineForTextMarker",
1046                       @"AXTextMarkerRangeForLine",
1047                       @"AXStringForTextMarkerRange",
1048                       @"AXTextMarkerForPosition",
1049                       @"AXBoundsForTextMarkerRange",
1050                       @"AXAttributedStringForTextMarkerRange",
1051                       @"AXTextMarkerRangeForUnorderedTextMarkers",
1052                       @"AXNextTextMarkerForTextMarker",
1053                       @"AXPreviousTextMarkerForTextMarker",
1054                       @"AXLeftWordTextMarkerRangeForTextMarker",
1055                       @"AXRightWordTextMarkerRangeForTextMarker",
1056                       @"AXLeftLineTextMarkerRangeForTextMarker",
1057                       @"AXRightLineTextMarkerRangeForTextMarker",
1058                       @"AXSentenceTextMarkerRangeForTextMarker",
1059                       @"AXParagraphTextMarkerRangeForTextMarker",
1060                       @"AXNextWordEndTextMarkerForTextMarker",
1061                       @"AXPreviousWordStartTextMarkerForTextMarker",
1062                       @"AXNextLineEndTextMarkerForTextMarker",
1063                       @"AXPreviousLineStartTextMarkerForTextMarker",
1064                       @"AXNextSentenceEndTextMarkerForTextMarker",
1065                       @"AXPreviousSentenceStartTextMarkerForTextMarker",
1066                       @"AXNextParagraphEndTextMarkerForTextMarker",
1067                       @"AXPreviousParagraphStartTextMarkerForTextMarker",
1068                       @"AXStyleTextMarkerRangeForTextMarker",
1069                       @"AXLengthForTextMarkerRange",
1070                       nil];
1071     }
1072
1073     if (textParamAttrs == nil) {
1074         NSMutableArray* tempArray = [[NSMutableArray alloc] initWithArray:paramAttrs];
1075         [tempArray addObject:(NSString*)kAXLineForIndexParameterizedAttribute];
1076         [tempArray addObject:(NSString*)kAXRangeForLineParameterizedAttribute];
1077         [tempArray addObject:(NSString*)kAXStringForRangeParameterizedAttribute];
1078         [tempArray addObject:(NSString*)kAXRangeForPositionParameterizedAttribute];
1079         [tempArray addObject:(NSString*)kAXRangeForIndexParameterizedAttribute];
1080         [tempArray addObject:(NSString*)kAXBoundsForRangeParameterizedAttribute];
1081         [tempArray addObject:(NSString*)kAXRTFForRangeParameterizedAttribute];
1082         [tempArray addObject:(NSString*)kAXAttributedStringForRangeParameterizedAttribute];
1083         [tempArray addObject:(NSString*)kAXStyleRangeForIndexParameterizedAttribute];
1084         textParamAttrs = [[NSArray alloc] initWithArray:tempArray];
1085         [tempArray release];
1086     }
1087     
1088     if (m_object->isPasswordField())
1089         return [NSArray array];
1090     
1091     if (!m_object->isAccessibilityRenderObject())
1092         return paramAttrs;
1093
1094     if (m_object->isTextControl())
1095         return textParamAttrs;
1096
1097     return paramAttrs;
1098 }
1099
1100 - (void)accessibilityPerformAction:(NSString*)action
1101 {
1102     if (![action isEqualToString:NSAccessibilityPressAction])
1103         return;
1104     
1105     if (m_object->isAttachment())
1106         [[self attachmentView] accessibilityPerformAction:NSAccessibilityPressAction];
1107
1108     m_object->press();
1109 }
1110
1111 - (void)accessibilitySetValue:(id)value forAttribute:(NSString*)attributeName
1112 {
1113     WebCoreTextMarkerRange* textMarkerRange = nil;
1114     NSNumber*               number = nil;
1115     NSString*               string = nil;
1116     NSRange                 range = {0, 0};
1117
1118     // decode the parameter
1119     if ([[WebCoreViewFactory sharedFactory] objectIsTextMarkerRange:value])
1120         textMarkerRange = (WebCoreTextMarkerRange*) value;
1121
1122     else if ([value isKindOfClass:[NSNumber self]])
1123         number = value;
1124
1125     else if ([value isKindOfClass:[NSString self]])
1126         string = value;
1127     
1128     else if ([value isKindOfClass:[NSValue self]])
1129         range = [value rangeValue];
1130     
1131     // handle the command
1132     if ([attributeName isEqualToString: @"AXSelectedTextMarkerRange"]) {
1133         ASSERT(textMarkerRange);
1134         m_object->doSetAXSelectedTextMarkerRange([self visiblePositionRangeForTextMarkerRange:textMarkerRange]);        
1135     } else if ([attributeName isEqualToString: NSAccessibilityFocusedAttribute]) {
1136         ASSERT(number);
1137         m_object->setFocused([number intValue] != 0);
1138     } else if ([attributeName isEqualToString: NSAccessibilityValueAttribute]) {
1139         if (!string)
1140             return;
1141         m_object->setValue(string);
1142     } else if (m_object->isTextControl()) {
1143         if ([attributeName isEqualToString: NSAccessibilitySelectedTextAttribute]) {
1144             m_object->setSelectedText(string);
1145         } else if ([attributeName isEqualToString: NSAccessibilitySelectedTextRangeAttribute]) {
1146             m_object->setSelectedTextRange(PlainTextRange(range.location, range.length));
1147         } else if ([attributeName isEqualToString: NSAccessibilityVisibleCharacterRangeAttribute]) {
1148             m_object->makeRangeVisible(PlainTextRange(range.location, range.length));
1149         }
1150     }
1151 }
1152
1153 static RenderObject* rendererForView(NSView* view)
1154 {
1155     if (![view conformsToProtocol:@protocol(WebCoreFrameView)])
1156         return 0;
1157
1158     NSView<WebCoreFrameView>* frameView = (NSView<WebCoreFrameView>*)view;
1159     Frame* frame = [frameView _web_frame];
1160     if (!frame)
1161         return 0;
1162
1163     Node* node = frame->document()->ownerElement();
1164     if (!node)
1165         return 0;
1166
1167     return node->renderer();
1168 }
1169
1170 - (id)_accessibilityParentForSubview:(NSView*)subview
1171 {   
1172     RenderObject* renderer = rendererForView(subview);
1173     if (!renderer)
1174         return nil;
1175
1176     AccessibilityObject* obj = renderer->document()->axObjectCache()->get(renderer);
1177     if (obj)
1178         return obj->parentObjectUnignored()->wrapper();
1179     return nil;
1180 }
1181
1182 - (NSString*)accessibilityActionDescription:(NSString*)action
1183 {
1184     // we have no custom actions
1185     return NSAccessibilityActionDescription(action);
1186 }
1187
1188 // The CFAttributedStringType representation of the text associated with this accessibility
1189 // object that is specified by the given range.
1190 - (NSAttributedString*)doAXAttributedStringForRange:(NSRange)range
1191 {
1192     PlainTextRange textRange = PlainTextRange(range.location, range.length);
1193     VisiblePositionRange visiblePosRange = m_object->textMarkerRangeForRange(textRange);
1194     return [self doAXAttributedStringForTextMarkerRange:textMarkerRangeFromVisiblePositions(visiblePosRange.start, visiblePosRange.end)];
1195 }
1196
1197 // The RTF representation of the text associated with this accessibility object that is
1198 // specified by the given range.
1199 - (NSData*)doAXRTFForRange:(NSRange)range
1200 {
1201     NSAttributedString* attrString = [self doAXAttributedStringForRange:range];
1202     return [attrString RTFFromRange: NSMakeRange(0, [attrString length]) documentAttributes: nil];
1203 }
1204
1205 - (id)accessibilityAttributeValue:(NSString*)attribute forParameter:(id)parameter
1206 {
1207     WebCoreTextMarker* textMarker = nil;
1208     WebCoreTextMarkerRange* textMarkerRange = nil;
1209     NSNumber* number = nil;
1210     NSArray* array = nil;
1211     RefPtr<AccessibilityObject> uiElement = 0;
1212     NSPoint point = NSZeroPoint;
1213     bool pointSet = false;
1214     NSRange range = {0, 0};
1215     bool rangeSet = false;
1216     
1217     // basic parameter validation
1218     if (!m_object || !attribute || !parameter)
1219         return nil;
1220
1221     // common parameter type check/casting.  Nil checks in handlers catch wrong type case.
1222     // NOTE: This assumes nil is not a valid parameter, because it is indistinguishable from
1223     // a parameter of the wrong type.
1224     if ([[WebCoreViewFactory sharedFactory] objectIsTextMarker:parameter])
1225         textMarker = (WebCoreTextMarker*) parameter;
1226
1227     else if ([[WebCoreViewFactory sharedFactory] objectIsTextMarkerRange:parameter])
1228         textMarkerRange = (WebCoreTextMarkerRange*) parameter;
1229
1230     else if ([parameter isKindOfClass:[AccessibilityObjectWrapper self]])
1231         uiElement = [(AccessibilityObjectWrapper*)parameter accessibilityObject];
1232
1233     else if ([parameter isKindOfClass:[NSNumber self]])
1234         number = parameter;
1235
1236     else if ([parameter isKindOfClass:[NSArray self]])
1237         array = parameter;
1238
1239     else if ([parameter isKindOfClass:[NSValue self]] && strcmp([(NSValue*)parameter objCType], @encode(NSPoint)) == 0) {
1240         pointSet = true;
1241         point = [(NSValue*)parameter pointValue];
1242
1243     } else if ([parameter isKindOfClass:[NSValue self]] && strcmp([(NSValue*)parameter objCType], @encode(NSRange)) == 0) {
1244         rangeSet = true;
1245         range = [(NSValue*)parameter rangeValue];
1246
1247     } else {
1248         // got a parameter of a type we never use
1249         // NOTE: No ASSERT_NOT_REACHED because this can happen accidentally
1250         // while using accesstool (e.g.), forcing you to start over
1251         return nil;
1252     }
1253     
1254     // Convert values to WebCore types
1255     // FIXME: prepping all of these values as WebCore types is unnecessary in many 
1256     // cases. Re-organization of this function or performing the conversion on a 
1257     // need basis are possible improvements. 
1258     VisiblePosition visiblePos;
1259     if (textMarker)
1260         visiblePos = visiblePositionForTextMarker(textMarker);
1261     int intNumber = [number intValue];
1262     VisiblePositionRange visiblePosRange;
1263     if (textMarkerRange)
1264         visiblePosRange = [self visiblePositionRangeForTextMarkerRange:textMarkerRange];
1265     IntPoint webCorePoint = IntPoint(point);
1266     PlainTextRange plainTextRange = PlainTextRange(range.location, range.length);
1267
1268     // dispatch
1269     if ([attribute isEqualToString: @"AXUIElementForTextMarker"])
1270         return m_object->doAXUIElementForTextMarker(visiblePos)->wrapper();
1271
1272     if ([attribute isEqualToString: @"AXTextMarkerRangeForUIElement"]) {
1273         VisiblePositionRange vpRange = uiElement.get()->visiblePositionRange();
1274         return (id)textMarkerRangeFromVisiblePositions(vpRange.start, vpRange.end);
1275     }
1276
1277     if ([attribute isEqualToString: @"AXLineForTextMarker"])
1278         return [NSNumber numberWithUnsignedInt:m_object->doAXLineForTextMarker(visiblePos)];
1279
1280     if ([attribute isEqualToString: @"AXTextMarkerRangeForLine"]) {
1281         VisiblePositionRange vpRange = m_object->doAXTextMarkerRangeForLine(intNumber);
1282         return (id)textMarkerRangeFromVisiblePositions(vpRange.start, vpRange.end);
1283     }
1284
1285     if ([attribute isEqualToString: @"AXStringForTextMarkerRange"])
1286         return m_object->doAXStringForTextMarkerRange(visiblePosRange);
1287
1288     if ([attribute isEqualToString: @"AXTextMarkerForPosition"])
1289         return pointSet ? textMarkerForVisiblePosition(m_object->doAXTextMarkerForPosition(webCorePoint)) : nil;
1290
1291     if ([attribute isEqualToString: @"AXBoundsForTextMarkerRange"]) {
1292         NSRect rect = m_object->doAXBoundsForTextMarkerRange(visiblePosRange);
1293         return [NSValue valueWithRect:rect];
1294     }
1295
1296     if ([attribute isEqualToString: @"AXAttributedStringForTextMarkerRange"])
1297         return [self doAXAttributedStringForTextMarkerRange:textMarkerRange];
1298
1299     if ([attribute isEqualToString: @"AXTextMarkerRangeForUnorderedTextMarkers"]) {
1300         if ([array count] < 2)
1301             return nil;
1302
1303         WebCoreTextMarker* textMarker1 = (WebCoreTextMarker*) [array objectAtIndex:0];
1304         WebCoreTextMarker* textMarker2 = (WebCoreTextMarker*) [array objectAtIndex:1];
1305         if (![[WebCoreViewFactory sharedFactory] objectIsTextMarker:textMarker1] 
1306             || ![[WebCoreViewFactory sharedFactory] objectIsTextMarker:textMarker2])
1307             return nil;
1308
1309         VisiblePosition visiblePos1 = visiblePositionForTextMarker(textMarker1);
1310         VisiblePosition visiblePos2 = visiblePositionForTextMarker(textMarker2);
1311         VisiblePositionRange vpRange = m_object->doAXTextMarkerRangeForUnorderedTextMarkers(visiblePos1, visiblePos2);
1312         return (id)textMarkerRangeFromVisiblePositions(vpRange.start, vpRange.end);
1313     }
1314
1315     if ([attribute isEqualToString: @"AXNextTextMarkerForTextMarker"])
1316         return textMarkerForVisiblePosition(m_object->doAXNextTextMarkerForTextMarker(visiblePos));
1317
1318     if ([attribute isEqualToString: @"AXPreviousTextMarkerForTextMarker"])
1319         return textMarkerForVisiblePosition(m_object->doAXPreviousTextMarkerForTextMarker(visiblePos));
1320
1321     if ([attribute isEqualToString: @"AXLeftWordTextMarkerRangeForTextMarker"]) {
1322         VisiblePositionRange vpRange = m_object->doAXLeftWordTextMarkerRangeForTextMarker(visiblePos);
1323         return (id)textMarkerRangeFromVisiblePositions(vpRange.start, vpRange.end);
1324     }
1325
1326     if ([attribute isEqualToString: @"AXRightWordTextMarkerRangeForTextMarker"]) {
1327         VisiblePositionRange vpRange = m_object->doAXRightWordTextMarkerRangeForTextMarker(visiblePos);
1328         return (id)textMarkerRangeFromVisiblePositions(vpRange.start, vpRange.end);
1329     }
1330
1331     if ([attribute isEqualToString: @"AXLeftLineTextMarkerRangeForTextMarker"]) {
1332         VisiblePositionRange vpRange = m_object->doAXLeftLineTextMarkerRangeForTextMarker(visiblePos);
1333         return (id)textMarkerRangeFromVisiblePositions(vpRange.start, vpRange.end);
1334     }
1335
1336     if ([attribute isEqualToString: @"AXRightLineTextMarkerRangeForTextMarker"]) {
1337         VisiblePositionRange vpRange = m_object->doAXRightLineTextMarkerRangeForTextMarker(visiblePos);
1338         return (id)textMarkerRangeFromVisiblePositions(vpRange.start, vpRange.end);
1339     }
1340
1341     if ([attribute isEqualToString: @"AXSentenceTextMarkerRangeForTextMarker"]) {
1342         VisiblePositionRange vpRange = m_object->doAXSentenceTextMarkerRangeForTextMarker(visiblePos);
1343         return (id)textMarkerRangeFromVisiblePositions(vpRange.start, vpRange.end);
1344     }
1345
1346     if ([attribute isEqualToString: @"AXParagraphTextMarkerRangeForTextMarker"]) {
1347         VisiblePositionRange vpRange = m_object->doAXParagraphTextMarkerRangeForTextMarker(visiblePos);
1348         return (id)textMarkerRangeFromVisiblePositions(vpRange.start, vpRange.end);
1349     }
1350
1351     if ([attribute isEqualToString: @"AXNextWordEndTextMarkerForTextMarker"])
1352         return textMarkerForVisiblePosition(m_object->doAXNextWordEndTextMarkerForTextMarker(visiblePos));
1353
1354     if ([attribute isEqualToString: @"AXPreviousWordStartTextMarkerForTextMarker"])
1355         return textMarkerForVisiblePosition(m_object->doAXPreviousWordStartTextMarkerForTextMarker(visiblePos));
1356
1357     if ([attribute isEqualToString: @"AXNextLineEndTextMarkerForTextMarker"])
1358         return textMarkerForVisiblePosition(m_object->doAXNextLineEndTextMarkerForTextMarker(visiblePos));
1359
1360     if ([attribute isEqualToString: @"AXPreviousLineStartTextMarkerForTextMarker"])
1361         return textMarkerForVisiblePosition(m_object->doAXPreviousLineStartTextMarkerForTextMarker(visiblePos));
1362
1363     if ([attribute isEqualToString: @"AXNextSentenceEndTextMarkerForTextMarker"])
1364         return textMarkerForVisiblePosition(m_object->doAXNextSentenceEndTextMarkerForTextMarker(visiblePos));
1365
1366     if ([attribute isEqualToString: @"AXPreviousSentenceStartTextMarkerForTextMarker"])
1367         return textMarkerForVisiblePosition(m_object->doAXPreviousSentenceStartTextMarkerForTextMarker(visiblePos));
1368
1369     if ([attribute isEqualToString: @"AXNextParagraphEndTextMarkerForTextMarker"])
1370         return textMarkerForVisiblePosition(m_object->doAXNextParagraphEndTextMarkerForTextMarker(visiblePos));
1371
1372     if ([attribute isEqualToString: @"AXPreviousParagraphStartTextMarkerForTextMarker"])
1373         return textMarkerForVisiblePosition(m_object->doAXPreviousParagraphStartTextMarkerForTextMarker(visiblePos));
1374
1375     if ([attribute isEqualToString: @"AXStyleTextMarkerRangeForTextMarker"]) {
1376         VisiblePositionRange vpRange = m_object->doAXStyleTextMarkerRangeForTextMarker(visiblePos);
1377         return (id)textMarkerRangeFromVisiblePositions(vpRange.start, vpRange.end);
1378     }
1379
1380     if ([attribute isEqualToString: @"AXLengthForTextMarkerRange"]) {
1381         int length = m_object->doAXLengthForTextMarkerRange(visiblePosRange);
1382         if (length < 0)
1383             return nil;
1384         return [NSNumber numberWithInt:length];
1385     }
1386
1387     if (m_object->isTextControl()) {
1388         if ([attribute isEqualToString: (NSString*)kAXLineForIndexParameterizedAttribute])
1389             return [NSNumber numberWithUnsignedInt: m_object->doAXLineForIndex(intNumber)];
1390
1391         if ([attribute isEqualToString: (NSString*)kAXRangeForLineParameterizedAttribute]) {
1392             PlainTextRange textRange = m_object->doAXRangeForLine(intNumber);
1393             return [NSValue valueWithRange: NSMakeRange(textRange.start, textRange.length)];
1394         }
1395
1396         if ([attribute isEqualToString: (NSString*)kAXStringForRangeParameterizedAttribute])
1397             return rangeSet ? (id)(m_object->doAXStringForRange(plainTextRange)) : nil;
1398
1399         if ([attribute isEqualToString: (NSString*)kAXRangeForPositionParameterizedAttribute]) {
1400             if (!pointSet)
1401                 return nil;
1402             PlainTextRange textRange = m_object->doAXRangeForPosition(webCorePoint);
1403             return [NSValue valueWithRange: NSMakeRange(textRange.start, textRange.length)];
1404         }
1405
1406         if ([attribute isEqualToString: (NSString*)kAXRangeForIndexParameterizedAttribute]) {
1407             PlainTextRange textRange = m_object->doAXRangeForIndex(intNumber);
1408             return [NSValue valueWithRange: NSMakeRange(textRange.start, textRange.length)];
1409         }
1410
1411         if ([attribute isEqualToString: (NSString*)kAXBoundsForRangeParameterizedAttribute]) {
1412             if (!rangeSet)
1413                 return nil;
1414             NSRect rect = m_object->doAXBoundsForRange(plainTextRange);
1415             return [NSValue valueWithRect:rect];
1416         }
1417
1418         if ([attribute isEqualToString: (NSString*)kAXRTFForRangeParameterizedAttribute])
1419             return rangeSet ? [self doAXRTFForRange:range] : nil;
1420
1421         if ([attribute isEqualToString: (NSString*)kAXAttributedStringForRangeParameterizedAttribute])
1422             return rangeSet ? [self doAXAttributedStringForRange:range] : nil;
1423
1424         if ([attribute isEqualToString: (NSString*)kAXStyleRangeForIndexParameterizedAttribute]) {
1425             PlainTextRange textRange = m_object->doAXStyleRangeForIndex(intNumber);
1426             return [NSValue valueWithRange: NSMakeRange(textRange.start, textRange.length)];
1427         }
1428     }
1429
1430     return nil;
1431 }
1432
1433 - (BOOL)accessibilityShouldUseUniqueId
1434 {
1435     return m_object->accessibilityShouldUseUniqueId();
1436 }
1437
1438 @end