fix: improve animation name transformation (#10432)

* fix: improve animation name transformation

* oops

* s

* minor style tweaks

* expand changeset description

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/10441/head
Ahmad 11 months ago committed by GitHub
parent 2d032c733d
commit 49bfb6b1d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: make CSS animation declaration transformation more robust

@ -8,7 +8,7 @@ import { push_array } from '../utils/push_array.js';
import { create_attribute } from '../../nodes.js'; import { create_attribute } from '../../nodes.js';
const regex_css_browser_prefix = /^-((webkit)|(moz)|(o)|(ms))-/; const regex_css_browser_prefix = /^-((webkit)|(moz)|(o)|(ms))-/;
const regex_name_boundary = /^[\s,;}]$/;
/** /**
* @param {string} name * @param {string} name
* @returns {string} * @returns {string}
@ -213,20 +213,29 @@ class Declaration {
transform(code, keyframes) { transform(code, keyframes) {
const property = this.node.property && remove_css_prefix(this.node.property.toLowerCase()); const property = this.node.property && remove_css_prefix(this.node.property.toLowerCase());
if (property === 'animation' || property === 'animation-name') { if (property === 'animation' || property === 'animation-name') {
const name = /** @type {string} */ (this.node.value) let index = this.node.start + this.node.property.length + 1;
.split(' ') let name = '';
.find((name) => keyframes.has(name));
if (name) {
const start = code.original.indexOf(
name,
code.original.indexOf(this.node.property, this.node.start)
);
const end = start + name.length; while (index < code.original.length) {
const character = code.original[index];
const keyframe = /** @type {string} */ (keyframes.get(name)); if (regex_name_boundary.test(character)) {
code.update(start, end, keyframe); const keyframe = keyframes.get(name);
if (keyframe) {
code.update(index - name.length, index, keyframe);
}
if (character === ';' || character === '}') {
break;
}
name = '';
} else {
name += character;
}
index++;
} }
} }
} }

@ -0,0 +1,24 @@
@keyframes svelte-xyz-a {
0% {
transform: scale(1);
}
100% {
transform: scale(2);
}
}
@keyframes svelte-xyz-animation {
0% {
transform: scale(1);
}
100% {
transform: scale(2);
}
}
h1.svelte-xyz {
animation: 1s linear infinite svelte-xyz-a;
animation: svelte-xyz-a 1s linear infinite;
animation: 1s linear infinite svelte-xyz-a,svelte-xyz-animation 1s linear infinite;
}

@ -0,0 +1,27 @@
<h1>test</h1>
<style>
@keyframes a {
0% {
transform: scale(1);
}
100% {
transform: scale(2);
}
}
@keyframes animation {
0% {
transform: scale(1);
}
100% {
transform: scale(2);
}
}
h1 {
animation: 1s linear infinite a;
animation: a 1s linear infinite;
animation: 1s linear infinite a,animation 1s linear infinite;
}
</style>
Loading…
Cancel
Save