Bug 1633056 [wpt PR 23243] - Don't serialize closed shadow roots with getInnerHTML(), a=testonly

Automatic update from web-platform-tests
Don't serialize closed shadow roots with getInnerHTML()

This CL corrects the behavior of getInnerHTML() so that closed
shadow roots are not serialized. It also adds a test of the
getInnerHTML() function.

Bug: 1042130
Change-Id: Iac214b16799cee45bcd90be8f618cc5560eb1db4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2166383
Commit-Queue: Kouhei Ueno <kouhei@chromium.org>
Reviewed-by: Kouhei Ueno <kouhei@chromium.org>
Auto-Submit: Mason Freed <masonfreed@chromium.org>
Cr-Commit-Position: refs/heads/master@{#763236}

--

wpt-commits: c88ed219c1a65af9f83844181b84111e9462f8b6
wpt-pr: 23243
This commit is contained in:
Mason Freed 2020-04-28 11:48:14 +00:00
parent de880f5b92
commit fe699ee239
3 changed files with 65 additions and 3 deletions

View File

@ -70,12 +70,10 @@ function testElementType(allowed, nochildren, elementType, mode, delegatesFocus)
}
function runAllTests() {
const noChildElements = ['iframe','noscript','script','select','style','textarea','title'];
const noCheck = ['body'];
const safelisted = ATTACHSHADOW_SAFELISTED_ELEMENTS.filter(el => !noCheck.includes(el));
const disallowed = ATTACHSHADOW_DISALLOWED_ELEMENTS.filter(el => !noCheck.includes(el));
const minimumKnownElements = 113; // We should have at least this many elements in the lists from shadow-dom-utils.js.
assert_true(safelisted.length + disallowed.length + noChildElements.length >= minimumKnownElements,'All element types should be tested');
const noChildElements = ['iframe','noscript','script','select','style','textarea','title'];
for (let delegatesFocus of [false, true]) {
for (let mode of ['open', 'closed', 'invalid']) {
for (let elementName of safelisted) {

View File

@ -0,0 +1,61 @@
<!DOCTYPE html>
<title>getInnerHTML </title>
<link rel='author' title='Mason Freed' href='mailto:masonfreed@chromium.org'>
<link rel='help' href='https://github.com/whatwg/dom/issues/831'>
<script src='/resources/testharness.js'></script>
<script src='/resources/testharnessreport.js'></script>
<script src='../resources/shadow-dom-utils.js'></script>
<body>
<script>
function testElementType(allowsShadowDom, elementType, mode, delegatesFocus) {
var t = test(t => {
// Create and attach element
const wrapper = document.createElement('div');
t.add_cleanup(function() { wrapper.remove(); });
document.body.appendChild(wrapper);
const element = document.createElement(elementType);
wrapper.appendChild(element);
const isOpen = mode === 'open';
if (allowsShadowDom) {
let correctShadowHtml = isOpen ? `<template shadowroot="${mode}"><slot></slot></template>` : '';
let correctHtml = `<${elementType}>${correctShadowHtml}</${elementType}>`;
const shadowRoot = element.attachShadow({mode: mode, delegatesFocus: delegatesFocus});
shadowRoot.appendChild(document.createElement('slot'));
assert_equals(wrapper.getInnerHTML(true),correctHtml);
} else {
// For non-shadow hosts, getInnerHTML(true) should also match .innerHTML
assert_equals(wrapper.getInnerHTML(true),wrapper.innerHTML);
}
// Either way, make sure getInnerHTML() matches .innerHTML
assert_equals(wrapper.getInnerHTML(false),wrapper.innerHTML,'getInnerHTML() with includeShadowRoots false should return the same as .innerHTML');
assert_equals(wrapper.getInnerHTML(),wrapper.innerHTML,'getInnerHTML() with no arguments should return the same as .innerHTML');
}, `getInnerHTML() on <${elementType}>${allowsShadowDom ? `, with mode=${mode}, delegatesFocus=${delegatesFocus}.` : ''}`);
}
function runAllTests() {
const allElements = HTML5_ELEMENT_NAMES;
const safelisted = ATTACHSHADOW_SAFELISTED_ELEMENTS;
const minimumKnownElements = 107; // We should have at least this many elements in the lists from shadow-dom-utils.js.
assert_true(allElements.length >= minimumKnownElements,'All element types should be tested');
for (let elementName of allElements) {
const allowsShadowDom = safelisted.includes(elementName);
if (allowsShadowDom) {
for (let delegatesFocus of [false, true]) {
for (let mode of ['open', 'closed']) {
testElementType(true, elementName, mode, delegatesFocus);
}
}
} else {
testElementType(false, elementName);
}
}
}
runAllTests();
</script>

View File

@ -20,11 +20,13 @@ function myObserver(mutationsList, observer) {
assert_in_array(shadowroot, ['open','closed'], 'Declarative template should have shadowroot attribute');
assert_equals(n.content, null, 'Declarative template content should be null');
assert_equals(n.innerHTML, "", 'Declarative template innerHTML should be empty');
assert_equals(n.getInnerHTML(true), "", 'Declarative template getInnerHTML() should be empty');
// Make sure removing the shadowroot attribute doesn't affect things.
n.removeAttribute('shadowroot');
assert_equals(n.content, null, 'Declarative template content should *still* be null');
assert_equals(n.innerHTML, "", 'Declarative template innerHTML should *still* be empty');
assert_equals(n.getInnerHTML(true), "", 'Declarative template getInnerHTML() should *still* be empty');
break;
case 'noroot':
// Make sure adding 'shadowroot' attribute doesn't trigger a shadow root,
@ -32,6 +34,7 @@ function myObserver(mutationsList, observer) {
n.setAttribute('shadowroot','open');
assert_not_equals(n.content, null, 'Regular template should have content, even after adding shadowroot attribute');
assert_not_equals(n.innerHTML, "", 'Regular template should have innerHTML, even after adding shadowroot attribute');
assert_not_equals(n.getInnerHTML(true), "", 'Regular template should have getInnerHTML(), even after adding shadowroot attribute');
break;
default:
assert_unreached('Unrecognized template');