OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) | 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
4 * (C) 2001 Dirk Mueller (mueller@kde.org) | 4 * (C) 2001 Dirk Mueller (mueller@kde.org) |
5 * Copyright (C) 2003, 2010 Apple Inc. All rights reserved. | 5 * Copyright (C) 2003, 2010 Apple Inc. All rights reserved. |
6 * | 6 * |
7 * This library is free software; you can redistribute it and/or | 7 * This library is free software; you can redistribute it and/or |
8 * modify it under the terms of the GNU Library General Public | 8 * modify it under the terms of the GNU Library General Public |
9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
10 * version 2 of the License, or (at your option) any later version. | 10 * version 2 of the License, or (at your option) any later version. |
11 * | 11 * |
12 * This library is distributed in the hope that it will be useful, | 12 * This library is distributed in the hope that it will be useful, |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Library General Public License for more details. | 15 * Library General Public License for more details. |
16 * | 16 * |
17 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 * Boston, MA 02110-1301, USA. | 20 * Boston, MA 02110-1301, USA. |
21 */ | 21 */ |
22 | 22 |
23 #include "config.h" | 23 #include "config.h" |
24 #include "core/html/HTMLTitleElement.h" | 24 #include "core/html/HTMLTitleElement.h" |
25 | 25 |
26 #include "HTMLNames.h" | 26 #include "HTMLNames.h" |
27 #include "bindings/v8/ExceptionStatePlaceholder.h" | 27 #include "bindings/v8/ExceptionStatePlaceholder.h" |
28 #include "core/dom/ChildListMutationScope.h" | |
29 #include "core/dom/Document.h" | 28 #include "core/dom/Document.h" |
30 #include "core/dom/Text.h" | 29 #include "core/dom/Text.h" |
31 #include "core/rendering/style/RenderStyle.h" | 30 #include "core/rendering/style/RenderStyle.h" |
32 #include "core/rendering/style/StyleInheritedData.h" | 31 #include "core/rendering/style/StyleInheritedData.h" |
33 #include "wtf/text/StringBuilder.h" | 32 #include "wtf/text/StringBuilder.h" |
34 | 33 |
35 namespace WebCore { | 34 namespace WebCore { |
36 | 35 |
37 using namespace HTMLNames; | 36 using namespace HTMLNames; |
38 | 37 |
39 inline HTMLTitleElement::HTMLTitleElement(Document& document) | 38 inline HTMLTitleElement::HTMLTitleElement(Document& document) |
40 : HTMLElement(titleTag, document) | 39 : HTMLElement(titleTag, document) |
41 , m_ignoreTitleUpdatesWhenChildrenChange(false) | |
42 { | 40 { |
43 setHasCustomStyleCallbacks(); | 41 setHasCustomStyleCallbacks(); |
44 ScriptWrappable::init(this); | 42 ScriptWrappable::init(this); |
45 } | 43 } |
46 | 44 |
47 PassRefPtr<HTMLTitleElement> HTMLTitleElement::create(Document& document) | 45 PassRefPtr<HTMLTitleElement> HTMLTitleElement::create(Document& document) |
48 { | 46 { |
49 return adoptRef(new HTMLTitleElement(document)); | 47 return adoptRef(new HTMLTitleElement(document)); |
50 } | 48 } |
51 | 49 |
52 Node::InsertionNotificationRequest HTMLTitleElement::insertedInto(ContainerNode*
insertionPoint) | 50 Node::InsertionNotificationRequest HTMLTitleElement::insertedInto(ContainerNode*
insertionPoint) |
53 { | 51 { |
54 HTMLElement::insertedInto(insertionPoint); | 52 HTMLElement::insertedInto(insertionPoint); |
55 if (inDocument() && !isInShadowTree()) | 53 if (inDocument() && !isInShadowTree()) |
56 document().setTitleElement(text(), this); | 54 document().setTitleElement(text(), this); |
57 return InsertionDone; | 55 return InsertionDone; |
58 } | 56 } |
59 | 57 |
60 void HTMLTitleElement::removedFrom(ContainerNode* insertionPoint) | 58 void HTMLTitleElement::removedFrom(ContainerNode* insertionPoint) |
61 { | 59 { |
62 HTMLElement::removedFrom(insertionPoint); | 60 HTMLElement::removedFrom(insertionPoint); |
63 if (insertionPoint->inDocument() && !insertionPoint->isInShadowTree()) | 61 if (insertionPoint->inDocument() && !insertionPoint->isInShadowTree()) |
64 document().removeTitle(this); | 62 document().removeTitle(this); |
65 } | 63 } |
66 | 64 |
67 void HTMLTitleElement::childrenChanged(bool changedByParser, Node* beforeChange,
Node* afterChange, int childCountDelta) | 65 void HTMLTitleElement::childrenChanged(bool changedByParser, Node* beforeChange,
Node* afterChange, int childCountDelta) |
68 { | 66 { |
69 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, chi
ldCountDelta); | 67 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, chi
ldCountDelta); |
70 if (inDocument() && !isInShadowTree() && !m_ignoreTitleUpdatesWhenChildrenCh
ange) | 68 if (inDocument() && !isInShadowTree()) |
71 document().setTitleElement(text(), this); | 69 document().setTitleElement(text(), this); |
72 } | 70 } |
73 | 71 |
74 String HTMLTitleElement::text() const | 72 String HTMLTitleElement::text() const |
75 { | 73 { |
76 StringBuilder result; | 74 StringBuilder result; |
77 | 75 |
78 for (Node *n = firstChild(); n; n = n->nextSibling()) { | 76 for (Node *n = firstChild(); n; n = n->nextSibling()) { |
79 if (n->isTextNode()) | 77 if (n->isTextNode()) |
80 result.append(toText(n)->data()); | 78 result.append(toText(n)->data()); |
81 } | 79 } |
82 | 80 |
83 return result.toString(); | 81 return result.toString(); |
84 } | 82 } |
85 | 83 |
86 void HTMLTitleElement::setText(const String &value) | 84 void HTMLTitleElement::setText(const String &value) |
87 { | 85 { |
88 RefPtr<Node> protectFromMutationEvents(this); | 86 RefPtr<Node> protectFromMutationEvents(this); |
89 ChildListMutationScope mutation(*this); | |
90 | 87 |
91 // Avoid calling Document::setTitleElement() during intermediate steps. | 88 int numChildren = childNodeCount(); |
92 m_ignoreTitleUpdatesWhenChildrenChange = true; | |
93 removeChildren(); | |
94 m_ignoreTitleUpdatesWhenChildrenChange = false; | |
95 | 89 |
96 appendChild(document().createTextNode(value.impl()), IGNORE_EXCEPTION); | 90 if (numChildren == 1 && firstChild()->isTextNode()) { |
| 91 toText(firstChild())->setData(value); |
| 92 document().setTitleElement(text(), this); |
| 93 } else { |
| 94 // We make a copy here because entity of "value" argument can be Documen
t::m_title, |
| 95 // which goes empty during removeChildren() invocation below, |
| 96 // which causes HTMLTitleElement::childrenChanged(), which ends up Docum
ent::setTitle(). |
| 97 String valueCopy(value); |
| 98 |
| 99 if (numChildren > 0) |
| 100 removeChildren(); |
| 101 |
| 102 appendChild(document().createTextNode(valueCopy.impl()), IGNORE_EXCEPTIO
N); |
| 103 } |
97 } | 104 } |
98 | 105 |
99 } | 106 } |
OLD | NEW |