summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-contain/container-queries/container-units-selection.html
blob: 16a44cd176618d0ed21e6cb8921fd286234441fb (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
<!doctype html>
<title>Container Relative Units: Advanced Container Selection</title>
<link rel="help" href="https://drafts.csswg.org/css-contain-3/#container-lengths">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/cq-testcommon.js"></script>
<style>
  * { writing-mode: initial; }
  .inline { container-type: inline-size; }
  .size { container-type: size; }
  .vertical { writing-mode: vertical-rl; }
  .w500 { width: 500px; }
  .h400 { height: 400px; }
  .w300 { width: 300px; }
  .h200 { height: 200px; }
  .w100 { width: 100px; }
</style>
<div id=ref></div>
<div id=c1>
  <div id=c2>
    <div id=c3>
      <div id=c4>
        <div id=child>Test</div>
      </div>
    </div>
  </div>
</div>
<script>
  setup(() => assert_implements_container_queries());

  function assert_unit_equals(element, actual, expected) {
    try {
      element.style.padding = actual;
      ref.style.padding = expected;
      assert_equals(getComputedStyle(element).paddingLeft,
                    getComputedStyle(ref).paddingLeft);
    } finally {
      element.style = '';
      ref.style = '';
    }
  }

  test(() => {
    try {
      c1.className = 'inline w500'; // Selected by nothing.
      c2.className = 'size h400 w300'; // Selected by cqh, cqb.
      c3.className = 'inline w100'; // Selected by cqw, cqi.
      assert_unit_equals(child, '10cqw', '10px');
      assert_unit_equals(child, '10cqi', '10px');
      assert_unit_equals(child, '10cqh', '40px');
      assert_unit_equals(child, '10cqb', '40px');
      assert_unit_equals(child, '10cqmin', '10px');
      assert_unit_equals(child, '10cqmax', '40px');

      c3.className = ''; // cqw, cqi now selects c2 instead.
      assert_unit_equals(child, '10cqw', '30px');
      assert_unit_equals(child, '10cqi', '30px');
      assert_unit_equals(child, '10cqh', '40px');
      assert_unit_equals(child, '10cqb', '40px');
      assert_unit_equals(child, '10cqmin', '30px');
      assert_unit_equals(child, '10cqmax', '40px');

    } finally {
      for (let c of [c1, c2, c3, c4, child])
        c.className = '';
    }
  }, 'Container units select the proper container');

  test(() => {
    try {
      c1.className = 'size w500 h400';
      c2.className = 'inline w300 h200';

      // [cqi, cqb] corresponds to [cqw, cqh].
      assert_unit_equals(child, '10cqw', '30px');
      assert_unit_equals(child, '10cqi', '30px');
      assert_unit_equals(child, '10cqh', '40px');
      assert_unit_equals(child, '10cqb', '40px');

      child.className = 'vertical';
      // [cqi, cqb] now corresponds to [cqh, cqw].
      assert_unit_equals(child, '10cqw', '30px');
      assert_unit_equals(child, '10cqi', '40px');
      assert_unit_equals(child, '10cqh', '40px');
      assert_unit_equals(child, '10cqb', '30px');

      c2.classList.add('vertical');
      // The inline containment on #c2 now applies to the vertical axis.
      // [cqi, cqb] still corresponds to [cqh, cqw], but we now expect
      // cqh to resolve against #c2, and cqw to resolve against #c1.
      assert_unit_equals(child, '10cqw', '50px');
      assert_unit_equals(child, '10cqi', '20px');
      assert_unit_equals(child, '10cqh', '20px');
      assert_unit_equals(child, '10cqb', '50px');

    } finally {
      for (let c of [c1, c2, c3, c4, child])
        c.className = '';
    }
  }, 'Units respond to the writing-mode of the element');
</script>