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