mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
fix: four reported bugs that dev still had
- Dashboard: a browser that names no time zone (Chrome on Windows for a zone it cannot map) crashed the world clocks and the whole dashboard. One helper now reads the browser zone and every reader picks its fallback; a stored zone the browser does not know is left out instead of throwing (#2694). - Phone: a leg of a multi-leg train or flight showed only its route, and its sheet showed the first leg's seat. The day plan subtitle is one helper for desktop and phone now, so a leg names its own train or flight, platform and seat, translated instead of the hardcoded "Gl."/"Sitz"; the sheet opened from a leg shows that leg's number, seat, platform and times (#2701). The PDF export translates platform and seat too. - Journey list: the year badge named the year a journey was written, not the years of its trips; a trip over New Year shows both (#2685). - A place's description and notes are markdown in the planner but printed as raw syntax in the PDF export and on a shared trip. Both render it now through the app's sanitized markdown pipeline, raw HTML stays text (#2692). Closes #2694 Closes #2701 Closes #2685 Closes #2692
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { SafeMarkdown, createMarkdownHtml } from './SafeMarkdown';
|
||||
|
||||
describe('SafeMarkdown (#2692)', () => {
|
||||
it('FE-COMP-SAFEMD-001: renders GitHub markdown with kept line breaks and safe links', () => {
|
||||
render(<SafeMarkdown>{'# Title\nfirst\nsecond\n\n[site](https://example.com)'}</SafeMarkdown>);
|
||||
expect(screen.getByRole('heading', { name: 'Title' })).toBeInTheDocument();
|
||||
expect(document.querySelector('br')).not.toBeNull();
|
||||
const link = screen.getByRole('link', { name: 'site' });
|
||||
expect(link).toHaveAttribute('target', '_blank');
|
||||
expect(link).toHaveAttribute('rel', 'noopener noreferrer nofollow');
|
||||
});
|
||||
|
||||
it('FE-COMP-SAFEMD-002: shows raw HTML as text and never as an element', () => {
|
||||
render(<SafeMarkdown>{'Room <TBD> <img src=x onerror=alert(1)>'}</SafeMarkdown>);
|
||||
expect(document.querySelector('img')).toBeNull();
|
||||
expect(screen.getByText(/Room <TBD>/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('createMarkdownHtml (#2692)', () => {
|
||||
it('FE-COMP-SAFEMD-003: returns the sanitized HTML, the same for the same text', () => {
|
||||
const markdown = createMarkdownHtml();
|
||||
try {
|
||||
const html = markdown.html('**bold** and <script>alert(1)</script>');
|
||||
expect(html).toContain('<strong>bold</strong>');
|
||||
expect(html).not.toContain('<script>');
|
||||
expect(markdown.html('**bold** and <script>alert(1)</script>')).toBe(html);
|
||||
expect(markdown.html('- a\n- b')).toContain('<li>a</li>');
|
||||
} finally {
|
||||
markdown.dispose();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,47 @@
|
||||
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';
|
||||
import { sanitizedMarkdownComponents, sanitizedMarkdownPlugins } from './markdownSanitize';
|
||||
|
||||
/**
|
||||
* Text a user wrote in markdown (a place's description, its notes), rendered
|
||||
* the way the planner shows it: GitHub flavour, line breaks kept, raw HTML
|
||||
* shown as text, the rest sanitized, links in a new tab without an opener.
|
||||
* Safe for pages strangers see, like a shared trip (#2692).
|
||||
*/
|
||||
export function SafeMarkdown({ children }: { children: string }) {
|
||||
return (
|
||||
<Markdown
|
||||
remarkPlugins={[remarkGfm, remarkBreaks]}
|
||||
rehypePlugins={sanitizedMarkdownPlugins}
|
||||
components={sanitizedMarkdownComponents}
|
||||
>
|
||||
{children}
|
||||
</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(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user