<!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>[205085] 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/205085">205085</a></dd>
<dt>Author</dt> <dd>rniwa@webkit.org</dd>
<dt>Date</dt> <dd>2016-08-27 15:13:44 -0700 (Sat, 27 Aug 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Source/WebCore:
Add adopted callback for custom elements
https://bugs.webkit.org/show_bug.cgi?id=161284

Reviewed by Antti Koivisto.

Added the support for adoptedCallback: https://dom.spec.whatwg.org/#concept-node-adopt
For now, we only support this callback on appendChild.

Test: fast/custom-elements/adopted-callback.html

* bindings/js/JSCustomElementInterface.cpp:
(WebCore::JSCustomElementInterface::invokeCallback): Added JSDOMGlobalObject* as an argument to the callback so that
we can invoke toJS on Document in invokeAdoptedCallback.
(WebCore::JSCustomElementInterface::setAdoptedCallback): Added.
(WebCore::JSCustomElementInterface::invokeAdoptedCallback): Added.
(WebCore::JSCustomElementInterface::setAttributeChangedCallback):
* bindings/js/JSCustomElementInterface.h:
(WebCore::JSCustomElementInterface::hasConnectedCallback): Added.
(WebCore::JSCustomElementInterface::hasDisconnectedCallback): Added.
(WebCore::JSCustomElementInterface::hasAdoptedCallback): Added.
* bindings/js/JSCustomElementRegistryCustom.cpp:
(WebCore::JSCustomElementRegistry::define):
* dom/CustomElementReactionQueue.cpp:
(WebCore::CustomElementReactionQueueItem::CustomElementReactionQueueItem): Added a variant that takes two documents.
(WebCore::CustomElementReactionQueueItem::invoke):
(WebCore::CustomElementReactionQueue::enqueueConnectedCallbackIfNeeded): Fixed a bug that this function was always
enqueuing a callback even when the interface didn't have connectedCallback. Also, there is no need to check
the nullity of the interface since it should never be null.
(WebCore::CustomElementReactionQueue::enqueueDisconnectedCallbackIfNeeded): Ditto.
(WebCore::CustomElementReactionQueue::enqueueAdoptedCallbackIfNeeded): Added.
(WebCore::CustomElementReactionQueue::enqueueAttributeChangedCallbackIfNeeded): Assert that the interface is never
null instead of exiting early.
* dom/CustomElementReactionQueue.h:
* dom/Element.cpp:
(WebCore::Element::didMoveToNewDocument): Added a call to enqueueAdoptedCallbackIfNeeded.

LayoutTests:
adoptcallback

Add adopted callback for custom elements
https://bugs.webkit.org/show_bug.cgi?id=161284

Reviewed by Antti Koivisto.

* fast/custom-elements/adopted-callback-expected.txt: Added.
* fast/custom-elements/adopted-callback.html: Added.
* fast/custom-elements/resources/document-types.js:
(const.DocumentTypes.create):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsfastcustomelementsresourcesdocumenttypesjs">trunk/LayoutTests/fast/custom-elements/resources/document-types.js</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorebindingsjsJSCustomElementInterfacecpp">trunk/Source/WebCore/bindings/js/JSCustomElementInterface.cpp</a></li>
<li><a href="#trunkSourceWebCorebindingsjsJSCustomElementInterfaceh">trunk/Source/WebCore/bindings/js/JSCustomElementInterface.h</a></li>
<li><a href="#trunkSourceWebCorebindingsjsJSCustomElementRegistryCustomcpp">trunk/Source/WebCore/bindings/js/JSCustomElementRegistryCustom.cpp</a></li>
<li><a href="#trunkSourceWebCoredomCustomElementReactionQueuecpp">trunk/Source/WebCore/dom/CustomElementReactionQueue.cpp</a></li>
<li><a href="#trunkSourceWebCoredomCustomElementReactionQueueh">trunk/Source/WebCore/dom/CustomElementReactionQueue.h</a></li>
<li><a href="#trunkSourceWebCoredomElementcpp">trunk/Source/WebCore/dom/Element.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsfastcustomelementsadoptedcallbackexpectedtxt">trunk/LayoutTests/fast/custom-elements/adopted-callback-expected.txt</a></li>
<li><a href="#trunkLayoutTestsfastcustomelementsadoptedcallbackhtml">trunk/LayoutTests/fast/custom-elements/adopted-callback.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (205084 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2016-08-27 21:34:57 UTC (rev 205084)
+++ trunk/LayoutTests/ChangeLog        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2016-08-27  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
+
+        adoptcallback
+
+        Add adopted callback for custom elements
+        https://bugs.webkit.org/show_bug.cgi?id=161284
+
+        Reviewed by Antti Koivisto.
+
+        * fast/custom-elements/adopted-callback-expected.txt: Added.
+        * fast/custom-elements/adopted-callback.html: Added.
+        * fast/custom-elements/resources/document-types.js:
+        (const.DocumentTypes.create):
+
</ins><span class="cx"> 2016-08-27  Youenn Fablet  &lt;youenn@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         html/dom/interfaces.html is flaky due to WebSocket test
</span></span></pre></div>
<a id="trunkLayoutTestsfastcustomelementsadoptedcallbackexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/custom-elements/adopted-callback-expected.txt (0 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/custom-elements/adopted-callback-expected.txt                                (rev 0)
+++ trunk/LayoutTests/fast/custom-elements/adopted-callback-expected.txt        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -0,0 +1,59 @@
</span><ins>+
+PASS Inserting a custom element into the owner document must not enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a document of a template element must enqueue and invoke adoptedCallback 
+PASS Moving a custom element from the owner document into a document of a template element must enqueue and invoke adoptedCallback 
+PASS Inserting an ancestor of custom element into a document of a template element must enqueue and invoke adoptedCallback 
+PASS Moving an ancestor of custom element from the owner document into a document of a template element must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a shadow tree in a document of a template element must enqueue and invoke adoptedCallback 
+PASS Inserting the shadow host of a custom element into a document of a template element must enqueue and invoke adoptedCallback 
+PASS Moving the shadow host of a custom element from the owner document into a document of a template element must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a detached shadow tree that belongs to a document of a template element must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a new document must enqueue and invoke adoptedCallback 
+PASS Moving a custom element from the owner document into a new document must enqueue and invoke adoptedCallback 
+PASS Inserting an ancestor of custom element into a new document must enqueue and invoke adoptedCallback 
+PASS Moving an ancestor of custom element from the owner document into a new document must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a shadow tree in a new document must enqueue and invoke adoptedCallback 
+PASS Inserting the shadow host of a custom element into a new document must enqueue and invoke adoptedCallback 
+PASS Moving the shadow host of a custom element from the owner document into a new document must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a detached shadow tree that belongs to a new document must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a cloned document must enqueue and invoke adoptedCallback 
+PASS Moving a custom element from the owner document into a cloned document must enqueue and invoke adoptedCallback 
+PASS Inserting an ancestor of custom element into a cloned document must enqueue and invoke adoptedCallback 
+PASS Moving an ancestor of custom element from the owner document into a cloned document must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a shadow tree in a cloned document must enqueue and invoke adoptedCallback 
+PASS Inserting the shadow host of a custom element into a cloned document must enqueue and invoke adoptedCallback 
+PASS Moving the shadow host of a custom element from the owner document into a cloned document must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a detached shadow tree that belongs to a cloned document must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a document created by createHTMLDocument must enqueue and invoke adoptedCallback 
+PASS Moving a custom element from the owner document into a document created by createHTMLDocument must enqueue and invoke adoptedCallback 
+PASS Inserting an ancestor of custom element into a document created by createHTMLDocument must enqueue and invoke adoptedCallback 
+PASS Moving an ancestor of custom element from the owner document into a document created by createHTMLDocument must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a shadow tree in a document created by createHTMLDocument must enqueue and invoke adoptedCallback 
+PASS Inserting the shadow host of a custom element into a document created by createHTMLDocument must enqueue and invoke adoptedCallback 
+PASS Moving the shadow host of a custom element from the owner document into a document created by createHTMLDocument must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a detached shadow tree that belongs to a document created by createHTMLDocument must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a HTML document created by createDocument must enqueue and invoke adoptedCallback 
+PASS Moving a custom element from the owner document into a HTML document created by createDocument must enqueue and invoke adoptedCallback 
+PASS Inserting an ancestor of custom element into a HTML document created by createDocument must enqueue and invoke adoptedCallback 
+PASS Moving an ancestor of custom element from the owner document into a HTML document created by createDocument must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a shadow tree in a HTML document created by createDocument must enqueue and invoke adoptedCallback 
+PASS Inserting the shadow host of a custom element into a HTML document created by createDocument must enqueue and invoke adoptedCallback 
+PASS Moving the shadow host of a custom element from the owner document into a HTML document created by createDocument must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a detached shadow tree that belongs to a HTML document created by createDocument must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a document in an iframe must enqueue and invoke adoptedCallback 
+PASS Moving a custom element from the owner document into a document in an iframe must enqueue and invoke adoptedCallback 
+PASS Inserting an ancestor of custom element into a document in an iframe must enqueue and invoke adoptedCallback 
+PASS Moving an ancestor of custom element from the owner document into a document in an iframe must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a shadow tree in a document in an iframe must enqueue and invoke adoptedCallback 
+PASS Inserting the shadow host of a custom element into a document in an iframe must enqueue and invoke adoptedCallback 
+PASS Moving the shadow host of a custom element from the owner document into a document in an iframe must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a detached shadow tree that belongs to a document in an iframe must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a HTML document fetched by XHR must enqueue and invoke adoptedCallback 
+PASS Moving a custom element from the owner document into a HTML document fetched by XHR must enqueue and invoke adoptedCallback 
+PASS Inserting an ancestor of custom element into a HTML document fetched by XHR must enqueue and invoke adoptedCallback 
+PASS Moving an ancestor of custom element from the owner document into a HTML document fetched by XHR must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a shadow tree in a HTML document fetched by XHR must enqueue and invoke adoptedCallback 
+PASS Inserting the shadow host of a custom element into a HTML document fetched by XHR must enqueue and invoke adoptedCallback 
+PASS Moving the shadow host of a custom element from the owner document into a HTML document fetched by XHR must enqueue and invoke adoptedCallback 
+PASS Inserting a custom element into a detached shadow tree that belongs to a HTML document fetched by XHR must enqueue and invoke adoptedCallback 

</ins></span></pre></div>
<a id="trunkLayoutTestsfastcustomelementsadoptedcallbackhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/custom-elements/adopted-callback.html (0 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/custom-elements/adopted-callback.html                                (rev 0)
+++ trunk/LayoutTests/fast/custom-elements/adopted-callback.html        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -0,0 +1,136 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;title&gt;Custom Elements: adoptedCallback&lt;/title&gt;
+&lt;meta name=&quot;author&quot; title=&quot;Ryosuke Niwa&quot; href=&quot;mailto:rniwa@webkit.org&quot;&gt;
+&lt;meta name=&quot;assert&quot; content=&quot;adoptedCallback must be enqueued whenever custom element is adopted into a new document&quot;&gt;
+&lt;link rel=&quot;help&quot; href=&quot;https://w3c.github.io/webcomponents/spec/custom/#dfn-connected-callback&quot;&gt;
+&lt;script src=&quot;../../resources/testharness.js&quot;&gt;&lt;/script&gt;
+&lt;script src=&quot;../../resources/testharnessreport.js&quot;&gt;&lt;/script&gt;
+&lt;script src=&quot;./resources/document-types.js&quot;&gt;&lt;/script&gt;
+&lt;link rel='stylesheet' href='../../resources/testharness.css'&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;div id=&quot;log&quot;&gt;&lt;/div&gt;
+&lt;script&gt;
+
+var calls = [];
+class MyCustomElement extends HTMLElement {
+    connectedCallback() { calls.push('connected'); }
+    adoptedCallback(oldDocument, newDocument) { calls.push('adopted'); calls.push(oldDocument); calls.push(newDocument); }
+    disconnectedCallback() { calls.push('disconnected'); }
+}
+customElements.define('my-custom-element', MyCustomElement);
+
+test(function () {
+    var instance = document.createElement('my-custom-element');
+    calls = [];
+    document.body.appendChild(instance);
+    assert_array_equals(calls, ['connected']);
+}, 'Inserting a custom element into the owner document must not enqueue and invoke adoptedCallback');
+
+DocumentTypes.forEach(function (entry) {
+    if (entry.isOwner)
+        return;
+
+    var documentName = entry.name;
+    var getDocument = entry.create;
+
+    promise_test(function () {
+        return getDocument().then(function (doc) {
+            var instance = document.createElement('my-custom-element');
+            calls = [];
+            doc.documentElement.appendChild(instance);
+            assert_array_equals(calls, ['adopted', document, doc, 'connected']);
+        });
+    }, 'Inserting a custom element into a ' + documentName + ' must enqueue and invoke adoptedCallback');
+
+    promise_test(function () {
+        return getDocument().then(function (doc) {
+            var instance = document.createElement('my-custom-element');
+            document.body.appendChild(instance);
+            calls = [];
+            doc.documentElement.appendChild(instance);
+            assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']);
+        });
+    }, 'Moving a custom element from the owner document into a ' + documentName + ' must enqueue and invoke adoptedCallback');
+
+    promise_test(function () {
+        return getDocument().then(function (doc) {
+            var instance = document.createElement('my-custom-element');
+            var parent = document.createElement('div');
+            parent.appendChild(instance);
+            calls = [];
+            doc.documentElement.appendChild(parent);
+            assert_array_equals(calls, ['adopted', document, doc, 'connected']);
+        });
+    }, 'Inserting an ancestor of custom element into a ' + documentName + ' must enqueue and invoke adoptedCallback');
+
+    promise_test(function () {
+        return getDocument().then(function (doc) {
+            var instance = document.createElement('my-custom-element');
+            var parent = document.createElement('div');
+            parent.appendChild(instance);
+            document.body.appendChild(parent);
+            calls = [];
+            doc.documentElement.appendChild(parent);
+            assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']);
+        });
+    }, 'Moving an ancestor of custom element from the owner document into a ' + documentName + ' must enqueue and invoke adoptedCallback');
+
+    promise_test(function () {
+        return getDocument().then(function (doc) {
+            var instance = document.createElement('my-custom-element');
+            var host = doc.createElementNS('http://www.w3.org/1999/xhtml', 'div');
+            var shadowRoot = host.attachShadow({mode: 'closed'});
+            doc.documentElement.appendChild(host);
+
+            calls = [];
+            shadowRoot.appendChild(instance);
+            assert_array_equals(calls, ['adopted', document, doc, 'connected']);
+        });
+    }, 'Inserting a custom element into a shadow tree in a ' + documentName + ' must enqueue and invoke adoptedCallback');
+
+    promise_test(function () {
+        return getDocument().then(function (doc) {
+            var instance = document.createElement('my-custom-element');
+            var host = document.createElement('div');
+            var shadowRoot = host.attachShadow({mode: 'closed'});
+            shadowRoot.appendChild(instance);
+
+            calls = [];
+            doc.documentElement.appendChild(host);
+            assert_array_equals(calls, ['adopted', document, doc, 'connected']);
+        });
+    }, 'Inserting the shadow host of a custom element into a ' + documentName + ' must enqueue and invoke adoptedCallback');
+
+    promise_test(function () {
+        return getDocument().then(function (doc) {
+            var instance = document.createElement('my-custom-element');
+            var host = document.createElement('div');
+            var shadowRoot = host.attachShadow({mode: 'closed'});
+            shadowRoot.appendChild(instance);
+            document.body.appendChild(host);
+
+            calls = [];
+            doc.documentElement.appendChild(host);
+            assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']);
+        });
+    }, 'Moving the shadow host of a custom element from the owner document into a ' + documentName + ' must enqueue and invoke adoptedCallback');
+
+    promise_test(function () {
+        return getDocument().then(function (doc) {
+            var instance = document.createElement('my-custom-element');
+            var host = doc.createElementNS('http://www.w3.org/1999/xhtml', 'div');
+            var shadowRoot = host.attachShadow({mode: 'closed'});
+
+            calls = [];
+            shadowRoot.appendChild(instance);
+            assert_array_equals(calls, ['adopted', document, doc]);
+        });
+    }, 'Inserting a custom element into a detached shadow tree that belongs to a ' + documentName + ' must enqueue and invoke adoptedCallback');
+});
+
+&lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkLayoutTestsfastcustomelementsresourcesdocumenttypesjs"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/fast/custom-elements/resources/document-types.js (205084 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/custom-elements/resources/document-types.js        2016-08-27 21:34:57 UTC (rev 205084)
+++ trunk/LayoutTests/fast/custom-elements/resources/document-types.js        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -1,7 +1,8 @@
</span><span class="cx"> const DocumentTypes = [
</span><span class="cx">     {
</span><span class="cx">         name: 'document',
</span><del>-        create: function () { return Promise.resolve(document); }
</del><ins>+        create: function () { return Promise.resolve(document); },
+        isOwner: true,
</ins><span class="cx">     },
</span><span class="cx">     {
</span><span class="cx">         name: 'document of a template element',
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (205084 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-08-27 21:34:57 UTC (rev 205084)
+++ trunk/Source/WebCore/ChangeLog        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -1,3 +1,41 @@
</span><ins>+2016-08-27  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
+
+        Add adopted callback for custom elements
+        https://bugs.webkit.org/show_bug.cgi?id=161284
+
+        Reviewed by Antti Koivisto.
+
+        Added the support for adoptedCallback: https://dom.spec.whatwg.org/#concept-node-adopt
+        For now, we only support this callback on appendChild.
+
+        Test: fast/custom-elements/adopted-callback.html
+
+        * bindings/js/JSCustomElementInterface.cpp:
+        (WebCore::JSCustomElementInterface::invokeCallback): Added JSDOMGlobalObject* as an argument to the callback so that
+        we can invoke toJS on Document in invokeAdoptedCallback.
+        (WebCore::JSCustomElementInterface::setAdoptedCallback): Added.
+        (WebCore::JSCustomElementInterface::invokeAdoptedCallback): Added.
+        (WebCore::JSCustomElementInterface::setAttributeChangedCallback):
+        * bindings/js/JSCustomElementInterface.h:
+        (WebCore::JSCustomElementInterface::hasConnectedCallback): Added.
+        (WebCore::JSCustomElementInterface::hasDisconnectedCallback): Added.
+        (WebCore::JSCustomElementInterface::hasAdoptedCallback): Added.
+        * bindings/js/JSCustomElementRegistryCustom.cpp:
+        (WebCore::JSCustomElementRegistry::define):
+        * dom/CustomElementReactionQueue.cpp:
+        (WebCore::CustomElementReactionQueueItem::CustomElementReactionQueueItem): Added a variant that takes two documents.
+        (WebCore::CustomElementReactionQueueItem::invoke):
+        (WebCore::CustomElementReactionQueue::enqueueConnectedCallbackIfNeeded): Fixed a bug that this function was always
+        enqueuing a callback even when the interface didn't have connectedCallback. Also, there is no need to check
+        the nullity of the interface since it should never be null.
+        (WebCore::CustomElementReactionQueue::enqueueDisconnectedCallbackIfNeeded): Ditto.
+        (WebCore::CustomElementReactionQueue::enqueueAdoptedCallbackIfNeeded): Added.
+        (WebCore::CustomElementReactionQueue::enqueueAttributeChangedCallbackIfNeeded): Assert that the interface is never
+        null instead of exiting early.
+        * dom/CustomElementReactionQueue.h:
+        * dom/Element.cpp:
+        (WebCore::Element::didMoveToNewDocument): Added a call to enqueueAdoptedCallbackIfNeeded.
+
</ins><span class="cx"> 2016-08-27  Yoshiaki Jitsukawa  &lt;Yoshiaki.Jitsukawa@sony.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Fix the !PLATFORM(WIN) &amp;&amp; USE(CURL) build.
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsJSCustomElementInterfacecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/JSCustomElementInterface.cpp (205084 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/JSCustomElementInterface.cpp        2016-08-27 21:34:57 UTC (rev 205084)
+++ trunk/Source/WebCore/bindings/js/JSCustomElementInterface.cpp        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -150,7 +150,7 @@
</span><span class="cx">     wrappedElement-&gt;setCustomElementIsResolved(*this);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void JSCustomElementInterface::invokeCallback(Element&amp; element, JSObject* callback, const WTF::Function&lt;void(ExecState*, MarkedArgumentBuffer&amp;)&gt;&amp; addArguments)
</del><ins>+void JSCustomElementInterface::invokeCallback(Element&amp; element, JSObject* callback, const WTF::Function&lt;void(ExecState*, JSDOMGlobalObject*, MarkedArgumentBuffer&amp;)&gt;&amp; addArguments)
</ins><span class="cx"> {
</span><span class="cx">     if (!canInvokeCallback())
</span><span class="cx">         return;
</span><span class="lines">@@ -174,7 +174,7 @@
</span><span class="cx">     ASSERT(callType != CallType::None);
</span><span class="cx"> 
</span><span class="cx">     MarkedArgumentBuffer args;
</span><del>-    addArguments(state, args);
</del><ins>+    addArguments(state, globalObject, args);
</ins><span class="cx"> 
</span><span class="cx">     InspectorInstrumentationCookie cookie = JSMainThreadExecState::instrumentFunctionCall(context, callType, callData);
</span><span class="cx"> 
</span><span class="lines">@@ -207,6 +207,19 @@
</span><span class="cx">     invokeCallback(element, m_disconnectedCallback.get());
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void JSCustomElementInterface::setAdoptedCallback(JSC::JSObject* callback)
+{
+    m_adoptedCallback = callback;
+}
+
+void JSCustomElementInterface::invokeAdoptedCallback(Element&amp; element, Document&amp; oldDocument, Document&amp; newDocument)
+{
+    invokeCallback(element, m_adoptedCallback.get(), [&amp;](ExecState* state, JSDOMGlobalObject* globalObject, MarkedArgumentBuffer&amp; args) {
+        args.append(toJS(state, globalObject, oldDocument));
+        args.append(toJS(state, globalObject, newDocument));
+    });
+}
+
</ins><span class="cx"> void JSCustomElementInterface::setAttributeChangedCallback(JSC::JSObject* callback, const Vector&lt;String&gt;&amp; observedAttributes)
</span><span class="cx"> {
</span><span class="cx">     m_attributeChangedCallback = callback;
</span><span class="lines">@@ -217,7 +230,7 @@
</span><span class="cx"> 
</span><span class="cx"> void JSCustomElementInterface::invokeAttributeChangedCallback(Element&amp; element, const QualifiedName&amp; attributeName, const AtomicString&amp; oldValue, const AtomicString&amp; newValue)
</span><span class="cx"> {
</span><del>-    invokeCallback(element, m_attributeChangedCallback.get(), [&amp;](ExecState* state, MarkedArgumentBuffer&amp; args) {
</del><ins>+    invokeCallback(element, m_attributeChangedCallback.get(), [&amp;](ExecState* state, JSDOMGlobalObject*, MarkedArgumentBuffer&amp; args) {
</ins><span class="cx">         args.append(jsStringWithCache(state, attributeName.localName()));
</span><span class="cx">         args.append(jsStringOrNull(state, oldValue));
</span><span class="cx">         args.append(jsStringOrNull(state, newValue));
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsJSCustomElementInterfaceh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/JSCustomElementInterface.h (205084 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/JSCustomElementInterface.h        2016-08-27 21:34:57 UTC (rev 205084)
+++ trunk/Source/WebCore/bindings/js/JSCustomElementInterface.h        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -50,6 +50,7 @@
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="cx"> class DOMWrapperWorld;
</span><ins>+class Document;
</ins><span class="cx"> class Element;
</span><span class="cx"> class JSDOMGlobalObject;
</span><span class="cx"> class MathMLElement;
</span><span class="lines">@@ -68,11 +69,17 @@
</span><span class="cx">     void upgradeElement(Element&amp;);
</span><span class="cx"> 
</span><span class="cx">     void setConnectedCallback(JSC::JSObject*);
</span><ins>+    bool hasConnectedCallback() const { return !!m_connectedCallback; }
</ins><span class="cx">     void invokeConnectedCallback(Element&amp;);
</span><span class="cx"> 
</span><span class="cx">     void setDisconnectedCallback(JSC::JSObject*);
</span><ins>+    bool hasDisconnectedCallback() const { return !!m_disconnectedCallback; }
</ins><span class="cx">     void invokeDisconnectedCallback(Element&amp;);
</span><span class="cx"> 
</span><ins>+    void setAdoptedCallback(JSC::JSObject*);
+    bool hasAdoptedCallback() const { return !!m_adoptedCallback; }
+    void invokeAdoptedCallback(Element&amp;, Document&amp; oldDocument, Document&amp; newDocument);
+
</ins><span class="cx">     void setAttributeChangedCallback(JSC::JSObject* callback, const Vector&lt;String&gt;&amp; observedAttributes);
</span><span class="cx">     bool observesAttribute(const AtomicString&amp; name) const { return m_observedAttributes.contains(name); }
</span><span class="cx">     void invokeAttributeChangedCallback(Element&amp;, const QualifiedName&amp;, const AtomicString&amp; oldValue, const AtomicString&amp; newValue);
</span><span class="lines">@@ -91,12 +98,13 @@
</span><span class="cx"> private:
</span><span class="cx">     JSCustomElementInterface(const QualifiedName&amp;, JSC::JSObject* callback, JSDOMGlobalObject*);
</span><span class="cx"> 
</span><del>-    void invokeCallback(Element&amp;, JSC::JSObject* callback, const WTF::Function&lt;void(JSC::ExecState*, JSC::MarkedArgumentBuffer&amp;)&gt;&amp; addArguments = {});
</del><ins>+    void invokeCallback(Element&amp;, JSC::JSObject* callback, const WTF::Function&lt;void(JSC::ExecState*, JSDOMGlobalObject*, JSC::MarkedArgumentBuffer&amp;)&gt;&amp; addArguments = { });
</ins><span class="cx"> 
</span><span class="cx">     QualifiedName m_name;
</span><span class="cx">     JSC::Weak&lt;JSC::JSObject&gt; m_constructor;
</span><span class="cx">     JSC::Weak&lt;JSC::JSObject&gt; m_connectedCallback;
</span><span class="cx">     JSC::Weak&lt;JSC::JSObject&gt; m_disconnectedCallback;
</span><ins>+    JSC::Weak&lt;JSC::JSObject&gt; m_adoptedCallback;
</ins><span class="cx">     JSC::Weak&lt;JSC::JSObject&gt; m_attributeChangedCallback;
</span><span class="cx">     RefPtr&lt;DOMWrapperWorld&gt; m_isolatedWorld;
</span><span class="cx">     Vector&lt;RefPtr&lt;Element&gt;, 1&gt; m_constructionStack;
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsJSCustomElementRegistryCustomcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/JSCustomElementRegistryCustom.cpp (205084 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/JSCustomElementRegistryCustom.cpp        2016-08-27 21:34:57 UTC (rev 205084)
+++ trunk/Source/WebCore/bindings/js/JSCustomElementRegistryCustom.cpp        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -119,10 +119,11 @@
</span><span class="cx">     if (disconnectedCallback)
</span><span class="cx">         elementInterface-&gt;setDisconnectedCallback(disconnectedCallback);
</span><span class="cx"> 
</span><del>-    // FIXME: Add the support for adoptedCallback.
-    getCustomElementCallback(state, prototypeObject, Identifier::fromString(&amp;vm, &quot;adoptedCallback&quot;));
</del><ins>+    auto* adoptedCallback = getCustomElementCallback(state, prototypeObject, Identifier::fromString(&amp;vm, &quot;adoptedCallback&quot;));
</ins><span class="cx">     if (state.hadException())
</span><span class="cx">         return jsUndefined();
</span><ins>+    if (adoptedCallback)
+        elementInterface-&gt;setAdoptedCallback(adoptedCallback);
</ins><span class="cx"> 
</span><span class="cx">     auto* attributeChangedCallback = getCustomElementCallback(state, prototypeObject, Identifier::fromString(&amp;vm, &quot;attributeChangedCallback&quot;));
</span><span class="cx">     if (state.hadException())
</span></span></pre></div>
<a id="trunkSourceWebCoredomCustomElementReactionQueuecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/CustomElementReactionQueue.cpp (205084 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/CustomElementReactionQueue.cpp        2016-08-27 21:34:57 UTC (rev 205084)
+++ trunk/Source/WebCore/dom/CustomElementReactionQueue.cpp        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -46,6 +46,7 @@
</span><span class="cx">         ElementUpgrade,
</span><span class="cx">         Connected,
</span><span class="cx">         Disconnected,
</span><ins>+        Adopted,
</ins><span class="cx">         AttributeChanged,
</span><span class="cx">     };
</span><span class="cx"> 
</span><span class="lines">@@ -55,6 +56,14 @@
</span><span class="cx">         , m_interface(elementInterface)
</span><span class="cx">     { }
</span><span class="cx"> 
</span><ins>+    CustomElementReactionQueueItem(Element&amp; element, JSCustomElementInterface&amp; elementInterface, Document&amp; oldDocument, Document&amp; newDocument)
+        : m_type(Type::Adopted)
+        , m_element(element)
+        , m_interface(elementInterface)
+        , m_oldDocument(&amp;oldDocument)
+        , m_newDocument(&amp;newDocument)
+    { }
+
</ins><span class="cx">     CustomElementReactionQueueItem(Element&amp; element, JSCustomElementInterface&amp; elementInterface, const QualifiedName&amp; attributeName, const AtomicString&amp; oldValue, const AtomicString&amp; newValue)
</span><span class="cx">         : m_type(Type::AttributeChanged)
</span><span class="cx">         , m_element(element)
</span><span class="lines">@@ -76,6 +85,9 @@
</span><span class="cx">         case Type::Disconnected:
</span><span class="cx">             m_interface-&gt;invokeDisconnectedCallback(m_element.get());
</span><span class="cx">             break;
</span><ins>+        case Type::Adopted:
+            m_interface-&gt;invokeAdoptedCallback(m_element.get(), *m_oldDocument, *m_newDocument);
+            break;
</ins><span class="cx">         case Type::AttributeChanged:
</span><span class="cx">             ASSERT(m_attributeName);
</span><span class="cx">             m_interface-&gt;invokeAttributeChangedCallback(m_element.get(), m_attributeName.value(), m_oldValue, m_newValue);
</span><span class="lines">@@ -87,6 +99,8 @@
</span><span class="cx">     Type m_type;
</span><span class="cx">     Ref&lt;Element&gt; m_element;
</span><span class="cx">     Ref&lt;JSCustomElementInterface&gt; m_interface;
</span><ins>+    RefPtr&lt;Document&gt; m_oldDocument;
+    RefPtr&lt;Document&gt; m_newDocument;
</ins><span class="cx">     Optional&lt;QualifiedName&gt; m_attributeName;
</span><span class="cx">     AtomicString m_oldValue;
</span><span class="cx">     AtomicString m_newValue;
</span><span class="lines">@@ -108,8 +122,10 @@
</span><span class="cx"> 
</span><span class="cx"> void CustomElementReactionQueue::enqueueConnectedCallbackIfNeeded(Element&amp; element)
</span><span class="cx"> {
</span><ins>+    ASSERT(element.isCustomElement());
</ins><span class="cx">     auto* elementInterface = element.customElementInterface();
</span><del>-    if (!elementInterface)
</del><ins>+    ASSERT(elementInterface);
+    if (!elementInterface-&gt;hasConnectedCallback())
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     if (auto* queue = CustomElementReactionStack::ensureCurrentQueue())
</span><span class="lines">@@ -118,8 +134,10 @@
</span><span class="cx"> 
</span><span class="cx"> void CustomElementReactionQueue::enqueueDisconnectedCallbackIfNeeded(Element&amp; element)
</span><span class="cx"> {
</span><ins>+    ASSERT(element.isCustomElement());
</ins><span class="cx">     auto* elementInterface = element.customElementInterface();
</span><del>-    if (!elementInterface)
</del><ins>+    ASSERT(elementInterface);
+    if (!elementInterface-&gt;hasDisconnectedCallback())
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     if (auto* queue = CustomElementReactionStack::ensureCurrentQueue())
</span><span class="lines">@@ -126,10 +144,24 @@
</span><span class="cx">         queue-&gt;m_items.append({CustomElementReactionQueueItem::Type::Disconnected, element, *elementInterface});
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void CustomElementReactionQueue::enqueueAdoptedCallbackIfNeeded(Element&amp; element, Document&amp; oldDocument, Document&amp; newDocument)
+{
+    ASSERT(element.isCustomElement());
+    auto* elementInterface = element.customElementInterface();
+    ASSERT(elementInterface);
+    if (!elementInterface-&gt;hasAdoptedCallback())
+        return;
+
+    if (auto* queue = CustomElementReactionStack::ensureCurrentQueue())
+        queue-&gt;m_items.append({element, *elementInterface, oldDocument, newDocument});
+}
+
</ins><span class="cx"> void CustomElementReactionQueue::enqueueAttributeChangedCallbackIfNeeded(Element&amp; element, const QualifiedName&amp; attributeName, const AtomicString&amp; oldValue, const AtomicString&amp; newValue)
</span><span class="cx"> {
</span><ins>+    ASSERT(element.isCustomElement());
</ins><span class="cx">     auto* elementInterface = element.customElementInterface();
</span><del>-    if (!elementInterface || !elementInterface-&gt;observesAttribute(attributeName.localName()))
</del><ins>+    ASSERT(elementInterface);
+    if (!elementInterface-&gt;observesAttribute(attributeName.localName()))
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     if (auto* queue = CustomElementReactionStack::ensureCurrentQueue())
</span></span></pre></div>
<a id="trunkSourceWebCoredomCustomElementReactionQueueh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/CustomElementReactionQueue.h (205084 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/CustomElementReactionQueue.h        2016-08-27 21:34:57 UTC (rev 205084)
+++ trunk/Source/WebCore/dom/CustomElementReactionQueue.h        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -34,6 +34,7 @@
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="cx"> class CustomElementReactionQueueItem;
</span><ins>+class Document;
</ins><span class="cx"> class Element;
</span><span class="cx"> class JSCustomElementInterface;
</span><span class="cx"> class QualifiedName;
</span><span class="lines">@@ -47,6 +48,7 @@
</span><span class="cx">     static void enqueueElementUpgrade(Element&amp;, JSCustomElementInterface&amp;);
</span><span class="cx">     static void enqueueConnectedCallbackIfNeeded(Element&amp;);
</span><span class="cx">     static void enqueueDisconnectedCallbackIfNeeded(Element&amp;);
</span><ins>+    static void enqueueAdoptedCallbackIfNeeded(Element&amp;, Document&amp; oldDocument, Document&amp; newDocument);
</ins><span class="cx">     static void enqueueAttributeChangedCallbackIfNeeded(Element&amp;, const QualifiedName&amp;, const AtomicString&amp; oldValue, const AtomicString&amp; newValue);
</span><span class="cx"> 
</span><span class="cx">     void invokeAll();
</span></span></pre></div>
<a id="trunkSourceWebCoredomElementcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Element.cpp (205084 => 205085)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Element.cpp        2016-08-27 21:34:57 UTC (rev 205084)
+++ trunk/Source/WebCore/dom/Element.cpp        2016-08-27 22:13:44 UTC (rev 205085)
</span><span class="lines">@@ -1493,6 +1493,11 @@
</span><span class="cx">         if (hasClass())
</span><span class="cx">             attributeChanged(classAttr, nullAtom, getAttribute(classAttr));
</span><span class="cx">     }
</span><ins>+
+#if ENABLE(CUSTOM_ELEMENTS)
+    if (UNLIKELY(isCustomElement()))
+        CustomElementReactionQueue::enqueueAdoptedCallbackIfNeeded(*this, *oldDocument, document());
+#endif
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool Element::hasAttributes() const
</span></span></pre>
</div>
</div>

</body>
</html>