fix(compiler-sfc): escape non-word characters of css var
This bug will affect dev mode only.
closes #6803
ATM, #6815 hasn't been merged. So reproduction cannot reproduce on SFC playground.
Reproduction:
<script setup>
import { ref } from 'vue'
const θθ² = ref('blue')
const εδ½ = ref(800)
</script>
<template>
<div class="text">
hello
</div>
</template>
<style>
.text {
color: v-bind(θθ²);
font-weight: v-bind(εδ½);
}
</style>
Maybe we could keep replacing symbols to _ but escape CJK chars for better readability.
We should always keep track of existing ones and add number suffixes to avoid conflicts.
Maybe we could keep replacing symbols to
_but escape CJK chars for better readability.
@antfu IMHO, I don't think it's a good solution. Matching CJK is a complicated feature and it's unnecessary to have this in vue core. To avoid adding complexity, I guess just to escape all non-word characters is enough. After all, it only affects dev mode.
I think readability is important for dev otherwise we could use hashes as prod. Filtering is quite simple with /\p{Unified_Ideograph}/u
It can only match Chinese characters, but can't Japanese Kana, Korean Hangeul... There are lots of other language characters around the world, e.g. Russian alphabet, and the Greek alphabet. How can vue handle all of them?

Plus, I think escaping is acceptable, not very bad

canβt we keep the unicode characters (in dev) as we do with variable names? After all, they seem to be valid CSS variable names
@posva I tried Chinese and Japanese characters, and it can work. So maybe can we skip escaping from Unicode characters?
I found an API CSS.escape, it can escape css var name perfectly. But it's browser-only API.
Can I add a polyfill dependency css.escape to compiler-sfc?
I think it's better to avoid adding any polyfill. If Unicode works and is specified in the spec, let's do that. It should also be the lightest solution
@posva Updated. It only escapes ASCII punctuation and symbols: Β !"#$%&'()*+,./:;<=>?@[\]^`{|}~
Deploy Preview for vuejs-coverage failed.
| Name | Link |
|---|---|
| Latest commit | 8123c376389134d79dfcf31f85a5702de980f563 |
| Latest deploy log | https://app.netlify.com/sites/vuejs-coverage/deploys/63614d6b7079f200089a7d4f |