summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-anchor-position/at-position-fallback-cssom.html
blob: df295bf2d0adc79d7792775d29e8373a88609112 (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
<!DOCTYPE html>
<title>Tests the CSSOM interfaces of @position-fallback and @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="not-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-fallback --pf { @try { left: anchor(right); } }');
  const positionFallbackRule = style.sheet.cssRules[0];
  assert_true(positionFallbackRule instanceof CSSPositionFallbackRule);
  assert_equals(positionFallbackRule.name, '--pf');
  assert_equals(positionFallbackRule.cssRules.length, 1);

  const tryRule = positionFallbackRule.cssRules[0];
  assert_true(tryRule instanceof CSSTryRule);
  assert_true(tryRule.style instanceof CSSStyleDeclaration);
  assert_equals(tryRule.style.length, 1);
  assert_equals(tryRule.style.left, 'anchor(right)');
}, 'CSSPositionFallbackRule and CSSTryRule attribute values');

test(t => {
  const style = createStyle(t, '@position-fallback --pf {}');
  const positionFallbackRule = style.sheet.cssRules[0];

  assert_equals(positionFallbackRule.insertRule('@try {}', 0), 0,
                '@try rules can be inserted');
  assert_throws_dom('HierarchyRequestError',
                    () => positionFallbackRule.insertRule('#target { color: red; }', 1),
                    'style rules cannot be inserted');
  assert_throws_dom('HierarchyRequestError',
                    () => positionFallbackRule.insertRule('@keyframes foo {}', 1),
                    'other at-rules cannot be inserted');
}, 'CSSPositionFallbackRule.insertRule can insert @try rules only');


test(t => {
  const style = createStyle(t, `
    @position-fallback --pf { @try { top: anchor(top); } }
    #anchor, #not-anchor, #target {
      position: absolute; width: 100px; height: 100px; left: 0;
    }
    #anchor { top: 100px; anchor-name: --a; }
    #not-anchor { top: 200px; anchor-name: --b; }
    #target { position-fallback: --pf; anchor-default: --a; }
  `);
  const positionFallbackRule = style.sheet.cssRules[0];
  const tryRule = positionFallbackRule.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 `@try` and should affect position fallback.
  tryRule.style.setProperty('left', 'anchor(right)');
  assert_equals(target.getBoundingClientRect().left, 100);
  assert_equals(target.getBoundingClientRect().top, 100);

  // These properties are disallowed in `@try` rule, and hence should not affect
  // position fallback.
  tryRule.style.setProperty('anchor-default', '--b');
  tryRule.style.setProperty('position', 'static');
  assert_equals(target.getBoundingClientRect().left, 100);
  assert_equals(target.getBoundingClientRect().top, 100);
}, 'CSSTryRule.style.setProperty setting allowed and disallowed properties');

</script>