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 "core/html/HTMLTitleElement.h"
25
26#include "bindings/core/v8/ExceptionStatePlaceholder.h"
27#include "core/HTMLNames.h"
28#include "core/dom/ChildListMutationScope.h"
29#include "core/dom/Document.h"
30#include "core/dom/Text.h"
31#include "core/rendering/style/RenderStyle.h"
32#include "core/rendering/style/StyleInheritedData.h"
33#include "wtf/text/StringBuilder.h"
34
35namespace blink {
36
37using namespace HTMLNames;
38
39inline HTMLTitleElement::HTMLTitleElement(Document& document)
40    : HTMLElement(titleTag, document)
41    , m_ignoreTitleUpdatesWhenChildrenChange(false)
42{
43}
44
45DEFINE_NODE_FACTORY(HTMLTitleElement)
46
47Node::InsertionNotificationRequest HTMLTitleElement::insertedInto(ContainerNode* insertionPoint)
48{
49    HTMLElement::insertedInto(insertionPoint);
50    if (inDocument() && !isInShadowTree())
51        document().setTitleElement(this);
52    return InsertionDone;
53}
54
55void HTMLTitleElement::removedFrom(ContainerNode* insertionPoint)
56{
57    HTMLElement::removedFrom(insertionPoint);
58    if (insertionPoint->inDocument() && !insertionPoint->isInShadowTree())
59        document().removeTitle(this);
60}
61
62void HTMLTitleElement::childrenChanged(const ChildrenChange& change)
63{
64    HTMLElement::childrenChanged(change);
65    if (inDocument() && !isInShadowTree() && !m_ignoreTitleUpdatesWhenChildrenChange)
66        document().setTitleElement(this);
67}
68
69String HTMLTitleElement::text() const
70{
71    StringBuilder result;
72
73    for (Node *n = firstChild(); n; n = n->nextSibling()) {
74        if (n->isTextNode())
75            result.append(toText(n)->data());
76    }
77
78    return result.toString();
79}
80
81void HTMLTitleElement::setText(const String &value)
82{
83    RefPtrWillBeRawPtr<Node> protectFromMutationEvents(this);
84    ChildListMutationScope mutation(*this);
85
86    // Avoid calling Document::setTitleElement() during intermediate steps.
87    m_ignoreTitleUpdatesWhenChildrenChange = !value.isEmpty();
88    removeChildren();
89    m_ignoreTitleUpdatesWhenChildrenChange = false;
90
91    if (!value.isEmpty())
92        appendChild(document().createTextNode(value.impl()), IGNORE_EXCEPTION);
93}
94
95}
96