diff --git a/src/testing/html-serializer.ts b/src/testing/html-serializer.ts index e18b0f1..d1a1e01 100644 --- a/src/testing/html-serializer.ts +++ b/src/testing/html-serializer.ts @@ -238,13 +238,18 @@ export function prettifyHtml(html: string): string { lines.push(' '.repeat(indentLevel * indentSize) + part); } else if (part.endsWith('/>')) { - // Self-closing tag + // Self-closing tag (XML style) lines.push(' '.repeat(indentLevel * indentSize) + part); } else { // Opening tag lines.push(' '.repeat(indentLevel * indentSize) + part); - // Increase indent for next content - indentLevel++; + + // Check if it's a void element - if so, don't increase indent + const tagName = part.match(/<([^\s>/]+)/)?.[1]?.toLowerCase(); + if (!tagName || !VOID_ELEMENTS.has(tagName)) { + // Not a void element, increase indent for next content + indentLevel++; + } } } else { // Text content - add on its own line with current indentation diff --git a/test-project/src/components/non-shadow/__snapshots__/non-shadow-jsdom.spec.tsx.snap b/test-project/src/components/non-shadow/__snapshots__/non-shadow-jsdom.spec.tsx.snap new file mode 100644 index 0000000..4a67c07 --- /dev/null +++ b/test-project/src/components/non-shadow/__snapshots__/non-shadow-jsdom.spec.tsx.snap @@ -0,0 +1,50 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`non-shadow-component serialization > nested components > should serialize nested non-shadow components 1`] = ` + +
+
+ + +
+
+ + Default Header + +
+
+ Placeholder Image + + + Nested Header + +
+ +
+
+
+
+ Placeholder Image + +

+ Main Content +

+
+ +
+
+`; diff --git a/test-project/src/components/non-shadow/non-shadow-jsdom.spec.tsx b/test-project/src/components/non-shadow/non-shadow-jsdom.spec.tsx index 103dce6..faa108a 100644 --- a/test-project/src/components/non-shadow/non-shadow-jsdom.spec.tsx +++ b/test-project/src/components/non-shadow/non-shadow-jsdom.spec.tsx @@ -31,6 +31,7 @@ describe('non-shadow-component serialization', () => {
+ Placeholder Image @@ -63,6 +64,7 @@ describe('non-shadow-component serialization', () => {
+ Placeholder Image Default Content @@ -96,6 +98,7 @@ describe('non-shadow-component serialization', () => {
+ Placeholder Image Default Content @@ -137,6 +140,7 @@ describe('non-shadow-component serialization', () => {
+ Placeholder Image @@ -203,6 +207,8 @@ describe('non-shadow-component serialization', () => { , ); + expect(result.root).toMatchSnapshot(); + expect(result.root).toEqualHtml(`
@@ -218,6 +224,7 @@ describe('non-shadow-component serialization', () => {
+ Placeholder Image @@ -234,6 +241,7 @@ describe('non-shadow-component serialization', () => {
+ Placeholder Image diff --git a/test-project/src/components/non-shadow/non-shadow.tsx b/test-project/src/components/non-shadow/non-shadow.tsx index 87ebd71..d3933b6 100644 --- a/test-project/src/components/non-shadow/non-shadow.tsx +++ b/test-project/src/components/non-shadow/non-shadow.tsx @@ -18,6 +18,7 @@ export class NonShadowComponent { Default Header
+ Placeholder Image Default Content