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) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
7 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
8 * Copyright (C) 2011 Google Inc. All rights reserved.
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public License
21 * along with this library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25 #include "config.h"
26 #include "core/dom/TreeScopeAdopter.h"
27
28 #include "core/dom/Attr.h"
29 #include "core/dom/Document.h"
30 #include "core/dom/NodeRareData.h"
31 #include "core/dom/NodeTraversal.h"
32 #include "core/dom/shadow/ElementShadow.h"
33 #include "core/dom/shadow/ShadowRoot.h"
34
35 namespace WebCore {
36
moveTreeToNewScope(Node & root) const37 void TreeScopeAdopter::moveTreeToNewScope(Node& root) const
38 {
39 ASSERT(needsScopeChange());
40
41 m_oldScope.guardRef();
42
43 // If an element is moved from a document and then eventually back again the collection cache for
44 // that element may contain stale data as changes made to it will have updated the DOMTreeVersion
45 // of the document it was moved to. By increasing the DOMTreeVersion of the donating document here
46 // we ensure that the collection cache will be invalidated as needed when the element is moved back.
47 Document* oldDocument = m_oldScope.documentScope();
48 ASSERT(oldDocument);
49 Document* newDocument = m_newScope.documentScope();
50 bool willMoveToNewDocument = oldDocument != newDocument;
51 if (willMoveToNewDocument)
52 oldDocument->incDOMTreeVersion();
53
54 for (Node* node = &root; node; node = NodeTraversal::next(*node, &root)) {
55 updateTreeScope(*node);
56
57 if (willMoveToNewDocument)
58 moveNodeToNewDocument(*node, *oldDocument, newDocument);
59 else if (node->hasRareData()) {
60 NodeRareData* rareData = node->rareData();
61 if (rareData->nodeLists())
62 rareData->nodeLists()->adoptTreeScope();
63 }
64
65 if (!node->isElementNode())
66 continue;
67
68 if (node->hasSyntheticAttrChildNodes()) {
69 const Vector<RefPtr<Attr> >& attrs = toElement(node)->attrNodeList();
70 for (unsigned i = 0; i < attrs.size(); ++i)
71 moveTreeToNewScope(*attrs[i]);
72 }
73
74 for (ShadowRoot* shadow = node->youngestShadowRoot(); shadow; shadow = shadow->olderShadowRoot()) {
75 shadow->setParentTreeScope(&m_newScope);
76 if (willMoveToNewDocument)
77 moveTreeToNewDocument(*shadow, *oldDocument, newDocument);
78 }
79 }
80
81 m_oldScope.guardDeref();
82 }
83
moveTreeToNewDocument(Node & root,Document & oldDocument,Document * newDocument) const84 void TreeScopeAdopter::moveTreeToNewDocument(Node& root, Document& oldDocument, Document* newDocument) const
85 {
86 for (Node* node = &root; node; node = NodeTraversal::next(*node, &root)) {
87 moveNodeToNewDocument(*node, oldDocument, newDocument);
88 for (ShadowRoot* shadow = node->youngestShadowRoot(); shadow; shadow = shadow->olderShadowRoot())
89 moveTreeToNewDocument(*shadow, oldDocument, newDocument);
90 }
91 }
92
93 #ifndef NDEBUG
94 static bool didMoveToNewDocumentWasCalled = false;
95 static Document* oldDocumentDidMoveToNewDocumentWasCalledWith = 0;
96
ensureDidMoveToNewDocumentWasCalled(Document & oldDocument)97 void TreeScopeAdopter::ensureDidMoveToNewDocumentWasCalled(Document& oldDocument)
98 {
99 ASSERT(!didMoveToNewDocumentWasCalled);
100 ASSERT_UNUSED(oldDocument, oldDocument == oldDocumentDidMoveToNewDocumentWasCalledWith);
101 didMoveToNewDocumentWasCalled = true;
102 }
103 #endif
104
updateTreeScope(Node & node) const105 inline void TreeScopeAdopter::updateTreeScope(Node& node) const
106 {
107 ASSERT(!node.isTreeScope());
108 ASSERT(node.treeScope() == m_oldScope);
109 m_newScope.guardRef();
110 m_oldScope.guardDeref();
111 node.setTreeScope(&m_newScope);
112 }
113
moveNodeToNewDocument(Node & node,Document & oldDocument,Document * newDocument) const114 inline void TreeScopeAdopter::moveNodeToNewDocument(Node& node, Document& oldDocument, Document* newDocument) const
115 {
116 ASSERT(!node.inDocument() || oldDocument != newDocument);
117
118 if (node.hasRareData()) {
119 NodeRareData* rareData = node.rareData();
120 if (rareData->nodeLists())
121 rareData->nodeLists()->adoptDocument(&oldDocument, newDocument);
122 }
123
124 oldDocument.moveNodeIteratorsToNewDocument(&node, newDocument);
125
126 if (node.isShadowRoot())
127 toShadowRoot(node).setDocumentScope(newDocument);
128
129 #ifndef NDEBUG
130 didMoveToNewDocumentWasCalled = false;
131 oldDocumentDidMoveToNewDocumentWasCalledWith = &oldDocument;
132 #endif
133
134 node.didMoveToNewDocument(oldDocument);
135 ASSERT(didMoveToNewDocumentWasCalled);
136 }
137
138 }
139