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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/*
* Copyright (C) 2016 Igalia, S.L.
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 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 THE COPYRIGHT
* OWNER OR 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 "AccessibilitySVGObject.h"
#include "AXObjectCache.h"
#include "ElementChildIteratorInlines.h"
#include "HTMLNames.h"
#include "RenderIterator.h"
#include "RenderText.h"
#include "SVGAElement.h"
#include "SVGDescElement.h"
#include "SVGElementTypeHelpers.h"
#include "SVGGElement.h"
#include "SVGTitleElement.h"
#include "SVGUseElement.h"
#include "XLinkNames.h"
#include <wtf/Language.h>
namespace WebCore {
AccessibilitySVGObject::AccessibilitySVGObject(AXID axID, RenderObject& renderer, AXObjectCache* cache)
: AccessibilityRenderObject(axID, renderer)
, m_axObjectCache(cache)
{
ASSERT(cache);
}
AccessibilitySVGObject::~AccessibilitySVGObject() = default;
Ref<AccessibilitySVGObject> AccessibilitySVGObject::create(AXID axID, RenderObject& renderer, AXObjectCache* cache)
{
return adoptRef(*new AccessibilitySVGObject(axID, renderer, cache));
}
AccessibilityObject* AccessibilitySVGObject::targetForUseElement() const
{
auto* use = dynamicDowncast<SVGUseElement>(element());
if (!use)
return nullptr;
auto href = use->href();
if (href.isEmpty())
href = getAttribute(HTMLNames::hrefAttr);
auto target = SVGURIReference::targetElementFromIRIString(href, use->treeScopeForSVGReferences());
CheckedPtr cache = axObjectCache();
return cache ? cache->getOrCreate(target.element.get()) : nullptr;
}
template <typename ChildrenType>
Element* AccessibilitySVGObject::childElementWithMatchingLanguage(ChildrenType& children) const
{
String languageCode = language();
if (languageCode.isEmpty())
languageCode = defaultLanguage();
// The best match for a group of child SVG2 'title' or 'desc' elements may be the one
// which lacks a 'lang' attribute value. However, indexOfBestMatchingLanguageInList()
// currently bases its decision on non-empty strings. Furthermore, we cannot count on
// that child element having a given position. So we'll look for such an element while
// building the language list and save it as our fallback.
Element* fallback = nullptr;
Vector<String> childLanguageCodes;
Vector<Element*> elements;
for (auto& child : children) {
auto& lang = child.attributeWithoutSynchronization(SVGNames::langAttr);
childLanguageCodes.append(lang);
elements.append(&child);
// The current draft of the SVG2 spec states if there are multiple equally-valid
// matches, the first match should be used.
if (lang.isEmpty() && !fallback)
fallback = &child;
}
bool exactMatch;
size_t index = indexOfBestMatchingLanguageInList(languageCode, childLanguageCodes, exactMatch);
if (index < childLanguageCodes.size())
return elements[index];
return fallback;
}
void AccessibilitySVGObject::accessibilityText(Vector<AccessibilityText>& textOrder) const
{
String description = this->description();
if (!description.isEmpty())
textOrder.append(AccessibilityText(description, AccessibilityTextSource::Alternative));
String helptext = helpText();
if (!helptext.isEmpty())
textOrder.append(AccessibilityText(helptext, AccessibilityTextSource::Help));
}
String AccessibilitySVGObject::description() const
{
// According to the SVG Accessibility API Mappings spec, the order of priority is:
// 1. aria-labelledby
// 2. aria-label
// 3. a direct child title element (selected according to language)
// 4. xlink:title attribute
// 5. for a use element, the accessible name calculated for the re-used content
// 6. for text container elements, the text content
String ariaDescription = ariaAccessibilityDescription();
if (!ariaDescription.isEmpty())
return ariaDescription;
RefPtr element = this->element();
if (element) {
auto titleElements = childrenOfType<SVGTitleElement>(*element);
if (auto* titleChild = childElementWithMatchingLanguage(titleElements))
return titleChild->textContent();
}
if (is<SVGAElement>(element.get())) {
const auto& xlinkTitle = element->attributeWithoutSynchronization(XLinkNames::titleAttr);
if (!xlinkTitle.isEmpty())
return xlinkTitle;
}
if (RefPtr target = targetForUseElement())
return target->description();
// FIXME: This is here to not break the svg-image.html test. But 'alt' is not
// listed as a supported attribute of the 'image' element in the SVG spec:
// https://www.w3.org/TR/SVG/struct.html#ImageElement
if (m_renderer && m_renderer->isRenderOrLegacyRenderSVGImage()) {
const auto& alt = getAttribute(HTMLNames::altAttr);
if (!alt.isNull())
return alt;
}
return { };
}
String AccessibilitySVGObject::helpText() const
{
RefPtr element = this->element();
if (!element)
return { };
// According to the SVG Accessibility API Mappings spec, the order of priority is:
// 1. aria-describedby
// 2. a direct child desc element
// 3. for a use element, the accessible description calculated for the re-used content
// 4. for text container elements, the text content, if not used for the name
// 5. a direct child title element that provides a tooltip, if not used for the name
String describedBy = ariaDescribedByAttribute();
if (!describedBy.isEmpty())
return describedBy;
auto descriptionElements = childrenOfType<SVGDescElement>(*element);
if (RefPtr descriptionChild = childElementWithMatchingLanguage(descriptionElements))
return descriptionChild->textContent();
if (RefPtr target = targetForUseElement())
return target->helpText();
auto titleElements = childrenOfType<SVGTitleElement>(*element);
if (RefPtr titleChild = childElementWithMatchingLanguage(titleElements)) {
if (titleChild->textContent() != description())
return titleChild->textContent();
}
return { };
}
bool AccessibilitySVGObject::hasTitleOrDescriptionChild() const
{
RefPtr element = this->element();
if (!element)
return false;
for (const auto& child : childrenOfType<SVGElement>(*element)) {
if (is<SVGTitleElement>(child) || is<SVGDescElement>(child))
return true;
}
return false;
}
bool AccessibilitySVGObject::computeIsIgnored() const
{
// According to the SVG Accessibility API Mappings spec, items should be excluded if:
// * They would be excluded according to the Core Accessibility API Mappings.
// * They are neither perceivable nor interactive.
// * Their first mappable role is presentational, unless they have a global ARIA
// attribute (covered by Core AAM) or at least one 'title' or 'desc' child element.
// * They have an ancestor with Children Presentational: True (covered by Core AAM)
AccessibilityObjectInclusion decision = defaultObjectInclusion();
if (decision == AccessibilityObjectInclusion::IgnoreObject)
return true;
if (!m_renderer || m_renderer->isLegacyRenderSVGHiddenContainer() || m_renderer->isRenderSVGHiddenContainer())
return true;
// The SVG AAM states objects with at least one 'title' or 'desc' element MUST be included.
// At this time, the presence of a matching 'lang' attribute is not mentioned in the spec.
if (hasTitleOrDescriptionChild())
return false;
if (ignoredFromPresentationalRole())
return true;
if (ariaRoleAttribute() != AccessibilityRole::Unknown)
return false;
// The SVG AAM states text elements should also be included, if they have content.
if (m_renderer->isRenderSVGText() || m_renderer->isRenderSVGTextPath()) {
for (auto& child : childrenOfType<RenderText>(downcast<RenderElement>(*m_renderer))) {
if (!child.containsOnlyCollapsibleWhitespace())
return false;
}
}
// SVG shapes should not be included unless there's a concrete reason for inclusion.
// https://rawgit.com/w3c/aria/master/svg-aam/svg-aam.html#exclude_elements
if (m_renderer->isRenderOrLegacyRenderSVGShape()) {
if (canSetFocusAttribute() || element()->hasEventListeners())
return false;
if (auto* svgParent = Accessibility::findAncestor<AccessibilityObject>(*this, true, [] (const AccessibilityObject& object) {
return object.hasAttributesRequiredForInclusion() || object.isAccessibilitySVGRoot();
}))
return !svgParent->hasAttributesRequiredForInclusion();
return true;
}
return AccessibilityRenderObject::computeIsIgnored();
}
bool AccessibilitySVGObject::inheritsPresentationalRole() const
{
if (canSetFocusAttribute())
return false;
AccessibilityRole role = roleValue();
if (role != AccessibilityRole::SVGTextPath && role != AccessibilityRole::SVGTSpan)
return false;
for (AccessibilityObject* parent = parentObject(); parent; parent = parent->parentObject()) {
if (is<AccessibilityRenderObject>(*parent) && parent->hasTagName(SVGNames::textTag))
return parent->roleValue() == AccessibilityRole::Presentational;
}
return false;
}
AccessibilityRole AccessibilitySVGObject::determineAriaRoleAttribute() const
{
auto role = AccessibilityRenderObject::determineAriaRoleAttribute();
if (role != AccessibilityRole::Presentational)
return role;
// The presence of a 'title' or 'desc' child element trumps PresentationalRole.
// https://lists.w3.org/Archives/Public/public-svg-a11y/2016Apr/0016.html
// At this time, the presence of a matching 'lang' attribute is not mentioned.
return hasTitleOrDescriptionChild() ? AccessibilityRole::Unknown : role;
}
AccessibilityRole AccessibilitySVGObject::determineAccessibilityRole()
{
if ((m_ariaRole = determineAriaRoleAttribute()) != AccessibilityRole::Unknown)
return m_ariaRole;
if (!m_renderer)
return AccessibilityRole::Unknown;
RefPtr element = this->element();
if (m_renderer->isRenderOrLegacyRenderSVGShape() || m_renderer->isRenderOrLegacyRenderSVGPath() || m_renderer->isRenderOrLegacyRenderSVGImage() || is<SVGUseElement>(element))
return AccessibilityRole::Image;
if (m_renderer->isRenderOrLegacyRenderSVGForeignObject())
return AccessibilityRole::Generic;
if (is<SVGGElement>(element)) {
// https://w3c.github.io/svg-aam/#include_elements
// g elements are generic (like a div) unless they have a name or is focusable.
if (WebCore::hasAccNameAttribute(*element) || hasTitleOrDescriptionChild() || canSetFocusAttribute())
return AccessibilityRole::Group;
return AccessibilityRole::Generic;
}
if (m_renderer->isRenderSVGInlineText())
return AccessibilityRole::StaticText;
if (m_renderer->isRenderSVGText())
return AccessibilityRole::SVGText;
if (m_renderer->isRenderSVGTextPath())
return AccessibilityRole::SVGTextPath;
if (m_renderer->isRenderSVGTSpan())
return AccessibilityRole::SVGTSpan;
if (is<SVGAElement>(element))
return AccessibilityRole::WebCoreLink;
return AccessibilityRenderObject::determineAccessibilityRole();
}
} // namespace WebCore
|