diff --git a/webview-ui/src/components/history/CopyButton.tsx b/webview-ui/src/components/history/CopyButton.tsx index ca42b75536..1ed422df22 100644 --- a/webview-ui/src/components/history/CopyButton.tsx +++ b/webview-ui/src/components/history/CopyButton.tsx @@ -10,6 +10,19 @@ type CopyButtonProps = { className?: string } +/** + * Strips only history highlight spans from text while preserving other HTML + * Targets: content + * @param text - Text that may contain highlight spans + * @returns Text with highlight spans removed but content preserved + */ +const stripHistoryHighlightSpans = (text: string): string => { + // Match opening tag, capture content until closing tag + // The [\s\S]*? pattern matches any character (including newlines) non-greedily, + // which properly handles content with < characters + return text.replace(/([\s\S]*?)<\/span>/g, "$1") +} + export const CopyButton = ({ itemTask, className }: CopyButtonProps) => { const { isCopied, copy } = useClipboard() const { t } = useAppTranslation() @@ -17,12 +30,10 @@ export const CopyButton = ({ itemTask, className }: CopyButtonProps) => { const onCopy = useCallback( (e: React.MouseEvent) => { e.stopPropagation() - const tempDiv = document.createElement("div") - tempDiv.innerHTML = itemTask - const text = tempDiv.textContent || tempDiv.innerText || "" - if (!isCopied) { - copy(text) + // Strip only history highlight spans before copying to clipboard + const cleanText = stripHistoryHighlightSpans(itemTask) + copy(cleanText) } }, [isCopied, copy, itemTask],