summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-anchor-position/at-position-try-cssom.html
blob: 91172c518531316323566c9bba24ecfc0e2e27fd (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
<!DOCTYPE html>
<title>Tests the CSSOM interfaces of @position-try rules</title>
<link rel="help" href="https://drafts.csswg.org/css-anchor-position-1/#interfaces">
<link rel="author" href="mailto:xiaochengh@chromium.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<div id="anchor"></div>
<div id="other-anchor"></div>
<div id="target"></div>

<script>
function createStyle(t, text) {
  const style = document.createElement('style');
  style.textContent = text;
  t.add_cleanup(() => style.remove());
  document.head.appendChild(style);
  return style;
}

test(t => {
  const style = createStyle(
      t, '@position-try --pf { left: anchor(right); }');
  const positionTryRule = style.sheet.cssRules[0];
  assert_true(positionTryRule instanceof CSSPositionTryRule);
  assert_equals(positionTryRule.name, '--pf');
  assert_true(positionTryRule.style instanceof CSSStyleDeclaration);
  assert_equals(positionTryRule.style.length, 1);
  assert_equals(positionTryRule.style.left, 'anchor(right)');
}, 'CSSPositionTryRule attribute values');

test(t => {
  const style = createStyle(t, `
    @position-try --pf { top: anchor(top); left: 0; }
    #anchor, #other-anchor, #target {
      position: absolute; width: 100px; height: 100px;
    }
    #anchor { top: 100px; left: 0; anchor-name: --a; }
    #other-anchor { top: 200px; left: 0; anchor-name: --b; }
    #target { position-try-options: --pf; position-anchor: --a; left: 999999px; }
  `);
  const positionTryRule = style.sheet.cssRules[0];

  // Check the initial position fallback result
  assert_equals(target.getBoundingClientRect().left, 0);
  assert_equals(target.getBoundingClientRect().top, 100);

  // `left` is an allowed property in `@position-try` and should affect position fallback.
  positionTryRule.style.setProperty('left', 'anchor(right)');
  assert_equals(target.getBoundingClientRect().left, 100);
  assert_equals(target.getBoundingClientRect().top, 100);

  // This property are disallowed in `@position-try` rule, and hence should not affect
  // position fallback.
  positionTryRule.style.setProperty('position', 'static');
  assert_equals(target.getBoundingClientRect().left, 100);
  assert_equals(target.getBoundingClientRect().top, 100);

  // `position-anchor` is an allowed property in `@position-try` and should affect position fallback.
  positionTryRule.style.setProperty('position-anchor', '--b');
  assert_equals(target.getBoundingClientRect().left, 100);
  assert_equals(target.getBoundingClientRect().top, 200);

}, 'CSSPositionTryRule.style.setProperty setting allowed and disallowed properties');

</script>