Files
talk/src/core/common/utils/purify.spec.ts
T
Wyatt JohnsonandKiwi 6f538d3235 [next] DOM Purification (#2115)
* feat: added purify + linkify + better link detection

* fix: linting + tests

* chore: refactor RequireProperty Type
2018-12-13 00:14:29 +01:00

47 lines
1.1 KiB
TypeScript

import { JSDOM } from "jsdom";
import { createPurify, sanitizeCommentBody } from "./purify";
const window = new JSDOM("", {}).window;
const DOMPurify = createPurify(window);
it("sanitizes out tags not allowed", () => {
expect(
sanitizeCommentBody(
DOMPurify,
"<script type=\"text/javascript\">alert('ok');</script>"
)
).toMatchSnapshot();
expect(
sanitizeCommentBody(DOMPurify, "<script src=malicious-code.js></script>")
).toMatchSnapshot();
});
it("sanitizes out attributes not allowed", () => {
expect(
sanitizeCommentBody(DOMPurify, '<div id="test">Test</div>')
).toMatchSnapshot();
});
it("allows anchor links", () => {
expect(
sanitizeCommentBody(DOMPurify, '<a href="test">This is a link</a>')
).toMatchSnapshot();
});
it("allows anchor tags and counts them correctly", () => {
const { body, linkCount } = sanitizeCommentBody(
DOMPurify,
`
<div>
<a href="https://mozilla.org/">Mozilla</a>
<a href="https://mozilla.org/">Mozilla</a>
</div>
`
);
expect(body).toMatchSnapshot();
expect(linkCount).toEqual(2);
});