From 02744411f3465742cc6ffb5bc6de364930f47f9c Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 1 Jul 2023 15:45:22 -0400 Subject: [PATCH] fix: Do not allow copy/pasting comment marks between documents (#5507) --- shared/editor/marks/Comment.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/shared/editor/marks/Comment.ts b/shared/editor/marks/Comment.ts index 290739667..06357c695 100644 --- a/shared/editor/marks/Comment.ts +++ b/shared/editor/marks/Comment.ts @@ -19,10 +19,31 @@ export default class Comment extends Mark { userId: {}, }, inclusive: false, - parseDOM: [{ tag: "span.comment-marker" }], + parseDOM: [ + { + tag: "span.comment-marker", + getAttrs: (dom: HTMLSpanElement) => { + // Ignore comment markers from other documents + const documentId = dom.getAttribute("data-document-id"); + if (documentId && documentId !== this.editor.props.id) { + return false; + } + + return { + id: dom.getAttribute("id")?.replace("comment-", ""), + userId: dom.getAttribute("data-user-id"), + }; + }, + }, + ], toDOM: (node) => [ "span", - { class: "comment-marker", id: `comment-${node.attrs.id}` }, + { + class: "comment-marker", + id: `comment-${node.attrs.id}`, + "data-user-id": node.attrs.userId, + "data-document-id": this.editor.props.id, + }, ], }; } @@ -87,7 +108,7 @@ export default class Comment extends Mark { new Plugin({ props: { handleDOMEvents: { - mouseup: (view, event: MouseEvent) => { + mouseup: (_view, event: MouseEvent) => { if ( !(event.target instanceof HTMLSpanElement) || !event.target.classList.contains("comment-marker")