Skip to content

Commit 49bfb6b

Browse files
fix: improve animation name transformation (#10432)
* fix: improve animation name transformation * oops * s * minor style tweaks * expand changeset description --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 2d032c7 commit 49bfb6b

File tree

4 files changed

+78
-13
lines changed

4 files changed

+78
-13
lines changed

.changeset/pink-mayflies-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: make CSS animation declaration transformation more robust

packages/svelte/src/compiler/phases/2-analyze/css/Stylesheet.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { push_array } from '../utils/push_array.js';
88
import { create_attribute } from '../../nodes.js';
99

1010
const regex_css_browser_prefix = /^-((webkit)|(moz)|(o)|(ms))-/;
11-
11+
const regex_name_boundary = /^[\s,;}]$/;
1212
/**
1313
* @param {string} name
1414
* @returns {string}
@@ -213,20 +213,29 @@ class Declaration {
213213
transform(code, keyframes) {
214214
const property = this.node.property && remove_css_prefix(this.node.property.toLowerCase());
215215
if (property === 'animation' || property === 'animation-name') {
216-
const name = /** @type {string} */ (this.node.value)
217-
.split(' ')
218-
.find((name) => keyframes.has(name));
219-
220-
if (name) {
221-
const start = code.original.indexOf(
222-
name,
223-
code.original.indexOf(this.node.property, this.node.start)
224-
);
216+
let index = this.node.start + this.node.property.length + 1;
217+
let name = '';
225218

226-
const end = start + name.length;
219+
while (index < code.original.length) {
220+
const character = code.original[index];
227221

228-
const keyframe = /** @type {string} */ (keyframes.get(name));
229-
code.update(start, end, keyframe);
222+
if (regex_name_boundary.test(character)) {
223+
const keyframe = keyframes.get(name);
224+
225+
if (keyframe) {
226+
code.update(index - name.length, index, keyframe);
227+
}
228+
229+
if (character === ';' || character === '}') {
230+
break;
231+
}
232+
233+
name = '';
234+
} else {
235+
name += character;
236+
}
237+
238+
index++;
230239
}
231240
}
232241
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
@keyframes svelte-xyz-a {
3+
0% {
4+
transform: scale(1);
5+
}
6+
100% {
7+
transform: scale(2);
8+
}
9+
}
10+
11+
@keyframes svelte-xyz-animation {
12+
0% {
13+
transform: scale(1);
14+
}
15+
100% {
16+
transform: scale(2);
17+
}
18+
}
19+
20+
h1.svelte-xyz {
21+
animation: 1s linear infinite svelte-xyz-a;
22+
animation: svelte-xyz-a 1s linear infinite;
23+
animation: 1s linear infinite svelte-xyz-a,svelte-xyz-animation 1s linear infinite;
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<h1>test</h1>
2+
3+
<style>
4+
@keyframes a {
5+
0% {
6+
transform: scale(1);
7+
}
8+
100% {
9+
transform: scale(2);
10+
}
11+
}
12+
13+
@keyframes animation {
14+
0% {
15+
transform: scale(1);
16+
}
17+
100% {
18+
transform: scale(2);
19+
}
20+
}
21+
22+
h1 {
23+
animation: 1s linear infinite a;
24+
animation: a 1s linear infinite;
25+
animation: 1s linear infinite a,animation 1s linear infinite;
26+
}
27+
</style>

0 commit comments

Comments
 (0)