Source/WebCore/ChangeLog

 12019-04-12 Ryosuke Niwa <rniwa@webkit.org>
 2
 3 Throw TypeError when custom element constructor returns a wrong element or tries to create itself
 4 https://bugs.webkit.org/show_bug.cgi?id=196892
 5
 6 Reviewed by NOBODY (OOPS!).
 7
 8 Throw TypeError instead of InvalidStateError for consistency. This updates WebKit's custom elements
 9 implementation for https://github.com/whatwg/html/pull/4525.
 10
 11 Tests: imported/w3c/web-platform-tests/custom-elements/upgrading/Node-cloneNode.html
 12 imported/w3c/web-platform-tests/custom-elements/upgrading/upgrading-parser-created-element.html
 13
 14 * bindings/js/JSCustomElementInterface.cpp:
 15 (WebCore::JSCustomElementInterface::upgradeElement):
 16 * bindings/js/JSHTMLElementCustom.cpp:
 17 (WebCore::constructJSHTMLElement):
 18
1192019-04-11 Ryosuke Niwa <rniwa@webkit.org>
220
321 WebThread should run at a higher priority than user initiated
244229

Source/WebCore/bindings/js/JSCustomElementInterface.cpp

@@void JSCustomElementInterface::upgradeEl
214214 Element* wrappedElement = JSElement::toWrapped(vm, returnedElement);
215215 if (!wrappedElement || wrappedElement != &element) {
216216 element.setIsFailedCustomElement(*this);
217  reportException(state, createDOMException(state, InvalidStateError, "Custom element constructor failed to upgrade an element"));
 217 reportException(state, createDOMException(state, TypeError, "Custom element constructor returned a wrong element"));
218218 return;
219219 }
220220 element.setIsDefinedCustomElement(*this);
244228

Source/WebCore/bindings/js/JSHTMLElementCustom.cpp

@@EncodedJSValue JSC_HOST_CALL constructJS
8989
9090 Element* elementToUpgrade = elementInterface->lastElementInConstructionStack();
9191 if (!elementToUpgrade) {
92  throwInvalidStateError(exec, scope, "Cannot instantiate a custom element inside its own constructor during upgrades"_s);
 92 throwTypeError(&exec, scope, "Cannot instantiate a custom element inside its own constructor during upgrades"_s);
9393 return JSValue::encode(jsUndefined());
9494 }
9595
244228

LayoutTests/imported/w3c/ChangeLog

 12019-04-12 Ryosuke Niwa <rniwa@webkit.org>
 2
 3 Throw TypeError when custom element constructor returns a wrong element or tries to create itself
 4 https://bugs.webkit.org/show_bug.cgi?id=196892
 5
 6 Reviewed by NOBODY (OOPS!).
 7
 8 Update the tests according to https://github.com/whatwg/html/pull/4525.
 9
 10 * web-platform-tests/custom-elements/upgrading/Node-cloneNode-expected.txt:
 11 * web-platform-tests/custom-elements/upgrading/Node-cloneNode.html:
 12 * web-platform-tests/custom-elements/upgrading/upgrading-parser-created-element-expected.txt:
 13 * web-platform-tests/custom-elements/upgrading/upgrading-parser-created-element.html:
 14
1152019-04-12 Rob Buis <rbuis@igalia.com>
216
317 Import WPT preload tests
244237

LayoutTests/imported/w3c/web-platform-tests/custom-elements/upgrading/Node-cloneNode-expected.txt

@@FAIL Node.prototype.cloneNode(false) mus
55PASS Node.prototype.cloneNode(false) must be able to clone a custom element inside an iframe
66PASS Node.prototype.cloneNode(true) must be able to clone a descendent custom element
77PASS Node.prototype.cloneNode(true) must set parentNode, previousSibling, and nextSibling before upgrading custom elements
8 PASS HTMLElement constructor must throw an InvalidStateError when the top of the construction stack is marked AlreadyConstructed due to a custom element constructor constructing itself after super() call
9 PASS HTMLElement constructor must throw an InvalidStateError when the top of the construction stack is marked AlreadyConstructed due to a custom element constructor constructing itself before super() call
10 PASS Upgrading a custom element must throw InvalidStateError when the custom element's constructor returns another element
 8PASS HTMLElement constructor must throw an TypeError when the top of the construction stack is marked AlreadyConstructed due to a custom element constructor constructing itself after super() call
 9PASS HTMLElement constructor must throw an TypeError when the top of the construction stack is marked AlreadyConstructed due to a custom element constructor constructing itself before super() call
 10PASS Upgrading a custom element must throw TypeError when the custom element's constructor returns another element
1111PASS Inserting an element must not try to upgrade a custom element when it had already failed to upgrade once
1212
244228

LayoutTests/imported/w3c/web-platform-tests/custom-elements/upgrading/Node-cloneNode.html

@@test(function () {
165165 var uncaughtError;
166166 window.onerror = function (message, url, lineNumber, columnNumber, error) { uncaughtError = error; return true; }
167167 instance.cloneNode(false);
168  assert_equals(uncaughtError.name, 'InvalidStateError');
169 }, 'HTMLElement constructor must throw an InvalidStateError when the top of the construction stack is marked AlreadyConstructed'
 168 assert_equals(uncaughtError.name, 'TypeError');
 169}, 'HTMLElement constructor must throw an TypeError when the top of the construction stack is marked AlreadyConstructed'
170170 + ' due to a custom element constructor constructing itself after super() call');
171171
172172test(function () {

@@test(function () {
183183 var uncaughtError;
184184 window.onerror = function (message, url, lineNumber, columnNumber, error) { uncaughtError = error; return true; }
185185 instance.cloneNode(false);
186  assert_equals(uncaughtError.name, 'InvalidStateError');
187 }, 'HTMLElement constructor must throw an InvalidStateError when the top of the construction stack is marked AlreadyConstructed'
 186 assert_equals(uncaughtError.name, 'TypeError');
 187}, 'HTMLElement constructor must throw an TypeError when the top of the construction stack is marked AlreadyConstructed'
188188 + ' due to a custom element constructor constructing itself before super() call');
189189
190190test(function () {

@@test(function () {
203203 var uncaughtError;
204204 window.onerror = function (message, url, lineNumber, columnNumber, error) { uncaughtError = error; return true; }
205205 instance.cloneNode(false);
206  assert_equals(uncaughtError.name, 'InvalidStateError');
207 }, 'Upgrading a custom element must throw InvalidStateError when the custom element\'s constructor returns another element');
 206 assert_equals(uncaughtError.name, 'TypeError');
 207}, 'Upgrading a custom element must throw TypeError when the custom element\'s constructor returns another element');
208208
209209test(function () {
210210 var instance = document.createElement('my-custom-element-throw-exception');
244228

LayoutTests/imported/w3c/web-platform-tests/custom-elements/upgrading/upgrading-parser-created-element-expected.txt

11
22PASS Element.prototype.createElement must add an unresolved custom element to the upgrade candidates map
3 PASS HTMLElement constructor must throw an InvalidStateError when the top of the construction stack is marked AlreadyConstructed due to a custom element constructor constructing itself after super() call
4 PASS HTMLElement constructor must throw an InvalidStateError when the top of the construction stack is marked AlreadyConstructed due to a custom element constructor constructing itself before super() call
5 PASS Upgrading a custom element must throw an InvalidStateError when the returned element is not SameValue as the upgraded element
 3PASS HTMLElement constructor must throw an TypeError when the top of the construction stack is marked AlreadyConstructed due to a custom element constructor constructing itself after super() call
 4PASS HTMLElement constructor must throw an TypeError when the top of the construction stack is marked AlreadyConstructed due to a custom element constructor constructing itself before super() call
 5PASS Upgrading a custom element must throw an TypeError when the returned element is not SameValue as the upgraded element
 6PASS Upgrading a custom element whose constructor returns a Text node must throw
 7PASS Upgrading a custom element whose constructor returns an Element must throw
68
244228

LayoutTests/imported/w3c/web-platform-tests/custom-elements/upgrading/upgrading-parser-created-element.html

1515<instantiates-itself-before-super></instantiates-itself-before-super>
1616<my-other-element id="instance"></my-other-element>
1717<my-other-element id="otherInstance"></my-other-element>
 18<not-an-element></not-an-element>
 19<not-an-html-element></not-an-html-element>
1820<script>
1921
2022setup({allow_uncaught_exception:true});

@@test(function () {
4850 var uncaughtError;
4951 window.onerror = function (message, url, lineNumber, columnNumber, error) { uncaughtError = error; return true; }
5052 customElements.define('instantiates-itself-after-super', InstantiatesItselfAfterSuper);
51  assert_equals(uncaughtError.name, 'InvalidStateError');
52 }, 'HTMLElement constructor must throw an InvalidStateError when the top of the construction stack is marked AlreadyConstructed'
 53 assert_equals(uncaughtError.name, 'TypeError');
 54}, 'HTMLElement constructor must throw an TypeError when the top of the construction stack is marked AlreadyConstructed'
5355 + ' due to a custom element constructor constructing itself after super() call');
5456
5557test(function () {

@@test(function () {
6466 var uncaughtError;
6567 window.onerror = function (message, url, lineNumber, columnNumber, error) { uncaughtError = error; return true; }
6668 customElements.define('instantiates-itself-before-super', InstantiatesItselfBeforeSuper);
67  assert_equals(uncaughtError.name, 'InvalidStateError');
68 }, 'HTMLElement constructor must throw an InvalidStateError when the top of the construction stack is marked AlreadyConstructed'
 69 assert_equals(uncaughtError.name, 'TypeError');
 70}, 'HTMLElement constructor must throw an TypeError when the top of the construction stack is marked AlreadyConstructed'
6971 + ' due to a custom element constructor constructing itself before super() call');
7072
7173test(function () {

@@test(function () {
8587 var uncaughtError;
8688 window.onerror = function (message, url, lineNumber, columnNumber, error) { uncaughtError = error; return true; }
8789 customElements.define('my-other-element', MyOtherElement);
88  assert_equals(uncaughtError.name, 'InvalidStateError');
 90 assert_equals(uncaughtError.name, 'TypeError');
8991
9092 assert_true(document.createElement('my-other-element') instanceof MyOtherElement,
9193 'Upgrading of custom elements must happen after the definition was added to the registry.');
9294
93 }, 'Upgrading a custom element must throw an InvalidStateError when the returned element is not SameValue as the upgraded element');
 95}, 'Upgrading a custom element must throw an TypeError when the returned element is not SameValue as the upgraded element');
 96
 97test(() => {
 98 class NotAnElement extends HTMLElement {
 99 constructor() {
 100 return new Text();
 101 }
 102 }
 103
 104 let uncaughtError;
 105 window.onerror = function (message, url, lineNumber, columnNumber, error) { uncaughtError = error; return true; }
 106 customElements.define("not-an-element", NotAnElement);
 107 assert_equals(uncaughtError.name, "TypeError");
 108}, "Upgrading a custom element whose constructor returns a Text node must throw");
 109
 110test(() => {
 111 class NotAnHTMLElement extends HTMLElement {
 112 constructor() {
 113 return document.createElementNS("", "test");
 114 }
 115 }
 116
 117 let uncaughtError;
 118 window.onerror = function (message, url, lineNumber, columnNumber, error) { uncaughtError = error; return true; }
 119 customElements.define("not-an-html-element", NotAnHTMLElement);
 120 assert_equals(uncaughtError.name, "TypeError");
 121}, "Upgrading a custom element whose constructor returns an Element must throw");
94122
95123</script>
96124</body>
244228