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 "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 38 inline HTMLTitleElement::HTMLTitleElement(const QualifiedName& tagName, Document* document) 39 : HTMLElement(tagName, document) 40 { 41 ASSERT(hasTagName(titleTag)); 42 setHasCustomStyleCallbacks(); 43 ScriptWrappable::init(this); 44 } 45 46 PassRefPtr<HTMLTitleElement> HTMLTitleElement::create(const QualifiedName& tagName, Document* document) 47 { 48 return adoptRef(new HTMLTitleElement(tagName, document)); 49 } 50 51 Node::InsertionNotificationRequest HTMLTitleElement::insertedInto(ContainerNode* insertionPoint) 52 { 53 HTMLElement::insertedInto(insertionPoint); 54 if (inDocument() && !isInShadowTree()) 55 document()->setTitleElement(textWithDirection(), this); 56 return InsertionDone; 57 } 58 59 void HTMLTitleElement::removedFrom(ContainerNode* insertionPoint) 60 { 61 HTMLElement::removedFrom(insertionPoint); 62 if (insertionPoint->inDocument() && !insertionPoint->isInShadowTree()) 63 document()->removeTitle(this); 64 } 65 66 void HTMLTitleElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) 67 { 68 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); 69 if (inDocument() && !isInShadowTree()) 70 document()->setTitleElement(textWithDirection(), this); 71 } 72 73 void HTMLTitleElement::attach(const AttachContext& context) 74 { 75 HTMLElement::attach(context); 76 // If after attaching nothing called styleForRenderer() on this node we 77 // manually cache the value. This happens if our parent doesn't have a 78 // renderer like <optgroup> or if it doesn't allow children like <select>. 79 if (!m_style) 80 updateNonRenderStyle(); 81 } 82 83 void HTMLTitleElement::detach(const AttachContext& context) 84 { 85 m_style.clear(); 86 HTMLElement::detach(context); 87 } 88 89 void HTMLTitleElement::updateNonRenderStyle() 90 { 91 m_style = document()->styleForElementIgnoringPendingStylesheets(this); 92 } 93 94 RenderStyle* HTMLTitleElement::nonRendererStyle() const 95 { 96 return m_style.get(); 97 } 98 99 PassRefPtr<RenderStyle> HTMLTitleElement::customStyleForRenderer() 100 { 101 // styleForRenderer is called whenever a new style should be associated 102 // with an Element so now is a good time to update our cached style. 103 updateNonRenderStyle(); 104 return m_style; 105 } 106 107 void HTMLTitleElement::didRecalcStyle(StyleChange) 108 { 109 if (isInShadowTree()) 110 return; 111 112 document()->setTitleElement(textWithDirection(), this); 113 } 114 115 String HTMLTitleElement::text() const 116 { 117 StringBuilder result; 118 119 for (Node *n = firstChild(); n; n = n->nextSibling()) { 120 if (n->isTextNode()) 121 result.append(toText(n)->data()); 122 } 123 124 return result.toString(); 125 } 126 127 StringWithDirection HTMLTitleElement::textWithDirection() 128 { 129 TextDirection direction = LTR; 130 if (m_style) 131 direction = m_style->direction(); 132 return StringWithDirection(text(), direction); 133 } 134 135 void HTMLTitleElement::setText(const String &value) 136 { 137 RefPtr<Node> protectFromMutationEvents(this); 138 139 int numChildren = childNodeCount(); 140 141 if (numChildren == 1 && firstChild()->isTextNode()) 142 toText(firstChild())->setData(value); 143 else { 144 // We make a copy here because entity of "value" argument can be Document::m_title, 145 // which goes empty during removeChildren() invocation below, 146 // which causes HTMLTitleElement::childrenChanged(), which ends up Document::setTitle(). 147 String valueCopy(value); 148 149 if (numChildren > 0) 150 removeChildren(); 151 152 appendChild(document()->createTextNode(valueCopy.impl()), IGNORE_EXCEPTION); 153 } 154 } 155 156 } 157