Răsfoiți Sursa

fix(compile): properly generate comments with special character (#6156)

close #6150
laoxiong 8 ani în urmă
părinte
comite
d03fa26687

+ 1 - 1
src/compiler/codegen/index.js

@@ -442,7 +442,7 @@ export function genText (text: ASTText | ASTExpression): string {
 }
 
 export function genComment (comment: ASTText): string {
-  return `_e('${comment.text}')`
+  return `_e(${JSON.stringify(comment.text)})`
 }
 
 function genSlot (el: ASTElement, state: CodegenState): string {

+ 15 - 1
test/unit/modules/compiler/codegen.spec.js

@@ -479,7 +479,21 @@ describe('codegen', () => {
       comments: true
     }, baseOptions)
     const template = '<div><!--comment--></div>'
-    const generatedCode = `with(this){return _c('div',[_e('comment')])}`
+    const generatedCode = `with(this){return _c('div',[_e("comment")])}`
+
+    const ast = parse(template, options)
+    optimize(ast, options)
+    const res = generate(ast, options)
+    expect(res.render).toBe(generatedCode)
+  })
+
+  // #6150
+  it('generate comments with special characters', () => {
+    const options = extend({
+      comments: true
+    }, baseOptions)
+    const template = '<div><!--\n\'comment\'\n--></div>'
+    const generatedCode = `with(this){return _c('div',[_e("\\n'comment'\\n")])}`
 
     const ast = parse(template, options)
     optimize(ast, options)