Browse Source

fix(compiler-dom): stringifyStatic should remove attribute bindings with `null` value (#3477)

fix #3475
pull/3202/head
GU Yiling 4 years ago
committed by GitHub
parent
commit
ca6aa01181
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts
  2. 18
      packages/compiler-dom/src/transforms/stringifyStatic.ts

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

@ -294,4 +294,26 @@ describe('stringify static html', () => {
})
})
})
test('should remove attribute for `null`', () => {
const { ast } = compileWithStringify(
`<div>${repeat(
`<span :title="null"></span>`,
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT
)}</div>`
)
expect(ast.hoists[0]).toMatchObject({
type: NodeTypes.JS_CALL_EXPRESSION,
callee: CREATE_STATIC,
arguments: [
JSON.stringify(
`${repeat(
`<span></span>`,
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT
)}`
),
'5'
]
})
})
})

18
packages/compiler-dom/src/transforms/stringifyStatic.ts

@ -264,15 +264,17 @@ function stringifyElement(
} else if (p.type === NodeTypes.DIRECTIVE && p.name === 'bind') {
// constant v-bind, e.g. :foo="1"
let evaluated = evaluateConstant(p.exp as SimpleExpressionNode)
const arg = p.arg && (p.arg as SimpleExpressionNode).content
if (arg === 'class') {
evaluated = normalizeClass(evaluated)
} else if (arg === 'style') {
evaluated = stringifyStyle(normalizeStyle(evaluated))
if (evaluated != null) {
const arg = p.arg && (p.arg as SimpleExpressionNode).content
if (arg === 'class') {
evaluated = normalizeClass(evaluated)
} else if (arg === 'style') {
evaluated = stringifyStyle(normalizeStyle(evaluated))
}
res += ` ${(p.arg as SimpleExpressionNode).content}="${escapeHtml(
evaluated
)}"`
}
res += ` ${(p.arg as SimpleExpressionNode).content}="${escapeHtml(
evaluated
)}"`
}
}
if (context.scopeId) {

Loading…
Cancel
Save