AccessibilityObjectWrapper.mm 121 KB
Newer Older
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1
/*
2
 * Copyright (C) 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
alice.liu@apple.com's avatar
alice.liu@apple.com committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
28

alice.liu@apple.com's avatar
alice.liu@apple.com committed
29 30 31
#import "config.h"
#import "AccessibilityObjectWrapper.h"

32 33
#if HAVE(ACCESSIBILITY)

alice.liu@apple.com's avatar
alice.liu@apple.com committed
34
#import "AXObjectCache.h"
35
#import "AccessibilityARIAGridRow.h"
36
#import "AccessibilityListBox.h"
37
#import "AccessibilityList.h"
38
#import "AccessibilityRenderObject.h"
39
#import "AccessibilityScrollView.h"
40 41 42 43
#import "AccessibilityTable.h"
#import "AccessibilityTableCell.h"
#import "AccessibilityTableRow.h"
#import "AccessibilityTableColumn.h"
44
#import "Chrome.h"
45
#import "ColorMac.h"
alice.liu@apple.com's avatar
alice.liu@apple.com committed
46
#import "Frame.h"
47
#import "FrameLoaderClient.h"
48
#import "FrameSelection.h"
alice.liu@apple.com's avatar
alice.liu@apple.com committed
49
#import "HTMLAnchorElement.h"
50
#import "HTMLAreaElement.h"
51
#import "HTMLFrameOwnerElement.h"
alice.liu@apple.com's avatar
alice.liu@apple.com committed
52 53
#import "HTMLImageElement.h"
#import "HTMLInputElement.h"
54
#import "HTMLNames.h"
alice.liu@apple.com's avatar
alice.liu@apple.com committed
55
#import "HTMLTextAreaElement.h"
56
#import "LocalizedStrings.h"
alice.liu@apple.com's avatar
alice.liu@apple.com committed
57 58 59
#import "RenderTextControl.h"
#import "RenderView.h"
#import "RenderWidget.h"
60
#import "ScrollView.h"
61
#import "SimpleFontData.h"
62
#import "TextCheckerClient.h"
alice.liu@apple.com's avatar
alice.liu@apple.com committed
63 64 65
#import "TextIterator.h"
#import "WebCoreFrameView.h"
#import "WebCoreObjCExtras.h"
66
#import "WebCoreSystemInterface.h"
alice.liu@apple.com's avatar
alice.liu@apple.com committed
67 68 69 70 71
#import "htmlediting.h"
#import "visible_units.h"

using namespace WebCore;
using namespace HTMLNames;
72
using namespace std;
73 74

// Cell Tables
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#ifndef NSAccessibilitySelectedCellsAttribute
#define NSAccessibilitySelectedCellsAttribute @"AXSelectedCells"
#endif

#ifndef NSAccessibilityVisibleCellsAttribute
#define NSAccessibilityVisibleCellsAttribute @"AXVisibleCells"
#endif

#ifndef NSAccessibilityRowHeaderUIElementsAttribute
#define NSAccessibilityRowHeaderUIElementsAttribute @"AXRowHeaderUIElements"
#endif

#ifndef NSAccessibilityRowIndexRangeAttribute
#define NSAccessibilityRowIndexRangeAttribute @"AXRowIndexRange"
#endif

#ifndef NSAccessibilityColumnIndexRangeAttribute
#define NSAccessibilityColumnIndexRangeAttribute @"AXColumnIndexRange"
#endif

#ifndef NSAccessibilityCellForColumnAndRowParameterizedAttribute
#define NSAccessibilityCellForColumnAndRowParameterizedAttribute @"AXCellForColumnAndRow"
#endif

#ifndef NSAccessibilityCellRole
#define NSAccessibilityCellRole @"AXCell"
#endif

103 104 105 106 107 108 109 110 111
// Lists
#ifndef NSAccessibilityContentListSubrole
#define NSAccessibilityContentListSubrole @"AXContentList"
#endif

#ifndef NSAccessibilityDefinitionListSubrole
#define NSAccessibilityDefinitionListSubrole @"AXDefinitionList"
#endif

112 113 114 115 116
// Miscellaneous
#ifndef NSAccessibilityBlockQuoteLevelAttribute
#define NSAccessibilityBlockQuoteLevelAttribute @"AXBlockQuoteLevel"
#endif

117 118 119 120
#ifndef NSAccessibilityAccessKeyAttribute
#define NSAccessibilityAccessKeyAttribute @"AXAccessKey"
#endif

121 122 123 124
#ifndef NSAccessibilityLanguageAttribute
#define NSAccessibilityLanguageAttribute @"AXLanguage"
#endif

125 126 127 128
#ifndef NSAccessibilityRequiredAttribute
#define NSAccessibilityRequiredAttribute @"AXRequired"
#endif

129 130 131 132
#ifndef NSAccessibilityInvalidAttribute
#define NSAccessibilityInvalidAttribute @"AXInvalid"
#endif

133 134 135 136
#ifndef NSAccessibilityOwnsAttribute
#define NSAccessibilityOwnsAttribute @"AXOwns"
#endif

137 138 139 140 141 142 143 144
#ifndef NSAccessibilityGrabbedAttribute
#define NSAccessibilityGrabbedAttribute @"AXGrabbed"
#endif

#ifndef NSAccessibilityDropEffectsAttribute
#define NSAccessibilityDropEffectsAttribute @"AXDropEffects"
#endif

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#ifndef NSAccessibilityARIALiveAttribute
#define NSAccessibilityARIALiveAttribute @"AXARIALive"
#endif

#ifndef NSAccessibilityARIAAtomicAttribute
#define NSAccessibilityARIAAtomicAttribute @"AXARIAAtomic"
#endif

#ifndef NSAccessibilityARIARelevantAttribute
#define NSAccessibilityARIARelevantAttribute @"AXARIARelevant"
#endif

#ifndef NSAccessibilityARIABusyAttribute
#define NSAccessibilityARIABusyAttribute @"AXARIABusy"
#endif

161 162 163 164
#ifndef NSAccessibilityLoadingProgressAttribute
#define NSAccessibilityLoadingProgressAttribute @"AXLoadingProgress"
#endif

165 166 167 168
#ifndef NSAccessibilityHasPopupAttribute
#define NSAccessibilityHasPopupAttribute @"AXHasPopup"
#endif

169 170 171 172
#ifndef NSAccessibilityPlaceholderValueAttribute
#define NSAccessibilityPlaceholderValueAttribute @"AXPlaceholderValue"
#endif

cfleizach@apple.com's avatar
cfleizach@apple.com committed
173

174 175 176 177 178 179 180 181
@interface NSObject (WebKitAccessibilityArrayCategory)

- (NSUInteger)accessibilityIndexOfChild:(id)child;
- (NSUInteger)accessibilityArrayAttributeCount:(NSString *)attribute;
- (NSArray *)accessibilityArrayAttributeValues:(NSString *)attribute index:(NSUInteger)index maxCount:(NSUInteger)maxCount;

@end

alice.liu@apple.com's avatar
alice.liu@apple.com committed
182 183 184 185 186 187 188 189 190 191 192 193
@implementation AccessibilityObjectWrapper

- (id)initWithAccessibilityObject:(AccessibilityObject*)axObject
{
    [super init];

    m_object = axObject;
    return self;
}

- (void)unregisterUniqueIdForUIElement
{
194
    wkUnregisterUniqueIdForElement(self);
alice.liu@apple.com's avatar
alice.liu@apple.com committed
195 196 197 198 199
}

- (void)detach
{
    // Send unregisterUniqueIdForUIElement unconditionally because if it is
200
    // ever accidentally not done (via other bugs in our AX implementation) you
alice.liu@apple.com's avatar
alice.liu@apple.com committed
201 202 203 204 205 206
    // end up with a crash like <rdar://problem/4273149>.  It is safe and not
    // expensive to send even if the object is not registered.
    [self unregisterUniqueIdForUIElement];
    m_object = 0;
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
- (BOOL)updateObjectBackingStore
{
    // Calling updateBackingStore() can invalidate this element so self must be retained.
    // If it does become invalidated, m_object will be nil.
    [[self retain] autorelease];
    
    if (!m_object)
        return NO;
    
    m_object->updateBackingStore();
    if (!m_object)
        return NO;
    
    return YES;
}

alice.liu@apple.com's avatar
alice.liu@apple.com committed
223 224 225 226 227
- (AccessibilityObject*)accessibilityObject
{
    return m_object;
}

228 229 230 231 232 233
- (NSView*)attachmentView
{
    ASSERT(m_object->isAttachment());
    Widget* widget = m_object->widgetForAttachmentView();
    if (!widget)
        return nil;
234
    return NSAccessibilityUnignoredDescendant(widget->platformWidget());
235 236
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
#pragma mark SystemInterface wrappers

static inline id CFAutoreleaseHelper(CFTypeRef obj)
{
    if (obj)
        CFMakeCollectable(obj);
    [(id)obj autorelease];
    return (id)obj;
}

static inline BOOL AXObjectIsTextMarker(id obj)
{
    return obj != nil && CFGetTypeID(obj) == wkGetAXTextMarkerTypeID();    
}

static inline BOOL AXObjectIsTextMarkerRange(id obj)
{
    return obj != nil && CFGetTypeID(obj) == wkGetAXTextMarkerRangeTypeID();    
}

static id AXTextMarkerRange(id startMarker, id endMarker)
{
    ASSERT(startMarker != nil);
    ASSERT(endMarker != nil);
    ASSERT(CFGetTypeID(startMarker) == wkGetAXTextMarkerTypeID());
    ASSERT(CFGetTypeID(endMarker) == wkGetAXTextMarkerTypeID());
    return CFAutoreleaseHelper(wkCreateAXTextMarkerRange((CFTypeRef)startMarker, (CFTypeRef)endMarker));
}

static id AXTextMarkerRangeStart(id range)
{
    ASSERT(range != nil);
    ASSERT(CFGetTypeID(range) == wkGetAXTextMarkerRangeTypeID());
    return CFAutoreleaseHelper(wkCopyAXTextMarkerRangeStart(range));    
}

static id AXTextMarkerRangeEnd(id range)
{
    ASSERT(range != nil);
    ASSERT(CFGetTypeID(range) == wkGetAXTextMarkerRangeTypeID());
    return CFAutoreleaseHelper(wkCopyAXTextMarkerRangeEnd(range));    
}

#pragma mark Text Marker helpers

static id textMarkerForVisiblePosition(AXObjectCache* cache, const VisiblePosition& visiblePos)
283
{
284 285
    ASSERT(cache);
    
286
    TextMarkerData textMarkerData;
287
    cache->textMarkerDataForVisiblePosition(textMarkerData, visiblePos);
288 289
    if (!textMarkerData.axID)
        return nil;
290
    
291
    return CFAutoreleaseHelper(wkCreateAXTextMarker(&textMarkerData, sizeof(textMarkerData)));
292 293
}

294
- (id)textMarkerForVisiblePosition:(const VisiblePosition &)visiblePos
295 296 297 298
{
    return textMarkerForVisiblePosition(m_object->axObjectCache(), visiblePos);
}

299
static VisiblePosition visiblePositionForTextMarker(AXObjectCache* cache, CFTypeRef textMarker)
300
{
301 302
    ASSERT(cache);

303 304
    if (!textMarker)
        return VisiblePosition();
305
    TextMarkerData textMarkerData;
306
    if (!wkGetBytesFromAXTextMarker(textMarker, &textMarkerData, sizeof(textMarkerData)))
307
        return VisiblePosition();
308
    
309
    return cache->visiblePositionForTextMarkerData(textMarkerData);
310 311
}

312
- (VisiblePosition)visiblePositionForTextMarker:(id)textMarker
313
{
314
    return visiblePositionForTextMarker(m_object->axObjectCache(), textMarker);
315 316
}

317
static VisiblePosition visiblePositionForStartOfTextMarkerRange(AXObjectCache *cache, id textMarkerRange)
318
{
319
    return visiblePositionForTextMarker(cache, AXTextMarkerRangeStart(textMarkerRange));
320 321
}

322
static VisiblePosition visiblePositionForEndOfTextMarkerRange(AXObjectCache *cache, id textMarkerRange)
323
{
324
    return visiblePositionForTextMarker(cache, AXTextMarkerRangeEnd(textMarkerRange));
325 326
}

327
static id textMarkerRangeFromMarkers(id textMarker1, id textMarker2)
328
{
329 330 331
    if (!textMarker1 || !textMarker2)
        return nil;
        
332
    return AXTextMarkerRange(textMarker1, textMarker2);
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
}

static void AXAttributeStringSetFont(NSMutableAttributedString* attrString, NSString* attribute, NSFont* font, NSRange range)
{
    NSDictionary* dict;
    
    if (font) {
        dict = [NSDictionary dictionaryWithObjectsAndKeys:
            [font fontName]                             , NSAccessibilityFontNameKey,
            [font familyName]                           , NSAccessibilityFontFamilyKey,
            [font displayName]                          , NSAccessibilityVisibleNameKey,
            [NSNumber numberWithFloat:[font pointSize]] , NSAccessibilityFontSizeKey,
        nil];

        [attrString addAttribute:attribute value:dict range:range];
    } else
        [attrString removeAttribute:attribute range:range];
    
}

static CGColorRef CreateCGColorIfDifferent(NSColor* nsColor, CGColorRef existingColor)
{
    // get color information assuming NSDeviceRGBColorSpace 
    NSColor* rgbColor = [nsColor colorUsingColorSpaceName:NSDeviceRGBColorSpace];
    if (rgbColor == nil)
        rgbColor = [NSColor blackColor];
    CGFloat components[4];
    [rgbColor getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]];
    
    // create a new CGColorRef to return
363
    CGColorSpaceRef cgColorSpace = CGColorSpaceCreateDeviceRGB();
364 365 366 367
    CGColorRef cgColor = CGColorCreate(cgColorSpace, components);
    CGColorSpaceRelease(cgColorSpace);
    
    // check for match with existing color
368 369 370 371 372
    if (existingColor && CGColorEqualToColor(cgColor, existingColor)) {
        CGColorRelease(cgColor);
        cgColor = 0;
    }
    
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    return cgColor;
}

static void AXAttributeStringSetColor(NSMutableAttributedString* attrString, NSString* attribute, NSColor* color, NSRange range)
{
    if (color) {
        CGColorRef existingColor = (CGColorRef) [attrString attribute:attribute atIndex:range.location effectiveRange:nil];
        CGColorRef cgColor = CreateCGColorIfDifferent(color, existingColor);
        if (cgColor) {
            [attrString addAttribute:attribute value:(id)cgColor range:range];
            CGColorRelease(cgColor);
        }
    } else
        [attrString removeAttribute:attribute range:range];
}

static void AXAttributeStringSetNumber(NSMutableAttributedString* attrString, NSString* attribute, NSNumber* number, NSRange range)
{
    if (number)
        [attrString addAttribute:attribute value:number range:range];
    else
        [attrString removeAttribute:attribute range:range];
}

static void AXAttributeStringSetStyle(NSMutableAttributedString* attrString, RenderObject* renderer, NSRange range)
{
    RenderStyle* style = renderer->style();

    // set basic font info
    AXAttributeStringSetFont(attrString, NSAccessibilityFontTextAttribute, style->font().primaryFont()->getNSFont(), range);

    // set basic colors
405 406
    AXAttributeStringSetColor(attrString, NSAccessibilityForegroundColorTextAttribute, nsColor(style->visitedDependentColor(CSSPropertyColor)), range);
    AXAttributeStringSetColor(attrString, NSAccessibilityBackgroundColorTextAttribute, nsColor(style->visitedDependentColor(CSSPropertyBackgroundColor)), range);
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

    // set super/sub scripting
    EVerticalAlign alignment = style->verticalAlign();
    if (alignment == SUB)
        AXAttributeStringSetNumber(attrString, NSAccessibilitySuperscriptTextAttribute, [NSNumber numberWithInt:(-1)], range);
    else if (alignment == SUPER)
        AXAttributeStringSetNumber(attrString, NSAccessibilitySuperscriptTextAttribute, [NSNumber numberWithInt:1], range);
    else
        [attrString removeAttribute:NSAccessibilitySuperscriptTextAttribute range:range];
    
    // set shadow
    if (style->textShadow())
        AXAttributeStringSetNumber(attrString, NSAccessibilityShadowTextAttribute, [NSNumber numberWithBool:YES], range);
    else
        [attrString removeAttribute:NSAccessibilityShadowTextAttribute range:range];
    
    // set underline and strikethrough
    int decor = style->textDecorationsInEffect();
    if ((decor & UNDERLINE) == 0) {
        [attrString removeAttribute:NSAccessibilityUnderlineTextAttribute range:range];
        [attrString removeAttribute:NSAccessibilityUnderlineColorTextAttribute range:range];
    }
    
    if ((decor & LINE_THROUGH) == 0) {
        [attrString removeAttribute:NSAccessibilityStrikethroughTextAttribute range:range];
        [attrString removeAttribute:NSAccessibilityStrikethroughColorTextAttribute range:range];
    }

    if ((decor & (UNDERLINE | LINE_THROUGH)) != 0) {
        // find colors using quirk mode approach (strict mode would use current
        // color for all but the root line box, which would use getTextDecorationColors)
        Color underline, overline, linethrough;
        renderer->getTextDecorationColors(decor, underline, overline, linethrough);
        
        if ((decor & UNDERLINE) != 0) {
            AXAttributeStringSetNumber(attrString, NSAccessibilityUnderlineTextAttribute, [NSNumber numberWithBool:YES], range);
            AXAttributeStringSetColor(attrString, NSAccessibilityUnderlineColorTextAttribute, nsColor(underline), range);
        }

        if ((decor & LINE_THROUGH) != 0) {
            AXAttributeStringSetNumber(attrString, NSAccessibilityStrikethroughTextAttribute, [NSNumber numberWithBool:YES], range);
            AXAttributeStringSetColor(attrString, NSAccessibilityStrikethroughColorTextAttribute, nsColor(linethrough), range);
        }
    }
}

static int blockquoteLevel(RenderObject* renderer)
{
455 456 457
    if (!renderer)
        return 0;
    
458
    int result = 0;
459
    for (Node* node = renderer->node(); node; node = node->parentNode()) {
460 461 462 463 464 465 466 467 468 469 470 471
        if (node->hasTagName(blockquoteTag))
            result += 1;
    }
    
    return result;
}

static void AXAttributeStringSetBlockquoteLevel(NSMutableAttributedString* attrString, RenderObject* renderer, NSRange range)
{
    int quoteLevel = blockquoteLevel(renderer);
    
    if (quoteLevel)
472
        [attrString addAttribute:NSAccessibilityBlockQuoteLevelAttribute value:[NSNumber numberWithInt:quoteLevel] range:range];
473
    else
474
        [attrString removeAttribute:NSAccessibilityBlockQuoteLevelAttribute range:range];
475 476
}

477
static void AXAttributeStringSetSpelling(NSMutableAttributedString* attrString, Node* node, const UChar* chars, int charLength, NSRange range)
478
{
479
#if USE(UNIFIED_TEXT_CHECKING)
480
    // Check the spelling directly since document->markersForNode() does not store the misspelled marking when the cursor is in a word.
481 482 483 484 485 486 487 488 489 490 491
    TextCheckerClient* checker = node->document()->frame()->editor()->textChecker();

    // checkTextOfParagraph is the only spelling/grammar checker implemented in WK1 and WK2
    Vector<TextCheckingResult> results;
    checker->checkTextOfParagraph(chars, charLength, TextCheckingTypeSpelling, results);
    
    size_t size = results.size();
    NSNumber* trueValue = [NSNumber numberWithBool:YES];
    for (unsigned i = 0; i < size; i++) {
        const TextCheckingResult& result = results[i];
        AXAttributeStringSetNumber(attrString, NSAccessibilityMisspelledTextAttribute, trueValue, NSMakeRange(result.location + range.location, result.length));
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    }    
#else
    int currentPosition = 0;
    while (charLength > 0) {
        const UChar* charData = chars + currentPosition;
        TextCheckerClient* checker = node->document()->frame()->editor()->textChecker();
        
        int misspellingLocation = -1;
        int misspellingLength = 0;
        checker->checkSpellingOfString(charData, charLength, &misspellingLocation, &misspellingLength);
        if (misspellingLocation == -1 || !misspellingLength)
            break;
        
        NSRange spellRange = NSMakeRange(range.location + currentPosition + misspellingLocation, misspellingLength);
        AXAttributeStringSetNumber(attrString, NSAccessibilityMisspelledTextAttribute, [NSNumber numberWithBool:YES], spellRange);
        charLength -= (misspellingLocation + misspellingLength);
        currentPosition += (misspellingLocation + misspellingLength);
509
    }
510
#endif    
511 512
}

513
static void AXAttributeStringSetHeadingLevel(NSMutableAttributedString* attrString, RenderObject* renderer, NSRange range)
514
{
515 516 517 518 519
    if (!renderer)
        return;
    
    AccessibilityObject* parentObject = renderer->document()->axObjectCache()->getOrCreate(renderer->parent());
    int parentHeadingLevel = parentObject->headingLevel();
520 521 522 523 524 525 526 527 528
    
    if (parentHeadingLevel)
        [attrString addAttribute:@"AXHeadingLevel" value:[NSNumber numberWithInt:parentHeadingLevel] range:range];
    else
        [attrString removeAttribute:@"AXHeadingLevel" range:range];
}

static void AXAttributeStringSetElement(NSMutableAttributedString* attrString, NSString* attribute, AccessibilityObject* object, NSRange range)
{
529
    if (object && object->isAccessibilityRenderObject()) {
530
        // make a serializable AX object
531 532
        
        RenderObject* renderer = static_cast<AccessibilityRenderObject*>(object)->renderer();
533 534 535 536 537 538 539 540 541 542 543
        if (!renderer)
            return;
        
        Document* doc = renderer->document();
        if (!doc)
            return;
        
        AXObjectCache* cache = doc->axObjectCache();
        if (!cache)
            return;

544
        AXUIElementRef axElement = wkCreateAXUIElementRef(object->wrapper());
545 546 547 548 549 550 551 552
        if (axElement) {
            [attrString addAttribute:attribute value:(id)axElement range:range];
            CFRelease(axElement);
        }
    } else
        [attrString removeAttribute:attribute range:range];
}

553
static void AXAttributedStringAppendText(NSMutableAttributedString* attrString, Node* node, const UChar* chars, int length)
554 555 556 557
{
    // skip invisible text
    if (!node->renderer())
        return;
558

559 560 561 562 563 564 565 566 567 568 569 570 571
    // easier to calculate the range before appending the string
    NSRange attrStringRange = NSMakeRange([attrString length], length);
    
    // append the string from this node
    [[attrString mutableString] appendString:[NSString stringWithCharacters:chars length:length]];

    // add new attributes and remove irrelevant inherited ones
    // NOTE: color attributes are handled specially because -[NSMutableAttributedString addAttribute: value: range:] does not merge
    // identical colors.  Workaround is to not replace an existing color attribute if it matches what we are adding.  This also means
    // we cannot just pre-remove all inherited attributes on the appended string, so we have to remove the irrelevant ones individually.

    // remove inherited attachment from prior AXAttributedStringAppendReplaced
    [attrString removeAttribute:NSAccessibilityAttachmentTextAttribute range:attrStringRange];
572
    [attrString removeAttribute:NSAccessibilityMisspelledTextAttribute range:attrStringRange];
573 574 575
    
    // set new attributes
    AXAttributeStringSetStyle(attrString, node->renderer(), attrStringRange);
576
    AXAttributeStringSetHeadingLevel(attrString, node->renderer(), attrStringRange);
577
    AXAttributeStringSetBlockquoteLevel(attrString, node->renderer(), attrStringRange);
578
    AXAttributeStringSetElement(attrString, NSAccessibilityLinkTextAttribute, AccessibilityObject::anchorElementForNode(node), attrStringRange);
579 580
    
    // do spelling last because it tends to break up the range
581
    AXAttributeStringSetSpelling(attrString, node, chars, length, attrStringRange);
582 583 584 585 586 587 588 589 590 591 592 593
}

static NSString* nsStringForReplacedNode(Node* replacedNode)
{
    // we should always be given a rendered node and a replaced node, but be safe
    // replaced nodes are either attachments (widgets) or images
    if (!replacedNode || !replacedNode->renderer() || !replacedNode->renderer()->isReplaced() || replacedNode->isTextNode()) {
        ASSERT_NOT_REACHED();
        return nil;
    }

    // create an AX object, but skip it if it is not supposed to be seen
594
    RefPtr<AccessibilityObject> obj = replacedNode->renderer()->document()->axObjectCache()->getOrCreate(replacedNode->renderer());
595 596 597 598 599 600 601 602
    if (obj->accessibilityIsIgnored())
        return nil;
    
    // use the attachmentCharacter to represent the replaced node
    const UniChar attachmentChar = NSAttachmentCharacter;
    return [NSString stringWithCharacters:&attachmentChar length:1];
}

603
- (NSAttributedString*)doAXAttributedStringForTextMarkerRange:(id)textMarkerRange
604
{
605 606 607
    if (!m_object)
        return nil;
    
608
    // extract the start and end VisiblePosition
609
    VisiblePosition startVisiblePosition = visiblePositionForStartOfTextMarkerRange(m_object->axObjectCache(), textMarkerRange);
610 611 612
    if (startVisiblePosition.isNull())
        return nil;

613
    VisiblePosition endVisiblePosition = visiblePositionForEndOfTextMarkerRange(m_object->axObjectCache(), textMarkerRange);
614 615 616
    if (endVisiblePosition.isNull())
        return nil;

617
    VisiblePositionRange visiblePositionRange(startVisiblePosition, endVisiblePosition);
618 619 620 621 622 623 624 625 626 627 628 629
    // iterate over the range to build the AX attributed string
    NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] init];
    TextIterator it(makeRange(startVisiblePosition, endVisiblePosition).get());
    while (!it.atEnd()) {
        // locate the node and starting offset for this range
        int exception = 0;
        Node* node = it.range()->startContainer(exception);
        ASSERT(node == it.range()->endContainer(exception));
        int offset = it.range()->startOffset(exception);

        // non-zero length means textual node, zero length means replaced node (AKA "attachments" in AX)
        if (it.length() != 0) {
630 631 632
            // Add the text of the list marker item if necessary.
            String listMarkerText = m_object->listMarkerTextForNodeAndPosition(node, VisiblePosition(it.range()->startPosition()));
            if (!listMarkerText.isEmpty())
633
                AXAttributedStringAppendText(attrString, node, listMarkerText.characters(), listMarkerText.length());
634
            
635
            AXAttributedStringAppendText(attrString, node, it.characters(), it.length());
636 637 638 639 640 641 642 643 644 645 646 647 648
        } else {
            Node* replacedNode = node->childNode(offset);
            NSString *attachmentString = nsStringForReplacedNode(replacedNode);
            if (attachmentString) {
                NSRange attrStringRange = NSMakeRange([attrString length], [attachmentString length]);

                // append the placeholder string
                [[attrString mutableString] appendString:attachmentString];

                // remove all inherited attributes
                [attrString setAttributes:nil range:attrStringRange];

                // add the attachment attribute
649
                AccessibilityObject* obj = replacedNode->renderer()->document()->axObjectCache()->getOrCreate(replacedNode->renderer());
650 651 652 653 654 655 656 657 658
                AXAttributeStringSetElement(attrString, NSAccessibilityAttachmentTextAttribute, obj, attrStringRange);
            }
        }
        it.advance();
    }

    return [attrString autorelease];
}

659
static id textMarkerRangeFromVisiblePositions(AXObjectCache *cache, VisiblePosition startPosition, VisiblePosition endPosition)
660
{
661 662
    id startTextMarker = textMarkerForVisiblePosition(cache, startPosition);
    id endTextMarker = textMarkerForVisiblePosition(cache, endPosition);
663 664 665
    return textMarkerRangeFromMarkers(startTextMarker, endTextMarker);
}

666
- (id)textMarkerRangeFromVisiblePositions:(VisiblePosition)startPosition endPosition:(VisiblePosition)endPosition
667 668 669 670
{
    return textMarkerRangeFromVisiblePositions(m_object->axObjectCache(), startPosition, endPosition);
}

alice.liu@apple.com's avatar
alice.liu@apple.com committed
671 672
- (NSArray*)accessibilityActionNames
{
673
    if (![self updateObjectBackingStore])
674
        return nil;
675

676 677
    static NSArray* actionElementActions = [[NSArray alloc] initWithObjects: NSAccessibilityPressAction, NSAccessibilityShowMenuAction, nil];
    static NSArray* defaultElementActions = [[NSArray alloc] initWithObjects: NSAccessibilityShowMenuAction, nil];
678
    static NSArray* menuElementActions = [[NSArray alloc] initWithObjects: NSAccessibilityCancelAction, NSAccessibilityPressAction, nil];
cfleizach@apple.com's avatar
cfleizach@apple.com committed
679 680
    static NSArray* sliderActions = [[NSArray alloc] initWithObjects: NSAccessibilityIncrementAction, NSAccessibilityDecrementAction, nil];

681 682 683
    NSArray *actions;
    if (m_object->actionElement()) 
        actions = actionElementActions;
684 685
    else if (m_object->isMenuRelated())
        actions = menuElementActions;
cfleizach@apple.com's avatar
cfleizach@apple.com committed
686 687
    else if (m_object->isSlider())
        actions = sliderActions;
688 689 690 691
    else if (m_object->isAttachment())
        actions = [[self attachmentView] accessibilityActionNames];
    else
        actions = defaultElementActions;
alice.liu@apple.com's avatar
alice.liu@apple.com committed
692 693 694 695

    return actions;
}

696 697 698 699 700 701 702 703 704
- (NSArray*)additionalAccessibilityAttributeNames
{
    if (!m_object)
        return nil;

    NSMutableArray *additional = [NSMutableArray array];
    if (m_object->supportsARIAOwns())
        [additional addObject:NSAccessibilityOwnsAttribute];

705 706 707
    if (m_object->supportsARIAExpanded())
        [additional addObject:NSAccessibilityExpandedAttribute];
    
708 709 710
    if (m_object->isScrollbar())
        [additional addObject:NSAccessibilityOrientationAttribute];
    
711 712 713 714 715 716
    if (m_object->supportsARIADragging())
        [additional addObject:NSAccessibilityGrabbedAttribute];

    if (m_object->supportsARIADropping())
        [additional addObject:NSAccessibilityDropEffectsAttribute];

717
    if (m_object->isAccessibilityTable() && static_cast<AccessibilityTable*>(m_object)->supportsSelectedRows())
718 719
        [additional addObject:NSAccessibilitySelectedRowsAttribute];        
    
720 721 722 723
    if (m_object->supportsARIALiveRegion()) {
        [additional addObject:NSAccessibilityARIALiveAttribute];
        [additional addObject:NSAccessibilityARIARelevantAttribute];
    }
724 725 726
    
    if (m_object->sortDirection() != SortDirectionNone)
        [additional addObject:NSAccessibilitySortDirectionAttribute];
727 728 729 730 731 732 733
        
    // If an object is a child of a live region, then add these
    if (m_object->isInsideARIALiveRegion()) {
        [additional addObject:NSAccessibilityARIAAtomicAttribute];
        [additional addObject:NSAccessibilityARIABusyAttribute];
    }
    
734 735 736
    if (m_object->ariaHasPopup())
        [additional addObject:NSAccessibilityHasPopupAttribute];
    
737 738 739
    return additional;
}

alice.liu@apple.com's avatar
alice.liu@apple.com committed
740 741
- (NSArray*)accessibilityAttributeNames
{
742
    if (![self updateObjectBackingStore])
743
        return nil;
744
    
alice.liu@apple.com's avatar
alice.liu@apple.com committed
745
    if (m_object->isAttachment())
746
        return [[self attachmentView] accessibilityAttributeNames];
747

alice.liu@apple.com's avatar
alice.liu@apple.com committed
748 749 750 751
    static NSArray* attributes = nil;
    static NSArray* anchorAttrs = nil;
    static NSArray* webAreaAttrs = nil;
    static NSArray* textAttrs = nil;
752
    static NSArray* listAttrs = nil;
753
    static NSArray* listBoxAttrs = nil;
bdakin@apple.com's avatar
bdakin@apple.com committed
754
    static NSArray* rangeAttrs = nil;
755 756 757 758 759
    static NSArray* commonMenuAttrs = nil;
    static NSArray* menuAttrs = nil;
    static NSArray* menuBarAttrs = nil;
    static NSArray* menuItemAttrs = nil;
    static NSArray* menuButtonAttrs = nil;
760
    static NSArray* controlAttrs = nil;
761 762 763 764
    static NSArray* tableAttrs = nil;
    static NSArray* tableRowAttrs = nil;
    static NSArray* tableColAttrs = nil;
    static NSArray* tableCellAttrs = nil;
765
    static NSArray* groupAttrs = nil;
766
    static NSArray* inputImageAttrs = nil;
767
    static NSArray* passwordFieldAttrs = nil;
768 769 770 771
    static NSArray* tabListAttrs = nil;
    static NSArray* comboBoxAttrs = nil;
    static NSArray* outlineAttrs = nil;
    static NSArray* outlineRowAttrs = nil;
772
    static NSArray* buttonAttrs = nil;
773
    static NSArray* scrollViewAttrs = nil;
alice.liu@apple.com's avatar
alice.liu@apple.com committed
774 775 776
    NSMutableArray* tempArray;
    if (attributes == nil) {
        attributes = [[NSArray alloc] initWithObjects: NSAccessibilityRoleAttribute,
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
                      NSAccessibilitySubroleAttribute,
                      NSAccessibilityRoleDescriptionAttribute,
                      NSAccessibilityChildrenAttribute,
                      NSAccessibilityHelpAttribute,
                      NSAccessibilityParentAttribute,
                      NSAccessibilityPositionAttribute,
                      NSAccessibilitySizeAttribute,
                      NSAccessibilityTitleAttribute,
                      NSAccessibilityDescriptionAttribute,
                      NSAccessibilityValueAttribute,
                      NSAccessibilityFocusedAttribute,
                      NSAccessibilityEnabledAttribute,
                      NSAccessibilityWindowAttribute,
                      @"AXSelectedTextMarkerRange",
                      @"AXStartTextMarker",
                      @"AXEndTextMarker",
                      @"AXVisited",
                      NSAccessibilityLinkedUIElementsAttribute,
                      NSAccessibilitySelectedAttribute,
796
                      NSAccessibilityBlockQuoteLevelAttribute,
797
                      NSAccessibilityTopLevelUIElementAttribute,
798
                      nil];
alice.liu@apple.com's avatar
alice.liu@apple.com committed
799
    }
800 801 802 803 804 805 806 807 808 809
    if (commonMenuAttrs == nil) {
        commonMenuAttrs = [[NSArray alloc] initWithObjects: NSAccessibilityRoleAttribute,
                            NSAccessibilityRoleDescriptionAttribute,
                            NSAccessibilityChildrenAttribute,
                            NSAccessibilityParentAttribute,
                            NSAccessibilityEnabledAttribute,
                            NSAccessibilityPositionAttribute,
                            NSAccessibilitySizeAttribute,
                            nil];
    }
alice.liu@apple.com's avatar
alice.liu@apple.com committed
810 811 812
    if (anchorAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityURLAttribute];
813
        [tempArray addObject:NSAccessibilityAccessKeyAttribute];
alice.liu@apple.com's avatar
alice.liu@apple.com committed
814 815 816 817 818 819 820 821
        anchorAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
    if (webAreaAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:@"AXLinkUIElements"];
        [tempArray addObject:@"AXLoaded"];
        [tempArray addObject:@"AXLayoutCount"];
822
        [tempArray addObject:NSAccessibilityLoadingProgressAttribute];
823
        [tempArray addObject:NSAccessibilityURLAttribute];
alice.liu@apple.com's avatar
alice.liu@apple.com committed
824 825 826 827 828 829 830 831 832 833
        webAreaAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
    if (textAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityNumberOfCharactersAttribute];
        [tempArray addObject:NSAccessibilitySelectedTextAttribute];
        [tempArray addObject:NSAccessibilitySelectedTextRangeAttribute];
        [tempArray addObject:NSAccessibilityVisibleCharacterRangeAttribute];
        [tempArray addObject:NSAccessibilityInsertionPointLineNumberAttribute];
834
        [tempArray addObject:NSAccessibilityTitleUIElementAttribute];
835
        [tempArray addObject:NSAccessibilityAccessKeyAttribute];
836
        [tempArray addObject:NSAccessibilityRequiredAttribute];
837
        [tempArray addObject:NSAccessibilityInvalidAttribute];
838
        [tempArray addObject:NSAccessibilityPlaceholderValueAttribute];
alice.liu@apple.com's avatar
alice.liu@apple.com committed
839 840 841
        textAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
842
    if (listAttrs == nil) {
843 844 845 846
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilitySelectedChildrenAttribute];
        [tempArray addObject:NSAccessibilityVisibleChildrenAttribute];
        [tempArray addObject:NSAccessibilityOrientationAttribute];
847
        [tempArray addObject:NSAccessibilityTitleUIElementAttribute];
848 849 850 851 852
        listAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
    if (listBoxAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:listAttrs];    
853
        [tempArray addObject:NSAccessibilityAccessKeyAttribute];
854
        [tempArray addObject:NSAccessibilityRequiredAttribute];
855
        [tempArray addObject:NSAccessibilityInvalidAttribute];
856 857 858
        listBoxAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
bdakin@apple.com's avatar
bdakin@apple.com committed
859
    if (rangeAttrs == nil) {
860 861 862
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityMinValueAttribute];
        [tempArray addObject:NSAccessibilityMaxValueAttribute];
cfleizach@apple.com's avatar
cfleizach@apple.com committed
863
        [tempArray addObject:NSAccessibilityOrientationAttribute];
864
        [tempArray addObject:NSAccessibilityValueDescriptionAttribute];
bdakin@apple.com's avatar
bdakin@apple.com committed
865
        rangeAttrs = [[NSArray alloc] initWithArray:tempArray];
866 867
        [tempArray release];
    }
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
    if (menuBarAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:commonMenuAttrs];
        [tempArray addObject:NSAccessibilitySelectedChildrenAttribute];
        [tempArray addObject:NSAccessibilityVisibleChildrenAttribute];
        [tempArray addObject:NSAccessibilityTitleUIElementAttribute];
        menuBarAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
    if (menuAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:commonMenuAttrs];
        [tempArray addObject:NSAccessibilitySelectedChildrenAttribute];
        [tempArray addObject:NSAccessibilityVisibleChildrenAttribute];
        [tempArray addObject:NSAccessibilityTitleUIElementAttribute];
        menuAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
    if (menuItemAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:commonMenuAttrs];
        [tempArray addObject:NSAccessibilityTitleAttribute];
        [tempArray addObject:NSAccessibilityHelpAttribute];
        [tempArray addObject:NSAccessibilitySelectedAttribute];
        [tempArray addObject:(NSString*)kAXMenuItemCmdCharAttribute];
        [tempArray addObject:(NSString*)kAXMenuItemCmdVirtualKeyAttribute];
        [tempArray addObject:(NSString*)kAXMenuItemCmdGlyphAttribute];
        [tempArray addObject:(NSString*)kAXMenuItemCmdModifiersAttribute];
        [tempArray addObject:(NSString*)kAXMenuItemMarkCharAttribute];
        [tempArray addObject:(NSString*)kAXMenuItemPrimaryUIElementAttribute];
        [tempArray addObject:NSAccessibilityServesAsTitleForUIElementsAttribute];
        menuItemAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
    if (menuButtonAttrs == nil) {
        menuButtonAttrs = [[NSArray alloc] initWithObjects:NSAccessibilityRoleAttribute,
            NSAccessibilityRoleDescriptionAttribute,
            NSAccessibilityParentAttribute,
            NSAccessibilityPositionAttribute,
            NSAccessibilitySizeAttribute,
            NSAccessibilityWindowAttribute,
            NSAccessibilityEnabledAttribute,
            NSAccessibilityFocusedAttribute,
            NSAccessibilityTitleAttribute,
            NSAccessibilityChildrenAttribute, nil];
    }
911 912 913
    if (controlAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityTitleUIElementAttribute];
914
        [tempArray addObject:NSAccessibilityAccessKeyAttribute];
915
        [tempArray addObject:NSAccessibilityRequiredAttribute];
916
        [tempArray addObject:NSAccessibilityInvalidAttribute];
917 918 919
        controlAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
920 921 922 923 924 925 926 927 928
    if (buttonAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        // Buttons should not expose AXValue.
        [tempArray removeObject:NSAccessibilityValueAttribute];
        [tempArray addObject:NSAccessibilityTitleUIElementAttribute];
        [tempArray addObject:NSAccessibilityAccessKeyAttribute];
        buttonAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
929 930 931 932 933 934
    if (comboBoxAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:controlAttrs];
        [tempArray addObject:NSAccessibilityExpandedAttribute];
        comboBoxAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];        
    }
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
    if (tableAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityRowsAttribute];
        [tempArray addObject:NSAccessibilityVisibleRowsAttribute];
        [tempArray addObject:NSAccessibilityColumnsAttribute];
        [tempArray addObject:NSAccessibilityVisibleColumnsAttribute];
        [tempArray addObject:NSAccessibilityVisibleCellsAttribute];
        [tempArray addObject:(NSString *)kAXColumnHeaderUIElementsAttribute];
        [tempArray addObject:NSAccessibilityRowHeaderUIElementsAttribute];
        [tempArray addObject:NSAccessibilityHeaderAttribute];
        tableAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
    if (tableRowAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityIndexAttribute];
        tableRowAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
    if (tableColAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityIndexAttribute];
        [tempArray addObject:NSAccessibilityHeaderAttribute];
        [tempArray addObject:NSAccessibilityRowsAttribute];
        [tempArray addObject:NSAccessibilityVisibleRowsAttribute];
        tableColAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
    if (tableCellAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityRowIndexRangeAttribute];
        [tempArray addObject:NSAccessibilityColumnIndexRangeAttribute];
        tableCellAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];        
    }
970 971 972 973 974 975
    if (groupAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityTitleUIElementAttribute];
        groupAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
976
    if (inputImageAttrs == nil) {
977
        tempArray = [[NSMutableArray alloc] initWithArray:buttonAttrs];
978 979 980 981
        [tempArray addObject:NSAccessibilityURLAttribute];
        inputImageAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
982 983 984
    if (passwordFieldAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityTitleUIElementAttribute];
985
        [tempArray addObject:NSAccessibilityRequiredAttribute];
986
        [tempArray addObject:NSAccessibilityInvalidAttribute];
987 988 989
        passwordFieldAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
cfleizach@apple.com's avatar
cfleizach@apple.com committed
990 991 992 993 994 995 996
    if (tabListAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityTabsAttribute];
        [tempArray addObject:NSAccessibilityContentsAttribute];
        tabListAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];        
    }
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    if (outlineAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilitySelectedRowsAttribute];
        [tempArray addObject:NSAccessibilityRowsAttribute];
        [tempArray addObject:NSAccessibilityColumnsAttribute];
        outlineAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
    if (outlineRowAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:tableRowAttrs];
        [tempArray addObject:NSAccessibilityDisclosingAttribute];
        [tempArray addObject:NSAccessibilityDisclosedByRowAttribute];
        [tempArray addObject:NSAccessibilityDisclosureLevelAttribute];
        [tempArray addObject:NSAccessibilityDisclosedRowsAttribute];
        outlineRowAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
1014 1015 1016 1017 1018 1019 1020 1021
    if (scrollViewAttrs == nil) {
        tempArray = [[NSMutableArray alloc] initWithArray:attributes];
        [tempArray addObject:NSAccessibilityContentsAttribute];
        [tempArray addObject:NSAccessibilityHorizontalScrollBarAttribute];
        [tempArray addObject:NSAccessibilityVerticalScrollBarAttribute];
        scrollViewAttrs = [[NSArray alloc] initWithArray:tempArray];
        [tempArray release];
    }
1022
    
1023
    NSArray *objectAttributes = attributes;
1024
    
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
    if (m_object->isPasswordField())
        objectAttributes = passwordFieldAttrs;

    else if (m_object->isWebArea())
        objectAttributes = webAreaAttrs;
    
    else if (m_object->isTextControl())
        objectAttributes = textAttrs;

    else if (m_object->isAnchor() || m_object->isImage() || m_object->isLink())
        objectAttributes = anchorAttrs;

1037
    else if (m_object->isAccessibilityTable())
1038 1039 1040 1041 1042
        objectAttributes = tableAttrs;
    else if (m_object->isTableColumn())
        objectAttributes = tableColAttrs;
    else if (m_object->isTableCell())
        objectAttributes = tableCellAttrs;
1043 1044 1045 1046 1047 1048 1049
    else if (m_object->isTableRow()) {
        // An ARIA table row can be collapsed and expanded, so it needs the extra attributes.
        if (m_object->isARIATreeGridRow())
            objectAttributes = outlineRowAttrs;
        else
            objectAttributes = tableRowAttrs;
    }
1050 1051 1052 1053 1054 1055
    
    else if (m_object->isTree())
        objectAttributes = outlineAttrs;
    else if (m_object->isTreeItem())
        objectAttributes = outlineRowAttrs;
    
1056
    else if (m_object->isListBox())
1057
        objectAttributes = listBoxAttrs;
1058 1059 1060
    else if (m_object->isList())
        objectAttributes = listAttrs;
    
1061 1062 1063 1064 1065 1066
    else if (m_object->isComboBox())
        objectAttributes = comboBoxAttrs;
    
    else if (m_object->isProgressIndicator() || m_object->isSlider())
        objectAttributes = rangeAttrs;

1067
    // These are processed in order because an input image is a button, and a button is a control.
1068 1069
    else if (m_object->isInputImage())
        objectAttributes = inputImageAttrs;
1070 1071
    else if (m_object->isButton())
        objectAttributes = buttonAttrs;
1072 1073 1074
    else if (m_object->isControl())
        objectAttributes = controlAttrs;
    
1075
    else if (m_object->isGroup() || m_object->isListItem())
1076 1077 1078
        objectAttributes = groupAttrs;
    else if (m_object->isTabList())
        objectAttributes = tabListAttrs;
1079 1080
    else if (m_object->isScrollView())
        objectAttributes = scrollViewAttrs;
1081 1082 1083 1084 1085 1086 1087 1088 1089
    
    else if (m_object->isMenu())
        objectAttributes = menuAttrs;
    else if (m_object->isMenuBar())
        objectAttributes = menuBarAttrs;
    else if (m_object->isMenuButton())
        objectAttributes = menuButtonAttrs;
    else if (m_object->isMenuItem())
        objectAttributes = menuItemAttrs;
1090
    
1091
    NSArray *additionalAttributes = [self additionalAccessibilityAttributeNames];
1092
    if ([additionalAttributes count])
1093
        objectAttributes = [objectAttributes arrayByAddingObjectsFromArray:additionalAttributes];
1094
    
1095
    return objectAttributes;
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1096 1097
}

1098
- (VisiblePositionRange)visiblePositionRangeForTextMarkerRange:(id)textMarkerRange
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1099
{
1100 1101
    if (!textMarkerRange)
        return VisiblePositionRange();
1102 1103
    AXObjectCache* cache = m_object->axObjectCache();
    return VisiblePositionRange(visiblePositionForStartOfTextMarkerRange(cache, textMarkerRange), visiblePositionForEndOfTextMarkerRange(cache, textMarkerRange));
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1104 1105 1106 1107 1108 1109 1110
}

- (NSArray*)renderWidgetChildren
{
    Widget* widget = m_object->widget();
    if (!widget)
        return nil;
1111
    return [(widget->platformWidget()) accessibilityAttributeValue: NSAccessibilityChildrenAttribute];
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1112 1113
}

1114 1115 1116 1117 1118 1119 1120 1121
- (id)remoteAccessibilityParentObject
{
    if (!m_object || !m_object->document())
        return nil;
    
    return m_object->document()->frame()->loader()->client()->accessibilityRemoteObject();
}

1122 1123 1124
static void convertToVector(NSArray* array, AccessibilityObject::AccessibilityChildrenVector& vector)
{
    unsigned length = [array count];
darin@apple.com's avatar
darin@apple.com committed
1125
    vector.reserveInitialCapacity(length);
1126 1127 1128 1129 1130 1131 1132 1133
    for (unsigned i = 0; i < length; ++i) {
        AccessibilityObject* obj = [[array objectAtIndex:i] accessibilityObject];
        if (obj)
            vector.append(obj);
    }
}

static NSMutableArray* convertToNSArray(const AccessibilityObject::AccessibilityChildrenVector& vector)
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1134 1135 1136
{
    unsigned length = vector.size();
    NSMutableArray* array = [NSMutableArray arrayWithCapacity: length];
1137
    for (unsigned i = 0; i < length; ++i) {
1138 1139 1140 1141 1142 1143 1144 1145