From 5cb6c2516290bdd9349d37f21e32c435da41a81b Mon Sep 17 00:00:00 2001 From: Edgar Chen Date: Tue, 5 Dec 2017 23:35:25 +0800 Subject: [PATCH] Fix document.write()/writeln() tests for custom elements * Fix custom-elements/reactions/Document.html by adding a defined insertion point. * Add test for calling document.write()/writeln() without a defined insertion point. Closes #8528. --- ...arser-uses-registry-of-owner-document.html | 29 +++++++++++++++++++ custom-elements/reactions/Document.html | 12 ++++++++ 2 files changed, 41 insertions(+) diff --git a/custom-elements/parser/parser-uses-registry-of-owner-document.html b/custom-elements/parser/parser-uses-registry-of-owner-document.html index 478cdcbf4e99af..51e3e5ddfb1c92 100644 --- a/custom-elements/parser/parser-uses-registry-of-owner-document.html +++ b/custom-elements/parser/parser-uses-registry-of-owner-document.html @@ -8,6 +8,7 @@ +
@@ -122,6 +123,34 @@ }); }, 'HTML parser must use the registry of window.document in a document created by XMLHttpRequest'); +test_with_window(function (contentWindow, contentDocument) { + const element = define_custom_element_in_window(contentWindow, 'my-custom-element', []); + // document-open-steps spec doesn't match most browsers; see https://github.com/whatwg/html/issues/1698. + // However, as explained in https://github.com/whatwg/html/issues/1698#issuecomment-298748641 + // the custom element registry will be replaced after document-open-steps. + contentDocument.write(''); + + var instance = contentDocument.querySelector('my-custom-element'); + + assert_true(instance instanceof contentWindow.HTMLElement); + assert_false(instance instanceof element.class); + +}, 'document.write() must not instantiate a custom element without a defined insertion point'); + +test_with_window(function (contentWindow, contentDocument) { + const element = define_custom_element_in_window(contentWindow, 'my-custom-element', []); + // document-open-steps spec doesn't match most browsers; see https://github.com/whatwg/html/issues/1698. + // However, as explained in https://github.com/whatwg/html/issues/1698#issuecomment-298748641 + // the custom element registry will be replaced after document-open-steps. + contentDocument.writeln(''); + + var instance = contentDocument.querySelector('my-custom-element'); + + assert_true(instance instanceof contentWindow.HTMLElement); + assert_false(instance instanceof element.class); + +}, 'document.writeln() must not instantiate a custom element without a defined insertion point'); + diff --git a/custom-elements/reactions/Document.html b/custom-elements/reactions/Document.html index 98c642bcf26474..721ad1f4ced84b 100644 --- a/custom-elements/reactions/Document.html +++ b/custom-elements/reactions/Document.html @@ -129,6 +129,12 @@ }, 'write on Document must enqueue disconnectedCallback when removing a custom element'); test_with_window(function (contentWindow, contentDocument) { + contentWindow.document.open(); + // document.open()'s spec doesn't match most browsers; see https://github.com/whatwg/html/issues/1698. + // However, as explained in https://github.com/whatwg/html/issues/1698#issuecomment-298748641 + // the custom element registry will be replaced after document.open() call, + // So call customElements.define() after that in order to register defintion + // to correct custom elements registry. const element = define_custom_element_in_window(contentWindow, 'custom-element', []); contentWindow.document.write(''); assert_array_equals(element.takeLog().types(), ['constructed', 'connected']); @@ -144,6 +150,12 @@ }, 'writeln on Document must enqueue disconnectedCallback when removing a custom element'); test_with_window(function (contentWindow) { + contentWindow.document.open(); + // document.open()'s spec doesn't match most browsers; see https://github.com/whatwg/html/issues/1698. + // However, as explained in https://github.com/whatwg/html/issues/1698#issuecomment-298748641 + // the custom element registry will be replaced after document.open() call, + // So call customElements.define() after that in order to register defintion + // to correct custom elements registry. const element = define_custom_element_in_window(contentWindow, 'custom-element', []); contentWindow.document.writeln(''); assert_array_equals(element.takeLog().types(), ['constructed', 'connected']);