Skip to content

Commit 091548e

Browse files
committed
normalize new lines
1 parent 102965c commit 091548e

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/components/ResultGrid/ResultGrid.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
isLeftAligned,
2727
formatCellValue,
2828
formatColumnType,
29+
toSingleLineDisplay,
2930
} from "./inlineGridUtils"
3031
import { useGridKeyboardNav } from "./useGridKeyboardNav"
3132
import {
@@ -105,7 +106,9 @@ const GridCell = React.memo(function GridCell({
105106
}: GridCellProps) {
106107
const colType = col?.type ?? ""
107108
const align = isLeftAligned(colType) ? "left" : "right"
108-
const displayValue = loaded ? formatCellValue(rawValue, col, colWidth) : ""
109+
const displayValue = loaded
110+
? toSingleLineDisplay(formatCellValue(rawValue, col, colWidth))
111+
: ""
109112
return (
110113
<Cell
111114
id={`cell-${rowIndex}-${colIndex}`}

src/components/ResultGrid/inlineGridUtils.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
formatCellValueForCopy,
77
formatColumnType,
88
isLeftAligned,
9+
toSingleLineDisplay,
910
} from "./inlineGridUtils"
1011
import type { ColumnDefinition } from "../../utils/questdb/types"
1112

@@ -193,6 +194,34 @@ describe("formatCellValueForCopy", () => {
193194
// Then the copied text is preserved exactly as returned by the server
194195
expect(out).toBe("a&amp;b&lt;c&gt;d")
195196
})
197+
198+
it("preserves embedded line breaks so copy keeps the raw value", () => {
199+
const out = formatCellValueForCopy(
200+
"line1\nline2\r\nline3",
201+
col("x", "VARCHAR"),
202+
)
203+
204+
expect(out).toBe("line1\nline2\r\nline3")
205+
})
206+
})
207+
208+
describe("toSingleLineDisplay", () => {
209+
it("collapses line breaks to a single space so cells render on one line", () => {
210+
expect(toSingleLineDisplay("line1\nline2")).toBe("line1 line2")
211+
expect(toSingleLineDisplay("line1\r\nline2")).toBe("line1 line2")
212+
expect(toSingleLineDisplay("line1\rline2")).toBe("line1 line2")
213+
expect(toSingleLineDisplay("a\n\n\nb")).toBe("a b")
214+
})
215+
216+
it("preserves leading and interior spaces (rendered via white-space: pre)", () => {
217+
expect(toSingleLineDisplay(" indented")).toBe(" indented")
218+
expect(toSingleLineDisplay("a b")).toBe("a b")
219+
})
220+
221+
it("leaves values without line breaks untouched", () => {
222+
expect(toSingleLineDisplay("a&amp;b<c>d")).toBe("a&amp;b<c>d")
223+
expect(toSingleLineDisplay("")).toBe("")
224+
})
196225
})
197226

198227
describe("sampleColumnWidths", () => {

src/components/ResultGrid/inlineGridUtils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,6 @@ export const formatCellValueForCopy = (
174174

175175
return formatCellValue(value, col)
176176
}
177+
178+
export const toSingleLineDisplay = (text: string): string =>
179+
text.replace(/[\r\n]+/g, " ")

src/js/console/grid.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { toast } from "../../components"
2626
import { trackEvent } from "../../modules/ConsoleEventTracker"
2727
import { ConsoleEvent } from "../../modules/ConsoleEventTracker/events"
2828
import { buildResultPageMarkdown } from "../../components/ResultGrid/resultPageMarkdown"
29+
import { toSingleLineDisplay } from "../../components/ResultGrid/inlineGridUtils"
2930

3031
const hashString = (str) => {
3132
let hash = 0
@@ -1106,7 +1107,9 @@ export function grid(rootElement, _paginationFn, id) {
11061107
if (cellData !== null) {
11071108
const layoutEntry = getLayoutEntry()
11081109
const columnWidth = layoutEntry.deviants[column.name] ?? null
1109-
cell.textContent = getDisplayedCellValue(column, cellData, columnWidth)
1110+
cell.textContent = toSingleLineDisplay(
1111+
getDisplayedCellValue(column, cellData, columnWidth),
1112+
)
11101113

11111114
cell.classList.remove("qg-null")
11121115

0 commit comments

Comments
 (0)