blob: 2d5d65fe0bd63aca3eea85f104ff14494a1baf61 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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)53e740f2013-05-09 18:38:43 +010026#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)53e740f2013-05-09 18:38:43 +010031#include "core/rendering/HitTestResult.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010032
33using namespace std;
34
35namespace WebCore {
36
37using namespace HTMLNames;
38
39HTMLMapElement::HTMLMapElement(const QualifiedName& tagName, Document* document)
40 : HTMLElement(tagName, document)
41{
42 ASSERT(hasTagName(mapTag));
43 ScriptWrappable::init(this);
44}
45
46PassRefPtr<HTMLMapElement> HTMLMapElement::create(Document* document)
47{
48 return adoptRef(new HTMLMapElement(mapTag, document));
49}
50
51PassRefPtr<HTMLMapElement> HTMLMapElement::create(const QualifiedName& tagName, Document* document)
52{
53 return adoptRef(new HTMLMapElement(tagName, document));
54}
55
56HTMLMapElement::~HTMLMapElement()
57{
58}
59
60bool 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 Murdoche69819b2013-07-17 14:56:49 +010065 if (isHTMLAreaElement(element)) {
66 HTMLAreaElement* areaElt = toHTMLAreaElement(element);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010067 if (areaElt->isDefault()) {
68 if (!defaultArea)
69 defaultArea = areaElt;
70 } else if (areaElt->mapMouseEvent(location, size, result))
71 return true;
72 }
73 }
Ben Murdoche69819b2013-07-17 14:56:49 +010074
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010075 if (defaultArea) {
76 result.setInnerNode(defaultArea);
77 result.setURLElement(defaultArea);
78 }
79 return defaultArea;
80}
81
82HTMLImageElement* 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 Murdoch02772c62013-07-26 10:21:05 +010088
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010089 // The HTMLImageElement's useMap() value includes the '#' symbol at the beginning,
90 // which has to be stripped off.
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010091 HTMLImageElement* imageElement = toHTMLImageElement(curr);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010092 String useMapName = imageElement->getAttribute(usemapAttr).string().substring(1);
93 if (equalIgnoringCase(useMapName, m_name))
94 return imageElement;
95 }
Ben Murdoch02772c62013-07-26 10:21:05 +010096
97 return 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010098}
99
100void 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
127PassRefPtr<HTMLCollection> HTMLMapElement::areas()
128{
129 return ensureCachedHTMLCollection(MapAreas);
130}
131
132Node::InsertionNotificationRequest HTMLMapElement::insertedInto(ContainerNode* insertionPoint)
133{
134 if (insertionPoint->inDocument())
135 treeScope()->addImageMap(this);
136 return HTMLElement::insertedInto(insertionPoint);
137}
138
139void HTMLMapElement::removedFrom(ContainerNode* insertionPoint)
140{
141 if (insertionPoint->inDocument())
142 treeScope()->removeImageMap(this);
143 HTMLElement::removedFrom(insertionPoint);
144}
145
146}