Skip to content

Commit 950e2a8

Browse files
dnukumamrasclaudedummdidumm
authored
fix: strip comments from inline style values in linear time (#18553)
`to_style()` strips CSS comments from an inline `style` value with `/\s*\/\*.*?\*\/\s*/g`. The leading `\s*` makes the match retry from every position, so a long run of whitespace backtracks in O(n^2). When a dynamic `style={value}` sits on an element that also has a `style:` directive, that regex runs on `value`, so a large whitespace string can stall rendering (server) or the main thread (client). ### Fix Drop the surrounding `\s*` and match only the comment: `/\/\*.*?\*\//g` to prevent quadratic regex. The surrounding whitespace was already removed by the `.trim()` --------- Co-authored-by: Claude Opus 4.8 <[email protected]> Co-authored-by: Simon H <[email protected]>
1 parent ffc0e6e commit 950e2a8

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

‎.changeset/strip-style-comments.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: strip comments from inline `style` values in linear time

‎packages/svelte/src/internal/shared/attributes.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ export function to_style(value, styles) {
142142
}
143143

144144
if (value) {
145+
// strip comments; surrounding whitespace is handled by the trims below (which is much faster than doing it through regex)
145146
value = String(value)
146-
.replaceAll(/\s*\/\*.*?\*\/\s*/g, '')
147+
.replaceAll(/\/\*.*?\*\//g, '')
147148
.trim();
148149

149150
/** @type {boolean | '"' | "'"} */

0 commit comments

Comments
 (0)