Match DOM4 spec with respect to DocumentFragment insertion
authoradamk@chromium.org <adamk@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 23 Mar 2012 22:51:25 +0000 (22:51 +0000)
committeradamk@chromium.org <adamk@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 23 Mar 2012 22:51:25 +0000 (22:51 +0000)
commit64cfb4e75d3118efdc231da4cdc245ae07d1f98f
treeefc79a4e2716f219abe16a8bd6a00f08eb59d51a
parentcef65d9ddd3aac3ef49003e4f04e23e0c83e8608
Match DOM4 spec with respect to DocumentFragment insertion
https://bugs.webkit.org/show_bug.cgi?id=82089

Reviewed by Ryosuke Niwa.

Source/WebCore:

DOM4 specifies the behavior of appendChild, insertBefore, and replaceChild
in terms of "mutation algorithms":

http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#mutation-algorithms

This change updates WebKit to match, in particular with regard to DocumentFragments.
Previously, ContainerNode would remove nodes one at a time, then add them to the new parent.
When combined with MutationObservers, this results in overly-verbose mutation records.
Now we create as few records as possible, matching the spec as well as Gecko's implementation
of MutationObservers.

Note that we still need to check validity each time through the loop,
since inserting a node may dispatch events. In a future change, I hope
to move these events so that they fire only after all nodes are inserted,
but that's too much to tackle all in one.

Tests: fast/mutation/document-fragment-insertion.html

* dom/ContainerNode.cpp:
(WebCore::collectChildrenAndRemoveFromOldParent): New helper method
combining collectTargetNodes() with the removal of the collected nodes from
their old parent, if any.
(WebCore::ContainerNode::insertBefore): Use new helper method instead
of removing nodes one at a time from the fragment.
(WebCore::ContainerNode::replaceChild): ditto. Also removed some redundant asserts
and moved the "do nothing" check out of the loop.
(WebCore::ContainerNode::appendChild): Use new helper method.

LayoutTests:

* fast/dom/Node/fragment-mutation-expected.txt:
* fast/dom/Node/fragment-mutation.html: Removed tests that no longer make sense
since the DocumentFragment has no children by the time DOMSubtreeModified is called.
* fast/mutation/document-fragment-insertion-expected.txt: Added.
* fast/mutation/document-fragment-insertion.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@111925 268f45cc-cd09-0410-ab3c-d52691b4dbfc
LayoutTests/ChangeLog
LayoutTests/fast/dom/Node/fragment-mutation-expected.txt
LayoutTests/fast/dom/Node/fragment-mutation.html
LayoutTests/fast/mutation/document-fragment-insertion-expected.txt [new file with mode: 0644]
LayoutTests/fast/mutation/document-fragment-insertion.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/dom/ContainerNode.cpp