28 lines
1.6 KiB
TypeScript
28 lines
1.6 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { sanitizeHtml } from './sanitize';
|
|
|
|
describe('sanitizeHtml', () => {
|
|
it('keeps structural/prose tags', () => {
|
|
const html = '<h1>T</h1><p>a <strong>b</strong> <em>c</em></p><ul><li>x</li></ul><pre><code>y</code></pre><table><tr><td>z</td></tr></table>';
|
|
expect(sanitizeHtml(html)).toBe(html);
|
|
});
|
|
it('strips script/style/iframe and event handlers', () => {
|
|
expect(sanitizeHtml('<p onclick="x()">a</p><script>evil()</script><iframe src="x"></iframe>')).toBe('<p>a</p>');
|
|
});
|
|
it('strips data: and vbscript: urls', () => {
|
|
expect(sanitizeHtml('<a href="data:text/html,x">y</a>')).toBe('<a>y</a>');
|
|
expect(sanitizeHtml('<img src="data:image/svg+xml,x">')).toBe('<img>');
|
|
expect(sanitizeHtml('<a href="vbscript:x">y</a>')).toBe('<a>y</a>');
|
|
});
|
|
|
|
it('keeps safe links and images, strips javascript: urls', () => {
|
|
expect(sanitizeHtml('<a href="https://x.y">l</a><a href="javascript:e()">m</a><img src="https://x.y/i.png" alt="i">'))
|
|
.toBe('<a href="https://x.y">l</a><a>m</a><img src="https://x.y/i.png" alt="i">');
|
|
});
|
|
it('realistic Moodle fragment: div/span survive, class stripped', () => {
|
|
const input = '<div class="no-overflow"><h3>Registres ESP32</h3><p>Le registre <span class="highlight">GPIO_OUT</span> contrôle les sorties.</p><pre><code>#define GPIO_OUT 0x3FF44004</code></pre></div>';
|
|
const expected = '<div><h3>Registres ESP32</h3><p>Le registre <span>GPIO_OUT</span> contrôle les sorties.</p><pre><code>#define GPIO_OUT 0x3FF44004</code></pre></div>';
|
|
expect(sanitizeHtml(input)).toBe(expected);
|
|
});
|
|
});
|