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