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" |
28 #include "core/dom/Document.h" | 29 #include "core/dom/Document.h" |
29 #include "core/dom/Text.h" | 30 #include "core/dom/Text.h" |
30 #include "core/rendering/style/RenderStyle.h" | 31 #include "core/rendering/style/RenderStyle.h" |
31 #include "core/rendering/style/StyleInheritedData.h" | 32 #include "core/rendering/style/StyleInheritedData.h" |
32 #include "wtf/text/StringBuilder.h" | 33 #include "wtf/text/StringBuilder.h" |
33 | 34 |
34 namespace WebCore { | 35 namespace WebCore { |
35 | 36 |
36 using namespace HTMLNames; | 37 using namespace HTMLNames; |
37 | 38 |
38 inline HTMLTitleElement::HTMLTitleElement(Document& document) | 39 inline HTMLTitleElement::HTMLTitleElement(Document& document) |
39 : HTMLElement(titleTag, document) | 40 : HTMLElement(titleTag, document) |
| 41 , m_ignoreTitleUpdatesWhenChildrenChange(false) |
40 { | 42 { |
41 setHasCustomStyleCallbacks(); | 43 setHasCustomStyleCallbacks(); |
42 ScriptWrappable::init(this); | 44 ScriptWrappable::init(this); |
43 } | 45 } |
44 | 46 |
45 PassRefPtr<HTMLTitleElement> HTMLTitleElement::create(Document& document) | 47 PassRefPtr<HTMLTitleElement> HTMLTitleElement::create(Document& document) |
46 { | 48 { |
47 return adoptRef(new HTMLTitleElement(document)); | 49 return adoptRef(new HTMLTitleElement(document)); |
48 } | 50 } |
49 | 51 |
50 Node::InsertionNotificationRequest HTMLTitleElement::insertedInto(ContainerNode*
insertionPoint) | 52 Node::InsertionNotificationRequest HTMLTitleElement::insertedInto(ContainerNode*
insertionPoint) |
51 { | 53 { |
52 HTMLElement::insertedInto(insertionPoint); | 54 HTMLElement::insertedInto(insertionPoint); |
53 if (inDocument() && !isInShadowTree()) | 55 if (inDocument() && !isInShadowTree()) |
54 document().setTitleElement(text(), this); | 56 document().setTitleElement(text(), this); |
55 return InsertionDone; | 57 return InsertionDone; |
56 } | 58 } |
57 | 59 |
58 void HTMLTitleElement::removedFrom(ContainerNode* insertionPoint) | 60 void HTMLTitleElement::removedFrom(ContainerNode* insertionPoint) |
59 { | 61 { |
60 HTMLElement::removedFrom(insertionPoint); | 62 HTMLElement::removedFrom(insertionPoint); |
61 if (insertionPoint->inDocument() && !insertionPoint->isInShadowTree()) | 63 if (insertionPoint->inDocument() && !insertionPoint->isInShadowTree()) |
62 document().removeTitle(this); | 64 document().removeTitle(this); |
63 } | 65 } |
64 | 66 |
65 void HTMLTitleElement::childrenChanged(bool changedByParser, Node* beforeChange,
Node* afterChange, int childCountDelta) | 67 void HTMLTitleElement::childrenChanged(bool changedByParser, Node* beforeChange,
Node* afterChange, int childCountDelta) |
66 { | 68 { |
67 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, chi
ldCountDelta); | 69 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, chi
ldCountDelta); |
68 if (inDocument() && !isInShadowTree()) | 70 if (inDocument() && !isInShadowTree() && !m_ignoreTitleUpdatesWhenChildrenCh
ange) |
69 document().setTitleElement(text(), this); | 71 document().setTitleElement(text(), this); |
70 } | 72 } |
71 | 73 |
72 String HTMLTitleElement::text() const | 74 String HTMLTitleElement::text() const |
73 { | 75 { |
74 StringBuilder result; | 76 StringBuilder result; |
75 | 77 |
76 for (Node *n = firstChild(); n; n = n->nextSibling()) { | 78 for (Node *n = firstChild(); n; n = n->nextSibling()) { |
77 if (n->isTextNode()) | 79 if (n->isTextNode()) |
78 result.append(toText(n)->data()); | 80 result.append(toText(n)->data()); |
79 } | 81 } |
80 | 82 |
81 return result.toString(); | 83 return result.toString(); |
82 } | 84 } |
83 | 85 |
84 void HTMLTitleElement::setText(const String &value) | 86 void HTMLTitleElement::setText(const String &value) |
85 { | 87 { |
86 RefPtr<Node> protectFromMutationEvents(this); | 88 RefPtr<Node> protectFromMutationEvents(this); |
| 89 ChildListMutationScope mutation(*this); |
87 | 90 |
88 int numChildren = childNodeCount(); | 91 // Avoid calling Document::setTitleElement() during intermediate steps. |
| 92 m_ignoreTitleUpdatesWhenChildrenChange = true; |
| 93 removeChildren(); |
| 94 m_ignoreTitleUpdatesWhenChildrenChange = false; |
89 | 95 |
90 if (numChildren == 1 && firstChild()->isTextNode()) | 96 appendChild(document().createTextNode(value.impl()), IGNORE_EXCEPTION); |
91 toText(firstChild())->setData(value); | |
92 else { | |
93 // We make a copy here because entity of "value" argument can be Documen
t::m_title, | |
94 // which goes empty during removeChildren() invocation below, | |
95 // which causes HTMLTitleElement::childrenChanged(), which ends up in Do
cument::setTitleElement(). | |
96 String valueCopy(value); | |
97 | |
98 if (numChildren > 0) | |
99 removeChildren(); | |
100 | |
101 appendChild(document().createTextNode(valueCopy.impl()), IGNORE_EXCEPTIO
N); | |
102 } | |
103 } | 97 } |
104 | 98 |
105 } | 99 } |
OLD | NEW |