1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * Copyright (C) 2004, 2005, 2006, 2007 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 "HTMLMapElement.h" 24 25 #include "Document.h" 26 #include "HTMLAreaElement.h" 27 #include "HTMLCollection.h" 28 #include "HTMLImageElement.h" 29 #include "HTMLNames.h" 30 #include "HitTestResult.h" 31 #include "IntSize.h" 32 #include "MappedAttribute.h" 33 #include "RenderObject.h" 34 35 using namespace std; 36 37 namespace WebCore { 38 39 using namespace HTMLNames; 40 41 HTMLMapElement::HTMLMapElement(const QualifiedName& tagName, Document* doc) 42 : HTMLElement(tagName, doc) 43 { 44 ASSERT(hasTagName(mapTag)); 45 } 46 47 HTMLMapElement::~HTMLMapElement() 48 { 49 document()->removeImageMap(this); 50 } 51 52 bool HTMLMapElement::checkDTD(const Node* newChild) 53 { 54 return inEitherTagList(newChild) || newChild->hasTagName(areaTag) // HTML 4 DTD 55 || newChild->hasTagName(scriptTag); // extensions 56 } 57 58 bool HTMLMapElement::mapMouseEvent(int x, int y, const IntSize& size, HitTestResult& result) 59 { 60 HTMLAreaElement* defaultArea = 0; 61 Node *node = this; 62 while ((node = node->traverseNextNode(this))) { 63 if (node->hasTagName(areaTag)) { 64 HTMLAreaElement* areaElt = static_cast<HTMLAreaElement*>(node); 65 if (areaElt->isDefault()) { 66 if (!defaultArea) 67 defaultArea = areaElt; 68 } else if (areaElt->mapMouseEvent(x, y, size, result)) 69 return true; 70 } 71 } 72 73 if (defaultArea) { 74 result.setInnerNode(defaultArea); 75 result.setURLElement(defaultArea); 76 } 77 return defaultArea; 78 } 79 80 HTMLImageElement* HTMLMapElement::imageElement() const 81 { 82 RefPtr<HTMLCollection> coll = renderer()->document()->images(); 83 for (Node* curr = coll->firstItem(); curr; curr = coll->nextItem()) { 84 if (!curr->hasTagName(imgTag)) 85 continue; 86 87 // The HTMLImageElement's useMap() value includes the '#' symbol at the beginning, 88 // which has to be stripped off. 89 HTMLImageElement* imageElement = static_cast<HTMLImageElement*>(curr); 90 String useMapName = imageElement->getAttribute(usemapAttr).string().substring(1); 91 if (equalIgnoringCase(useMapName, m_name)) 92 return imageElement; 93 } 94 95 return 0; 96 } 97 98 void HTMLMapElement::parseMappedAttribute(MappedAttribute* attr) 99 { 100 const QualifiedName& attrName = attr->name(); 101 if (attrName == idAttributeName() || attrName == nameAttr) { 102 Document* doc = document(); 103 if (attrName == idAttributeName()) { 104 // Call base class so that hasID bit gets set. 105 HTMLElement::parseMappedAttribute(attr); 106 if (doc->isHTMLDocument()) 107 return; 108 } 109 doc->removeImageMap(this); 110 String mapName = attr->value(); 111 if (mapName[0] == '#') 112 mapName = mapName.substring(1); 113 m_name = doc->isHTMLDocument() ? mapName.lower() : mapName; 114 doc->addImageMap(this); 115 } else 116 HTMLElement::parseMappedAttribute(attr); 117 } 118 119 PassRefPtr<HTMLCollection> HTMLMapElement::areas() 120 { 121 return HTMLCollection::create(this, MapAreas); 122 } 123 124 String HTMLMapElement::name() const 125 { 126 return getAttribute(nameAttr); 127 } 128 129 void HTMLMapElement::setName(const String& value) 130 { 131 setAttribute(nameAttr, value); 132 } 133 134 } 135