1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2001 Dirk Mueller (mueller@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 "HTMLBaseElement.h"
25
26#include "Attribute.h"
27#include "Document.h"
28#include "HTMLNames.h"
29#include "HTMLParserIdioms.h"
30#include "TextResourceDecoder.h"
31
32namespace WebCore {
33
34using namespace HTMLNames;
35
36inline HTMLBaseElement::HTMLBaseElement(const QualifiedName& tagName, Document& document)
37    : HTMLElement(tagName, document)
38{
39    ASSERT(hasTagName(baseTag));
40}
41
42PassRefPtr<HTMLBaseElement> HTMLBaseElement::create(const QualifiedName& tagName, Document& document)
43{
44    return adoptRef(new HTMLBaseElement(tagName, document));
45}
46
47void HTMLBaseElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
48{
49    if (name == hrefAttr || name == targetAttr)
50        document().processBaseElement();
51    else
52        HTMLElement::parseAttribute(name, value);
53}
54
55Node::InsertionNotificationRequest HTMLBaseElement::insertedInto(ContainerNode& insertionPoint)
56{
57    HTMLElement::insertedInto(insertionPoint);
58    if (insertionPoint.inDocument())
59        document().processBaseElement();
60    return InsertionDone;
61}
62
63void HTMLBaseElement::removedFrom(ContainerNode& insertionPoint)
64{
65    HTMLElement::removedFrom(insertionPoint);
66    if (insertionPoint.inDocument())
67        document().processBaseElement();
68}
69
70bool HTMLBaseElement::isURLAttribute(const Attribute& attribute) const
71{
72    return attribute.name().localName() == hrefAttr || HTMLElement::isURLAttribute(attribute);
73}
74
75String HTMLBaseElement::target() const
76{
77    return fastGetAttribute(targetAttr);
78}
79
80URL HTMLBaseElement::href() const
81{
82    // This does not use the getURLAttribute function because that will resolve relative to the document's base URL;
83    // base elements like this one can be used to set that base URL. Thus we need to resolve relative to the document's
84    // URL and ignore the base URL.
85
86    const AtomicString& attributeValue = fastGetAttribute(hrefAttr);
87    if (attributeValue.isNull())
88        return document().url();
89
90    URL url = !document().decoder() ?
91        URL(document().url(), stripLeadingAndTrailingHTMLSpaces(attributeValue)) :
92        URL(document().url(), stripLeadingAndTrailingHTMLSpaces(attributeValue), document().decoder()->encoding());
93
94    if (!url.isValid())
95        return URL();
96
97    return url;
98}
99
100void HTMLBaseElement::setHref(const AtomicString& value)
101{
102    setAttribute(hrefAttr, value);
103}
104
105}
106