summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-anchor-position/at-position-try-cssom.html
blob: 42f82d9d4bd05aa66a9f243fd3772bc7cd9092d0 (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
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
<!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 CSSPositionTryDescriptors);
  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');

test(t => {
  const style = createStyle(t, `
    @position-try --pf {
      top: 10px;
      left: 20px;
      --x: 200px;
      color: red;
    }
  `);
  let declarations = style.sheet.cssRules[0].style;
  assert_equals(declarations.length, 2);
  assert_equals(declarations.item(0), 'top');
  assert_equals(declarations.item(1), 'left');
}, 'CSSPositionTryDescriptors.item');

test(t => {
  const style = createStyle(t, '@position-try --pf {}');
  let declarations = style.sheet.cssRules[0].style;
  assert_equals(declarations.length, 0);
  declarations.cssText = `color:red;top:10px;`;
  assert_equals(declarations.length, 1);
}, 'CSSPositionTryDescriptors.cssText');

let supported_properties = [
  'margin',
  'margin-top',
  'margin-right',
  'margin-bottom',
  'margin-left',
  'margin-block',
  'margin-block-start',
  'margin-block-end',
  'margin-inline',
  'margin-inline-start',
  'margin-inline-end',
  'inset',
  'top',
  'left',
  'right',
  'bottom',
  'inset-block',
  'inset-block-start',
  'inset-block-end',
  'inset-inline',
  'inset-inline-start',
  'inset-inline-end',
  'width',
  'height',
  'min-width',
  'max-width',
  'min-height',
  'max-height',
  'block-size',
  'min-block-size',
  'max-block-size',
  'inline-size',
  'min-inline-size',
  'max-inline-size',
  'place-self',
  'align-self',
  'justify-self',
  'position-anchor',
  'inset-area',
];

// A selection of unsupported properties.
let unsupported_properties = [
  'color',
  'align-items',
  'align-content',
  'background',
  'display',
  'position',
  'writing-mode',
  'direction',
  'syntax', // @property
];

let upperFirst = (x) => x[0].toUpperCase() + x.slice(1);
let lowerFirst = (x) => x[0].toLowerCase() + x.slice(1);
let toLowerCamelCase = (x) => lowerFirst(x.split('-').map(upperFirst).join(''));

// Test getting/setting the specified property on a CSSPositionTryDescriptors
// object. The property can either be supported or not supported,
// which determines the expected results.
function test_property(prop, supported) {
  test(t => {
    let decls = supported_properties.map(x => `${x}:unset;`).join('');
    let style = createStyle(t, `@position-try --pf { ${decls} }`);
    let declarations = style.sheet.cssRules[0].style;
    assert_equals(declarations.getPropertyValue(prop), supported ? 'unset' : '');
  }, `CSSPositionTryDescriptors.getPropertyValue(${prop})`);

  test(t => {
    let style = createStyle(t, '@position-try --pf {}');
    let declarations = style.sheet.cssRules[0].style;
    declarations.setProperty(prop, 'unset');
    assert_equals(declarations.getPropertyValue(prop), supported ? 'unset' : '');
  }, `CSSPositionTryDescriptors.setProperty(${prop})`);

  test(t => {
    let decls = supported_properties.map(x => `${x}:unset;`).join('');
    let style = createStyle(t, `@position-try --pf { ${decls} }`);
    let declarations = style.sheet.cssRules[0].style;
    assert_equals(declarations[prop], supported ? 'unset' : undefined);
  }, `CSSPositionTryDescriptors[${prop}] (set)`);

  test(t => {
    let style = createStyle(t, '@position-try --pf {}');
    let declarations = style.sheet.cssRules[0].style;
    declarations[prop] = 'unset';
    assert_equals(declarations.getPropertyValue(prop), supported ? 'unset' : '');
  }, `CSSPositionTryDescriptors[${prop}] (get)`);

  let camelCaseAttr = toLowerCamelCase(prop);
  if (camelCaseAttr != prop) {
    // Also test the camelCase version of the attribute.
    test(t => {
      let decls = supported_properties.map(x => `${x}:unset;`).join('');
      let style = createStyle(t, `@position-try --pf { ${decls} }`);
      let declarations = style.sheet.cssRules[0].style;
      assert_equals(declarations[camelCaseAttr], supported ? 'unset' : undefined);
    }, `CSSPositionTryDescriptors[${camelCaseAttr}] (get)`);

    test(t => {
      let style = createStyle(t, '@position-try --pf {}');
      let declarations = style.sheet.cssRules[0].style;
      declarations[camelCaseAttr] = 'unset';
      assert_equals(declarations.getPropertyValue(prop), supported ? 'unset' : '');
    }, `CSSPositionTryDescriptors[${camelCaseAttr}] (set)`);
  }
}

supported_properties.forEach(x => { test_property(x, /* supported */ true);  });
unsupported_properties.forEach(x => { test_property(x, /* supported */ false);  });

</script>