Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / html / HTMLMapElement.cpp
blobe2c6025b915722eb8638cf7736e22fc4c13c46b2
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.
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.
22 #include "config.h"
23 #include "core/html/HTMLMapElement.h"
25 #include "core/HTMLNames.h"
26 #include "core/dom/Document.h"
27 #include "core/dom/ElementTraversal.h"
28 #include "core/dom/NodeListsNodeData.h"
29 #include "core/frame/UseCounter.h"
30 #include "core/html/HTMLAreaElement.h"
31 #include "core/html/HTMLCollection.h"
32 #include "core/html/HTMLImageElement.h"
33 #include "core/layout/HitTestResult.h"
35 namespace blink {
37 using namespace HTMLNames;
39 inline HTMLMapElement::HTMLMapElement(Document& document)
40 : HTMLElement(mapTag, document)
42 UseCounter::count(document, UseCounter::MapElement);
45 DEFINE_NODE_FACTORY(HTMLMapElement)
47 HTMLMapElement::~HTMLMapElement()
51 HTMLAreaElement* HTMLMapElement::areaForPoint(LayoutPoint location, const LayoutSize& containerSize)
53 HTMLAreaElement* defaultArea = nullptr;
54 for (HTMLAreaElement& area : Traversal<HTMLAreaElement>::descendantsOf(*this)) {
55 if (area.isDefault() && !defaultArea)
56 defaultArea = &area;
57 else if (area.pointInArea(location, containerSize))
58 return &area;
61 return defaultArea;
64 HTMLImageElement* HTMLMapElement::imageElement()
66 RefPtrWillBeRawPtr<HTMLCollection> images = document().images();
67 for (unsigned i = 0; Element* curr = images->item(i); ++i) {
68 ASSERT(isHTMLImageElement(curr));
70 // The HTMLImageElement's useMap() value includes the '#' symbol at the beginning,
71 // which has to be stripped off.
72 HTMLImageElement& imageElement = toHTMLImageElement(*curr);
73 String useMapName = imageElement.getAttribute(usemapAttr).string().substring(1);
74 if (equalIgnoringCase(useMapName, m_name))
75 return &imageElement;
78 return nullptr;
81 void HTMLMapElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
83 // FIXME: This logic seems wrong for XML documents.
84 // Either the id or name will be used depending on the order the attributes are parsed.
86 if (name == idAttr || name == nameAttr) {
87 if (name == idAttr) {
88 // Call base class so that hasID bit gets set.
89 HTMLElement::parseAttribute(name, value);
90 if (document().isHTMLDocument())
91 return;
93 if (inDocument())
94 treeScope().removeImageMap(this);
95 String mapName = value;
96 if (mapName[0] == '#')
97 mapName = mapName.substring(1);
98 m_name = AtomicString(document().isHTMLDocument() ? mapName.lower() : mapName);
99 if (inDocument())
100 treeScope().addImageMap(this);
102 return;
105 HTMLElement::parseAttribute(name, value);
108 PassRefPtrWillBeRawPtr<HTMLCollection> HTMLMapElement::areas()
110 return ensureCachedCollection<HTMLCollection>(MapAreas);
113 Node::InsertionNotificationRequest HTMLMapElement::insertedInto(ContainerNode* insertionPoint)
115 if (insertionPoint->inDocument())
116 treeScope().addImageMap(this);
117 return HTMLElement::insertedInto(insertionPoint);
120 void HTMLMapElement::removedFrom(ContainerNode* insertionPoint)
122 if (insertionPoint->inDocument())
123 treeScope().removeImageMap(this);
124 HTMLElement::removedFrom(insertionPoint);