From 40b8bab7aba68a723696dc8cea2b40afd9179482 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 29 Sep 2022 11:23:49 -0500 Subject: [PATCH] Chats: strip HTML when copying message text to clipboard --- .../features/chats/components/chat-message-list.tsx | 4 +++- app/soapbox/utils/html.ts | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/soapbox/features/chats/components/chat-message-list.tsx b/app/soapbox/features/chats/components/chat-message-list.tsx index 31810dfb9..1d3264094 100644 --- a/app/soapbox/features/chats/components/chat-message-list.tsx +++ b/app/soapbox/features/chats/components/chat-message-list.tsx @@ -16,6 +16,7 @@ import { MediaGallery } from 'soapbox/features/ui/util/async-components'; import { useAppSelector, useAppDispatch, useOwnAccount } from 'soapbox/hooks'; import { chatKeys, IChat, IChatMessage, useChatActions, useChatMessages } from 'soapbox/queries/chats'; import { queryClient } from 'soapbox/queries/client'; +import { stripHTML } from 'soapbox/utils/html'; import { onlyEmoji } from 'soapbox/utils/rich_content'; import ChatMessageListIntro from './chat-message-list-intro'; @@ -219,7 +220,8 @@ const ChatMessageList: React.FC = ({ chat, autosize }) => { const handleCopyText = (chatMessage: IChatMessage) => { if (navigator.clipboard) { - navigator.clipboard.writeText(chatMessage.content); + const text = stripHTML(chatMessage.content); + navigator.clipboard.writeText(text); } }; diff --git a/app/soapbox/utils/html.ts b/app/soapbox/utils/html.ts index 0b19ef819..32a47caed 100644 --- a/app/soapbox/utils/html.ts +++ b/app/soapbox/utils/html.ts @@ -27,3 +27,11 @@ export const stripCompatibilityFeatures = (html: string): string => { return node.innerHTML; }; + +/** Convert HTML to plaintext. */ +// https://stackoverflow.com/a/822486 +export const stripHTML = (html: string) => { + const div = document.createElement('div'); + div.innerHTML = html; + return div.textContent || div.innerText || ''; +};