Skip to content

shadowrootadoptedstylesheets on <template>#12339

Open
KurtCattiSchmidt wants to merge 7 commits into
whatwg:mainfrom
KurtCattiSchmidt:sas-fetch
Open

shadowrootadoptedstylesheets on <template>#12339
KurtCattiSchmidt wants to merge 7 commits into
whatwg:mainfrom
KurtCattiSchmidt:sas-fetch

Conversation

@KurtCattiSchmidt
Copy link
Copy Markdown
Contributor

@KurtCattiSchmidt KurtCattiSchmidt commented Apr 6, 2026

Adds support for shadowrootadoptedstylesheets on HTMLTemplateElement. <style type="module"> support is added in this PR: #11687

(See WHATWG Working Mode: Changes for more details.)

Addresses #10673


/indices.html ( diff )
/infrastructure.html ( diff )
/parsing.html ( diff )
/scripting.html ( diff )
/webappapis.html ( diff )

@KurtCattiSchmidt KurtCattiSchmidt added the agenda+ To be discussed at a triage meeting label Apr 8, 2026
@noamr
Copy link
Copy Markdown
Collaborator

noamr commented Apr 16, 2026

From an initial look this seems to be on the right track. I need to spend some time on the details though.
cc @emilio

@keithamus
Copy link
Copy Markdown
Member

Right now all of the shadowroot* properties map to attachShadow({}) properties. Do we want to add adoptedStyleSheets to ShadowRootInit?

@noamr
Copy link
Copy Markdown
Collaborator

noamr commented Apr 16, 2026

Right now all of the shadowroot* properties map to attachShadow({}) properties. Do we want to add adoptedStyleSheets to ShadowRootInit?

Note that the current properties of ShadowRootInit become read only on the ShadowRoot, unlike adoptedStylesheets whch is a mutable array-like thing.

So I think it might be nice to include it in the init dictionary but I could also see why it's not necessary.

@KurtCattiSchmidt
Copy link
Copy Markdown
Contributor Author

Right now all of the shadowroot* properties map to attachShadow({}) properties. Do we want to add adoptedStyleSheets to ShadowRootInit?

Agreed with @noamr's response here - since there's already an adoptedStyleSheets property on the imperative version that references a mutable array, we don't need to add it again to ShadowRootInit.

Think of shadowrootadoptedstylesheets as a purely declarative thing, since it's lacking in the declarative version but present in the imperative version.

Copy link
Copy Markdown
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, might be me misunderstanding CSS modules, but I'd figure I'd ask...

Comment thread source
&lt;/template>
&lt;/div>
&lt;div id="host_shadowrootadoptedstylesheets_attribute">
&lt;template shadowrootmode="open" shadowrootadoptedstylesheets="foo">
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how is this example guaranteed to work? The modulemap uses fetch, right? And that's asynchronous per spec. What guarantees that the data: uri is available by the time the adoption happens?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I read, the modulemap marks the module as "fetching", adopts an empty stylesheet, and calls the equivalent of replaceSync on the adopted empty stylesheet when the stylesheet response arrives.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so there is a (very unlikely) chance of FOUC, but no chance of it not working right? Makes sense, might be worth a note?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a note to the example, thanks for looking @emilio!

<p class="note">Note that these two examples are functionally equivalent, but the example with
  <span data-x="attr-template-shadowrootadoptedstylesheets">shadowrootadoptedstylesheets</span>
  might initially render without styles applied.</p>

@jstenback jstenback removed the agenda+ To be discussed at a triage meeting label Apr 30, 2026
Comment thread source

<li>
<p>Let <var>result</var> be <span>ParseJSONModule</span>(<var>source</var>).</p>
<p>Run the steps to <span>synchronously replace the rules of a <code>CSSStyleSheet</code></span>
on <var>sheet</var> given <var>source</var>.</p>

<p>If this throws an exception, catch it, and set <var>script</var>'s <span
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double "and", this is probably better as a set of steps.

Comment thread source
empty, then return.</p></li>
<li><p>Let <var>adoptedStyleSheets</var> be an empty array.</p></li>

<li><p>For each <span>ordered set of unique space-separated tokens</span> <var>specifier</var> in
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this line. Is the specifier an ordered set? I think this can just be "For each specifier" maybe?

brave-builds pushed a commit to brave/chromium that referenced this pull request May 12, 2026
This change updates the fetch behavior to match
whatwg/html#12339. Note that this modifies the
code for existing import, import(), and modulepreload for consistency,
but the existing behavior for these API's does *not* change in any
circumstance. The only observable change is in the
shadowrootadoptedstylesheets fetching behavior.

By modifying the CSS module fetching to first create the CSSStyleSheet
before fetching and calling replaceSync upon completion, we are able to
remove the placeholder stylesheets that were being inserted for
shadowrootadoptedstylesheets. This simplifies the logic considerably and
will be more performant.

Unlike the other API's for fetching a CSS Module Script,
shadowrootadoptedstylesheets will leave behind an empty CSSStyleSheet in
adoptedStyleSheets if the fetch fails. This simplifies the logic and
seems like a reasonable trade off for a rare condition. Earlier versions
of this CL removed it upon failure, but the implementation was
considerably more complex and had other side effects.

Change-Id: If00445458983f5062a1ca2d83ed0253cc171d1ff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7709812
Reviewed-by: Dan Clark <[email protected]>
Commit-Queue: Kurt Catti-Schmidt <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1629010}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request May 12, 2026
This change updates the fetch behavior to match
whatwg/html#12339. Note that this modifies the
code for existing import, import(), and modulepreload for consistency,
but the existing behavior for these API's does *not* change in any
circumstance. The only observable change is in the
shadowrootadoptedstylesheets fetching behavior.

By modifying the CSS module fetching to first create the CSSStyleSheet
before fetching and calling replaceSync upon completion, we are able to
remove the placeholder stylesheets that were being inserted for
shadowrootadoptedstylesheets. This simplifies the logic considerably and
will be more performant.

Unlike the other API's for fetching a CSS Module Script,
shadowrootadoptedstylesheets will leave behind an empty CSSStyleSheet in
adoptedStyleSheets if the fetch fails. This simplifies the logic and
seems like a reasonable trade off for a rare condition. Earlier versions
of this CL removed it upon failure, but the implementation was
considerably more complex and had other side effects.

Change-Id: If00445458983f5062a1ca2d83ed0253cc171d1ff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7709812
Reviewed-by: Dan Clark <[email protected]>
Commit-Queue: Kurt Catti-Schmidt <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1629010}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request May 12, 2026
This change updates the fetch behavior to match
whatwg/html#12339. Note that this modifies the
code for existing import, import(), and modulepreload for consistency,
but the existing behavior for these API's does *not* change in any
circumstance. The only observable change is in the
shadowrootadoptedstylesheets fetching behavior.

By modifying the CSS module fetching to first create the CSSStyleSheet
before fetching and calling replaceSync upon completion, we are able to
remove the placeholder stylesheets that were being inserted for
shadowrootadoptedstylesheets. This simplifies the logic considerably and
will be more performant.

Unlike the other API's for fetching a CSS Module Script,
shadowrootadoptedstylesheets will leave behind an empty CSSStyleSheet in
adoptedStyleSheets if the fetch fails. This simplifies the logic and
seems like a reasonable trade off for a rare condition. Earlier versions
of this CL removed it upon failure, but the implementation was
considerably more complex and had other side effects.

Change-Id: If00445458983f5062a1ca2d83ed0253cc171d1ff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7709812
Reviewed-by: Dan Clark <[email protected]>
Commit-Queue: Kurt Catti-Schmidt <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1629010}
lando-worker Bot pushed a commit to mozilla-firefox/firefox that referenced this pull request May 13, 2026
…a=testonly

Automatic update from web-platform-tests
[CSS Modules] Update fetching strategy

This change updates the fetch behavior to match
whatwg/html#12339. Note that this modifies the
code for existing import, import(), and modulepreload for consistency,
but the existing behavior for these API's does *not* change in any
circumstance. The only observable change is in the
shadowrootadoptedstylesheets fetching behavior.

By modifying the CSS module fetching to first create the CSSStyleSheet
before fetching and calling replaceSync upon completion, we are able to
remove the placeholder stylesheets that were being inserted for
shadowrootadoptedstylesheets. This simplifies the logic considerably and
will be more performant.

Unlike the other API's for fetching a CSS Module Script,
shadowrootadoptedstylesheets will leave behind an empty CSSStyleSheet in
adoptedStyleSheets if the fetch fails. This simplifies the logic and
seems like a reasonable trade off for a rare condition. Earlier versions
of this CL removed it upon failure, but the implementation was
considerably more complex and had other side effects.

Change-Id: If00445458983f5062a1ca2d83ed0253cc171d1ff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7709812
Reviewed-by: Dan Clark <[email protected]>
Commit-Queue: Kurt Catti-Schmidt <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1629010}

--

wpt-commits: e564867fbfe3cc70cc8d43ddac392b7b848bc7e4
wpt-pr: 59805
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants