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.
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/HTMLMapElement.h"
25 #include "HTMLNames.h"
26 #include "core/dom/Document.h"
27 #include "core/dom/ElementTraversal.h"
28 #include "core/html/HTMLAreaElement.h"
29 #include "core/html/HTMLCollection.h"
30 #include "core/html/HTMLImageElement.h"
31 #include "core/rendering/HitTestResult.h"
37 using namespace HTMLNames;
39 HTMLMapElement::HTMLMapElement(Document& document)
40 : HTMLElement(mapTag, document)
42 ScriptWrappable::init(this);
45 PassRefPtr<HTMLMapElement> HTMLMapElement::create(Document& document)
47 return adoptRef(new HTMLMapElement(document));
50 HTMLMapElement::~HTMLMapElement()
54 bool HTMLMapElement::mapMouseEvent(LayoutPoint location, const LayoutSize& size, HitTestResult& result)
56 HTMLAreaElement* defaultArea = 0;
57 for (HTMLAreaElement* area = Traversal<HTMLAreaElement>::firstWithin(*this); area; area = Traversal<HTMLAreaElement>::next(*area, this)) {
58 if (area->isDefault()) {
61 } else if (area->mapMouseEvent(location, size, result)) {
67 result.setInnerNode(defaultArea);
68 result.setURLElement(defaultArea);
73 HTMLImageElement* HTMLMapElement::imageElement()
75 RefPtr<HTMLCollection> images = document().images();
76 for (unsigned i = 0; Element* curr = images->item(i); i++) {
77 ASSERT(isHTMLImageElement(curr));
79 // The HTMLImageElement's useMap() value includes the '#' symbol at the beginning,
80 // which has to be stripped off.
81 HTMLImageElement& imageElement = toHTMLImageElement(*curr);
82 String useMapName = imageElement.getAttribute(usemapAttr).string().substring(1);
83 if (equalIgnoringCase(useMapName, m_name))
90 void HTMLMapElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
92 // FIXME: This logic seems wrong for XML documents.
93 // Either the id or name will be used depending on the order the attributes are parsed.
95 if (isIdAttributeName(name) || name == nameAttr) {
96 if (isIdAttributeName(name)) {
97 // Call base class so that hasID bit gets set.
98 HTMLElement::parseAttribute(name, value);
99 if (document().isHTMLDocument())
103 treeScope().removeImageMap(this);
104 String mapName = value;
105 if (mapName[0] == '#')
106 mapName = mapName.substring(1);
107 m_name = AtomicString(document().isHTMLDocument() ? mapName.lower() : mapName);
109 treeScope().addImageMap(this);
114 HTMLElement::parseAttribute(name, value);
117 PassRefPtr<HTMLCollection> HTMLMapElement::areas()
119 return ensureCachedHTMLCollection(MapAreas);
122 Node::InsertionNotificationRequest HTMLMapElement::insertedInto(ContainerNode* insertionPoint)
124 if (insertionPoint->inDocument())
125 treeScope().addImageMap(this);
126 return HTMLElement::insertedInto(insertionPoint);
129 void HTMLMapElement::removedFrom(ContainerNode* insertionPoint)
131 if (insertionPoint->inDocument())
132 treeScope().removeImageMap(this);
133 HTMLElement::removedFrom(insertionPoint);