Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      4  *           (C) 2001 Dirk Mueller (mueller (at) kde.org)
      5  * Copyright (C) 2003, 2008, 2009, 2010 Apple Inc. All rights reserved.
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Library General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Library General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Library General Public License
     18  * along with this library; see the file COPYING.LIB.  If not, write to
     19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     20  * Boston, MA 02110-1301, USA.
     21  */
     22 
     23 #include "config.h"
     24 #include "core/html/HTMLBaseElement.h"
     25 
     26 #include "core/HTMLNames.h"
     27 #include "core/dom/Attribute.h"
     28 #include "core/dom/Document.h"
     29 #include "core/html/parser/HTMLParserIdioms.h"
     30 #include "core/html/parser/TextResourceDecoder.h"
     31 
     32 namespace WebCore {
     33 
     34 using namespace HTMLNames;
     35 
     36 inline HTMLBaseElement::HTMLBaseElement(Document& document)
     37     : HTMLElement(baseTag, document)
     38 {
     39     ScriptWrappable::init(this);
     40 }
     41 
     42 DEFINE_NODE_FACTORY(HTMLBaseElement)
     43 
     44 void HTMLBaseElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
     45 {
     46     if (name == hrefAttr || name == targetAttr)
     47         document().processBaseElement();
     48     else
     49         HTMLElement::parseAttribute(name, value);
     50 }
     51 
     52 Node::InsertionNotificationRequest HTMLBaseElement::insertedInto(ContainerNode* insertionPoint)
     53 {
     54     HTMLElement::insertedInto(insertionPoint);
     55     if (insertionPoint->inDocument())
     56         document().processBaseElement();
     57     return InsertionDone;
     58 }
     59 
     60 void HTMLBaseElement::removedFrom(ContainerNode* insertionPoint)
     61 {
     62     HTMLElement::removedFrom(insertionPoint);
     63     if (insertionPoint->inDocument())
     64         document().processBaseElement();
     65 }
     66 
     67 bool HTMLBaseElement::isURLAttribute(const Attribute& attribute) const
     68 {
     69     return attribute.name().localName() == hrefAttr || HTMLElement::isURLAttribute(attribute);
     70 }
     71 
     72 KURL HTMLBaseElement::href() const
     73 {
     74     // This does not use the getURLAttribute function because that will resolve relative to the document's base URL;
     75     // base elements like this one can be used to set that base URL. Thus we need to resolve relative to the document's
     76     // URL and ignore the base URL.
     77 
     78     const AtomicString& attributeValue = fastGetAttribute(hrefAttr);
     79     if (attributeValue.isNull())
     80         return document().url();
     81 
     82     KURL url = document().encoding().isValid() ?
     83         KURL(document().url(), stripLeadingAndTrailingHTMLSpaces(attributeValue)) :
     84         KURL(document().url(), stripLeadingAndTrailingHTMLSpaces(attributeValue), document().encoding());
     85 
     86     if (!url.isValid())
     87         return KURL();
     88 
     89     return url;
     90 }
     91 
     92 void HTMLBaseElement::setHref(const AtomicString& value)
     93 {
     94     setAttribute(hrefAttr, value);
     95 }
     96 
     97 }
     98