From 4152ba4193b6c7d682a66aa3197d478a02789830 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 1 Mar 2017 16:22:05 -0800 Subject: [PATCH 1/2] SFC parseComponent pads content with spaces when `{ pad: true }` is provided. That is, all content is converted to spaces. Previously, each line was truncated to "//". The new padding method works better with character-oriented tools that calculate positions by distance from the beginning of the file instead of by line number. --- src/sfc/parser.js | 7 +------ test/unit/modules/sfc/sfc-parser.spec.js | 13 +++++++++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/sfc/parser.js b/src/sfc/parser.js index 4ac29d2d1df..fb1cdb06785 100644 --- a/src/sfc/parser.js +++ b/src/sfc/parser.js @@ -4,7 +4,6 @@ import deindent from 'de-indent' import { parseHTML } from 'compiler/parser/html-parser' import { makeMap } from 'shared/util' -const splitRE = /\r?\n/g const isSpecialTag = makeMap('script,style,template', true) type Attribute = { @@ -95,11 +94,7 @@ export function parseComponent ( } function padContent (block: SFCBlock | SFCCustomBlock) { - const offset = content.slice(0, block.start).split(splitRE).length - const padChar = block.type === 'script' && !block.lang - ? '//\n' - : '\n' - return Array(offset).join(padChar) + return content.slice(0, block.start).replace(/./g, ' ') } parseHTML(content, { diff --git a/test/unit/modules/sfc/sfc-parser.spec.js b/test/unit/modules/sfc/sfc-parser.spec.js index be3f518a584..2996f809d7a 100644 --- a/test/unit/modules/sfc/sfc-parser.spec.js +++ b/test/unit/modules/sfc/sfc-parser.spec.js @@ -67,8 +67,17 @@ describe('Single File Component parser', () => { h1 { color: red } `.trim(), { pad: true }) - expect(res.script.content).toBe(Array(3 + 1).join('//\n') + '\nexport default {}\n') - expect(res.styles[0].content).toBe(Array(6 + 1).join('\n') + '\nh1 { color: red }\n') + expect(res.script.content).toBe(` + + - `.trim(), { pad: true }) - expect(res.script.content).toBe(`