Jelajahi Sumber

fix(ssr): handle v-text/v-html with non-string value

fix #6572
Evan You 8 tahun lalu
induk
melakukan
09106f066a
2 mengubah file dengan 26 tambahan dan 2 penghapusan
  1. 2 2
      src/server/optimizing-compiler/codegen.js
  2. 24 0
      test/ssr/ssr-string.spec.js

+ 2 - 2
src/server/optimizing-compiler/codegen.js

@@ -200,10 +200,10 @@ function elementToOpenTagSegments (el, state): Array<StringSegment> {
 function childrenToSegments (el, state): Array<StringSegment> {
   let binding
   if ((binding = el.attrsMap['v-html'])) {
-    return [{ type: EXPRESSION, value: binding }]
+    return [{ type: EXPRESSION, value: `_s(${binding})` }]
   }
   if ((binding = el.attrsMap['v-text'])) {
-    return [{ type: INTERPOLATION, value: binding }]
+    return [{ type: INTERPOLATION, value: `_s(${binding})` }]
   }
   return el.children
     ? nodesToSegments(el.children, state)

+ 24 - 0
test/ssr/ssr-string.spec.js

@@ -374,6 +374,18 @@ describe('SSR: renderToString', () => {
     })
   })
 
+  it('v-html with null value', done => {
+    renderVmWithOptions({
+      template: '<div><div v-html="text"></div></div>',
+      data: {
+        text: null
+      }
+    }, result => {
+      expect(result).toContain('<div data-server-rendered="true"><div></div></div>')
+      done()
+    })
+  })
+
   it('v-text', done => {
     renderVmWithOptions({
       template: '<div><div v-text="text"></div></div>',
@@ -386,6 +398,18 @@ describe('SSR: renderToString', () => {
     })
   })
 
+  it('v-text with null value', done => {
+    renderVmWithOptions({
+      template: '<div><div v-text="text"></div></div>',
+      data: {
+        text: null
+      }
+    }, result => {
+      expect(result).toContain('<div data-server-rendered="true"><div></div></div>')
+      done()
+    })
+  })
+
   it('child component (hoc)', done => {
     renderVmWithOptions({
       template: '<child class="foo" :msg="msg"></child>',