Chromium Code Reviews| Index: third_party/WebKit/Source/core/dom/AccessibleNode.cpp |
| diff --git a/third_party/WebKit/Source/core/dom/AccessibleNode.cpp b/third_party/WebKit/Source/core/dom/AccessibleNode.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5dd3dece70a1dfb9504151a327f12d957b92e79a |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/dom/AccessibleNode.cpp |
| @@ -0,0 +1,94 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "core/dom/AccessibleNode.h" |
| + |
| +#include "core/dom/AXObjectCache.h" |
| +#include "core/dom/Element.h" |
| +#include "core/dom/QualifiedName.h" |
| +#include "core/frame/Settings.h" |
| + |
| +namespace blink { |
| + |
| +AccessibleNode::AccessibleNode(Element* element) : m_element(element) { |
| + DCHECK(RuntimeEnabledFeatures::accessibilityObjectModelEnabled()); |
| +} |
| + |
| +AccessibleNode::~AccessibleNode() {} |
| + |
| +// static |
| +const AtomicString& AccessibleNode::getProperty(Element* element, |
| + AOMStringProperty property) { |
| + DCHECK_GE(property, AOMStringProperty::kFirst); |
| + DCHECK_LT(property, AOMStringProperty::kCount); |
|
esprehn
2017/03/28 21:39:58
Remove this and just static_assert somewhere?
dmazzoni
2017/03/29 07:00:42
Good point, actually those were really only needed
|
| + |
| + if (!element) |
| + return nullAtom; |
| + |
| + if (AccessibleNode* accessibleNode = element->existingAccessibleNode()) { |
| + for (const auto& item : accessibleNode->m_stringProperties) { |
| + if (item.first == property) |
| + return item.second; |
| + } |
| + } |
| + |
| + // Fall back on the equivalent ARIA attribute. |
| + switch (property) { |
| + case AOMStringProperty::kRole: |
| + return element->getAttribute(HTMLNames::roleAttr); |
| + case AOMStringProperty::kLabel: |
| + return element->getAttribute(HTMLNames::aria_labelAttr); |
| + default: |
| + NOTREACHED(); |
| + return nullAtom; |
| + } |
| +} |
| + |
| +AtomicString AccessibleNode::role() const { |
| + return getProperty(m_element, AOMStringProperty::kRole); |
| +} |
| + |
| +void AccessibleNode::setRole(const AtomicString& role) { |
| + if (!m_element) |
| + return; |
| + |
| + setStringProperty(AOMStringProperty::kRole, role); |
| + |
| + // TODO(dmazzoni): Make a cleaner API for this rather than pretending |
| + // the DOM attribute changed. |
| + if (AXObjectCache* cache = m_element->document().axObjectCache()) |
| + cache->handleAttributeChanged(HTMLNames::roleAttr, m_element); |
| +} |
| + |
| +AtomicString AccessibleNode::label() const { |
| + return getProperty(m_element, AOMStringProperty::kLabel); |
| +} |
| + |
| +void AccessibleNode::setLabel(const AtomicString& label) { |
| + if (!m_element) |
| + return; |
| + |
| + setStringProperty(AOMStringProperty::kLabel, label); |
| + |
| + if (AXObjectCache* cache = m_element->document().axObjectCache()) |
| + cache->handleAttributeChanged(HTMLNames::aria_labelAttr, m_element); |
| +} |
| + |
| +void AccessibleNode::setStringProperty(AOMStringProperty property, |
| + const AtomicString& value) { |
| + for (auto& item : m_stringProperties) { |
| + if (item.first == property) { |
| + item.second = value; |
| + return; |
| + } |
| + } |
| + |
| + m_stringProperties.push_back(std::make_pair(property, value)); |
| +} |
| + |
| +DEFINE_TRACE(AccessibleNode) { |
| + visitor->trace(m_element); |
| +} |
| + |
| +} // namespace blink |