summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-nesting/serialize-group-rules-with-decls.tentative.html
blob: c3b6bb7be749e27d3ead37d2f2d32ab37d350828 (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
<!doctype html>
<title>Serialization of declarations in group rules</title>
<link rel="author" title="Steinar H. Gunderson" href="mailto:sesse@chromium.org">
<link rel="help" href="https://github.com/w3c/csswg-drafts/issues/7850">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<style id="test-sheet"></style>
<script>
  function serialize(cssText) {
    let [ss] = document.styleSheets;
    while (ss.rules.length) {
      ss.removeRule(0)
    }
    ss.insertRule(cssText);
    return ss.rules[0].cssText;
  }

  function assert_unchanged(cssText) {
    assert_equals(serialize(cssText), cssText);
  }

  function assert_becomes(cssText, serializedCssText) {
    assert_equals(serialize(cssText), serializedCssText);
  }

  // Declarations are serialized on one line, rules on two.
  test(() => {
    assert_unchanged("@media screen {\n  div { color: red; background-color: green; }\n}");
    assert_unchanged("div {\n  @media screen { color: red; background-color: green; }\n}");
  });

  // Mixed declarations/rules are on two lines.
  test(() => {
    assert_unchanged("div {\n  @supports selector(&) {\n  color: red; background-color: green;\n  &:hover { color: navy; }\n}\n}");
  });

  // & {} rules are removed if and only if they are first, and they have no children.
  test(() => {
    assert_becomes("div { @media screen { & { color: red; } }",
                   "div {\n  @media screen { color: red; }\n}");
    assert_becomes("div { @media screen { & { color: red; &:hover { } } }",
                   "div {\n  @media screen {\n  & {\n  color: red;\n  &:hover { }\n}\n}\n}");
    assert_becomes("div { @media screen { &.cls { color: red; } & { color: red; }",
                   "div {\n  @media screen {\n  &.cls { color: red; }\n  & { color: red; }\n}\n}");
    assert_becomes("div { @media screen { & { color: red; } & { color: red; }",
                   "div {\n  @media screen {\n  color: red;\n  & { color: red; }\n}\n}");
    assert_becomes("div { @media screen { color: red; & { color: red; }",
                   "div {\n  @media screen {\n  color: red;\n  & { color: red; }\n}\n}");
    assert_becomes("div { @media screen { color: red; & { color: blue; }",
                   "div {\n  @media screen {\n  color: red;\n  & { color: blue; }\n}\n}");
    assert_becomes("div { @media screen { &, p > & { color: blue; }",
                   "div {\n  @media screen {\n  &, p > & { color: blue; }\n}\n}");
  });

  // They are not removed from regular rules.
  test(() => {
    assert_becomes("div { & { color: red; } }", "div {\n  & { color: red; }\n}");
  });

  // Empty rules (confusingly?) serialize different between style rules
  // and conditional group rules.
  test(() => {
    assert_unchanged("@media screen {\n}");
    assert_unchanged("div { }");
    assert_unchanged("div {\n  @media screen {\n}\n}");
    assert_unchanged("@media screen {\n  div { }\n}");
  });
</script>