Test <template for> parser, shadowrootmode, registry, and sanitizer edge cases
diff --git a/html/semantics/scripting-1/the-template-element/template-for/template-for-body-scope.html b/html/semantics/scripting-1/the-template-element/template-for/template-for-body-scope.html
new file mode 100644
index 0000000..970e98a
--- /dev/null
+++ b/html/semantics/scripting-1/the-template-element/template-for/template-for-body-scope.html
@@ -0,0 +1,103 @@
+<!DOCTYPE HTML>
+<meta charset="utf-8">
+<title>HTML partial updates: the body-to-parent scope redirection only applies to the document's body</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead">
+<link rel="help" href="https://github.com/WICG/declarative-partial-updates">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<body>
+<script>
+// When a <template for> is parsed directly inside the body element of its
+// node document, markers are looked up in the body's parent so that <head>
+// can be patched from <body>. That redirection is keyed on the body element
+// of the document, not on the context element of a fragment parser.
+
+function head_markup(name) {
+  return `<!doctype html><head><?marker name="${name}"></head><body></body>`;
+}
+
+async function load_iframe(t, srcdoc) {
+  const iframe = document.createElement("iframe");
+  t.add_cleanup(() => iframe.remove());
+  await new Promise(resolve => {
+    iframe.onload = resolve;
+    iframe.srcdoc = srcdoc;
+    document.body.append(iframe);
+  });
+  return iframe.contentDocument;
+}
+
+function assert_head_not_patched(doc, root = doc.body) {
+  const marker = doc.head.firstChild;
+  assert_equals(marker.nodeType, Node.PROCESSING_INSTRUCTION_NODE, "marker is kept in head");
+  assert_equals(doc.getElementById("added"), null, "head is not patched");
+  const template = root.querySelector('template[for="h"]');
+  assert_not_equals(template, null, "template is inserted as a regular template");
+  assert_equals(template.content.querySelector("#added").localName, "meta");
+}
+
+const template_markup = '<template for="h"><meta id="added"></template>';
+
+const setters = {
+  innerHTML: (body, markup) => { body.innerHTML = markup; },
+  setHTMLUnsafe: (body, markup) => body.setHTMLUnsafe(markup),
+  insertAdjacentHTML: (body, markup) => body.insertAdjacentHTML("beforeend", markup),
+  appendHTMLUnsafe: (body, markup) => body.appendHTMLUnsafe(markup),
+  streamAppendHTMLUnsafe: async (body, markup) => {
+    const writer = body.streamAppendHTMLUnsafe().getWriter();
+    await writer.write(markup);
+    await writer.close();
+  },
+  createContextualFragment: (body, markup) => {
+    const range = body.ownerDocument.createRange();
+    range.selectNodeContents(body);
+    body.append(range.createContextualFragment(markup));
+  },
+};
+
+promise_test(async (t) => {
+  const doc = await load_iframe(
+    t,
+    `<!doctype html><head><?marker name="h"></head><body>${template_markup}</body>`,
+  );
+  assert_not_equals(doc.head.querySelector("meta#added"), null, "head is patched");
+  assert_equals(doc.body.querySelector("template"), null, "template is not inserted in body");
+}, "Document parser in body redirects scope to documentElement and patches markers in head");
+
+for (const [name, setter] of Object.entries(setters)) {
+  promise_test(async (t) => {
+    const doc = await load_iframe(t, head_markup("h"));
+    await setter(doc.body, template_markup);
+    assert_head_not_patched(doc);
+  }, `${name} on the document's body does not patch markers in head`);
+}
+
+promise_test(async (t) => {
+  const doc = await load_iframe(t, head_markup("h"));
+  const body = doc.createElement("body");
+  body.innerHTML = template_markup;
+  assert_head_not_patched(doc, body);
+}, "innerHTML on a detached body does not patch markers in the document's head");
+
+promise_test(async (t) => {
+  const doc = await load_iframe(t, head_markup("h"));
+  const body = doc.createElement("body");
+  body.innerHTML = '<div><?start name="x">old<?end></div><template for="x">new</template>';
+  assert_equals(body.innerHTML, "<div>new</div>");
+
+  body.innerHTML = '<?start name="x">old<?end><template for="x">new</template>';
+  assert_equals(body.innerHTML, "new");
+}, "innerHTML on a detached body patches markers within the fragment");
+
+promise_test(async (t) => {
+  const doc = await load_iframe(t, head_markup("h"));
+  const html = doc.createElement("html");
+  html.innerHTML = `<head><?marker name="h"></head><body>${template_markup}</body>`;
+  const head = html.querySelector("head");
+  assert_equals(head.firstChild.nodeType, Node.PROCESSING_INSTRUCTION_NODE, "marker is kept in the fragment's head");
+  assert_equals(html.querySelector("meta"), null, "neither head is patched");
+  assert_equals(doc.getElementById("added"), null);
+  assert_head_not_patched(doc, html.querySelector("body"));
+}, "A body element that is not the document's body does not redirect the scope to its parent");
+</script>
+</body>
diff --git a/html/semantics/scripting-1/the-template-element/template-for/template-for-eof.html b/html/semantics/scripting-1/the-template-element/template-for/template-for-eof.html
new file mode 100644
index 0000000..1bf62ec
--- /dev/null
+++ b/html/semantics/scripting-1/the-template-element/template-for/template-for-eof.html
@@ -0,0 +1,63 @@
+<!DOCTYPE HTML>
+<meta charset="utf-8">
+<title>HTML partial updates: patching should finalize at EOF</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intemplate">
+<link rel="help" href="https://github.com/whatwg/html/issues/13036">
+<link rel="help" href="https://github.com/WICG/declarative-partial-updates">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script>
+promise_test(async () => {
+  await new Promise((resolve) =>
+    document.addEventListener("DOMContentLoaded", resolve),
+  );
+  const placeholder = document.querySelector("#placeholder");
+  assert_equals(placeholder.textContent, "New content");
+  assert_equals(placeholder.childNodes.length, 1);
+}, "EOF should finalize patching in document parser");
+
+test(() => {
+  const container = document.createElement("div");
+  container.innerHTML =
+    '<section><?marker name="m"></section><template for="m">New content';
+  const section = container.querySelector("section");
+  assert_equals(section.textContent, "New content");
+  assert_equals(section.childNodes.length, 1);
+}, "EOF should finalize <?marker> patching in innerHTML");
+
+test(() => {
+  const container = document.createElement("div");
+  container.setHTMLUnsafe(
+    '<section><?start name="p">Old content<?end></section>' +
+    '<template for="p"><span>New content',
+  );
+  const section = container.querySelector("section");
+  assert_equals(section.innerHTML, "<span>New content</span>");
+  assert_equals(section.childNodes.length, 1);
+}, "EOF with an unclosed child element inside <template for> should finalize patching");
+
+test(() => {
+  const container = document.createElement("div");
+  container.setHTMLUnsafe(
+    '<template id="outer"><section><?start name="p">Old content<?end></section>' +
+    '<template for="p"><span>New content',
+  );
+  const outer = container.querySelector("#outer");
+  const section = outer.content.querySelector("section");
+  assert_equals(section.innerHTML, "<span>New content</span>");
+  assert_equals(section.childNodes.length, 1);
+}, "EOF with nested open templates should finalize patching");
+
+promise_test(async () => {
+  const container = document.createElement("div");
+  const writer = container.streamAppendHTMLUnsafe().getWriter();
+  await writer.write('<section><?start name="p">Old content<?end></section>');
+  await writer.write('<template for="p"><span>New content');
+  await writer.close();
+  const section = container.querySelector("section");
+  assert_equals(section.innerHTML, "<span>New content</span>");
+  assert_equals(section.childNodes.length, 1);
+}, "EOF should finalize patching when streaming HTML");
+</script>
+<section id="placeholder"><?start name="p">Old content<?end></section>
+<template for="p">New content
\ No newline at end of file
diff --git a/html/semantics/scripting-1/the-template-element/template-for/template-for-malformed-marker.html b/html/semantics/scripting-1/the-template-element/template-for/template-for-malformed-marker.html
new file mode 100644
index 0000000..9f53b42
--- /dev/null
+++ b/html/semantics/scripting-1/the-template-element/template-for/template-for-malformed-marker.html
@@ -0,0 +1,93 @@
+<!DOCTYPE HTML>
+<meta charset="utf-8">
+<title>HTML partial updates: markers with malformed pseudo-attributes don't match</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#find-markers">
+<link rel="help" href="https://www.w3.org/TR/xml-stylesheet/#dt-parsing">
+<link rel="help" href="https://github.com/WICG/declarative-partial-updates">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+
+<!-- A duplicate pseudo-attribute name, or anything that isn't a well-formed
+     pseudo-attribute, makes the whole processing instruction parse as an
+     error. Such a marker has no "name" at all, so it matches nothing: neither
+     of the duplicated names, and not the name that precedes the junk. -->
+<div id="duplicate"><?marker name="p" name="q"></div>
+<template for="p">patched p</template>
+<template for="q">patched q</template>
+
+<div id="trailing-junk"><?marker name="j" junk></div>
+<template for="j">patched j</template>
+
+<div id="leading-junk"><?marker junk name="k"></div>
+<template for="k">patched k</template>
+
+<div id="no-space"><?marker name="n"other="1"></div>
+<template for="n">patched n</template>
+
+<div id="duplicate-range"><?start name="r" name="r">old<?end></div>
+<template for="r">patched r</template>
+
+<!-- Control: extra well-formed pseudo-attributes are fine. -->
+<div id="well-formed"><?marker name="g" other="1"></div>
+<template for="g">patched g</template>
+
+<script>
+function assert_not_patched(id, ...names) {
+  const container = document.getElementById(id);
+  const pi = container.firstChild;
+  assert_equals(pi.nodeType, Node.PROCESSING_INSTRUCTION_NODE, `${id}: marker is kept`);
+  assert_equals(pi.getAttribute("name"), null, `${id}: marker has no name`);
+  assert_false(container.textContent.startsWith("patched"), `${id}: not patched`);
+  for (const name of names) {
+    const template = document.querySelector(`template[for="${name}"]`);
+    assert_not_equals(template, null, `template[for=${name}] is inserted as a regular template`);
+    assert_equals(template.content.textContent, `patched ${name}`);
+  }
+}
+
+test(() => {
+  assert_not_patched("duplicate", "p", "q");
+  assert_equals(document.getElementById("duplicate").textContent, "");
+}, "Duplicate name pseudo-attribute matches neither name");
+
+test(() => {
+  assert_not_patched("trailing-junk", "j");
+}, "Junk after a well-formed pseudo-attribute invalidates the marker");
+
+test(() => {
+  assert_not_patched("leading-junk", "k");
+}, "Junk before a well-formed pseudo-attribute invalidates the marker");
+
+test(() => {
+  assert_not_patched("no-space", "n");
+}, "Pseudo-attributes without whitespace between them invalidate the marker");
+
+test(() => {
+  assert_not_patched("duplicate-range", "r");
+  assert_equals(document.getElementById("duplicate-range").textContent, "old");
+}, "Duplicate name pseudo-attribute on <?start> matches nothing");
+
+test(() => {
+  const container = document.getElementById("well-formed");
+  assert_equals(container.textContent, "patched g");
+  assert_equals(container.firstChild.nodeType, Node.TEXT_NODE, "marker is consumed");
+  assert_equals(document.querySelector('template[for="g"]'), null);
+}, "Additional well-formed pseudo-attributes don't prevent matching");
+
+test(() => {
+  const container = document.createElement("div");
+  container.innerHTML =
+    '<div><?marker name="a" name="b"></div><template for="a">A</template><template for="b">B</template>' +
+    '<div><?marker name="c" junk></div><template for="c">C</template>' +
+    '<div><?marker name="d"></div><template for="d">D</template>';
+  const [dupe, junk, good] = container.querySelectorAll("div");
+  assert_equals(dupe.textContent, "");
+  assert_equals(dupe.firstChild.nodeType, Node.PROCESSING_INSTRUCTION_NODE);
+  assert_equals(junk.textContent, "");
+  assert_equals(junk.firstChild.nodeType, Node.PROCESSING_INSTRUCTION_NODE);
+  assert_equals(good.textContent, "D");
+  assert_array_equals(
+    [...container.querySelectorAll("template")].map(t => t.getAttribute("for")),
+    ["a", "b", "c"]);
+}, "Malformed markers don't match in a fragment parser either");
+</script>
diff --git a/html/semantics/scripting-1/the-template-element/template-for/template-for-sanitizer-config.html b/html/semantics/scripting-1/the-template-element/template-for/template-for-sanitizer-config.html
new file mode 100644
index 0000000..d677f60
--- /dev/null
+++ b/html/semantics/scripting-1/the-template-element/template-for/template-for-sanitizer-config.html
@@ -0,0 +1,98 @@
+<!DOCTYPE HTML>
+<meta charset="utf-8">
+<title>HTML partial updates: patching while sanitizing</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead">
+<link rel="help" href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#html-sanitization">
+<link rel="help" href="https://github.com/WICG/declarative-partial-updates">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<body>
+<script>
+const markup =
+  '<div id="target"><?marker name="x"></div>' +
+  '<!-- comment -->' +
+  '<template for="x"><b title="t">bold</b><i>italic</i></template>';
+
+function parse(method, options) {
+  const container = document.createElement("div");
+  container[method](markup, options);
+  return container;
+}
+
+function assert_patched(container, expected_target_html) {
+  const target = container.querySelector("div");
+  assert_equals(target.innerHTML, expected_target_html, "target is patched");
+  assert_equals(container.querySelector("template"), null, "template is not inserted");
+}
+
+function assert_not_patched(container) {
+  const target = container.querySelector("div");
+  assert_equals(target.childNodes.length, 0, "marker was removed and nothing was patched");
+  const template = container.querySelector('template[for="x"]');
+  assert_not_equals(template, null, "template is inserted as a regular template");
+  assert_equals(template.content.textContent, "bolditalic");
+}
+
+function has_comment(container) {
+  return [...container.childNodes].some(node => node.nodeType === Node.COMMENT_NODE);
+}
+
+// Sanitizing unrelated content doesn't prevent patching.
+test(() => {
+  const container = parse("setHTMLUnsafe", { sanitizer: { removeElements: ["b"] } });
+  assert_patched(container, "<i>italic</i>");
+}, "setHTMLUnsafe: removeElements for an element inside the template still patches");
+
+test(() => {
+  const container = parse("setHTMLUnsafe", { sanitizer: { replaceWithChildrenElements: ["b"] } });
+  assert_patched(container, "bold<i>italic</i>");
+}, "setHTMLUnsafe: replaceWithChildrenElements for an element inside the template still patches");
+
+test(() => {
+  const container = parse("setHTMLUnsafe", { sanitizer: { removeAttributes: ["title"] } });
+  assert_patched(container, "<b>bold</b><i>italic</i>");
+}, "setHTMLUnsafe: removeAttributes for an attribute inside the template still patches");
+
+test(() => {
+  const container = parse("setHTMLUnsafe", { sanitizer: { elements: ["div", "template", "b", "i"], attributes: ["for", "title"] } });
+  assert_patched(container, '<b title="t">bold</b><i>italic</i>');
+}, "setHTMLUnsafe: allow-lists that keep template and for still patch");
+
+// Processing instructions are governed by processingInstructions /
+// removeProcessingInstructions, independently of comments.
+test(() => {
+  const container = parse("setHTMLUnsafe", { sanitizer: { comments: false } });
+  assert_false(has_comment(container), "comment is removed");
+  assert_patched(container, '<b title="t">bold</b><i>italic</i>');
+}, "setHTMLUnsafe: comments: false keeps markers and patches");
+
+test(() => {
+  const container = parse("setHTMLUnsafe", { sanitizer: { comments: false, processingInstructions: ["marker"] } });
+  assert_false(has_comment(container), "comment is removed");
+  assert_patched(container, '<b title="t">bold</b><i>italic</i>');
+}, "setHTMLUnsafe: comments: false with processingInstructions allowing the marker patches");
+
+test(() => {
+  const container = parse("setHTMLUnsafe", { sanitizer: { comments: true, removeProcessingInstructions: ["marker"] } });
+  assert_true(has_comment(container), "comment is kept");
+  assert_not_patched(container);
+}, "setHTMLUnsafe: comments: true with removeProcessingInstructions removing the marker does not patch");
+
+test(() => {
+  const container = parse("setHTMLUnsafe", { sanitizer: { comments: true, processingInstructions: [] } });
+  assert_true(has_comment(container), "comment is kept");
+  assert_not_patched(container);
+}, "setHTMLUnsafe: comments: true with an empty processingInstructions list does not patch");
+
+test(() => {
+  const container = parse("setHTMLUnsafe", { sanitizer: { processingInstructions: ["start", "end"] } });
+  assert_not_patched(container);
+}, "setHTMLUnsafe: processingInstructions not including the marker's target does not patch");
+
+test(() => {
+  const container = parse("setHTML", { sanitizer: { comments: false, processingInstructions: ["marker"] } });
+  assert_false(has_comment(container), "comment is removed");
+  assert_patched(container, '<b title="t">bold</b><i>italic</i>');
+}, "setHTML: comments: false with processingInstructions allowing the marker patches");
+</script>
+</body>
diff --git a/html/semantics/scripting-1/the-template-element/template-for/template-for-scoped-custom-element-registry.html b/html/semantics/scripting-1/the-template-element/template-for/template-for-scoped-custom-element-registry.html
new file mode 100644
index 0000000..0150e6d
--- /dev/null
+++ b/html/semantics/scripting-1/the-template-element/template-for/template-for-scoped-custom-element-registry.html
@@ -0,0 +1,122 @@
+<!DOCTYPE HTML>
+<meta charset="utf-8">
+<title>HTML partial updates: patched elements use the custom element registry of the tree they are inserted into</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/parsing.html#create-an-element-for-the-token">
+<link rel="help" href="https://github.com/WICG/declarative-partial-updates">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+
+<script>
+class GlobalFoo extends HTMLElement {}
+customElements.define("x-foo", GlobalFoo);
+</script>
+
+<!-- Control: patching in the light tree uses the global registry. -->
+<div id="light"><?marker name="light"></div>
+<template for="light"><x-foo id="light-foo"></x-foo></template>
+
+<!-- The template is parsed by the document parser, whose document has the
+     global registry, but the patch target is inside a shadow root with a
+     null registry. Elements are created with the registry of their intended
+     parent, so the patched <x-foo> must not be upgraded by the global
+     definition, just like the unpatched one next to it. -->
+<div id="host">
+  <template shadowrootmode="open" shadowrootcustomelementregistry>
+    <x-foo id="unpatched"></x-foo>
+    <span><?marker name="shadow"></span>
+    <template for="shadow"><x-foo id="patched"></x-foo></template>
+  </template>
+</div>
+
+<script>
+test(() => {
+  const foo = document.getElementById("light-foo");
+  assert_true(foo instanceof GlobalFoo);
+  assert_equals(foo.customElementRegistry, customElements);
+}, "Patched element in the light tree uses the global registry (control)");
+
+test(() => {
+  const shadow = document.getElementById("host").shadowRoot;
+  assert_equals(shadow.customElementRegistry, null);
+
+  const unpatched = shadow.getElementById("unpatched");
+  assert_false(unpatched instanceof GlobalFoo, "unpatched element is not upgraded");
+  assert_equals(unpatched.customElementRegistry, null);
+
+  const patched = shadow.querySelector("span > x-foo");
+  assert_not_equals(patched, null, "the patch was applied");
+  assert_equals(patched.id, "patched");
+  assert_equals(shadow.querySelector("template"), null);
+  assert_false(patched instanceof GlobalFoo, "patched element is not upgraded by the document's registry");
+  assert_equals(patched.customElementRegistry, null);
+  assert_equals(Object.getPrototypeOf(patched), HTMLElement.prototype);
+}, "Patched element inside a declarative shadow root with a null registry is not upgraded by the global registry");
+
+function makeHost(t, registry) {
+  const host = document.createElement("div");
+  document.body.append(host);
+  t.add_cleanup(() => host.remove());
+  return host.attachShadow({ mode: "open", customElementRegistry: registry });
+}
+
+test((t) => {
+  const scoped = new CustomElementRegistry();
+  class ScopedFoo extends HTMLElement {}
+  scoped.define("x-foo", ScopedFoo);
+  const shadow = makeHost(t, scoped);
+  shadow.setHTMLUnsafe('<span><?marker name="x"></span><template for="x"><x-foo></x-foo></template>');
+  const foo = shadow.querySelector("span > x-foo");
+  assert_not_equals(foo, null, "the patch was applied");
+  assert_true(foo instanceof ScopedFoo, "patched element is upgraded by the shadow root's registry");
+  assert_false(foo instanceof GlobalFoo);
+  assert_equals(foo.customElementRegistry, scoped);
+}, "setHTMLUnsafe on a shadow root with a scoped registry: patched element uses the scoped registry");
+
+test((t) => {
+  const scoped = new CustomElementRegistry();
+  const shadow = makeHost(t, scoped);
+  shadow.setHTMLUnsafe('<span><?marker name="x"></span><template for="x"><x-foo></x-foo></template>');
+  const foo = shadow.querySelector("span > x-foo");
+  assert_not_equals(foo, null, "the patch was applied");
+  assert_false(foo instanceof GlobalFoo, "patched element is not upgraded by the document's registry");
+  assert_equals(foo.customElementRegistry, scoped);
+  class ScopedFoo extends HTMLElement {}
+  scoped.define("x-foo", ScopedFoo);
+  assert_true(foo instanceof ScopedFoo, "patched element is upgraded once the scoped registry defines it");
+}, "setHTMLUnsafe on a shadow root with an empty scoped registry: patched element is not upgraded by the global registry");
+
+test((t) => {
+  const scoped = new CustomElementRegistry();
+  class ScopedFoo extends HTMLElement {}
+  scoped.define("x-foo", ScopedFoo);
+  const shadow = makeHost(t, scoped);
+  shadow.setHTMLUnsafe(
+    '<?marker name="x"><template for="x"><x-foo id="direct"></x-foo><div><x-foo id="nested"></x-foo></div></template>',
+  );
+  for (const id of ["#direct", "#nested"]) {
+    const foo = shadow.querySelector(id);
+    assert_not_equals(foo, null, `${id} was patched`);
+    assert_true(foo instanceof ScopedFoo, `${id} is upgraded by the shadow root's registry`);
+    assert_false(foo instanceof GlobalFoo);
+    assert_equals(foo.customElementRegistry, scoped);
+  }
+}, "setHTMLUnsafe on a shadow root with a top-level marker: direct and nested patched elements use the scoped registry");
+
+promise_test(async (t) => {
+  const scoped = new CustomElementRegistry();
+  class ScopedFoo extends HTMLElement {}
+  scoped.define("x-foo", ScopedFoo);
+  const shadow = makeHost(t, scoped);
+  // The marker is a child of the shadow root itself, so the patch target is
+  // the live ShadowRoot rather than a node created by this parser.
+  shadow.innerHTML = '<?marker name="x">';
+  const writer = shadow.streamAppendHTMLUnsafe().getWriter();
+  await writer.write('<template for="x"><x-foo></x-foo></template>');
+  await writer.close();
+  const foo = shadow.querySelector("x-foo");
+  assert_not_equals(foo, null, "the patch was applied");
+  assert_true(foo instanceof ScopedFoo, "patched element is upgraded by the shadow root's registry");
+  assert_false(foo instanceof GlobalFoo);
+  assert_equals(foo.customElementRegistry, scoped);
+}, "Streaming into a shadow root with a scoped registry: element patched directly into the shadow root uses the scoped registry");
+</script>
diff --git a/html/semantics/scripting-1/the-template-element/template-for/template-for-shadowrootmode-not-attached.html b/html/semantics/scripting-1/the-template-element/template-for/template-for-shadowrootmode-not-attached.html
new file mode 100644
index 0000000..ea7b5d5
--- /dev/null
+++ b/html/semantics/scripting-1/the-template-element/template-for/template-for-shadowrootmode-not-attached.html
@@ -0,0 +1,109 @@
+<!DOCTYPE HTML>
+<meta charset="utf-8">
+<title>HTML partial updates: for applies when shadowrootmode doesn't attach a shadow root</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead">
+<link rel="help" href="https://github.com/whatwg/html/issues/13075">
+<link rel="help" href="https://github.com/WICG/declarative-partial-updates">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+
+<!-- A <template> with both shadowrootmode and for only skips patching when a
+     shadow root is actually attached. When it isn't - a non-shadow-host host,
+     a host that already has a shadow root, the host being the root of the
+     fragment, a host the sanitizer replaces with its children, or declarative
+     shadow roots being disallowed altogether (innerHTML, DOMParser) - the
+     template falls through to the "for" steps and patches as usual. -->
+
+<a id="anchor-host"><?marker name="anchor"><template shadowrootmode="open" for="anchor">content</template></a>
+<span id="double-host"><?marker name="double"><template shadowrootmode="open">first</template><template shadowrootmode="open" for="double">content</template></span>
+
+<script>
+const markup = '<span><?marker name="x"></span><template shadowrootmode="open" for="x">content</template>';
+
+// The marker was consumed, the content was patched in its place and no
+// template element was inserted.
+function assert_patched(root, host = root.querySelector("span")) {
+  assert_equals(host.textContent, "content", "content is patched into the marker's parent");
+  assert_false([...host.childNodes].some(n => n.nodeType === Node.PROCESSING_INSTRUCTION_NODE),
+    "marker is removed");
+  assert_equals(root.querySelector("template"), null, "template is not inserted");
+}
+
+test(() => {
+  const anchor = document.getElementById("anchor-host");
+  assert_equals(anchor.shadowRoot, null);
+  assert_patched(anchor, anchor);
+}, "Host that can't have a shadow root: patches");
+
+test(() => {
+  const host = document.getElementById("double-host");
+  assert_equals(host.shadowRoot.textContent, "first", "first template attached the shadow root");
+  assert_patched(host, host);
+}, "Host that already has a shadow root: patches");
+
+test(() => {
+  const container = document.createElement("div");
+  container.innerHTML = markup;
+  assert_equals(container.querySelector("span").shadowRoot, null);
+  assert_patched(container);
+}, "innerHTML (declarative shadow roots disallowed): patches");
+
+test(() => {
+  const doc = new DOMParser().parseFromString(markup, "text/html");
+  assert_equals(doc.querySelector("span").shadowRoot, null);
+  assert_patched(doc.body);
+}, "DOMParser (declarative shadow roots disallowed): patches");
+
+test(() => {
+  const container = document.createElement("div");
+  container.setHTMLUnsafe('<?marker name="x"><template shadowrootmode="open" for="x">content</template>');
+  assert_equals(container.shadowRoot, null, "no shadow root is attached to the context element");
+  assert_patched(container, container);
+}, "setHTMLUnsafe with the root as host: patches");
+
+test(() => {
+  const container = document.createElement("div");
+  container.setHTMLUnsafe('<a><?marker name="x"><template shadowrootmode="open" for="x">content</template></a>');
+  const a = container.querySelector("a");
+  assert_equals(a.shadowRoot, null);
+  assert_patched(container, a);
+}, "setHTMLUnsafe with a host that can't have a shadow root: patches");
+
+test(() => {
+  customElements.define(
+    "x-disabled-shadow",
+    class extends HTMLElement {
+      static disabledFeatures = ["shadow"];
+    },
+  );
+  const container = document.createElement("div");
+  container.setHTMLUnsafe(
+    '<x-disabled-shadow><?marker name="x"><template shadowrootmode="open" for="x">content</template></x-disabled-shadow>',
+  );
+  const host = container.querySelector("x-disabled-shadow");
+  assert_equals(host.shadowRoot, null);
+  assert_patched(container, host);
+}, "setHTMLUnsafe with a custom element host that disables shadow: patches");
+
+test(() => {
+  const container = document.createElement("div");
+  container.setHTMLUnsafe(
+    '<span><?marker name="x"><template shadowrootmode="open" for="x">content</template></span>',
+    { sanitizer: { replaceWithChildrenElements: ["span"] } });
+  assert_equals(container.querySelector("span"), null, "span was replaced with its children");
+  assert_equals(container.shadowRoot, null);
+  assert_patched(container, container);
+}, "setHTMLUnsafe with a host replaced with its children: patches");
+
+test(() => {
+  // Control: when the shadow root is attached, "for" is ignored and the
+  // marker is kept in the light tree.
+  const container = document.createElement("div");
+  container.setHTMLUnsafe('<span><?marker name="x"><template shadowrootmode="open" for="x">content</template></span>');
+  const span = container.querySelector("span");
+  assert_equals(span.shadowRoot.textContent, "content");
+  assert_equals(span.firstChild.nodeType, Node.PROCESSING_INSTRUCTION_NODE, "marker is kept");
+  assert_equals(span.textContent, "");
+  assert_equals(container.querySelector("template"), null);
+}, "setHTMLUnsafe with a shadow root attached: control, shadowrootmode wins");
+</script>