Skip to content

Commit 3ca0e2c

Browse files
authored
Merge pull request #244 from tuespetre/fallbacks-nested-functions
Fallbacks: flip anchor functions that are nested in other function calls
2 parents f1d3bcc + ede0a6e commit 3ca0e2c

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ following features:
8888
- The `flip-start` `try-tactic` is only partially supported. The tactic is
8989
only applied to property names and anchor sides.
9090
- a `position-area` as a `try-tactic`
91-
- Fallback does does not support anchor functions that are nested or passed
92-
through custom properties.
91+
- Fallback does not support percentage anchor-side values, nor anchor
92+
functions that are passed through custom properties.
9393
- Polyfill allows anchoring in scroll more permissively than the spec allows,
9494
for instance without a default `position-anchor`.
9595
- `anchor-scope` property on pseudo-elements

src/fallback.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,20 @@ export function applyTryTacticToBlock(
421421
declarations[key] ??= 'revert';
422422
}
423423

424-
// todo: This does not support anchor functions that are nested or passed
425-
// through custom properties.
426-
if (isAnchorFunction(valueAst.children.first)) {
427-
valueAst.children.first.children.forEach((item) => {
428-
if (isIdentifier(item) && isAnchorSide(item.name)) {
429-
item.name = mapAnchorSide(item.name, tactic);
424+
// todo: This does not support percentage anchor-side values, nor anchor
425+
// functions that are passed through custom properties.
426+
csstree.walk(valueAst, {
427+
visit: 'Function',
428+
enter(node) {
429+
if (isAnchorFunction(node)) {
430+
node.children.forEach((item) => {
431+
if (isIdentifier(item) && isAnchorSide(item.name)) {
432+
item.name = mapAnchorSide(item.name, tactic);
433+
}
434+
});
430435
}
431-
});
432-
}
436+
},
437+
});
433438

434439
if (key === 'position-area') {
435440
valueAst.children.forEach((id) => {

tests/unit/fallback.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ describe('fallback', () => {
4747
bottom: 'anchor(self-start)',
4848
},
4949
],
50+
[
51+
'flips anchor functions that are nested',
52+
`${propWrap('bottom')}: calc(anchor(top) + 5px);${propWrap('top')}:calc(calc(anchor(--a top) + 5px) - 0.5em)`,
53+
{
54+
top: 'calc(anchor(bottom) + 5px)',
55+
bottom: 'calc(calc(anchor(--a bottom) + 5px) - 0.5em)',
56+
},
57+
],
5058
[
5159
'does not flip left and right anchors',
5260
`${propWrap('left')}: anchor(right);${propWrap('right')}:anchor(--a left)`,
@@ -147,6 +155,14 @@ describe('fallback', () => {
147155
right: 'anchor(self-start)',
148156
},
149157
],
158+
[
159+
'flips anchor functions that are nested',
160+
`${propWrap('right')}: calc(anchor(left) + 5px);${propWrap('left')}:calc(calc(anchor(--a left) + 5px) - 0.5em)`,
161+
{
162+
left: 'calc(anchor(right) + 5px)',
163+
right: 'calc(calc(anchor(--a right) + 5px) - 0.5em)',
164+
},
165+
],
150166
[
151167
'does not flip top and bottom anchors',
152168
`${propWrap('top')}: anchor(bottom);${propWrap('bottom')}:anchor(--a top)`,

0 commit comments

Comments
 (0)