diff --git a/src/sfc/parser.js b/src/sfc/parser.js index 868a43ca082..957e5d97085 100644 --- a/src/sfc/parser.js +++ b/src/sfc/parser.js @@ -83,7 +83,10 @@ export function parseComponent ( function end (tag: string, start: number, end: 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 4cf6bad574a..ecc97fc5c33 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 = `