Skip to content

Commit 2dd9b45

Browse files
committed
refactor: double escape directly
1 parent b298b3c commit 2dd9b45

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

packages/compiler-sfc/src/script/utils.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ export function getEscapedPropName(key: string) {
121121

122122
export const cssVarNameEscapeSymbolsRE = /[ !"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g
123123

124-
export function getEscapedCssVarName(key: string) {
125-
return key.replace(cssVarNameEscapeSymbolsRE, s => `\\${s}`)
124+
export function getEscapedCssVarName(key: string, doubleEscape: boolean) {
125+
return key.replace(cssVarNameEscapeSymbolsRE, s =>
126+
doubleEscape ? `\\\\${s}` : `\\${s}`
127+
)
126128
}

packages/compiler-sfc/src/style/cssVars.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,26 @@ export function genCssVarsFromList(
2121
isSSR = false
2222
): string {
2323
return `{\n ${vars
24-
.map(key => {
25-
const varName = genVarName(id, key, isProd)
26-
return `"${isSSR ? `--` : ``}${
27-
isSSR && !isProd ? varName.replace(/\\/g, '\\\\') : varName
28-
}": (${key})`
29-
})
24+
.map(
25+
key =>
26+
`"${isSSR ? `--` : ``}${genVarName(id, key, isProd, isSSR)}": (${key})`
27+
)
3028
.join(',\n ')}\n}`
3129
}
3230

33-
function genVarName(id: string, raw: string, isProd: boolean): string {
31+
function genVarName(
32+
id: string,
33+
raw: string,
34+
isProd: boolean,
35+
isSSR = false
36+
): string {
3437
if (isProd) {
3538
return hash(id + raw)
3639
} else {
3740
// escape ASCII Punctuation & Symbols
38-
return `${id}-${getEscapedCssVarName(raw)}`
41+
// #7823 need to double-escape in SSR because the attributes are rendered
42+
// into an HTML string
43+
return `${id}-${getEscapedCssVarName(raw, isSSR)}`
3944
}
4045
}
4146

0 commit comments

Comments
 (0)