Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix transition:name can be unicode #9822

Merged
merged 28 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-bobcats-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Applies the correct escaping to identifiers used with `transition:name`.
25 changes: 24 additions & 1 deletion packages/astro/src/runtime/server/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import type {
} from '../../@types/astro.js';
import { fade, slide } from '../../transitions/index.js';
import { markHTMLString } from './escape.js';
import { Logger } from '../../core/logger/core.js';
import { nodeLogDestination } from '../../core/logger/node.js';
import { bold } from 'kleur/colors';

const transitionNameMap = new WeakMap<SSRResult, number>();
function incrementTransitionNumber(result: SSRResult) {
Expand All @@ -25,7 +28,27 @@ export function createTransitionScope(result: SSRResult, hash: string) {

// Ensure animationName is a valid CSS identifier
function toValidIdent(name: string): string {
return name.replace(/[^a-zA-Z0-9\-\_]/g, '_').replace(/^\_+|\_+$/g, '');
// can't start with a number, - + number, or --
liruifengv marked this conversation as resolved.
Show resolved Hide resolved
if (/^[0-9]|^-[0-9]|^--/.test(name)) {
throw new Error("Your transition:name is not a valid CSS identifier.");
}
// can't be a CSS keyword
if (['unset', 'initial', 'inherit', 'none' ].includes(name)) {
liruifengv marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Your transition:name cannot be ${name}.`);
}
if (/^[a-zA-Z0-9_\\-]*$/.test(name)) {
return name;
liruifengv marked this conversation as resolved.
Show resolved Hide resolved
}
let result = "";
liruifengv marked this conversation as resolved.
Show resolved Hide resolved
for (const char of name) {
if (/[a-zA-Z0-9_-]/.test(char)) {
result += char;
liruifengv marked this conversation as resolved.
Show resolved Hide resolved
} else {
liruifengv marked this conversation as resolved.
Show resolved Hide resolved
const suffix = char === name[name.length - 1] ? "" : "\\ ";
result += `\\${char.codePointAt(0)!.toString(16)}${suffix}`;
}
liruifengv marked this conversation as resolved.
Show resolved Hide resolved
}
return result;
}

type Entries<T extends Record<string, any>> = Iterable<[keyof T, T[keyof T]]>;
Expand Down