Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function extractAttachments(
const attachments: Attachment[] = [];
const canUserPerformAction = canUserPerformWriteAction(report, isReportArchived);
let currentLink = '';
// A video without data-name waits for its inner text, which server-rendered <video> tags use for the original file name
let videoAwaitingFileName: Attachment | undefined;

const htmlParser = new HtmlParser({
onopentag: (name, attribs) => {
Expand All @@ -51,17 +53,19 @@ function extractAttachments(
if (name === 'video') {
const source = tryResolveUrlFromApiRoot(attribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE]);

const fileName = attribs[CONST.ATTACHMENT_ORIGINAL_FILENAME_ATTRIBUTE] || getFileName(`${source}`);
attachments.unshift({
const originalFileName = attribs[CONST.ATTACHMENT_ORIGINAL_FILENAME_ATTRIBUTE];
const videoAttachment: Attachment = {
reportActionID: attribs['data-id'],
attachmentID: attribs[CONST.ATTACHMENT_ID_ATTRIBUTE],
source: tryResolveUrlFromApiRoot(attribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE]),
isAuthTokenRequired: !!attribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE],
file: {name: fileName},
file: {name: originalFileName || getFileName(`${source}`)},
duration: Number(attribs[CONST.ATTACHMENT_DURATION_ATTRIBUTE]),
isReceipt: false,
hasBeenFlagged: attribs['data-flagged'] === 'true',
});
};
attachments.unshift(videoAttachment);
videoAwaitingFileName = originalFileName ? undefined : videoAttachment;
return;
}

Expand Down Expand Up @@ -98,7 +102,22 @@ function extractAttachments(
});
}
},
ontext: (text) => {
const innerText = text.trim();
if (!videoAwaitingFileName || !innerText) {
return;
}

const videoIndex = attachments.indexOf(videoAwaitingFileName);
attachments[videoIndex] = {...videoAwaitingFileName, file: {...videoAwaitingFileName.file, name: innerText}};
videoAwaitingFileName = undefined;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep collecting video text until the closing tag

When a legacy video filename contains an encoded character, such as <video ...>clip&amp;demo.mp4</video>, htmlparser2 may invoke ontext multiple times for that single text node; its documentation explicitly says consumers may need to stitch the pieces together (htmlparser2 usage). Clearing videoAwaitingFileName after the first nonempty callback therefore records only clip, losing both the rest of the name and its extension. Accumulate the callbacks and assign the filename when </video> is reached instead.

Useful? React with 👍 / 👎.

},
onclosetag: (name) => {
if (name === 'video') {
videoAwaitingFileName = undefined;
return;
}

if (name !== 'a' || !currentLink) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ function VideoRenderer({tnode, key}: VideoRendererProps) {
const sourceURL = tryResolveUrlFromApiRoot(attrHref);
// Prefer the stored file name, like ImageRenderer does: deriving it from the URL yields the raw storage
// key, which has no extension and so downloads as a file the OS treats as a generic document.
const fileName = htmlAttribs[CONST.ATTACHMENT_ORIGINAL_FILENAME_ATTRIBUTE] || getFileName(`${sourceURL}`);
// Server-rendered <video> tags may omit data-name and carry the original file name only as their inner text.
const tNodeChild = tnode?.domNode?.children?.at(0);
const innerText = tNodeChild && 'data' in tNodeChild && typeof tNodeChild.data === 'string' ? tNodeChild.data.trim() : '';
const fileName = htmlAttribs[CONST.ATTACHMENT_ORIGINAL_FILENAME_ATTRIBUTE] || innerText || getFileName(`${sourceURL}`);
const thumbnailUrl = tryResolveUrlFromApiRoot(htmlAttribs[CONST.ATTACHMENT_THUMBNAIL_URL_ATTRIBUTE]);
const width = Number(htmlAttribs[CONST.ATTACHMENT_THUMBNAIL_WIDTH_ATTRIBUTE]);
const height = Number(htmlAttribs[CONST.ATTACHMENT_THUMBNAIL_HEIGHT_ATTRIBUTE]);
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/VideoRendererTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,40 @@ describe('VideoRenderer', () => {
const navigateArgs = firstCall?.at(0);
expect(navigateArgs).toContain('isAuthTokenRequired=true');
});

it('should use the inner text as the file name when data-name is missing', () => {
// Given a server-rendered video tag that carries the original file name only as its inner text
render(
<ShowContextMenuStateContext.Provider value={mockShowContextMenuStateValue}>
<ShowContextMenuActionsContext.Provider value={mockShowContextMenuActionsValue}>
<AttachmentContext.Provider value={{type: CONST.ATTACHMENT_TYPE.SEARCH}}>
{/* @ts-expect-error - Ignoring type errors for testing purposes */}
<VideoRenderer tnode={{attributes: mockTNodeAttributes, domNode: {children: [{data: 'clip.mp4'}]}}} />
</AttachmentContext.Provider>
</ShowContextMenuActionsContext.Provider>
</ShowContextMenuStateContext.Provider>,
);

// When the video preview renders
// Then it should receive the original file name instead of the storage key from the URL
expect(screen.getByLabelText('clip.mp4')).toBeTruthy();
});

it('should fall back to the URL file name when data-name and inner text are missing', () => {
// Given a video tag with neither data-name nor inner text
render(
<ShowContextMenuStateContext.Provider value={mockShowContextMenuStateValue}>
<ShowContextMenuActionsContext.Provider value={mockShowContextMenuActionsValue}>
<AttachmentContext.Provider value={{type: CONST.ATTACHMENT_TYPE.SEARCH}}>
{/* @ts-expect-error - Ignoring type errors for testing purposes */}
<VideoRenderer tnode={{attributes: mockTNodeAttributes}} />
</AttachmentContext.Provider>
</ShowContextMenuActionsContext.Provider>
</ShowContextMenuStateContext.Provider>,
);

// When the video preview renders
// Then it should use the last part of the source URL, since there is no other name to use
expect(screen.getByLabelText('test.mp4')).toBeTruthy();
});
});
61 changes: 61 additions & 0 deletions tests/unit/extractAttachmentsTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import extractAttachments from '@components/Attachments/AttachmentCarousel/extractAttachments';

import CONST from '@src/CONST';

const ACCOUNT_ID = 1;
const VIDEO_SOURCE = 'https://www.expensify.com/chat-attachments/123/w_abc123';

function extractNoteAttachments(note: string) {
return extractAttachments(CONST.ATTACHMENT_TYPE.NOTE, {
privateNotes: {[ACCOUNT_ID]: {note}},
accountID: ACCOUNT_ID,
report: undefined,
isReportArchived: false,
});
}

describe('extractAttachments', () => {
it('uses data-name as the video file name when it is present', () => {
// Given a video tag with a data-name attribute and a different inner text
const note = `<video data-expensify-source="${VIDEO_SOURCE}" data-name="original.mp4">other.mp4</video>`;

// When the attachments are extracted
const attachments = extractNoteAttachments(note);

// Then the stored data-name is preferred
expect(attachments.at(0)?.file?.name).toBe('original.mp4');
});

it('uses the inner text as the video file name when data-name is missing', () => {
// Given a server-rendered video tag that carries the original file name only as its inner text
const note = `<video data-expensify-source="${VIDEO_SOURCE}">clip.mp4</video>`;

// When the attachments are extracted
const attachments = extractNoteAttachments(note);

// Then the original file name is used instead of the storage key from the URL
expect(attachments.at(0)?.file?.name).toBe('clip.mp4');
});

it('falls back to the URL file name when data-name and inner text are missing', () => {
// Given a video tag with neither data-name nor inner text
const note = `<video data-expensify-source="${VIDEO_SOURCE}"></video>`;

// When the attachments are extracted
const attachments = extractNoteAttachments(note);

// Then the last part of the source URL is used, since there is no other name to use
expect(attachments.at(0)?.file?.name).toBe('w_abc123');
});

it('does not use text that follows a video as its file name', () => {
// Given an empty video tag followed by regular comment text
const note = `<video data-expensify-source="${VIDEO_SOURCE}"></video>Some comment`;

// When the attachments are extracted
const attachments = extractNoteAttachments(note);

// Then the text outside the video tag is ignored
expect(attachments.at(0)?.file?.name).toBe('w_abc123');
});
});
Loading