diff --git a/src/pages/VideoLinkAddModal.test.ts b/src/pages/VideoLinkAddModal.test.ts new file mode 100644 index 000000000..d245a9dd9 --- /dev/null +++ b/src/pages/VideoLinkAddModal.test.ts @@ -0,0 +1,19 @@ +import { ALLOWED_VIDEO_SOURCES_REGEX } from '@/pages/VideoLinkAddModal'; + +describe('ALLOWED_VIDEO_SOURCES_REGEX', () => { + const tests = { + 'https://www.youtube.com/watch?v=12345678901': true, + 'http://youtu.be/12345678901?si=6N2ks_X0YOE_0Pmg': true, + 'http://youtudbe/12345678901?si=6N2ks_X0YOE_0Pmg': false, + 'https://www.youtube.com/shorts/ViOS7SeT0HE': true, + 'https://vimeo.com/789006133': true, + 'http://www.vimeo.com/789006133': true, + 'http://miveo.com/789006133': false, + }; + + test.each(Object.entries(tests))( + 'can test whether video URL %p being valid is %p', + (url, expected) => + expect(ALLOWED_VIDEO_SOURCES_REGEX.test(url)).toBe(expected), + ); +}); diff --git a/src/pages/VideoLinkAddModal.tsx b/src/pages/VideoLinkAddModal.tsx index 46ccd8114..0ccf86dcb 100644 --- a/src/pages/VideoLinkAddModal.tsx +++ b/src/pages/VideoLinkAddModal.tsx @@ -16,8 +16,14 @@ type Props = { onClose: () => void; }; -const ALLOWED_VIDEO_SOURCES_REGEX: RegExp = - /^http(s?):\/\/(www\.)?((youtube\.com\/watch\?v=([^/#&?]*))|(vimeo\.com\/([^/#&?]*))|(youtu\.be\/([^/#&?]*)))/; +const ALLOWED_VIDEO_SOURCES_REGEX = new RegExp( + '^(https?://(www\\.)?' + // Protocol + '(youtube.com/watch\\?v=|' + // YouTube + 'youtu\\.be/|' + // Shortened YouTube + 'vimeo\\.com/|' + // Vimeo + 'youtube\\.com/shorts/)' + // YouTube Shorts + '[^/#&?]+)', // Video ID +); type FormData = { link: string; @@ -87,4 +93,4 @@ const VideoLinkAddModal = ({ visible, onConfirm, onClose }: Props) => { ); }; -export { VideoLinkAddModal }; +export { ALLOWED_VIDEO_SOURCES_REGEX, VideoLinkAddModal };