7777#include " ScopedEventQueue.h"
7878#include " ScriptController.h"
7979#include " SelectorNodeList.h"
80+ #include " ShadowRoot.h"
8081#include " StaticNodeList.h"
8182#include " TagNodeList.h"
8283#include " Text.h"
@@ -468,39 +469,22 @@ void Node::setDocument(Document* document)
468469
469470TreeScope* Node::treeScope () const
470471{
472+ // FIXME: Using m_document directly is not good -> see comment with document() in the header file.
471473 if (!hasRareData ())
472- return document () ;
474+ return m_document ;
473475 TreeScope* scope = rareData ()->treeScope ();
474- // FIXME: Until we land shadow scopes, there should be no non-document scopes.
475- ASSERT (!scope);
476- return scope ? scope : document ();
476+ return scope ? scope : m_document;
477477}
478478
479- void Node::setTreeScope (TreeScope* newTreeScope)
479+ void Node::setTreeScopeRecursively (TreeScope* newTreeScope, bool includeRoot )
480480{
481- ASSERT (!isDocumentNode ());
481+ ASSERT (this );
482+ ASSERT (!includeRoot || !isDocumentNode ());
482483 ASSERT (newTreeScope);
483- ASSERT (!inDocument () || treeScope () == newTreeScope );
484+ ASSERT (!m_deletionHasBegun );
484485
485- if (newTreeScope->isDocumentNode ()) {
486- if (hasRareData ())
487- rareData ()->setTreeScope (0 );
488- // Setting the new document scope will be handled implicitly
489- // by setDocument() below.
490- } else {
491- // FIXME: Until we land shadow scopes, this branch should be inert.
492- ASSERT_NOT_REACHED ();
493- ensureRareData ()->setTreeScope (newTreeScope);
494- }
495-
496- setDocument (newTreeScope->document ());
497- }
498-
499- void Node::setTreeScopeRecursively (TreeScope* newTreeScope)
500- {
501- ASSERT (!isDocumentNode ());
502- ASSERT (newTreeScope);
503- if (treeScope () == newTreeScope)
486+ TreeScope* currentTreeScope = treeScope ();
487+ if (currentTreeScope == newTreeScope)
504488 return ;
505489
506490 Document* currentDocument = document ();
@@ -512,9 +496,25 @@ void Node::setTreeScopeRecursively(TreeScope* newTreeScope)
512496 if (currentDocument && currentDocument != newDocument)
513497 currentDocument->incDOMTreeVersion ();
514498
515- for (Node* node = this ; node; node = node->traverseNextNode (this )) {
516- node->setTreeScope (newTreeScope);
517- // FIXME: Once shadow scopes are landed, update parent scope, etc.
499+ for (Node* node = includeRoot ? this : traverseNextNode (this ); node; node = node->traverseNextNode (this )) {
500+ if (newTreeScope == newDocument) {
501+ if (node->hasRareData ())
502+ node->rareData ()->setTreeScope (0 );
503+ // Setting the new document tree scope will be handled implicitly
504+ // by setDocument() below.
505+ } else
506+ node->ensureRareData ()->setTreeScope (newTreeScope);
507+
508+ node->setDocument (newDocument);
509+
510+ if (!node->isElementNode ())
511+ continue ;
512+ // FIXME: Remove toShadowRoot() once shadowRoot() returns a proper ShadowRoot* (bug 58703).
513+ if (ShadowRoot* shadowRoot = toShadowRoot (toElement (node)->shadowRoot ())) {
514+ shadowRoot->setParentTreeScope (newTreeScope);
515+ if (currentDocument != newDocument)
516+ shadowRoot->setDocumentRecursively (newDocument);
517+ }
518518 }
519519}
520520
0 commit comments