Title: [112546] trunk/Source/WebCore
Revision
112546
Author
[email protected]
Date
2012-03-29 11:03:59 -0700 (Thu, 29 Mar 2012)

Log Message

Factor out common post-insertion logic in ContainerNode
https://bugs.webkit.org/show_bug.cgi?id=82544

Reviewed by Ryosuke Niwa.

appendChild, insertBefore, and replaceChild all share a great deal of logic.
This patch factors out the "post-insertion" logic that deals with
notifying parents that their children changed and notifying children
that they've been added to the tree.

Besides reducing code duplication, this is in preparation for moving
this post-insertion notification later in the insertion process.

No new tests, no change in behavior.

* dom/ContainerNode.cpp:
(WebCore):
(WebCore::ContainerNode::insertBefore): Factor out shared logic, remove unnecessary "prev" variable.
(WebCore::ContainerNode::replaceChild): ditto.
(WebCore::ContainerNode::appendChild): ditto.
(WebCore::updateTreeAfterInsertion): New helper method encapsulating shared logic.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (112545 => 112546)


--- trunk/Source/WebCore/ChangeLog	2012-03-29 17:52:03 UTC (rev 112545)
+++ trunk/Source/WebCore/ChangeLog	2012-03-29 18:03:59 UTC (rev 112546)
@@ -1,3 +1,27 @@
+2012-03-29  Adam Klein  <[email protected]>
+
+        Factor out common post-insertion logic in ContainerNode
+        https://bugs.webkit.org/show_bug.cgi?id=82544
+
+        Reviewed by Ryosuke Niwa.
+
+        appendChild, insertBefore, and replaceChild all share a great deal of logic.
+        This patch factors out the "post-insertion" logic that deals with
+        notifying parents that their children changed and notifying children
+        that they've been added to the tree.
+
+        Besides reducing code duplication, this is in preparation for moving
+        this post-insertion notification later in the insertion process.
+
+        No new tests, no change in behavior.
+
+        * dom/ContainerNode.cpp:
+        (WebCore):
+        (WebCore::ContainerNode::insertBefore): Factor out shared logic, remove unnecessary "prev" variable.
+        (WebCore::ContainerNode::replaceChild): ditto.
+        (WebCore::ContainerNode::appendChild): ditto.
+        (WebCore::updateTreeAfterInsertion): New helper method encapsulating shared logic.
+
 2012-03-29  Tony Chang  <[email protected]>
 
         Need to implement flex-line-pack

Modified: trunk/Source/WebCore/dom/ContainerNode.cpp (112545 => 112546)


--- trunk/Source/WebCore/dom/ContainerNode.cpp	2012-03-29 17:52:03 UTC (rev 112545)
+++ trunk/Source/WebCore/dom/ContainerNode.cpp	2012-03-29 18:03:59 UTC (rev 112546)
@@ -50,6 +50,7 @@
 static void notifyChildInserted(Node*);
 static void dispatchChildInsertionEvents(Node*);
 static void dispatchChildRemovalEvents(Node*);
+static void updateTreeAfterInsertion(ContainerNode*, Node*, bool shouldLazyAttach);
 
 typedef pair<RefPtr<Node>, unsigned> CallbackParameters;
 typedef pair<NodeCallback, CallbackParameters> CallbackInfo;
@@ -155,7 +156,6 @@
     ChildListMutationScope mutation(this);
 #endif
 
-    RefPtr<Node> prev = next->previousSibling();
     for (NodeVector::const_iterator it = targets.begin(); it != targets.end(); ++it) {
         Node* child = it->get();
 
@@ -176,21 +176,7 @@
 
         insertBeforeCommon(next.get(), child);
 
-        // Send notification about the children change.
-        childrenChanged(false, prev.get(), next.get(), 1);
-        notifyChildInserted(child);
-
-        // Add child to the rendering tree.
-        if (attached() && !child->attached() && child->parentNode() == this) {
-            if (shouldLazyAttach)
-                child->lazyAttach();
-            else
-                child->attach();
-        }
-
-        // Now that the child is attached to the render tree, dispatch
-        // the relevant mutation events.
-        dispatchChildInsertionEvents(child);
+        updateTreeAfterInsertion(this, child, shouldLazyAttach);
     }
 
     dispatchSubtreeModifiedEvent();
@@ -276,7 +262,6 @@
     ChildListMutationScope mutation(this);
 #endif
 
-    RefPtr<Node> prev = oldChild->previousSibling();
     RefPtr<Node> next = oldChild->nextSibling();
 
     // Remove the node we're replacing
@@ -320,21 +305,7 @@
             appendChildToContainer(child, this);
         allowEventDispatch();
 
-        childrenChanged(false, prev.get(), next.get(), 1);
-        notifyChildInserted(child);
-
-        // Add child to the rendering tree
-        if (attached() && !child->attached() && child->parentNode() == this) {
-            if (shouldLazyAttach)
-                child->lazyAttach();
-            else
-                child->attach();
-        }
-
-        // Now that the child is attached to the render tree, dispatch
-        // the relevant mutation events.
-        dispatchChildInsertionEvents(child);
-        prev = child;
+        updateTreeAfterInsertion(this, child, shouldLazyAttach);
     }
 
     dispatchSubtreeModifiedEvent();
@@ -592,7 +563,6 @@
 #endif
 
     // Now actually add the child(ren)
-    RefPtr<Node> prev = lastChild();
     for (NodeVector::const_iterator it = targets.begin(); it != targets.end(); ++it) {
         Node* child = it->get();
 
@@ -613,22 +583,7 @@
         appendChildToContainer(child, this);
         allowEventDispatch();
 
-        // Send notification about the children change.
-        childrenChanged(false, prev.get(), 0, 1);
-        notifyChildInserted(child);
-
-        // Add child to the rendering tree
-        if (attached() && !child->attached() && child->parentNode() == this) {
-            if (shouldLazyAttach)
-                child->lazyAttach();
-            else
-                child->attach();
-        }
-
-        // Now that the child is attached to the render tree, dispatch
-        // the relevant mutation events.
-        dispatchChildInsertionEvents(child);
-        prev = child;
+        updateTreeAfterInsertion(this, child, shouldLazyAttach);
     }
 
     dispatchSubtreeModifiedEvent();
@@ -1132,4 +1087,25 @@
     }
 }
 
+static void updateTreeAfterInsertion(ContainerNode* parent, Node* child, bool shouldLazyAttach)
+{
+    ASSERT(parent->refCount());
+    ASSERT(child->refCount());
+
+    parent->childrenChanged(false, child->previousSibling(), child->nextSibling(), 1);
+
+    notifyChildInserted(child);
+
+    // FIXME: Attachment should be the first operation in this function, but some code
+    // (for example, HTMLFormControlElement's autofocus support) requires this ordering.
+    if (parent->attached() && !child->attached() && child->parentNode() == parent) {
+        if (shouldLazyAttach)
+            child->lazyAttach();
+        else
+            child->attach();
+    }
+
+    dispatchChildInsertionEvents(child);
+}
+
 } // namespace WebCore
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to