fix: test strings, logical sides in the PDF markdown styles, one export per markdown module

This commit is contained in:
Maurice
2026-10-24 08:46:22 +02:00
parent a0554c2346
commit 0534d7b3a7
8 changed files with 44 additions and 51 deletions
+2 -7
View File
@@ -399,13 +399,8 @@ describe('downloadTripPDF', () => {
// #2692: a place's description and notes print as the planner shows them, not as raw markdown.
it('FE-COMP-TRIPPDF-016c: renders markdown in a place description, sanitized', async () => {
const place = { ...placeWithDetails, description: '**Colosseum** tickets
| Gate | Time |
| --- | --- |
| A | 9:00 |
<script>alert(1)</script>' }
const description = ['**Colosseum** tickets', '', '| Gate | Time |', '| --- | --- |', '| A | 9:00 |', '', '<script>alert(1)</script>'].join('\n')
const place = { ...placeWithDetails, description }
const md = { ...richArgs, places: [place], assignments: { '10': [{ ...assignmentForDay, place }] } }
await downloadTripPDF(md as any)
const html = getIframe()!.srcdoc
+5 -5
View File
@@ -24,7 +24,7 @@ import { fetchExchangeRates } from '../../hooks/useExchangeRates'
import { getFlightLegs, getTrainLegs, usesStationRoute } from '../../utils/flightLegs'
import { isServiceStopType } from '../Roadtrip/roadtripModel'
import { onlyMyPlan } from './pdfScope'
import { createMarkdownHtml } from '../shared/SafeMarkdown'
import { createMarkdownHtml } from '../shared/markdownHtml'
/**
* Every day starts a new page by default. On a trip of short days that prints
@@ -845,13 +845,13 @@ export async function downloadTripPDF({ trip, days, places, assignments: allStor
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md table, .md blockquote, .md pre { margin: 0 0 4px; }
.md ul, .md ol { padding-left: 14px; }
.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 { font-size: 10px; font-weight: 700; color: #475569; margin: 4px 0 2px; }
.md ul, .md ol { padding-inline-start: 14px; }
.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 { font-size: 1.1em; font-weight: 700; margin: 4px 0 2px; }
.md table { border-collapse: collapse; }
.md th, .md td { border: 1px solid #e2e8f0; padding: 2px 5px; text-align: left; }
.md th, .md td { border: 1px solid #e2e8f0; padding: 2px 5px; text-align: start; }
.md code { font-family: ui-monospace, monospace; font-size: 8.5px; }
.md a { color: inherit; text-decoration: underline; }
.md blockquote { border-left: 2px solid #e2e8f0; padding-left: 6px; }
.md blockquote { border-inline-start: 2px solid #e2e8f0; padding-inline-start: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; padding-left: 21px; margin-top: 4px; }
.chip { display: inline-flex; align-items: center; gap: 3px; font-size: 8px; font-weight: 600; background: #f1f5f9; color: #374151; border-radius: 99px; padding: 2px 7px; white-space: nowrap; }
@@ -1,7 +1,8 @@
import { render, screen } from '@testing-library/react';
import { describe, expect, it } from 'vitest';
import { SafeMarkdown, createMarkdownHtml } from './SafeMarkdown';
import { createMarkdownHtml } from './markdownHtml';
import { SafeMarkdown } from './SafeMarkdown';
describe('SafeMarkdown (#2692)', () => {
it('FE-COMP-SAFEMD-001: renders GitHub markdown with kept line breaks and safe links', () => {
@@ -1,5 +1,3 @@
import { flushSync } from 'react-dom';
import { createRoot } from 'react-dom/client';
import Markdown from 'react-markdown';
import remarkBreaks from 'remark-breaks';
import remarkGfm from 'remark-gfm';
@@ -22,26 +20,3 @@ export function SafeMarkdown({ children }: { children: string }) {
</Markdown>
);
}
/**
* Turns markdown into the HTML {@link SafeMarkdown} renders, for output built as
* a string such as the PDF export (#2692). It renders through the same
* pipeline into a detached node, so the sanitizer is the one the app uses and
* no second markdown library is bundled. Call it outside a render and dispose
* of it when done; the same text is rendered once.
*/
export function createMarkdownHtml(): { html: (text: string) => string; dispose: () => void } {
const host = document.createElement('div');
const root = createRoot(host);
const done = new Map<string, string>();
return {
html: (text) => {
const known = done.get(text);
if (known !== undefined) return known;
flushSync(() => root.render(<SafeMarkdown>{text}</SafeMarkdown>));
done.set(text, host.innerHTML);
return host.innerHTML;
},
dispose: () => root.unmount(),
};
}
@@ -0,0 +1,27 @@
import { flushSync } from 'react-dom';
import { createRoot } from 'react-dom/client';
import { SafeMarkdown } from './SafeMarkdown';
/**
* Turns markdown into the HTML {@link SafeMarkdown} renders, for output built as
* a string such as the PDF export (#2692). It renders through the same
* pipeline into a detached node, so the sanitizer is the one the app uses and
* no second markdown library is bundled. Call it outside a render and dispose
* of it when done; the same text is rendered once.
*/
export function createMarkdownHtml(): { html: (text: string) => string; dispose: () => void } {
const host = document.createElement('div');
const root = createRoot(host);
const done = new Map<string, string>();
return {
html: (text) => {
const known = done.get(text);
if (known !== undefined) return known;
flushSync(() => root.render(<SafeMarkdown>{text}</SafeMarkdown>));
done.set(text, host.innerHTML);
return host.innerHTML;
},
dispose: () => root.unmount(),
};
}
@@ -125,7 +125,8 @@ export default function MTransportSheet({ planner, shell }: MTripSheetsProps) {
// Per-segment booking codes (#1943), only on a real stopover booking: the
// single-leg fallback would just echo the booking's own code shown below.
const routeLegs: (FlightLeg | TrainLeg)[] = res.type === 'flight' ? getFlightLegs(res) : usesStationRoute(res.type) ? getTrainLegs(res) : []
const isFlight = res.type === 'flight'
const routeLegs: (FlightLeg | TrainLeg)[] = isFlight ? getFlightLegs(res) : usesStationRoute(res.type) ? getTrainLegs(res) : []
const legCodes = routeLegs.length > 1 ? routeLegs.filter(l => l.confirmation_number) : []
// Opened from one leg's row, the sheet shows that leg's own number, seat,
// platform and times rather than the first leg's (#2701).
@@ -148,8 +149,8 @@ export default function MTransportSheet({ planner, shell }: MTripSheetsProps) {
else if (date) subParts.push(bookingDayLabel(date, locale))
// A flight's legs carry no seat or platform of their own, so those stay the booking's.
const seat = leg && res.type !== 'flight' ? legDetail.seat : meta.seat
const platform = leg && res.type !== 'flight' ? legDetail.platform : meta.platform
const seat = leg && !isFlight ? legDetail.seat : meta.seat
const platform = leg && !isFlight ? legDetail.platform : meta.platform
const transitLegs: TransitLeg[] = Array.isArray(meta.transit?.legs) ? meta.transit.legs : []
const resFiles = filesFor(res, planner.files || [])
+3 -9
View File
@@ -1664,15 +1664,9 @@ describe('SharedTripPage', () => {
21: [{
id: 304, day_id: 21, order_index: 0, notes: '**Bring** cash',
place: {
id: 504, name: 'Market', description: '## Opening
| Day | Hours |
| --- | --- |
| Sat | 8 to 14 |
<img src=x onerror=alert(1)>',
address: 'Market Square 1', lat: null, lng: null, duration_minutes: 0, notes: '- fruit
- bread',
id: 504, name: 'Market',
description: ['## Opening', '', '| Day | Hours |', '| --- | --- |', '| Sat | 8 to 14 |', '', '<img src=x onerror=alert(1)>'].join('\n'),
address: 'Market Square 1', lat: null, lng: null, duration_minutes: 0, notes: ['- fruit', '- bread'].join('\n'),
website: null, phone: null, category_id: null, price: null, place_time: null, end_time: null,
image_url: null, transport_mode: 'walking', category: null, tags: [],
},
@@ -193,7 +193,7 @@ describe('TransportRow', () => {
expect(screen.getByText('LH 714 to Tokyo')).toBeInTheDocument()
expect(container.textContent).toContain('08:15 – 18:40')
expect(screen.getByText('Lufthansa · LH714 · MUC → HND')).toBeInTheDocument()
expect(screen.getByText('Lufthansa, LH714, MUC → HND')).toBeInTheDocument()
})
it('FE-MOB-PLROW-014: a multi-day span shows only the day-relevant time', () => {