Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 | * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 4 | * Copyright (C) 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reserved. |
| 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 |
| 18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 | * Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include "core/html/HTMLMapElement.h" |
| 24 | |
| 25 | #include "HTMLNames.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 26 | #include "core/dom/Document.h" |
| 27 | #include "core/dom/NodeTraversal.h" |
| 28 | #include "core/html/HTMLAreaElement.h" |
| 29 | #include "core/html/HTMLCollection.h" |
| 30 | #include "core/html/HTMLImageElement.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 31 | #include "core/rendering/HitTestResult.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 32 | |
| 33 | using namespace std; |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | using namespace HTMLNames; |
| 38 | |
| 39 | HTMLMapElement::HTMLMapElement(const QualifiedName& tagName, Document* document) |
| 40 | : HTMLElement(tagName, document) |
| 41 | { |
| 42 | ASSERT(hasTagName(mapTag)); |
| 43 | ScriptWrappable::init(this); |
| 44 | } |
| 45 | |
| 46 | PassRefPtr<HTMLMapElement> HTMLMapElement::create(Document* document) |
| 47 | { |
| 48 | return adoptRef(new HTMLMapElement(mapTag, document)); |
| 49 | } |
| 50 | |
| 51 | PassRefPtr<HTMLMapElement> HTMLMapElement::create(const QualifiedName& tagName, Document* document) |
| 52 | { |
| 53 | return adoptRef(new HTMLMapElement(tagName, document)); |
| 54 | } |
| 55 | |
| 56 | HTMLMapElement::~HTMLMapElement() |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | bool HTMLMapElement::mapMouseEvent(LayoutPoint location, const LayoutSize& size, HitTestResult& result) |
| 61 | { |
| 62 | HTMLAreaElement* defaultArea = 0; |
| 63 | Element* element = this; |
| 64 | while ((element = ElementTraversal::next(element, this))) { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 65 | if (isHTMLAreaElement(element)) { |
| 66 | HTMLAreaElement* areaElt = toHTMLAreaElement(element); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 67 | if (areaElt->isDefault()) { |
| 68 | if (!defaultArea) |
| 69 | defaultArea = areaElt; |
| 70 | } else if (areaElt->mapMouseEvent(location, size, result)) |
| 71 | return true; |
| 72 | } |
| 73 | } |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 74 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 75 | if (defaultArea) { |
| 76 | result.setInnerNode(defaultArea); |
| 77 | result.setURLElement(defaultArea); |
| 78 | } |
| 79 | return defaultArea; |
| 80 | } |
| 81 | |
| 82 | HTMLImageElement* HTMLMapElement::imageElement() |
| 83 | { |
| 84 | RefPtr<HTMLCollection> images = document()->images(); |
| 85 | for (unsigned i = 0; Node* curr = images->item(i); i++) { |
| 86 | if (!curr->hasTagName(imgTag)) |
| 87 | continue; |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame^] | 88 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 89 | // The HTMLImageElement's useMap() value includes the '#' symbol at the beginning, |
| 90 | // which has to be stripped off. |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 91 | HTMLImageElement* imageElement = toHTMLImageElement(curr); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 92 | String useMapName = imageElement->getAttribute(usemapAttr).string().substring(1); |
| 93 | if (equalIgnoringCase(useMapName, m_name)) |
| 94 | return imageElement; |
| 95 | } |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame^] | 96 | |
| 97 | return 0; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void HTMLMapElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
| 101 | { |
| 102 | // FIXME: This logic seems wrong for XML documents. |
| 103 | // Either the id or name will be used depending on the order the attributes are parsed. |
| 104 | |
| 105 | if (isIdAttributeName(name) || name == nameAttr) { |
| 106 | if (isIdAttributeName(name)) { |
| 107 | // Call base class so that hasID bit gets set. |
| 108 | HTMLElement::parseAttribute(name, value); |
| 109 | if (document()->isHTMLDocument()) |
| 110 | return; |
| 111 | } |
| 112 | if (inDocument()) |
| 113 | treeScope()->removeImageMap(this); |
| 114 | String mapName = value; |
| 115 | if (mapName[0] == '#') |
| 116 | mapName = mapName.substring(1); |
| 117 | m_name = document()->isHTMLDocument() ? mapName.lower() : mapName; |
| 118 | if (inDocument()) |
| 119 | treeScope()->addImageMap(this); |
| 120 | |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | HTMLElement::parseAttribute(name, value); |
| 125 | } |
| 126 | |
| 127 | PassRefPtr<HTMLCollection> HTMLMapElement::areas() |
| 128 | { |
| 129 | return ensureCachedHTMLCollection(MapAreas); |
| 130 | } |
| 131 | |
| 132 | Node::InsertionNotificationRequest HTMLMapElement::insertedInto(ContainerNode* insertionPoint) |
| 133 | { |
| 134 | if (insertionPoint->inDocument()) |
| 135 | treeScope()->addImageMap(this); |
| 136 | return HTMLElement::insertedInto(insertionPoint); |
| 137 | } |
| 138 | |
| 139 | void HTMLMapElement::removedFrom(ContainerNode* insertionPoint) |
| 140 | { |
| 141 | if (insertionPoint->inDocument()) |
| 142 | treeScope()->removeImageMap(this); |
| 143 | HTMLElement::removedFrom(insertionPoint); |
| 144 | } |
| 145 | |
| 146 | } |