diff --git a/src/printer.js b/src/printer.js index 753ba61..6b768ee 100644 --- a/src/printer.js +++ b/src/printer.js @@ -464,7 +464,7 @@ function printElement(path, opts, print) { ) { return group([ openTag, - fragments.map(({ printed }) => printed), + indent(fragments.map(({ printed }) => printed)), closeTag ]); } diff --git a/test/__snapshots__/format.test.js.snap b/test/__snapshots__/format.test.js.snap index 5a89e2a..2f73ce3 100644 --- a/test/__snapshots__/format.test.js.snap +++ b/test/__snapshots__/format.test.js.snap @@ -1400,8 +1400,8 @@ use {
text with space
-
-
+
+
even more diff --git a/test/format.test.js b/test/format.test.js index 5448dc2..9dddcf5 100644 --- a/test/format.test.js +++ b/test/format.test.js @@ -73,6 +73,52 @@ test("xmlWhitespaceSensitivity => preserve", async () => { expect(formatted).toMatchSnapshot(); }); +test.each([2, 4])( + "preserve indents nested elements after text with tabWidth %i", + async (tabWidth) => { + const space = " ".repeat(tabWidth); + const input = [ + "", + `${space}`, + `${space.repeat(2)}Text`, + `${space.repeat(2)}`, + `${space.repeat(3)}`, + `${space.repeat(2)}`, + `${space}`, + "", + "" + ].join("\n"); + const options = { xmlWhitespaceSensitivity: "preserve", tabWidth }; + + const formatted = await format(input, options); + expect(formatted).toBe(input); + expect(await format(formatted, options)).toBe(formatted); + } +); + +test("preserve indents nested elements after text with tabs", async () => { + const input = "\n\tText\n\t\n\t\t\n\t\n\n"; + expect( + await format(input, { xmlWhitespaceSensitivity: "preserve", useTabs: true }) + ).toBe(input); +}); + +test("preserve keeps inline mixed text whitespace", async () => { + const input = " before middle after \n"; + expect(await format(input, { xmlWhitespaceSensitivity: "preserve" })).toBe( + input + ); +}); + +test.each(["strict", "preserve", "ignore"])( + "xml:space preserves mixed content in %s mode", + async (xmlWhitespaceSensitivity) => { + const input = + '\n Text\n \n \n \n\n'; + expect(await format(input, { xmlWhitespaceSensitivity })).toBe(input); + } +); + test("xmlSortAttributesByKey => true", async () => { const formatted = await format(fixture, { xmlSortAttributesByKey: true