remove 'mailto:' prefix from links (#2914)

Co-authored-by: Kim Gardner <kgardnr@gmail.com>
This commit is contained in:
Tessa Thornton
2020-03-27 14:09:42 -04:00
committed by GitHub
parent 4645ed4cd7
commit 5db096942a
3 changed files with 15 additions and 4 deletions
@@ -2,7 +2,7 @@
exports[`allows anchor links 1`] = `
Object {
"body": "<a href=\\"test\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">test</a>",
"body": "<a href=\\"http://test.com\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">http://test.com</a>",
"linkCount": 1,
}
`;
+4 -1
View File
@@ -26,7 +26,10 @@ it("sanitizes out attributes not allowed", () => {
it("allows anchor links", () => {
expect(
sanitizeCommentBody(DOMPurify, '<a href="test">This is a link</a>')
sanitizeCommentBody(
DOMPurify,
'<a href="http://test.com">This is a link</a>'
)
).toMatchSnapshot();
});
+10 -2
View File
@@ -1,6 +1,7 @@
import createDOMPurify from "dompurify";
type DOMPurify = ReturnType<typeof createDOMPurify>;
const MAILTO_PROTOCOL = "mailto:";
export function createPurify(window: Window, returnDOM = true) {
// Initializing JSDOM and DOMPurify
@@ -26,8 +27,15 @@ export function createPurify(window: Window, returnDOM = true) {
node.setAttribute("rel", "noopener noreferrer");
// Ensure that all the links have the same link as they do text.
const href = node.getAttribute("href");
if (node.textContent !== href) {
let href = node.getAttribute("href");
if (href) {
if (node.textContent !== href) {
// remove "mailto:" prefix from link text
const url = new URL(href);
if (url.protocol === MAILTO_PROTOCOL) {
href = href.replace(url.protocol, "");
}
}
node.textContent = href;
}
} else {