Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#7054): vue-template-compiler support switch deindent #7215

Merged
merged 3 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, 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) {
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