wip(ssr): should only render renderable values

This commit is contained in:
Evan You 2020-02-05 15:21:20 -05:00
parent 8f9e85afb1
commit ae92925011
2 changed files with 20 additions and 2 deletions

View File

@ -53,6 +53,16 @@ describe('ssr: renderAttrs', () => {
).toBe(` foo="false"`) // non boolean should render `false` as is
})
test('ingore non-renderable values', () => {
expect(
renderAttrs({
foo: {},
bar: [],
baz: () => {}
})
).toBe(``)
})
test('props to attrs', () => {
expect(
renderAttrs({

View File

@ -45,7 +45,7 @@ export function renderDynamicAttr(
value: unknown,
tag?: string
): string {
if (value == null) {
if (!isRenderableValue(value)) {
return ``
}
const attrKey =
@ -64,12 +64,20 @@ export function renderDynamicAttr(
// Render a v-bind attr with static key. The key is pre-processed at compile
// time and we only need to check and escape value.
export function renderAttr(key: string, value: unknown): string {
if (value == null) {
if (!isRenderableValue(value)) {
return ``
}
return ` ${key}="${escapeHtml(value)}"`
}
function isRenderableValue(value: unknown): boolean {
if (value == null) {
return false
}
const type = typeof value
return type === 'string' || type === 'number' || type === 'boolean'
}
export function renderClass(raw: unknown): string {
return escapeHtml(normalizeClass(raw))
}