Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLMapElement.cpp
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 "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/rendering/HitTestResult.h"
34
35 namespace blink {
36
37 using namespace HTMLNames;
38
39 inline HTMLMapElement::HTMLMapElement(Document& document)
40     : HTMLElement(mapTag, document)
41 {
42     ScriptWrappable::init(this);
43     UseCounter::count(document, UseCounter::MapElement);
44 }
45
46 DEFINE_NODE_FACTORY(HTMLMapElement)
47
48 HTMLMapElement::~HTMLMapElement()
49 {
50 }
51
52 bool HTMLMapElement::mapMouseEvent(LayoutPoint location, const LayoutSize& size, HitTestResult& result)
53 {
54     HTMLAreaElement* defaultArea = 0;
55     for (HTMLAreaElement* area = Traversal<HTMLAreaElement>::firstWithin(*this); area; area = Traversal<HTMLAreaElement>::next(*area, this)) {
56         if (area->isDefault()) {
57             if (!defaultArea)
58                 defaultArea = area;
59         } else if (area->mapMouseEvent(location, size, result)) {
60             return true;
61         }
62     }
63
64     if (defaultArea) {
65         result.setInnerNode(defaultArea);
66         result.setURLElement(defaultArea);
67     }
68     return defaultArea;
69 }
70
71 HTMLImageElement* HTMLMapElement::imageElement()
72 {
73     RefPtrWillBeRawPtr<HTMLCollection> images = document().images();
74     for (unsigned i = 0; Element* curr = images->item(i); ++i) {
75         ASSERT(isHTMLImageElement(curr));
76
77         // The HTMLImageElement's useMap() value includes the '#' symbol at the beginning,
78         // which has to be stripped off.
79         HTMLImageElement& imageElement = toHTMLImageElement(*curr);
80         String useMapName = imageElement.getAttribute(usemapAttr).string().substring(1);
81         if (equalIgnoringCase(useMapName, m_name))
82             return &imageElement;
83     }
84
85     return 0;
86 }
87
88 void HTMLMapElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
89 {
90     // FIXME: This logic seems wrong for XML documents.
91     // Either the id or name will be used depending on the order the attributes are parsed.
92
93     if (isIdAttributeName(name) || name == nameAttr) {
94         if (isIdAttributeName(name)) {
95             // Call base class so that hasID bit gets set.
96             HTMLElement::parseAttribute(name, value);
97             if (document().isHTMLDocument())
98                 return;
99         }
100         if (inDocument())
101             treeScope().removeImageMap(this);
102         String mapName = value;
103         if (mapName[0] == '#')
104             mapName = mapName.substring(1);
105         m_name = AtomicString(document().isHTMLDocument() ? mapName.lower() : mapName);
106         if (inDocument())
107             treeScope().addImageMap(this);
108
109         return;
110     }
111
112     HTMLElement::parseAttribute(name, value);
113 }
114
115 PassRefPtrWillBeRawPtr<HTMLCollection> HTMLMapElement::areas()
116 {
117     return ensureCachedCollection<HTMLCollection>(MapAreas);
118 }
119
120 Node::InsertionNotificationRequest HTMLMapElement::insertedInto(ContainerNode* insertionPoint)
121 {
122     if (insertionPoint->inDocument())
123         treeScope().addImageMap(this);
124     return HTMLElement::insertedInto(insertionPoint);
125 }
126
127 void HTMLMapElement::removedFrom(ContainerNode* insertionPoint)
128 {
129     if (insertionPoint->inDocument())
130         treeScope().removeImageMap(this);
131     HTMLElement::removedFrom(insertionPoint);
132 }
133
134 }