Skip to content

Commit 2d78539

Browse files
authoredNov 15, 2024··
fix(compiler-dom): properly stringify template string style (#12392)
close #12391
1 parent 54812ea commit 2d78539

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed
 

‎packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ return function render(_ctx, _cache) {
3232
}"
3333
`;
3434

35+
exports[`stringify static html > serializing template string style 1`] = `
36+
"const { toDisplayString: _toDisplayString, normalizeClass: _normalizeClass, createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
37+
38+
return function render(_ctx, _cache) {
39+
return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [
40+
_createStaticVNode("<div style=\\"color:red;\\"><span class=\\"foo bar\\">1 + false</span><span class=\\"foo bar\\">1 + false</span><span class=\\"foo bar\\">1 + false</span><span class=\\"foo bar\\">1 + false</span><span class=\\"foo bar\\">1 + false</span></div>", 1)
41+
])))
42+
}"
43+
`;
44+
3545
exports[`stringify static html > should bail for <option> elements with null values 1`] = `
3646
"const { createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
3747

‎packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,27 @@ describe('stringify static html', () => {
162162
expect(code).toMatchSnapshot()
163163
})
164164

165+
// #12391
166+
test('serializing template string style', () => {
167+
const { ast, code } = compileWithStringify(
168+
`<div><div :style="\`color:red;\`">${repeat(
169+
`<span :class="[{ foo: true }, { bar: true }]">{{ 1 }} + {{ false }}</span>`,
170+
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT,
171+
)}</div></div>`,
172+
)
173+
// should be optimized now
174+
expect(ast.cached).toMatchObject([
175+
cachedArrayStaticNodeMatcher(
176+
`<div style="color:red;">${repeat(
177+
`<span class="foo bar">1 + false</span>`,
178+
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT,
179+
)}</div>`,
180+
1,
181+
),
182+
])
183+
expect(code).toMatchSnapshot()
184+
})
185+
165186
test('escape', () => {
166187
const { ast, code } = compileWithStringify(
167188
`<div><div>${repeat(

‎packages/shared/__tests__/normalizeProp.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ describe('normalizeStyle', () => {
153153
})
154154

155155
describe('stringifyStyle', () => {
156-
test('should return empty string for undefined or string styles', () => {
156+
test('should return empty string for undefined', () => {
157157
expect(stringifyStyle(undefined)).toBe('')
158158
expect(stringifyStyle('')).toBe('')
159-
expect(stringifyStyle('color: blue;')).toBe('')
159+
expect(stringifyStyle('color: blue;')).toBe('color: blue;')
160160
})
161161

162162
test('should return valid CSS string for normalized style object', () => {

‎packages/shared/src/normalizeProp.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ export function parseStringStyle(cssText: string): NormalizedStyle {
4545
export function stringifyStyle(
4646
styles: NormalizedStyle | string | undefined,
4747
): string {
48+
if (!styles) return ''
49+
if (isString(styles)) return styles
50+
4851
let ret = ''
49-
if (!styles || isString(styles)) {
50-
return ret
51-
}
5252
for (const key in styles) {
5353
const value = styles[key]
5454
if (isString(value) || typeof value === 'number') {

0 commit comments

Comments
 (0)
Please sign in to comment.