summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-cascade/scope-proximity.html
blob: c133a71e9a804b52c8d2556b1a8d1f26b3962636 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<title>@scope - proximity to root</title>
<link rel="help" href="https://drafts.csswg.org/css-cascade-6/#scope-proximity">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

function test_scope(script_element, callback_fn, description) {
  test((t) => {
    // The provided <script> element must be an immedate subsequent sibling of
    // a <template> element.
    let template_element = script_element.previousElementSibling;
    assert_equals(template_element.tagName, 'TEMPLATE');

    t.add_cleanup(() => {
      while (main.firstChild)
        main.firstChild.remove()
    });

    main.append(template_element.content.cloneNode(true));

    callback_fn();
  }, description);
}

function assert_green(selector) {
  assert_equals(getComputedStyle(main.querySelector(selector)).backgroundColor, 'rgb(0, 128, 0)');
}
function assert_not_green(selector) {
  assert_equals(getComputedStyle(main.querySelector(selector)).backgroundColor, 'rgb(0, 0, 0)');
}
</script>
<style>
  main * {
    background-color: black;
  }
</style>
<main id=main>
</main>

<template>
  <style>
    .item {
      padding: 0px;
      border: 5px solid red;
    }

    @scope (.light) {
      [id] { border-color: rgb(100, 100, 100); }
    }

    @scope (.dark) {
      [id] { border-color: rgb(200, 200, 200); }
    }
  </style>
  <div class=light>
    <div id=item1>
      <div class=dark>
        <div id=item2>
          <div class=light>
            <div id=item3>
              <div class=dark>
                <div id=item4></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
test_scope(document.currentScript, () => {
  assert_equals(getComputedStyle(item1).borderColor, 'rgb(100, 100, 100)');
  assert_equals(getComputedStyle(item2).borderColor, 'rgb(200, 200, 200)');
  assert_equals(getComputedStyle(item3).borderColor, 'rgb(100, 100, 100)');
  assert_equals(getComputedStyle(item4).borderColor, 'rgb(200, 200, 200)');
}, 'Alternating light/dark');
</script>


<template>
  <style>
    @scope (.b) {
      [id] { border-color:green; }
    }
    @scope (.a) {
      [id] { border-color:red; }
    }
  </style>
  <div class=a>
    <div class=b>
      <span id=item></span>
    </div>
  </div>
</template>
<script>
test_scope(document.currentScript, () => {
  assert_equals(getComputedStyle(item).borderColor, 'rgb(0, 128, 0)');
}, 'Proximity wins over order of appearance');
</script>


<template>
  <style>
    @scope (.a) {
      span[id] { border-color:green; }
    }
    @scope (.b) {
      [id] { border-color:red; }
    }
  </style>
  <div class=a>
    <div class=b>
      <span id=item></span>
    </div>
  </div>
</template>
<script>
test_scope(document.currentScript, () => {
  assert_equals(getComputedStyle(item).borderColor, 'rgb(0, 128, 0)');
}, 'Specificity wins over proximity');
</script>