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, 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 "HTMLTitleElement.h"
25
26#include "Document.h"
27#include "HTMLNames.h"
28#include "RenderStyle.h"
29#include "Text.h"
30
31namespace WebCore {
32
33using namespace HTMLNames;
34
35inline HTMLTitleElement::HTMLTitleElement(const QualifiedName& tagName, Document* document)
36    : HTMLElement(tagName, document)
37{
38    ASSERT(hasTagName(titleTag));
39}
40
41PassRefPtr<HTMLTitleElement> HTMLTitleElement::create(const QualifiedName& tagName, Document* document)
42{
43    return adoptRef(new HTMLTitleElement(tagName, document));
44}
45
46void HTMLTitleElement::insertedIntoDocument()
47{
48    HTMLElement::insertedIntoDocument();
49    document()->setTitleElement(m_title, this);
50}
51
52void HTMLTitleElement::removedFromDocument()
53{
54    HTMLElement::removedFromDocument();
55    document()->removeTitle(this);
56}
57
58void HTMLTitleElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
59{
60    m_title = textWithDirection();
61    if (inDocument())
62        document()->setTitleElement(m_title, this);
63    HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
64}
65
66String HTMLTitleElement::text() const
67{
68    String val = "";
69
70    for (Node *n = firstChild(); n; n = n->nextSibling()) {
71        if (n->isTextNode())
72            val += static_cast<Text*>(n)->data();
73    }
74
75    return val;
76}
77
78StringWithDirection HTMLTitleElement::textWithDirection()
79{
80    TextDirection direction = LTR;
81    if (RenderStyle* style = computedStyle())
82        direction = style->direction();
83    else if (RefPtr<RenderStyle> style = styleForRenderer())
84        direction = style->direction();
85    return StringWithDirection(text(), direction);
86}
87
88void HTMLTitleElement::setText(const String &value)
89{
90    ExceptionCode ec = 0;
91    int numChildren = childNodeCount();
92
93    if (numChildren == 1 && firstChild()->isTextNode())
94        static_cast<Text*>(firstChild())->setData(value, ec);
95    else {
96        // We make a copy here because entity of "value" argument can be Document::m_title,
97        // which goes empty during removeChildren() invocation below,
98        // which causes HTMLTitleElement::childrenChanged(), which ends up Document::setTitle().
99        String valueCopy(value);
100
101        if (numChildren > 0)
102            removeChildren();
103
104        appendChild(document()->createTextNode(valueCopy.impl()), ec);
105    }
106}
107
108}
109