AccessibilityRenderObject.cpp [plain text]
#include "config.h"
#include "AccessibilityRenderObject.h"
#include "AXObjectCache.h"
#include "AccessibilityImageMapLink.h"
#include "AccessibilityListBox.h"
#include "AccessibilitySVGRoot.h"
#include "AccessibilitySpinButton.h"
#include "AccessibilityTable.h"
#include "CachedImage.h"
#include "Chrome.h"
#include "ElementIterator.h"
#include "EventNames.h"
#include "FloatRect.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameSelection.h"
#include "HTMLAreaElement.h"
#include "HTMLFormElement.h"
#include "HTMLFrameElementBase.h"
#include "HTMLImageElement.h"
#include "HTMLInputElement.h"
#include "HTMLLabelElement.h"
#include "HTMLMapElement.h"
#include "HTMLNames.h"
#include "HTMLOptGroupElement.h"
#include "HTMLOptionElement.h"
#include "HTMLOptionsCollection.h"
#include "HTMLSelectElement.h"
#include "HTMLTableElement.h"
#include "HTMLTextAreaElement.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "Image.h"
#include "LocalizedStrings.h"
#include "MathMLNames.h"
#include "NodeList.h"
#include "Page.h"
#include "ProgressTracker.h"
#include "RenderButton.h"
#include "RenderFieldset.h"
#include "RenderFileUploadControl.h"
#include "RenderHTMLCanvas.h"
#include "RenderImage.h"
#include "RenderInline.h"
#include "RenderIterator.h"
#include "RenderLayer.h"
#include "RenderLineBreak.h"
#include "RenderListBox.h"
#include "RenderListMarker.h"
#include "RenderMathMLBlock.h"
#include "RenderMathMLFraction.h"
#include "RenderMathMLOperator.h"
#include "RenderMathMLRoot.h"
#include "RenderMenuList.h"
#include "RenderSVGRoot.h"
#include "RenderSVGShape.h"
#include "RenderText.h"
#include "RenderTextControl.h"
#include "RenderTextControlSingleLine.h"
#include "RenderTextFragment.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "RenderWidget.h"
#include "RenderedPosition.h"
#include "SVGDocument.h"
#include "SVGImage.h"
#include "SVGImageChromeClient.h"
#include "SVGNames.h"
#include "SVGSVGElement.h"
#include "Text.h"
#include "TextControlInnerElements.h"
#include "TextIterator.h"
#include "VisibleUnits.h"
#include "htmlediting.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/unicode/CharacterNames.h>
namespace WebCore {
using namespace HTMLNames;
AccessibilityRenderObject::AccessibilityRenderObject(RenderObject* renderer)
: AccessibilityNodeObject(renderer->node())
, m_renderer(renderer)
{
#ifndef NDEBUG
m_renderer->setHasAXObject(true);
#endif
}
AccessibilityRenderObject::~AccessibilityRenderObject()
{
ASSERT(isDetached());
}
void AccessibilityRenderObject::init()
{
AccessibilityNodeObject::init();
}
PassRefPtr<AccessibilityRenderObject> AccessibilityRenderObject::create(RenderObject* renderer)
{
return adoptRef(new AccessibilityRenderObject(renderer));
}
void AccessibilityRenderObject::detach(AccessibilityDetachmentType detachmentType, AXObjectCache* cache)
{
AccessibilityNodeObject::detach(detachmentType, cache);
detachRemoteSVGRoot();
#ifndef NDEBUG
if (m_renderer)
m_renderer->setHasAXObject(false);
#endif
m_renderer = 0;
}
RenderBoxModelObject* AccessibilityRenderObject::renderBoxModelObject() const
{
if (!m_renderer || !m_renderer->isBoxModelObject())
return 0;
return toRenderBoxModelObject(m_renderer);
}
void AccessibilityRenderObject::setRenderer(RenderObject* renderer)
{
m_renderer = renderer;
setNode(renderer->node());
}
static inline bool isInlineWithContinuation(RenderObject* object)
{
if (!object->isBoxModelObject())
return false;
RenderBoxModelObject* renderer = toRenderBoxModelObject(object);
if (!renderer->isRenderInline())
return false;
return toRenderInline(renderer)->continuation();
}
static inline RenderObject* firstChildInContinuation(RenderInline& renderer)
{
auto continuation = renderer.continuation();
while (continuation) {
if (continuation->isRenderBlock())
return continuation;
if (RenderObject* child = continuation->firstChild())
return child;
continuation = toRenderInline(continuation)->continuation();
}
return nullptr;
}
static inline RenderObject* firstChildConsideringContinuation(RenderObject* renderer)
{
RenderObject* firstChild = renderer->firstChildSlow();
if (!firstChild && isInlineWithContinuation(renderer))
firstChild = firstChildInContinuation(toRenderInline(*renderer));
return firstChild;
}
static inline RenderObject* lastChildConsideringContinuation(RenderObject* renderer)
{
RenderObject* lastChild = renderer->lastChildSlow();
RenderObject* prev;
RenderObject* cur = renderer;
if (!cur->isRenderInline() && !cur->isRenderBlock())
return renderer;
while (cur) {
prev = cur;
if (RenderObject* lc = cur->lastChildSlow())
lastChild = lc;
if (cur->isRenderInline()) {
cur = toRenderInline(cur)->inlineElementContinuation();
ASSERT_UNUSED(prev, cur || !toRenderInline(prev)->continuation());
} else
cur = toRenderBlock(cur)->inlineElementContinuation();
}
return lastChild;
}
AccessibilityObject* AccessibilityRenderObject::firstChild() const
{
if (!m_renderer)
return 0;
RenderObject* firstChild = firstChildConsideringContinuation(m_renderer);
if (!firstChild && !canHaveChildren())
return AccessibilityNodeObject::firstChild();
return axObjectCache()->getOrCreate(firstChild);
}
AccessibilityObject* AccessibilityRenderObject::lastChild() const
{
if (!m_renderer)
return 0;
RenderObject* lastChild = lastChildConsideringContinuation(m_renderer);
if (!lastChild && !canHaveChildren())
return AccessibilityNodeObject::lastChild();
return axObjectCache()->getOrCreate(lastChild);
}
static inline RenderInline* startOfContinuations(RenderObject* r)
{
if (r->isInlineElementContinuation() && r->node() && r->node()->renderer() && r->node()->renderer()->isRenderInline())
return toRenderInline(r->node()->renderer());
if (r->isRenderBlock() && toRenderBlock(r)->inlineElementContinuation())
return toRenderInline(toRenderBlock(r)->inlineElementContinuation()->element()->renderer());
return 0;
}
static inline RenderObject* endOfContinuations(RenderObject* renderer)
{
RenderObject* prev = renderer;
RenderObject* cur = renderer;
if (!cur->isRenderInline() && !cur->isRenderBlock())
return renderer;
while (cur) {
prev = cur;
if (cur->isRenderInline()) {
cur = toRenderInline(cur)->inlineElementContinuation();
ASSERT(cur || !toRenderInline(prev)->continuation());
} else
cur = toRenderBlock(cur)->inlineElementContinuation();
}
return prev;
}
static inline RenderObject* childBeforeConsideringContinuations(RenderInline* r, RenderObject* child)
{
RenderBoxModelObject* curContainer = r;
RenderObject* cur = 0;
RenderObject* prev = 0;
while (curContainer) {
if (curContainer->isRenderInline()) {
cur = curContainer->firstChild();
while (cur) {
if (cur == child)
return prev;
prev = cur;
cur = cur->nextSibling();
}
curContainer = toRenderInline(curContainer)->continuation();
} else if (curContainer->isRenderBlock()) {
if (curContainer == child)
return prev;
prev = curContainer;
curContainer = toRenderBlock(curContainer)->inlineElementContinuation();
}
}
ASSERT_NOT_REACHED();
return 0;
}
static inline bool firstChildIsInlineContinuation(RenderObject* renderer)
{
RenderObject* child = renderer->firstChildSlow();
return child && child->isInlineElementContinuation();
}
AccessibilityObject* AccessibilityRenderObject::previousSibling() const
{
if (!m_renderer)
return 0;
RenderObject* previousSibling = 0;
RenderInline* startOfConts;
if (m_renderer->isRenderBlock() && (startOfConts = startOfContinuations(m_renderer)))
previousSibling = childBeforeConsideringContinuations(startOfConts, m_renderer);
else if (m_renderer->isAnonymousBlock() && firstChildIsInlineContinuation(m_renderer)) {
RenderObject* firstParent = startOfContinuations(m_renderer->firstChildSlow())->parent();
while (firstChildIsInlineContinuation(firstParent))
firstParent = startOfContinuations(firstParent->firstChildSlow())->parent();
previousSibling = firstParent->previousSibling();
}
else if (RenderObject* ps = m_renderer->previousSibling())
previousSibling = ps;
else if (m_renderer->parent()->isRenderInline() && (startOfConts = startOfContinuations(m_renderer->parent())))
previousSibling = childBeforeConsideringContinuations(startOfConts, m_renderer->parent()->firstChild());
if (!previousSibling)
return 0;
return axObjectCache()->getOrCreate(previousSibling);
}
static inline bool lastChildHasContinuation(RenderObject* renderer)
{
RenderObject* child = renderer->lastChildSlow();
return child && isInlineWithContinuation(child);
}
AccessibilityObject* AccessibilityRenderObject::nextSibling() const
{
if (!m_renderer)
return 0;
RenderObject* nextSibling = 0;
RenderInline* inlineContinuation;
if (m_renderer->isRenderBlock() && (inlineContinuation = toRenderBlock(m_renderer)->inlineElementContinuation()))
nextSibling = firstChildConsideringContinuation(inlineContinuation);
else if (m_renderer->isAnonymousBlock() && lastChildHasContinuation(m_renderer)) {
RenderElement* lastParent = endOfContinuations(toRenderBlock(m_renderer)->lastChild())->parent();
while (lastChildHasContinuation(lastParent))
lastParent = endOfContinuations(lastParent->lastChild())->parent();
nextSibling = lastParent->nextSibling();
}
else if (RenderObject* ns = m_renderer->nextSibling())
nextSibling = ns;
else if (isInlineWithContinuation(m_renderer))
nextSibling = endOfContinuations(m_renderer)->nextSibling();
else if (isInlineWithContinuation(m_renderer->parent())) {
auto continuation = toRenderInline(m_renderer->parent())->continuation();
if (continuation->isRenderBlock())
nextSibling = continuation;
else
nextSibling = firstChildConsideringContinuation(continuation);
}
if (!nextSibling)
return 0;
return axObjectCache()->getOrCreate(nextSibling);
}
static RenderBoxModelObject* nextContinuation(RenderObject* renderer)
{
ASSERT(renderer);
if (renderer->isRenderInline() && !renderer->isReplaced())
return toRenderInline(renderer)->continuation();
if (renderer->isRenderBlock())
return toRenderBlock(renderer)->inlineElementContinuation();
return 0;
}
RenderObject* AccessibilityRenderObject::renderParentObject() const
{
if (!m_renderer)
return 0;
RenderElement* parent = m_renderer->parent();
RenderInline* startOfConts = nullptr;
RenderObject* firstChild = 0;
if (m_renderer->isRenderBlock() && (startOfConts = startOfContinuations(m_renderer)))
parent = startOfConts;
else if (parent && parent->isRenderInline() && (startOfConts = startOfContinuations(parent)))
parent = startOfConts;
else if (parent && (firstChild = parent->firstChild()) && firstChild->node()) {
RenderObject* nodeRenderFirstChild = firstChild->node()->renderer();
while (nodeRenderFirstChild != firstChild) {
for (RenderObject* contsTest = nodeRenderFirstChild; contsTest; contsTest = nextContinuation(contsTest)) {
if (contsTest == firstChild) {
parent = nodeRenderFirstChild->parent();
break;
}
}
RenderObject* parentFirstChild = parent->firstChild();
if (firstChild == parentFirstChild)
break;
firstChild = parentFirstChild;
if (!firstChild->node())
break;
nodeRenderFirstChild = firstChild->node()->renderer();
}
}
return parent;
}
AccessibilityObject* AccessibilityRenderObject::parentObjectIfExists() const
{
AXObjectCache* cache = axObjectCache();
if (!cache)
return nullptr;
if (isWebArea())
return cache->get(&m_renderer->view().frameView());
return cache->get(renderParentObject());
}
AccessibilityObject* AccessibilityRenderObject::parentObject() const
{
if (!m_renderer)
return 0;
if (ariaRoleAttribute() == MenuBarRole)
return axObjectCache()->getOrCreate(m_renderer->parent());
if (ariaRoleAttribute() == MenuRole) {
AccessibilityObject* parent = menuButtonForMenu();
if (parent)
return parent;
}
AXObjectCache* cache = axObjectCache();
if (!cache)
return nullptr;
RenderObject* parentObj = renderParentObject();
if (parentObj)
return cache->getOrCreate(parentObj);
if (isWebArea())
return cache->getOrCreate(&m_renderer->view().frameView());
return 0;
}
bool AccessibilityRenderObject::isAttachment() const
{
RenderBoxModelObject* renderer = renderBoxModelObject();
if (!renderer)
return false;
bool isWidget = renderer->isWidget();
return isWidget && ariaRoleAttribute() == UnknownRole;
}
bool AccessibilityRenderObject::isFileUploadButton() const
{
if (m_renderer && m_renderer->node() && isHTMLInputElement(m_renderer->node())) {
HTMLInputElement* input = toHTMLInputElement(m_renderer->node());
return input->isFileUpload();
}
return false;
}
bool AccessibilityRenderObject::isReadOnly() const
{
ASSERT(m_renderer);
if (isWebArea()) {
if (HTMLElement* body = m_renderer->document().body()) {
if (body->hasEditableStyle())
return false;
}
return !m_renderer->document().hasEditableStyle();
}
return AccessibilityNodeObject::isReadOnly();
}
bool AccessibilityRenderObject::isOffScreen() const
{
ASSERT(m_renderer);
IntRect contentRect = pixelSnappedIntRect(m_renderer->absoluteClippedOverflowRect());
IntRect viewRect = m_renderer->view().frameView().visibleContentRect(ScrollableArea::LegacyIOSDocumentVisibleRect);
viewRect.intersect(contentRect);
return viewRect.isEmpty();
}
Element* AccessibilityRenderObject::anchorElement() const
{
if (!m_renderer)
return 0;
AXObjectCache* cache = axObjectCache();
if (!cache)
return nullptr;
RenderObject* currRenderer;
for (currRenderer = m_renderer; currRenderer && !currRenderer->node(); currRenderer = currRenderer->parent()) {
if (currRenderer->isAnonymousBlock()) {
RenderObject* continuation = toRenderBlock(currRenderer)->continuation();
if (continuation)
return cache->getOrCreate(continuation)->anchorElement();
}
}
if (!currRenderer)
return 0;
Node* node = currRenderer->node();
for ( ; node; node = node->parentNode()) {
if (isHTMLAnchorElement(node) || (node->renderer() && cache->getOrCreate(node->renderer())->isAnchor()))
return toElement(node);
}
return 0;
}
String AccessibilityRenderObject::helpText() const
{
if (!m_renderer)
return String();
const AtomicString& ariaHelp = getAttribute(aria_helpAttr);
if (!ariaHelp.isEmpty())
return ariaHelp;
String describedBy = ariaDescribedByAttribute();
if (!describedBy.isEmpty())
return describedBy;
String description = accessibilityDescription();
for (RenderObject* curr = m_renderer; curr; curr = curr->parent()) {
if (curr->node() && curr->node()->isHTMLElement()) {
const AtomicString& summary = toElement(curr->node())->getAttribute(summaryAttr);
if (!summary.isEmpty())
return summary;
const AtomicString& title = toElement(curr->node())->getAttribute(titleAttr);
if (!title.isEmpty() && description != title)
return title;
}
AccessibilityObject* axObj = axObjectCache()->getOrCreate(curr);
if (axObj) {
AccessibilityRole role = axObj->roleValue();
if (role != GroupRole && role != UnknownRole)
break;
}
}
return String();
}
String AccessibilityRenderObject::textUnderElement(AccessibilityTextUnderElementMode mode) const
{
if (!m_renderer)
return String();
if (m_renderer->isFileUploadControl())
return toRenderFileUploadControl(m_renderer)->buttonValue();
if (m_renderer->isBR())
return ASCIILiteral("\n");
#if ENABLE(MATHML)
if (m_renderer->isText() && m_renderer->isAnonymous() && ancestorsOfType<RenderMathMLOperator>(*m_renderer).first())
return toRenderText(*m_renderer).text();
#endif
if (m_renderer->isText() || mode.childrenInclusion == AccessibilityTextUnderElementMode::TextUnderElementModeIncludeAllChildren) {
Document* nodeDocument = 0;
RefPtr<Range> textRange;
if (Node* node = m_renderer->node()) {
nodeDocument = &node->document();
textRange = rangeOfContents(*node);
} else {
RenderObject* firstChildRenderer = m_renderer->firstChildSlow();
RenderObject* lastChildRenderer = m_renderer->lastChildSlow();
if (firstChildRenderer && lastChildRenderer) {
ASSERT(firstChildRenderer->node());
ASSERT(lastChildRenderer->node());
Node* firstNodeInBlock = firstChildRenderer->node();
Position startPosition = positionInParentBeforeNode(firstNodeInBlock);
Position endPosition = positionInParentAfterNode(lastChildRenderer->node());
nodeDocument = &firstNodeInBlock->document();
textRange = Range::create(*nodeDocument, startPosition, endPosition);
}
}
if (nodeDocument && textRange) {
if (Frame* frame = nodeDocument->frame()) {
if (frame->document() != nodeDocument)
return String();
return plainText(textRange.get(), textIteratorBehaviorForTextRange());
}
}
if (m_renderer->isText()) {
RenderText* renderTextObject = toRenderText(m_renderer);
if (renderTextObject->isTextFragment()) {
const String& altText = toRenderTextFragment(renderTextObject)->altText();
if (!altText.isEmpty())
return altText;
return String(static_cast<RenderTextFragment*>(m_renderer)->contentString());
}
return String(renderTextObject->text());
}
}
return AccessibilityNodeObject::textUnderElement(mode);
}
Node* AccessibilityRenderObject::node() const
{
if (!m_renderer)
return 0;
if (m_renderer->isRenderView())
return &m_renderer->document();
return m_renderer->node();
}
String AccessibilityRenderObject::stringValue() const
{
if (!m_renderer)
return String();
if (isPasswordField())
return passwordFieldValue();
RenderBoxModelObject* cssBox = renderBoxModelObject();
if (ariaRoleAttribute() == StaticTextRole) {
String staticText = text();
if (!staticText.length())
staticText = textUnderElement();
return staticText;
}
if (m_renderer->isText())
return textUnderElement();
if (cssBox && cssBox->isMenuList()) {
HTMLSelectElement* selectElement = toHTMLSelectElement(m_renderer->node());
int selectedIndex = selectElement->selectedIndex();
const Vector<HTMLElement*>& listItems = selectElement->listItems();
if (selectedIndex >= 0 && static_cast<size_t>(selectedIndex) < listItems.size()) {
const AtomicString& overriddenDescription = listItems[selectedIndex]->fastGetAttribute(aria_labelAttr);
if (!overriddenDescription.isNull())
return overriddenDescription;
}
return toRenderMenuList(m_renderer)->text();
}
if (m_renderer->isListMarker())
return toRenderListMarker(*m_renderer).text();
if (isWebArea())
return String();
if (isTextControl())
return text();
if (m_renderer->isFileUploadControl())
return toRenderFileUploadControl(m_renderer)->fileTextValue();
return String();
}
HTMLLabelElement* AccessibilityRenderObject::labelElementContainer() const
{
if (!m_renderer)
return 0;
if (isControl())
return 0;
for (Node* parentNode = m_renderer->node(); parentNode; parentNode = parentNode->parentNode()) {
if (isHTMLLabelElement(parentNode))
return toHTMLLabelElement(parentNode);
}
return 0;
}
void AccessibilityRenderObject::offsetBoundingBoxForRemoteSVGElement(LayoutRect& rect) const
{
for (AccessibilityObject* parent = parentObject(); parent; parent = parent->parentObject()) {
if (parent->isAccessibilitySVGRoot()) {
rect.moveBy(parent->parentObject()->boundingBoxRect().location());
break;
}
}
}
LayoutRect AccessibilityRenderObject::boundingBoxRect() const
{
RenderObject* obj = m_renderer;
if (!obj)
return LayoutRect();
if (obj->node()) obj = obj->node()->renderer();
Vector<FloatQuad> quads;
bool isSVGRoot = false;
if (obj->isSVGRoot())
isSVGRoot = true;
if (obj->isText())
quads = toRenderText(obj)->absoluteQuadsClippedToEllipsis();
else if (isWebArea() || isSVGRoot)
obj->absoluteQuads(quads);
else
obj->absoluteFocusRingQuads(quads);
LayoutRect result = boundingBoxForQuads(obj, quads);
Document* document = this->document();
if (document && document->isSVGDocument())
offsetBoundingBoxForRemoteSVGElement(result);
if (isWebArea())
result.setSize(obj->view().frameView().contentsSize());
return result;
}
LayoutRect AccessibilityRenderObject::checkboxOrRadioRect() const
{
if (!m_renderer)
return LayoutRect();
HTMLLabelElement* label = labelForElement(toElement(m_renderer->node()));
if (!label || !label->renderer())
return boundingBoxRect();
LayoutRect labelRect = axObjectCache()->getOrCreate(label)->elementRect();
labelRect.unite(boundingBoxRect());
return labelRect;
}
LayoutRect AccessibilityRenderObject::elementRect() const
{
if (isCheckboxOrRadio())
return checkboxOrRadioRect();
return boundingBoxRect();
}
bool AccessibilityRenderObject::supportsPath() const
{
if (m_renderer && m_renderer->isSVGShape())
return true;
return false;
}
Path AccessibilityRenderObject::elementPath() const
{
if (m_renderer && m_renderer->isSVGShape() && toRenderSVGShape(m_renderer)->hasPath()) {
Path path = toRenderSVGShape(m_renderer)->path();
if (auto svgRoot = ancestorsOfType<RenderSVGRoot>(*m_renderer).first()) {
LayoutPoint parentOffset = axObjectCache()->getOrCreate(&*svgRoot)->elementRect().location();
path.transform(AffineTransform().translate(parentOffset.x(), parentOffset.y()));
}
return path;
}
return Path();
}
IntPoint AccessibilityRenderObject::clickPoint()
{
if (isHeading() && children().size() == 1)
return children()[0]->clickPoint();
if (!isWebArea() || isReadOnly())
return AccessibilityObject::clickPoint();
VisibleSelection visSelection = selection();
VisiblePositionRange range = VisiblePositionRange(visSelection.visibleStart(), visSelection.visibleEnd());
IntRect bounds = boundsForVisiblePositionRange(range);
#if PLATFORM(COCOA)
bounds.setLocation(m_renderer->view().frameView().screenToContents(bounds.location()));
#endif
return IntPoint(bounds.x() + (bounds.width() / 2), bounds.y() - (bounds.height() / 2));
}
AccessibilityObject* AccessibilityRenderObject::internalLinkElement() const
{
Element* element = anchorElement();
if (!element)
return 0;
if (!isHTMLAnchorElement(element))
return 0;
HTMLAnchorElement* anchor = toHTMLAnchorElement(element);
URL linkURL = anchor->href();
String fragmentIdentifier = linkURL.fragmentIdentifier();
if (fragmentIdentifier.isEmpty())
return 0;
URL documentURL = m_renderer->document().url();
if (!equalIgnoringFragmentIdentifier(documentURL, linkURL))
return 0;
Node* linkedNode = m_renderer->document().findAnchor(fragmentIdentifier);
if (!linkedNode)
return 0;
return firstAccessibleObjectFromNode(linkedNode);
}
ESpeak AccessibilityRenderObject::speakProperty() const
{
if (!m_renderer)
return AccessibilityObject::speakProperty();
return m_renderer->style().speak();
}
void AccessibilityRenderObject::addRadioButtonGroupMembers(AccessibilityChildrenVector& linkedUIElements) const
{
if (!m_renderer || roleValue() != RadioButtonRole)
return;
Node* node = m_renderer->node();
if (!node || !isHTMLInputElement(node))
return;
HTMLInputElement* input = toHTMLInputElement(node);
if (input->form()) {
Vector<Ref<Element>> formElements;
input->form()->getNamedElements(input->name(), formElements);
for (auto& associateElement : formElements) {
if (AccessibilityObject* object = axObjectCache()->getOrCreate(&associateElement.get()))
linkedUIElements.append(object);
}
} else {
RefPtr<NodeList> list = node->document().getElementsByTagName("input");
unsigned len = list->length();
for (unsigned i = 0; i < len; ++i) {
if (isHTMLInputElement(list->item(i))) {
HTMLInputElement* associateElement = toHTMLInputElement(list->item(i));
if (associateElement->isRadioButton() && associateElement->name() == input->name()) {
if (AccessibilityObject* object = axObjectCache()->getOrCreate(associateElement))
linkedUIElements.append(object);
}
}
}
}
}
void AccessibilityRenderObject::linkedUIElements(AccessibilityChildrenVector& linkedUIElements) const
{
ariaFlowToElements(linkedUIElements);
if (isAnchor()) {
AccessibilityObject* linkedAXElement = internalLinkElement();
if (linkedAXElement)
linkedUIElements.append(linkedAXElement);
}
if (roleValue() == RadioButtonRole)
addRadioButtonGroupMembers(linkedUIElements);
}
bool AccessibilityRenderObject::hasTextAlternative() const
{
return ariaAccessibilityDescription().length();
}
bool AccessibilityRenderObject::ariaHasPopup() const
{
return elementAttributeValue(aria_haspopupAttr);
}
void AccessibilityRenderObject::ariaElementsFromAttribute(AccessibilityChildrenVector& children, const QualifiedName& attributeName) const
{
Vector<Element*> elements;
elementsFromAttribute(elements, attributeName);
AXObjectCache* cache = axObjectCache();
for (const auto& element : elements) {
if (AccessibilityObject* axObject = cache->getOrCreate(element))
children.append(axObject);
}
}
bool AccessibilityRenderObject::supportsARIAFlowTo() const
{
return !getAttribute(aria_flowtoAttr).isEmpty();
}
void AccessibilityRenderObject::ariaFlowToElements(AccessibilityChildrenVector& flowTo) const
{
ariaElementsFromAttribute(flowTo, aria_flowtoAttr);
}
bool AccessibilityRenderObject::supportsARIADescribedBy() const
{
return !getAttribute(aria_describedbyAttr).isEmpty();
}
void AccessibilityRenderObject::ariaDescribedByElements(AccessibilityChildrenVector& ariaDescribedBy) const
{
ariaElementsFromAttribute(ariaDescribedBy, aria_describedbyAttr);
}
bool AccessibilityRenderObject::supportsARIAControls() const
{
return !getAttribute(aria_controlsAttr).isEmpty();
}
void AccessibilityRenderObject::ariaControlsElements(AccessibilityChildrenVector& ariaControls) const
{
ariaElementsFromAttribute(ariaControls, aria_controlsAttr);
}
bool AccessibilityRenderObject::supportsARIADropping() const
{
const AtomicString& dropEffect = getAttribute(aria_dropeffectAttr);
return !dropEffect.isEmpty();
}
bool AccessibilityRenderObject::supportsARIADragging() const
{
const AtomicString& grabbed = getAttribute(aria_grabbedAttr);
return equalIgnoringCase(grabbed, "true") || equalIgnoringCase(grabbed, "false");
}
bool AccessibilityRenderObject::isARIAGrabbed()
{
return elementAttributeValue(aria_grabbedAttr);
}
void AccessibilityRenderObject::determineARIADropEffects(Vector<String>& effects)
{
const AtomicString& dropEffects = getAttribute(aria_dropeffectAttr);
if (dropEffects.isEmpty()) {
effects.clear();
return;
}
String dropEffectsString = dropEffects.string();
dropEffectsString.replace('\n', ' ');
dropEffectsString.split(' ', effects);
}
bool AccessibilityRenderObject::exposesTitleUIElement() const
{
if (!isControl())
return false;
if (accessibilityIsIgnored())
return true;
if (hasTextAlternative())
return false;
return true;
}
AccessibilityObject* AccessibilityRenderObject::titleUIElement() const
{
if (!m_renderer)
return 0;
if (isFieldset())
return axObjectCache()->getOrCreate(toRenderFieldset(m_renderer)->findLegend(RenderFieldset::IncludeFloatingOrOutOfFlow));
Node* node = m_renderer->node();
if (!node || !node->isElementNode())
return 0;
HTMLLabelElement* label = labelForElement(toElement(node));
if (label && label->renderer())
return axObjectCache()->getOrCreate(label);
return 0;
}
bool AccessibilityRenderObject::isAllowedChildOfTree() const
{
AccessibilityObject* axObj = parentObject();
bool isInTree = false;
while (axObj) {
if (axObj->isTree()) {
isInTree = true;
break;
}
axObj = axObj->parentObject();
}
if (isInTree) {
AccessibilityRole role = roleValue();
if (role != TreeItemRole && role != StaticTextRole)
return false;
}
return true;
}
static AccessibilityObjectInclusion objectInclusionFromAltText(const String& altText)
{
if (!altText.containsOnlyWhitespace())
return IncludeObject;
if (!altText.isNull())
return IgnoreObject;
return DefaultBehavior;
}
AccessibilityObjectInclusion AccessibilityRenderObject::defaultObjectInclusion() const
{
if (!m_renderer)
return IgnoreObject;
if (m_renderer->style().visibility() != VISIBLE) {
if (equalIgnoringCase(getAttribute(aria_hiddenAttr), "false"))
return DefaultBehavior;
return IgnoreObject;
}
return AccessibilityObject::defaultObjectInclusion();
}
bool AccessibilityRenderObject::computeAccessibilityIsIgnored() const
{
#ifndef NDEBUG
ASSERT(m_initialized);
#endif
if (!m_renderer)
return true;
AccessibilityObjectInclusion decision = defaultObjectInclusion();
if (decision == IncludeObject)
return false;
if (decision == IgnoreObject)
return true;
if (isDescendantOfBarrenParent())
return true;
if (roleValue() == IgnoredRole)
return true;
if (roleValue() == PresentationalRole || inheritsPresentationalRole())
return true;
if (!isAllowedChildOfTree())
return true;
if (isAttachment())
return accessibilityIgnoreAttachment();
if (m_renderer && ancestorsOfType<RenderMenuList>(*m_renderer).first())
return true;
AccessibilityObject* controlObject = correspondingControlForLabelElement();
if (controlObject && !controlObject->exposesTitleUIElement() && controlObject->isCheckboxOrRadio())
return true;
if (m_renderer->isBR())
return true;
if (m_renderer->isText()) {
AccessibilityObject* parent = parentObjectUnignored();
if (parent && (parent->isMenuItem() || parent->ariaRoleAttribute() == MenuButtonRole))
return true;
auto& renderText = toRenderText(*m_renderer);
if (!renderText.hasRenderedText())
return true;
for (AccessibilityObject* parent = parentObject(); parent; parent = parent->parentObject()) {
if (parent->roleValue() == TextFieldRole)
return true;
}
if (renderText.isTextFragment()) {
AccessibilityObjectInclusion altTextInclusion = objectInclusionFromAltText(toRenderTextFragment(&renderText)->altText());
if (altTextInclusion == IgnoreObject)
return true;
if (altTextInclusion == IncludeObject)
return false;
}
return renderText.text()->containsOnlyWhitespace();
}
if (isHeading())
return false;
if (isLink())
return false;
if (isControl())
return false;
if (ariaRoleAttribute() != UnknownRole)
return false;
if (roleValue() == HorizontalRuleRole)
return false;
Node* node = m_renderer->node();
if (node && isHTMLLabelElement(node))
return false;
if (hasContentEditableAttributeSet())
return false;
switch (roleValue()) {
case AudioRole:
case ListItemRole:
case VideoRole:
return false;
default:
break;
}
if (supportsARIAAttributes())
return false;
#if ENABLE(MATHML)
if (isIgnoredElementWithinMathTree())
return true;
if (isMathElement())
return false;
#endif
if (m_renderer->isRenderBlockFlow() && m_renderer->childrenInline() && !canSetFocusAttribute())
return !toRenderBlockFlow(m_renderer)->hasLines() && !mouseButtonListener();
if (isImage()) {
if (canSetFocusAttribute())
return false;
AccessibilityObjectInclusion altTextInclusion = DefaultBehavior;
bool isRenderImage = m_renderer && m_renderer->isRenderImage();
if (isRenderImage)
altTextInclusion = objectInclusionFromAltText(toRenderImage(m_renderer)->altText());
else
altTextInclusion = objectInclusionFromAltText(getAttribute(altAttr).string());
if (altTextInclusion == IgnoreObject)
return true;
if (altTextInclusion == IncludeObject)
return false;
if (!getAttribute(titleAttr).isEmpty())
return false;
if (isRenderImage) {
RenderImage* image = toRenderImage(m_renderer);
if (image->height() <= 1 || image->width() <= 1)
return true;
if (image->cachedImage()) {
LayoutSize imageSize = image->cachedImage()->imageSizeForRenderer(toRenderElement(m_renderer), image->view().zoomFactor());
return imageSize.height() <= 1 || imageSize.width() <= 1;
}
}
return false;
}
if (isCanvas()) {
if (canvasHasFallbackContent())
return false;
if (m_renderer->isBox()) {
RenderBox* canvasBox = toRenderBox(m_renderer);
if (canvasBox->height() <= 1 || canvasBox->width() <= 1)
return true;
}
}
if (isWebArea() || m_renderer->isListMarker())
return false;
if (hasAttributesRequiredForInclusion())
return false;
if (isGenericFocusableElement() && node->firstChild())
return false;
if (node && node->hasTagName(spanTag))
return true;
if (node && node->hasTagName(dfnTag))
return false;
return true;
}
bool AccessibilityRenderObject::isLoaded() const
{
return !m_renderer->document().parser();
}
double AccessibilityRenderObject::estimatedLoadingProgress() const
{
if (!m_renderer)
return 0;
if (isLoaded())
return 1.0;
Page* page = m_renderer->document().page();
if (!page)
return 0;
return page->progress().estimatedProgress();
}
int AccessibilityRenderObject::layoutCount() const
{
if (!m_renderer->isRenderView())
return 0;
return toRenderView(*m_renderer).frameView().layoutCount();
}
String AccessibilityRenderObject::text() const
{
if (isPasswordField())
return passwordFieldValue();
return AccessibilityNodeObject::text();
}
int AccessibilityRenderObject::textLength() const
{
ASSERT(isTextControl());
if (isPasswordField())
return passwordFieldValue().length();
return text().length();
}
PlainTextRange AccessibilityRenderObject::documentBasedSelectedTextRange() const
{
Node* node = m_renderer->node();
if (!node)
return PlainTextRange();
VisibleSelection visibleSelection = selection();
RefPtr<Range> currentSelectionRange = visibleSelection.toNormalizedRange();
if (!currentSelectionRange || !currentSelectionRange->intersectsNode(node, IGNORE_EXCEPTION))
return PlainTextRange();
int start = indexForVisiblePosition(visibleSelection.start());
int end = indexForVisiblePosition(visibleSelection.end());
return PlainTextRange(start, end - start);
}
String AccessibilityRenderObject::selectedText() const
{
ASSERT(isTextControl());
if (isPasswordField())
return String();
if (isNativeTextControl()) {
HTMLTextFormControlElement& textControl = toRenderTextControl(m_renderer)->textFormControlElement();
return textControl.selectedText();
}
return doAXStringForRange(documentBasedSelectedTextRange());
}
const AtomicString& AccessibilityRenderObject::accessKey() const
{
Node* node = m_renderer->node();
if (!node)
return nullAtom;
if (!node->isElementNode())
return nullAtom;
return toElement(node)->getAttribute(accesskeyAttr);
}
VisibleSelection AccessibilityRenderObject::selection() const
{
return m_renderer->frame().selection().selection();
}
PlainTextRange AccessibilityRenderObject::selectedTextRange() const
{
ASSERT(isTextControl());
if (isPasswordField())
return PlainTextRange();
AccessibilityRole ariaRole = ariaRoleAttribute();
if (isNativeTextControl() && ariaRole == UnknownRole) {
HTMLTextFormControlElement& textControl = toRenderTextControl(m_renderer)->textFormControlElement();
return PlainTextRange(textControl.selectionStart(), textControl.selectionEnd() - textControl.selectionStart());
}
return documentBasedSelectedTextRange();
}
void AccessibilityRenderObject::setSelectedTextRange(const PlainTextRange& range)
{
if (isNativeTextControl()) {
HTMLTextFormControlElement& textControl = toRenderTextControl(m_renderer)->textFormControlElement();
textControl.setSelectionRange(range.start, range.start + range.length);
return;
}
Node* node = m_renderer->node();
m_renderer->frame().selection().setSelection(VisibleSelection(Position(node, range.start, Position::PositionIsOffsetInAnchor),
Position(node, range.start + range.length, Position::PositionIsOffsetInAnchor), DOWNSTREAM));
}
URL AccessibilityRenderObject::url() const
{
if (isAnchor() && isHTMLAnchorElement(m_renderer->node())) {
if (HTMLAnchorElement* anchor = toHTMLAnchorElement(anchorElement()))
return anchor->href();
}
if (isWebArea())
return m_renderer->document().url();
if (isImage() && m_renderer->node() && isHTMLImageElement(m_renderer->node()))
return toHTMLImageElement(m_renderer->node())->src();
if (isInputImage())
return toHTMLInputElement(m_renderer->node())->src();
return URL();
}
bool AccessibilityRenderObject::isUnvisited() const
{
return m_renderer->style().isLink() && m_renderer->style().insideLink() == InsideUnvisitedLink;
}
bool AccessibilityRenderObject::isVisited() const
{
return m_renderer->style().isLink() && m_renderer->style().insideLink() == InsideVisitedLink;
}
void AccessibilityRenderObject::setElementAttributeValue(const QualifiedName& attributeName, bool value)
{
if (!m_renderer)
return;
Node* node = m_renderer->node();
if (!node || !node->isElementNode())
return;
Element* element = toElement(node);
element->setAttribute(attributeName, (value) ? "true" : "false");
}
bool AccessibilityRenderObject::elementAttributeValue(const QualifiedName& attributeName) const
{
if (!m_renderer)
return false;
return equalIgnoringCase(getAttribute(attributeName), "true");
}
bool AccessibilityRenderObject::isSelected() const
{
if (!m_renderer)
return false;
Node* node = m_renderer->node();
if (!node)
return false;
const AtomicString& ariaSelected = getAttribute(aria_selectedAttr);
if (equalIgnoringCase(ariaSelected, "true"))
return true;
if (isTabItem() && isTabItemSelected())
return true;
return false;
}
bool AccessibilityRenderObject::isTabItemSelected() const
{
if (!isTabItem() || !m_renderer)
return false;
Node* node = m_renderer->node();
if (!node || !node->isElementNode())
return false;
AccessibilityObject* focusedElement = focusedUIElement();
if (!focusedElement)
return false;
Vector<Element*> elements;
elementsFromAttribute(elements, aria_controlsAttr);
AXObjectCache* cache = axObjectCache();
if (!cache)
return false;
for (const auto& element : elements) {
AccessibilityObject* tabPanel = cache->getOrCreate(element);
if (!tabPanel || tabPanel->roleValue() != TabPanelRole)
continue;
AccessibilityObject* checkFocusElement = focusedElement;
while (checkFocusElement) {
if (tabPanel == checkFocusElement)
return true;
checkFocusElement = checkFocusElement->parentObject();
}
}
return false;
}
bool AccessibilityRenderObject::isFocused() const
{
if (!m_renderer)
return false;
Document& document = m_renderer->document();
Element* focusedElement = document.focusedElement();
if (!focusedElement)
return false;
if (focusedElement == m_renderer->node()
|| (roleValue() == WebAreaRole && document.frame()->selection().isFocusedAndActive()))
return true;
return false;
}
void AccessibilityRenderObject::setFocused(bool on)
{
if (!canSetFocusAttribute())
return;
Document* document = this->document();
Node* node = this->node();
if (!on || !node || !node->isElementNode()) {
document->setFocusedElement(0);
return;
}
if (document->focusedElement() == node)
document->setFocusedElement(0);
toElement(node)->focus();
}
void AccessibilityRenderObject::setSelectedRows(AccessibilityChildrenVector& selectedRows)
{
AccessibilityRole role = roleValue();
if (role != TreeRole && role != TreeGridRole && role != TableRole)
return;
bool isMulti = isMultiSelectable();
unsigned count = selectedRows.size();
if (count > 1 && !isMulti)
count = 1;
for (const auto& selectedRow : selectedRows)
selectedRow->setSelected(true);
}
void AccessibilityRenderObject::setValue(const String& string)
{
if (!m_renderer || !m_renderer->node() || !m_renderer->node()->isElementNode())
return;
Element* element = toElement(m_renderer->node());
if (!m_renderer->isBoxModelObject())
return;
RenderBoxModelObject* renderer = toRenderBoxModelObject(m_renderer);
if (renderer->isTextField()) {
toHTMLInputElement(element)->setValue(string);
} else if (renderer->isTextArea()) {
toHTMLTextAreaElement(element)->setValue(string);
}
}
void AccessibilityRenderObject::ariaOwnsElements(AccessibilityChildrenVector& axObjects) const
{
ariaElementsFromAttribute(axObjects, aria_ownsAttr);
}
bool AccessibilityRenderObject::supportsARIAOwns() const
{
if (!m_renderer)
return false;
const AtomicString& ariaOwns = getAttribute(aria_ownsAttr);
return !ariaOwns.isEmpty();
}
RenderView* AccessibilityRenderObject::topRenderer() const
{
Document* topDoc = topDocument();
if (!topDoc)
return 0;
return topDoc->renderView();
}
Document* AccessibilityRenderObject::document() const
{
if (!m_renderer)
return 0;
return &m_renderer->document();
}
Widget* AccessibilityRenderObject::widget() const
{
if (!m_renderer->isBoxModelObject() || !toRenderBoxModelObject(m_renderer)->isWidget())
return 0;
return toRenderWidget(m_renderer)->widget();
}
AccessibilityObject* AccessibilityRenderObject::accessibilityParentForImageMap(HTMLMapElement* map) const
{
if (!map)
return 0;
HTMLImageElement* imageElement = map->imageElement();
if (!imageElement)
return 0;
if (AXObjectCache* cache = axObjectCache())
return cache->getOrCreate(imageElement);
return nullptr;
}
void AccessibilityRenderObject::getDocumentLinks(AccessibilityChildrenVector& result)
{
Document& document = m_renderer->document();
RefPtr<HTMLCollection> links = document.links();
for (unsigned i = 0; Node* curr = links->item(i); i++) {
RenderObject* obj = curr->renderer();
if (obj) {
RefPtr<AccessibilityObject> axobj = document.axObjectCache()->getOrCreate(obj);
ASSERT(axobj);
if (!axobj->accessibilityIsIgnored() && axobj->isLink())
result.append(axobj);
} else {
Node* parent = curr->parentNode();
if (parent && isHTMLAreaElement(curr) && isHTMLMapElement(parent)) {
AccessibilityImageMapLink* areaObject = toAccessibilityImageMapLink(axObjectCache()->getOrCreate(ImageMapLinkRole));
HTMLMapElement* map = toHTMLMapElement(parent);
areaObject->setHTMLAreaElement(toHTMLAreaElement(curr));
areaObject->setHTMLMapElement(map);
areaObject->setParent(accessibilityParentForImageMap(map));
result.append(areaObject);
}
}
}
}
FrameView* AccessibilityRenderObject::documentFrameView() const
{
if (!m_renderer)
return 0;
return &m_renderer->view().frameView();
}
Widget* AccessibilityRenderObject::widgetForAttachmentView() const
{
if (!isAttachment())
return 0;
return toRenderWidget(m_renderer)->widget();
}
VisiblePositionRange AccessibilityRenderObject::visiblePositionRange() const
{
if (!m_renderer)
return VisiblePositionRange();
Node* node = m_renderer->node();
if (!node)
return VisiblePositionRange();
VisiblePosition startPos = firstPositionInOrBeforeNode(node);
VisiblePosition endPos = lastPositionInOrAfterNode(node);
if (startPos == endPos) {
endPos = endPos.next();
if (endPos.isNull())
endPos = startPos;
}
return VisiblePositionRange(startPos, endPos);
}
VisiblePositionRange AccessibilityRenderObject::visiblePositionRangeForLine(unsigned lineCount) const
{
if (!lineCount || !m_renderer)
return VisiblePositionRange();
VisiblePosition visiblePos = m_renderer->view().positionForPoint(IntPoint(), nullptr);
VisiblePosition savedVisiblePos;
while (--lineCount) {
savedVisiblePos = visiblePos;
visiblePos = nextLinePosition(visiblePos, 0);
if (visiblePos.isNull() || visiblePos == savedVisiblePos)
return VisiblePositionRange();
}
FrameSelection selection;
selection.setSelection(VisibleSelection(visiblePos));
selection.modify(FrameSelection::AlterationExtend, DirectionRight, LineBoundary);
return VisiblePositionRange(selection.selection().visibleStart(), selection.selection().visibleEnd());
}
VisiblePosition AccessibilityRenderObject::visiblePositionForIndex(int index) const
{
if (!m_renderer)
return VisiblePosition();
if (isNativeTextControl())
return toRenderTextControl(m_renderer)->textFormControlElement().visiblePositionForIndex(index);
if (!allowsTextRanges() && !m_renderer->isText())
return VisiblePosition();
Node* node = m_renderer->node();
if (!node)
return VisiblePosition();
return visiblePositionForIndexUsingCharacterIterator(node, index);
}
int AccessibilityRenderObject::indexForVisiblePosition(const VisiblePosition& pos) const
{
if (isNativeTextControl())
return toRenderTextControl(m_renderer)->textFormControlElement().indexForVisiblePosition(pos);
if (!isTextControl())
return 0;
Node* node = m_renderer->node();
if (!node)
return 0;
Position indexPosition = pos.deepEquivalent();
if (indexPosition.isNull() || highestEditableRoot(indexPosition, HasEditableAXRole) != node)
return 0;
#if PLATFORM(GTK)
bool forSelectionPreservation = true;
#else
bool forSelectionPreservation = false;
#endif
return WebCore::indexForVisiblePosition(node, pos, forSelectionPreservation);
}
Element* AccessibilityRenderObject::rootEditableElementForPosition(const Position& position) const
{
Element* result = 0;
Element* rootEditableElement = position.rootEditableElement();
for (Element* e = position.element(); e && e != rootEditableElement; e = e->parentElement()) {
if (nodeIsTextControl(e))
result = e;
if (e->hasTagName(bodyTag))
break;
}
if (result)
return result;
return rootEditableElement;
}
bool AccessibilityRenderObject::nodeIsTextControl(const Node* node) const
{
if (!node)
return false;
if (AXObjectCache* cache = axObjectCache()) {
if (AccessibilityObject* axObjectForNode = cache->getOrCreate(const_cast<Node*>(node)))
return axObjectForNode->isTextControl();
}
return false;
}
IntRect AccessibilityRenderObject::boundsForVisiblePositionRange(const VisiblePositionRange& visiblePositionRange) const
{
if (visiblePositionRange.isNull())
return IntRect();
VisiblePositionRange range(visiblePositionRange);
LayoutRect rect1 = range.start.absoluteCaretBounds();
LayoutRect rect2 = range.end.absoluteCaretBounds();
if (rect2.y() != rect1.y()) {
VisiblePosition endOfFirstLine = endOfLine(range.start);
if (range.start == endOfFirstLine) {
range.start.setAffinity(DOWNSTREAM);
rect1 = range.start.absoluteCaretBounds();
}
if (range.end == endOfFirstLine) {
range.end.setAffinity(UPSTREAM);
rect2 = range.end.absoluteCaretBounds();
}
}
LayoutRect ourrect = rect1;
ourrect.unite(rect2);
if (rect1.maxY() != rect2.maxY()) {
RefPtr<Range> dataRange = makeRange(range.start, range.end);
LayoutRect boundingBox = dataRange->boundingBox();
String rangeString = plainText(dataRange.get());
if (rangeString.length() > 1 && !boundingBox.isEmpty())
ourrect = boundingBox;
}
#if PLATFORM(MAC)
return m_renderer->view().frameView().contentsToScreen(pixelSnappedIntRect(ourrect));
#else
return pixelSnappedIntRect(ourrect);
#endif
}
void AccessibilityRenderObject::setSelectedVisiblePositionRange(const VisiblePositionRange& range) const
{
if (range.start.isNull() || range.end.isNull())
return;
if (range.start == range.end)
m_renderer->frame().selection().moveTo(range.start, UserTriggered);
else {
VisibleSelection newSelection = VisibleSelection(range.start, range.end);
m_renderer->frame().selection().setSelection(newSelection);
}
}
VisiblePosition AccessibilityRenderObject::visiblePositionForPoint(const IntPoint& point) const
{
if (!m_renderer)
return VisiblePosition();
RenderView* renderView = topRenderer();
if (!renderView)
return VisiblePosition();
#if PLATFORM(COCOA)
FrameView* frameView = &renderView->frameView();
#endif
Node* innerNode = 0;
LayoutPoint pointResult;
while (1) {
LayoutPoint ourpoint;
#if PLATFORM(MAC)
ourpoint = frameView->screenToContents(point);
#else
ourpoint = point;
#endif
HitTestRequest request(HitTestRequest::ReadOnly |
HitTestRequest::Active);
HitTestResult result(ourpoint);
renderView->hitTest(request, result);
innerNode = result.innerNode();
if (!innerNode)
return VisiblePosition();
RenderObject* renderer = innerNode->renderer();
if (!renderer)
return VisiblePosition();
pointResult = result.localPoint();
if (!renderer->isWidget())
break;
Widget* widget = toRenderWidget(renderer)->widget();
if (!widget || !widget->isFrameView())
break;
Frame& frame = toFrameView(widget)->frame();
renderView = frame.document()->renderView();
#if PLATFORM(COCOA)
frameView = toFrameView(widget);
#endif
}
return innerNode->renderer()->positionForPoint(pointResult, nullptr);
}
VisiblePosition AccessibilityRenderObject::visiblePositionForIndex(unsigned indexValue, bool lastIndexOK) const
{
if (!isTextControl())
return VisiblePosition();
if (indexValue >= text().length()) {
if (!lastIndexOK || indexValue > text().length())
return VisiblePosition();
}
VisiblePosition position = visiblePositionForIndex(indexValue);
position.setAffinity(DOWNSTREAM);
return position;
}
int AccessibilityRenderObject::index(const VisiblePosition& position) const
{
if (position.isNull() || !isTextControl())
return -1;
if (renderObjectContainsPosition(m_renderer, position.deepEquivalent()))
return indexForVisiblePosition(position);
return -1;
}
void AccessibilityRenderObject::lineBreaks(Vector<int>& lineBreaks) const
{
if (!isTextControl())
return;
VisiblePosition visiblePos = visiblePositionForIndex(0);
VisiblePosition savedVisiblePos = visiblePos;
visiblePos = nextLinePosition(visiblePos, 0);
while (!visiblePos.isNull() && visiblePos != savedVisiblePos) {
lineBreaks.append(indexForVisiblePosition(visiblePos));
savedVisiblePos = visiblePos;
visiblePos = nextLinePosition(visiblePos, 0);
}
}
PlainTextRange AccessibilityRenderObject::doAXRangeForLine(unsigned lineNumber) const
{
if (!isTextControl())
return PlainTextRange();
VisiblePosition visiblePos = visiblePositionForIndex(0);
VisiblePosition savedVisiblePos;
for (unsigned lineCount = lineNumber; lineCount; lineCount -= 1) {
savedVisiblePos = visiblePos;
visiblePos = nextLinePosition(visiblePos, 0);
if (visiblePos.isNull() || visiblePos == savedVisiblePos)
return PlainTextRange();
}
VisiblePosition endPosition = endOfLine(visiblePos);
int index1 = indexForVisiblePosition(visiblePos);
int index2 = indexForVisiblePosition(endPosition);
if (endPosition.affinity() == DOWNSTREAM && endPosition.next().isNotNull())
index2 += 1;
if (index1 == index2)
return PlainTextRange();
return PlainTextRange(index1, index2 - index1);
}
PlainTextRange AccessibilityRenderObject::doAXRangeForIndex(unsigned index) const
{
if (!isTextControl())
return PlainTextRange();
String elementText = text();
if (!elementText.length() || index > elementText.length() - 1)
return PlainTextRange();
return PlainTextRange(index, 1);
}
String AccessibilityRenderObject::doAXStringForRange(const PlainTextRange& range) const
{
if (!range.length)
return String();
if (!isTextControl())
return String();
String elementText = isPasswordField() ? passwordFieldValue() : text();
return elementText.substring(range.start, range.length);
}
IntRect AccessibilityRenderObject::doAXBoundsForRange(const PlainTextRange& range) const
{
if (allowsTextRanges())
return boundsForVisiblePositionRange(visiblePositionRangeForRange(range));
return IntRect();
}
AccessibilityObject* AccessibilityRenderObject::accessibilityImageMapHitTest(HTMLAreaElement* area, const IntPoint& point) const
{
if (!area)
return 0;
AccessibilityObject* parent = 0;
for (Element* mapParent = area->parentElement(); mapParent; mapParent = mapParent->parentElement()) {
if (isHTMLMapElement(mapParent)) {
parent = accessibilityParentForImageMap(toHTMLMapElement(mapParent));
break;
}
}
if (!parent)
return 0;
for (const auto& child : parent->children()) {
if (child->elementRect().contains(point))
return child.get();
}
return 0;
}
AccessibilityObject* AccessibilityRenderObject::remoteSVGElementHitTest(const IntPoint& point) const
{
AccessibilityObject* remote = remoteSVGRootElement();
if (!remote)
return 0;
IntSize offset = point - roundedIntPoint(boundingBoxRect().location());
return remote->accessibilityHitTest(IntPoint(offset));
}
AccessibilityObject* AccessibilityRenderObject::elementAccessibilityHitTest(const IntPoint& point) const
{
if (isSVGImage())
return remoteSVGElementHitTest(point);
return AccessibilityObject::elementAccessibilityHitTest(point);
}
AccessibilityObject* AccessibilityRenderObject::accessibilityHitTest(const IntPoint& point) const
{
if (!m_renderer || !m_renderer->hasLayer())
return 0;
RenderLayer* layer = toRenderBox(m_renderer)->layer();
HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::AccessibilityHitTest);
HitTestResult hitTestResult = HitTestResult(point);
layer->hitTest(request, hitTestResult);
if (!hitTestResult.innerNode())
return 0;
Node* node = hitTestResult.innerNode()->deprecatedShadowAncestorNode();
if (isHTMLAreaElement(node))
return accessibilityImageMapHitTest(toHTMLAreaElement(node), point);
if (isHTMLOptionElement(node))
node = toHTMLOptionElement(node)->ownerSelectElement();
RenderObject* obj = node->renderer();
if (!obj)
return 0;
AccessibilityObject* result = obj->document().axObjectCache()->getOrCreate(obj);
result->updateChildrenIfNecessary();
result = result->elementAccessibilityHitTest(point);
if (result && result->accessibilityIsIgnored()) {
AccessibilityObject* controlObject = result->correspondingControlForLabelElement();
if (controlObject && !controlObject->exposesTitleUIElement())
return controlObject;
result = result->parentObjectUnignored();
}
return result;
}
bool AccessibilityRenderObject::shouldNotifyActiveDescendant() const
{
if (isComboBox())
return true;
return shouldFocusActiveDescendant();
}
bool AccessibilityRenderObject::shouldFocusActiveDescendant() const
{
switch (ariaRoleAttribute()) {
case GroupRole:
case ListBoxRole:
case MenuRole:
case MenuBarRole:
case RadioGroupRole:
case RowRole:
case PopUpButtonRole:
case ProgressIndicatorRole:
case ToolbarRole:
case OutlineRole:
case TreeRole:
case GridRole:
return true;
default:
return false;
}
}
AccessibilityObject* AccessibilityRenderObject::activeDescendant() const
{
if (!m_renderer)
return nullptr;
const AtomicString& activeDescendantAttrStr = getAttribute(aria_activedescendantAttr);
if (activeDescendantAttrStr.isNull() || activeDescendantAttrStr.isEmpty())
return nullptr;
Element* element = this->element();
if (!element)
return nullptr;
Element* target = element->treeScope().getElementById(activeDescendantAttrStr);
if (!target)
return nullptr;
if (AXObjectCache* cache = axObjectCache()) {
AccessibilityObject* obj = cache->getOrCreate(target);
if (obj && obj->isAccessibilityRenderObject())
return obj;
}
return nullptr;
}
void AccessibilityRenderObject::handleAriaExpandedChanged()
{
AccessibilityObject* containerParent = this->parentObject();
while (containerParent) {
bool foundParent = false;
switch (containerParent->roleValue()) {
case TreeRole:
case TreeGridRole:
case GridRole:
case TableRole:
case BrowserRole:
foundParent = true;
break;
default:
break;
}
if (foundParent)
break;
containerParent = containerParent->parentObject();
}
AXObjectCache* cache = axObjectCache();
if (!cache)
return;
if (containerParent)
cache->postNotification(containerParent, document(), AXObjectCache::AXRowCountChanged);
if (roleValue() == RowRole || roleValue() == TreeItemRole)
cache->postNotification(this, document(), isExpanded() ? AXObjectCache::AXRowExpanded : AXObjectCache::AXRowCollapsed);
else
cache->postNotification(this, document(), AXObjectCache::AXExpandedChanged);
}
void AccessibilityRenderObject::handleActiveDescendantChanged()
{
Element* element = toElement(renderer()->node());
if (!element)
return;
if (!renderer()->frame().selection().isFocusedAndActive() || renderer()->document().focusedElement() != element)
return;
if (toAccessibilityRenderObject(activeDescendant()) && shouldNotifyActiveDescendant())
renderer()->document().axObjectCache()->postNotification(m_renderer, AXObjectCache::AXActiveDescendantChanged);
}
AccessibilityObject* AccessibilityRenderObject::correspondingControlForLabelElement() const
{
HTMLLabelElement* labelElement = labelElementContainer();
if (!labelElement)
return 0;
HTMLElement* correspondingControl = labelElement->control();
if (!correspondingControl)
return 0;
if (correspondingControl->renderer() && !correspondingControl->renderer()->parent())
return 0;
return axObjectCache()->getOrCreate(correspondingControl);
}
AccessibilityObject* AccessibilityRenderObject::correspondingLabelForControlElement() const
{
if (!m_renderer)
return 0;
if (hasTextAlternative())
return 0;
Node* node = m_renderer->node();
if (node && node->isHTMLElement()) {
HTMLLabelElement* label = labelForElement(toElement(node));
if (label)
return axObjectCache()->getOrCreate(label);
}
return 0;
}
bool AccessibilityRenderObject::renderObjectIsObservable(RenderObject* renderer) const
{
if (renderer->isTextControl())
return true;
Node* node = renderer->node();
if (!node)
return false;
if (nodeHasRole(node, "listbox") || (renderer->isBoxModelObject() && toRenderBoxModelObject(renderer)->isListBox()))
return true;
if (nodeHasRole(node, "textbox") || (node->isElementNode() && contentEditableAttributeIsEnabled(toElement(node))))
return true;
return false;
}
AccessibilityObject* AccessibilityRenderObject::observableObject() const
{
for (RenderObject* renderer = m_renderer; renderer && renderer->node(); renderer = renderer->parent()) {
if (renderObjectIsObservable(renderer)) {
if (AXObjectCache* cache = axObjectCache())
return cache->getOrCreate(renderer);
}
}
return 0;
}
bool AccessibilityRenderObject::isDescendantOfElementType(const QualifiedName& tagName) const
{
for (auto& ancestor : ancestorsOfType<RenderElement>(*m_renderer)) {
if (ancestor.element() && ancestor.element()->hasTagName(tagName))
return true;
}
return false;
}
String AccessibilityRenderObject::expandedTextValue() const
{
if (AccessibilityObject* parent = parentObject()) {
if (parent->hasTagName(abbrTag) || parent->hasTagName(acronymTag))
return parent->getAttribute(titleAttr);
}
return String();
}
bool AccessibilityRenderObject::supportsExpandedTextValue() const
{
if (roleValue() == StaticTextRole) {
if (AccessibilityObject* parent = parentObject())
return parent->hasTagName(abbrTag) || parent->hasTagName(acronymTag);
}
return false;
}
AccessibilityRole AccessibilityRenderObject::determineAccessibilityRole()
{
if (!m_renderer)
return UnknownRole;
m_ariaRole = determineAriaRoleAttribute();
Node* node = m_renderer->node();
AccessibilityRole ariaRole = ariaRoleAttribute();
if (ariaRole != UnknownRole)
return ariaRole;
RenderBoxModelObject* cssBox = renderBoxModelObject();
if (node && node->isLink()) {
if (cssBox && cssBox->isImage())
return ImageMapRole;
return WebCoreLinkRole;
}
if ((cssBox && cssBox->isListItem()) || (node && node->hasTagName(liTag)))
return ListItemRole;
if (m_renderer->isListMarker())
return ListMarkerRole;
if (node && node->hasTagName(buttonTag))
return buttonRoleType();
if (node && node->hasTagName(legendTag))
return LegendRole;
if (m_renderer->isText())
return StaticTextRole;
if (cssBox && cssBox->isImage()) {
if (node && isHTMLInputElement(node))
return ariaHasPopup() ? PopUpButtonRole : ButtonRole;
if (isSVGImage())
return SVGRootRole;
return ImageRole;
}
if (node && node->hasTagName(canvasTag))
return CanvasRole;
if (cssBox && cssBox->isRenderView())
return WebAreaRole;
if (cssBox && cssBox->isTextField())
return TextFieldRole;
if (cssBox && cssBox->isTextArea())
return TextAreaRole;
if (node && isHTMLInputElement(node)) {
HTMLInputElement* input = toHTMLInputElement(node);
if (input->isCheckbox())
return CheckBoxRole;
if (input->isRadioButton())
return RadioButtonRole;
if (input->isTextButton())
return buttonRoleType();
#if PLATFORM(IOS)
if (input->isDateField())
return PopUpButtonRole;
#endif
#if ENABLE(INPUT_TYPE_COLOR)
const AtomicString& type = input->getAttribute(typeAttr);
if (equalIgnoringCase(type, "color"))
return ColorWellRole;
#endif
}
if (hasContentEditableAttributeSet())
return TextAreaRole;
if (isFileUploadButton())
return ButtonRole;
if (cssBox && cssBox->isMenuList())
return PopUpButtonRole;
if (headingLevel())
return HeadingRole;
if (m_renderer->isSVGImage())
return ImageRole;
if (m_renderer->isSVGRoot())
return SVGRootRole;
if (node && node->hasTagName(SVGNames::gTag))
return GroupRole;
#if ENABLE(MATHML)
if (node && node->hasTagName(MathMLNames::mathTag))
return DocumentMathRole;
#endif
if (isMathElement())
return MathElementRole;
if (node && node->hasTagName(ddTag))
return DescriptionListDetailRole;
if (node && node->hasTagName(dtTag))
return DescriptionListTermRole;
if (node && node->hasTagName(dlTag))
return DescriptionListRole;
if (node && (node->hasTagName(rpTag) || node->hasTagName(rtTag)))
return AnnotationRole;
#if PLATFORM(GTK)
if (node && (node->hasTagName(tdTag) || node->hasTagName(thTag)))
return CellRole;
if (node && node->hasTagName(trTag))
return RowRole;
if (node && isHTMLTableElement(node))
return TableRole;
#endif
if (m_renderer->isTableSection())
return IgnoredRole;
if (m_renderer->isHR())
return HorizontalRuleRole;
if (node && node->hasTagName(pTag))
return ParagraphRole;
if (node && isHTMLLabelElement(node))
return LabelRole;
if (node && node->hasTagName(dfnTag))
return DefinitionRole;
if (node && node->hasTagName(divTag))
return DivRole;
if (node && isHTMLFormElement(node))
return FormRole;
if (node && node->hasTagName(articleTag))
return DocumentArticleRole;
if (node && node->hasTagName(mainTag))
return LandmarkMainRole;
if (node && node->hasTagName(navTag))
return LandmarkNavigationRole;
if (node && node->hasTagName(asideTag))
return LandmarkComplementaryRole;
if (node && node->hasTagName(sectionTag))
return DocumentRegionRole;
if (node && node->hasTagName(addressTag))
return LandmarkContentInfoRole;
#if ENABLE(VIDEO)
if (node && isHTMLVideoElement(node))
return VideoRole;
if (node && isHTMLAudioElement(node))
return AudioRole;
#endif
if (node && node->hasTagName(htmlTag))
return IgnoredRole;
if (node && node->hasTagName(headerTag) && !isDescendantOfElementType(articleTag) && !isDescendantOfElementType(sectionTag))
return LandmarkBannerRole;
if (node && node->hasTagName(footerTag) && !isDescendantOfElementType(articleTag) && !isDescendantOfElementType(sectionTag))
return FooterRole;
if (m_renderer->isRenderBlockFlow())
return GroupRole;
if (supportsARIAAttributes() || canSetFocusAttribute())
return GroupRole;
return UnknownRole;
}
AccessibilityOrientation AccessibilityRenderObject::orientation() const
{
const AtomicString& ariaOrientation = getAttribute(aria_orientationAttr);
if (equalIgnoringCase(ariaOrientation, "horizontal"))
return AccessibilityOrientationHorizontal;
if (equalIgnoringCase(ariaOrientation, "vertical"))
return AccessibilityOrientationVertical;
return AccessibilityObject::orientation();
}
bool AccessibilityRenderObject::inheritsPresentationalRole() const
{
if (canSetFocusAttribute())
return false;
static NeverDestroyed<HashSet<QualifiedName>> listItemParents;
static NeverDestroyed<HashSet<QualifiedName>> tableCellParents;
HashSet<QualifiedName>* possibleParentTagNames = 0;
switch (roleValue()) {
case ListItemRole:
case ListMarkerRole:
if (listItemParents.get().isEmpty()) {
listItemParents.get().add(ulTag);
listItemParents.get().add(olTag);
listItemParents.get().add(dlTag);
}
possibleParentTagNames = &listItemParents.get();
break;
case CellRole:
if (tableCellParents.get().isEmpty())
tableCellParents.get().add(tableTag);
possibleParentTagNames = &tableCellParents.get();
break;
default:
break;
}
if (!possibleParentTagNames)
return false;
for (AccessibilityObject* parent = parentObject(); parent; parent = parent->parentObject()) {
if (!parent->isAccessibilityRenderObject())
continue;
Node* elementNode = toAccessibilityRenderObject(parent)->node();
if (!elementNode || !elementNode->isElementNode())
continue;
if (possibleParentTagNames->contains(toElement(elementNode)->tagQName()))
return parent->roleValue() == PresentationalRole;
}
return false;
}
bool AccessibilityRenderObject::isPresentationalChildOfAriaRole() const
{
AccessibilityObject* parent;
for (parent = parentObject(); parent && !parent->ariaRoleHasPresentationalChildren(); parent = parent->parentObject())
{ }
return parent;
}
bool AccessibilityRenderObject::ariaRoleHasPresentationalChildren() const
{
switch (m_ariaRole) {
case ButtonRole:
case SliderRole:
case ImageRole:
case ProgressIndicatorRole:
case SpinButtonRole:
return true;
default:
return false;
}
}
bool AccessibilityRenderObject::canSetExpandedAttribute() const
{
const AtomicString& ariaExpanded = getAttribute(aria_expandedAttr);
return equalIgnoringCase(ariaExpanded, "true") || equalIgnoringCase(ariaExpanded, "false");
}
bool AccessibilityRenderObject::canSetValueAttribute() const
{
if (isNativeTextControl())
return !isReadOnly();
if (isMeter())
return false;
if (equalIgnoringCase(getAttribute(aria_readonlyAttr), "true"))
return false;
if (equalIgnoringCase(getAttribute(aria_readonlyAttr), "false"))
return true;
if (isProgressIndicator() || isSlider())
return true;
if (isTextControl() && !isNativeTextControl())
return true;
return !isReadOnly();
}
bool AccessibilityRenderObject::canSetTextRangeAttributes() const
{
return isTextControl();
}
void AccessibilityRenderObject::textChanged()
{
AXObjectCache* cache = axObjectCache();
if (!cache)
return;
for (RenderObject* renderParent = m_renderer; renderParent; renderParent = renderParent->parent()) {
AccessibilityObject* parent = cache->get(renderParent);
if (!parent)
continue;
if (parent->supportsARIALiveRegion())
cache->postNotification(renderParent, AXObjectCache::AXLiveRegionChanged);
if ((parent->isARIATextControl() || parent->hasContentEditableAttributeSet()) && !parent->isNativeTextControl())
cache->postNotification(renderParent, AXObjectCache::AXValueChanged);
}
}
void AccessibilityRenderObject::clearChildren()
{
AccessibilityObject::clearChildren();
m_childrenDirty = false;
}
void AccessibilityRenderObject::addImageMapChildren()
{
RenderBoxModelObject* cssBox = renderBoxModelObject();
if (!cssBox || !cssBox->isRenderImage())
return;
HTMLMapElement* map = toRenderImage(cssBox)->imageMap();
if (!map)
return;
for (auto& area : descendantsOfType<HTMLAreaElement>(*map)) {
if (!area.isLink())
continue;
AccessibilityImageMapLink* areaObject = toAccessibilityImageMapLink(axObjectCache()->getOrCreate(ImageMapLinkRole));
areaObject->setHTMLAreaElement(&area);
areaObject->setHTMLMapElement(map);
areaObject->setParent(this);
if (!areaObject->accessibilityIsIgnored())
m_children.append(areaObject);
else
axObjectCache()->remove(areaObject->axObjectID());
}
}
void AccessibilityRenderObject::updateChildrenIfNecessary()
{
if (needsToUpdateChildren())
clearChildren();
AccessibilityObject::updateChildrenIfNecessary();
}
void AccessibilityRenderObject::addTextFieldChildren()
{
Node* node = this->node();
if (!node || !isHTMLInputElement(node))
return;
HTMLInputElement* input = toHTMLInputElement(node);
HTMLElement* spinButtonElement = input->innerSpinButtonElement();
if (!spinButtonElement || !spinButtonElement->isSpinButtonElement())
return;
AccessibilitySpinButton* axSpinButton = toAccessibilitySpinButton(axObjectCache()->getOrCreate(SpinButtonRole));
axSpinButton->setSpinButtonElement(static_cast<SpinButtonElement*>(spinButtonElement));
axSpinButton->setParent(this);
m_children.append(axSpinButton);
}
bool AccessibilityRenderObject::isSVGImage() const
{
return remoteSVGRootElement();
}
void AccessibilityRenderObject::detachRemoteSVGRoot()
{
if (AccessibilitySVGRoot* root = remoteSVGRootElement())
root->setParent(0);
}
AccessibilitySVGRoot* AccessibilityRenderObject::remoteSVGRootElement() const
{
if (!m_renderer || !m_renderer->isRenderImage())
return nullptr;
CachedImage* cachedImage = toRenderImage(m_renderer)->cachedImage();
if (!cachedImage)
return nullptr;
Image* image = cachedImage->image();
if (!image || !image->isSVGImage())
return nullptr;
SVGImage* svgImage = static_cast<SVGImage*>(image);
FrameView* frameView = svgImage->frameView();
if (!frameView)
return nullptr;
Frame& frame = frameView->frame();
Document* doc = frame.document();
if (!doc || !doc->isSVGDocument())
return nullptr;
SVGSVGElement* rootElement = toSVGDocument(doc)->rootElement();
if (!rootElement)
return nullptr;
RenderObject* rendererRoot = rootElement->renderer();
if (!rendererRoot)
return nullptr;
AXObjectCache* cache = frame.document()->axObjectCache();
if (!cache)
return nullptr;
AccessibilityObject* rootSVGObject = cache->getOrCreate(rendererRoot);
ASSERT(rootSVGObject && rootSVGObject->isAccessibilitySVGRoot());
if (!rootSVGObject->isAccessibilitySVGRoot())
return nullptr;
return toAccessibilitySVGRoot(rootSVGObject);
}
void AccessibilityRenderObject::addRemoteSVGChildren()
{
AccessibilitySVGRoot* root = remoteSVGRootElement();
if (!root)
return;
root->setParent(this);
if (root->accessibilityIsIgnored()) {
for (const auto& child : root->children())
m_children.append(child);
} else
m_children.append(root);
}
void AccessibilityRenderObject::addCanvasChildren()
{
if (!node() || !node()->hasTagName(canvasTag) || (renderer() && !renderer()->isCanvas()))
return;
ASSERT(!m_children.size());
m_haveChildren = false;
AccessibilityNodeObject::addChildren();
}
void AccessibilityRenderObject::addAttachmentChildren()
{
if (!isAttachment())
return;
Widget* widget = widgetForAttachmentView();
if (!widget || !widget->isFrameView())
return;
AccessibilityObject* axWidget = axObjectCache()->getOrCreate(widget);
if (!axWidget->accessibilityIsIgnored())
m_children.append(axWidget);
}
#if PLATFORM(COCOA)
void AccessibilityRenderObject::updateAttachmentViewParents()
{
if (accessibilityIsIgnored())
return;
for (const auto& child : m_children) {
if (child->isAttachment())
child->overrideAttachmentParent(this);
}
}
#endif
void AccessibilityRenderObject::addHiddenChildren()
{
Node* node = this->node();
if (!node)
return;
bool shouldInsertHiddenNodes = false;
for (Node* child = node->firstChild(); child; child = child->nextSibling()) {
if (!child->renderer() && isNodeAriaVisible(child)) {
shouldInsertHiddenNodes = true;
break;
}
}
if (!shouldInsertHiddenNodes)
return;
unsigned insertionIndex = 0;
for (Node* child = node->firstChild(); child; child = child->nextSibling()) {
if (child->renderer()) {
AccessibilityObject* childObject = axObjectCache()->get(child->renderer());
if (childObject && childObject->accessibilityIsIgnored()) {
auto& children = childObject->children();
if (children.size())
childObject = children.last().get();
else
childObject = 0;
}
if (childObject)
insertionIndex = m_children.find(childObject) + 1;
continue;
}
if (!isNodeAriaVisible(child))
continue;
unsigned previousSize = m_children.size();
if (insertionIndex > previousSize)
insertionIndex = previousSize;
insertChild(axObjectCache()->getOrCreate(child), insertionIndex);
insertionIndex += (m_children.size() - previousSize);
}
}
void AccessibilityRenderObject::updateRoleAfterChildrenCreation()
{
if (roleValue() == MenuRole) {
size_t menuItemCount = 0;
for (const auto& child : children()) {
if (child->isMenuItem()) {
menuItemCount++;
break;
}
}
if (!menuItemCount)
m_role = GroupRole;
}
}
void AccessibilityRenderObject::addChildren()
{
ASSERT(!m_haveChildren);
m_haveChildren = true;
if (!canHaveChildren())
return;
for (RefPtr<AccessibilityObject> obj = firstChild(); obj; obj = obj->nextSibling())
addChild(obj.get());
addHiddenChildren();
addAttachmentChildren();
addImageMapChildren();
addTextFieldChildren();
addCanvasChildren();
addRemoteSVGChildren();
#if PLATFORM(COCOA)
updateAttachmentViewParents();
#endif
updateRoleAfterChildrenCreation();
}
bool AccessibilityRenderObject::canHaveChildren() const
{
if (!m_renderer)
return false;
return AccessibilityNodeObject::canHaveChildren();
}
const String AccessibilityRenderObject::ariaLiveRegionStatus() const
{
const AtomicString& liveRegionStatus = getAttribute(aria_liveAttr);
if (liveRegionStatus.isEmpty())
return defaultLiveRegionStatusForRole(roleValue());
return liveRegionStatus;
}
const AtomicString& AccessibilityRenderObject::ariaLiveRegionRelevant() const
{
static NeverDestroyed<const AtomicString> defaultLiveRegionRelevant("additions text", AtomicString::ConstructFromLiteral);
const AtomicString& relevant = getAttribute(aria_relevantAttr);
if (relevant.isEmpty())
return defaultLiveRegionRelevant;
return relevant;
}
bool AccessibilityRenderObject::ariaLiveRegionAtomic() const
{
const AtomicString& atomic = getAttribute(aria_atomicAttr);
if (equalIgnoringCase(atomic, "true"))
return true;
if (equalIgnoringCase(atomic, "false"))
return false;
switch (roleValue()) {
case ApplicationAlertRole:
case ApplicationStatusRole:
return true;
default:
return false;
}
}
bool AccessibilityRenderObject::ariaLiveRegionBusy() const
{
return elementAttributeValue(aria_busyAttr);
}
void AccessibilityRenderObject::ariaSelectedRows(AccessibilityChildrenVector& result)
{
bool isMulti = isMultiSelectable();
AccessibilityObject* activeDesc = activeDescendant();
if (activeDesc && (activeDesc->isTreeItem() || activeDesc->isTableRow())) {
result.append(activeDesc);
if (!isMulti)
return;
}
auto rowsIteration = [&](const AccessibilityChildrenVector& rows) {
for (auto& row : rows) {
if (row->isSelected()) {
result.append(row);
if (!isMulti)
break;
}
}
};
if (isTree()) {
AccessibilityChildrenVector allRows;
ariaTreeRows(allRows);
rowsIteration(allRows);
} else if (isAccessibilityTable() && toAccessibilityTable(this)->supportsSelectedRows())
rowsIteration(toAccessibilityTable(this)->rows());
}
void AccessibilityRenderObject::ariaListboxSelectedChildren(AccessibilityChildrenVector& result)
{
bool isMulti = isMultiSelectable();
for (const auto& child : children()) {
if (child->isSelected() && child->ariaRoleAttribute() == ListBoxOptionRole) {
result.append(child);
if (!isMulti)
return;
}
}
}
void AccessibilityRenderObject::selectedChildren(AccessibilityChildrenVector& result)
{
ASSERT(result.isEmpty());
AccessibilityRole role = roleValue();
if (role == ListBoxRole) ariaListboxSelectedChildren(result);
else if (role == TreeRole || role == TreeGridRole || role == TableRole)
ariaSelectedRows(result);
}
void AccessibilityRenderObject::ariaListboxVisibleChildren(AccessibilityChildrenVector& result)
{
if (!hasChildren())
addChildren();
for (const auto& child : children()) {
if (child->isOffScreen())
result.append(child);
}
}
void AccessibilityRenderObject::visibleChildren(AccessibilityChildrenVector& result)
{
ASSERT(result.isEmpty());
if (ariaRoleAttribute() != ListBoxRole) { ASSERT_NOT_REACHED();
return;
}
return ariaListboxVisibleChildren(result);
}
void AccessibilityRenderObject::tabChildren(AccessibilityChildrenVector& result)
{
ASSERT(roleValue() == TabListRole);
for (const auto& child : children()) {
if (child->isTabItem())
result.append(child);
}
}
const String& AccessibilityRenderObject::actionVerb() const
{
#if !PLATFORM(IOS)
static NeverDestroyed<const String> buttonAction(AXButtonActionVerb());
static NeverDestroyed<const String> textFieldAction(AXTextFieldActionVerb());
static NeverDestroyed<const String> radioButtonAction(AXRadioButtonActionVerb());
static NeverDestroyed<const String> checkedCheckBoxAction(AXUncheckedCheckBoxActionVerb());
static NeverDestroyed<const String> uncheckedCheckBoxAction(AXUncheckedCheckBoxActionVerb());
static NeverDestroyed<const String> linkAction(AXLinkActionVerb());
switch (roleValue()) {
case ButtonRole:
case ToggleButtonRole:
return buttonAction;
case TextFieldRole:
case TextAreaRole:
return textFieldAction;
case RadioButtonRole:
return radioButtonAction;
case CheckBoxRole:
return isChecked() ? checkedCheckBoxAction : uncheckedCheckBoxAction;
case LinkRole:
case WebCoreLinkRole:
return linkAction;
default:
return nullAtom;
}
#else
return nullAtom;
#endif
}
void AccessibilityRenderObject::setAccessibleName(const AtomicString& name)
{
if (!m_renderer)
return;
Node* domNode = 0;
if (isWebArea())
domNode = m_renderer->document().documentElement();
else
domNode = m_renderer->node();
if (domNode && domNode->isElementNode())
toElement(domNode)->setAttribute(aria_labelAttr, name);
}
static bool isLinkable(const AccessibilityRenderObject& object)
{
if (!object.renderer())
return false;
return object.isLink() || object.isImage() || object.renderer()->isText();
}
String AccessibilityRenderObject::stringValueForMSAA() const
{
if (isLinkable(*this)) {
Element* anchor = anchorElement();
if (anchor && isHTMLAnchorElement(anchor))
return toHTMLAnchorElement(anchor)->href();
}
return stringValue();
}
bool AccessibilityRenderObject::isLinked() const
{
if (!isLinkable(*this))
return false;
Element* anchor = anchorElement();
if (!anchor || !isHTMLAnchorElement(anchor))
return false;
return !toHTMLAnchorElement(anchor)->href().isEmpty();
}
bool AccessibilityRenderObject::hasBoldFont() const
{
if (!m_renderer)
return false;
return m_renderer->style().fontDescription().weight() >= FontWeightBold;
}
bool AccessibilityRenderObject::hasItalicFont() const
{
if (!m_renderer)
return false;
return m_renderer->style().fontDescription().italic() == FontItalicOn;
}
bool AccessibilityRenderObject::hasPlainText() const
{
if (!m_renderer)
return false;
const RenderStyle& style = m_renderer->style();
return style.fontDescription().weight() == FontWeightNormal
&& style.fontDescription().italic() == FontItalicOff
&& style.textDecorationsInEffect() == TextDecorationNone;
}
bool AccessibilityRenderObject::hasSameFont(RenderObject* renderer) const
{
if (!m_renderer || !renderer)
return false;
return m_renderer->style().fontDescription().families() == renderer->style().fontDescription().families();
}
bool AccessibilityRenderObject::hasSameFontColor(RenderObject* renderer) const
{
if (!m_renderer || !renderer)
return false;
return m_renderer->style().visitedDependentColor(CSSPropertyColor) == renderer->style().visitedDependentColor(CSSPropertyColor);
}
bool AccessibilityRenderObject::hasSameStyle(RenderObject* renderer) const
{
if (!m_renderer || !renderer)
return false;
return m_renderer->style() == renderer->style();
}
bool AccessibilityRenderObject::hasUnderline() const
{
if (!m_renderer)
return false;
return m_renderer->style().textDecorationsInEffect() & TextDecorationUnderline;
}
String AccessibilityRenderObject::nameForMSAA() const
{
if (m_renderer && m_renderer->isText())
return textUnderElement();
return title();
}
static bool shouldReturnTagNameAsRoleForMSAA(const Element& element)
{
return element.hasTagName(h1Tag) || element.hasTagName(h2Tag)
|| element.hasTagName(h3Tag) || element.hasTagName(h4Tag)
|| element.hasTagName(h5Tag) || element.hasTagName(h6Tag);
}
String AccessibilityRenderObject::stringRoleForMSAA() const
{
if (!m_renderer)
return String();
Node* node = m_renderer->node();
if (!node || !node->isElementNode())
return String();
Element* element = toElement(node);
if (!shouldReturnTagNameAsRoleForMSAA(*element))
return String();
return element->tagName();
}
String AccessibilityRenderObject::positionalDescriptionForMSAA() const
{
if (isHeading())
return "L" + String::number(headingLevel());
return String();
}
String AccessibilityRenderObject::descriptionForMSAA() const
{
String description = positionalDescriptionForMSAA();
if (!description.isEmpty())
return description;
description = accessibilityDescription();
if (!description.isEmpty()) {
return "Description: " + description;
}
return String();
}
static AccessibilityRole msaaRoleForRenderer(const RenderObject* renderer)
{
if (!renderer)
return UnknownRole;
if (renderer->isText())
return EditableTextRole;
if (renderer->isBoxModelObject() && toRenderBoxModelObject(renderer)->isListItem())
return ListItemRole;
return UnknownRole;
}
AccessibilityRole AccessibilityRenderObject::roleValueForMSAA() const
{
if (m_roleForMSAA != UnknownRole)
return m_roleForMSAA;
m_roleForMSAA = msaaRoleForRenderer(m_renderer);
if (m_roleForMSAA == UnknownRole)
m_roleForMSAA = roleValue();
return m_roleForMSAA;
}
String AccessibilityRenderObject::passwordFieldValue() const
{
ASSERT(isPasswordField());
RenderObject* renderer = node()->renderer();
while (renderer && !renderer->isText())
renderer = toRenderElement(renderer)->firstChild();
if (!renderer || !renderer->isText())
return String();
return toRenderText(renderer)->textWithoutConvertingBackslashToYenSymbol();
}
ScrollableArea* AccessibilityRenderObject::getScrollableAreaIfScrollable() const
{
if (parentObject() && parentObject()->isAccessibilityScrollView())
return 0;
if (!m_renderer || !m_renderer->isBox())
return 0;
RenderBox* box = toRenderBox(m_renderer);
if (!box->canBeScrolledAndHasScrollableArea())
return 0;
return box->layer();
}
void AccessibilityRenderObject::scrollTo(const IntPoint& point) const
{
if (!m_renderer || !m_renderer->isBox())
return;
RenderBox* box = toRenderBox(m_renderer);
if (!box->canBeScrolledAndHasScrollableArea())
return;
RenderLayer* layer = box->layer();
layer->scrollToOffset(toIntSize(point), RenderLayer::ScrollOffsetClamped);
}
#if ENABLE(MATHML)
bool AccessibilityRenderObject::isMathElement() const
{
Node* node = this->node();
if (!m_renderer || !node)
return false;
return node->isMathMLElement();
}
bool AccessibilityRenderObject::isMathFraction() const
{
return m_renderer && m_renderer->isRenderMathMLFraction();
}
bool AccessibilityRenderObject::isMathFenced() const
{
return m_renderer && m_renderer->isRenderMathMLFenced();
}
bool AccessibilityRenderObject::isMathSubscriptSuperscript() const
{
return m_renderer && m_renderer->isRenderMathMLScripts() && !isMathMultiscript();
}
bool AccessibilityRenderObject::isMathRow() const
{
return m_renderer && m_renderer->isRenderMathMLRow();
}
bool AccessibilityRenderObject::isMathUnderOver() const
{
return m_renderer && m_renderer->isRenderMathMLUnderOver();
}
bool AccessibilityRenderObject::isMathSquareRoot() const
{
return m_renderer && m_renderer->isRenderMathMLSquareRoot();
}
bool AccessibilityRenderObject::isMathRoot() const
{
return m_renderer && m_renderer->isRenderMathMLRoot();
}
bool AccessibilityRenderObject::isMathOperator() const
{
if (!m_renderer || !m_renderer->isRenderMathMLOperator())
return false;
return true;
}
bool AccessibilityRenderObject::isMathFenceOperator() const
{
if (!m_renderer || !m_renderer->isRenderMathMLOperator())
return false;
return toRenderMathMLOperator(*m_renderer).hasOperatorFlag(MathMLOperatorDictionary::Fence);
}
bool AccessibilityRenderObject::isMathSeparatorOperator() const
{
if (!m_renderer || !m_renderer->isRenderMathMLOperator())
return false;
return toRenderMathMLOperator(*m_renderer).hasOperatorFlag(MathMLOperatorDictionary::Separator);
}
bool AccessibilityRenderObject::isMathText() const
{
return node() && node()->hasTagName(MathMLNames::mtextTag);
}
bool AccessibilityRenderObject::isMathNumber() const
{
return node() && node()->hasTagName(MathMLNames::mnTag);
}
bool AccessibilityRenderObject::isMathIdentifier() const
{
return node() && node()->hasTagName(MathMLNames::miTag);
}
bool AccessibilityRenderObject::isMathMultiscript() const
{
return node() && node()->hasTagName(MathMLNames::mmultiscriptsTag);
}
bool AccessibilityRenderObject::isMathTable() const
{
return node() && node()->hasTagName(MathMLNames::mtableTag);
}
bool AccessibilityRenderObject::isMathTableRow() const
{
return node() && node()->hasTagName(MathMLNames::mtrTag);
}
bool AccessibilityRenderObject::isMathTableCell() const
{
return node() && node()->hasTagName(MathMLNames::mtdTag);
}
bool AccessibilityRenderObject::isIgnoredElementWithinMathTree() const
{
if (!m_renderer)
return true;
if (m_renderer->isAnonymous()) {
if (m_renderer->isRenderMathMLOperator())
return false;
for (AccessibilityObject* parent = parentObject(); parent; parent = parent->parentObject()) {
if (parent->isMathElement())
return !(m_renderer->isText() && ancestorsOfType<RenderMathMLOperator>(*m_renderer).first());
}
}
if (isMathElement()) {
if (isMathFraction() || isMathFenced() || isMathSubscriptSuperscript() || isMathRow()
|| isMathUnderOver() || isMathRoot() || isMathText() || isMathNumber()
|| isMathOperator() || isMathFenceOperator() || isMathSeparatorOperator()
|| isMathIdentifier() || isMathTable() || isMathTableRow() || isMathTableCell() || isMathMultiscript())
return false;
return true;
}
return false;
}
AccessibilityObject* AccessibilityRenderObject::mathRadicandObject()
{
if (!isMathRoot())
return 0;
RenderMathMLRoot* root = &toRenderMathMLRoot(*m_renderer);
AccessibilityObject* rootRadicandWrapper = axObjectCache()->getOrCreate(root->baseWrapper());
if (!rootRadicandWrapper)
return nullptr;
AccessibilityObject* rootRadicand = rootRadicandWrapper->children().first().get();
ASSERT(rootRadicand && children().contains(rootRadicand));
return rootRadicand;
}
AccessibilityObject* AccessibilityRenderObject::mathRootIndexObject()
{
if (!isMathRoot())
return 0;
RenderMathMLRoot* root = &toRenderMathMLRoot(*m_renderer);
AccessibilityObject* rootIndexWrapper = axObjectCache()->getOrCreate(root->indexWrapper());
if (!rootIndexWrapper)
return 0;
AccessibilityObject* rootIndex = rootIndexWrapper->children().first().get();
ASSERT(rootIndex && children().contains(rootIndex));
return rootIndex;
}
AccessibilityObject* AccessibilityRenderObject::mathNumeratorObject()
{
if (!isMathFraction())
return 0;
const auto& children = this->children();
if (children.size() != 2)
return 0;
return children[0].get();
}
AccessibilityObject* AccessibilityRenderObject::mathDenominatorObject()
{
if (!isMathFraction())
return 0;
const auto& children = this->children();
if (children.size() != 2)
return 0;
return children[1].get();
}
AccessibilityObject* AccessibilityRenderObject::mathUnderObject()
{
if (!isMathUnderOver() || !node())
return 0;
const auto& children = this->children();
if (children.size() < 2)
return 0;
if (node()->hasTagName(MathMLNames::munderTag) || node()->hasTagName(MathMLNames::munderoverTag))
return children[1].get();
return 0;
}
AccessibilityObject* AccessibilityRenderObject::mathOverObject()
{
if (!isMathUnderOver() || !node())
return 0;
const auto& children = this->children();
if (children.size() < 2)
return 0;
if (node()->hasTagName(MathMLNames::moverTag))
return children[1].get();
if (node()->hasTagName(MathMLNames::munderoverTag))
return children[2].get();
return 0;
}
AccessibilityObject* AccessibilityRenderObject::mathBaseObject()
{
if (!isMathSubscriptSuperscript() && !isMathUnderOver() && !isMathMultiscript())
return 0;
const auto& children = this->children();
if (children.size() > 0)
return children[0].get();
return 0;
}
AccessibilityObject* AccessibilityRenderObject::mathSubscriptObject()
{
if (!isMathSubscriptSuperscript() || !node())
return 0;
const auto& children = this->children();
if (children.size() < 2)
return 0;
if (node()->hasTagName(MathMLNames::msubTag) || node()->hasTagName(MathMLNames::msubsupTag))
return children[1].get();
return 0;
}
AccessibilityObject* AccessibilityRenderObject::mathSuperscriptObject()
{
if (!isMathSubscriptSuperscript() || !node())
return 0;
const auto& children = this->children();
unsigned count = children.size();
if (count >= 2 && node()->hasTagName(MathMLNames::msupTag))
return children[1].get();
if (count >= 3 && node()->hasTagName(MathMLNames::msubsupTag))
return children[2].get();
return 0;
}
String AccessibilityRenderObject::mathFencedOpenString() const
{
if (!isMathFenced())
return String();
return getAttribute(MathMLNames::openAttr);
}
String AccessibilityRenderObject::mathFencedCloseString() const
{
if (!isMathFenced())
return String();
return getAttribute(MathMLNames::closeAttr);
}
void AccessibilityRenderObject::mathPrescripts(AccessibilityMathMultiscriptPairs& prescripts)
{
if (!isMathMultiscript() || !node())
return;
bool foundPrescript = false;
std::pair<AccessibilityObject*, AccessibilityObject*> prescriptPair;
for (Node* child = node()->firstChild(); child; child = child->nextSibling()) {
if (foundPrescript) {
AccessibilityObject* axChild = axObjectCache()->getOrCreate(child);
if (axChild && axChild->isMathElement()) {
if (!prescriptPair.first)
prescriptPair.first = axChild;
else {
prescriptPair.second = axChild;
prescripts.append(prescriptPair);
prescriptPair.first = 0;
prescriptPair.second = 0;
}
}
} else if (child->hasTagName(MathMLNames::mprescriptsTag))
foundPrescript = true;
}
if (prescriptPair.first)
prescripts.append(prescriptPair);
}
void AccessibilityRenderObject::mathPostscripts(AccessibilityMathMultiscriptPairs& postscripts)
{
if (!isMathMultiscript() || !node())
return;
std::pair<AccessibilityObject*, AccessibilityObject*> postscriptPair;
bool foundBaseElement = false;
for (Node* child = node()->firstChild(); child; child = child->nextSibling()) {
if (child->hasTagName(MathMLNames::mprescriptsTag))
break;
AccessibilityObject* axChild = axObjectCache()->getOrCreate(child);
if (axChild && axChild->isMathElement()) {
if (!foundBaseElement)
foundBaseElement = true;
else if (!postscriptPair.first)
postscriptPair.first = axChild;
else {
postscriptPair.second = axChild;
postscripts.append(postscriptPair);
postscriptPair.first = 0;
postscriptPair.second = 0;
}
}
}
if (postscriptPair.first)
postscripts.append(postscriptPair);
}
int AccessibilityRenderObject::mathLineThickness() const
{
if (!isMathFraction())
return -1;
return toRenderMathMLFraction(m_renderer)->lineThickness();
}
#endif
}