1 /*
2  * Copyright (C) 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "third_party/blink/renderer/modules/accessibility/ax_image_map_link.h"
30 
31 #include "third_party/blink/renderer/core/aom/accessible_node.h"
32 #include "third_party/blink/renderer/core/dom/element_traversal.h"
33 #include "third_party/blink/renderer/modules/accessibility/ax_layout_object.h"
34 #include "third_party/blink/renderer/modules/accessibility/ax_object_cache_impl.h"
35 #include "third_party/blink/renderer/platform/graphics/path.h"
36 #include "third_party/skia/include/core/SkMatrix44.h"
37 
38 namespace blink {
39 
AXImageMapLink(HTMLAreaElement * area,AXObjectCacheImpl & ax_object_cache)40 AXImageMapLink::AXImageMapLink(HTMLAreaElement* area,
41                                AXObjectCacheImpl& ax_object_cache)
42     : AXNodeObject(area, ax_object_cache) {}
43 
44 AXImageMapLink::~AXImageMapLink() = default;
45 
MapElement() const46 HTMLMapElement* AXImageMapLink::MapElement() const {
47   HTMLAreaElement* area = AreaElement();
48   if (!area)
49     return nullptr;
50   return Traversal<HTMLMapElement>::FirstAncestor(*area);
51 }
52 
ComputeParent() const53 AXObject* AXImageMapLink::ComputeParent() const {
54   DCHECK(!IsDetached());
55   if (parent_)
56     return parent_;
57 
58   if (!MapElement())
59     return nullptr;
60 
61   return AXObjectCache().GetOrCreate(MapElement()->GetLayoutObject());
62 }
63 
RoleValue() const64 ax::mojom::Role AXImageMapLink::RoleValue() const {
65   const AtomicString& aria_role =
66       GetAOMPropertyOrARIAAttribute(AOMStringProperty::kRole);
67   if (!aria_role.IsEmpty())
68     return AXObject::AriaRoleToWebCoreRole(aria_role);
69 
70   // https://www.w3.org/TR/html-aam-1.0/#html-element-role-mappings
71   // <area> tags without an href should be treated as static text.
72   KURL url = Url();
73   if (url.IsNull() || url.IsEmpty())
74     return ax::mojom::Role::kStaticText;
75 
76   return ax::mojom::Role::kLink;
77 }
78 
ComputeAccessibilityIsIgnored(IgnoredReasons * ignored_reasons) const79 bool AXImageMapLink::ComputeAccessibilityIsIgnored(
80     IgnoredReasons* ignored_reasons) const {
81   return AccessibilityIsIgnoredByDefault(ignored_reasons);
82 }
83 
ActionElement() const84 Element* AXImageMapLink::ActionElement() const {
85   return AnchorElement();
86 }
87 
AnchorElement() const88 Element* AXImageMapLink::AnchorElement() const {
89   return To<Element>(GetNode());
90 }
91 
Url() const92 KURL AXImageMapLink::Url() const {
93   if (!AreaElement())
94     return KURL();
95 
96   return AreaElement()->Href();
97 }
98 
GetRelativeBounds(AXObject ** out_container,FloatRect & out_bounds_in_container,SkMatrix44 & out_container_transform,bool * clips_children) const99 void AXImageMapLink::GetRelativeBounds(AXObject** out_container,
100                                        FloatRect& out_bounds_in_container,
101                                        SkMatrix44& out_container_transform,
102                                        bool* clips_children) const {
103   *out_container = nullptr;
104   out_bounds_in_container = FloatRect();
105   out_container_transform.setIdentity();
106 
107   HTMLAreaElement* area = AreaElement();
108   HTMLMapElement* map = MapElement();
109   if (!area || !map)
110     return;
111 
112   LayoutObject* layout_object;
113   if (auto* ax_object = DynamicTo<AXLayoutObject>(parent_.Get()))
114     layout_object = ax_object->GetLayoutObject();
115   else
116     layout_object = map->GetLayoutObject();
117 
118   if (!layout_object)
119     return;
120 
121   out_bounds_in_container = area->GetPath(layout_object).BoundingRect();
122   *out_container = AXObjectCache().GetOrCreate(layout_object);
123 }
124 
IsImageMapLink() const125 bool AXImageMapLink::IsImageMapLink() const {
126   return true;
127 }
128 
Trace(Visitor * visitor) const129 void AXImageMapLink::Trace(Visitor* visitor) const {
130   AXNodeObject::Trace(visitor);
131 }
132 
133 }  // namespace blink
134