Skip to content

Commit

Permalink
[SyntheticModules] Implements CSS Modules
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Sam Sebree authored and chromium-wpt-export-bot committed Nov 26, 2019
1 parent 3880d95 commit 9955e88
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!doctype html>

<head>
<title>import-css-module-worker</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>

<body>
<h1>import-css-module-worker</h1>
<script>
async_test(function (test) {
const worker = new Worker("./resources/worker.js", {
type: "module"
});
worker.onmessage = test.step_func(() => {
assert_unreached(
"A CSS Module within a web worker should not load.")
});
worker.onerror = test.step_func_done();
}, "A CSS Module within a web worker should not load.");
</script>

<script>
async_test(function (test) {
const worker = new Worker("./resources/worker_dynamic_import.js", {
type: "module"
});
worker.onmessage = test.step_func(() => {
assert_unreached(
"A dynamic import CSS Module within a web worker should not load.")
});
worker.onerror = test.step_func_done();
}, "A dynamic import CSS Module within a web worker should not load.");
</script>

</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!doctype html>

<head>
<title>import-css-module-basic</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>

<body>
<h1>import-css-module-basic</h1>
<script type="module">
import v from "./resources/basic.css";
document.adoptedStyleSheets = [v];
test(function () {
assert_equals(getComputedStyle(document.querySelector('#test'))
.backgroundColor, "rgb(255, 0, 0)",
"CSS module should import correctly on top level document");
}, "Test basic CSS module import");
</script>

<script type="module">
async_test(function (test) {
let iframe = document.createElement("iframe");
iframe.src = "resources/css_module_at_import_iframe.html";
iframe.onload = test.step_func_done(function () {
assert_equals(iframe.contentDocument.load_error, "NotAllowedError");
assert_not_equals(getComputedStyle(iframe.contentDocument.querySelector('#test'))
.backgroundColor, "rgb(255, 0, 0)",
"CSS module @import should not succeed");
});
document.body.appendChild(iframe);
}, "An @import CSS Module should not load");
</script>

<script type="module">
async_test(function (test) {
let iframe = document.createElement("iframe");
iframe.src = "resources/malformed.html";
iframe.onload = test.step_func_done(function () {
assert_equals(iframe.contentDocument.load_error, "ParseError");
assert_not_equals(getComputedStyle(iframe.contentDocument.querySelector('#test'))
.backgroundColor, "rgb(255, 0, 0)",
"Malformed CSS should throw parse error");
});
document.body.appendChild(iframe);
}, "Malformed CSS should throw parse error");
</script>

<div id="test">
I am a test div.
</div>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "basic.css"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#test {
background-color:red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<body>
css-module-at-import
<script>
window.onerror = function (errorMsg, url, lineNumber, column, errorObj)
{
document.load_error = errorObj.name;
return true;
};
</script>
<script type="module">
import v from "./bad_import.css";
document.adoptedStyleSheets = [v];
</script>

<div id="test">
I am a test div.
</div>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#test {{
background-color:red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./basic.css";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "basic.css"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>CSS modules: UTF-8 decoding</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
function check(t, v) {
t.step(() => {
assert_equals(typeof v, "object");
assert_array_equals(Object.keys(v), ["test"]);
assert_equals(v.test, "\u2026");
t.done();
});
}
const t1 = async_test("utf-8");
const t2 = async_test("shift-jis");
const t3 = async_test("windows-1252");
const t4 = async_test("utf-7");
</script>
<script type="module" onerror="t1.step(() => assert_unreached(event))">
import v from "../serve-with-content-type.py?fn=css-module/resources/basic.css&ct=text/css%3Bcharset=utf-8";
check(t1, v);
</script>
<script type="module" onerror="t2.step(() => assert_unreached(event))">
import v from "../serve-with-content-type.py?fn=css-module/resources/basic.css&ct=text/css%3Bcharset=shift-jis";
check(t2, v);
</script>
<script type="module" onerror="t3.step(() => assert_unreached(event))">
import v from "../serve-with-content-type.py?fn=css-module/resources/basic.css&ct=text/css%3Bcharset=windows-1252";
check(t3, v);
</script>
<script type="module" onerror="t4.step(() => assert_unreached(event))">
import v from "../serve-with-content-type.py?fn=css-module/resources/basic.css&ct=text/css%3Bcharset=utf-7";
check(t4, v);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>JSON modules: Content-Type</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
function check(t, v) {
t.step(() => {
assert_equals(typeof v, "object");
assert_array_equals(Object.keys(v), ["test"]);
assert_equals(v.test, true);
t.done();
});
}
const t1 = async_test("text/css");
</script>
<script type="module" onerror="t1.step(() => assert_unreached(event))">
import v from "../serve-with-content-type.py?fn=css-module/resources/basic.css&ct=text/css";
check(t1, v);
</script>

0 comments on commit 9955e88

Please sign in to comment.