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