/*--------------------------------------------------------------------------------------------- * 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'; import { getNodeFSRequestService } from '../node/nodeFs'; suite('HTML Embedded Formatting', () => { async function assertFormat(value: string, expected: string, options?: any, formatOptions?: FormattingOptions, message?: string): Promise { let workspace = { settings: options, folders: [{ name: 'foo', uri: 'test://foo' }] }; const languageModes = getLanguageModes({ css: true, javascript: true }, workspace, ClientCapabilities.LATEST, getNodeFSRequestService()); 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 = await format(languageModes, document, range, formatOptions, undefined, { css: true, javascript: true }); let actual = TextDocument.applyEdits(document, result); assert.equal(actual, expected, message); } async function assertFormatWithFixture(fixtureName: string, expectedPath: string, options?: any, formatOptions?: FormattingOptions): Promise { 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'); await assertFormat(input, expected, options, formatOptions, expectedPath); } test('HTML only', async () => { await assertFormat('

Hello

', '\n\n\n

Hello

\n\n\n'); await assertFormat('|

Hello

|', '\n\n\n

Hello

\n\n\n'); await assertFormat('|

Hello

|', '\n

Hello

\n'); }); test('HTML & Scripts', async () => { await assertFormat('', '\n\n\n \n\n\n'); await assertFormat('', '\n\n\n \n\n\n'); await assertFormat('', '\n\n\n \n\n\n'); await assertFormat('\n ', '\n\n\n \n\n\n'); await assertFormat('\n ', '\n\n\n \n\n\n'); await assertFormat('\n ||', '\n '); }); test('HTLM & Scripts - Fixtures', async () => { 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', async () => { await assertFormat('\n\n ', '\n\n\n \n\n\n'); }); test('HTML & Multiple Scripts', async () => { await assertFormat('\n', '\n\n\n \n \n\n\n'); }); test('HTML & Styles', async () => { await assertFormat('\n', '\n\n\n \n\n\n'); }); test('EndWithNewline', async () => { let options = { html: { format: { endWithNewline: true } } }; await assertFormat('

Hello

', '\n\n\n

Hello

\n\n\n\n', options); await assertFormat('|

Hello

|', '\n

Hello

\n', options); await assertFormat('', '\n\n\n \n\n\n\n', options); }); test('Inside script', async () => { await assertFormat('\n ', '\n '); await assertFormat('\n ', '\n '); }); test('Range after new line', async () => { await assertFormat('\n |\n|', '\n \n'); }); test('bug 36574', async () => { await assertFormat('', ''); }); test('bug 48049', async () => { await assertFormat( [ '', '', '', '', '', '', ' ', '', '', '', ' ', '', '' ].join('\n'), [ '', '', '', '', '', '', '', ' ', '', '', '', '', '', '' ].join('\n') ); }); test('#58435', async () => { let options = { html: { format: { contentUnformatted: 'textarea' } } }; const content = [ '', '', '', ' ', '', '', '', ].join('\n'); const expected = [ '', '', '', ' ', '', '', '', ].join('\n'); await 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*/