diff --git a/packages/compiler-sfc/src/script/utils.ts b/packages/compiler-sfc/src/script/utils.ts index 3cbc270269a..bc621d25840 100644 --- a/packages/compiler-sfc/src/script/utils.ts +++ b/packages/compiler-sfc/src/script/utils.ts @@ -121,6 +121,8 @@ export function getEscapedPropName(key: string) { export const cssVarNameEscapeSymbolsRE = /[ !"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g -export function getEscapedCssVarName(key: string) { - return key.replace(cssVarNameEscapeSymbolsRE, s => `\\${s}`) +export function getEscapedCssVarName(key: string, doubleEscape: boolean) { + return key.replace(cssVarNameEscapeSymbolsRE, s => + doubleEscape ? `\\\\${s}` : `\\${s}` + ) } diff --git a/packages/compiler-sfc/src/style/cssVars.ts b/packages/compiler-sfc/src/style/cssVars.ts index cbb7648dce9..cb5274bc287 100644 --- a/packages/compiler-sfc/src/style/cssVars.ts +++ b/packages/compiler-sfc/src/style/cssVars.ts @@ -21,21 +21,26 @@ export function genCssVarsFromList( isSSR = false ): string { return `{\n ${vars - .map(key => { - const varName = genVarName(id, key, isProd) - return `"${isSSR ? `--` : ``}${ - isSSR && !isProd ? varName.replace(/\\/g, '\\\\') : varName - }": (${key})` - }) + .map( + key => + `"${isSSR ? `--` : ``}${genVarName(id, key, isProd, isSSR)}": (${key})` + ) .join(',\n ')}\n}` } -function genVarName(id: string, raw: string, isProd: boolean): string { +function genVarName( + id: string, + raw: string, + isProd: boolean, + isSSR = false +): string { if (isProd) { return hash(id + raw) } else { // escape ASCII Punctuation & Symbols - return `${id}-${getEscapedCssVarName(raw)}` + // #7823 need to double-escape in SSR because the attributes are rendered + // into an HTML string + return `${id}-${getEscapedCssVarName(raw, isSSR)}` } }