feat(training): rich-text lessons with inline images and 25 MB manuals
CI / build (push) Has been cancelled
CI / build (push) Has been cancelled
Admins were authoring plain-text lessons, which made illustrated tutorials and reordering images impossible. Lesson bodies are now sanitized HTML from a Tiptap editor with alignment, sizing, drag-reorder and alt text, and inline images reuse the training_resources table so no migration is needed. Attachments grow to 25 MB, PDFs stream inline, and SVG is rejected because it can execute as a document. Legacy plain-text bodies are normalized on read. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sanitizeTrainingHtml, trainingBodyToHtml } from "./rich-text";
|
||||
|
||||
describe("sanitizeTrainingHtml", () => {
|
||||
it("keeps editor formatting and removes scripts and event handlers", () => {
|
||||
const html = sanitizeTrainingHtml(
|
||||
'<h2 onclick="alert(1)">Gain staging</h2><p>Set <strong>gain</strong> before <em>the service</em>.</p><script>alert(1)</script>',
|
||||
);
|
||||
|
||||
expect(html).toContain("<h2>Gain staging</h2>");
|
||||
expect(html).toContain("<strong>gain</strong>");
|
||||
expect(html).toContain("<em>the service</em>");
|
||||
expect(html).not.toContain("onclick");
|
||||
expect(html).not.toContain("<script");
|
||||
});
|
||||
|
||||
it("keeps uploaded images but drops data URLs and inline handlers", () => {
|
||||
const allowed = sanitizeTrainingHtml(
|
||||
'<img src="/api/training/files/abc" alt="Board" width="50%" data-align="right" onerror="alert(1)">',
|
||||
);
|
||||
expect(allowed).toContain('src="/api/training/files/abc"');
|
||||
expect(allowed).toContain('alt="Board"');
|
||||
expect(allowed).toContain('width="50%"');
|
||||
expect(allowed).toContain('data-align="right"');
|
||||
expect(allowed).not.toContain("onerror");
|
||||
|
||||
const dataUrl = sanitizeTrainingHtml(
|
||||
'<img src="data:image/png;base64,abc" alt="pasted">',
|
||||
);
|
||||
expect(dataUrl).not.toContain("<img");
|
||||
});
|
||||
|
||||
it("opens external links safely and keeps internal links local", () => {
|
||||
const external = sanitizeTrainingHtml(
|
||||
'<a href="https://example.com/manual">Manual</a>',
|
||||
);
|
||||
expect(external).toContain('target="_blank"');
|
||||
expect(external).toContain('rel="noopener noreferrer nofollow"');
|
||||
|
||||
const internal = sanitizeTrainingHtml('<a href="/training">Training</a>');
|
||||
expect(internal).toContain('href="/training"');
|
||||
expect(internal).not.toContain('target="_blank"');
|
||||
});
|
||||
});
|
||||
|
||||
describe("trainingBodyToHtml", () => {
|
||||
it("turns legacy plain text into paragraphs", () => {
|
||||
expect(trainingBodyToHtml("Step one\nStep two")).toBe(
|
||||
"<p>Step one</p><p>Step two</p>",
|
||||
);
|
||||
});
|
||||
|
||||
it("escapes legacy text instead of interpreting it as markup", () => {
|
||||
expect(trainingBodyToHtml("Use <b> carefully")).toBe(
|
||||
"<p>Use <b> carefully</p>",
|
||||
);
|
||||
});
|
||||
|
||||
it("sanitizes already-authored HTML", () => {
|
||||
expect(trainingBodyToHtml("<p>Hello</p><script>alert(1)</script>")).toBe(
|
||||
"<p>Hello</p>",
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user