From 1dacc4921d68d0eec97d1fd35094ed781d88d7dd Mon Sep 17 00:00:00 2001 From: Kaiserunix <227545839+Kaiserunix@users.noreply.github.com> Date: Wed, 9 Sep 2026 21:59:32 +0800 Subject: [PATCH] Fix indentation of nested elements in preserved mixed content --- src/printer.js | 2 +- test/__snapshots__/format.test.js.snap | 4 +-- test/format.test.js | 46 ++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/printer.js b/src/printer.js index 753ba610..6b768eeb 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 5a89e2a8..2f73ce35 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 5448dc26..9dddcf57 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