From 485d559187e67f9c1fcc5984c16fea106f9dd094 Mon Sep 17 00:00:00 2001 From: happydev829 Date: Wed, 10 Jan 2018 13:00:02 +0000 Subject: [PATCH] Less aggressive URL matching Fixes https://github.com/CookPete/react-player/issues/297 --- src/players/DailyMotion.js | 2 +- src/players/Facebook.js | 2 +- src/players/SoundCloud.js | 2 +- src/players/Streamable.js | 2 +- src/players/Twitch.js | 4 ++-- src/players/Vimeo.js | 2 +- src/players/Wistia.js | 4 ++-- src/players/YouTube.js | 2 +- test/specs/canPlay.js | 13 +++++++++++++ 9 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/players/DailyMotion.js b/src/players/DailyMotion.js index dea4eec..0d9d6e0 100644 --- a/src/players/DailyMotion.js +++ b/src/players/DailyMotion.js @@ -5,7 +5,7 @@ import { callPlayer, getSDK, parseStartTime } from '../utils' const SDK_URL = 'https://api.dmcdn.net/all.js' const SDK_GLOBAL = 'DM' const SDK_GLOBAL_READY = 'dmAsyncInit' -const MATCH_URL = /^.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/ +const MATCH_URL = /dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/ export default class DailyMotion extends Component { static displayName = 'DailyMotion' diff --git a/src/players/Facebook.js b/src/players/Facebook.js index 3d094d0..045e0eb 100644 --- a/src/players/Facebook.js +++ b/src/players/Facebook.js @@ -5,7 +5,7 @@ import { callPlayer, getSDK, randomString } from '../utils' const SDK_URL = '//connect.facebook.net/en_US/sdk.js' const SDK_GLOBAL = 'FB' const SDK_GLOBAL_READY = 'fbAsyncInit' -const MATCH_URL = /^https:\/\/www\.facebook\.com\/([^/?].+\/)?video(s|\.php)[/?].*$/ +const MATCH_URL = /facebook\.com\/([^/?].+\/)?video(s|\.php)[/?].*$/ const PLAYER_ID_PREFIX = 'facebook-player-' export default class Facebook extends Component { diff --git a/src/players/SoundCloud.js b/src/players/SoundCloud.js index 93bec41..e50b8a6 100644 --- a/src/players/SoundCloud.js +++ b/src/players/SoundCloud.js @@ -4,7 +4,7 @@ import { callPlayer, getSDK } from '../utils' const SDK_URL = 'https://w.soundcloud.com/player/api.js' const SDK_GLOBAL = 'SC' -const MATCH_URL = /^https?:\/\/(soundcloud.com|snd.sc)\/([a-z0-9-_]+\/[a-z0-9-_]+)$/ +const MATCH_URL = /(soundcloud.com|snd.sc)\/([a-z0-9-_]+\/[a-z0-9-_]+)$/ export default class SoundCloud extends Component { static displayName = 'SoundCloud' diff --git a/src/players/Streamable.js b/src/players/Streamable.js index 9c8c476..dc0fdb9 100644 --- a/src/players/Streamable.js +++ b/src/players/Streamable.js @@ -4,7 +4,7 @@ import { callPlayer, getSDK } from '../utils' const SDK_URL = '//cdn.embed.ly/player-0.0.12.min.js' const SDK_GLOBAL = 'playerjs' -const MATCH_URL = /^https?:\/\/streamable.com\/([a-z0-9]+)$/ +const MATCH_URL = /streamable.com\/([a-z0-9]+)$/ export default class Streamable extends Component { static displayName = 'Streamable' diff --git a/src/players/Twitch.js b/src/players/Twitch.js index 329f301..85aabd9 100644 --- a/src/players/Twitch.js +++ b/src/players/Twitch.js @@ -4,8 +4,8 @@ import { callPlayer, getSDK, randomString } from '../utils' const SDK_URL = '//player.twitch.tv/js/embed/v1.js' const SDK_GLOBAL = 'Twitch' -const MATCH_VIDEO_URL = /^(?:https?:\/\/)?(?:www\.|go\.)?twitch\.tv\/videos\/(\d+)($|\?)/ -const MATCH_CHANNEL_URL = /^(?:https?:\/\/)?(?:www\.|go\.)?twitch\.tv\/([a-z0-9_]+)($|\?)/ +const MATCH_VIDEO_URL = /(?:www\.|go\.)?twitch\.tv\/videos\/(\d+)($|\?)/ +const MATCH_CHANNEL_URL = /(?:www\.|go\.)?twitch\.tv\/([a-z0-9_]+)($|\?)/ const PLAYER_ID_PREFIX = 'twitch-player-' export default class Twitch extends Component { diff --git a/src/players/Vimeo.js b/src/players/Vimeo.js index 4808d43..27e17bf 100644 --- a/src/players/Vimeo.js +++ b/src/players/Vimeo.js @@ -4,7 +4,7 @@ import { callPlayer, getSDK } from '../utils' const SDK_URL = 'https://player.vimeo.com/api/player.js' const SDK_GLOBAL = 'Vimeo' -const MATCH_URL = /https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:(?:channels|ondemand)\/(?:\w+\/)?|groups\/([^/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/ +const MATCH_URL = /(?:www\.|player\.)?vimeo.com\/(?:(?:channels|ondemand)\/(?:\w+\/)?|groups\/([^/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/ export default class Vimeo extends Component { static displayName = 'Vimeo' diff --git a/src/players/Wistia.js b/src/players/Wistia.js index 8ce1c3a..de2c663 100644 --- a/src/players/Wistia.js +++ b/src/players/Wistia.js @@ -4,7 +4,7 @@ import { callPlayer, getSDK } from '../utils' const SDK_URL = '//fast.wistia.com/assets/external/E-v1.js' const SDK_GLOBAL = 'Wistia' -const MATCH_URL = /^https?:\/\/(.+)?(wistia.com|wi.st)\/(medias|embed)\/(.*)$/ +const MATCH_URL = /(?:wistia.com|wi.st)\/(?:medias|embed)\/(.*)$/ export default class Wistia extends Component { static displayName = 'Wistia' @@ -13,7 +13,7 @@ export default class Wistia extends Component { callPlayer = callPlayer getID (url) { - return url && url.match(MATCH_URL)[4] + return url && url.match(MATCH_URL)[1] } load (url) { const { controls, onReady, onPlay, onPause, onSeek, onEnded, config } = this.props diff --git a/src/players/YouTube.js b/src/players/YouTube.js index d8c694c..4273b33 100644 --- a/src/players/YouTube.js +++ b/src/players/YouTube.js @@ -5,7 +5,7 @@ import { callPlayer, getSDK, parseStartTime } from '../utils' const SDK_URL = 'https://www.youtube.com/iframe_api' const SDK_GLOBAL = 'YT' const SDK_GLOBAL_READY = 'onYouTubeIframeAPIReady' -const MATCH_URL = /^(?:https?:\/\/)?(?:www\.|m\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/ +const MATCH_URL = /(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})$/ export default class YouTube extends Component { static displayName = 'YouTube' diff --git a/test/specs/canPlay.js b/test/specs/canPlay.js index 2fe7ef1..b7fec04 100644 --- a/test/specs/canPlay.js +++ b/test/specs/canPlay.js @@ -11,6 +11,10 @@ const { describe, it, expect } = window describe('canPlay', () => { describe('YouTube', () => { it('knows what it can play', () => { + expect(YouTube.canPlay('youtube.com/watch?v=12345678901')).to.be.true + expect(YouTube.canPlay('www.youtube.com/watch?v=12345678901')).to.be.true + expect(YouTube.canPlay('//www.youtube.com/watch?v=12345678901')).to.be.true + expect(YouTube.canPlay('https://www.youtube.com/watch?v=12345678901')).to.be.true expect(YouTube.canPlay('https://www.youtube.com/watch?v=12345678901')).to.be.true expect(YouTube.canPlay('http://www.youtube.com/watch?v=12345678901')).to.be.true expect(YouTube.canPlay('https://youtube.com/watch?v=12345678901')).to.be.true @@ -26,6 +30,8 @@ describe('canPlay', () => { describe('SoundCloud', () => { it('knows what it can play', () => { + expect(SoundCloud.canPlay('soundcloud.com/artist-name/title-name')).to.be.true + expect(SoundCloud.canPlay('//soundcloud.com/artist-name/title-name')).to.be.true expect(SoundCloud.canPlay('http://soundcloud.com/artist-name/title-name')).to.be.true expect(SoundCloud.canPlay('http://soundcloud.com/artist_name/title_name')).to.be.true expect(SoundCloud.canPlay('http://snd.sc/artist-name/title-name')).to.be.true @@ -40,6 +46,8 @@ describe('canPlay', () => { describe('Vimeo', () => { it('knows what it can play', () => { + expect(Vimeo.canPlay('vimeo.com/1234')).to.be.true + expect(Vimeo.canPlay('//vimeo.com/1234')).to.be.true expect(Vimeo.canPlay('http://vimeo.com/1234')).to.be.true expect(Vimeo.canPlay('https://vimeo.com/1234')).to.be.true expect(Vimeo.canPlay('https://www.vimeo.com/1234')).to.be.true @@ -58,6 +66,8 @@ describe('canPlay', () => { describe('Wistia', () => { it('knows what it can play', () => { + expect(Wistia.canPlay('fast.wistia.com/medias/e4a27b971d')).to.be.true + expect(Wistia.canPlay('//fast.wistia.com/medias/e4a27b971d')).to.be.true expect(Wistia.canPlay('https://fast.wistia.com/medias/e4a27b971d')).to.be.true expect(Wistia.canPlay('http://fast.wistia.com/medias/e4a27b971d')).to.be.true expect(Wistia.canPlay('https://fast.wi.st/medias/e4a27b971d')).to.be.true @@ -77,6 +87,9 @@ describe('canPlay', () => { describe('Twitch', () => { it('knows what it can play', () => { + expect(Twitch.canPlay('twitch.tv/videos/106400740')).to.be.true + expect(Twitch.canPlay('www.twitch.tv/videos/106400740')).to.be.true + expect(Twitch.canPlay('//www.twitch.tv/videos/106400740')).to.be.true expect(Twitch.canPlay('https://www.twitch.tv/videos/106400740')).to.be.true expect(Twitch.canPlay('https://www.twitch.tv/kronovi')).to.be.true expect(Twitch.canPlay('https://twitch.tv/videos/106400740')).to.be.true