| 12
 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
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 
 | <!DOCTYPE HTML>
<meta charset="UTF-8">
<title>CSS Toggles: toggle-visibility</title>
<link rel="author" title="L. David Baron" href="https://dbaron.org/">
<link rel="author" title="Google" href="http://www.google.com/">
<link rel="help" href="https://tabatkins.github.io/css-toggle/#toggle-visibility-property">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/toggle-helpers.js"></script>
<style id="style">
</style>
<body>
<div id="container"></div>
<script>
let style = document.getElementById("style");
let container = document.getElementById("container");
promise_test(async function() {
  style.innerText = `
    #container { toggle-group: t self; }
    #t, #t2 { toggle-root: t 1 at 0 group; }
    #trigger, #t2 { toggle-trigger: t; }
    #vis { toggle-visibility: toggle t; }
    #child { height: 100px; }
  `;
  container.innerHTML = `
    <div id="t"></div>
    <div id="vis">
      <div id="child"></div>
    </div>
    <div id="trigger"></div>
    <div id="t2"></div>
  `;
  let t = document.getElementById("t");
  let trigger = document.getElementById("trigger");
  let t2 = document.getElementById("t2");
  let vis = document.getElementById("vis");
  await wait_for_toggle_creation(t);
  const kVisibleHeight = 100;
  const kHiddenHeight = 0;
  assert_equals(vis.clientHeight, kHiddenHeight, "before first click");
  trigger.click();
  assert_equals(vis.clientHeight, kVisibleHeight, "after first click");
  trigger.click();
  assert_equals(vis.clientHeight, kHiddenHeight, "after second click");
  t.toggles.get("t").value = 1;
  assert_equals(vis.clientHeight, kVisibleHeight, "after toggle value setter");
  t2.click();
  assert_equals(vis.clientHeight, kHiddenHeight, "after click on other element in group");
}, "showing and hiding in response to toggle changes");
const action_tests = [
  {
    name: "focus",
    markup: `<input type="checkbox" id="check">`,
    action: () => {
      document.getElementById("check").focus();
    },
  },
  {
    name: "scrollIntoView",
    markup: `<input type="checkbox" id="check">`,
    action: () => {
      document.getElementById("check").scrollIntoView();
    },
  },
];
for (let test of action_tests) {
  promise_test(async function() {
    style.innerText = `
      #container { toggle-group: tab self; }
      #tab { toggle: tab 1 at 0 group; }
      #panel { toggle-visibility: toggle tab; }
    `;
    container.innerHTML = `
      <div id="tab">Tab</div>
      <div id="panel">
        ${test.markup}
      </div>
    `;
    let tab = document.getElementById("tab");
    let panel = document.getElementById("panel");
    await wait_for_toggle_creation(tab);
    let tab_toggle = tab.toggles.get("tab");
    assert_equals(tab_toggle.value, 0, "toggle value before focus");
    assert_equals(panel.clientHeight, 0, "panel height before focus");
    test.action();
    assert_equals(tab_toggle.value, 1, "toggle value after focus");
    assert_not_equals(panel.clientHeight, 0, "panel height after focus");
  }, `${test.name} inside hidden element changes a toggle to show it`);
}
const scope_tests = [
  `
    <div class="root-inactive">
      <div class="vis expect-hidden"></div>
    </div>
    <div class="vis expect-hidden"></div>
  `,
  `
    <div class="root-inactive-self">
      <div class="vis expect-hidden"></div>
    </div>
    <div class="vis expect-shown"></div>
  `,
  `
    <div class="root-active">
      <div class="vis expect-shown"></div>
    </div>
    <div class="vis expect-shown"></div>
  `,
  `
    <div class="root-active-self">
      <div class="vis expect-shown"></div>
    </div>
    <div class="vis expect-shown"></div>
  `,
];
for (let test of scope_tests) {
  promise_test(async function() {
    container.innerHTML = test;
    style.innerText = `
      .root-inactive { toggle-root: t 1 at 0; }
      .root-active { toggle-root: t 1 at 1; }
      .root-inactive-self { toggle-root: t 1 at 0 self; }
      .root-active-self { toggle-root: t 1 at 1 self; }
      .vis {
        toggle-visibility: toggle t;
      }
      .vis::before {
        content: "";
        display: block;
        height: 100px;
        width: 100px;
      }
    `;
    await wait_for_toggle_creation(container.querySelector('[class^="root-"]'));
    for (let e of container.querySelectorAll('.expect-hidden')) {
      assert_equals(e.clientHeight, 0);
    }
    for (let e of container.querySelectorAll('.expect-shown')) {
      assert_equals(e.clientHeight, 100);
    }
  }, `scope test: ${test}`);
}
async function wait_for_content_visibility_auto() {
  return new Promise(resolve => {
    requestAnimationFrame(() => {
      requestAnimationFrame(resolve);
    });
  });
}
promise_test(async function() {
  style.innerText = `
    #t { toggle-root: t 1 at 0; }
    #vis {
      toggle-visibility: toggle t;
      content-visibility: auto;
      min-height: 50px;
    }
    #child { height: 100px; }
    #spacer { height: 300vh; }
  `;
  // TODO (dbaron): Why doesn't padding-top on #container have the same effect
  // as this spacer?
  container.innerHTML = `
    <div id="spacer"></div>
    <div id="t"></div>
    <div id="vis">
      <div id="child"></div>
    </div>
  `;
  let t = document.getElementById("t");
  let vis = document.getElementById("vis");
  let spacer = document.getElementById("spacer");
  await wait_for_toggle_creation(t);
  let toggle = t.toggles.get("t");
  const kVisibleHeight = 100;
  const kHiddenHeight = 50;
  await wait_for_content_visibility_auto();
  assert_equals(vis.clientHeight, kHiddenHeight, "with toggle inactive and offscreen");
  toggle.value = 1;
  await wait_for_content_visibility_auto();
  assert_equals(vis.clientHeight, kHiddenHeight, "with toggle active and offscreen");
  spacer.remove();
  await wait_for_content_visibility_auto();
  assert_equals(vis.clientHeight, kVisibleHeight, "with toggle active and onscreen");
  toggle.value = 0;
  await wait_for_content_visibility_auto();
  assert_equals(vis.clientHeight, kHiddenHeight, "with toggle inactive and onscreen");
  toggle.value = 1;
  await wait_for_content_visibility_auto();
  assert_equals(vis.clientHeight, kVisibleHeight, "with toggle active and onscreen (2)");
  toggle.value = 0;
  t.before(spacer);
  await wait_for_content_visibility_auto();
  assert_equals(vis.clientHeight, kHiddenHeight, "with toggle inactive and offscreen (2)");
  spacer.remove();
  await wait_for_content_visibility_auto();
  assert_equals(vis.clientHeight, kHiddenHeight, "with toggle inactive and onscreen (2)");
  toggle.value = 1;
  await wait_for_content_visibility_auto();
  assert_equals(vis.clientHeight, kVisibleHeight, "with toggle active and onscreen (3)");
  t.before(spacer);
  await wait_for_content_visibility_auto();
  assert_equals(vis.clientHeight, kHiddenHeight, "with toggle active and offscreen (2)");
}, "interaction of toggle-visibility and content-visibility: auto");
promise_test(async function() {
  style.innerText = `
    #t { toggle-root: t 1 at 0; }
    #vis {
      toggle-visibility: toggle t;
      content-visibility: hidden;
    }
    #child { height: 100px; }
  `;
  container.innerHTML = `
    <div id="t"></div>
    <div id="vis">
      <div id="child"></div>
    </div>
  `;
  let t = document.getElementById("t");
  let vis = document.getElementById("vis");
  await wait_for_toggle_creation(t);
  let toggle = t.toggles.get("t");
  const kVisibleHeight = 100;
  const kHiddenHeight = 0;
  assert_equals(vis.clientHeight, kHiddenHeight, "with toggle inactive and offscreen");
  toggle.value = 1;
  assert_equals(vis.clientHeight, kHiddenHeight, "with toggle active and offscreen");
}, "interaction of toggle-visibility and content-visibility: hidden");
</script>
 |