summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/html_area_element.cc
blob: b7630fa4075fbf2526506d8a7be896f623c5122e (plain)
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
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 * Copyright (C) 2004, 2005, 2006, 2009, 2011 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "third_party/blink/renderer/core/html/html_area_element.h"

#include "third_party/blink/renderer/core/dom/element_traversal.h"
#include "third_party/blink/renderer/core/html/html_image_element.h"
#include "third_party/blink/renderer/core/html/html_map_element.h"
#include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/layout/hit_test_result.h"
#include "third_party/blink/renderer/core/layout/layout_image.h"
#include "third_party/blink/renderer/platform/graphics/path.h"
#include "third_party/blink/renderer/platform/transforms/affine_transform.h"

namespace blink {

namespace {

// Adapt a double to the allowed range of a LayoutUnit and narrow it to float
// precision.
float ClampCoordinate(double value) {
  return LayoutUnit(value).ToFloat();
}
}

HTMLAreaElement::HTMLAreaElement(Document& document)
    : HTMLAnchorElement(html_names::kAreaTag, document), shape_(kRect) {}

// An explicit empty destructor should be in html_area_element.cc, because
// if an implicit destructor is used or an empty destructor is defined in
// html_area_element.h, when including html_area_element.h, msvc tries to expand
// the destructor and causes a compile error because of lack of blink::Path
// definition.
HTMLAreaElement::~HTMLAreaElement() = default;

void HTMLAreaElement::ParseAttribute(
    const AttributeModificationParams& params) {
  const AtomicString& value = params.new_value;
  if (params.name == html_names::kShapeAttr) {
    if (EqualIgnoringASCIICase(value, "default")) {
      shape_ = kDefault;
    } else if (EqualIgnoringASCIICase(value, "circle") ||
               EqualIgnoringASCIICase(value, "circ")) {
      shape_ = kCircle;
    } else if (EqualIgnoringASCIICase(value, "polygon") ||
               EqualIgnoringASCIICase(value, "poly")) {
      shape_ = kPoly;
    } else {
      // The missing (and implicitly invalid) value default for the
      // 'shape' attribute is 'rect'.
      shape_ = kRect;
    }
    InvalidateCachedPath();
  } else if (params.name == html_names::kCoordsAttr) {
    coords_ = ParseHTMLListOfFloatingPointNumbers(value.GetString());
    InvalidateCachedPath();
  } else if (params.name == html_names::kAltAttr ||
             params.name == html_names::kAccesskeyAttr) {
    // Do nothing.
  } else {
    HTMLAnchorElement::ParseAttribute(params);
  }
}

void HTMLAreaElement::InvalidateCachedPath() {
  path_ = nullptr;
}

bool HTMLAreaElement::PointInArea(const PhysicalOffset& location,
                                  const LayoutObject* container_object) const {
  return GetPath(container_object).Contains(FloatPoint(location));
}

PhysicalRect HTMLAreaElement::ComputeAbsoluteRect(
    const LayoutObject* container_object) const {
  if (!container_object)
    return PhysicalRect();

  // FIXME: This doesn't work correctly with transforms.
  PhysicalOffset abs_pos = container_object->LocalToAbsolutePoint(
      PhysicalOffset(), kIgnoreTransforms);

  Path path = GetPath(container_object);
  path.Translate(FloatSize(abs_pos));
  return PhysicalRect::EnclosingRect(path.BoundingRect());
}

Path HTMLAreaElement::GetPath(const LayoutObject* container_object) const {
  if (!container_object)
    return Path();

  // Always recompute for default shape because it depends on container object's
  // size and is cheap.
  if (shape_ == kDefault) {
    Path path;
    // No need to zoom because it is already applied in
    // containerObject->borderBoxRect().
    if (container_object->IsBox())
      path.AddRect(FloatRect(ToLayoutBox(container_object)->BorderBoxRect()));
    path_ = nullptr;
    return path;
  }

  Path path;
  if (path_) {
    path = *path_;
  } else {
    if (coords_.IsEmpty())
      return path;

    switch (shape_) {
      case kPoly:
        if (coords_.size() >= 6) {
          int num_points = coords_.size() / 2;
          path.MoveTo(FloatPoint(ClampCoordinate(coords_[0]),
                                 ClampCoordinate(coords_[1])));
          for (int i = 1; i < num_points; ++i)
            path.AddLineTo(FloatPoint(ClampCoordinate(coords_[i * 2]),
                                      ClampCoordinate(coords_[i * 2 + 1])));
          path.CloseSubpath();
          path.SetWindRule(RULE_EVENODD);
        }
        break;
      case kCircle:
        if (coords_.size() >= 3 && coords_[2] > 0) {
          float r = ClampCoordinate(coords_[2]);
          path.AddEllipse(FloatRect(ClampCoordinate(coords_[0]) - r,
                                    ClampCoordinate(coords_[1]) - r, 2 * r,
                                    2 * r));
        }
        break;
      case kRect:
        if (coords_.size() >= 4) {
          float x0 = ClampCoordinate(coords_[0]);
          float y0 = ClampCoordinate(coords_[1]);
          float x1 = ClampCoordinate(coords_[2]);
          float y1 = ClampCoordinate(coords_[3]);
          path.AddRect(FloatRect(x0, y0, x1 - x0, y1 - y0));
        }
        break;
      default:
        NOTREACHED();
        break;
    }

    // Cache the original path, not depending on containerObject.
    path_ = std::make_unique<Path>(path);
  }

  // Zoom the path into coordinates of the container object.
  float zoom_factor = container_object->StyleRef().EffectiveZoom();
  if (zoom_factor != 1.0f) {
    AffineTransform zoom_transform;
    zoom_transform.Scale(zoom_factor);
    path.Transform(zoom_transform);
  }
  return path;
}

HTMLImageElement* HTMLAreaElement::ImageElement() const {
  if (HTMLMapElement* map_element =
          Traversal<HTMLMapElement>::FirstAncestor(*this))
    return map_element->ImageElement();
  return nullptr;
}

bool HTMLAreaElement::IsKeyboardFocusable() const {
  return IsFocusable();
}

bool HTMLAreaElement::IsMouseFocusable() const {
  return IsFocusable();
}

bool HTMLAreaElement::IsFocusableStyle() const {
  HTMLImageElement* image = ImageElement();
  if (!image || !image->GetLayoutObject() ||
      image->GetLayoutObject()->Style()->Visibility() != EVisibility::kVisible)
    return false;

  return SupportsFocus() && Element::tabIndex() >= 0;
}

void HTMLAreaElement::SetFocused(bool should_be_focused,
                                 mojom::blink::FocusType focus_type) {
  if (IsFocused() == should_be_focused)
    return;

  HTMLAnchorElement::SetFocused(should_be_focused, focus_type);

  HTMLImageElement* image_element = ImageElement();
  if (!image_element)
    return;

  LayoutObject* layout_object = image_element->GetLayoutObject();
  if (!layout_object || !layout_object->IsImage())
    return;

  ToLayoutImage(layout_object)->AreaElementFocusChanged(this);
}

void HTMLAreaElement::UpdateFocusAppearanceWithOptions(
    SelectionBehaviorOnFocus selection_behavior,
    const FocusOptions* options) {
  GetDocument().UpdateStyleAndLayoutTreeForNode(this);
  if (!IsFocusable())
    return;

  if (HTMLImageElement* image_element = ImageElement()) {
    image_element->UpdateFocusAppearanceWithOptions(selection_behavior,
                                                    options);
  }
}

}  // namespace blink