diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 37bf4a9d4674..ff8fce31a986 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -6697,7 +6697,7 @@ function replaceLocalAttachmentReferences(draftMarkdown: string, currentCommentH return draftMarkdown.replace(localAttachmentReferenceRegex, '').trim(); } - const attachmentTags = currentCommentHtml.match(/<(?:img|video)\b[^>]*>|]*data-expensify-source="[^"]*"[^>]*>[\s\S]*?<\/a>/gi) ?? []; + const attachmentTags = currentCommentHtml.match(/]*>|]*>[\s\S]*?<\/video>|]*data-expensify-source="[^"]*"[^>]*>[\s\S]*?<\/a>/gi) ?? []; const syncedAttachmentTag = attachmentTags.find((tag) => { const source = tag.match(CONST.REGEX.ATTACHMENT.ATTACHMENT_SOURCE)?.at(2) ?? ''; return source.match(CONST.REGEX.ATTACHMENT.ATTACHMENT_SOURCE_ID)?.at(1) === reportActionID; @@ -6718,13 +6718,23 @@ function replaceLocalAttachmentReferences(draftMarkdown: string, currentCommentH }); } +/** Empty parts are dropped so deleting all the text does not leave a separator above the attachment. */ +function buildEditedCommentWithAttachment(commentHtml: string, uploadingAttachmentHtml: string | undefined): string { + if (!uploadingAttachmentHtml) { + return commentHtml; + } + return [commentHtml, uploadingAttachmentHtml].filter(Boolean).join('

'); +} + /** The still-uploading attachment tag, re-appended to an edit's optimistic message so it stays on screen. */ function getUploadingAttachmentHtmlFromComment(currentCommentHtml: string | undefined): string | undefined { if (!currentCommentHtml?.includes(CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE)) { return undefined; } const attachmentTagRegex = new RegExp( - `<(?:img|video)\\b[^>]*${CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE}="[^"]*"[^>]*>|]*${CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE}="[^"]*"[^>]*>[\\s\\S]*?`, + `]*${CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE}="[^"]*"[^>]*>` + + `|]*${CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE}="[^"]*"[^>]*>[\\s\\S]*?` + + `|]*${CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE}="[^"]*"[^>]*>[\\s\\S]*?`, 'i', ); return currentCommentHtml.match(attachmentTagRegex)?.at(0); @@ -14474,6 +14484,7 @@ export { replaceLocalAttachmentReferences, isUploadingAttachmentRemovedFromDraft, getUploadingAttachmentHtmlFromComment, + buildEditedCommentWithAttachment, }; export type { diff --git a/src/libs/actions/Report/index.ts b/src/libs/actions/Report/index.ts index ca9180724a36..900b06038e79 100644 --- a/src/libs/actions/Report/index.ts +++ b/src/libs/actions/Report/index.ts @@ -106,6 +106,7 @@ import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import {updateTitleFieldToMatchPolicy} from '@libs/ReportTitleUtils'; import type {Ancestor, OptimisticAddCommentReportAction, OptimisticChatReport, SelfDMParameters} from '@libs/ReportUtils'; import { + buildEditedCommentWithAttachment, buildOptimisticAddCommentReportAction, buildOptimisticChangeFieldAction, buildOptimisticChangePolicyReportAction, @@ -3565,7 +3566,7 @@ function editReportComment( // Optimistic message only: the sent copy is stripped, so without this the attachment vanishes until upload lands. const uploadingAttachmentHtml = shouldRemoveQueuedAttachment ? undefined : getUploadingAttachmentHtmlFromComment(originalCommentHTML); - const optimisticHtml = uploadingAttachmentHtml ? `${htmlForNewComment}

${uploadingAttachmentHtml}` : htmlForNewComment; + const optimisticHtml = buildEditedCommentWithAttachment(htmlForNewComment, uploadingAttachmentHtml); const optimisticText = uploadingAttachmentHtml ? Parser.htmlToText(optimisticHtml) : reportComment; const optimisticReportActions: PartialDeep = { diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 931b0b781a82..20e939ac7115 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -22,6 +22,7 @@ import createDynamicRoute from '@libs/Navigation/helpers/dynamicRoutesUtils/crea import getReportURLForCurrentContext from '@libs/Navigation/helpers/getReportURLForCurrentContext'; import isSearchTopmostFullScreenRoute from '@libs/Navigation/helpers/isSearchTopmostFullScreenRoute'; import Navigation from '@libs/Navigation/Navigation'; +import Parser from '@libs/Parser'; import * as PolicyUtils from '@libs/PolicyUtils'; import {getOriginalMessage, getReportAction, isActionOfType, isWhisperAction} from '@libs/ReportActionsUtils'; // Testing only so it's okay to import computeReportName @@ -30,6 +31,7 @@ import {buildReportNameFromParticipantNames, computeReportName as computeReportN import type {OptionData} from '@libs/ReportUtils'; import { areAllRequestsBeingSmartScanned, + buildEditedCommentWithAttachment, buildOptimisticAnnounceChat, buildOptimisticApprovedReportAction, buildOptimisticCancelPaymentReportAction, @@ -6470,6 +6472,42 @@ describe('ReportUtils', () => { expect(getUploadingAttachmentHtmlFromComment(syncedImageHtml)).toBeUndefined(); }); + describe('buildEditedCommentWithAttachment', () => { + const attachmentTag = ''; + + it('separates the attachment from the text that was kept', () => { + expect(buildEditedCommentWithAttachment('Hello edited', attachmentTag)).toBe(`Hello edited

${attachmentTag}`); + }); + + it('leaves no separator when the edit removed all of the text', () => { + expect(buildEditedCommentWithAttachment('', attachmentTag)).toBe(attachmentTag); + }); + + it('returns the comment untouched when nothing is uploading', () => { + expect(buildEditedCommentWithAttachment('Hello edited', undefined)).toBe('Hello edited'); + }); + }); + + describe('video attachments', () => { + const videoSource = 'blob:https://dev.new.expensify.com:8082/uuid-video'; + const uploadingVideoHtml = `Hello

`; + + it('re-appends the whole video element, not just its opening tag', () => { + const tag = getUploadingAttachmentHtmlFromComment(uploadingVideoHtml); + + expect(tag).toContain(''); + expect(tag).toContain('>clip.mp4<'); + }); + + it('survives a second edit, because the stored element still parses back to a reference', () => { + const storedAfterFirstEdit = `Hello edited

${getUploadingAttachmentHtmlFromComment(uploadingVideoHtml)}`; + const secondEditDraft = Parser.htmlToMarkdown(storedAfterFirstEdit).trim(); + + expect(secondEditDraft).toContain(videoSource); + expect(isUploadingAttachmentRemovedFromDraft(secondEditDraft, storedAfterFirstEdit)).toBe(false); + }); + }); + it('does not swap in an attachment owned by a different report action', () => { const otherActionHtml = 'Hello

';