Commit f82aba8
authored
feat(css): add classNameSlug option to createCssContext (#4834)
* feat(css): add classNameSlug option to createCssContext
Add an optional classNameSlug function to createCssContext that lets
users customize generated CSS class names instead of the default
hash-based 'css-1234567890' format.
The function receives (hash, label, styleString) and returns the desired
class name string. This enables readable, debug-friendly class names
like 'h-hero-section' while preserving hash-based uniqueness.
Addresses review feedback:
- Move ClassNameSlug type near CssVariableType exports
- Rename third parameter from css to styleString
- Propagate classNameSlug to keyframesCommon and viewTransitionCommon
- Add classNameSlug support to DOM-side createCssContext
- Add TSDoc documentation
Closes #4577
* fix(css): sanitize and normalize classNameSlug output
Normalize and sanitize the return value of classNameSlug to prevent
CSS injection and improve readability:
- Trim leading/trailing whitespace
- Collapse whitespace sequences to hyphens (e.g. 'ultra fast' -> 'ultra-fast')
- Strip characters outside [a-zA-Z0-9_-]
- Fall back to hash if result is empty
Addresses review feedback from yusukebe and usualoma regarding
potential CSS injection through malicious classNameSlug functions.
* feat(css): add onInvalidSlug and unify server/DOM contexts
Add strict CSS identifier validation for classNameSlug output and
onInvalidSlug callback for invalid slugs, unified across server
and DOM-side createCssContext.
- Replace sanitizeClassName with validateClassName/validateKeyframeName
(rejects digit-leading names and reserved @Keyframes keywords)
- Add onInvalidSlug option (defaults to console.warn)
- Normalize labels (trim + hyphenate) before passing to classNameSlug
* feat(css): refine classNameSlug with strict validation and onInvalidSlug
Refine classNameSlug implementation by replacing sanitization with
strict validation and adding an onInvalidSlug callback.
- Add isValidClassName and isValidKeyframeName for strict identifier
checks (^?-?[_a-zA-Z][_a-zA-Z0-9-]*$).
- Reject CSS-wide reserved keywords for @Keyframes animation names.
- Pre-normalize labels (trim + hyphenate) before passing to classNameSlug.
- Add optional onInvalidSlug callback to createCssContext for custom
error handling (defaults to console.warn).
- Ensure parity between SSR and DOM-side createCssContext.
- Ensure the default path remains zero-overhead when classNameSlug is unused.
* test: remove .trim() usage in classNameSlug test1 parent 9f374a5 commit f82aba8
5 files changed
Lines changed: 435 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
52 | 75 | | |
53 | 76 | | |
54 | 77 | | |
| |||
90 | 113 | | |
91 | 114 | | |
92 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
93 | 135 | | |
94 | 136 | | |
95 | 137 | | |
| |||
154 | 196 | | |
155 | 197 | | |
156 | 198 | | |
157 | | - | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
158 | 202 | | |
159 | 203 | | |
160 | 204 | | |
161 | 205 | | |
162 | 206 | | |
163 | 207 | | |
164 | | - | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
165 | 223 | | |
166 | 224 | | |
167 | 225 | | |
| |||
196 | 254 | | |
197 | 255 | | |
198 | 256 | | |
199 | | - | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
200 | 260 | | |
201 | 261 | | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
202 | 276 | | |
203 | 277 | | |
204 | | - | |
| 278 | + | |
205 | 279 | | |
206 | 280 | | |
207 | 281 | | |
208 | 282 | | |
209 | 283 | | |
210 | 284 | | |
211 | 285 | | |
212 | | - | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
213 | 292 | | |
214 | 293 | | |
215 | 294 | | |
216 | 295 | | |
217 | 296 | | |
218 | 297 | | |
219 | 298 | | |
220 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
221 | 302 | | |
222 | 303 | | |
223 | 304 | | |
224 | 305 | | |
225 | 306 | | |
226 | 307 | | |
227 | | - | |
| 308 | + | |
228 | 309 | | |
229 | 310 | | |
230 | 311 | | |
231 | | - | |
232 | | - | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
233 | 318 | | |
234 | 319 | | |
235 | 320 | | |
| |||
0 commit comments