Skip to content

Should <template for> patch when shadowrootmode doesn't attach a shadow root? #13075

Open

Description

@zcorpan

What is the issue with the HTML Standard?

The template start tag steps in "in head" give a valid shadowrootmode precedence over for, even when no shadow root gets attached:

If template's shadowrootmode attribute is not in the None state:

  1. If any of the following are true:

    • the parser's allow declarative shadow roots is false;
    • hostIsRoot is true; or
    • the parser's insertion target redirection map[declarativeShadowHostElement] exists,

    then insert an element at the adjusted insertion location with template, insertionLocation, and true, and return.

...

Otherwise, if template has a for attribute: ...

Attaching the shadow root can also fail, in which case the steps likewise insert template and return. So per spec, for is ignored whenever shadowrootmode is valid.

Chrome Canary 157 patches instead whenever no shadow root is attached:

const div = document.createElement("div");
div.innerHTML = '<?marker name="x"><template shadowrootmode=open for=x>A</template>';
div.innerHTML;
// Spec: '<?marker name="x"?><template shadowrootmode="open" for="x">A</template>'
// Chrome Canary 157: 'A'

The same happens with a top-level template in setHTMLUnsafe(), and with Document.parseHTMLUnsafe() when the host already has a shadow root. Chrome only skips patching when the shadow root is attached. Gecko's in-progress implementation (bug 2053472) does the same as Chrome. template-for-superseded-by-shadowrootmode.html only covers the case where the shadow root is attached.

Maybe the spec should match: fall through to the for steps when no shadow root is attached, instead of returning.

cc @foolip @noamr

Activity

  1. noamr commented on Oct 14, 2026

    @noamr
    Contributor

    Aligning with the implementations seem OK here. Though this is kind of a judgment call since a combination of for and shadowrootmode is an error condition in any case.

  2. noamr commented on Oct 14, 2026

    @noamr
    Contributor

    WPT added as part of web-platform-tests/wpt#63320

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions