Skip to content

Commit

Permalink
Fix document.write()/writeln() tests for custom elements
Browse files Browse the repository at this point in the history
* 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 web-platform-tests#8528.
  • Loading branch information
EdgarChen authored and domenic committed Dec 5, 2017
1 parent 9c6c509 commit 5cb6c25
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
29 changes: 29 additions & 0 deletions custom-elements/parser/parser-uses-registry-of-owner-document.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<link rel="help" href="https://dom.spec.whatwg.org/#concept-create-element">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/custom-elements-helpers.js"></script>
</head>
<body>
<div id="log"></div>
Expand Down Expand Up @@ -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('<my-custom-element></my-custom-element>');

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('<my-custom-element></my-custom-element>');

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');

</script>
</body>
</html>
12 changes: 12 additions & 0 deletions custom-elements/reactions/Document.html
Original file line number Diff line number Diff line change
Expand Up @@ -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('<custom-element></custom-element>');
assert_array_equals(element.takeLog().types(), ['constructed', 'connected']);
Expand All @@ -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('<custom-element></custom-element>');
assert_array_equals(element.takeLog().types(), ['constructed', 'connected']);
Expand Down

0 comments on commit 5cb6c25

Please sign in to comment.