Skip to content

Commit ae92925

Browse files
committed
wip(ssr): should only render renderable values
1 parent 8f9e85a commit ae92925

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/server-renderer/__tests__/renderAttrs.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ describe('ssr: renderAttrs', () => {
5353
).toBe(` foo="false"`) // non boolean should render `false` as is
5454
})
5555

56+
test('ingore non-renderable values', () => {
57+
expect(
58+
renderAttrs({
59+
foo: {},
60+
bar: [],
61+
baz: () => {}
62+
})
63+
).toBe(``)
64+
})
65+
5666
test('props to attrs', () => {
5767
expect(
5868
renderAttrs({

packages/server-renderer/src/helpers/renderAttrs.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function renderDynamicAttr(
4545
value: unknown,
4646
tag?: string
4747
): string {
48-
if (value == null) {
48+
if (!isRenderableValue(value)) {
4949
return ``
5050
}
5151
const attrKey =
@@ -64,12 +64,20 @@ export function renderDynamicAttr(
6464
// Render a v-bind attr with static key. The key is pre-processed at compile
6565
// time and we only need to check and escape value.
6666
export function renderAttr(key: string, value: unknown): string {
67-
if (value == null) {
67+
if (!isRenderableValue(value)) {
6868
return ``
6969
}
7070
return ` ${key}="${escapeHtml(value)}"`
7171
}
7272

73+
function isRenderableValue(value: unknown): boolean {
74+
if (value == null) {
75+
return false
76+
}
77+
const type = typeof value
78+
return type === 'string' || type === 'number' || type === 'boolean'
79+
}
80+
7381
export function renderClass(raw: unknown): string {
7482
return escapeHtml(normalizeClass(raw))
7583
}

0 commit comments

Comments
 (0)