<!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>[205261] 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/205261">205261</a></dd>
<dt>Author</dt> <dd>rniwa@webkit.org</dd>
<dt>Date</dt> <dd>2016-08-31 12:31:11 -0700 (Wed, 31 Aug 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add the check for reentrancy to CustomElementRegistry
https://bugs.webkit.org/show_bug.cgi?id=161423

Reviewed by Antti Koivisto.

Source/WebCore:

Added the &quot;element definition is running&quot; flag to JSCustomElementRegistry:
https://html.spec.whatwg.org/multipage/scripting.html#element-definition-is-running

And added an exception for when this flag is set during JSCustomElementRegistry::define:
https://html.spec.whatwg.org/multipage/scripting.html#dom-customelementregistry-define

Tests: fast/custom-elements/CustomElementRegistry.html

* bindings/js/JSCustomElementRegistryCustom.cpp:
(WebCore::JSCustomElementRegistry::define): Throw NotSupportedError when m_elementDefinitionIsRunning is true.
* dom/CustomElementRegistry.h:
(WebCore::CustomElementRegistry::elementDefinitionIsRunning): Added.

LayoutTests:

Add test cases for reentrancy during customElements.define.

* fast/custom-elements/CustomElementRegistry-expected.txt:
* fast/custom-elements/CustomElementRegistry.html:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsfastcustomelementsCustomElementRegistryexpectedtxt">trunk/LayoutTests/fast/custom-elements/CustomElementRegistry-expected.txt</a></li>
<li><a href="#trunkLayoutTestsfastcustomelementsCustomElementRegistryhtml">trunk/LayoutTests/fast/custom-elements/CustomElementRegistry.html</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorebindingsjsJSCustomElementRegistryCustomcpp">trunk/Source/WebCore/bindings/js/JSCustomElementRegistryCustom.cpp</a></li>
<li><a href="#trunkSourceWebCoredomCustomElementRegistryh">trunk/Source/WebCore/dom/CustomElementRegistry.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (205260 => 205261)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2016-08-31 19:15:32 UTC (rev 205260)
+++ trunk/LayoutTests/ChangeLog        2016-08-31 19:31:11 UTC (rev 205261)
</span><span class="lines">@@ -1,3 +1,15 @@
</span><ins>+2016-08-31  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
+
+        Add the check for reentrancy to CustomElementRegistry
+        https://bugs.webkit.org/show_bug.cgi?id=161423
+
+        Reviewed by Antti Koivisto.
+
+        Add test cases for reentrancy during customElements.define.
+
+        * fast/custom-elements/CustomElementRegistry-expected.txt:
+        * fast/custom-elements/CustomElementRegistry.html:
+
</ins><span class="cx"> 2016-08-31  Chris Dumez  &lt;cdumez@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Object.getPrototypeOf() should return null cross-origin
</span></span></pre></div>
<a id="trunkLayoutTestsfastcustomelementsCustomElementRegistryexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/fast/custom-elements/CustomElementRegistry-expected.txt (205260 => 205261)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/custom-elements/CustomElementRegistry-expected.txt        2016-08-31 19:15:32 UTC (rev 205260)
+++ trunk/LayoutTests/fast/custom-elements/CustomElementRegistry-expected.txt        2016-08-31 19:31:11 UTC (rev 205261)
</span><span class="lines">@@ -1,9 +1,14 @@
</span><span class="cx"> 
</span><span class="cx"> PASS CustomElementRegistry interface must have define as a method 
</span><ins>+PASS customElements.define must throw when the element interface is not a constructor 
</ins><span class="cx"> PASS customElements.define must throw with an invalid name 
</span><span class="cx"> PASS customElements.define must throw when there is already a custom element of the same name 
</span><del>-PASS customElements.define must throw when there is already a custom element with the same class 
-PASS customElements.define must throw when the element interface is not a constructor 
</del><ins>+PASS customElements.define must throw a NotSupportedError when there is already a custom element with the same class 
+PASS customElements.define must throw a NotSupportedError when element definition is running flag is set 
+PASS customElements.define must check IsConstructor on the constructor before checking the element definition is running flag 
+PASS customElements.define must validate the custom element name before checking the element definition is running flag 
+PASS customElements.define must not throw when defining another custom element in a different global object during Get(constructor, &quot;prototype&quot;) 
+PASS Custom Elements: CustomElementRegistry interface 
</ins><span class="cx"> PASS customElements.define must get &quot;prototype&quot; property of the constructor 
</span><span class="cx"> PASS customElements.define must rethrow an exception thrown while getting &quot;prototype&quot; property of the constructor 
</span><span class="cx"> PASS customElements.define must throw when &quot;prototype&quot; property of the constructor is not an object 
</span></span></pre></div>
<a id="trunkLayoutTestsfastcustomelementsCustomElementRegistryhtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/fast/custom-elements/CustomElementRegistry.html (205260 => 205261)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/custom-elements/CustomElementRegistry.html        2016-08-31 19:15:32 UTC (rev 205260)
+++ trunk/LayoutTests/fast/custom-elements/CustomElementRegistry.html        2016-08-31 19:31:11 UTC (rev 205261)
</span><span class="lines">@@ -18,6 +18,17 @@
</span><span class="cx"> }, 'CustomElementRegistry interface must have define as a method');
</span><span class="cx"> 
</span><span class="cx"> test(function () {
</span><ins>+    assert_throws({'name': 'TypeError'}, function () { customElements.define('badname', 1); },
+        'customElements.define must throw a TypeError when the element interface is a number');
+    assert_throws({'name': 'TypeError'}, function () { customElements.define('badname', '123'); },
+        'customElements.define must throw a TypeError when the element interface is a string');
+    assert_throws({'name': 'TypeError'}, function () { customElements.define('badname', {}); },
+        'customElements.define must throw a TypeError when the element interface is an object');
+    assert_throws({'name': 'TypeError'}, function () { customElements.define('badname', []); },
+        'customElements.define must throw a TypeError when the element interface is an array');
+}, 'customElements.define must throw when the element interface is not a constructor');
+
+test(function () {
</ins><span class="cx">     class MyCustomElement extends HTMLElement {};
</span><span class="cx"> 
</span><span class="cx">     assert_throws({'name': 'SyntaxError'}, function () { customElements.define(null, MyCustomElement); },
</span><span class="lines">@@ -49,11 +60,19 @@
</span><span class="cx"> 
</span><span class="cx"> test(function () {
</span><span class="cx">     class SomeCustomElement extends HTMLElement {};
</span><del>-    class OtherCustomElement extends HTMLElement {};
</del><span class="cx"> 
</span><ins>+    var calls = [];
+    var OtherCustomElement = new Proxy(class extends HTMLElement {}, {
+        get: function (target, name) {
+            calls.push(name);
+            return target[name];
+        }
+    })
+
</ins><span class="cx">     customElements.define('some-custom-element', SomeCustomElement);
</span><span class="cx">     assert_throws({'name': 'NotSupportedError'}, function () { customElements.define('some-custom-element', OtherCustomElement); },
</span><span class="cx">         'customElements.define must throw a NotSupportedError if the specified tag name is already used');
</span><ins>+    assert_array_equals(calls, [], 'customElements.define must validate the custom element name before getting the prototype of the constructor');
</ins><span class="cx"> 
</span><span class="cx"> }, 'customElements.define must throw when there is already a custom element of the same name');
</span><span class="cx"> 
</span><span class="lines">@@ -64,21 +83,114 @@
</span><span class="cx">     assert_throws({'name': 'NotSupportedError'}, function () { customElements.define('some-other-element', AnotherCustomElement); },
</span><span class="cx">         'customElements.define must throw a NotSupportedError if the specified class already defines an element');
</span><span class="cx"> 
</span><del>-}, 'customElements.define must throw when there is already a custom element with the same class');
</del><ins>+}, 'customElements.define must throw a NotSupportedError when there is already a custom element with the same class');
</ins><span class="cx"> 
</span><span class="cx"> test(function () {
</span><del>-    assert_throws({'name': 'TypeError'}, function () { customElements.define('invalid-element', 1); },
-        'customElements.define must throw a TypeError when the element interface is a number');
-    assert_throws({'name': 'TypeError'}, function () { customElements.define('invalid-element', '123'); },
-        'customElements.define must throw a TypeError when the element interface is a string');
-    assert_throws({'name': 'TypeError'}, function () { customElements.define('invalid-element', {}); },
-        'customElements.define must throw a TypeError when the element interface is an object');
-    assert_throws({'name': 'TypeError'}, function () { customElements.define('invalid-element', []); },
-        'customElements.define must throw a TypeError when the element interface is an array');
-}, 'customElements.define must throw when the element interface is not a constructor');
</del><ins>+    var outerCalls = [];
+    var OuterCustomElement = new Proxy(class extends HTMLElement { }, {
+        get: function (target, name) {
+            outerCalls.push(name);
+            customElements.define('inner-custom-element', InnerCustomElement);
+            return target[name];
+        }
+    });
+    var innerCalls = [];
+    var InnerCustomElement = new Proxy(class extends HTMLElement { }, {
+        get: function (target, name) {
+            outerCalls.push(name);
+            return target[name];
+        }
+    });
</ins><span class="cx"> 
</span><ins>+    assert_throws({'name': 'NotSupportedError'}, function () { customElements.define('outer-custom-element', OuterCustomElement); },
+        'customElements.define must throw a NotSupportedError if the specified class already defines an element');
+    assert_array_equals(outerCalls, ['prototype'], 'customElements.define must get &quot;prototype&quot;');
+    assert_array_equals(innerCalls, [],
+        'customElements.define must throw a NotSupportedError when element definition is running flag is set'
+        + ' before getting the prototype of the constructor');
+
+}, 'customElements.define must throw a NotSupportedError when element definition is running flag is set');
+
</ins><span class="cx"> test(function () {
</span><span class="cx">     var calls = [];
</span><ins>+    var ElementWithBadInnerConstructor = new Proxy(class extends HTMLElement { }, {
+        get: function (target, name) {
+            calls.push(name);
+            customElements.define('inner-custom-element', 1);
+            return target[name];
+        }
+    });
+
+    assert_throws({'name': 'TypeError'}, function () {
+        customElements.define('element-with-bad-inner-constructor', ElementWithBadInnerConstructor);
+    }, 'customElements.define must throw a NotSupportedError if IsConstructor(constructor) is false');
+
+    assert_array_equals(calls, ['prototype'], 'customElements.define must get &quot;prototype&quot;');
+}, 'customElements.define must check IsConstructor on the constructor before checking the element definition is running flag');
+
+test(function () {
+    var calls = [];
+    var ElementWithBadInnerName = new Proxy(class extends HTMLElement { }, {
+        get: function (target, name) {
+            calls.push(name);
+            customElements.define('badname', class extends HTMLElement {});
+            return target[name];
+        }
+    });
+
+    assert_throws({'name': 'SyntaxError'}, function () {
+        customElements.define('element-with-bad-inner-name', ElementWithBadInnerName);
+    }, 'customElements.define must throw a SyntaxError if the specified name is not a valid custom element name');
+
+    assert_array_equals(calls, ['prototype'], 'customElements.define must get &quot;prototype&quot;');
+}, 'customElements.define must validate the custom element name before checking the element definition is running flag');
+
+(function () {
+    var testCase = async_test('customElements.define must not throw'
+        +' when defining another custom element in a different global object during Get(constructor, &quot;prototype&quot;)', {timeout: 100});
+
+    var iframe = document.createElement('iframe');
+    iframe.onload = function () {
+        testCase.step(function () {
+            var InnerCustomElement = class extends iframe.contentWindow.HTMLElement {};
+            var calls = [];
+            var proxy = new Proxy(class extends HTMLElement { }, {
+                get: function (target, name) {
+                    calls.push(name);
+                    iframe.contentWindow.customElements.define('another-custom-element', InnerCustomElement);
+                    return target[name];
+                }
+            })
+            customElements.define('element-with-inner-element-define', proxy);
+            assert_array_equals(calls, ['prototype'], 'customElements.define must get &quot;prototype&quot;');
+            assert_true(iframe.contentDocument.createElement('another-custom-element') instanceof InnerCustomElement);
+        });
+        document.body.removeChild(iframe);
+        testCase.done();
+    }
+
+    document.body.appendChild(iframe);
+})();
+
+test(function () {
+    var calls = [];
+    var ElementWithBadInnerName = new Proxy(class extends HTMLElement { }, {
+        get: function (target, name) {
+            calls.push(name);
+            customElements.define('badname', class extends HTMLElement {});
+            return target[name];
+        }
+    });
+
+    assert_throws({'name': 'SyntaxError'}, function () {
+        customElements.define('element-with-bad-inner-name', ElementWithBadInnerName);
+    }, 'customElements.define must throw a SyntaxError if the specified name is not a valid custom element name');
+
+    assert_array_equals(calls, ['prototype'], 'customElements.define must get &quot;prototype&quot;');
+}, '');
+
+test(function () {
+    var calls = [];
</ins><span class="cx">     var proxy = new Proxy(class extends HTMLElement { }, {
</span><span class="cx">         get: function (target, name) {
</span><span class="cx">             calls.push(name);
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (205260 => 205261)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-08-31 19:15:32 UTC (rev 205260)
+++ trunk/Source/WebCore/ChangeLog        2016-08-31 19:31:11 UTC (rev 205261)
</span><span class="lines">@@ -1,3 +1,23 @@
</span><ins>+2016-08-31  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
+
+        Add the check for reentrancy to CustomElementRegistry
+        https://bugs.webkit.org/show_bug.cgi?id=161423
+
+        Reviewed by Antti Koivisto.
+
+        Added the &quot;element definition is running&quot; flag to JSCustomElementRegistry:
+        https://html.spec.whatwg.org/multipage/scripting.html#element-definition-is-running
+
+        And added an exception for when this flag is set during JSCustomElementRegistry::define:
+        https://html.spec.whatwg.org/multipage/scripting.html#dom-customelementregistry-define
+
+        Tests: fast/custom-elements/CustomElementRegistry.html
+
+        * bindings/js/JSCustomElementRegistryCustom.cpp:
+        (WebCore::JSCustomElementRegistry::define): Throw NotSupportedError when m_elementDefinitionIsRunning is true.
+        * dom/CustomElementRegistry.h:
+        (WebCore::CustomElementRegistry::elementDefinitionIsRunning): Added.
+
</ins><span class="cx"> 2016-08-30  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Avoid using strong reference in JSDOMPromiseā€™s DeferredWrapper
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsJSCustomElementRegistryCustomcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/JSCustomElementRegistryCustom.cpp (205260 => 205261)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/JSCustomElementRegistryCustom.cpp        2016-08-31 19:15:32 UTC (rev 205260)
+++ trunk/Source/WebCore/bindings/js/JSCustomElementRegistryCustom.cpp        2016-08-31 19:31:11 UTC (rev 205261)
</span><span class="lines">@@ -92,6 +92,13 @@
</span><span class="cx">     // https://github.com/w3c/webcomponents/issues/545
</span><span class="cx"> 
</span><span class="cx">     CustomElementRegistry&amp; registry = wrapped();
</span><ins>+
+    if (registry.elementDefinitionIsRunning()) {
+        throwNotSupportedError(state, scope, ASCIILiteral(&quot;Cannot define a custom element while defining another custom element&quot;));
+        return jsUndefined();
+    }
+    TemporaryChange&lt;bool&gt; change(registry.elementDefinitionIsRunning(), true);
+
</ins><span class="cx">     if (registry.findInterface(localName)) {
</span><span class="cx">         throwNotSupportedError(state, scope, ASCIILiteral(&quot;Cannot define multiple custom elements with the same tag name&quot;));
</span><span class="cx">         return jsUndefined();
</span></span></pre></div>
<a id="trunkSourceWebCoredomCustomElementRegistryh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/CustomElementRegistry.h (205260 => 205261)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/CustomElementRegistry.h        2016-08-31 19:15:32 UTC (rev 205260)
+++ trunk/Source/WebCore/dom/CustomElementRegistry.h        2016-08-31 19:31:11 UTC (rev 205261)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> 
</span><span class="cx"> #include &quot;QualifiedName.h&quot;
</span><span class="cx"> #include &lt;wtf/HashMap.h&gt;
</span><ins>+#include &lt;wtf/TemporaryChange.h&gt;
</ins><span class="cx"> #include &lt;wtf/text/AtomicString.h&gt;
</span><span class="cx"> #include &lt;wtf/text/AtomicStringHash.h&gt;
</span><span class="cx"> 
</span><span class="lines">@@ -41,6 +42,7 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><ins>+class CustomElementRegistry;
</ins><span class="cx"> class Element;
</span><span class="cx"> class JSCustomElementInterface;
</span><span class="cx"> class QualifiedName;
</span><span class="lines">@@ -53,6 +55,8 @@
</span><span class="cx">     void addElementDefinition(Ref&lt;JSCustomElementInterface&gt;&amp;&amp;);
</span><span class="cx">     void addUpgradeCandidate(Element&amp;);
</span><span class="cx"> 
</span><ins>+    bool&amp; elementDefinitionIsRunning() { return m_elementDefinitionIsRunning; }
+
</ins><span class="cx">     JSCustomElementInterface* findInterface(const QualifiedName&amp;) const;
</span><span class="cx">     JSCustomElementInterface* findInterface(const AtomicString&amp;) const;
</span><span class="cx">     JSCustomElementInterface* findInterface(const JSC::JSObject*) const;
</span><span class="lines">@@ -66,6 +70,10 @@
</span><span class="cx">     HashMap&lt;AtomicString, Vector&lt;RefPtr&lt;Element&gt;&gt;&gt; m_upgradeCandidatesMap;
</span><span class="cx">     HashMap&lt;AtomicString, Ref&lt;JSCustomElementInterface&gt;&gt; m_nameMap;
</span><span class="cx">     HashMap&lt;const JSC::JSObject*, JSCustomElementInterface*&gt; m_constructorMap;
</span><ins>+
+    bool m_elementDefinitionIsRunning { false };
+
+    friend class ElementDefinitionIsRunningTemporaryChange;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>