diff options
Diffstat (limited to 'Source/WebCore/dom/TreeScopeAdopter.cpp')
-rw-r--r-- | Source/WebCore/dom/TreeScopeAdopter.cpp | 91 |
1 files changed, 46 insertions, 45 deletions
diff --git a/Source/WebCore/dom/TreeScopeAdopter.cpp b/Source/WebCore/dom/TreeScopeAdopter.cpp index 3f44fd60e..751d2a813 100644 --- a/Source/WebCore/dom/TreeScopeAdopter.cpp +++ b/Source/WebCore/dom/TreeScopeAdopter.cpp @@ -32,104 +32,105 @@ namespace WebCore { -void TreeScopeAdopter::moveTreeToNewScope(Node* root) const +// FIXME: Do we ever change tree scopes except between documents? +void TreeScopeAdopter::moveTreeToNewScope(Node& root) const { ASSERT(needsScopeChange()); - m_oldScope.selfOnlyRef(); - // If an element is moved from a document and then eventually back again the collection cache for // that element may contain stale data as changes made to it will have updated the DOMTreeVersion // of the document it was moved to. By increasing the DOMTreeVersion of the donating document here // we ensure that the collection cache will be invalidated as needed when the element is moved back. - Document* oldDocument = m_oldScope.documentScope(); - Document* newDocument = m_newScope.documentScope(); - bool willMoveToNewDocument = oldDocument != newDocument; - if (oldDocument && willMoveToNewDocument) - oldDocument->incDOMTreeVersion(); + Document& oldDocument = m_oldScope.documentScope(); + Document& newDocument = m_newScope.documentScope(); + bool willMoveToNewDocument = &oldDocument != &newDocument; + if (willMoveToNewDocument) { + oldDocument.incrementReferencingNodeCount(); + oldDocument.incDOMTreeVersion(); + } - for (Node* node = root; node; node = NodeTraversal::next(node, root)) { - updateTreeScope(node); + for (Node* node = &root; node; node = NodeTraversal::next(*node, &root)) { + updateTreeScope(*node); if (willMoveToNewDocument) - moveNodeToNewDocument(node, oldDocument, newDocument); + moveNodeToNewDocument(*node, oldDocument, newDocument); else if (node->hasRareData()) { NodeRareData* rareData = node->rareData(); if (rareData->nodeLists()) rareData->nodeLists()->adoptTreeScope(); } - if (!node->isElementNode()) + if (!is<Element>(*node)) continue; if (node->hasSyntheticAttrChildNodes()) { - const Vector<RefPtr<Attr>>& attrs = toElement(node)->attrNodeList(); - for (unsigned i = 0; i < attrs.size(); ++i) - moveTreeToNewScope(attrs[i].get()); + for (auto& attr : downcast<Element>(*node).attrNodeList()) + moveTreeToNewScope(*attr); } - if (ShadowRoot* shadow = node->shadowRoot()) { - shadow->setParentTreeScope(&m_newScope); + if (auto* shadow = node->shadowRoot()) { + shadow->setParentTreeScope(m_newScope); if (willMoveToNewDocument) - moveTreeToNewDocument(shadow, oldDocument, newDocument); + moveShadowTreeToNewDocument(*shadow, oldDocument, newDocument); } } - m_oldScope.selfOnlyDeref(); + if (willMoveToNewDocument) + oldDocument.decrementReferencingNodeCount(); } -void TreeScopeAdopter::moveTreeToNewDocument(Node* root, Document* oldDocument, Document* newDocument) const +void TreeScopeAdopter::moveShadowTreeToNewDocument(ShadowRoot& shadowRoot, Document& oldDocument, Document& newDocument) const { - for (Node* node = root; node; node = NodeTraversal::next(node, root)) { - moveNodeToNewDocument(node, oldDocument, newDocument); - if (ShadowRoot* shadow = node->shadowRoot()) - moveTreeToNewDocument(shadow, oldDocument, newDocument); + for (Node* node = &shadowRoot; node; node = NodeTraversal::next(*node, &shadowRoot)) { + moveNodeToNewDocument(*node, oldDocument, newDocument); + if (auto* shadow = node->shadowRoot()) + moveShadowTreeToNewDocument(*shadow, oldDocument, newDocument); } } #ifndef NDEBUG static bool didMoveToNewDocumentWasCalled = false; -static Document* oldDocumentDidMoveToNewDocumentWasCalledWith = 0; +static Document* oldDocumentDidMoveToNewDocumentWasCalledWith = nullptr; -void TreeScopeAdopter::ensureDidMoveToNewDocumentWasCalled(Document* oldDocument) +void TreeScopeAdopter::ensureDidMoveToNewDocumentWasCalled(Document& oldDocument) { ASSERT(!didMoveToNewDocumentWasCalled); - ASSERT_UNUSED(oldDocument, oldDocument == oldDocumentDidMoveToNewDocumentWasCalledWith); + ASSERT_UNUSED(oldDocument, &oldDocument == oldDocumentDidMoveToNewDocumentWasCalledWith); didMoveToNewDocumentWasCalled = true; } #endif -inline void TreeScopeAdopter::updateTreeScope(Node* node) const +inline void TreeScopeAdopter::updateTreeScope(Node& node) const { - ASSERT(!node->isTreeScope()); - ASSERT(&node->treeScope() == &m_oldScope); - m_newScope.selfOnlyRef(); - m_oldScope.selfOnlyDeref(); - node->setTreeScope(m_newScope); + ASSERT(!node.isTreeScope()); + ASSERT(&node.treeScope() == &m_oldScope); + node.setTreeScope(m_newScope); } -inline void TreeScopeAdopter::moveNodeToNewDocument(Node* node, Document* oldDocument, Document* newDocument) const +inline void TreeScopeAdopter::moveNodeToNewDocument(Node& node, Document& oldDocument, Document& newDocument) const { - ASSERT(!node->inDocument() || oldDocument != newDocument); + ASSERT(!node.isConnected() || &oldDocument != &newDocument); + + newDocument.incrementReferencingNodeCount(); + oldDocument.decrementReferencingNodeCount(); - if (node->hasRareData()) { - NodeRareData* rareData = node->rareData(); - if (rareData->nodeLists()) - rareData->nodeLists()->adoptDocument(oldDocument, newDocument); + if (node.hasRareData()) { + NodeRareData* rareData = node.rareData(); + if (auto* nodeLists = rareData->nodeLists()) + nodeLists->adoptDocument(oldDocument, newDocument); } - if (oldDocument) - oldDocument->moveNodeIteratorsToNewDocument(node, newDocument); + oldDocument.moveNodeIteratorsToNewDocument(node, newDocument); - if (node->isShadowRoot()) - toShadowRoot(node)->setDocumentScope(newDocument); + if (is<ShadowRoot>(node)) + downcast<ShadowRoot>(node).setDocumentScope(newDocument); #ifndef NDEBUG didMoveToNewDocumentWasCalled = false; - oldDocumentDidMoveToNewDocumentWasCalledWith = oldDocument; + oldDocumentDidMoveToNewDocumentWasCalledWith = &oldDocument; #endif - node->didMoveToNewDocument(oldDocument); + node.didMoveToNewDocument(oldDocument); ASSERT(didMoveToNewDocumentWasCalled); } |