AccessibilityObject.cpp 61.9 KB
Newer Older
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * Copyright (C) 2008 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple 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.
 */

#include "config.h"
#include "AccessibilityObject.h"

#include "AXObjectCache.h"
#include "CharacterNames.h"
#include "EventNames.h"
35
#include "FloatRect.h"
alice.liu@apple.com's avatar
alice.liu@apple.com committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "FocusController.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "HTMLAreaElement.h"
#include "HTMLFrameElementBase.h"
#include "HTMLImageElement.h"
#include "HTMLInputElement.h"
#include "HTMLLabelElement.h"
#include "HTMLMapElement.h"
#include "HTMLSelectElement.h"
#include "HTMLTextAreaElement.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "NodeList.h"
50
#include "NotImplemented.h"
alice.liu@apple.com's avatar
alice.liu@apple.com committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "Page.h"
#include "RenderImage.h"
#include "RenderListMarker.h"
#include "RenderMenuList.h"
#include "RenderTextControl.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "RenderWidget.h"
#include "SelectionController.h"
#include "TextIterator.h"
#include "htmlediting.h"
#include "visible_units.h"

using namespace std;

namespace WebCore {

using namespace EventNames;
using namespace HTMLNames;

AccessibilityObject::AccessibilityObject(RenderObject* renderer)
    : m_renderer(renderer)
    , m_id(0)
{
75
    ASSERT_ARG(renderer, renderer);
alice.liu@apple.com's avatar
alice.liu@apple.com committed
76
#ifndef NDEBUG
77
78
    m_renderer->setHasAXObject(true);
#endif
alice.liu@apple.com's avatar
alice.liu@apple.com committed
79
80
81
82
}

AccessibilityObject::~AccessibilityObject()
{
83
    ASSERT(isDetached());
alice.liu@apple.com's avatar
alice.liu@apple.com committed
84
85
86
87
88
89
90
91
92
93
}

PassRefPtr<AccessibilityObject> AccessibilityObject::create(RenderObject* renderer)
{
    return adoptRef(new AccessibilityObject(renderer));
}

void AccessibilityObject::detach()
{
    removeAXObjectID();
94
95
96
#ifndef NDEBUG
    if (m_renderer)
        m_renderer->setHasAXObject(false);
alice.liu@apple.com's avatar
alice.liu@apple.com committed
97
98
#endif
    m_renderer = 0;
99
#if HAVE(ACCESSIBILITY)
alice.liu@apple.com's avatar
alice.liu@apple.com committed
100
    m_wrapper = 0;
101
#endif
alice.liu@apple.com's avatar
alice.liu@apple.com committed
102
103
104
    clearChildren();
}

105
AccessibilityObject* AccessibilityObject::firstChild() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
106
107
108
109
110
111
112
{
    if (!m_renderer || !m_renderer->firstChild())
        return 0;

    return m_renderer->document()->axObjectCache()->get(m_renderer->firstChild());
}

113
AccessibilityObject* AccessibilityObject::lastChild() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
114
115
116
117
118
119
120
{
    if (!m_renderer || !m_renderer->lastChild())
        return 0;

    return m_renderer->document()->axObjectCache()->get(m_renderer->lastChild());
}

121
AccessibilityObject* AccessibilityObject::previousSibling() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
122
123
124
125
126
127
128
{
    if (!m_renderer || !m_renderer->previousSibling())
        return 0;

    return m_renderer->document()->axObjectCache()->get(m_renderer->previousSibling());
}

129
AccessibilityObject* AccessibilityObject::nextSibling() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
130
131
132
133
134
135
136
{
    if (!m_renderer || !m_renderer->nextSibling())
        return 0;

    return m_renderer->document()->axObjectCache()->get(m_renderer->nextSibling());
}

137
AccessibilityObject* AccessibilityObject::parentObject() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
138
139
140
141
142
143
144
145
146
147
{
    if (m_areaElement)
        return m_renderer->document()->axObjectCache()->get(m_renderer);

    if (!m_renderer || !m_renderer->parent())
        return 0;

    return m_renderer->document()->axObjectCache()->get(m_renderer->parent());
}

148
AccessibilityObject* AccessibilityObject::parentObjectUnignored() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
{
    AccessibilityObject* parent;
    for (parent = parentObject(); parent && parent->accessibilityIsIgnored(); parent = parent->parentObject())
        ;
    return parent;
}

bool AccessibilityObject::isWebArea() const
{
    return m_renderer->isRenderView();
}

bool AccessibilityObject::isImageButton() const
{
163
164
165
    return isImage() && m_renderer->element() && m_renderer->element()->hasTagName(inputTag);
}

alice.liu@apple.com's avatar
alice.liu@apple.com committed
166
167
bool AccessibilityObject::isAnchor() const
{
168
169
    return m_areaElement || (!isImage() && m_renderer->element() && m_renderer->element()->isLink());
}
alice.liu@apple.com's avatar
alice.liu@apple.com committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

bool AccessibilityObject::isTextControl() const
{
    return m_renderer->isTextField() || m_renderer->isTextArea();
}

bool AccessibilityObject::isImage() const
{
    return m_renderer->isImage();
}

bool AccessibilityObject::isAttachment() const
{
    // widgets are the replaced elements that we represent to AX as attachments
    bool isWidget = m_renderer && m_renderer->isWidget();
185

alice.liu@apple.com's avatar
alice.liu@apple.com committed
186
    // assert that a widget is a replaced element that is not an image
187
    ASSERT(!isWidget || (m_renderer->isReplaced() && !isImage()));
alice.liu@apple.com's avatar
alice.liu@apple.com committed
188
189
190
191
192
    return isWidget;
}

bool AccessibilityObject::isPasswordField() const
{
193
194
195
    ASSERT(m_renderer);
    if (!m_renderer->element() || !m_renderer->element()->isHTMLElement())
        return false;
196
    return static_cast<HTMLElement*>(m_renderer->element())->isPasswordField();
alice.liu@apple.com's avatar
alice.liu@apple.com committed
197
198
}

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
bool AccessibilityObject::isPressed() const
{
    ASSERT(m_renderer);
    if (roleValue() != ButtonRole)
        return false;
    return m_renderer->node() && m_renderer->node()->active();
}

bool AccessibilityObject::isIndeterminate() const
{
    ASSERT(m_renderer);
    return m_renderer->node() && m_renderer->node()->isIndeterminate();
}

bool AccessibilityObject::isChecked() const
{
    ASSERT(m_renderer);
    return m_renderer->node() && m_renderer->node()->isChecked();
}

bool AccessibilityObject::isHovered() const
{
    ASSERT(m_renderer);
    return m_renderer->node() && m_renderer->node()->hovered();
}

bool AccessibilityObject::isMultiSelect() const
{
    ASSERT(m_renderer);
    if (!m_renderer->isListBox())
        return false;
    return m_renderer->element() && static_cast<HTMLSelectElement*>(m_renderer->element())->multiple();
}

bool AccessibilityObject::isReadOnly() const
{
    ASSERT(m_renderer);
    return !m_renderer->node() || !m_renderer->node()->isContentEditable();
}

bool AccessibilityObject::isOffScreen() const
{
    ASSERT(m_renderer);
    IntRect contentRect = m_renderer->absoluteClippedOverflowRect();
    FrameView* view = m_renderer->document()->frame()->view();
    FloatRect viewRect = view->visibleContentRect();
    viewRect.intersect(contentRect);
    return viewRect.isEmpty();
}

249
int AccessibilityObject::headingLevel(Node* node)
alice.liu@apple.com's avatar
alice.liu@apple.com committed
250
251
252
253
254
{
    // headings can be in block flow and non-block flow

    if (!node)
        return 0;
255

alice.liu@apple.com's avatar
alice.liu@apple.com committed
256
257
    if (node->hasTagName(h1Tag))
        return 1;
258

alice.liu@apple.com's avatar
alice.liu@apple.com committed
259
260
    if (node->hasTagName(h2Tag))
        return 2;
261

alice.liu@apple.com's avatar
alice.liu@apple.com committed
262
263
    if (node->hasTagName(h3Tag))
        return 3;
264

alice.liu@apple.com's avatar
alice.liu@apple.com committed
265
266
    if (node->hasTagName(h4Tag))
        return 4;
267

alice.liu@apple.com's avatar
alice.liu@apple.com committed
268
269
    if (node->hasTagName(h5Tag))
        return 5;
270

alice.liu@apple.com's avatar
alice.liu@apple.com committed
271
272
273
274
275
276
277
278
279
280
281
    if (node->hasTagName(h6Tag))
        return 6;

    return 0;
}

bool AccessibilityObject::isHeading() const
{
    return headingLevel(m_renderer->element()) != 0;
}

282
HTMLAnchorElement* AccessibilityObject::anchorElement() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
283
284
285
286
287
288
289
290
291
292
293
{
    // return already-known anchor for image areas
    if (m_areaElement)
        return m_areaElement.get();

    // Search up the render tree for a RenderObject with a DOM node.  Defer to an earlier continuation, though.
    RenderObject* currRenderer;
    for (currRenderer = m_renderer; currRenderer && !currRenderer->element(); currRenderer = currRenderer->parent()) {
        if (currRenderer->continuation())
            return currRenderer->document()->axObjectCache()->get(currRenderer->continuation())->anchorElement();
    }
294

alice.liu@apple.com's avatar
alice.liu@apple.com committed
295
296
297
    // bail if none found
    if (!currRenderer)
        return 0;
298

alice.liu@apple.com's avatar
alice.liu@apple.com committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
    // search up the DOM tree for an anchor element
    // NOTE: this assumes that any non-image with an anchor is an HTMLAnchorElement
    Node* node = currRenderer->node();
    for ( ; node; node = node->parentNode()) {
        if (node->hasTagName(aTag))
            return static_cast<HTMLAnchorElement*>(node);
    }

    return 0;
}

static bool isCheckboxOrRadio(HTMLInputElement* input)
{
    return (input->inputType() == HTMLInputElement::CHECKBOX ||
            input->inputType() == HTMLInputElement::RADIO);
}

static bool isCheckboxOrRadio(Node* node)
{
    if (!node->hasTagName(inputTag))
        return false;
    return isCheckboxOrRadio(static_cast<HTMLInputElement*>(node));
}

323
Element* AccessibilityObject::actionElement() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
324
325
326
327
328
329
330
331
332
333
{
    if (m_renderer->element() && m_renderer->element()->hasTagName(inputTag)) {
        HTMLInputElement* input = static_cast<HTMLInputElement*>(m_renderer->element());
        if (!input->disabled() && (isCheckboxOrRadio(input) ||
                                   input->isTextButton()))
            return input;
    }

    if (isImageButton())
        return static_cast<Element*>(m_renderer->element());
334

alice.liu@apple.com's avatar
alice.liu@apple.com committed
335
336
337
338
339
340
341
342
343
    if (m_renderer->isMenuList())
        return static_cast<RenderMenuList*>(m_renderer)->selectElement();

    Element* elt = anchorElement();
    if (!elt)
        elt = mouseButtonListener();
    return elt;
}

344
Element* AccessibilityObject::mouseButtonListener() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
345
346
347
348
349
350
{
    Node* node = m_renderer->element();
    if (!node)
        return 0;
    if (!node->isEventTargetNode())
        return 0;
351

alice.liu@apple.com's avatar
alice.liu@apple.com committed
352
353
354
355
356
357
358
359
360
    // FIXME: Do the continuation search like anchorElement does
    for (EventTargetNode* elt = static_cast<EventTargetNode*>(node); elt; elt = static_cast<EventTargetNode*>(elt->parentNode())) {
        if (elt->getHTMLEventListener(clickEvent) || elt->getHTMLEventListener(mousedownEvent) || elt->getHTMLEventListener(mouseupEvent))
            return static_cast<Element*>(elt);
    }

    return 0;
}

361
String AccessibilityObject::helpText() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
{
    if (!m_renderer)
        return String();

    if (m_areaElement) {
        const AtomicString& summary = m_areaElement->getAttribute(summaryAttr);
        if (!summary.isEmpty())
            return summary;
        const AtomicString& title = m_areaElement->getAttribute(titleAttr);
        if (!title.isEmpty())
            return title;
    }

    for (RenderObject* curr = m_renderer; curr; curr = curr->parent()) {
        if (curr->element() && curr->element()->isHTMLElement()) {
            const AtomicString& summary = static_cast<Element*>(curr->element())->getAttribute(summaryAttr);
            if (!summary.isEmpty())
                return summary;
            const AtomicString& title = static_cast<Element*>(curr->element())->getAttribute(titleAttr);
            if (!title.isEmpty())
                return title;
        }
    }

    return String();
}

389
String AccessibilityObject::textUnderElement() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
{
    if (!m_renderer)
        return String();

    Node* node = m_renderer->element();
    if (node) {
        if (Frame* frame = node->document()->frame()) {
            // catch stale WebCoreAXObject (see <rdar://problem/3960196>)
            if (frame->document() != node->document())
                return String();
            return plainText(rangeOfContents(node).get());
        }
    }

    // return the null string for anonymous text because it is non-trivial to get
    // the actual text and, so far, that is not needed
    return String();
}

409
bool AccessibilityObject::hasIntValue() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
410
411
412
{
    if (isHeading())
        return true;
413

alice.liu@apple.com's avatar
alice.liu@apple.com committed
414
415
    if (m_renderer->element() && isCheckboxOrRadio(m_renderer->element()))
        return true;
416

alice.liu@apple.com's avatar
alice.liu@apple.com committed
417
418
419
    return false;
}

420
int AccessibilityObject::intValue() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
421
422
423
424
425
426
427
428
429
430
{
    if (!m_renderer || m_areaElement || isPasswordField())
        return 0;

    if (isHeading())
        return headingLevel(m_renderer->element());

    Node* node = m_renderer->element();
    if (node && isCheckboxOrRadio(node))
        return static_cast<HTMLInputElement*>(node)->checked();
431

alice.liu@apple.com's avatar
alice.liu@apple.com committed
432
433
434
    return 0;
}

435
String AccessibilityObject::stringValue() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
436
437
438
439
440
441
{
    if (!m_renderer || m_areaElement || isPasswordField())
        return String();

    if (m_renderer->isText())
        return textUnderElement();
442

alice.liu@apple.com's avatar
alice.liu@apple.com committed
443
444
    if (m_renderer->isMenuList())
        return static_cast<RenderMenuList*>(m_renderer)->text();
445

alice.liu@apple.com's avatar
alice.liu@apple.com committed
446
447
448
449
450
451
    if (m_renderer->isListMarker())
        return static_cast<RenderListMarker*>(m_renderer)->text();

    if (isWebArea()) {
        if (m_renderer->document()->frame())
            return String();
452

alice.liu@apple.com's avatar
alice.liu@apple.com committed
453
454
455
456
457
        // FIXME: should use startOfDocument and endOfDocument (or rangeForDocument?) here
        VisiblePosition startVisiblePosition = m_renderer->positionForCoordinates(0, 0);
        VisiblePosition endVisiblePosition = m_renderer->positionForCoordinates(INT_MAX, INT_MAX);
        if (startVisiblePosition.isNull() || endVisiblePosition.isNull())
            return String();
458

alice.liu@apple.com's avatar
alice.liu@apple.com committed
459
        return plainText(makeRange(startVisiblePosition, endVisiblePosition).get());
460
461
    }

alice.liu@apple.com's avatar
alice.liu@apple.com committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    if (isTextControl())
        return static_cast<RenderTextControl*>(m_renderer)->text();

    // FIXME: We might need to implement a value here for more types
    // FIXME: It would be better not to advertise a value at all for the types for which we don't implement one;
    // this would require subclassing or making accessibilityAttributeNames do something other than return a
    // single static array.
    return String();
}

static HTMLLabelElement* labelForElement(Element* element)
{
    RefPtr<NodeList> list = element->document()->getElementsByTagName("label");
    unsigned len = list->length();
    for (unsigned i = 0; i < len; i++) {
        if (list->item(i)->hasTagName(labelTag)) {
            HTMLLabelElement* label = static_cast<HTMLLabelElement*>(list->item(i));
            if (label->correspondingControl() == element)
                return label;
        }
    }
483

alice.liu@apple.com's avatar
alice.liu@apple.com committed
484
485
486
    return 0;
}

487
String AccessibilityObject::title() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
488
489
490
{
    if (!m_renderer || m_areaElement || !m_renderer->element())
        return String();
491

alice.liu@apple.com's avatar
alice.liu@apple.com committed
492
493
    if (m_renderer->element()->hasTagName(buttonTag))
        return textUnderElement();
494

alice.liu@apple.com's avatar
alice.liu@apple.com committed
495
496
497
498
499
500
501
502
503
    if (m_renderer->element()->hasTagName(inputTag)) {
        HTMLInputElement* input = static_cast<HTMLInputElement*>(m_renderer->element());
        if (input->isTextButton())
            return input->value();

        HTMLLabelElement* label = labelForElement(input);
        if (label)
            return label->innerText();
    }
504

alice.liu@apple.com's avatar
alice.liu@apple.com committed
505
506
507
508
509
510
    if (m_renderer->element()->isLink() || isHeading())
        return textUnderElement();

    return String();
}

511
String AccessibilityObject::accessibilityDescription() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
512
513
514
{
    if (!m_renderer || m_areaElement)
        return String();
515
516

    if (isImage()) {
alice.liu@apple.com's avatar
alice.liu@apple.com committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
        if (m_renderer->element() && m_renderer->element()->isHTMLElement()) {
            const AtomicString& alt = static_cast<HTMLElement*>(m_renderer->element())->getAttribute(altAttr);
            if (alt.isEmpty())
                return String();
            return alt;
        }
    }

    if (isWebArea()) {
        Document *document = m_renderer->document();
        Node* owner = document->ownerElement();
        if (owner) {
            if (owner->hasTagName(frameTag) || owner->hasTagName(iframeTag))
                return static_cast<HTMLFrameElementBase*>(owner)->name();
            if (owner->isHTMLElement())
                return static_cast<HTMLElement*>(owner)->getAttribute(nameAttr);
533
        }
alice.liu@apple.com's avatar
alice.liu@apple.com committed
534
        owner = document->body();
535
        if (owner && owner->isHTMLElement())
alice.liu@apple.com's avatar
alice.liu@apple.com committed
536
537
            return static_cast<HTMLElement*>(owner)->getAttribute(nameAttr);
    }
538

alice.liu@apple.com's avatar
alice.liu@apple.com committed
539
540
541
    return String();
}

542
IntRect AccessibilityObject::boundingBoxRect() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
{
    IntRect rect;
    RenderObject* obj = m_renderer;

    if (!obj)
        return IntRect();

    if (obj->isInlineContinuation())
        obj = obj->element()->renderer();
    Vector<IntRect> rects;
    int x, y;
    obj->absolutePosition(x, y);
    obj->absoluteRects(rects, x, y);
    const size_t n = rects.size();
    for (size_t i = 0; i < n; ++i) {
        IntRect r = rects[i];
        if (!r.isEmpty()) {
            if (obj->style()->hasAppearance())
                theme()->adjustRepaintRect(obj, r);
            rect.unite(r);
        }
    }
    return rect;
}

568
IntSize AccessibilityObject::size() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
569
570
571
572
573
574
{
    IntRect rect = m_areaElement ? m_areaElement->getRect(m_renderer) : boundingBoxRect();
    return rect.size();
}

// the closest object for an internal anchor
575
AccessibilityObject* AccessibilityObject::linkedUIElement() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
576
577
578
{
    if (!isAnchor())
        return 0;
579

alice.liu@apple.com's avatar
alice.liu@apple.com committed
580
    HTMLAnchorElement* anchor = anchorElement();
581
    if (!anchor)
alice.liu@apple.com's avatar
alice.liu@apple.com committed
582
        return 0;
583

alice.liu@apple.com's avatar
alice.liu@apple.com committed
584
585
586
587
    KURL linkURL = anchor->href();
    String ref = linkURL.ref();
    if (ref.isEmpty())
        return 0;
588

alice.liu@apple.com's avatar
alice.liu@apple.com committed
589
590
591
592
    // check if URL is the same as current URL
    linkURL.setRef("");
    if (m_renderer->document()->url() != linkURL)
        return 0;
593

alice.liu@apple.com's avatar
alice.liu@apple.com committed
594
595
596
597
598
599
    Node* linkedNode = m_renderer->document()->getElementById(ref);
    if (!linkedNode) {
        linkedNode = m_renderer->document()->anchors()->namedItem(ref, !m_renderer->document()->inCompatMode());
        if (!linkedNode)
            return 0;
    }
600

alice.liu@apple.com's avatar
alice.liu@apple.com committed
601
602
603
604
605
606
607
608
    // the element we find may not be accessible, keep searching until we find a good one
    AccessibilityObject* linkedAXElement = m_renderer->document()->axObjectCache()->get(linkedNode->renderer());
    while (linkedAXElement && linkedAXElement->accessibilityIsIgnored()) {
        linkedNode = linkedNode->traverseNextNode();
        if (!linkedNode)
            return 0;
        linkedAXElement = m_renderer->document()->axObjectCache()->get(linkedNode->renderer());
    }
609
610

    return linkedAXElement;
alice.liu@apple.com's avatar
alice.liu@apple.com committed
611
612
}

613
bool AccessibilityObject::accessibilityShouldUseUniqueId() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
614
615
616
617
{
    return isWebArea() || isTextControl();
}

618
bool AccessibilityObject::accessibilityIsIgnored() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
619
620
621
622
623
624
625
626
627
628
{
    // ignore invisible element
    if (!m_renderer || m_renderer->style()->visibility() != VISIBLE)
        return true;

    // ignore popup menu items because AppKit does
    for (RenderObject* parent = m_renderer->parent(); parent; parent = parent->parent()) {
        if (parent->isMenuList())
            return true;
    }
629

alice.liu@apple.com's avatar
alice.liu@apple.com committed
630
631
632
    // NOTE: BRs always have text boxes now, so the text box check here can be removed
    if (m_renderer->isText())
        return m_renderer->isBR() || !static_cast<RenderText*>(m_renderer)->firstTextBox();
633

alice.liu@apple.com's avatar
alice.liu@apple.com committed
634
635
    if (isHeading())
        return false;
636

alice.liu@apple.com's avatar
alice.liu@apple.com committed
637
638
639
640
641
642
643
644
645
646
647
    if (m_areaElement || (m_renderer->element() && m_renderer->element()->isLink()))
        return false;

    // all controls are accessible
    if (m_renderer->element() && m_renderer->element()->isControl())
        return false;

    if (m_renderer->isBlockFlow() && m_renderer->childrenInline())
        return !static_cast<RenderBlock*>(m_renderer)->firstLineBox() && !mouseButtonListener();

    // ignore images seemingly used as spacers
648
    if (m_renderer->isRenderImage()) {
alice.liu@apple.com's avatar
alice.liu@apple.com committed
649
650
        // informal standard is to ignore images with zero-length alt strings
        Node* node = m_renderer->element();
651
        if (node && node->isElementNode()) {
alice.liu@apple.com's avatar
alice.liu@apple.com committed
652
653
654
655
656
            Element* elt = static_cast<Element*>(node);
            const AtomicString& alt = elt->getAttribute(altAttr);
            if (alt.isEmpty() && !alt.isNull())
                return true;
        }
657

alice.liu@apple.com's avatar
alice.liu@apple.com committed
658
659
660
        // check for one-dimensional image
        if (m_renderer->height() <= 1 || m_renderer->width() <= 1)
            return true;
661

alice.liu@apple.com's avatar
alice.liu@apple.com committed
662
663
664
665
666
667
        // check whether rendered image was stretched from one-dimensional file image
        RenderImage* image = static_cast<RenderImage*>(m_renderer);
        if (image->cachedImage()) {
            IntSize imageSize = image->cachedImage()->imageSize(image->view()->zoomFactor());
            return imageSize.height() <= 1 || imageSize.width() <= 1;
        }
668

alice.liu@apple.com's avatar
alice.liu@apple.com committed
669
670
        return false;
    }
671

alice.liu@apple.com's avatar
alice.liu@apple.com committed
672
673
674
    return !m_renderer->isListMarker() && !isWebArea();
}

675
bool AccessibilityObject::isLoaded() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
676
677
678
679
{
    return !m_renderer->document()->tokenizer();
}

680
int AccessibilityObject::layoutCount() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
681
682
683
684
{
    return static_cast<RenderView*>(m_renderer)->frameView()->layoutCount();
}

685
int AccessibilityObject::textLength() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
686
687
688
689
690
{
    ASSERT(isTextControl());

    if (isPasswordField())
        return -1; // need to return something distinct from 0
691

alice.liu@apple.com's avatar
alice.liu@apple.com committed
692
693
694
    return static_cast<RenderTextControl*>(m_renderer)->text().length();
}

695
String AccessibilityObject::selectedText() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
696
697
698
699
700
701
702
703
704
705
{
    ASSERT(isTextControl());

    if (isPasswordField())
        return String(); // need to return something distinct from empty string

    RenderTextControl* textControl = static_cast<RenderTextControl*>(m_renderer);
    return textControl->text().substring(textControl->selectionStart(), textControl->selectionEnd() - textControl->selectionStart());
}

706
Selection AccessibilityObject::selection() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
707
708
709
710
{
    return m_renderer->document()->frame()->selectionController()->selection();
}

711
AccessibilityObject::PlainTextRange AccessibilityObject::selectedTextRange() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
712
713
714
715
716
717
{
    ASSERT(isTextControl());

    if (isPasswordField())
        return PlainTextRange();

718
    RenderTextControl* textControl = static_cast<RenderTextControl*>(m_renderer);
alice.liu@apple.com's avatar
alice.liu@apple.com committed
719
720
721
    return PlainTextRange(textControl->selectionStart(), textControl->selectionEnd() - textControl->selectionStart());
}

722
void AccessibilityObject::setSelectedText(const String&)
alice.liu@apple.com's avatar
alice.liu@apple.com committed
723
724
{
    // TODO: set selected text (ReplaceSelectionCommand). <rdar://problem/4712125>
725
    notImplemented();
alice.liu@apple.com's avatar
alice.liu@apple.com committed
726
727
}

728
void AccessibilityObject::setSelectedTextRange(const PlainTextRange& range)
alice.liu@apple.com's avatar
alice.liu@apple.com committed
729
730
731
732
733
{
    RenderTextControl* textControl = static_cast<RenderTextControl*>(m_renderer);
    textControl->setSelectionRange(range.start, range.start + range.length);
}

734
void AccessibilityObject::makeRangeVisible(const PlainTextRange&)
alice.liu@apple.com's avatar
alice.liu@apple.com committed
735
736
{
    // TODO: make range visible (scrollRectToVisible).  <rdar://problem/4712101>
737
    notImplemented();
alice.liu@apple.com's avatar
alice.liu@apple.com committed
738
739
}

740
KURL AccessibilityObject::url() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
741
{
742
    if (isAnchor()) {
alice.liu@apple.com's avatar
alice.liu@apple.com committed
743
744
745
        if (HTMLAnchorElement* anchor = anchorElement())
            return anchor->href();
    }
746
    if (isImage() && m_renderer->element() && m_renderer->element()->hasTagName(imgTag))
alice.liu@apple.com's avatar
alice.liu@apple.com committed
747
748
749
750
751
        return static_cast<HTMLImageElement*>(m_renderer->element())->src();

    return KURL();
}

752
bool AccessibilityObject::isVisited() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
753
754
755
756
{
    return m_renderer->style()->pseudoState() == PseudoVisited;
}

757
bool AccessibilityObject::isFocused() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
{
    return (m_renderer->element() && m_renderer->document()->focusedNode() == m_renderer->element());
}

void AccessibilityObject::setFocused(bool on)
{
    if (!canSetFocusAttribute())
        return;

    if (!on)
        m_renderer->document()->setFocusedNode(0);
    else {
        if (m_renderer->element()->isElementNode())
            static_cast<Element*>(m_renderer->element())->focus();
        else
            m_renderer->document()->setFocusedNode(m_renderer->element());
    }
}

777
void AccessibilityObject::setValue(const String& string)
alice.liu@apple.com's avatar
alice.liu@apple.com committed
778
779
780
781
782
783
784
785
786
787
{
    if (m_renderer->isTextField()) {
        HTMLInputElement* input = static_cast<HTMLInputElement*>(m_renderer->element());
        input->setValue(string);
    } else if (m_renderer->isTextArea()) {
        HTMLTextAreaElement* textArea = static_cast<HTMLTextAreaElement*>(m_renderer->element());
        textArea->setValue(string);
    }
}

788
bool AccessibilityObject::isEnabled() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
789
790
791
792
793
{
    return m_renderer->element() ? m_renderer->element()->isEnabled() : true;
}

void AccessibilityObject::press()
794
{
alice.liu@apple.com's avatar
alice.liu@apple.com committed
795
796
797
798
799
800
801
802
    Element* actionElem = actionElement();
    if (!actionElem)
        return;
    if (Frame* f = actionElem->document()->frame())
        f->loader()->resetMultipleFormSubmissionProtection();
    actionElem->accessKeyAction(true);
}

803
RenderObject* AccessibilityObject::topRenderer() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
804
805
806
807
{
    return m_renderer->document()->topDocument()->renderer();
}

808
RenderTextControl* AccessibilityObject::textControl() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
809
{
810
    if (!isTextControl())
alice.liu@apple.com's avatar
alice.liu@apple.com committed
811
        return 0;
812

alice.liu@apple.com's avatar
alice.liu@apple.com committed
813
814
815
    return static_cast<RenderTextControl*>(m_renderer);
}

816
Widget* AccessibilityObject::widget() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
817
818
819
820
821
822
823
{
    if (!m_renderer->isWidget())
        return 0;

    return static_cast<RenderWidget*>(m_renderer)->widget();
}

824
AXObjectCache* AccessibilityObject::axObjectCache() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
825
826
827
828
{
    return m_renderer->document()->axObjectCache();
}

829
void AccessibilityObject::getDocumentLinks(Vector<RefPtr<AccessibilityObject> >& result) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
{
    Document* document = m_renderer->document();
    RefPtr<HTMLCollection> coll = document->links();
    Node* curr = coll->firstItem();
    while (curr) {
        RenderObject* obj = curr->renderer();
        if (obj) {
            RefPtr<AccessibilityObject> axobj = document->axObjectCache()->get(obj);
            ASSERT(axobj);
            ASSERT(axobj->roleValue() == WebCoreLinkRole);
            if (!axobj->accessibilityIsIgnored())
                result.append(axobj);
        }
        curr = coll->nextItem();
    }
}

847
FrameView* AccessibilityObject::documentFrameView() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
848
{
849
850
851
    if (!m_renderer || !m_renderer->document())
        return 0;
        
alice.liu@apple.com's avatar
alice.liu@apple.com committed
852
853
854
855
    // this is the RenderObject's Document's Frame's FrameView
    return m_renderer->document()->view();
}

856
Widget* AccessibilityObject::widgetForAttachmentView() const
857
858
859
860
861
862
{
    if (!isAttachment())
        return 0;
    return static_cast<RenderWidget*>(m_renderer)->widget();
}

863
FrameView* AccessibilityObject::frameViewIfRenderView() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
864
865
866
867
{
    if (!m_renderer->isRenderView())
        return 0;
    // this is the RenderObject's Document's renderer's FrameView
868
    return m_renderer->view()->frameView();
alice.liu@apple.com's avatar
alice.liu@apple.com committed
869
870
}

871
// This function is like a cross-platform version of - (WebCoreTextMarkerRange*)textMarkerRange. It returns
alice.liu@apple.com's avatar
alice.liu@apple.com committed
872
// a Range that we can convert to a WebCoreTextMarkerRange in the Obj-C file
873
VisiblePositionRange AccessibilityObject::visiblePositionRange() const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
874
875
876
{
    if (!m_renderer)
        return VisiblePositionRange();
877

alice.liu@apple.com's avatar
alice.liu@apple.com committed
878
879
880
881
    // construct VisiblePositions for start and end
    Node* node = m_renderer->element();
    VisiblePosition startPos = VisiblePosition(node, 0, VP_DEFAULT_AFFINITY);
    VisiblePosition endPos = VisiblePosition(node, maxDeepOffset(node), VP_DEFAULT_AFFINITY);
882

alice.liu@apple.com's avatar
alice.liu@apple.com committed
883
884
885
886
887
888
    // the VisiblePositions are equal for nodes like buttons, so adjust for that
    if (startPos == endPos) {
        endPos = endPos.next();
        if (endPos.isNull())
            endPos = startPos;
    }
889

alice.liu@apple.com's avatar
alice.liu@apple.com committed
890
891
892
    return VisiblePositionRange(startPos, endPos);
}

893
VisiblePositionRange AccessibilityObject::doAXTextMarkerRangeForLine(unsigned lineCount) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
894
895
896
{
    if (lineCount == 0 || !m_renderer)
        return VisiblePositionRange();
897

alice.liu@apple.com's avatar
alice.liu@apple.com committed
898
899
900
901
902
903
904
905
906
907
908
    // iterate over the lines
    // FIXME: this is wrong when lineNumber is lineCount+1,  because nextLinePosition takes you to the
    // last offset of the last line
    VisiblePosition visiblePos = m_renderer->document()->renderer()->positionForCoordinates(0, 0);
    VisiblePosition savedVisiblePos;
    while (--lineCount != 0) {
        savedVisiblePos = visiblePos;
        visiblePos = nextLinePosition(visiblePos, 0);
        if (visiblePos.isNull() || visiblePos == savedVisiblePos)
            return VisiblePositionRange();
    }
909

alice.liu@apple.com's avatar
alice.liu@apple.com committed
910
911
912
913
914
915
916
917
918
919
920
    // make a caret selection for the marker position, then extend it to the line
    // NOTE: ignores results of sel.modify because it returns false when
    // starting at an empty line.  The resulting selection in that case
    // will be a caret at visiblePos.
    SelectionController selectionController;
    selectionController.setSelection(Selection(visiblePos));
    selectionController.modify(SelectionController::EXTEND, SelectionController::RIGHT, LineBoundary);

    return VisiblePositionRange(selectionController.selection().visibleStart(), selectionController.selection().visibleEnd());
}

921
VisiblePositionRange AccessibilityObject::doAXTextMarkerRangeForUnorderedTextMarkers(const VisiblePosition& visiblePos1, const VisiblePosition& visiblePos2) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
922
923
924
{
    if (visiblePos1.isNull() || visiblePos2.isNull())
        return VisiblePositionRange();
925

alice.liu@apple.com's avatar
alice.liu@apple.com committed
926
927
928
    VisiblePosition startPos;
    VisiblePosition endPos;
    bool alreadyInOrder;
929

alice.liu@apple.com's avatar
alice.liu@apple.com committed
930
    // upstream is ordered before downstream for the same position
931
    if (visiblePos1 == visiblePos2 && visiblePos2.affinity() == UPSTREAM)
alice.liu@apple.com's avatar
alice.liu@apple.com committed
932
        alreadyInOrder = false;
933

alice.liu@apple.com's avatar
alice.liu@apple.com committed
934
    // use selection order to see if the positions are in order
935
    else
alice.liu@apple.com's avatar
alice.liu@apple.com committed
936
        alreadyInOrder = Selection(visiblePos1, visiblePos2).isBaseFirst();
937

alice.liu@apple.com's avatar
alice.liu@apple.com committed
938
939
940
941
942
943
944
    if (alreadyInOrder) {
        startPos = visiblePos1;
        endPos = visiblePos2;
    } else {
        startPos = visiblePos2;
        endPos = visiblePos1;
    }
945

alice.liu@apple.com's avatar
alice.liu@apple.com committed
946
947
948
    return VisiblePositionRange(startPos, endPos);
}

949
VisiblePositionRange AccessibilityObject::doAXLeftWordTextMarkerRangeForTextMarker(const VisiblePosition& visiblePos) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
950
951
952
953
954
955
{
    VisiblePosition startPosition = startOfWord(visiblePos, LeftWordIfOnBoundary);
    VisiblePosition endPosition = endOfWord(startPosition);
    return VisiblePositionRange(startPosition, endPosition);
}

956
VisiblePositionRange AccessibilityObject::doAXRightWordTextMarkerRangeForTextMarker(const VisiblePosition& visiblePos) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
{
    VisiblePosition startPosition = startOfWord(visiblePos, RightWordIfOnBoundary);
    VisiblePosition endPosition = endOfWord(startPosition);
    return VisiblePositionRange(startPosition, endPosition);
}

static VisiblePosition updateAXLineStartForVisiblePosition(const VisiblePosition& visiblePosition)
{
    // A line in the accessibility sense should include floating objects, such as aligned image, as part of a line.
    // So let's update the position to include that.
    VisiblePosition tempPosition;
    VisiblePosition startPosition = visiblePosition;
    Position p;
    RenderObject* renderer;
    while (true) {
        tempPosition = startPosition.previous();
        if (tempPosition.isNull())
            break;
        p = tempPosition.deepEquivalent();
        if (!p.node())
            break;
        renderer = p.node()->renderer();
        if (!renderer || renderer->inlineBox(p.offset(), tempPosition.affinity()) || (renderer->isRenderBlock() && p.offset() == 0))
            break;
        startPosition = tempPosition;
    }
983

alice.liu@apple.com's avatar
alice.liu@apple.com committed
984
985
986
    return startPosition;
}

987
VisiblePositionRange AccessibilityObject::doAXLeftLineTextMarkerRangeForTextMarker(const VisiblePosition& visiblePos) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
988
989
990
{
    if (visiblePos.isNull())
        return VisiblePositionRange();
991

alice.liu@apple.com's avatar
alice.liu@apple.com committed
992
993
994
995
996
    // make a caret selection for the position before marker position (to make sure
    // we move off of a line start)
    VisiblePosition prevVisiblePos = visiblePos.previous();
    if (prevVisiblePos.isNull())
        return VisiblePositionRange();
997

alice.liu@apple.com's avatar
alice.liu@apple.com committed
998
999
1000
    VisiblePosition startPosition = startOfLine(prevVisiblePos);

    // keep searching for a valid line start position.  Unless the textmarker is at the very beginning, there should
1001
1002
    // always be a valid line range.  However, startOfLine will return null for position next to a floating object,
    // since floating object doesn't really belong to any line.
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1003
1004
1005
1006
1007
1008
    // This check will reposition the marker before the floating object, to ensure we get a line start.
    if (startPosition.isNull()) {
        while (startPosition.isNull() && prevVisiblePos.isNotNull()) {
            prevVisiblePos = prevVisiblePos.previous();
            startPosition = startOfLine(prevVisiblePos);
        }
1009
    } else
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1010
        startPosition = updateAXLineStartForVisiblePosition(startPosition);
1011

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1012
1013
1014
1015
    VisiblePosition endPosition = endOfLine(prevVisiblePos);
    return VisiblePositionRange(startPosition, endPosition);
}

1016
VisiblePositionRange AccessibilityObject::doAXRightLineTextMarkerRangeForTextMarker(const VisiblePosition& visiblePos) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1017
1018
1019
{
    if (visiblePos.isNull())
        return VisiblePositionRange();
1020

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1021
1022
1023
1024
    // make sure we move off of a line end
    VisiblePosition nextVisiblePos = visiblePos.next();
    if (nextVisiblePos.isNull())
        return VisiblePositionRange();
1025

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1026
    VisiblePosition startPosition = startOfLine(nextVisiblePos);
1027

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1028
1029
1030
1031
    // fetch for a valid line start position
    if (startPosition.isNull() ) {
        startPosition = visiblePos;
        nextVisiblePos = nextVisiblePos.next();
1032
    } else
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1033
        startPosition = updateAXLineStartForVisiblePosition(startPosition);
1034

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1035
1036
1037
    VisiblePosition endPosition = endOfLine(nextVisiblePos);

    // as long as the position hasn't reached the end of the doc,  keep searching for a valid line end position
1038
1039
    // Unless the textmarker is at the very end, there should always be a valid line range.  However, endOfLine will
    // return null for position by a floating object, since floating object doesn't really belong to any line.
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1040
1041
1042
1043
1044
    // This check will reposition the marker after the floating object, to ensure we get a line end.
    while (endPosition.isNull() && nextVisiblePos.isNotNull()) {
        nextVisiblePos = nextVisiblePos.next();
        endPosition = endOfLine(nextVisiblePos);
    }
1045

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1046
1047
1048
    return VisiblePositionRange(startPosition, endPosition);
}

1049
VisiblePositionRange AccessibilityObject::doAXSentenceTextMarkerRangeForTextMarker(const VisiblePosition& visiblePos) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1050
1051
1052
1053
1054
1055
1056
1057
{
    // FIXME: FO 2 IMPLEMENT (currently returns incorrect answer)
    // Related? <rdar://problem/3927736> Text selection broken in 8A336
    VisiblePosition startPosition = startOfSentence(visiblePos);
    VisiblePosition endPosition = endOfSentence(startPosition);
    return VisiblePositionRange(startPosition, endPosition);
}

1058
VisiblePositionRange AccessibilityObject::doAXParagraphTextMarkerRangeForTextMarker(const VisiblePosition& visiblePos) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
{
    VisiblePosition startPosition = startOfParagraph(visiblePos);
    VisiblePosition endPosition = endOfParagraph(startPosition);
    return VisiblePositionRange(startPosition, endPosition);
}

static VisiblePosition startOfStyleRange(const VisiblePosition visiblePos)
{
    RenderObject* renderer = visiblePos.deepEquivalent().node()->renderer();
    RenderObject* startRenderer = renderer;
    RenderStyle* style = renderer->style();
1070

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1071
1072
1073
1074
1075
    // traverse backward by renderer to look for style change
    for (RenderObject* r = renderer->previousInPreOrder(); r; r = r->previousInPreOrder()) {
        // skip non-leaf nodes
        if (r->firstChild())
            continue;
1076

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1077
1078
1079
        // stop at style change
        if (r->style() != style)
            break;
1080

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1081
1082
1083
        // remember match
        startRenderer = r;
    }
1084

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
    return VisiblePosition(startRenderer->node(), 0, VP_DEFAULT_AFFINITY);
}

static VisiblePosition endOfStyleRange(const VisiblePosition visiblePos)
{
    RenderObject* renderer = visiblePos.deepEquivalent().node()->renderer();
    RenderObject* endRenderer = renderer;
    RenderStyle* style = renderer->style();

    // traverse forward by renderer to look for style change
    for (RenderObject* r = renderer->nextInPreOrder(); r; r = r->nextInPreOrder()) {
        // skip non-leaf nodes
        if (r->firstChild())
            continue;
1099

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1100
1101
1102
        // stop at style change
        if (r->style() != style)
            break;
1103

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1104
1105
1106
        // remember match
        endRenderer = r;
    }
1107

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1108
1109
1110
    return VisiblePosition(endRenderer->node(), maxDeepOffset(endRenderer->node()), VP_DEFAULT_AFFINITY);
}

1111
VisiblePositionRange AccessibilityObject::doAXStyleTextMarkerRangeForTextMarker(const VisiblePosition& visiblePos) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1112
1113
1114
1115
1116
1117
1118
1119
{
    if (visiblePos.isNull())
        return VisiblePositionRange();

    return VisiblePositionRange(startOfStyleRange(visiblePos), endOfStyleRange(visiblePos));
}

// NOTE: Consider providing this utility method as AX API
1120
VisiblePositionRange AccessibilityObject::textMarkerRangeForRange(const PlainTextRange& range, RenderTextControl* textControl) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1121
1122
1123
{
    if (range.start + range.length > textControl->text().length())
        return VisiblePositionRange();
1124

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1125
1126
1127
1128
1129
1130
    VisiblePosition startPosition = textControl->visiblePositionForIndex(range.start);
    startPosition.setAffinity(DOWNSTREAM);
    VisiblePosition endPosition = textControl->visiblePositionForIndex(range.start + range.length);
    return VisiblePositionRange(startPosition, endPosition);
}

1131
static String stringForReplacedNode(Node* replacedNode)
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
{
    // 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 String();
    }

    // create an AX object, but skip it if it is not supposed to be seen
    AccessibilityObject* object = replacedNode->renderer()->document()->axObjectCache()->get(replacedNode->renderer());
    if (object->accessibilityIsIgnored())
        return String();
1144

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1145
1146
1147
    return String(&objectReplacementCharacter, 1);
}

1148
String AccessibilityObject::doAXStringForTextMarkerRange(const VisiblePositionRange& visiblePositionRange) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1149
1150
1151
{
    if (visiblePositionRange.isNull())
        return String();
1152

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
    String resultString;
    TextIterator it(makeRange(visiblePositionRange.start, visiblePositionRange.end).get());
    while (!it.atEnd()) {
        // non-zero length means textual node, zero length means replaced node (AKA "attachments" in AX)
        if (it.length() != 0) {
            resultString.append(it.characters(), it.length());
        } else {
            // locate the node and starting offset for this replaced range
            int exception = 0;
            Node* node = it.range()->startContainer(exception);
            ASSERT(node == it.range()->endContainer(exception));
            int offset = it.range()->startOffset(exception);
            String attachmentString = stringForReplacedNode(node->childNode(offset));
1166

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1167
1168
1169
1170
1171
1172
            // append the replacement string
            if (!attachmentString.isNull())
                resultString.append(attachmentString);
        }
        it.advance();
    }
1173

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1174
1175
1176
    return resultString.isEmpty() ? String() : resultString;
}

1177
IntRect AccessibilityObject::doAXBoundsForTextMarkerRange(const VisiblePositionRange& visiblePositionRange) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1178
1179
1180
{
    if (visiblePositionRange.isNull())
        return IntRect();
1181

1182
1183
1184
1185
    // Create a mutable VisiblePositionRange.
    VisiblePositionRange range(visiblePositionRange);
    IntRect rect1 = range.start.caretRect();
    IntRect rect2 = range.end.caretRect();
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1186

1187
    // readjust for position at the edge of a line.  This is to exclude line rect that doesn't need to be accounted in the range bounds
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1188
    if (rect2.y() != rect1.y()) {
1189
1190
1191
1192
        VisiblePosition endOfFirstLine = endOfLine(range.start);
        if (range.start == endOfFirstLine) {
            range.start.setAffinity(DOWNSTREAM);
            rect1 = range.start.caretRect();
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1193
        }
1194
1195
1196
        if (range.end == endOfFirstLine) {
            range.end.setAffinity(UPSTREAM);
            rect2 = range.end.caretRect();
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1197
1198
1199
1200
1201
1202
1203
1204
        }
    }

    IntRect ourrect = rect1;
    ourrect.unite(rect2);

    // if the rectangle spans lines and contains multiple text chars, use the range's bounding box intead
    if (rect1.bottom() != rect2.bottom()) {
1205
        RefPtr<Range> dataRange = makeRange(range.start, range.end);
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1206
1207
        IntRect boundingBox = dataRange->boundingBox();
        String rangeString = plainText(dataRange.get());
1208
        if (rangeString.length() > 1 && !boundingBox.isEmpty())
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1209
1210
1211
            ourrect = boundingBox;
    }

1212
1213
1214
1215
1216
#if PLATFORM(MAC)
    return m_renderer->document()->view()->contentsToScreen(ourrect);
#else
    return ourrect;
#endif
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1217
1218
}

1219
int AccessibilityObject::doAXLengthForTextMarkerRange(const VisiblePositionRange& visiblePositionRange) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
{
    // FIXME: Multi-byte support
    String string = doAXStringForTextMarkerRange(visiblePositionRange);
    if (string.isEmpty())
        return -1;

    return string.length();
}

1229
void AccessibilityObject::doSetAXSelectedTextMarkerRange(const VisiblePositionRange& textMarkerRange) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1230
1231
1232
{
    if (textMarkerRange.start.isNull() || textMarkerRange.end.isNull())
        return;
1233

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1234
1235
1236
1237
1238
    // make selection and tell the document to use it
    Selection newSelection = Selection(textMarkerRange.start, textMarkerRange.end);
    m_renderer->document()->frame()->selectionController()->setSelection(newSelection);
}

1239
VisiblePosition AccessibilityObject::doAXTextMarkerForPosition(const IntPoint& point) const
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1240
1241
1242
1243
1244
{
    // convert absolute point to view coordinates
    FrameView* frameView = m_renderer->document()->topDocument()->renderer()->view()->frameView();
    RenderObject* renderer = topRenderer();
    Node* innerNode = 0;
1245

alice.liu@apple.com's avatar
alice.liu@apple.com committed
1246
1247
1248
    // locate the node containing the point
    IntPoint pointResult;
    while (1) {
1249
1250
1251
1252
1253
1254
        IntPoint ourpoint;
#if PLATFORM(MAC)
        ourpoint = frameView->screenToContents(point);
#else
        ourpoint = point;
#endif
alice.liu@apple.com's avatar
alice.liu@apple.com committed
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
        HitTestRequest request(true, true);
        HitTestResult result(ourpoint);
        renderer->layer()->hitTest(request, result);
        innerNode = result.innerNode();
        if (!innerNode || !innerNode->renderer())
            return VisiblePosition();

        pointResult = result.localPoint();

        // done if hit something other than a widget
        renderer = innerNode->renderer();
        if (!renderer</