diff --git a/src/YouTube.js b/src/YouTube.js index 152e94d9..e42ea170 100644 --- a/src/YouTube.js +++ b/src/YouTube.js @@ -4,8 +4,8 @@ export const shouldTransform = url => { const { host, pathname, searchParams } = new URL(url); return ( - host.endsWith('youtu.be') || - (host.endsWith('youtube.com') && + host === 'youtu.be' || + (host === 'youtube.com' && pathname.includes('/watch') && Boolean(searchParams.get('v'))) ); diff --git a/src/__tests__/YouTube.js b/src/__tests__/YouTube.js index 3e04185d..f1076382 100644 --- a/src/__tests__/YouTube.js +++ b/src/__tests__/YouTube.js @@ -17,6 +17,18 @@ cases( url: 'https://not-a-youtube-url.com', valid: false, }, + "non-YouTube url ending with 'youtu.be'": { + url: 'https://this-is-not-youtu.be', + valid: false, + }, + "non-YouTube url ending with 'youtube.com'": { + url: 'https://this-is-not-youtube.com', + valid: false, + }, + "non-YouTube url ending with 'youtube.com' and having a valid video path": { + url: 'https://this-is-not-youtube.com/watch?v=dQw4w9WgXcQ', + valid: false, + }, 'short url': { url: 'https://youtu.be/dQw4w9WgXcQ', valid: true,