mirror of
https://github.com/wassname/talk.git
synced 2026-06-27 19:33:06 +08:00
remove 'mailto:' prefix from links (#2914)
Co-authored-by: Kim Gardner <kgardnr@gmail.com>
This commit is contained in:
@@ -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,
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user