Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: Preserve paragraph and line breaks in translation dialog Original column (#15066)

Damian Legawiec1b3818d19ab8
diff --git a/packages/dashboard/src/components/spree/translations/resource-translations-dialog.tsx b/packages/dashboard/src/components/spree/translations/resource-translations-dialog.tsx
index 9df705a387..29abb34fc8 100644
--- a/packages/dashboard/src/components/spree/translations/resource-translations-dialog.tsx
+++ b/packages/dashboard/src/components/spree/translations/resource-translations-dialog.tsx
@@ -355,7 +355,7 @@ function TranslationGrid({
{row.rowLabel ?? fieldLabel(row.resourceType, field)}
</th>
<td
- className="border-b border-r px-3 py-2 align-top text-muted-foreground"
+ className="border-b border-r px-3 py-2 align-top whitespace-pre-wrap text-muted-foreground"
data-testid={`source-${cellName}`}
>
{stripHtml(field.source ?? '')}
@@ -417,9 +417,41 @@ function flattenTree(data: ResourceTranslations, fields?: readonly string[]): Tr
return rows
}
-// Plain text of an HTML fragment. DOMParser handles nested/malformed tags
-// correctly — a single-pass regex strip can leak tags via patterns like
-// `<scr<script>ipt>` (CodeQL js/incomplete-multi-character-sanitization).
+// Plain text of an HTML fragment, keeping paragraph and line breaks so the
+// read-only "Original" column mirrors the structure translators are working on.
+// DOMParser handles nested/malformed tags correctly — a single-pass regex strip
+// can leak tags via patterns like `<scr<script>ipt>`
+// (CodeQL js/incomplete-multi-character-sanitization).
+const BLOCK_SELECTOR = 'p, div, li, ul, ol, blockquote, pre, h1, h2, h3, h4, h5, h6, tr'
+
function stripHtml(html: string): string {
- return (new DOMParser().parseFromString(html, 'text/html').body.textContent ?? '').trim()
+ const doc = new DOMParser().parseFromString(html, 'text/html')
+ // Drop indentation-only text nodes at block boundaries — the cell renders with
+ // `whitespace-pre-wrap`, so they would show up as leading spaces. Whitespace
+ // between inline elements (`<b>a</b> <i>b</i>`) and inside `<pre>` is kept.
+ const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_TEXT)
+ const blankNodes: Node[] = []
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {
+ if (node.textContent?.trim() !== '' || node.parentElement?.closest('pre')) continue
+ const isBlock = (n: Node | null) => n instanceof Element && n.matches(BLOCK_SELECTOR)
+ const atBlockEdge = isBlock(node.parentElement) && (!node.previousSibling || !node.nextSibling)
+ if (atBlockEdge || isBlock(node.previousSibling) || isBlock(node.nextSibling)) {
+ blankNodes.push(node)
+ }
+ }
+ for (const node of blankNodes) node.parentNode?.removeChild(node)
+
+ for (const br of Array.from(doc.body.querySelectorAll('br'))) {
+ br.replaceWith(doc.createTextNode('\n'))
+ }
+ for (const block of Array.from(doc.body.querySelectorAll(BLOCK_SELECTOR))) {
+ if (block.tagName === 'LI') {
+ block.append(doc.createTextNode('\n'))
+ } else if (
+ !(block.tagName === 'P' && block.parentElement?.tagName === 'LI' && !block.nextElementSibling)
+ ) {
+ block.append(doc.createTextNode('\n\n'))
+ }
+ }
+ return (doc.body.textContent ?? '').replace(/\n{3,}/g, '\n\n').trim()
}