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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
<!DOCTYPE html>
<html>
<title>Tests that when an anchor name is mutated, the anchor-positioned element adjusts to the new anchor</title>
<link rel="author" href="mailto:kiet.ho@apple.com">
<link rel="help" href="https://drafts.csswg.org/css-anchor-position-1/#name">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/rendering-utils.js"></script>
<style>
.containing-block {
position: relative;
width: 300px;
height: 300px;
border: 1px black solid;
}
.cell {
width: 100px;
height: 100px;
}
#anchor-1 {
position: absolute;
top: 0;
left: 0;
background: green;
}
#anchor-2 {
position: absolute;
top: 100px;
left: 0;
background: blue;
}
.anchor {
anchor-name: --anchor;
}
#anchor-positioned {
background: orange;
position: absolute;
top: 0;
left: 0;
}
.positioned-using-anchor-function-explicit-name {
position: absolute;
top: anchor(--anchor bottom) !important;
left: anchor(--anchor right) !important;
}
.positioned-using-anchor-function-implicit-name {
position: absolute;
position-anchor: --anchor;
top: anchor(bottom) !important;
left: anchor(right) !important;
}
.positioned-using-position-area {
position: absolute;
position-anchor: --anchor;
position-area: bottom right;
}
</style>
<body>
<main id="main">
</main>
<template id="test-template">
<div class="containing-block">
<div class="cell" id="anchor-1"></div>
<div class="cell" id="anchor-2"></div>
<div class="cell" id="anchor-positioned"></div>
</div>
</template>
<script>
function inflate(t, template_element) {
const main_element = document.getElementById("main");
t.add_cleanup(() => main_element.replaceChildren());
main_element.append(template_element.content.cloneNode(true));
}
const test_template = document.getElementById("test-template");
const positioning_methods = [
"positioned-using-anchor-function-explicit-name",
"positioned-using-anchor-function-implicit-name",
"positioned-using-position-area"
]
for (let positioning_method of positioning_methods) {
promise_test(async (t) => {
inflate(t, test_template);
const anchor1 = document.getElementById("anchor-1");
const anchorPositioned = document.getElementById("anchor-positioned");
anchorPositioned.classList.add(positioning_method);
// Here, anchorPositioned should be at the default location (top-left of containing block)
// This is because the anchor elements don't have an anchor name yet.
assert_equals(anchorPositioned.offsetTop, 0);
assert_equals(anchorPositioned.offsetLeft, 0);
anchor1.classList.add("anchor");
// Here, anchorPositioned should be bottom-right of anchor1
assert_equals(anchorPositioned.offsetTop, 100);
assert_equals(anchorPositioned.offsetLeft, 100);
}, `Tests when an anchor-name is added later ${positioning_method}`);
promise_test(async (t) => {
inflate(t, test_template);
const anchor1 = document.getElementById("anchor-1");
const anchorPositioned = document.getElementById("anchor-positioned");
anchor1.classList.add("anchor");
anchorPositioned.classList.add(positioning_method);
// Here, anchorPositioned should be bottom-right of anchor1
assert_equals(anchorPositioned.offsetTop, 100);
assert_equals(anchorPositioned.offsetLeft, 100);
anchor1.classList.remove("anchor");
// Here, anchorPositioned should be at the default location (top-left of containing block)
// This is because the anchor elements don't have an anchor name.
assert_equals(anchorPositioned.offsetTop, 0);
assert_equals(anchorPositioned.offsetLeft, 0);
}, `Tests when an anchor-name is removed later ${positioning_method}`);
promise_test(async (t) => {
inflate(t, test_template);
const anchor1 = document.getElementById("anchor-1");
const anchor2 = document.getElementById("anchor-2");
const anchorPositioned = document.getElementById("anchor-positioned");
anchor1.classList.add("anchor");
anchorPositioned.classList.add(positioning_method);
// Here, anchorPositioned should be bottom-right of anchor1
assert_equals(anchorPositioned.offsetTop, 100);
assert_equals(anchorPositioned.offsetLeft, 100);
anchor1.classList.remove("anchor");
anchor2.classList.add("anchor");
// Here, anchorPositioned should be bottom-right of anchor2
assert_equals(anchorPositioned.offsetTop, 200);
assert_equals(anchorPositioned.offsetLeft, 100);
}, `Tests when an anchor-name moves from one element to another ${positioning_method}`);
promise_test(async (t) => {
inflate(t, test_template);
const anchor1 = document.getElementById("anchor-1");
const anchor2 = document.getElementById("anchor-2");
const anchorPositioned = document.getElementById("anchor-positioned");
anchor1.classList.add("anchor");
anchorPositioned.classList.add(positioning_method);
// Here, anchorPositioned should be bottom-right of anchor1
assert_equals(anchorPositioned.offsetTop, 100);
assert_equals(anchorPositioned.offsetLeft, 100);
anchor2.classList.add("anchor");
// Here, anchorPositioned should be bottom-right of anchor2
assert_equals(anchorPositioned.offsetTop, 200);
assert_equals(anchorPositioned.offsetLeft, 100);
}, `Tests when a new anchor candidate is added (which wins out previous anchor) ${positioning_method}`);
promise_test(async (t) => {
inflate(t, test_template);
const anchor1 = document.getElementById("anchor-1");
const anchor2 = document.getElementById("anchor-2");
const anchorPositioned = document.getElementById("anchor-positioned");
anchor2.classList.add("anchor");
anchorPositioned.classList.add(positioning_method);
// Here, anchorPositioned should be bottom-right of anchor2
assert_equals(anchorPositioned.offsetTop, 200);
assert_equals(anchorPositioned.offsetLeft, 100);
anchor1.classList.add("anchor");
// Here, anchorPositioned should be bottom-right of anchor2
assert_equals(anchorPositioned.offsetTop, 200);
assert_equals(anchorPositioned.offsetLeft, 100);
}, `Tests when a new anchor candidate is added (which loses out previous anchor) ${positioning_method}`);
}
</script>
</body>
</html>
|