Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ function printElement(path, opts, print) {
) {
return group([
openTag,
fragments.map(({ printed }) => printed),
indent(fragments.map(({ printed }) => printed)),
closeTag
]);
}
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1400,8 +1400,8 @@ use {
</span>

<div> text with space<div>
<hr />
</div> </div>
<hr />
</div> </div>

<div>
even more
Expand Down
46 changes: 46 additions & 0 deletions test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
"<root>",
`${space}<a>`,
`${space.repeat(2)}Text`,
`${space.repeat(2)}<b>`,
`${space.repeat(3)}<c />`,
`${space.repeat(2)}</b>`,
`${space}</a>`,
"</root>",
""
].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 = "<a>\n\tText\n\t<b>\n\t\t<c />\n\t</b>\n</a>\n";
expect(
await format(input, { xmlWhitespaceSensitivity: "preserve", useTabs: true })
).toBe(input);
});

test("preserve keeps inline mixed text whitespace", async () => {
const input = "<a> before <b> middle </b> after </a>\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 =
'<a xml:space="preserve">\n Text\n <b>\n <c />\n </b>\n</a>\n';
expect(await format(input, { xmlWhitespaceSensitivity })).toBe(input);
}
);

test("xmlSortAttributesByKey => true", async () => {
const formatted = await format(fixture, {
xmlSortAttributesByKey: true
Expand Down