Bug 1902333 - Fix crash when serializing empty shadow tree. r=smaug,dom-core,peterv

Differential Revision: https://phabricator.services.mozilla.com/D213628
This commit is contained in:
Adam Vandolder 2024-06-18 14:55:20 +00:00
parent c7d66816f8
commit bd4a4535d9
2 changed files with 19 additions and 8 deletions

View File

@ -9705,6 +9705,11 @@ static bool StartSerializingShadowDOM(
}
aBuilder.Append(u">");
if (!shadow->HasChildren()) {
aBuilder.Append(u"</template>");
return false;
}
return true;
}

View File

@ -12,7 +12,7 @@
<script>
function testElementType(allowsShadowDom, elementType, runGetHTMLOnShadowRoot,
lightDOMContent, declarativeShadowDom, mode, delegatesFocus,
serializable, clonable) {
serializable, clonable, emptyShadowTree) {
const t = test(t => {
// Create and attach element
let wrapper;
@ -63,15 +63,17 @@ function testElementType(allowsShadowDom, elementType, runGetHTMLOnShadowRoot,
if (allowsShadowDom) {
const correctShadowHtml = `<template shadowrootmode="${mode}"` +
`${delegatesAttr}${serializableAttr}${clonableAttr}><slot></slot>` +
`</template>`;
`${delegatesAttr}${serializableAttr}${clonableAttr}>` +
(emptyShadowTree ? `` : `<slot></slot>`) + `</template>`;
const correctHtml = `<${elementType}>${correctShadowHtml}` +
`${lightDOMContent}</${elementType}>`;
assert_equals(shadowRoot.mode,mode);
assert_equals(shadowRoot.delegatesFocus,delegatesFocus);
assert_equals(shadowRoot.serializable,expectedSerializable);
assert_equals(shadowRoot.clonable,clonable);
shadowRoot.appendChild(document.createElement('slot'));
if (!emptyShadowTree) {
shadowRoot.appendChild(document.createElement('slot'));
}
const emptyElement = `<${elementType}>${lightDOMContent}</${elementType}>`;
if (expectedSerializable) {
assert_equals(wrapper.getHTML({serializableShadowRoots: true}),
@ -104,7 +106,8 @@ function testElementType(allowsShadowDom, elementType, runGetHTMLOnShadowRoot,
`<${elementType}>${lightDOMContent}${allowsShadowDom ?
`, with ${declarativeShadowDom ? 'declarative' : 'imperative'} shadow, ` +
`mode=${mode}, delegatesFocus=${delegatesFocus}, ` +
`serializable=${serializable}, clonable=${clonable}.` : ''}`);
`serializable=${serializable}, clonable=${clonable},` : ''}` +
` with${emptyShadowTree ? `out` : ``} shadow tree contents.`);
}
function runAllTests() {
@ -120,9 +123,12 @@ function runAllTests() {
for (const clonable of [false, true]) {
for (const mode of ['open', 'closed']) {
for (const serializable of [undefined, 'false', 'true']) {
testElementType(true, elementName, runGetHTMLOnShadowRoot,
lightDOMContent, declarativeShadowDom, mode,
delegatesFocus, serializable, clonable);
for (const emptyShadowTree of [false, true]) {
testElementType(true, elementName, runGetHTMLOnShadowRoot,
lightDOMContent, declarativeShadowDom, mode,
delegatesFocus, serializable, clonable,
emptyShadowTree);
}
}
}
}