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.
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.
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.
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.
24 #include "HTMLTitleElement.h"
27 #include "HTMLNames.h"
28 #include "NodeRenderingContext.h"
29 #include "RenderStyle.h"
34 using namespace HTMLNames;
36 inline HTMLTitleElement::HTMLTitleElement(const QualifiedName& tagName, Document* document)
37 : HTMLElement(tagName, document)
39 ASSERT(hasTagName(titleTag));
42 PassRefPtr<HTMLTitleElement> HTMLTitleElement::create(const QualifiedName& tagName, Document* document)
44 return adoptRef(new HTMLTitleElement(tagName, document));
47 void HTMLTitleElement::insertedIntoDocument()
49 HTMLElement::insertedIntoDocument();
50 document()->setTitleElement(m_title, this);
53 void HTMLTitleElement::removedFromDocument()
55 HTMLElement::removedFromDocument();
56 document()->removeTitle(this);
59 void HTMLTitleElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
61 m_title = textWithDirection();
63 document()->setTitleElement(m_title, this);
64 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
67 String HTMLTitleElement::text() const
71 for (Node *n = firstChild(); n; n = n->nextSibling()) {
73 val += static_cast<Text*>(n)->data();
79 StringWithDirection HTMLTitleElement::textWithDirection()
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);
89 void HTMLTitleElement::setText(const String &value)
92 int numChildren = childNodeCount();
94 if (numChildren == 1 && firstChild()->isTextNode())
95 static_cast<Text*>(firstChild())->setData(value, ec);
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);
105 appendChild(document()->createTextNode(valueCopy.impl()), ec);