Changeset 104528 in webkit


Ignore:
Timestamp:
Jan 9, 2012 6:11:30 PM (12 years ago)
Author:
morrita@google.com
Message:

[Refactoring] Moving between TreeScopes should be done by its own class.
https://bugs.webkit.org/show_bug.cgi?id=75290

Reviewed by Ryosuke Niwa.

This change extracted Node::setTreeScopeRecursively(),
setDocumentRecursively() and a part of setDocument() into a new
class called TreeScopeAdopter. By doing this, the idea of
moving a node from scope to scope, that was originally hidden
behind the forest of Node APIs, has become clearer.

Note that this change is a preparation for Bug 59816.

No new tests. No behavioral change.

  • CMakeLists.txt:
  • GNUmakefile.list.am:
  • Target.pri:
  • WebCore.gypi:
  • WebCore.vcproj/WebCore.vcproj:
  • WebCore.xcodeproj/project.pbxproj:
  • dom/ContainerNode.cpp: Followed the renaming.

(WebCore::ContainerNode::takeAllChildrenFrom):
(WebCore::ContainerNode::insertBefore):
(WebCore::ContainerNode::replaceChild):
(WebCore::ContainerNode::removeBetween):
(WebCore::ContainerNode::removeChildren):
(WebCore::ContainerNode::appendChild):
(WebCore::ContainerNode::parserAddChild):

  • dom/DOMAllInOne.cpp:
  • dom/Document.cpp: Followed te renaming.

(WebCore::Document::setDocType):
(WebCore::Document::adoptNode):

  • dom/Element.cpp: Followed te renaming.

(WebCore::Element::removeShadowRoot):

  • dom/Node.cpp:

(WebCore::Node::setDocument):
(WebCore::Node::setTreeScope):
(WebCore::Node::didMoveToNewDocument):

  • dom/Node.h:
  • dom/TreeScope.h:
  • dom/TreeScope.cpp:

(WebCore::TreeScope::adoptIfNeeded): moved from setTreeScopeRecursively()

  • dom/TreeScopeAdopter.cpp: Added.

(WebCore::TreeScopeAdopter::TreeScopeAdopter):
(WebCore::TreeScopeAdopter::moveTreeToNewScope):
(WebCore::TreeScopeAdopter::moveTreeToNewDocument):
(WebCore::TreeScopeAdopter::ensureDidMoveToNewDocumentWasCalled):
(WebCore::TreeScopeAdopter::moveNodeToNewDocument):

  • dom/TreeScopeAdopter.h: Added.

(WebCore::TreeScopeAdopter::ensureDidMoveToNewDocumentWasCalled):
(WebCore::TreeScopeAdopter::execute):
(WebCore::TreeScopeAdopter::needsScopeChange()):
(WebCore::TreeScopeAdopter::shadowRootFor):

Location:
trunk/Source/WebCore
Files:
2 added
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/CMakeLists.txt

    r104380 r104528  
    611611    dom/Traversal.cpp
    612612    dom/TreeScope.cpp
     613    dom/TreeScopeAdopter.cpp
    613614    dom/TreeWalker.cpp
    614615    dom/UIEvent.cpp
  • trunk/Source/WebCore/ChangeLog

    r104526 r104528  
     12012-01-06  Hajime Morrita  <morrita@chromium.org>
     2
     3        [Refactoring] Moving between TreeScopes should be done by its own class.
     4        https://bugs.webkit.org/show_bug.cgi?id=75290
     5
     6        Reviewed by Ryosuke Niwa.
     7
     8        This change extracted Node::setTreeScopeRecursively(),
     9        setDocumentRecursively() and a part of setDocument() into a new
     10        class called TreeScopeAdopter. By doing this, the idea of
     11        moving a node from scope to scope, that was originally hidden
     12        behind the forest of Node APIs, has become clearer.
     13
     14        Note that this change is a preparation for Bug 59816.
     15
     16        No new tests. No behavioral change.
     17
     18        * CMakeLists.txt:
     19        * GNUmakefile.list.am:
     20        * Target.pri:
     21        * WebCore.gypi:
     22        * WebCore.vcproj/WebCore.vcproj:
     23        * WebCore.xcodeproj/project.pbxproj:
     24        * dom/ContainerNode.cpp: Followed the renaming.
     25        (WebCore::ContainerNode::takeAllChildrenFrom):
     26        (WebCore::ContainerNode::insertBefore):
     27        (WebCore::ContainerNode::replaceChild):
     28        (WebCore::ContainerNode::removeBetween):
     29        (WebCore::ContainerNode::removeChildren):
     30        (WebCore::ContainerNode::appendChild):
     31        (WebCore::ContainerNode::parserAddChild):
     32        * dom/DOMAllInOne.cpp:
     33        * dom/Document.cpp: Followed te renaming.
     34        (WebCore::Document::setDocType):
     35        (WebCore::Document::adoptNode):
     36        * dom/Element.cpp: Followed te renaming.
     37        (WebCore::Element::removeShadowRoot):
     38        * dom/Node.cpp:
     39        (WebCore::Node::setDocument):
     40        (WebCore::Node::setTreeScope):
     41        (WebCore::Node::didMoveToNewDocument):
     42        * dom/Node.h:
     43        * dom/TreeScope.h:
     44        * dom/TreeScope.cpp:
     45        (WebCore::TreeScope::adoptIfNeeded): moved from setTreeScopeRecursively()
     46        * dom/TreeScopeAdopter.cpp: Added.
     47        (WebCore::TreeScopeAdopter::TreeScopeAdopter):
     48        (WebCore::TreeScopeAdopter::moveTreeToNewScope):
     49        (WebCore::TreeScopeAdopter::moveTreeToNewDocument):
     50        (WebCore::TreeScopeAdopter::ensureDidMoveToNewDocumentWasCalled):
     51        (WebCore::TreeScopeAdopter::moveNodeToNewDocument):
     52        * dom/TreeScopeAdopter.h: Added.
     53        (WebCore::TreeScopeAdopter::ensureDidMoveToNewDocumentWasCalled):
     54        (WebCore::TreeScopeAdopter::execute):
     55        (WebCore::TreeScopeAdopter::needsScopeChange()):
     56        (WebCore::TreeScopeAdopter::shadowRootFor):
     57
    1582012-01-09  Adam Barth  <abarth@webkit.org>
    259
  • trunk/Source/WebCore/GNUmakefile.list.am

    r104459 r104528  
    16751675        Source/WebCore/dom/TreeScope.cpp \
    16761676        Source/WebCore/dom/TreeScope.h \
     1677        Source/WebCore/dom/TreeScopeAdopter.cpp \
     1678        Source/WebCore/dom/TreeScopeAdopter.h \
    16771679        Source/WebCore/dom/TreeWalker.cpp \
    16781680        Source/WebCore/dom/TreeWalker.h \
  • trunk/Source/WebCore/Target.pri

    r104447 r104528  
    575575    dom/Traversal.cpp \
    576576    dom/TreeScope.cpp \
     577    dom/TreeScopeAdopter.cpp \
    577578    dom/TreeWalker.cpp \
    578579    dom/UIEvent.cpp \
     
    16751676    dom/TreeDepthLimit.h \
    16761677    dom/TreeScope.h \
     1678    dom/TreeScopeAdopter.h \
    16771679    dom/TreeWalker.h \
    16781680    dom/UIEvent.h \
  • trunk/Source/WebCore/WebCore.gypi

    r104507 r104528  
    54715471            'dom/TreeScope.cpp',
    54725472            'dom/TreeScope.h',
     5473            'dom/TreeScopeAdopter.cpp',
     5474            'dom/TreeScopeAdopter.h',
    54735475            'dom/TreeWalker.cpp',
    54745476            'dom/TreeWalker.h',
  • trunk/Source/WebCore/WebCore.vcproj/WebCore.vcproj

    r104380 r104528  
    5179151791                        </File>
    5179251792                        <File
     51793                                RelativePath="..\dom\TreeScopeAdopter.cpp"
     51794                                >
     51795                                <FileConfiguration
     51796                                        Name="Debug|Win32"
     51797                                        ExcludedFromBuild="true"
     51798                                        >
     51799                                        <Tool
     51800                                                Name="VCCLCompilerTool"
     51801                                        />
     51802                                </FileConfiguration>
     51803                                <FileConfiguration
     51804                                        Name="Release|Win32"
     51805                                        ExcludedFromBuild="true"
     51806                                        >
     51807                                        <Tool
     51808                                                Name="VCCLCompilerTool"
     51809                                        />
     51810                                </FileConfiguration>
     51811                                <FileConfiguration
     51812                                        Name="Debug_Cairo_CFLite|Win32"
     51813                                        ExcludedFromBuild="true"
     51814                                        >
     51815                                        <Tool
     51816                                                Name="VCCLCompilerTool"
     51817                                        />
     51818                                </FileConfiguration>
     51819                                <FileConfiguration
     51820                                        Name="Release_Cairo_CFLite|Win32"
     51821                                        ExcludedFromBuild="true"
     51822                                        >
     51823                                        <Tool
     51824                                                Name="VCCLCompilerTool"
     51825                                        />
     51826                                </FileConfiguration>
     51827                                <FileConfiguration
     51828                                        Name="Debug_All|Win32"
     51829                                        ExcludedFromBuild="true"
     51830                                        >
     51831                                        <Tool
     51832                                                Name="VCCLCompilerTool"
     51833                                        />
     51834                                </FileConfiguration>
     51835                                <FileConfiguration
     51836                                        Name="Production|Win32"
     51837                                        ExcludedFromBuild="true"
     51838                                        >
     51839                                        <Tool
     51840                                                Name="VCCLCompilerTool"
     51841                                        />
     51842                                </FileConfiguration>
     51843                        </File>
     51844                        <File
     51845                                RelativePath="..\dom\TreeScopeAdopter.h"
     51846                                >
     51847                        </File>
     51848                        <File
    5179351849                                RelativePath="..\dom\TreeWalker.cpp"
    5179451850                                >
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r104380 r104528  
    35123512                A77B41A012E675A90054343D /* TextEventInputType.h in Headers */ = {isa = PBXBuildFile; fileRef = A77B419F12E675A90054343D /* TextEventInputType.h */; settings = {ATTRIBUTES = (Private, ); }; };
    35133513                A77D0012133B0AEB00D6658C /* TextChecking.h in Headers */ = {isa = PBXBuildFile; fileRef = A77D0011133B0AEB00D6658C /* TextChecking.h */; settings = {ATTRIBUTES = (Private, ); }; };
     3514                A77E1FEF14AACB6E005B7CB6 /* TreeScopeAdopter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A77E1FED14AACB6E005B7CB6 /* TreeScopeAdopter.cpp */; };
     3515                A77E1FF014AACB6E005B7CB6 /* TreeScopeAdopter.h in Headers */ = {isa = PBXBuildFile; fileRef = A77E1FEE14AACB6E005B7CB6 /* TreeScopeAdopter.h */; };
    35143516                A781C6A713828B5D0012A62A /* DocumentMarker.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A781C6A613828B5D0012A62A /* DocumentMarker.cpp */; };
    35153517                A784941B0B5FE507001E237A /* Clipboard.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A784941A0B5FE507001E237A /* Clipboard.cpp */; };
     
    1078710789                A77B419F12E675A90054343D /* TextEventInputType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextEventInputType.h; sourceTree = "<group>"; };
    1078810790                A77D0011133B0AEB00D6658C /* TextChecking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextChecking.h; sourceTree = "<group>"; };
     10791                A77E1FED14AACB6E005B7CB6 /* TreeScopeAdopter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TreeScopeAdopter.cpp; sourceTree = "<group>"; };
     10792                A77E1FEE14AACB6E005B7CB6 /* TreeScopeAdopter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TreeScopeAdopter.h; sourceTree = "<group>"; };
    1078910793                A781C6A613828B5D0012A62A /* DocumentMarker.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DocumentMarker.cpp; sourceTree = "<group>"; };
    1079010794                A784941A0B5FE507001E237A /* Clipboard.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = Clipboard.cpp; sourceTree = "<group>"; };
     
    2146321467                                14D64B5A134A5B6B00E58FDA /* TreeScope.cpp */,
    2146421468                                14D64B5B134A5B6B00E58FDA /* TreeScope.h */,
     21469                                A77E1FED14AACB6E005B7CB6 /* TreeScopeAdopter.cpp */,
     21470                                A77E1FEE14AACB6E005B7CB6 /* TreeScopeAdopter.h */,
    2146521471                                854FE72E0A2297BE0058D7AD /* TreeWalker.cpp */,
    2146621472                                854FE72F0A2297BE0058D7AD /* TreeWalker.h */,
     
    2471524721                                37FD4298118368460093C029 /* TreeDepthLimit.h in Headers */,
    2471624722                                14D64B5D134A5B6B00E58FDA /* TreeScope.h in Headers */,
     24723                                A77E1FF014AACB6E005B7CB6 /* TreeScopeAdopter.h in Headers */,
    2471724724                                1419D2C50CEA6F6100FF507A /* TreeShared.h in Headers */,
    2471824725                                854FE7390A2297BE0058D7AD /* TreeWalker.h in Headers */,
     
    2764927656                                854FE7360A2297BE0058D7AD /* Traversal.cpp in Sources */,
    2765027657                                14D64B5C134A5B6B00E58FDA /* TreeScope.cpp in Sources */,
     27658                                A77E1FEF14AACB6E005B7CB6 /* TreeScopeAdopter.cpp in Sources */,
    2765127659                                854FE7380A2297BE0058D7AD /* TreeWalker.cpp in Sources */,
    2765227660                                93309E19099E64920056E581 /* TypingCommand.cpp in Sources */,
  • trunk/Source/WebCore/dom/ContainerNode.cpp

    r104271 r104528  
    100100        // (if the document changed or oldParent was in a shadow tree, AND *this is in a shadow tree).
    101101        // Can we do better?
    102         child->setTreeScopeRecursively(treeScope());
     102        treeScope()->adoptIfNeeded(child.get());
    103103        if (attached() && !child->attached())
    104104            child->attach();
     
    176176#endif
    177177
    178         child->setTreeScopeRecursively(treeScope());
     178        treeScope()->adoptIfNeeded(child);
    179179
    180180        insertBeforeCommon(next.get(), child);
     
    330330#endif
    331331
    332         child->setTreeScopeRecursively(treeScope());
     332        treeScope()->adoptIfNeeded(child.get());
    333333
    334334        // Add child after "prev".
     
    510510    oldChild->setParent(0);
    511511
    512     oldChild->setTreeScopeRecursively(document());
     512    document()->adoptIfNeeded(oldChild);
    513513
    514514    allowEventDispatch();
     
    565565        n->setNextSibling(0);
    566566        n->setParent(0);
    567         n->setTreeScopeRecursively(document());
     567        document()->adoptIfNeeded(n.get());
    568568
    569569        m_firstChild = next;
     
    650650#endif
    651651
    652         child->setTreeScopeRecursively(treeScope());
     652        treeScope()->adoptIfNeeded(child);
    653653
    654654        // Append child to the end of the list
     
    698698    // FIXME: This method should take a PassRefPtr.
    699699    appendChildToContainer<Node, ContainerNode>(newChild.get(), this);
    700     newChild->setTreeScopeRecursively(treeScope());
     700    treeScope()->adoptIfNeeded(newChild.get());
    701701   
    702702    allowEventDispatch();
  • trunk/Source/WebCore/dom/DOMAllInOne.cpp

    r104271 r104528  
    130130#include "Traversal.cpp"
    131131#include "TreeScope.cpp"
     132#include "TreeScopeAdopter.cpp"
    132133#include "TreeWalker.cpp"
    133134#include "UIEvent.cpp"
  • trunk/Source/WebCore/dom/Document.cpp

    r104458 r104528  
    709709    m_docType = docType;
    710710    if (m_docType)
    711         m_docType->setTreeScopeRecursively(this);
     711        this->adoptIfNeeded(m_docType.get());
    712712    // Doctype affects the interpretation of the stylesheets.
    713713    clearStyleSelector();
     
    944944    }
    945945
    946     source->setTreeScopeRecursively(this);
     946    this->adoptIfNeeded(source.get());
    947947
    948948    return source;
  • trunk/Source/WebCore/dom/Element.cpp

    r104404 r104528  
    12151215
    12161216        oldRoot->setShadowHost(0);
    1217         oldRoot->setTreeScopeRecursively(document());
     1217        document()->adoptIfNeeded(oldRoot.get());
    12181218        if (oldRoot->inDocument())
    12191219            oldRoot->removedFromDocument();
  • trunk/Source/WebCore/dom/Node.cpp

    r104276 r104528  
    9191#include "Text.h"
    9292#include "TextEvent.h"
     93#include "TreeScopeAdopter.h"
    9394#include "UIEvent.h"
    9495#include "UIEventWithKeyState.h"
     
    418419}
    419420
    420 #ifndef NDEBUG
    421 
    422 static bool didMoveToNewDocumentWasCalled;
    423 static Document* oldDocumentDidMoveToNewDocumentWasCalledWith;
    424 
    425 #endif
    426    
    427421void Node::setDocument(Document* document)
    428422{
     
    431425        return;
    432426
    433     document->guardRef();
    434 
    435     if (m_document) {
    436         m_document->moveNodeIteratorsToNewDocument(this, document);
    437         m_document->guardDeref();
    438     }
    439 
    440     Document* oldDocument = m_document;
    441427    m_document = document;
    442 
    443 #ifndef NDEBUG
    444     didMoveToNewDocumentWasCalled = false;
    445     oldDocumentDidMoveToNewDocumentWasCalledWith = oldDocument;
    446 #endif
    447 
    448     didMoveToNewDocument(oldDocument);
    449 
    450     ASSERT(didMoveToNewDocumentWasCalled);
     428}
     429
     430NodeRareData* Node::setTreeScope(TreeScope* scope)
     431{
     432    if (!scope) {
     433        if (hasRareData()) {
     434            NodeRareData* data = rareData();
     435            data->setTreeScope(0);
     436            return data;
     437        }
     438
     439        return 0;
     440    }
     441
     442    NodeRareData* data = ensureRareData();
     443    data->setTreeScope(scope);
     444    return data;
    451445}
    452446
     
    458452    TreeScope* scope = rareData()->treeScope();
    459453    return scope ? scope : m_document;
    460 }
    461 
    462 void Node::setTreeScopeRecursively(TreeScope* newTreeScope)
    463 {
    464     ASSERT(this);
    465     ASSERT(!isDocumentNode());
    466     ASSERT(newTreeScope);
    467     ASSERT(!m_deletionHasBegun);
    468 
    469     TreeScope* currentTreeScope = treeScope();
    470     if (currentTreeScope == newTreeScope)
    471         return;
    472 
    473     Document* currentDocument = document();
    474     Document* newDocument = newTreeScope->document();
    475     // If an element is moved from a document and then eventually back again the collection cache for
    476     // that element may contain stale data as changes made to it will have updated the DOMTreeVersion
    477     // of the document it was moved to. By increasing the DOMTreeVersion of the donating document here
    478     // we ensure that the collection cache will be invalidated as needed when the element is moved back.
    479     if (currentDocument && currentDocument != newDocument)
    480         currentDocument->incDOMTreeVersion();
    481 
    482     for (Node* node = this; node; node = node->traverseNextNode(this)) {
    483         if (newTreeScope == newDocument) {
    484             if (node->hasRareData())
    485                 node->rareData()->setTreeScope(0);
    486             // Setting the new document tree scope will be handled implicitly
    487             // by setDocument() below.
    488         } else
    489             node->ensureRareData()->setTreeScope(newTreeScope);
    490 
    491         if (node->hasRareData() && node->rareData()->nodeLists()) {
    492             node->rareData()->nodeLists()->invalidateCaches();
    493             if (currentTreeScope)
    494                 currentTreeScope->removeNodeListCache();
    495             newTreeScope->addNodeListCache();
    496         }
    497 
    498         node->setDocument(newDocument);
    499 
    500         if (!node->isElementNode())
    501             continue;
    502         if (ShadowRoot* shadowRoot = toElement(node)->shadowRoot()) {
    503             shadowRoot->setParentTreeScope(newTreeScope);
    504             if (currentDocument != newDocument)
    505                 shadowRoot->setDocumentRecursively(newDocument);
    506         }
    507     }
    508454}
    509455
     
    870816{
    871817    return node->isElementNode() ? toElement(node)->shadowRoot() : 0;
    872 }
    873 
    874 void Node::setDocumentRecursively(Document* newDocument)
    875 {
    876     ASSERT(document() != newDocument);
    877 
    878     for (Node* node = this; node; node = node->traverseNextNode(this)) {
    879         node->setDocument(newDocument);
    880         if (!node->isElementNode())
    881             continue;
    882         if (ShadowRoot* shadow = shadowRoot(node))
    883             shadow->setDocumentRecursively(newDocument);
    884     }
    885818}
    886819
     
    24362369void Node::didMoveToNewDocument(Document* oldDocument)
    24372370{
    2438     ASSERT(!didMoveToNewDocumentWasCalled);
    2439     ASSERT_UNUSED(oldDocument, oldDocument == oldDocumentDidMoveToNewDocumentWasCalledWith);
    2440 
    2441 #ifndef NDEBUG
    2442     didMoveToNewDocumentWasCalled = true;
    2443 #endif
     2371    TreeScopeAdopter::ensureDidMoveToNewDocumentWasCalled(oldDocument);
    24442372
    24452373    // FIXME: Event listener types for this node should be set on the new owner document here.
  • trunk/Source/WebCore/dom/Node.h

    r104276 r104528  
    106106    friend class Document;
    107107    friend class TreeScope;
     108    friend class TreeScopeAdopter;
    108109
    109110public:
     
    408409    TreeScope* treeScope() const;
    409410
    410     // Used by the basic DOM methods (e.g., appendChild()).
    411     void setTreeScopeRecursively(TreeScope*);
    412 
    413411    // Returns true if this node is associated with a document and is in its associated document's
    414412    // node tree, false otherwise.
     
    720718
    721719private:
    722     // Do not use this method to change the document of a node until after the node has been
    723     // removed from its previous document.
    724     void setDocumentRecursively(Document*);
     720    // These API should be only used for a tree scope migration.
     721    // setTreeScope() returns NodeRareData to save extra nodeRareData() invocations on the caller site.
     722    NodeRareData* setTreeScope(TreeScope*);
    725723    void setDocument(Document*);
    726724
  • trunk/Source/WebCore/dom/TreeScope.cpp

    r104271 r104528  
    3232#include "HTMLNames.h"
    3333#include "NodeRareData.h"
     34#include "TreeScopeAdopter.h"
    3435
    3536namespace WebCore {
     
    138139}
    139140
     141void TreeScope::adoptIfNeeded(Node* node)
     142{
     143    ASSERT(this);
     144    ASSERT(node);
     145    ASSERT(!node->isDocumentNode());
     146    ASSERT(!node->m_deletionHasBegun);
     147    TreeScopeAdopter adopter(node, this);
     148    if (adopter.needsScopeChange())
     149        adopter.execute();
     150}
     151
    140152} // namespace WebCore
    141153
  • trunk/Source/WebCore/dom/TreeScope.h

    r104271 r104528  
    6565    virtual bool applyAuthorSheets() const;
    6666
     67    // Used by the basic DOM mutation methods (e.g., appendChild()).
     68    void adoptIfNeeded(Node*);
     69
    6770protected:
    6871    TreeScope(Document*, ConstructionType = CreateContainer);
Note: See TracChangeset for help on using the changeset viewer.