Skip to content

fix(compiler-ssr): remove undefined/null class and style attrs #13745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/compiler-ssr/__tests__/ssrElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,45 +131,45 @@ describe('ssr: element', () => {
test('v-bind:class', () => {
expect(getCompiledString(`<div id="foo" :class="bar"></div>`))
.toMatchInlineSnapshot(`
"\`<div id="foo" class="\${
"\`<div id="foo"\${
_ssrRenderClass(_ctx.bar)
}"></div>\`"
}></div>\`"
`)
})

test('static class + v-bind:class', () => {
expect(getCompiledString(`<div class="foo" :class="bar"></div>`))
.toMatchInlineSnapshot(`
"\`<div class="\${
"\`<div\${
_ssrRenderClass([_ctx.bar, "foo"])
}"></div>\`"
}></div>\`"
`)
})

test('v-bind:class + static class', () => {
expect(getCompiledString(`<div :class="bar" class="foo"></div>`))
.toMatchInlineSnapshot(`
"\`<div class="\${
"\`<div\${
_ssrRenderClass([_ctx.bar, "foo"])
}"></div>\`"
}></div>\`"
`)
})

test('v-bind:style', () => {
expect(getCompiledString(`<div id="foo" :style="bar"></div>`))
.toMatchInlineSnapshot(`
"\`<div id="foo" style="\${
"\`<div id="foo"\${
_ssrRenderStyle(_ctx.bar)
}"></div>\`"
}></div>\`"
`)
})

test('static style + v-bind:style', () => {
expect(getCompiledString(`<div style="color:red;" :style="bar"></div>`))
.toMatchInlineSnapshot(`
"\`<div style="\${
"\`<div\${
_ssrRenderStyle([{"color":"red"}, _ctx.bar])
}"></div>\`"
}></div>\`"
`)
})

Expand Down
16 changes: 8 additions & 8 deletions packages/compiler-ssr/__tests__/ssrVShow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ describe('ssr: v-show', () => {
return function ssrRender(_ctx, _push, _parent, _attrs) {
_push(\`<div\${
_ssrRenderAttrs(_attrs)
}><div style="\${
}><div\${
_ssrRenderStyle((_ctx.foo) ? null : { display: "none" })
}"></div></div>\`)
}></div></div>\`)
}"
`)
})
Expand All @@ -41,12 +41,12 @@ describe('ssr: v-show', () => {
return function ssrRender(_ctx, _push, _parent, _attrs) {
_push(\`<div\${
_ssrRenderAttrs(_attrs)
}><div style="\${
}><div\${
_ssrRenderStyle([
{"color":"red"},
(_ctx.foo) ? null : { display: "none" }
])
}"></div></div>\`)
}></div></div>\`)
}"
`)
})
Expand All @@ -60,12 +60,12 @@ describe('ssr: v-show', () => {
return function ssrRender(_ctx, _push, _parent, _attrs) {
_push(\`<div\${
_ssrRenderAttrs(_attrs)
}><div style="\${
}><div\${
_ssrRenderStyle([
{ color: 'red' },
(_ctx.foo) ? null : { display: "none" }
])
}"></div></div>\`)
}></div></div>\`)
}"
`)
})
Expand All @@ -81,13 +81,13 @@ describe('ssr: v-show', () => {
return function ssrRender(_ctx, _push, _parent, _attrs) {
_push(\`<div\${
_ssrRenderAttrs(_attrs)
}><div style="\${
}><div\${
_ssrRenderStyle([
{"color":"red"},
{ fontSize: 14 },
(_ctx.foo) ? null : { display: "none" }
])
}"></div></div>\`)
}></div></div>\`)
}"
`)
})
Expand Down
4 changes: 0 additions & 4 deletions packages/compiler-ssr/src/transforms/ssrTransformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,25 +249,21 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
}
if (attrName === 'class') {
openTag.push(
` class="`,
(dynamicClassBinding = createCallExpression(
context.helper(SSR_RENDER_CLASS),
[value],
)),
`"`,
)
} else if (attrName === 'style') {
if (dynamicStyleBinding) {
// already has style binding, merge into it.
mergeCall(dynamicStyleBinding, value)
} else {
openTag.push(
` style="`,
(dynamicStyleBinding = createCallExpression(
context.helper(SSR_RENDER_STYLE),
[value],
)),
`"`,
)
}
} else {
Expand Down
32 changes: 32 additions & 0 deletions packages/server-renderer/__tests__/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,29 @@ function testRender(type: string, render: typeof renderToString) {
)
})

test('undefined class/style excluded', async () => {
const app = createApp({
template: `<div><div :class="undefined" :style="undefined"></div></div>`,
})
expect(await render(app)).toBe(`<div><div></div></div>`)
})

test('null class/style excluded', async () => {
const app = createApp({
template: `<div><div :class="null" :style="null"></div></div>`,
})
expect(await render(app)).toBe(`<div><div></div></div>`)
})

test('empty string class/style not excluded', async () => {
const app = createApp({
template: `<div><div :class="''" :style="''"></div></div>`,
})
expect(await render(app)).toBe(
`<div><div class="" style=""></div></div>`,
)
})

test('template components with dynamic class attribute before static', async () => {
const app = createApp({
template: `<div><div :class="'dynamic'" class="child"></div></div>`,
Expand All @@ -261,6 +284,15 @@ function testRender(type: string, render: typeof renderToString) {
)
})

test('template components with both static and undefined dynamic class/style attributes', async () => {
const app = createApp({
template: `<div><div :class="undefined" class="child" :style="undefined" style="color: red;"></div></div>`,
})
expect(await render(app)).toBe(
`<div><div class="child" style="color:red;"></div></div>`,
)
})

test('mixing optimized / vnode / template components', async () => {
const OptimizedChild = {
props: ['msg'],
Expand Down
33 changes: 21 additions & 12 deletions packages/server-renderer/__tests__/ssrRenderAttrs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,16 @@ describe('ssr: renderClass', () => {
})

test('standalone', () => {
expect(ssrRenderClass(`foo`)).toBe(`foo`)
expect(ssrRenderClass([`foo`, `bar`])).toBe(`foo bar`)
expect(ssrRenderClass({ foo: true, bar: false })).toBe(`foo`)
expect(ssrRenderClass([{ foo: true, bar: false }, `baz`])).toBe(`foo baz`)
expect(ssrRenderClass(`foo`)).toBe(` class="foo"`)
expect(ssrRenderClass([`foo`, `bar`])).toBe(` class="foo bar"`)
expect(ssrRenderClass({ foo: true, bar: false })).toBe(` class="foo"`)
expect(ssrRenderClass([{ foo: true, bar: false }, `baz`])).toBe(
` class="foo baz"`,
)
})

test('escape class values', () => {
expect(ssrRenderClass(`"><script`)).toBe(`&quot;&gt;&lt;script`)
expect(ssrRenderClass(`"><script`)).toBe(` class="&quot;&gt;&lt;script"`)
})

test('className', () => {
Expand All @@ -155,6 +157,11 @@ describe('ssr: renderClass', () => {
className: ['foo', 'bar'],
}),
).toBe(` class="foo,bar"`)
expect(
ssrRenderAttrs({
className: undefined,
}),
).toBe(``)
})
})

Expand All @@ -172,18 +179,18 @@ describe('ssr: renderStyle', () => {
})

test('standalone', () => {
expect(ssrRenderStyle(`color:red`)).toBe(`color:red`)
expect(ssrRenderStyle(`color:red`)).toBe(` style="color:red"`)
expect(
ssrRenderStyle({
color: `red`,
}),
).toBe(`color:red;`)
).toBe(` style="color:red;"`)
expect(
ssrRenderStyle([
{ color: `red` },
{ fontSize: `15px` }, // case conversion
]),
).toBe(`color:red;font-size:15px;`)
).toBe(` style="color:red;font-size:15px;"`)
})

test('number handling', () => {
Expand All @@ -192,16 +199,16 @@ describe('ssr: renderStyle', () => {
fontSize: null, // invalid value should be ignored
opacity: 0.5,
}),
).toBe(`opacity:0.5;`)
).toBe(` style="opacity:0.5;"`)
})

test('escape inline CSS', () => {
expect(ssrRenderStyle(`"><script`)).toBe(`&quot;&gt;&lt;script`)
expect(ssrRenderStyle(`"><script`)).toBe(` style="&quot;&gt;&lt;script"`)
expect(
ssrRenderStyle({
color: `"><script`,
}),
).toBe(`color:&quot;&gt;&lt;script;`)
).toBe(` style="color:&quot;&gt;&lt;script;"`)
})

test('useCssVars handling', () => {
Expand All @@ -216,6 +223,8 @@ describe('ssr: renderStyle', () => {
':--v6': 0,
'--foo': 1,
}),
).toBe(`--v1:initial;--v2:initial;--v3: ;--v4: ;--v5:foo;--v6:0;--foo:1;`)
).toBe(
` style="--v1:initial;--v2:initial;--v3: ;--v4: ;--v5:foo;--v6:0;--foo:1;"`,
)
})
})
18 changes: 14 additions & 4 deletions packages/server-renderer/src/helpers/ssrRenderAttrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export function ssrRenderAttrs(
}
const value = props[key]
if (key === 'class') {
ret += ` class="${ssrRenderClass(value)}"`
ret += ssrRenderClass(value)
} else if (key === 'style') {
ret += ` style="${ssrRenderStyle(value)}"`
} else if (key === 'className') {
ret += ssrRenderStyle(value)
} else if (key === 'className' && value != null) {
ret += ` class="${String(value)}"`
} else {
ret += ssrRenderDynamicAttr(key, value, tag)
Expand Down Expand Up @@ -86,10 +86,20 @@ export function ssrRenderAttr(key: string, value: unknown): string {
}

export function ssrRenderClass(raw: unknown): string {
return escapeHtml(normalizeClass(raw))
if (raw == null) {
return ''
}
return ` class="${escapeHtml(normalizeClass(raw))}"`
}

export function ssrRenderStyle(raw: unknown): string {
if (raw == null) {
return ''
}
return ` style="${ssrRenderStyleHelper(raw)}"`
}

function ssrRenderStyleHelper(raw: unknown): string {
if (!raw) {
return ''
}
Expand Down
Loading