summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/cssom/shorthand-serialization.html
blob: 97e11da8b8a266c7efa3826272e3fdb7ce55ed86 (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
84
85
86
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Shorthand serialization should be done correctly.</title>
    <link rel="help" href="https://drafts.csswg.org/cssom/#serialize-a-css-declaration-block">
    <link rel="help" href="https://drafts.csswg.org/css-variables/#variables-in-shorthands">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
</head>
<body>
    <div id="foo1" style="background: red;">foo</div>
    <div id="foo2" style="background-color: blue; background: red !important; background-color: green;">foo</div>
    <div id="foo3" style="background-color: blue; background: red; background-color: green !important;">foo</div>

    <div id="foo4" style="margin-right: 10px; margin-left: 10px; margin-top: 10px; margin-bottom: 10px;">foo</div>
    <div id="foo5" style="margin-right: 10px; margin-left: 10px; margin-top: 10px; margin-bottom: 10px!important;">foo</div>
    <div id="foo6" style="margin-right: 10px !important; margin-left: 10px !important; margin-top: 10px !important; margin-bottom: 10px!important;">foo</div>

    <div id="foo7" style="background:var(--a);">foo</div>
    <div id="test"></div>

    <script>
        test(function() {
            var elem1 = document.getElementById('foo1');
            var elem2 = document.getElementById('foo2');
            var elem3 = document.getElementById('foo3');

            assert_false(elem1.style.cssText.endsWith('!important;'));
            assert_true(elem2.style.cssText.endsWith('!important;'));
            assert_false(elem3.style.background.endsWith('!important'));

        }, "Shorthand serialization with shorthand and longhands mixed.");

        test(function() {
            var elem4 = document.getElementById('foo4');
            var elem5 = document.getElementById('foo5');
            var elem6 = document.getElementById('foo6');

            assert_equals(elem4.style.cssText, 'margin: 10px;');
            assert_equals(elem4.style.margin, '10px');
            assert_equals(elem5.style.cssText, 'margin-right: 10px; margin-left: 10px; margin-top: 10px; margin-bottom: 10px !important;');
            assert_equals(elem5.style.margin, '');
            assert_equals(elem6.style.cssText, 'margin: 10px !important;');
            assert_equals(elem6.style.margin, '10px');
        }, "Shorthand serialization with just longhands.");

        test(function() {
          var elem7 = document.getElementById('foo7');

          assert_equals(elem7.style.background, 'var(--a)');
          assert_equals(elem7.style.backgroundPosition, '');
        }, "Shorthand serialization with variable and variable from other shorthand.");

        test(function() {
            var testElem = document.getElementById("test");
            testElem.style.margin = "20px 20px 20px 20px";
            assert_equals(testElem.style.margin, "20px");
            assert_equals(testElem.style.cssText, "margin: 20px;")
        }, "Shorthand serialization after setting");

        test(function() {
            const testElem = document.getElementById("test");
            testElem.style.cssText = "margin: initial;";
            assert_equals(testElem.style.margin, "initial");
            assert_equals(testElem.style.cssText, "margin: initial;");
        }, "Shorthand serialization with 'initial' value.");

        test(function() {
            const testElem = document.getElementById("test");
            testElem.style.setProperty("margin-top", "initial", "important");
            assert_equals(testElem.style.margin, "");
        }, "Shorthand serialization with 'initial' value, one longhand with important flag.");

        test(function() {
            const testElem = document.getElementById("test");
            testElem.style.cssText = "";
            testElem.style.setProperty("margin-top", "initial");
            testElem.style.setProperty("margin-right", "initial");
            testElem.style.setProperty("margin-bottom", "initial");
            testElem.style.setProperty("margin-left", "initial", "important");
            assert_equals(testElem.style.margin, "");
        }, "Shorthand serialization with 'initial' value, longhands set individually, one with important flag.");
    </script>
</body>
</html>