-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarkdown.test.js
More file actions
62 lines (53 loc) · 2.11 KB
/
Copy pathmarkdown.test.js
File metadata and controls
62 lines (53 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import DOMPurify from 'dompurify';
import { marked } from 'marked';
import { cleanContent, renderMarkdown } from './markdown.js';
const _origMarked = globalThis.marked;
const _origDOMPurify = globalThis.DOMPurify;
describe('cleanContent', () => {
it('strips system-reminder blocks', () => {
const raw = 'Hello<system-reminder>secret</system-reminder> world';
expect(cleanContent(raw)).toBe('Hello world');
});
it('returns empty string for falsy input', () => {
expect(cleanContent('')).toBe('');
});
});
describe('renderMarkdown', () => {
beforeEach(() => {
globalThis.marked = marked;
globalThis.DOMPurify = DOMPurify;
});
afterEach(() => {
globalThis.marked = _origMarked;
globalThis.DOMPurify = _origDOMPurify;
});
it('sanitizes script tags from parsed output', () => {
const sanitizeSpy = vi.spyOn(DOMPurify, 'sanitize');
const html = renderMarkdown('# Hello\n\n<script>alert(1)</script>');
expect(sanitizeSpy).toHaveBeenCalled();
expect(html).not.toContain('<script');
expect(html).not.toMatch(/alert\s*\(/);
sanitizeSpy.mockRestore();
});
it('strips event handlers from parsed output', () => {
const sanitizeSpy = vi.spyOn(DOMPurify, 'sanitize');
const html = renderMarkdown('<img src=x onerror=alert(1)>');
expect(sanitizeSpy).toHaveBeenCalled();
expect(html).not.toMatch(/onerror/i);
sanitizeSpy.mockRestore();
});
it('falls back to inline code when marked is unavailable', () => {
delete globalThis.marked;
const html = renderMarkdown('`code`');
expect(html).toBe('<code>code</code>');
});
it('falls back to escaped output when DOMPurify is unavailable', () => {
delete globalThis.DOMPurify;
const html = renderMarkdown('Hello **world**');
expect(html).toBeDefined();
expect(html).toContain('Hello');
expect(html).toContain('**world**');
expect(html).not.toMatch(/<script/i);
});
});