]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-ssr): fix v-html SSR for nullish values
authorEvan You <yyx990803@gmail.com>
Thu, 18 Apr 2024 04:49:56 +0000 (12:49 +0800)
committerEvan You <yyx990803@gmail.com>
Thu, 18 Apr 2024 04:49:56 +0000 (12:49 +0800)
close #10725

packages/compiler-ssr/__tests__/ssrElement.spec.ts
packages/compiler-ssr/src/transforms/ssrTransformElement.ts

index 8f0e28f4e146b19f0381973f7cd1bc3a334fcb99..4056b4c3c71a2e811a9752250ced422a849d9b49 100644 (file)
@@ -25,7 +25,7 @@ describe('ssr: element', () => {
     test('v-html', () => {
       expect(getCompiledString(`<div v-html="foo"/>`)).toMatchInlineSnapshot(`
         "\`<div>\${
-            _ctx.foo
+            (_ctx.foo) ?? ''
           }</div>\`"
       `)
     })
index 050528f5870b604b810991bd47d68662b11e97a5..7175b797d4817e694ec75d405bf6c197505b2813 100644 (file)
@@ -22,6 +22,7 @@ import {
   createAssignmentExpression,
   createCallExpression,
   createCompilerError,
+  createCompoundExpression,
   createConditionalExpression,
   createInterpolation,
   createSequenceExpression,
@@ -188,7 +189,10 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
       // special cases with children override
       if (prop.type === NodeTypes.DIRECTIVE) {
         if (prop.name === 'html' && prop.exp) {
-          rawChildrenMap.set(node, prop.exp)
+          rawChildrenMap.set(
+            node,
+            createCompoundExpression([`(`, prop.exp, `) ?? ''`]),
+          )
         } else if (prop.name === 'text' && prop.exp) {
           node.children = [createInterpolation(prop.exp, prop.loc)]
         } else if (prop.name === 'slot') {