WebCore/html/HTMLMapElement.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 "HTMLMapElement.h"
       
    24 
       
    25 #include "Attribute.h"
       
    26 #include "Document.h"
       
    27 #include "HTMLAreaElement.h"
       
    28 #include "HTMLCollection.h"
       
    29 #include "HTMLImageElement.h"
       
    30 #include "HTMLNames.h"
       
    31 #include "HitTestResult.h"
       
    32 #include "IntSize.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* document)
       
    42     : HTMLElement(tagName, document)
       
    43 {
       
    44     ASSERT(hasTagName(mapTag));
       
    45 }
       
    46 
       
    47 PassRefPtr<HTMLMapElement> HTMLMapElement::create(Document* document)
       
    48 {
       
    49     return adoptRef(new HTMLMapElement(mapTag, document));
       
    50 }
       
    51 
       
    52 PassRefPtr<HTMLMapElement> HTMLMapElement::create(const QualifiedName& tagName, Document* document)
       
    53 {
       
    54     return adoptRef(new HTMLMapElement(tagName, document));
       
    55 }
       
    56 
       
    57 HTMLMapElement::~HTMLMapElement()
       
    58 {
       
    59 }
       
    60 
       
    61 bool HTMLMapElement::checkDTD(const Node* newChild)
       
    62 {
       
    63     return inEitherTagList(newChild) || newChild->hasTagName(areaTag) // HTML 4 DTD
       
    64         || newChild->hasTagName(scriptTag); // extensions
       
    65 }
       
    66 
       
    67 bool HTMLMapElement::mapMouseEvent(int x, int y, const IntSize& size, HitTestResult& result)
       
    68 {
       
    69     HTMLAreaElement* defaultArea = 0;
       
    70     Node *node = this;
       
    71     while ((node = node->traverseNextNode(this))) {
       
    72         if (node->hasTagName(areaTag)) {
       
    73             HTMLAreaElement* areaElt = static_cast<HTMLAreaElement*>(node);
       
    74             if (areaElt->isDefault()) {
       
    75                 if (!defaultArea)
       
    76                     defaultArea = areaElt;
       
    77             } else if (areaElt->mapMouseEvent(x, y, size, result))
       
    78                 return true;
       
    79         }
       
    80     }
       
    81     
       
    82     if (defaultArea) {
       
    83         result.setInnerNode(defaultArea);
       
    84         result.setURLElement(defaultArea);
       
    85     }
       
    86     return defaultArea;
       
    87 }
       
    88 
       
    89 HTMLImageElement* HTMLMapElement::imageElement() const
       
    90 {
       
    91     RefPtr<HTMLCollection> coll = document()->images();
       
    92     for (Node* curr = coll->firstItem(); curr; curr = coll->nextItem()) {
       
    93         if (!curr->hasTagName(imgTag))
       
    94             continue;
       
    95         
       
    96         // The HTMLImageElement's useMap() value includes the '#' symbol at the beginning,
       
    97         // which has to be stripped off.
       
    98         HTMLImageElement* imageElement = static_cast<HTMLImageElement*>(curr);
       
    99         String useMapName = imageElement->getAttribute(usemapAttr).string().substring(1);
       
   100         if (equalIgnoringCase(useMapName, m_name))
       
   101             return imageElement;
       
   102     }
       
   103     
       
   104     return 0;    
       
   105 }
       
   106 
       
   107 void HTMLMapElement::parseMappedAttribute(Attribute* attribute)
       
   108 {
       
   109     // FIXME: This logic seems wrong for XML documents.
       
   110     // Either the id or name will be used depending on the order the attributes are parsed.
       
   111 
       
   112     const QualifiedName& attrName = attribute->name();
       
   113     if (isIdAttributeName(attrName) || attrName == nameAttr) {
       
   114         Document* document = this->document();
       
   115         if (isIdAttributeName(attrName)) {
       
   116             // Call base class so that hasID bit gets set.
       
   117             HTMLElement::parseMappedAttribute(attribute);
       
   118             if (document->isHTMLDocument())
       
   119                 return;
       
   120         }
       
   121         if (inDocument())
       
   122             document->removeImageMap(this);
       
   123         String mapName = attribute->value();
       
   124         if (mapName[0] == '#')
       
   125             mapName = mapName.substring(1);
       
   126         m_name = document->isHTMLDocument() ? mapName.lower() : mapName;
       
   127         if (inDocument())
       
   128             document->addImageMap(this);
       
   129         return;
       
   130     }
       
   131 
       
   132     HTMLElement::parseMappedAttribute(attribute);
       
   133 }
       
   134 
       
   135 PassRefPtr<HTMLCollection> HTMLMapElement::areas()
       
   136 {
       
   137     return HTMLCollection::create(this, MapAreas);
       
   138 }
       
   139 
       
   140 void HTMLMapElement::insertedIntoDocument()
       
   141 {
       
   142     document()->addImageMap(this);
       
   143     HTMLElement::insertedIntoDocument();
       
   144 }
       
   145 
       
   146 void HTMLMapElement::removedFromDocument()
       
   147 {
       
   148     document()->removeImageMap(this);
       
   149     HTMLElement::removedFromDocument();
       
   150 }
       
   151 
       
   152 }