]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
1. Assimilated youtube regex's
authorBobby Richter <secretrobotron@gmail.com>
Sat, 7 May 2011 02:54:26 +0000 (22:54 -0400)
committerBobby Richter <secretrobotron@gmail.com>
Sat, 7 May 2011 02:54:26 +0000 (22:54 -0400)
2. Added youtube regex tests

players/youtube/popcorn.youtube.js
players/youtube/popcorn.youtube.unit.html
players/youtube/popcorn.youtube.unit.js

index c9b7085be304bedd5afd42d52ff7266c257ff296..fff547320e594e5cd158a4508876f2952fc3b889 100644 (file)
@@ -45,8 +45,7 @@ var onYouTubePlayerReady;
       YOUTUBE_STATE_BUFFERING = 3,
       YOUTUBE_STATE_CUED = 5;
 
-  var uriRegex = /^[^v]+v.(.{11}).*/,
-      urlRegex = /^[^d]+d.(.{11}).*/;
+  var urlRegex = /(\/e\/|be\/|\/embed\/|\/watch\?v=)([a-zA-Z0-9_]{11})/;
   
   // Collection of all Youtube players
   var registry = {},
@@ -65,7 +64,7 @@ var onYouTubePlayerReady;
     var matches = urlRegex.exec( url ); 
 
     // Return id, which comes after first equals sign
-    return matches ? matches[1] : "";
+    return matches ? matches[2] : "";
   }
   
   // Extract the id from a player url
@@ -74,10 +73,10 @@ var onYouTubePlayerReady;
       return;
     }
     
-    var matches = uriRegex.exec( url ); 
+    var matches = urlRegex.exec( url ); 
     
     // Return id, which comes after first equals sign
-    return matches ? matches[1] : "";
+    return matches ? matches[2] : "";
   }
   
   function getPlayerAddress( vidId, playerId ) {
@@ -196,7 +195,7 @@ var onYouTubePlayerReady;
     this.duration = 0;
     
     this.vidId = extractIdFromUrl( url ) || extractIdFromUri( url );
-    
+
     if ( !this.vidId ) {
       throw "Could not find video id";
     }
index ce0a83c299823937ea5d7566b1ebb56b5f81e722..405821f089613dd1396e25cc8a09f6300f4cf784 100644 (file)
@@ -30,6 +30,7 @@
   </p>
   <div id="video" style="width: 360px; height: 300px" ></div>
   <div id="video2" style="width: 360px; height: 300px" ></div>
+  <div id="video3" style="width: 360px; height: 300px" ></div>
 </body>
 </html>
 
index 12edb7d72523c4f3813a1a71623dd4e35cd2723e..5db19be5a86893e3b00e8260ac3bf6a9faeb3885 100644 (file)
@@ -185,3 +185,42 @@ test( "Popcorn YouTube Plugin Url and Duration Tests", function() {
   
   popcorn.play();
 });
+
+test( "Popcorn YouTube Plugin Url Regex Test", function() {
+
+  QUnit.reset();
+
+  var urlTests = [
+    { name: 'standard',
+      url: 'http://www.youtube.com/watch?v=9oar9glUCL0',
+    },
+    { name: 'share url',
+      url: 'http://youtu.be/9oar9glUCL0',
+    },
+    { name: 'long embed',
+      url: 'http://www.youtube.com/embed/9oar9glUCL0',
+    },
+    { name: 'short embed',
+      url: 'http://www.youtube.com/e/9oar9glUCL0',
+    },
+  ];
+
+  expect( urlTests.length );
+  stop( 10000 );
+
+  for ( var t in urlTests ) {
+
+    var urlTest = urlTests[t],
+        popcorn = Popcorn( Popcorn.youtube( 'video3', urlTest.url ) );
+
+    equals( popcorn.video.vidId, '9oar9glUCL0', 'Video id is correct for ' + urlTest.name );
+    popcorn.pause();
+
+    // Get rid of the youtube object inside the video3, to keep things simple
+    var div = document.getElementById('video3');
+    div.removeChild(div.firstChild);
+  }
+  
+  start(); 
+});
+