darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 1 | /* |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 2 | * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 | * (C) 1999 Antti Koivisto (koivisto@kde.org) |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 4 | * Copyright (C) 2004, 2005, 2006, 2009, 2011 Apple Inc. All rights reserved. |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 | * Boston, MA 02110-1301, USA. |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 20 | */ |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 21 | |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | #include "HTMLAreaElement.h" |
| 24 | |
tonyg@chromium.org | 9c38421 | 2011-01-19 23:13:26 +0000 | [diff] [blame] | 25 | #include "AffineTransform.h" |
weinig@apple.com | c360893 | 2010-05-19 17:48:06 +0000 | [diff] [blame] | 26 | #include "Attribute.h" |
yael.aharon@nokia.com | 8c59fca | 2010-11-19 16:05:13 +0000 | [diff] [blame] | 27 | #include "Frame.h" |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 28 | #include "HTMLImageElement.h" |
| 29 | #include "HTMLMapElement.h" |
bdakin | c1a5a83 | 2006-10-28 18:28:00 +0000 | [diff] [blame] | 30 | #include "HitTestResult.h" |
treat@webkit.org | 0d381b4 | 2009-07-15 23:02:01 +0000 | [diff] [blame] | 31 | #include "Path.h" |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 32 | #include "RenderImage.h" |
commit-queue@webkit.org | c558b51 | 2012-03-27 19:19:59 +0000 | [diff] [blame] | 33 | #include "RenderView.h" |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 34 | |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 35 | namespace WebCore { |
| 36 | |
| 37 | using namespace HTMLNames; |
| 38 | |
weinig@apple.com | c1d18d0 | 2013-09-14 19:51:53 +0000 | [diff] [blame] | 39 | inline HTMLAreaElement::HTMLAreaElement(const QualifiedName& tagName, Document& document) |
darin@apple.com | e8c3a2a | 2009-02-02 17:29:54 +0000 | [diff] [blame] | 40 | : HTMLAnchorElement(tagName, document) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 41 | , m_coordsLen(0) |
| 42 | , m_lastSize(-1, -1) |
| 43 | , m_shape(Unknown) |
| 44 | { |
jchaffraix@webkit.org | 94d95b0 | 2008-12-04 22:39:05 +0000 | [diff] [blame] | 45 | ASSERT(hasTagName(areaTag)); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 46 | } |
| 47 | |
weinig@apple.com | c1d18d0 | 2013-09-14 19:51:53 +0000 | [diff] [blame] | 48 | PassRefPtr<HTMLAreaElement> HTMLAreaElement::create(const QualifiedName& tagName, Document& document) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 49 | { |
darin@apple.com | cf9dd0f | 2009-08-23 06:55:57 +0000 | [diff] [blame] | 50 | return adoptRef(new HTMLAreaElement(tagName, document)); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 51 | } |
| 52 | |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 53 | void HTMLAreaElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 54 | { |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 55 | if (name == shapeAttr) { |
| 56 | if (equalIgnoringCase(value, "default")) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 57 | m_shape = Default; |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 58 | else if (equalIgnoringCase(value, "circle")) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 59 | m_shape = Circle; |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 60 | else if (equalIgnoringCase(value, "poly")) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 61 | m_shape = Poly; |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 62 | else if (equalIgnoringCase(value, "rect")) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 63 | m_shape = Rect; |
andreas.kling@nokia.com | 7b570a5 | 2011-05-17 19:26:11 +0000 | [diff] [blame] | 64 | invalidateCachedRegion(); |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 65 | } else if (name == coordsAttr) { |
| 66 | m_coords = newCoordsArray(value.string(), m_coordsLen); |
andreas.kling@nokia.com | 7b570a5 | 2011-05-17 19:26:11 +0000 | [diff] [blame] | 67 | invalidateCachedRegion(); |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 68 | } else if (name == altAttr || name == accesskeyAttr) { |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 69 | // Do nothing. |
| 70 | } else |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 71 | HTMLAnchorElement::parseAttribute(name, value); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 72 | } |
| 73 | |
andreas.kling@nokia.com | 7b570a5 | 2011-05-17 19:26:11 +0000 | [diff] [blame] | 74 | void HTMLAreaElement::invalidateCachedRegion() |
| 75 | { |
eae@chromium.org | 020f587 | 2011-08-18 03:38:05 +0000 | [diff] [blame] | 76 | m_lastSize = LayoutSize(-1, -1); |
andreas.kling@nokia.com | 7b570a5 | 2011-05-17 19:26:11 +0000 | [diff] [blame] | 77 | } |
| 78 | |
eae@chromium.org | 020f587 | 2011-08-18 03:38:05 +0000 | [diff] [blame] | 79 | bool HTMLAreaElement::mapMouseEvent(LayoutPoint location, const LayoutSize& size, HitTestResult& result) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 80 | { |
| 81 | if (m_lastSize != size) { |
abarth@webkit.org | e5a57d8 | 2010-08-24 18:31:25 +0000 | [diff] [blame] | 82 | m_region = adoptPtr(new Path(getRegion(size))); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 83 | m_lastSize = size; |
| 84 | } |
| 85 | |
eae@chromium.org | 020f587 | 2011-08-18 03:38:05 +0000 | [diff] [blame] | 86 | if (!m_region->contains(location)) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 87 | return false; |
| 88 | |
bdakin | 1c628c0 | 2006-10-29 22:07:47 +0000 | [diff] [blame] | 89 | result.setInnerNode(this); |
| 90 | result.setURLElement(this); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
dbates@webkit.org | 1edd81d | 2013-12-18 00:15:02 +0000 | [diff] [blame] | 94 | // FIXME: We should use RenderElement* instead of RenderObject* once we upstream iOS's DOMUIKitExtensions.{h, mm}. |
| 95 | Path HTMLAreaElement::computePath(RenderObject* obj) const |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 96 | { |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 97 | if (!obj) |
| 98 | return Path(); |
| 99 | |
simon.fraser@apple.com | c65d328 | 2008-11-04 18:54:50 +0000 | [diff] [blame] | 100 | // FIXME: This doesn't work correctly with transforms. |
| 101 | FloatPoint absPos = obj->localToAbsolute(); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 102 | |
| 103 | // Default should default to the size of the containing object. |
eae@chromium.org | 020f587 | 2011-08-18 03:38:05 +0000 | [diff] [blame] | 104 | LayoutSize size = m_lastSize; |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 105 | if (m_shape == Default) |
| 106 | size = obj->absoluteOutlineBounds().size(); |
| 107 | |
| 108 | Path p = getRegion(size); |
akling@apple.com | 827be9c | 2013-10-29 02:58:43 +0000 | [diff] [blame] | 109 | float zoomFactor = obj->style().effectiveZoom(); |
yael.aharon@nokia.com | 8c59fca | 2010-11-19 16:05:13 +0000 | [diff] [blame] | 110 | if (zoomFactor != 1.0f) { |
| 111 | AffineTransform zoomTransform; |
| 112 | zoomTransform.scale(zoomFactor); |
| 113 | p.transform(zoomTransform); |
| 114 | } |
| 115 | |
commit-queue@webkit.org | 00e1f2f | 2013-01-08 07:46:58 +0000 | [diff] [blame] | 116 | p.translate(toFloatSize(absPos)); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 117 | return p; |
| 118 | } |
dbates@webkit.org | 1edd81d | 2013-12-18 00:15:02 +0000 | [diff] [blame] | 119 | |
| 120 | // FIXME: Use RenderElement* instead of RenderObject* once we upstream iOS's DOMUIKitExtensions.{h, mm}. |
| 121 | LayoutRect HTMLAreaElement::computeRect(RenderObject* obj) const |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 122 | { |
timothy_horton@apple.com | 581c839 | 2011-11-07 20:40:24 +0000 | [diff] [blame] | 123 | return enclosingLayoutRect(computePath(obj).fastBoundingRect()); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 124 | } |
| 125 | |
eae@chromium.org | 020f587 | 2011-08-18 03:38:05 +0000 | [diff] [blame] | 126 | Path HTMLAreaElement::getRegion(const LayoutSize& size) const |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 127 | { |
hyatt | 00af680d | 2006-09-19 21:31:59 +0000 | [diff] [blame] | 128 | if (!m_coords && m_shape != Default) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 129 | return Path(); |
hyatt | 00af680d | 2006-09-19 21:31:59 +0000 | [diff] [blame] | 130 | |
eae@chromium.org | 020f587 | 2011-08-18 03:38:05 +0000 | [diff] [blame] | 131 | LayoutUnit width = size.width(); |
| 132 | LayoutUnit height = size.height(); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 133 | |
| 134 | // If element omits the shape attribute, select shape based on number of coordinates. |
| 135 | Shape shape = m_shape; |
| 136 | if (shape == Unknown) { |
| 137 | if (m_coordsLen == 3) |
| 138 | shape = Circle; |
| 139 | else if (m_coordsLen == 4) |
| 140 | shape = Rect; |
| 141 | else if (m_coordsLen >= 6) |
| 142 | shape = Poly; |
| 143 | } |
| 144 | |
| 145 | Path path; |
akling@apple.com | 622b1a4 | 2013-08-30 14:30:12 +0000 | [diff] [blame] | 146 | RenderView* renderView = document().renderView(); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 147 | switch (shape) { |
| 148 | case Poly: |
| 149 | if (m_coordsLen >= 6) { |
| 150 | int numPoints = m_coordsLen / 2; |
commit-queue@webkit.org | c558b51 | 2012-03-27 19:19:59 +0000 | [diff] [blame] | 151 | path.moveTo(FloatPoint(minimumValueForLength(m_coords[0], width, renderView), minimumValueForLength(m_coords[1], height, renderView))); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 152 | for (int i = 1; i < numPoints; ++i) |
commit-queue@webkit.org | c558b51 | 2012-03-27 19:19:59 +0000 | [diff] [blame] | 153 | path.addLineTo(FloatPoint(minimumValueForLength(m_coords[i * 2], width, renderView), minimumValueForLength(m_coords[i * 2 + 1], height, renderView))); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 154 | path.closeSubpath(); |
| 155 | } |
| 156 | break; |
| 157 | case Circle: |
| 158 | if (m_coordsLen >= 3) { |
| 159 | Length radius = m_coords[2]; |
andersca@apple.com | ff9adb8 | 2013-10-25 01:15:36 +0000 | [diff] [blame] | 160 | int r = std::min(minimumValueForLength(radius, width, renderView), minimumValueForLength(radius, height, renderView)); |
commit-queue@webkit.org | c558b51 | 2012-03-27 19:19:59 +0000 | [diff] [blame] | 161 | path.addEllipse(FloatRect(minimumValueForLength(m_coords[0], width, renderView) - r, minimumValueForLength(m_coords[1], height, renderView) - r, 2 * r, 2 * r)); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 162 | } |
| 163 | break; |
| 164 | case Rect: |
| 165 | if (m_coordsLen >= 4) { |
commit-queue@webkit.org | c558b51 | 2012-03-27 19:19:59 +0000 | [diff] [blame] | 166 | int x0 = minimumValueForLength(m_coords[0], width, renderView); |
| 167 | int y0 = minimumValueForLength(m_coords[1], height, renderView); |
| 168 | int x1 = minimumValueForLength(m_coords[2], width, renderView); |
| 169 | int y1 = minimumValueForLength(m_coords[3], height, renderView); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 170 | path.addRect(FloatRect(x0, y0, x1 - x0, y1 - y0)); |
| 171 | } |
| 172 | break; |
| 173 | case Default: |
| 174 | path.addRect(FloatRect(0, 0, width, height)); |
| 175 | break; |
| 176 | case Unknown: |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | return path; |
| 181 | } |
| 182 | |
commit-queue@webkit.org | c1beaa0 | 2012-03-26 18:51:52 +0000 | [diff] [blame] | 183 | HTMLImageElement* HTMLAreaElement::imageElement() const |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 184 | { |
dglazkov@chromium.org | e43caa7 | 2010-11-18 00:20:10 +0000 | [diff] [blame] | 185 | Node* mapElement = parentNode(); |
kangil.han@samsung.com | be99eac | 2013-07-04 04:31:30 +0000 | [diff] [blame] | 186 | if (!mapElement || !isHTMLMapElement(mapElement)) |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 187 | return 0; |
| 188 | |
kangil.han@samsung.com | be99eac | 2013-07-04 04:31:30 +0000 | [diff] [blame] | 189 | return toHTMLMapElement(mapElement)->imageElement(); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 190 | } |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 191 | |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 192 | bool HTMLAreaElement::isKeyboardFocusable(KeyboardEvent*) const |
| 193 | { |
cfleizach@apple.com | 77be71c | 2010-10-06 22:09:04 +0000 | [diff] [blame] | 194 | return isFocusable(); |
| 195 | } |
| 196 | |
| 197 | bool HTMLAreaElement::isMouseFocusable() const |
| 198 | { |
| 199 | return isFocusable(); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | bool HTMLAreaElement::isFocusable() const |
| 203 | { |
commit-queue@webkit.org | c1beaa0 | 2012-03-26 18:51:52 +0000 | [diff] [blame] | 204 | HTMLImageElement* image = imageElement(); |
akling@apple.com | 827be9c | 2013-10-29 02:58:43 +0000 | [diff] [blame] | 205 | if (!image || !image->renderer() || image->renderer()->style().visibility() != VISIBLE) |
commit-queue@webkit.org | c1beaa0 | 2012-03-26 18:51:52 +0000 | [diff] [blame] | 206 | return false; |
| 207 | |
cfleizach@apple.com | 77be71c | 2010-10-06 22:09:04 +0000 | [diff] [blame] | 208 | return supportsFocus() && Element::tabIndex() >= 0; |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 209 | } |
| 210 | |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 211 | void HTMLAreaElement::setFocus(bool shouldBeFocused) |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 212 | { |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 213 | if (focused() == shouldBeFocused) |
| 214 | return; |
| 215 | |
| 216 | HTMLAnchorElement::setFocus(shouldBeFocused); |
| 217 | |
| 218 | HTMLImageElement* imageElement = this->imageElement(); |
| 219 | if (!imageElement) |
| 220 | return; |
| 221 | |
darin@apple.com | 8cdf712 | 2013-09-30 02:40:50 +0000 | [diff] [blame] | 222 | auto renderer = imageElement->renderer(); |
jhoneycutt@apple.com | 59248b4 | 2014-01-24 02:55:15 +0000 | [diff] [blame] | 223 | if (!renderer || !renderer->isRenderImage()) |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 224 | return; |
| 225 | |
| 226 | toRenderImage(renderer)->areaElementFocusChanged(this); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void HTMLAreaElement::updateFocusAppearance(bool restorePreviousSelection) |
| 230 | { |
cfleizach@apple.com | 77be71c | 2010-10-06 22:09:04 +0000 | [diff] [blame] | 231 | if (!isFocusable()) |
| 232 | return; |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 233 | |
| 234 | HTMLImageElement* imageElement = this->imageElement(); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 235 | if (!imageElement) |
| 236 | return; |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 237 | |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 238 | imageElement->updateFocusAppearance(restorePreviousSelection); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 239 | } |
| 240 | |
abarth@webkit.org | 4093379 | 2009-09-07 05:10:12 +0000 | [diff] [blame] | 241 | bool HTMLAreaElement::supportsFocus() const |
alice.liu@apple.com | 8648482 | 2008-04-29 01:18:52 +0000 | [diff] [blame] | 242 | { |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 243 | // If the AREA element was a link, it should support focus. |
| 244 | // The inherited method is not used because it assumes that a render object must exist |
| 245 | // for the element to support focus. AREA elements do not have render objects. |
| 246 | return isLink(); |
alice.liu@apple.com | 8648482 | 2008-04-29 01:18:52 +0000 | [diff] [blame] | 247 | } |
| 248 | |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 249 | String HTMLAreaElement::target() const |
| 250 | { |
commit-queue@webkit.org | 03477c8 | 2011-09-02 17:07:51 +0000 | [diff] [blame] | 251 | return getAttribute(targetAttr); |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 252 | } |
| 253 | |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 254 | } |