summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/scripting-1/the-script-element/css-module-assertions/import-css-module-basic.html
blob: 4ea1790aabdf2bf420d05eb0c902770b5cef7139 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!doctype html>
<head>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
</head>
<body>
    <div id="test">I am a test div.</div>
    <div id="test2">I am a test div.</div>
    <div id="test3">I am a test div.</div>
    <div id="test3b">I am a test div.</div>
    <div id="test4">I am a test div.</div>
    <div id="test4b">I am a test div.</div>
    <script>
        window.errorCount = 0;
        window.onerror = (errorMsg, url, lineNumber, column, errorObj) => {
            window.errorCount++;
        };
    </script>
    <script type="module" onerror="unreachable()">
        import sheet from "./resources/basic.css" assert { type: "css" };
        test(() => {
            document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
            assert_equals(getComputedStyle(document.querySelector('#test'))
                    .backgroundColor, "rgb(255, 0, 0)", "CSS module import should succeed");
        }, "A CSS Module should load");
    </script>
    <script type="module" onerror="unreachable()">
        import sheet from "./resources/basic-large.css" assert { type: "css" };
        test(() => {
            // This tests potential streaming compilation of modules in
            // Chromium that is triggered only for large (32>KiB) files in older
            // versions.
            document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
            assert_equals(getComputedStyle(document.querySelector('#test2'))
                    .backgroundColor, "rgb(255, 0, 0)",
                    "CSS module import should succeed");
        }, "A large CSS Module should load");
    </script>
    <script type="module" onerror="unreachable()">
        import sheet from "./resources/bad-import.css" assert { type: "css" };
        test(() => {
            document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
            assert_equals(window.errorCount, 0);
            assert_equals(sheet.cssRules.length, 1, "Parser should skip @import rule");
            assert_equals(getComputedStyle(document.querySelector('#test3b'))
                .backgroundColor, "rgba(0, 0, 0, 0)",
                "CSS module @import should not succeed");
            assert_equals(getComputedStyle(document.querySelector('#test3'))
                .backgroundColor, "rgb(0, 255, 0)",
                "Rule after @import should still be applied");
        }, "An @import CSS Module should not load, but should not throw an exception");
    </script>
    <script type="module" onerror="unreachable()">
        import sheet from "./resources/malformed.css" assert { type: "css" };
        test(() => {
            document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
            assert_equals(window.errorCount, 0);
            assert_equals(sheet.cssRules.length, 1, "Import of malformed CSS should succeed and rules after the parse error should still be parsed");
            assert_equals(getComputedStyle(document.querySelector('#test4'))
                .backgroundColor, "rgba(0, 0, 0, 0)",
                "Malformed CSS rule should not be applied");
            assert_equals(getComputedStyle(document.querySelector('#test4b'))
                .backgroundColor, "rgb(0, 255, 0)",
                "Parsing should recover and rules after malformed rules should be applied");
        }, "A parse error should not prevent subsequent rules from being included in a CSS module");
    </script>
    <script type="module">
        promise_test(function (test) {
            const iframe = document.createElement("iframe");
            iframe.src = "resources/css-module-without-assertion-iframe.html";
            return new Promise(resolve => {
                iframe.onload = resolve;
                document.body.appendChild(iframe);
            }).then(event => {
                assert_equals(iframe.contentDocument.window_onerror, undefined);
                assert_equals(iframe.contentDocument.script_onerror.type, "error");
                assert_equals(getComputedStyle(iframe.contentDocument.querySelector('#test'))
                    .backgroundColor, "rgba(0, 0, 0, 0)",
                    "CSS module without type assertion should result in a fetch error");
            });
        }, "CSS module without type assertion should result in a fetch error");
    </script>
</body>