wip(ssr): should only render renderable values
This commit is contained in:
parent
8f9e85afb1
commit
ae92925011
@ -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({
|
||||
|
@ -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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user