Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      4  *           (C) 2001 Dirk Mueller (mueller (at) 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/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 
     35 namespace WebCore {
     36 
     37 using namespace HTMLNames;
     38 
     39 inline HTMLTitleElement::HTMLTitleElement(Document& document)
     40     : HTMLElement(titleTag, document)
     41     , m_ignoreTitleUpdatesWhenChildrenChange(false)
     42 {
     43     setHasCustomStyleCallbacks();
     44     ScriptWrappable::init(this);
     45 }
     46 
     47 DEFINE_NODE_FACTORY(HTMLTitleElement)
     48 
     49 Node::InsertionNotificationRequest HTMLTitleElement::insertedInto(ContainerNode* insertionPoint)
     50 {
     51     HTMLElement::insertedInto(insertionPoint);
     52     if (inDocument() && !isInShadowTree())
     53         document().setTitleElement(text(), this);
     54     return InsertionDone;
     55 }
     56 
     57 void HTMLTitleElement::removedFrom(ContainerNode* insertionPoint)
     58 {
     59     HTMLElement::removedFrom(insertionPoint);
     60     if (insertionPoint->inDocument() && !insertionPoint->isInShadowTree())
     61         document().removeTitle(this);
     62 }
     63 
     64 void HTMLTitleElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
     65 {
     66     HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
     67     if (inDocument() && !isInShadowTree() && !m_ignoreTitleUpdatesWhenChildrenChange)
     68         document().setTitleElement(text(), this);
     69 }
     70 
     71 String HTMLTitleElement::text() const
     72 {
     73     StringBuilder result;
     74 
     75     for (Node *n = firstChild(); n; n = n->nextSibling()) {
     76         if (n->isTextNode())
     77             result.append(toText(n)->data());
     78     }
     79 
     80     return result.toString();
     81 }
     82 
     83 void HTMLTitleElement::setText(const String &value)
     84 {
     85     RefPtrWillBeRawPtr<Node> protectFromMutationEvents(this);
     86     ChildListMutationScope mutation(*this);
     87 
     88     // Avoid calling Document::setTitleElement() during intermediate steps.
     89     m_ignoreTitleUpdatesWhenChildrenChange = !value.isEmpty();
     90     removeChildren();
     91     m_ignoreTitleUpdatesWhenChildrenChange = false;
     92 
     93     if (!value.isEmpty())
     94         appendChild(document().createTextNode(value.impl()), IGNORE_EXCEPTION);
     95 }
     96 
     97 }
     98