From bf0efb02b1f52cceb0bad8588cf6c90e22349049 Mon Sep 17 00:00:00 2001 From: Zhenfei You Date: Sat, 22 Dec 2018 00:59:34 +0800 Subject: [PATCH] feat(compiler): support deindent: false in vue-template-compiler (#7215) close #7054 --- src/sfc/parser.js | 5 ++++- test/unit/modules/sfc/sfc-parser.spec.js | 27 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/sfc/parser.js b/src/sfc/parser.js index d7868a40ab..1e6f32acf2 100644 --- a/src/sfc/parser.js +++ b/src/sfc/parser.js @@ -83,7 +83,10 @@ export function parseComponent ( function end (tag: string, start: number) { if (depth === 1 && currentBlock) { currentBlock.end = start - let text = deindent(content.slice(currentBlock.start, currentBlock.end)) + let text = content.slice(currentBlock.start, currentBlock.end) + if (options.deindent !== false) { + text = deindent(text) + } // pad content so that linters and pre-processors can output correct // line numbers in errors and warnings if (currentBlock.type !== 'template' && options.pad) { diff --git a/test/unit/modules/sfc/sfc-parser.spec.js b/test/unit/modules/sfc/sfc-parser.spec.js index 4cf6bad574..ecc97fc5c3 100644 --- a/test/unit/modules/sfc/sfc-parser.spec.js +++ b/test/unit/modules/sfc/sfc-parser.spec.js @@ -55,6 +55,33 @@ describe('Single File Component parser', () => { expect(res.template.content.trim()).toBe('
') }) + it('deindent content', () => { + const content = ` + + + + ` + const deindentDefault = parseComponent(content.trim(), { pad: false }) + const deindentEnabled = parseComponent(content.trim(), { pad: false, deindent: true }) + const deindentDisabled = parseComponent(content.trim(), { pad: false, deindent: false }) + + expect(deindentDefault.template.content).toBe('\n
\n') + expect(deindentDefault.script.content).toBe('\nexport default {}\n') + expect(deindentDefault.styles[0].content).toBe('\nh1 { color: red }\n') + expect(deindentEnabled.template.content).toBe('\n
\n') + expect(deindentEnabled.script.content).toBe('\nexport default {}\n') + expect(deindentEnabled.styles[0].content).toBe('\nh1 { color: red }\n') + expect(deindentDisabled.template.content).toBe('\n
\n ') + expect(deindentDisabled.script.content).toBe('\n export default {}\n ') + expect(deindentDisabled.styles[0].content).toBe('\n h1 { color: red }\n ') + }) + it('pad content', () => { const content = `