Skip to content

Commit

Permalink
feat(compiler): support deindent: false in vue-template-compiler (#7215)
Browse files Browse the repository at this point in the history
close #7054
  • Loading branch information
imyzf authored and yyx990803 committed Dec 21, 2018
1 parent a981c80 commit bf0efb0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/sfc/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions test/unit/modules/sfc/sfc-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ describe('Single File Component parser', () => {
expect(res.template.content.trim()).toBe('<div><template v-if="ok">hi</template></div>')
})

it('deindent content', () => {
const content = `
<template>
<div></div>
</template>
<script>
export default {}
</script>
<style>
h1 { color: red }
</style>
`
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<div></div>\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<div></div>\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 <div></div>\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 = `
<template>
Expand Down

0 comments on commit bf0efb0

Please sign in to comment.