From 82436614de0026dc2489cdb72d7fbe112240b494 Mon Sep 17 00:00:00 2001 From: Zhenfei You Date: Mon, 11 Dec 2017 13:20:08 +0800 Subject: [PATCH 1/2] feat: vue-template-compiler support switch deindent --- src/sfc/parser.js | 10 +++++++-- test/unit/modules/sfc/sfc-parser.spec.js | 27 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/sfc/parser.js b/src/sfc/parser.js index 643e55ca2ff..1850930312d 100644 --- a/src/sfc/parser.js +++ b/src/sfc/parser.js @@ -2,7 +2,7 @@ import deindent from 'de-indent' import { parseHTML } from 'compiler/parser/html-parser' -import { makeMap } from 'shared/util' +import { makeMap, extend } from 'shared/util' const splitRE = /\r?\n/g const replaceRE = /./g @@ -28,6 +28,9 @@ export function parseComponent ( } let depth = 0 let currentBlock: ?(SFCBlock | SFCCustomBlock) = null + options = extend({ + deindent: true + }, options) function start ( tag: string, @@ -83,7 +86,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) { + 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 = `