core icon indicating copy to clipboard operation
core copied to clipboard

fix(compiler-sfc): escape non-word characters of css var

Open sxzz opened this issue 3 years ago β€’ 11 comments

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>

sxzz avatar Oct 03 '22 18:10 sxzz

Maybe we could keep replacing symbols to _ but escape CJK chars for better readability.

antfu avatar Oct 04 '22 06:10 antfu

but it will cause conflict if there is the same length of symbols.

Example

sxzz avatar Oct 04 '22 06:10 sxzz

We should always keep track of existing ones and add number suffixes to avoid conflicts.

antfu avatar Oct 04 '22 07:10 antfu

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.

sxzz avatar Oct 04 '22 10:10 sxzz

I think readability is important for dev otherwise we could use hashes as prod. Filtering is quite simple with /\p{Unified_Ideograph}/u

antfu avatar Oct 04 '22 12:10 antfu

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? image


Plus, I think escaping is acceptable, not very bad image

sxzz avatar Oct 04 '22 14:10 sxzz

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 avatar Oct 06 '22 13:10 posva

@posva I tried Chinese and Japanese characters, and it can work. So maybe can we skip escaping from Unicode characters?

sxzz avatar Oct 06 '22 13:10 sxzz

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?

sxzz avatar Oct 06 '22 13:10 sxzz

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 avatar Oct 06 '22 14:10 posva

@posva Updated. It only escapes ASCII punctuation and symbols: Β !"#$%&'()*+,./:;<=>?@[\]^`{|}~

sxzz avatar Oct 06 '22 16:10 sxzz

Deploy Preview for vuejs-coverage failed.

Name Link
Latest commit 8123c376389134d79dfcf31f85a5702de980f563
Latest deploy log https://app.netlify.com/sites/vuejs-coverage/deploys/63614d6b7079f200089a7d4f

netlify[bot] avatar Nov 01 '22 16:11 netlify[bot]