Skip to content

Commit b8ca5e7

Browse files
Sam Sebreechromium-wpt-export-bot
authored andcommitted
[SyntheticModules] Implements CSS Modules
This is the final change required for CSS Modules to be utilized by developers. Following the acceptance of this change, if you run chromium with the CSSModules runtime flag, the following is now valid syntax: <script type="module"> import sheet from "./example.css"; </script> CSS Modules Explainer: https://github.com/w3c/webcomponents/blob/gh-pages/proposals/css-modules-v1-explainer.md CSS Modules Spec PR: whatwg/html#4898 Bug: 967018 Change-Id: Ifdee5b92259fb7e4e9c8f9aa88e69a98eb55c551
1 parent 9e9653f commit b8ca5e7

12 files changed

+202
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!doctype html>
2+
3+
<head>
4+
<title>import-css-module-worker</title>
5+
<script src="/resources/testharness.js"></script>
6+
<script src="/resources/testharnessreport.js"></script>
7+
</head>
8+
9+
<body>
10+
<h1>import-css-module-worker</h1>
11+
<script>
12+
async_test(function (test) {
13+
const worker = new Worker("./resources/worker.js", {
14+
type: "module"
15+
});
16+
worker.onmessage = test.step_func(() => {
17+
assert_unreached(
18+
"A CSS Module within a web worker should not load.")
19+
});
20+
worker.onerror = test.step_func_done();
21+
}, "A CSS Module within a web worker should not load.");
22+
23+
async_test(function (test) {
24+
const worker = new Worker("./resources/worker_dynamic_import.js", {
25+
type: "module"
26+
});
27+
worker.onmessage = test.step_func_done();
28+
}, "A dynamic import CSS Module within a web worker should not load.");
29+
30+
async_test(function (test) {
31+
const worker = new Worker("./resources/basic.css", {
32+
type: "module"
33+
});
34+
worker.onmessage = test.step_func(() => {
35+
assert_unreached(
36+
"A CSS Module within a web worker should not load.")
37+
});
38+
worker.onerror = test.step_func_done();
39+
}, "A CSS Module within a web worker should not load.");
40+
41+
</script>
42+
43+
</body>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!doctype html>
2+
3+
<head>
4+
<title>import-css-module-basic</title>
5+
<script src="/resources/testharness.js"></script>
6+
<script src="/resources/testharnessreport.js"></script>
7+
</head>
8+
9+
<body>
10+
<h1>import-css-module-basic</h1>
11+
<script type="module">
12+
import v from "./resources/basic.css";
13+
document.adoptedStyleSheets = [v];
14+
test(function () {
15+
assert_equals(getComputedStyle(document.querySelector('#test'))
16+
.backgroundColor, "rgb(255, 0, 0)",
17+
"CSS module should import correctly on top level document");
18+
}, "Test basic CSS module import");
19+
20+
async_test(function (test) {
21+
let iframe = document.createElement("iframe");
22+
iframe.src = "resources/css_module_at_import_iframe.html";
23+
iframe.onload = test.step_func_done(function () {
24+
assert_equals(iframe.contentDocument.load_error, "NotAllowedError");
25+
assert_not_equals(getComputedStyle(iframe.contentDocument.querySelector('#test'))
26+
.backgroundColor, "rgb(255, 0, 0)",
27+
"CSS module @import should not succeed");
28+
});
29+
document.body.appendChild(iframe);
30+
}, "An @import CSS Module should not load");
31+
32+
async_test(function (test) {
33+
let iframe = document.createElement("iframe");
34+
iframe.src = "resources/malformed-iframe.html";
35+
iframe.onload = test.step_func_done(function () {
36+
assert_not_equals(getComputedStyle(iframe.contentDocument.querySelector('#test'))
37+
.backgroundColor, "rgb(255, 0, 0)",
38+
"Malformed CSS should throw parse error");
39+
});
40+
document.body.appendChild(iframe);
41+
}, "Malformed CSS should throw parse error");
42+
</script>
43+
44+
<div id="test">
45+
I am a test div.
46+
</div>
47+
</body>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "basic.css"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#test {
2+
background-color:red;
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<body>
2+
<script>
3+
window.onerror = function (errorMsg, url, lineNumber, column, errorObj)
4+
{
5+
document.load_error = errorObj.name;
6+
return true;
7+
};
8+
</script>
9+
<script type="module">
10+
import v from "./bad_import.css";
11+
document.adoptedStyleSheets = [v];
12+
</script>
13+
14+
<div id="test">
15+
I am a test div.
16+
</div>
17+
</body>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<body>
2+
<script type="module">
3+
import v from "./malformed.css"
4+
document.adoptedStyleSheets = [v]
5+
</script>
6+
7+
<div id="test">
8+
I am a test div.
9+
</div>
10+
</body>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#test {{
2+
background-color:red;
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<body>
2+
css-module-at-import
3+
<script>
4+
window.onerror = function (errorMsg, url, lineNumber, column, errorObj)
5+
{
6+
document.load_error = errorObj.name;
7+
return true;
8+
};
9+
</script>
10+
<script type="module">
11+
import v from "./malformed.css";
12+
document.adoptedStyleSheets = [v];
13+
</script>
14+
15+
<div id="test">
16+
I am a test div.
17+
</div>
18+
</body>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./basic.css";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import("./basic.css")
2+
.catch(e => postMessage("NOT LOADED"))

0 commit comments

Comments
 (0)