From 1aada0ecf3ee404c2de41c9e92c629ad8539bec8 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 29 Sep 2026 11:01:39 -0400 Subject: [PATCH] Fix custom View Transition direction names that are not valid CSS identifiers --- .changeset/quiet-foxes-point.md | 5 ++++ .../astro/src/runtime/server/transition.ts | 4 ++- .../test/units/render/transitions.test.ts | 25 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 .changeset/quiet-foxes-point.md diff --git a/.changeset/quiet-foxes-point.md b/.changeset/quiet-foxes-point.md new file mode 100644 index 000000000000..90d6538ceb02 --- /dev/null +++ b/.changeset/quiet-foxes-point.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes custom View Transition direction names that are not valid CSS identifiers, such as names starting with a digit or containing spaces. These directions now match their animations correctly. diff --git a/packages/astro/src/runtime/server/transition.ts b/packages/astro/src/runtime/server/transition.ts index 623583ddafa4..1c6d1f8300ae 100644 --- a/packages/astro/src/runtime/server/transition.ts +++ b/packages/astro/src/runtime/server/transition.ts @@ -176,7 +176,9 @@ export class ViewTransitionStyleSheet { ? `[data-astro-transition=back]` : direction === 'forwards' ? '' - : `[data-astro-transition=${direction}]`; + : // Quote custom direction names so values that are not valid CSS identifiers + // (e.g. names starting with a digit or containing spaces) still match. + `[data-astro-transition=${cssesc(direction, { quotes: 'double', wrap: true })}]`; this.addRule('modern', `${prefix}::view-transition-${image}(${name}) { ${animation} }`); this.addRule( 'fallback', diff --git a/packages/astro/test/units/render/transitions.test.ts b/packages/astro/test/units/render/transitions.test.ts index d7553e9ab475..466574a6e379 100644 --- a/packages/astro/test/units/render/transitions.test.ts +++ b/packages/astro/test/units/render/transitions.test.ts @@ -203,6 +203,31 @@ describe('ViewTransitionStyleSheet', () => { ); }); + it('addAnimationPair() quotes custom direction names in the selector', () => { + const sheet = new ViewTransitionStyleSheet('astro-xyz-1', 'banner'); + sheet.addAnimationPair('downward', 'old', { name: 'slide-out' }); + const css = sheet.toString(); + assert.ok( + css.includes('[data-astro-transition="downward"]::view-transition-old(banner)'), + `expected quoted custom direction prefix: ${css}`, + ); + }); + + it('addAnimationPair() supports custom direction names that are not CSS identifiers', () => { + const sheet = new ViewTransitionStyleSheet('astro-xyz-1', 'banner'); + sheet.addAnimationPair('2nd step', 'new', { name: 'slide-in' }); + sheet.addAnimationPair('a"b]c{d}', 'old', { name: 'slide-out' }); + const css = sheet.toString(); + assert.ok( + css.includes('[data-astro-transition="2nd step"]::view-transition-new(banner)'), + `expected quoted custom direction prefix: ${css}`, + ); + assert.ok( + css.includes('[data-astro-transition="a\\"b]c{d}"]::view-transition-old(banner)'), + `expected punctuation in custom direction to be escaped: ${css}`, + ); + }); + it('addAnimationRaw() adds same rule to both modern and fallback', () => { const sheet = new ViewTransitionStyleSheet('astro-abc-1', 'hero'); sheet.addAnimationRaw('new', 'animation: none; mix-blend-mode: normal;');