File: margin-collapse-through-for-various-height-values.tentative.html

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (153 lines) | stat: -rw-r--r-- 6,506 bytes parent folder | download
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
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="author" title="Oriol Brufau" href="obrufau@igalia.com">
<link rel="help" href="https://www.w3.org/TR/CSS22/box.html#collapsing-margins">
<link rel="help" href="https://github.com/w3c/csswg-drafts/issues/8919">
<link rel="help" href="https://github.com/servo/servo/pull/32064">
<meta name="assert" content="
  Margins can collapse through when the used height is zero, regardless of the computed values
  of `height`, `min-height` and `max-height`.

  Note that this test uses sizing values which didn't exist in CSS2, but since the expectations
  only depend on the used height, the test can still pass on implementations that don't support
  these values.
">

<style>
.before, .after {
  overflow: hidden;
}
.wrapper {
  border: 1px solid;
  margin-bottom: 2em;
}
.test {
  margin: 50px;
}
</style>

<div id="log"></div>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
let testcases = [
  // Tests for `height`
  { height: "auto" },
  { height: "0px" },
  { height: "1px" },
  { height: "0%", parentHeight: "auto" },
  { height: "0%", parentHeight: "0px" },
  { height: "0%", parentHeight: "100px" },
  { height: "1%", parentHeight: "auto" },
  { height: "1%", parentHeight: "0px" },
  { height: "1%", parentHeight: "100px" },
  { height: "calc(0px + 0%)", parentHeight: "auto" },
  { height: "calc(0px + 0%)", parentHeight: "0px" },
  { height: "calc(0px + 0%)", parentHeight: "100px" },
  { height: "calc(0px + 1%)", parentHeight: "auto" },
  { height: "calc(0px + 1%)", parentHeight: "0px" },
  { height: "calc(0px + 1%)", parentHeight: "100px" },
  { height: "calc(1px + 0%)", parentHeight: "auto" },
  { height: "calc(1px + 0%)", parentHeight: "0px" },
  { height: "calc(1px + 0%)", parentHeight: "100px" },
  { height: "calc(1px + 1%)", parentHeight: "auto" },
  { height: "calc(1px + 1%)", parentHeight: "0px" },
  { height: "calc(1px + 1%)", parentHeight: "100px" },
  { height: "stretch", parentHeight: "auto" },
  { height: "stretch", parentHeight: "0px" },
  { height: "stretch", parentHeight: "100px" },

  // Tests for `min-height` (with `height: auto`)
  { minHeight: "auto" },
  { minHeight: "0px" },
  { minHeight: "1px" },
  { minHeight: "0%", parentHeight: "auto" },
  { minHeight: "0%", parentHeight: "0px" },
  { minHeight: "0%", parentHeight: "100px" },
  { minHeight: "1%", parentHeight: "auto" },
  { minHeight: "1%", parentHeight: "0px" },
  { minHeight: "1%", parentHeight: "100px" },
  { minHeight: "calc(0px + 0%)", parentHeight: "auto" },
  { minHeight: "calc(0px + 0%)", parentHeight: "0px" },
  { minHeight: "calc(0px + 0%)", parentHeight: "100px" },
  { minHeight: "calc(0px + 1%)", parentHeight: "auto" },
  { minHeight: "calc(0px + 1%)", parentHeight: "0px" },
  { minHeight: "calc(0px + 1%)", parentHeight: "100px" },
  { minHeight: "calc(1px + 0%)", parentHeight: "auto" },
  { minHeight: "calc(1px + 0%)", parentHeight: "0px" },
  { minHeight: "calc(1px + 0%)", parentHeight: "100px" },
  { minHeight: "calc(1px + 1%)", parentHeight: "auto" },
  { minHeight: "calc(1px + 1%)", parentHeight: "0px" },
  { minHeight: "calc(1px + 1%)", parentHeight: "100px" },
  { minHeight: "stretch", parentHeight: "auto" },
  { minHeight: "stretch", parentHeight: "0px" },
  { minHeight: "stretch", parentHeight: "100px" },

  // Tests for `max-height` (with `height: 1px`)
  { height: "1px", maxHeight: "none" },
  { height: "1px", maxHeight: "0px" },
  { height: "1px", maxHeight: "1px" },
  { height: "1px", maxHeight: "0%", parentHeight: "auto" },
  { height: "1px", maxHeight: "0%", parentHeight: "0px" },
  { height: "1px", maxHeight: "0%", parentHeight: "100px" },
  { height: "1px", maxHeight: "1%", parentHeight: "auto" },
  { height: "1px", maxHeight: "1%", parentHeight: "0px" },
  { height: "1px", maxHeight: "1%", parentHeight: "100px" },
  { height: "1px", maxHeight: "calc(0px + 0%)", parentHeight: "auto" },
  { height: "1px", maxHeight: "calc(0px + 0%)", parentHeight: "0px" },
  { height: "1px", maxHeight: "calc(0px + 0%)", parentHeight: "100px" },
  { height: "1px", maxHeight: "calc(0px + 1%)", parentHeight: "auto" },
  { height: "1px", maxHeight: "calc(0px + 1%)", parentHeight: "0px" },
  { height: "1px", maxHeight: "calc(0px + 1%)", parentHeight: "100px" },
  { height: "1px", maxHeight: "calc(1px + 0%)", parentHeight: "auto" },
  { height: "1px", maxHeight: "calc(1px + 0%)", parentHeight: "0px" },
  { height: "1px", maxHeight: "calc(1px + 0%)", parentHeight: "100px" },
  { height: "1px", maxHeight: "calc(1px + 1%)", parentHeight: "auto" },
  { height: "1px", maxHeight: "calc(1px + 1%)", parentHeight: "0px" },
  { height: "1px", maxHeight: "calc(1px + 1%)", parentHeight: "100px" },
  { height: "1px", maxHeight: "stretch", parentHeight: "auto" },
  { height: "1px", maxHeight: "stretch", parentHeight: "0px" },
  { height: "1px", maxHeight: "stretch", parentHeight: "100px" },
];

// The checks below force layout, so as an optimization, generate all elements first.
let generatedData = testcases.map(testcase => {
  let { height, minHeight, maxHeight, parentHeight, ...unrecognized } = testcase;
  assert_array_equals(Object.keys(unrecognized), [], "No unrecognized key.");
  let serialization = JSON.stringify(testcase);
  let wrapper = document.createElement("div");
  wrapper.className = "wrapper";
  wrapper.style.height = parentHeight || "";
  let before = document.createElement("div");
  before.className = "before";
  before.textContent = serialization;
  let after = document.createElement("div");
  after.className = "after";
  let test = document.createElement("div");
  test.className = "test";
  test.style.height = height || "";
  test.style.minHeight = minHeight || "";
  test.style.maxHeight = maxHeight || "";
  wrapper.append(before, test, after);
  document.body.append(wrapper);
  return { serialization, before, test, after };
});

function round(value) {
  return Math.round(value * 10) / 10;
}

for (let data of generatedData) {
  test(() => {
    let { before, test, after } = data;
    let height = round(test.getBoundingClientRect().height);
    let marginSum = round(after.getBoundingClientRect().top - before.getBoundingClientRect().bottom) - height;
    if (height === 0) {
      assert_equals(marginSum, 50, "margins should collapse through");
    } else {
      assert_equals(marginSum, 100, "margins should NOT collapse through when height is " + height);
    }
  }, data.serialization);
}
</script>