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