/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import 'mocha'; import * as path from 'path'; import * as fs from 'fs'; import * as assert from 'assert'; import { getLanguageModes, TextDocument, Range, FormattingOptions, ClientCapabilities } from '../modes/languageModes'; import { format } from '../modes/formatting'; suite('HTML Embedded Formatting', () => { function assertFormat(value: string, expected: string, options?: any, formatOptions?: FormattingOptions, message?: string): void { let workspace = { settings: options, folders: [{ name: 'foo', uri: 'test://foo' }] }; let languageModes = getLanguageModes({ css: true, javascript: true }, workspace, ClientCapabilities.LATEST); let rangeStartOffset = value.indexOf('|'); let rangeEndOffset; if (rangeStartOffset !== -1) { value = value.substr(0, rangeStartOffset) + value.substr(rangeStartOffset + 1); rangeEndOffset = value.indexOf('|'); value = value.substr(0, rangeEndOffset) + value.substr(rangeEndOffset + 1); } else { rangeStartOffset = 0; rangeEndOffset = value.length; } let document = TextDocument.create('test://test/test.html', 'html', 0, value); let range = Range.create(document.positionAt(rangeStartOffset), document.positionAt(rangeEndOffset)); if (!formatOptions) { formatOptions = FormattingOptions.create(2, true); } let result = format(languageModes, document, range, formatOptions, undefined, { css: true, javascript: true }); let actual = TextDocument.applyEdits(document, result); assert.equal(actual, expected, message); } function assertFormatWithFixture(fixtureName: string, expectedPath: string, options?: any, formatOptions?: FormattingOptions): void { let input = fs.readFileSync(path.join(__dirname, '..', '..', 'src', 'test', 'fixtures', 'inputs', fixtureName)).toString().replace(/\r\n/mg, '\n'); let expected = fs.readFileSync(path.join(__dirname, '..', '..', 'src', 'test', 'fixtures', 'expected', expectedPath)).toString().replace(/\r\n/mg, '\n'); assertFormat(input, expected, options, formatOptions, expectedPath); } test('HTML only', function (): any { assertFormat('
Hello
', '\n\n\nHello
\n\n\n'); assertFormat('|Hello
|', '\n\n\nHello
\n\n\n'); assertFormat('|Hello
|', '\nHello
\n'); }); test('HTML & Scripts', function (): any { assertFormat('', '\n\n\n \n\n\n'); assertFormat('', '\n\n\n \n\n\n'); assertFormat('', '\n\n\n \n\n\n'); assertFormat('\n ', '\n\n\n \n\n\n'); assertFormat('\n ', '\n\n\n \n\n\n'); assertFormat('\n ||', '\n '); }); test('HTLM & Scripts - Fixtures', function () { assertFormatWithFixture('19813.html', '19813.html'); assertFormatWithFixture('19813.html', '19813-4spaces.html', undefined, FormattingOptions.create(4, true)); assertFormatWithFixture('19813.html', '19813-tab.html', undefined, FormattingOptions.create(1, false)); assertFormatWithFixture('21634.html', '21634.html'); }); test('Script end tag', function (): any { assertFormat('\n\n ', '\n\n\n \n\n\n'); }); test('HTML & Multiple Scripts', function (): any { assertFormat('\n', '\n\n\n \n \n\n\n'); }); test('HTML & Styles', function (): any { assertFormat('\n', '\n\n\n \n\n\n'); }); test('EndWithNewline', function (): any { let options = { html: { format: { endWithNewline: true } } }; assertFormat('Hello
', '\n\n\nHello
\n\n\n\n', options); assertFormat('|Hello
|', '\nHello
\n', options); assertFormat('', '\n\n\n \n\n\n\n', options); }); test('Inside script', function (): any { assertFormat('\n ', '\n '); assertFormat('\n ', '\n '); }); test('Range after new line', function (): any { assertFormat('\n |\n|', '\n \n'); }); test('bug 36574', function (): any { assertFormat('', ''); }); test('bug 48049', function (): any { assertFormat( [ '', '', '', '', '', '', ' ', '', '', '', ' ', '', '' ].join('\n'), [ '', '', '', '', '', '', '', ' ', '', '', '', '', '', '' ].join('\n') ); }); test('#58435', () => { let options = { html: { format: { contentUnformatted: 'textarea' } } }; const content = [ '', '', '', ' ', '', '', '', ].join('\n'); const expected = [ '', '', '', ' ', '', '', '', ].join('\n'); assertFormat(content, expected, options); }); }); /* content_unformatted: Array(4)["pre", "code", "textarea", …] end_with_newline: false eol: "\n" extra_liners: Array(3)["head", "body", "/html"] indent_char: "\t" indent_handlebars: false indent_inner_html: false indent_size: 1 max_preserve_newlines: 32786 preserve_newlines: true unformatted: Array(1)["wbr"] wrap_attributes: "auto" wrap_attributes_indent_size: undefined wrap_line_length: 120*/