<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[190233] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/190233">190233</a></dd>
<dt>Author</dt> <dd>cdumez@apple.com</dd>
<dt>Date</dt> <dd>2015-09-24 19:48:55 -0700 (Thu, 24 Sep 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Node.replaceChild() does not behave according to the specification
https://bugs.webkit.org/show_bug.cgi?id=149546
&lt;rdar://problem/22571887&gt;

Reviewed by Ryosuke Niwa.

LayoutTests/imported/w3c:

Rebaseline W3C DOM test now that one more check is passing.

* web-platform-tests/dom/nodes/MutationObserver-childList-expected.txt:

Source/WebCore:

Node.replaceChild() does not behave according to the specification. In
particular, when replacing |child| with |node| we are supposed to remove
|node| from its parent *before* removing |child| from its parent:
- https://dom.spec.whatwg.org/#concept-node-replace (Steps 10 &amp; 11)

This patch reverses the order as per the specification. Our new behavior
matches Firefox's behavior. Note that this patch also remove an
optimization when replacing a child with its next sibling. This
optimization was observable from JS. It seems likely this case is not
common enough for it to be an issue. However, we can revisit if we see
it regresses things.

This leads to incorrect Mutation Records being queued in some cases.

No new tests, already covered by existing test.

* dom/ContainerNode.cpp:
(WebCore::ContainerNode::replaceChild):

LayoutTests:

Update existing test that now throws a different exception.

* fast/events/mutation-during-replace-child-expected.txt:
* fast/events/mutation-during-replace-child.html:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsfasteventsmutationduringreplacechildexpectedtxt">trunk/LayoutTests/fast/events/mutation-during-replace-child-expected.txt</a></li>
<li><a href="#trunkLayoutTestsfasteventsmutationduringreplacechildhtml">trunk/LayoutTests/fast/events/mutation-during-replace-child.html</a></li>
<li><a href="#trunkLayoutTestsimportedw3cChangeLog">trunk/LayoutTests/imported/w3c/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsimportedw3cwebplatformtestsdomnodesMutationObserverchildListexpectedtxt">trunk/LayoutTests/imported/w3c/web-platform-tests/dom/nodes/MutationObserver-childList-expected.txt</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoredomContainerNodecpp">trunk/Source/WebCore/dom/ContainerNode.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (190232 => 190233)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2015-09-25 01:50:04 UTC (rev 190232)
+++ trunk/LayoutTests/ChangeLog        2015-09-25 02:48:55 UTC (rev 190233)
</span><span class="lines">@@ -1,3 +1,16 @@
</span><ins>+2015-09-24  Chris Dumez  &lt;cdumez@apple.com&gt;
+
+        Node.replaceChild() does not behave according to the specification
+        https://bugs.webkit.org/show_bug.cgi?id=149546
+        &lt;rdar://problem/22571887&gt;
+
+        Reviewed by Ryosuke Niwa.
+
+        Update existing test that now throws a different exception.
+
+        * fast/events/mutation-during-replace-child-expected.txt:
+        * fast/events/mutation-during-replace-child.html:
+
</ins><span class="cx"> 2015-09-24  Beth Dakin  &lt;bdakin@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         The same problem happens on El Capitan.
</span></span></pre></div>
<a id="trunkLayoutTestsfasteventsmutationduringreplacechildexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/fast/events/mutation-during-replace-child-expected.txt (190232 => 190233)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/events/mutation-during-replace-child-expected.txt        2015-09-25 01:50:04 UTC (rev 190232)
+++ trunk/LayoutTests/fast/events/mutation-during-replace-child-expected.txt        2015-09-25 02:48:55 UTC (rev 190233)
</span><span class="lines">@@ -3,7 +3,7 @@
</span><span class="cx"> On success, you will see a series of &quot;PASS&quot; messages, followed by &quot;TEST COMPLETE&quot;.
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-PASS target.replaceChild(newChild, oldChild); threw exception Error: HierarchyRequestError: DOM Exception 3.
</del><ins>+PASS target.replaceChild(newChild, oldChild); threw exception Error: NotFoundError: DOM Exception 8.
</ins><span class="cx"> PASS successfullyParsed is true
</span><span class="cx"> 
</span><span class="cx"> TEST COMPLETE
</span></span></pre></div>
<a id="trunkLayoutTestsfasteventsmutationduringreplacechildhtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/fast/events/mutation-during-replace-child.html (190232 => 190233)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/events/mutation-during-replace-child.html        2015-09-25 01:50:04 UTC (rev 190232)
+++ trunk/LayoutTests/fast/events/mutation-during-replace-child.html        2015-09-25 02:48:55 UTC (rev 190233)
</span><span class="lines">@@ -24,7 +24,7 @@
</span><span class="cx">     newChild.appendChild(target);
</span><span class="cx"> }   
</span><span class="cx"> document.addEventListener(&quot;DOMNodeRemoved&quot;, handler, false);
</span><del>-shouldThrow(&quot;target.replaceChild(newChild, oldChild);&quot;,  &quot;'Error: HierarchyRequestError: DOM Exception 3'&quot;);
</del><ins>+shouldThrow(&quot;target.replaceChild(newChild, oldChild);&quot;,  &quot;'Error: NotFoundError: DOM Exception 8'&quot;);
</ins><span class="cx"> &lt;/script&gt;
</span><span class="cx"> &lt;script src=&quot;../../resources/js-test-post.js&quot;&gt;&lt;/script&gt;
</span><span class="cx"> &lt;/body&gt;
</span></span></pre></div>
<a id="trunkLayoutTestsimportedw3cChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/ChangeLog (190232 => 190233)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/ChangeLog        2015-09-25 01:50:04 UTC (rev 190232)
+++ trunk/LayoutTests/imported/w3c/ChangeLog        2015-09-25 02:48:55 UTC (rev 190233)
</span><span class="lines">@@ -1,5 +1,17 @@
</span><span class="cx"> 2015-09-24  Chris Dumez  &lt;cdumez@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Node.replaceChild() does not behave according to the specification
+        https://bugs.webkit.org/show_bug.cgi?id=149546
+        &lt;rdar://problem/22571887&gt;
+
+        Reviewed by Ryosuke Niwa.
+
+        Rebaseline W3C DOM test now that one more check is passing.
+
+        * web-platform-tests/dom/nodes/MutationObserver-childList-expected.txt:
+
+2015-09-24  Chris Dumez  &lt;cdumez@apple.com&gt;
+
</ins><span class="cx">         Rewrite Range::insertNode() as per the latest DOM specification
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=149528
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsimportedw3cwebplatformtestsdomnodesMutationObserverchildListexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/dom/nodes/MutationObserver-childList-expected.txt (190232 => 190233)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/web-platform-tests/dom/nodes/MutationObserver-childList-expected.txt        2015-09-25 01:50:04 UTC (rev 190232)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/dom/nodes/MutationObserver-childList-expected.txt        2015-09-25 02:48:55 UTC (rev 190233)
</span><span class="lines">@@ -22,7 +22,7 @@
</span><span class="cx"> PASS childList Node.appendChild: addition outside document tree mutation 
</span><span class="cx"> PASS childList Node.replaceChild: replacement mutation 
</span><span class="cx"> PASS childList Node.replaceChild: removal mutation 
</span><del>-FAIL childList Node.replaceChild: internal replacement mutation assert_equals: mutation records must match expected 2 but got 1
</del><ins>+PASS childList Node.replaceChild: internal replacement mutation 
</ins><span class="cx"> PASS childList Node.removeChild: removal mutation 
</span><span class="cx"> PASS Range (r70) is created 
</span><span class="cx"> PASS childList Range.deleteContents: child removal mutation 
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (190232 => 190233)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-09-25 01:50:04 UTC (rev 190232)
+++ trunk/Source/WebCore/ChangeLog        2015-09-25 02:48:55 UTC (rev 190233)
</span><span class="lines">@@ -1,5 +1,32 @@
</span><span class="cx"> 2015-09-24  Chris Dumez  &lt;cdumez@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Node.replaceChild() does not behave according to the specification
+        https://bugs.webkit.org/show_bug.cgi?id=149546
+        &lt;rdar://problem/22571887&gt;
+
+        Reviewed by Ryosuke Niwa.
+
+        Node.replaceChild() does not behave according to the specification. In
+        particular, when replacing |child| with |node| we are supposed to remove
+        |node| from its parent *before* removing |child| from its parent:
+        - https://dom.spec.whatwg.org/#concept-node-replace (Steps 10 &amp; 11)
+
+        This patch reverses the order as per the specification. Our new behavior
+        matches Firefox's behavior. Note that this patch also remove an
+        optimization when replacing a child with its next sibling. This
+        optimization was observable from JS. It seems likely this case is not
+        common enough for it to be an issue. However, we can revisit if we see
+        it regresses things.
+
+        This leads to incorrect Mutation Records being queued in some cases.
+
+        No new tests, already covered by existing test.
+
+        * dom/ContainerNode.cpp:
+        (WebCore::ContainerNode::replaceChild):
+
+2015-09-24  Chris Dumez  &lt;cdumez@apple.com&gt;
+
</ins><span class="cx">         Unreviewed, roll out r187615 as it seems to have caused a ~1% PLT regression.
</span><span class="cx">         &lt;rdar://problem/22657123&gt;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoredomContainerNodecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/ContainerNode.cpp (190232 => 190233)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/ContainerNode.cpp        2015-09-25 01:50:04 UTC (rev 190232)
+++ trunk/Source/WebCore/dom/ContainerNode.cpp        2015-09-25 02:48:55 UTC (rev 190233)
</span><span class="lines">@@ -414,50 +414,48 @@
</span><span class="cx"> 
</span><span class="cx">     ChildListMutationScope mutation(*this);
</span><span class="cx"> 
</span><del>-    RefPtr&lt;Node&gt; next = oldChild.nextSibling();
</del><ins>+    RefPtr&lt;Node&gt; refChild = oldChild.nextSibling();
+    if (refChild.get() == newChild.ptr())
+        refChild = refChild-&gt;nextSibling();
</ins><span class="cx"> 
</span><del>-    // Remove the node we're replacing
-    Ref&lt;Node&gt; removedChild(oldChild);
-    removeChild(oldChild, ec);
</del><ins>+    NodeVector targets;
+    collectChildrenAndRemoveFromOldParent(newChild, targets, ec);
</ins><span class="cx">     if (ec)
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><del>-    if (next &amp;&amp; (next-&gt;previousSibling() == newChild.ptr() || next == newChild.ptr())) // nothing to do
-        return true;
-
-    // Does this one more time because removeChild() fires a MutationEvent.
</del><ins>+    // Does this one more time because collectChildrenAndRemoveFromOldParent() fires a MutationEvent.
</ins><span class="cx">     if (!checkPreReplacementValidity(*this, newChild, oldChild, ec))
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><del>-    NodeVector targets;
-    collectChildrenAndRemoveFromOldParent(newChild, targets, ec);
</del><ins>+    // Remove the node we're replacing.
+    Ref&lt;Node&gt; protectOldChild(oldChild);
+    removeChild(oldChild, ec);
</ins><span class="cx">     if (ec)
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><del>-    // Does this yet another check because collectChildrenAndRemoveFromOldParent() fires a MutationEvent.
</del><ins>+    // Does this one more time because removeChild() fires a MutationEvent.
</ins><span class="cx">     if (!checkPreReplacementValidity(*this, newChild, oldChild, ec))
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><span class="cx">     InspectorInstrumentation::willInsertDOMNode(document(), *this);
</span><span class="cx"> 
</span><del>-    // Add the new child(ren)
</del><ins>+    // Add the new child(ren).
</ins><span class="cx">     for (auto&amp; child : targets) {
</span><span class="cx">         // Due to arbitrary code running in response to a DOM mutation event it's
</span><del>-        // possible that &quot;next&quot; is no longer a child of &quot;this&quot;.
</del><ins>+        // possible that &quot;refChild&quot; is no longer a child of &quot;this&quot;.
</ins><span class="cx">         // It's also possible that &quot;child&quot; has been inserted elsewhere.
</span><span class="cx">         // In either of those cases, we'll just stop.
</span><del>-        if (next &amp;&amp; next-&gt;parentNode() != this)
</del><ins>+        if (refChild &amp;&amp; refChild-&gt;parentNode() != this)
</ins><span class="cx">             break;
</span><span class="cx">         if (child-&gt;parentNode())
</span><span class="cx">             break;
</span><span class="cx"> 
</span><span class="cx">         treeScope().adoptIfNeeded(child.ptr());
</span><span class="cx"> 
</span><del>-        // Add child before &quot;next&quot;.
</del><span class="cx">         {
</span><span class="cx">             NoEventDispatchAssertion assertNoEventDispatch;
</span><del>-            if (next)
-                insertBeforeCommon(*next, child.get());
</del><ins>+            if (refChild)
+                insertBeforeCommon(*refChild, child.get());
</ins><span class="cx">             else
</span><span class="cx">                 appendChildCommon(child);
</span><span class="cx">         }
</span></span></pre>
</div>
</div>

</body>
</html>