From fbbb507c53a3a3de02cd96d44b0de04c2ead73b6 Mon Sep 17 00:00:00 2001 From: Harsh Date: Fri, 15 May 2026 17:40:28 +0200 Subject: [PATCH] fix: remove trailing newline after last rendered line --- src/code-view/__tests__/code-view.test.tsx | 27 +++++++++++++++++----- src/code-view/highlight/index.tsx | 2 +- src/code-view/internal.tsx | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/code-view/__tests__/code-view.test.tsx b/src/code-view/__tests__/code-view.test.tsx index 437aed6..f896f0c 100644 --- a/src/code-view/__tests__/code-view.test.tsx +++ b/src/code-view/__tests__/code-view.test.tsx @@ -14,16 +14,24 @@ describe("CodeView", () => { cleanup(); }); test("correctly renders simple content", () => { - render(); - const wrapper = createWrapper()!.findCodeView(); - expect(wrapper!.findContent().getElement()).toHaveTextContent("Hello World"); + const input = "Hello World"; + render(); + const wrapper = createWrapper()!.findCodeView()!; + expect(wrapper.findContent().getElement().textContent).toBe(input); }); test("correctly renders multi line content", () => { - render(); + const input = `# Hello World\n\nThis is a markdown example.`; + render(); const wrapper = createWrapper()!.findCodeView()!; - const content = wrapper.findContent(); - expect(content.getElement()).toHaveTextContent("# Hello World This is a markdown example."); + expect(wrapper.findContent().getElement().textContent).toBe(input); + }); + + test("preserves a trailing newline when present in the input", () => { + const input = "a\nb\n"; + render(); + const wrapper = createWrapper()!.findCodeView()!; + expect(wrapper.findContent().getElement().textContent).toBe(input); }); test("correctly renders table markup with line numbers", () => { @@ -102,6 +110,13 @@ describe("CodeView", () => { expect(getByText(element, '"world"')).toHaveClass("ace_string"); }); + test("renders highlighted content without trailing newline", () => { + const input = 'const hello: string = "world";'; + render(); + const wrapper = createWrapper()!.findCodeView()!; + expect(wrapper.findContent().getElement().textContent).toBe(input); + }); + test("sets nowrap class to line if linesWrapping undefined", () => { render(); const wrapper = createWrapper().findCodeView()!; diff --git a/src/code-view/highlight/index.tsx b/src/code-view/highlight/index.tsx index d0103e8..84a9840 100644 --- a/src/code-view/highlight/index.tsx +++ b/src/code-view/highlight/index.tsx @@ -25,7 +25,7 @@ export function createHighlight(rules: Ace.HighlightRules): CreateHighlightType token.value ); })} - {"\n"} + {lineIndex < tokens.length - 1 ? "\n" : ""} ))} diff --git a/src/code-view/internal.tsx b/src/code-view/internal.tsx index 496c640..48f3e4e 100644 --- a/src/code-view/internal.tsx +++ b/src/code-view/internal.tsx @@ -28,7 +28,7 @@ const textHighlight = (code: string) => { {lines.map((line, lineIndex) => ( {line} - {"\n"} + {lineIndex < lines.length - 1 ? "\n" : ""} ))}