Skip to content

Commit 8694fc6

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 9c1f9d4 commit 8694fc6

File tree

12 files changed

+177
-0
lines changed

12 files changed

+177
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!doctype html>
2+
3+
<head>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
</head>
7+
8+
<body>
9+
<script>
10+
setup({allow_uncaught_exception: true});
11+
async_test(function (test) {
12+
const worker = new Worker("./resources/worker.js", {
13+
type: "module"
14+
});
15+
worker.onmessage = test.unreached_func("A CSS Module within a web worker should not load.");
16+
worker.onerror = test.step_func_done();
17+
}, "A static import CSS Module within a web worker should not load.");
18+
19+
async_test(function (test) {
20+
const worker = new Worker("./resources/worker-dynamic-import.js", {
21+
type: "module"
22+
});
23+
worker.onmessage = test.step_func_done(e => {
24+
assert_equals(e.data, "NOT LOADED");
25+
});
26+
}, "A dynamic import CSS Module within a web worker should not load.");
27+
28+
async_test(function (test) {
29+
const worker = new Worker("./resources/basic.css", {
30+
type: "module"
31+
});
32+
worker.onerror = test.step_func_done();
33+
}, "A CSS Module within a web worker should not load.");
34+
35+
</script>
36+
37+
</body>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!doctype html>
2+
3+
<head>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
</head>
7+
8+
<body>
9+
<script>
10+
async_test(function (test) {
11+
const iframe = document.createElement("iframe");
12+
iframe.src = "resources/css-module-basic-iframe.html";
13+
iframe.onload = test.step_func_done(function () {
14+
assert_equals(getComputedStyle(iframe.contentDocument.querySelector('#test'))
15+
.backgroundColor, "rgb(255, 0, 0)",
16+
"CSS module import should succeed");
17+
});
18+
document.body.appendChild(iframe);
19+
}, "A CSS Module should load");
20+
21+
async_test(function (test) {
22+
const iframe = document.createElement("iframe");
23+
iframe.src = "resources/css-module-at-import-iframe.html";
24+
iframe.onload = test.step_func_done(function () {
25+
assert_equals(iframe.contentDocument.load_error, "NotAllowedError");
26+
assert_not_equals(getComputedStyle(iframe.contentDocument.querySelector('#test'))
27+
.backgroundColor, "rgb(255, 0, 0)",
28+
"CSS module @import should not succeed");
29+
});
30+
document.body.appendChild(iframe);
31+
}, "An @import CSS Module should not load");
32+
33+
async_test(function (test) {
34+
const iframe = document.createElement("iframe");
35+
iframe.src = "resources/malformed-iframe.html";
36+
iframe.onload = test.step_func_done(function () {
37+
assert_not_equals(getComputedStyle(iframe.contentDocument.querySelector('#test'))
38+
.backgroundColor, "rgb(255, 0, 0)",
39+
"Malformed CSS should not load");
40+
});
41+
document.body.appendChild(iframe);
42+
}, "Malformed CSS should not load");
43+
</script>
44+
</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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<body>
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 "./bad-import.css";
12+
document.adoptedStyleSheets = [v];
13+
</script>
14+
15+
<div id="test">
16+
I am a test div.
17+
</div>
18+
</body>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<body>
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 "./basic.css";
12+
document.adoptedStyleSheets = [v];
13+
</script>
14+
15+
<div id="test">
16+
I am a test div.
17+
</div>
18+
</body>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<script type="module">
4+
import v from "./malformed.css";
5+
document.adoptedStyleSheets = [v];
6+
</script>
7+
8+
<div id="test">
9+
I am a test div.
10+
</div>
11+
</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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#test {
2+
content: "…";
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import("./basic.css")
2+
.then(() => postMessage("LOADED"))
3+
.catch(e => postMessage("NOT LOADED"));

0 commit comments

Comments
 (0)