Skip to content

Commit ca6aa01

Browse files
authored
fix(compiler-dom): stringifyStatic should remove attribute bindings with null value (#3477)
fix #3475
1 parent 7cf143d commit ca6aa01

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,26 @@ describe('stringify static html', () => {
294294
})
295295
})
296296
})
297+
298+
test('should remove attribute for `null`', () => {
299+
const { ast } = compileWithStringify(
300+
`<div>${repeat(
301+
`<span :title="null"></span>`,
302+
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT
303+
)}</div>`
304+
)
305+
expect(ast.hoists[0]).toMatchObject({
306+
type: NodeTypes.JS_CALL_EXPRESSION,
307+
callee: CREATE_STATIC,
308+
arguments: [
309+
JSON.stringify(
310+
`${repeat(
311+
`<span></span>`,
312+
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT
313+
)}`
314+
),
315+
'5'
316+
]
317+
})
318+
})
297319
})

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,17 @@ function stringifyElement(
264264
} else if (p.type === NodeTypes.DIRECTIVE && p.name === 'bind') {
265265
// constant v-bind, e.g. :foo="1"
266266
let evaluated = evaluateConstant(p.exp as SimpleExpressionNode)
267-
const arg = p.arg && (p.arg as SimpleExpressionNode).content
268-
if (arg === 'class') {
269-
evaluated = normalizeClass(evaluated)
270-
} else if (arg === 'style') {
271-
evaluated = stringifyStyle(normalizeStyle(evaluated))
267+
if (evaluated != null) {
268+
const arg = p.arg && (p.arg as SimpleExpressionNode).content
269+
if (arg === 'class') {
270+
evaluated = normalizeClass(evaluated)
271+
} else if (arg === 'style') {
272+
evaluated = stringifyStyle(normalizeStyle(evaluated))
273+
}
274+
res += ` ${(p.arg as SimpleExpressionNode).content}="${escapeHtml(
275+
evaluated
276+
)}"`
272277
}
273-
res += ` ${(p.arg as SimpleExpressionNode).content}="${escapeHtml(
274-
evaluated
275-
)}"`
276278
}
277279
}
278280
if (context.scopeId) {

0 commit comments

Comments
 (0)