Skip to content

Commit d6c0330

Browse files
dandclarkchromium-wpt-export-bot
authored andcommitted
Pass the correct sourceURLs to DevTools for constructed stylesheets
A CSSStyleSheet imported as a CSS module script has the constructed flag set (https://wicg.github.io/construct-stylesheets/#cssstylesheet-constructed-flag). This is because it is useful to treat imported CSSStyleSheets the same way as stylesheets created via `new`. For example we want to be able to add both to document.adoptedStyleSheets, and we want both to treat @import rules the same way. However, we want DevTools to display them differently. Sheets created with `new` don't have a source file to show, but imported sheets do, so we want the latter to be shown more like "normal" stylesheets. To enable DevTools to tell these types of constructed stylesheets apart, start passing an empty sourceURL to DevTools for constructed stylesheets created by `new`. The corresponding frontend change https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2964580 will check for an empty sourceURL to determine whether to try to link the stylesheet to a source file or not. Additionally, a CSSStyleSheet for a module import now stores the URL of the module request as its base URL. This is later passed to DevTools as the source URL of the stylesheet. This works because the base URL and source URL for a CSS module script are always the same. This also happens to fix a bug with resolution of relative URLs in CSS modules. The previous behavior was to resolve these using the URL of the importing document. The new, correct, behavior is to resolve them using the URL of the stylesheet resource. Bug: 1219441 Change-Id: I48de349e1f7c8e10a329bff016cef68ac9901bb6 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2964539 Reviewed-by: Andrey Kosyakov <[email protected]> Reviewed-by: Kouhei Ueno <[email protected]> Reviewed-by: Alex Rudenko <[email protected]> Reviewed-by: Sigurd Schneider <[email protected]> Commit-Queue: Dan Clark <[email protected]> Cr-Commit-Position: refs/heads/master@{#894456}
1 parent d41f24f commit d6c0330

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!doctype html>
2+
<head>
3+
<title>Test resolution of relative URL in CSS module</title>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
</head>
7+
<body>
8+
<div id="target"></div>
9+
<script type="module">
10+
import styleSheet from "./resources/load-relative-url.css" assert { type: "css"};
11+
test(() => {
12+
const target = document.querySelector("#target");
13+
document.adoptedStyleSheets = [ styleSheet ];
14+
let backgroundStyle = window.getComputedStyle(target).background;
15+
assert_not_equals(backgroundStyle.indexOf("css-module/resources/image.png"), -1);
16+
}, "A relative URL in a CSS module should be resolved relative to the CSS file's URL, not the importing document's URL");
17+
</script>
18+
</body>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
#target {
3+
/* image.png doesn't exist, but that's irrelevant to the test. */
4+
background: url('./image.png');
5+
}

0 commit comments

Comments
 (0)