From eec57b4f9d73cd560692b4caeb27d62dd0b903e5 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Thu, 20 Oct 2016 14:24:07 +0200 Subject: [PATCH] [html] filter empty lines in virtual documents --- extensions/html/server/src/embeddedSupport.ts | 22 ++++++++++++++++--- .../html/server/src/test/embedded.test.ts | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/extensions/html/server/src/embeddedSupport.ts b/extensions/html/server/src/embeddedSupport.ts index 4c97c3a4361..8ef212403ab 100644 --- a/extensions/html/server/src/embeddedSupport.ts +++ b/extensions/html/server/src/embeddedSupport.ts @@ -44,12 +44,28 @@ export function getEmbeddedContent(languageService: LanguageService, document: T } function substituteWithWhitespace(result, start, end, oldContent) { + let accumulatedWS = 0; for (let i = start; i < end; i++) { let ch = oldContent[i]; - if (ch !== '\n' && ch !== '\r') { - ch = ' '; + if (ch === '\n' || ch === '\r') { + // only write new lines, skip the whitespace + accumulatedWS = 0; + result += ch; + } else { + accumulatedWS++; } - result += ch; + } + result = append(result, ' ', accumulatedWS); + return result; +} + +function append(result: string, str: string, n: number): string { + while (n) { + if (n & 1) { + result += str; + } + n >>= 1; + str += str; } return result; } diff --git a/extensions/html/server/src/test/embedded.test.ts b/extensions/html/server/src/test/embedded.test.ts index ec92bcb5431..3bdbec2eb1b 100644 --- a/extensions/html/server/src/test/embedded.test.ts +++ b/extensions/html/server/src/test/embedded.test.ts @@ -52,6 +52,8 @@ suite('HTML Embedded Support', () => { assertEmbeddedLanguageContent('', 'css', ' foo { } '); assertEmbeddedLanguageContent('', 'css', ' '); assertEmbeddedLanguageContent('Hello', 'css', ' foo { } foo { } '); + assertEmbeddedLanguageContent('\n \n\n', 'css', '\n \n foo { } \n \n\n'); + }); test('Scripts', function (): any {