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