summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-contain/contain-paint-dynamic-001.html
blob: 899b5643c1a00358ee4b908b0e1ba813f4407783 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<meta charset="utf-8">
<title>Dynamic change to paint containment</title>
<link rel="help" href="https://drafts.csswg.org/css-contain/#contain-property">
<link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1765615">
<link rel="stylesheet" type="text/css" href="/fonts/ahem.css" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<meta name="assert" content="Verify paint containment is properly updated after dynamic change to the contain property.">
<style>
  /* Selectors for contain */
  #none .wrapper {
      contain: none;
  }
  #paint .wrapper {
      contain: paint;
  }
  #none_to_paint .wrapper {
      contain: none;
  }
  #paint_to_none .wrapper {
      contain: paint;
  }

  /* Selectors for testing absolute/fixed positioned elements */
  #top_spacer {
      height: 100px;
      background: lightgray;
  }
  .absolute_pos {
      position: absolute;
      top: 42px;
  }
  .fixed_pos {
      position: fixed;
      top: 42px;
  }

  /* Selectors for testing IFC (floats) */
  .floatleft {
      float: left;
  }
  .clearleft {
      clear: left;
  }

  /* Selectors for testing IFC (margin collapsing) */
  .blockmargin {
      margin: 25px 0;
  }
  .wrapper.blockmargin {
      background: lightgray;
  }

  .rect {
      background: black;
      width: 50px;
      height: 100px;
  }
</style>
<body>
  <div id="log"></div>

  <div id="top_spacer"></div>

  <div id="none">
    <div class="wrapper">
      <div class="absolute_pos"></div>
      <div class="fixed_pos"></div>
    </div>
    <div>
      <div class="floatleft rect"></div>
      <div class="wrapper">
        <div class="clearleft rect"></div>
      </div>
    </div>
    <div>
      <div class="wrapper blockmargin">
        <div class="rect blockmargin"></div>
      </div>
    </div>
  </div>

  <div id="paint">
    <div class="wrapper">
      <div class="absolute_pos"></div>
      <div class="fixed_pos"></div>
    </div>
    <div>
      <div class="floatleft rect"></div>
      <div class="wrapper">
        <div class="clearleft rect"></div>
      </div>
    </div>
    <div>
      <div class="wrapper blockmargin">
        <div class="rect blockmargin"></div>
      </div>
    </div>
  </div>

  <div id="none_to_paint">
    <div class="wrapper">
      <div class="absolute_pos"></div>
      <div class="fixed_pos"></div>
    </div>
    <div>
      <div class="floatleft rect"></div>
      <div class="wrapper">
        <div class="clearleft rect"></div>
      </div>
    </div>
    <div>
      <div class="wrapper blockmargin">
        <div class="rect blockmargin"></div>
      </div>
    </div>
  </div>

  <div id="paint_to_none">
    <div class="wrapper">
      <div class="absolute_pos"></div>
      <div class="fixed_pos"></div>
    </div>
    <div>
      <div class="floatleft rect"></div>
      <div class="wrapper">
        <div class="clearleft rect"></div>
      </div>
    </div>
    <div>
      <div class="wrapper blockmargin">
        <div class="rect blockmargin"></div>
      </div>
    </div>
  </div>

  <script>
    function verifyPaintContainment(id, applied) {
        let container = document.getElementById(id);
        let wrappers = container.getElementsByClassName("wrapper");

        // To verify the containment box establishes an absolute positioning
        // containing block and a fixed positioning containing block, we test
        // positions of absolutely/fixed positioned children (a bit below the
        // containment box rather than a bit below the top of the viewport).
        let containingBlockTop = wrappers[0].getBoundingClientRect().top;
        let absTop = container.getElementsByClassName("absolute_pos")[0]
            .getBoundingClientRect().top;
        assert_equals(absTop > containingBlockTop, applied, "absolute positioning containing block");
        let fixedTop = container.getElementsByClassName("fixed_pos")[0]
            .getBoundingClientRect().top;
        assert_equals(fixedTop > containingBlockTop, applied, "fixed positioning containing block");

        // To verify the containment box establishes an independent formatting
        // context, we test position the clear: left div with respect to the
        // float: left div.
        let floatLeft = wrappers[1].previousElementSibling;
        let clearLeft = wrappers[1].firstElementChild;
        let clearNextToFloat = Math.abs(floatLeft.getBoundingClientRect().top - clearLeft.getBoundingClientRect().top) <= 1;
        assert_equals(clearNextToFloat, applied, "independent formatting context");

        // In addition, we verify that the margin inside the containment box
        // are not collapsed.
        let IFCWithMargin = wrappers[2];
        let childWithMargin = IFCWithMargin.firstElementChild;
        let marginCollapsed = Math.abs(IFCWithMargin.getBoundingClientRect().height - childWithMargin.getBoundingClientRect().height) <= 1;
        assert_equals(!marginCollapsed, applied, "independent formatting context (margins collapsing)");
    }

    function setContain(id, value) {
        let container = document.getElementById(id);
        Array.from(container.getElementsByClassName("wrapper"))
            .forEach(element => element.style.contain = value);
    }

    promise_test(async () => {
        await document.fonts.ready;
        verifyPaintContainment("none", /* applied=*/false);
    }, "contain: none");

    promise_test(async () => {
        await document.fonts.ready;
        verifyPaintContainment("paint", /* applied=*/true);
    }, "contain: paint");

    promise_test(async () => {
        await document.fonts.ready;
        setContain("none_to_paint", "paint");
        verifyPaintContainment("none_to_paint", /* applied=*/true)
    }, "switching contain from none to paint");

    promise_test(async () => {
        await document.fonts.ready;
        setContain("paint_to_none", "none");
        verifyPaintContainment("paint_to_none", /* applied=*/false);
    }, "switching contain from paint to none");
  </script>
</body>