Skip to content

Commit fe5fa0e

Browse files
[CSS Modules] Bypass fetch for Blob URL's
After switching to Blob URL's in CL:7075691, I noticed that the fetched stylesheet is not always available immediately in a fully declarative scenario (where both the style tag is a module and shadowrootadoptedstylesheets references it), which wasn't the case for dataURI's. Typically, the first n fetches fail to fetch, where n is random. I discovered that dataURI's were always synchronously returned from ResourceFetcher via CreateResourceForStaticData. To match this behavior with Blob URL's, this CL immediately creates a module map entry for a <style type="module"> upon parsing. This entry can then be immediately queried when referenced from the `shadowrootadoptedstylesheets` attribute, avoiding a fetch entirely. DataURI's still need a fetch to generate the underlying object. A test was added that failed ~50% of the time without this change. Bug: 448174611 Change-Id: If50701981f7eaa138fff7d9ca799431c3303fa72
1 parent 625ad19 commit fe5fa0e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<title>shadowrootadoptedstylesheets with Declarative CSS Module</title>
3+
<meta name="author" title="Kurt Catti-Schmidt" href="mailto:[email protected]" />
4+
<link rel='help' href='https://github.com/whatwg/html/pull/11687'>
5+
<script src='/resources/testharness.js'></script>
6+
<script src='/resources/testharnessreport.js'></script>
7+
8+
<style type="module" specifier="foo">
9+
span {color:blue}
10+
</style>
11+
12+
<div id="host">
13+
<template shadowrootmode="open" shadowrootadoptedstylesheets="foo">
14+
<span id="test_element">Test content</span>
15+
</template>
16+
</div>
17+
18+
<script>
19+
const host = document.getElementById("host");
20+
test(function (t) {
21+
assert_equals(
22+
host.shadowRoot.adoptedStyleSheets.length,
23+
1,
24+
"The shadowrootadoptedstylesheets will declaratively import named specifiers into the adoptedStyleSheets array.",
25+
);
26+
27+
}, "adoptedStyleSheets is populated from a <template> element's shadowrootadoptedstylesheets attribute.");
28+
const test_element = host.shadowRoot.getElementById("test_element");
29+
30+
test(function (t) {
31+
assert_equals(getComputedStyle(test_element)
32+
.color, "rgb(0, 0, 255)",
33+
"Declarative styles were applied.");
34+
35+
}, "Styles from the adoptedStyleSheets are applied to elements.");
36+
</script>

0 commit comments

Comments
 (0)