Skip to content

Commit 0b4dc3a

Browse files
authored
Improve transition animation value serialization (#17970)
1 parent aaf14c3 commit 0b4dc3a

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Improves serialization of transition animation values in generated CSS

‎packages/astro/src/runtime/server/transition.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,22 @@ type AnimationBuilder = {
191191
[key: string]: string[] | ((k: string) => string);
192192
};
193193

194+
// Keep animation values within their declaration while preserving valid CSS functions and strings.
195+
const CSS_VALUE_DELIMITERS = /[;{}]/g;
196+
197+
function escapeCSSValue(value: string | number) {
198+
return value.toString().replace(CSS_VALUE_DELIMITERS, (character) => {
199+
return `\\${character.charCodeAt(0).toString(16).toUpperCase()} `;
200+
});
201+
}
202+
194203
function addAnimationProperty(builder: AnimationBuilder, prop: string, value: string | number) {
204+
const escapedValue = escapeCSSValue(value);
195205
let arr = builder[prop];
196206
if (Array.isArray(arr)) {
197-
arr.push(value.toString());
207+
arr.push(escapedValue);
198208
} else {
199-
builder[prop] = [value.toString()];
209+
builder[prop] = [escapedValue];
200210
}
201211
}
202212

‎packages/astro/test/units/render/transitions.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ describe('stringifyAnimation', () => {
9292
assert.ok(result.includes('both'));
9393
});
9494

95+
it('escapes CSS delimiters in animation values', () => {
96+
const result = stringifyAnimation({
97+
name: 'fade',
98+
duration: '1ms;}custom{value',
99+
});
100+
101+
assert.ok(result.includes('animation-duration: 1ms\\3B \\7D custom\\7B value;'));
102+
assert.ok(!result.includes('}custom{'));
103+
});
104+
105+
it('preserves CSS functions in animation values', () => {
106+
const result = stringifyAnimation({
107+
name: 'fade',
108+
duration: 'calc(1s + var(--transition-delay, 200ms))',
109+
});
110+
111+
assert.ok(result.includes('calc(1s + var(--transition-delay, 200ms))'));
112+
});
113+
95114
it('accepts an array of animations', () => {
96115
const result = stringifyAnimation([{ name: 'fade-in' }, { name: 'slide-in' }]);
97116
assert.ok(result.includes('fade-in'));
@@ -224,4 +243,26 @@ describe('renderTransition', () => {
224243
assert.equal($('head script').length, 0);
225244
assert.ok(style.includes('\\3C /style>\\3C script>test\\3C /script>'));
226245
});
246+
247+
it('keeps animation values inside their generated CSS declarations', () => {
248+
const result = {
249+
_metadata: { extraHead: [] },
250+
} as unknown as SSRResult;
251+
const animation = {
252+
forwards: {
253+
old: { name: 'fade', duration: '1ms;}custom{value' },
254+
new: { name: 'fade' },
255+
},
256+
backwards: {
257+
old: { name: 'fade' },
258+
new: { name: 'fade' },
259+
},
260+
};
261+
262+
renderTransition(result, 'hash', animation, 'name');
263+
264+
const style = String(result._metadata.extraHead[0]);
265+
assert.ok(style.includes('animation-duration: 1ms\\3B \\7D custom\\7B value;'));
266+
assert.ok(!style.includes('}custom{'));
267+
});
227268
});

0 commit comments

Comments
 (0)