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 <noreply@anthropic.com>
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
pull/18537/merge
Mukund Sarma 5 days ago committed by GitHub
parent ffc0e6e7ef
commit 950e2a837c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: strip comments from inline `style` values in linear time

@ -142,8 +142,9 @@ export function to_style(value, styles) {
}
if (value) {
// strip comments; surrounding whitespace is handled by the trims below (which is much faster than doing it through regex)
value = String(value)
.replaceAll(/\s*\/\*.*?\*\/\s*/g, '')
.replaceAll(/\/\*.*?\*\//g, '')
.trim();
/** @type {boolean | '"' | "'"} */

Loading…
Cancel
Save