2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2009, 2011 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "core/html/HTMLAreaElement.h"
25 #include "HTMLNames.h"
26 #include "core/html/HTMLImageElement.h"
27 #include "core/html/HTMLMapElement.h"
28 #include "core/rendering/HitTestResult.h"
29 #include "core/rendering/RenderImage.h"
30 #include "core/rendering/RenderView.h"
31 #include "platform/LengthFunctions.h"
32 #include "platform/graphics/Path.h"
33 #include "platform/transforms/AffineTransform.h"
39 using namespace HTMLNames;
41 inline HTMLAreaElement::HTMLAreaElement(Document& document)
42 : HTMLAnchorElement(areaTag, document)
46 ScriptWrappable::init(this);
49 PassRefPtr<HTMLAreaElement> HTMLAreaElement::create(Document& document)
51 return adoptRef(new HTMLAreaElement(document));
54 void HTMLAreaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
56 if (name == shapeAttr) {
57 if (equalIgnoringCase(value, "default"))
59 else if (equalIgnoringCase(value, "circle"))
61 else if (equalIgnoringCase(value, "poly"))
63 else if (equalIgnoringCase(value, "rect"))
65 invalidateCachedRegion();
66 } else if (name == coordsAttr) {
67 m_coords = parseHTMLAreaElementCoords(value.string());
68 invalidateCachedRegion();
69 } else if (name == altAttr || name == accesskeyAttr) {
72 HTMLAnchorElement::parseAttribute(name, value);
75 void HTMLAreaElement::invalidateCachedRegion()
77 m_lastSize = LayoutSize(-1, -1);
80 bool HTMLAreaElement::mapMouseEvent(LayoutPoint location, const LayoutSize& size, HitTestResult& result)
82 if (m_lastSize != size) {
83 m_region = adoptPtr(new Path(getRegion(size)));
87 if (!m_region->contains(location))
90 result.setInnerNode(this);
91 result.setURLElement(this);
95 Path HTMLAreaElement::computePath(RenderObject* obj) const
100 // FIXME: This doesn't work correctly with transforms.
101 FloatPoint absPos = obj->localToAbsolute();
103 // Default should default to the size of the containing object.
104 LayoutSize size = m_lastSize;
105 if (m_shape == Default)
106 size = obj->absoluteClippedOverflowRect().size();
108 Path p = getRegion(size);
109 float zoomFactor = obj->style()->effectiveZoom();
110 if (zoomFactor != 1.0f) {
111 AffineTransform zoomTransform;
112 zoomTransform.scale(zoomFactor);
113 p.transform(zoomTransform);
116 p.translate(toFloatSize(absPos));
120 LayoutRect HTMLAreaElement::computeRect(RenderObject* obj) const
122 return enclosingLayoutRect(computePath(obj).boundingRect());
125 Path HTMLAreaElement::getRegion(const LayoutSize& size) const
127 if (m_coords.isEmpty() && m_shape != Default)
130 LayoutUnit width = size.width();
131 LayoutUnit height = size.height();
133 // If element omits the shape attribute, select shape based on number of coordinates.
134 Shape shape = m_shape;
135 if (shape == Unknown) {
136 if (m_coords.size() == 3)
138 else if (m_coords.size() == 4)
140 else if (m_coords.size() >= 6)
147 if (m_coords.size() >= 6) {
148 int numPoints = m_coords.size() / 2;
149 path.moveTo(FloatPoint(minimumValueForLength(m_coords[0], width).toFloat(), minimumValueForLength(m_coords[1], height).toFloat()));
150 for (int i = 1; i < numPoints; ++i)
151 path.addLineTo(FloatPoint(minimumValueForLength(m_coords[i * 2], width).toFloat(), minimumValueForLength(m_coords[i * 2 + 1], height).toFloat()));
156 if (m_coords.size() >= 3) {
157 Length radius = m_coords[2];
158 float r = min(minimumValueForLength(radius, width).toFloat(), minimumValueForLength(radius, height).toFloat());
159 path.addEllipse(FloatRect(minimumValueForLength(m_coords[0], width).toFloat() - r, minimumValueForLength(m_coords[1], height).toFloat() - r, 2 * r, 2 * r));
163 if (m_coords.size() >= 4) {
164 float x0 = minimumValueForLength(m_coords[0], width).toFloat();
165 float y0 = minimumValueForLength(m_coords[1], height).toFloat();
166 float x1 = minimumValueForLength(m_coords[2], width).toFloat();
167 float y1 = minimumValueForLength(m_coords[3], height).toFloat();
168 path.addRect(FloatRect(x0, y0, x1 - x0, y1 - y0));
172 path.addRect(FloatRect(0, 0, width.toFloat(), height.toFloat()));
181 HTMLImageElement* HTMLAreaElement::imageElement() const
183 Element* mapElement = parentElement();
184 while (mapElement && !isHTMLMapElement(*mapElement))
185 mapElement = mapElement->parentElement();
190 return toHTMLMapElement(*mapElement).imageElement();
193 bool HTMLAreaElement::isKeyboardFocusable() const
195 return isFocusable();
198 bool HTMLAreaElement::isMouseFocusable() const
200 return isFocusable();
203 bool HTMLAreaElement::rendererIsFocusable() const
205 HTMLImageElement* image = imageElement();
206 if (!image || !image->renderer() || image->renderer()->style()->visibility() != VISIBLE)
209 return supportsFocus() && Element::tabIndex() >= 0;
212 void HTMLAreaElement::setFocus(bool shouldBeFocused)
214 if (focused() == shouldBeFocused)
217 HTMLAnchorElement::setFocus(shouldBeFocused);
219 HTMLImageElement* imageElement = this->imageElement();
223 RenderObject* renderer = imageElement->renderer();
224 if (!renderer || !renderer->isImage())
227 toRenderImage(renderer)->areaElementFocusChanged(this);
230 void HTMLAreaElement::updateFocusAppearance(bool restorePreviousSelection)
235 HTMLImageElement* imageElement = this->imageElement();
239 imageElement->updateFocusAppearance(restorePreviousSelection);
242 bool HTMLAreaElement::supportsFocus() const
244 // If the AREA element was a link, it should support focus.
245 // FIXME: This means that an AREA that is not a link cannot be made focusable through contenteditable or tabindex. Is it correct?