// Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. import { defaultSanitizer } from '@jupyterlab/apputils'; describe('defaultSanitizer', () => { describe('#sanitize()', () => { it('should allow h1 tags', () => { const h1 = '

foo

'; expect(defaultSanitizer.sanitize(h1)).toBe(h1); }); it('should allow h2 tags', () => { const h2 = '

foo

'; expect(defaultSanitizer.sanitize(h2)).toBe(h2); }); it('should not allow svg tags', () => { const svg = 'foo'; expect(defaultSanitizer.sanitize(svg)).toBe('foo'); }); it('should allow img tags and some attributes', () => { const img = 'Smiley face'; expect(defaultSanitizer.sanitize(img)).toBe(img); }); it('should allow span tags and class attribute', () => { const span = 'bar'; expect(defaultSanitizer.sanitize(span)).toBe(span); }); it('should set the rel attribute for tags to "nofollow', () => { const a = 'Baz'; const expected = a.replace('foo', 'nofollow'); expect(defaultSanitizer.sanitize(a)).toBe(expected); }); it('should allow the class attribute for code tags', () => { const code = 'bar'; expect(defaultSanitizer.sanitize(code)).toBe(code); }); it('should allow the class attribute for div tags', () => { const div = '
bar
'; expect(defaultSanitizer.sanitize(div)).toBe(div); }); it('should allow the class attribute for p tags', () => { const p = '

bar

'; expect(defaultSanitizer.sanitize(p)).toBe(p); }); it('should allow the class attribute for pre tags', () => { const pre = '
bar
'; expect(defaultSanitizer.sanitize(pre)).toBe(pre); }); it('should strip script tags', () => { const script = ''; expect(defaultSanitizer.sanitize(script)).toBe(''); }); it('should strip iframe tags', () => { const script = ''; expect(defaultSanitizer.sanitize(script)).toBe(''); }); it('should strip link tags', () => { const link = ''; expect(defaultSanitizer.sanitize(link)).toBe(''); }); it('should pass through simple well-formed whitelisted markup', () => { const div = '

Hello there

'; expect(defaultSanitizer.sanitize(div)).toBe(div); }); it('should allow video tags with some attributes', () => { const video = ''; expect(defaultSanitizer.sanitize(video)).toBe(video); }); it('should allow audio tags with some attributes', () => { const audio = ''; expect(defaultSanitizer.sanitize(audio)).toBe(audio); }); it('should allow input tags but disable them', () => { const html = defaultSanitizer.sanitize( '' ); const div = document.createElement('div'); let input: HTMLInputElement; div.innerHTML = html; input = div.querySelector('input')!; expect(input.disabled).toBe(true); }); // Test unwanted inline CSS style stripping it('should allow harmless inline CSS', () => { const div = '
'; expect(defaultSanitizer.sanitize(div)).toBe(div); }); it("should strip 'content' properties from inline CSS", () => { const div = '
'; expect(defaultSanitizer.sanitize(div)).toBe( '
' ); }); it("should strip 'counter-increment' properties from inline CSS", () => { const div = '
'; expect(defaultSanitizer.sanitize(div)).toBe('
'); }); it("should strip 'counter-reset' properties from inline CSS", () => { const div = '
'; expect(defaultSanitizer.sanitize(div)).toBe('
'); }); it("should strip 'widows' properties from inline CSS", () => { const div = '
'; expect(defaultSanitizer.sanitize(div)).toBe('
'); }); it("should strip 'orphans' properties from inline CSS", () => { const div = '
'; expect(defaultSanitizer.sanitize(div)).toBe('
'); }); }); });