Commit 5d03cad7 by Anton Stupak

Merge pull request #3883 from edx/anton/fix-youtube-regexp

Fix youtube regular expression in video player editor
parents 926ce567 db213bf3
......@@ -5,6 +5,8 @@ These are notable changes in edx-platform. This is a rolling list of changes,
in roughly chronological order, most recent first. Add your entries at or near
the top. Include a label indicating the component affected.
Blades: Fix Youtube regular expression in video player editor. BLD-967.
Blades: Fix displaying transcripts on touch devices. BLD-1033.
Blades: Tolerance expressed in percentage now computes correctly. BLD-522.
......
define(
[
"jquery", "underscore",
"js/views/video/transcripts/utils",
"underscore.string", "xmodule", "jasmine-jquery"
'jquery', 'underscore',
'js/views/video/transcripts/utils',
'underscore.string', 'xmodule', 'jasmine-jquery'
],
function ($, _, Utils, _str) {
describe('Transcripts.Utils', function () {
'use strict';
describe('Transcripts.Utils', function () {
var videoId = 'OEoXaMPEzfM',
ytLinksList = (function (id) {
var links = [
......@@ -33,6 +34,7 @@ function ($, _, Utils, _str) {
'http://somelink.com/%s.%s',
'ftp://somelink.com/%s.%s',
'https://somelink.com/%s.%s',
'http://cdn.somecdn.net/v/%s.%s',
'somelink.com/%s.%s',
'%s.%s'
],
......@@ -214,7 +216,7 @@ function ($, _, Utils, _str) {
describe('Method: parseLink', function () {
var resultDataDict = {
'html5': {
link: html5LinksList['mp4'][0],
link: html5LinksList.mp4[0],
resp: {
mode: 'html5',
video: html5FileName,
......@@ -260,5 +262,5 @@ function ($, _, Utils, _str) {
});
});
});
});
});
});
define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
var Utils = (function () {
define(['jquery', 'underscore', 'jquery.ajaxQueue'], function($) {
'use strict';
return (function () {
var Storage = {};
/**
* @function
*
* Adds some data to the Storage object. If data with existent `data_id`
* is added, nothing happens.
*
* @param {string} data_id Unique identifier for the data.
* @param {any} data Data that should be stored.
*
* @returns {object} Object itself for chaining.
* @function
* @param {String} data_id Unique identifier for the data.
* @param {Any} data Data that should be stored.
* @return {Object} Object itself for chaining.
*/
Storage.set = function (data_id, data) {
Storage[data_id] = data;
......@@ -20,13 +18,10 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
};
/**
* @function
*
* Return data from the Storage object by identifier.
*
* @param {string} data_id Unique identifier of the data.
*
* @returns {any} Stored data.
* @function
* @param {String} data_id Unique identifier of the data.
* @return {Any} Stored data.
*/
Storage.get= function (data_id) {
......@@ -35,13 +30,10 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
/**
* @function
*
* Deletes data from the Storage object by identifier.
*
* @param {string} data_id Unique identifier of the data.
*
* @returns {boolean} Boolean value that indicate if data is removed.
* @function
* @param {String} data_id Unique identifier of the data.
* @return {Boolean} Boolean value that indicate if data is removed.
*/
Storage.remove = function (data_id) {
......@@ -49,17 +41,14 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
};
/**
* @function
*
* Returns model from collection by 'field_name' property.
*
* @param {object} collection The model (CMS.Models.Metadata) containing
* information about metadata setting editors.
* @param {string} field_name Name of field that should be found.
*
* @returns {
* object: when model exist,
* undefined: when model doesn't exist.
* @function
* @param {Object} collection The model (CMS.Models.Metadata) information
* about metadata setting editors.
* @param {String} field_name Name of field that should be found.
* @return {
* Object: When model exist.
* Undefined: When model doesn't exist.
* }
*/
var _getField = function (collection, field_name) {
......@@ -75,10 +64,8 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
};
/**
* @function
*
* Parses Youtube link and return video id.
*
* @function
* These are the types of URLs supported:
* http://www.youtube.com/watch?v=OEoXaMPEzfM&feature=feedrec_grec_index
* http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/OEoXaMPEzfM
......@@ -87,17 +74,16 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
* http://www.youtube.com/embed/OEoXaMPEzfM?rel=0
* http://www.youtube.com/watch?v=OEoXaMPEzfM
* http://youtu.be/OEoXaMPEzfM
*
* @param {string} url Url that should be parsed.
*
* @returns {
* string: Video Id,
* undefined: when url has incorrect format or argument is
* @param {String} url Url that should be parsed.
* @return {
* String: Video Id.
* Undefined: When url has incorrect format or argument is
* non-string, video id's length is not equal 11.
* }
*/
var _youtubeParser = (function () {
var cache = {};
var cache = {},
regExp = /(?:http|https|)(?:\:\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*/i;
return function (url) {
if (typeof url !== 'string') {
......@@ -109,22 +95,20 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
return cache[url];
}
var regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;
var match = url.match(regExp);
cache[url] = (match && match[1].length === 11) ? match[1] : void(0);
cache[url] = (match && match[1].length === 11) ?
match[1] :
void(0);
return cache[url];
};
}());
/**
* @function
*
* Parses links with html5 video sources in mp4 or webm formats.
*
* @param {string} url Url that should be parsed.
*
* @returns {
* @function
* @param {String} url Url that should be parsed.
* @return {
* object: Object with information about the video
* (file name, video type),
* undefined: when url has incorrect format or argument is
......@@ -151,27 +135,31 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
match = link.pathname
.split('/')
.pop()
.match(/(.+)\.(mp4|webm)$/);
.match(/(.+)\.(mp?4v?|webm)$/);
if (match) {
cache[url] = {
video: match[1],
type: match[2]
};
}
} /*else {
cache[url] = {
video: link.pathname
.split('/')
.pop(),
type: 'other'
};
}*/
return cache[url];
};
}());
/**
* @function
*
* Facade function that parses html5 and youtube links.
*
* @param {string} url Url that should be parsed.
*
* @returns {
* @function
* @param {String} url Url that should be parsed.
* @return {
* object: Object with information about the video:
* {
* mode: "youtube|html5|incorrect",
......@@ -207,32 +195,23 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
};
/**
* @function
*
* Returns short-hand youtube url.
*
* @param {string} video_id Youtube Video Id that will be added to the link.
*
* @returns {string} Short-hand Youtube url.
*
* @example
* @function
* @param {string} video_id Youtube Video Id that will be added to the
* link.
* @return {string} Short-hand Youtube url.
* @examples
* _getYoutubeLink('OEoXaMPEzfM'); => 'http://youtu.be/OEoXaMPEzfM'
*/
var _getYoutubeLink = function (video_id) {
return 'http://youtu.be/' + video_id;
};
/**
* @function
*
* Returns list of objects with information about the passed links.
*
* @function
* @param {array} links List of links that will be processed.
*
* @returns {array} List of objects.
*
* @examples
* var links = [
* 'http://youtu.be/OEoXaMPEzfM',
......@@ -246,7 +225,6 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
* {mode: `html5`, type: `mp4`, ...},
* {mode: `html5`, type: `webm`, ...}
* ]
*
*/
var _getVideoList = function (links) {
if ($.isArray(links)) {
......@@ -267,14 +245,11 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
/**
* @function
*
* Synchronizes 2 Backbone collections by 'field_name' property.
*
* @param {object} fromCollection Collection with which synchronization
* will happens.
* @param {object} toCollection Collection which will synchronized.
*
* @function
* @param {Object} fromCollection Collection with which synchronization will
* happens.
* @param {Object} toCollection Collection which will synchronized.
*/
var _syncCollections = function (fromCollection, toCollection) {
fromCollection.each(function (m) {
......@@ -289,21 +264,17 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
};
/**
* @function
*
* Sends Ajax requests in appropriate format.
*
* @param {string} action Action that will be invoked on server. Is a part
* of url.
* @param {string} component_locator the locator of component.
* @param {array} videoList List of object with information about inserted
* @function
* @param {String} action Action that will be invoked on server.
* @param {String} component_locator the locator of component.
* @param {Array} videoList List of object with information about inserted
* urls.
* @param {object} extraParams Extra parameters that can be send to the
* server
*
* @returns {object} XMLHttpRequest object. Using this object, we can attach
* callbacks to AJAX request events (for example on 'done', 'fail',
* etc.).
* @param {Object} extraParams Extra parameters that can be send to the
* server.
* @return {Object} XMLHttpRequest object. Using this object, we can
* attach callbacks to AJAX request events (for example on 'done',
* 'fail', etc.).
*/
var _command = (function () {
// We will store the XMLHttpRequest object that $.ajax() function
......@@ -314,7 +285,7 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
// _command() function.
var xhr = null;
return function (action, component_locator, videoList, extraParams) {
return function (action, locator, videoList, extraParams) {
var params, data;
if (extraParams) {
......@@ -326,7 +297,7 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
}
data = $.extend(
{ locator: component_locator },
{ locator: locator },
{ videos: videoList },
params
);
......@@ -357,7 +328,5 @@ define(["jquery", "underscore", "jquery.ajaxQueue"], function($, _) {
remove: Storage.remove
}
};
}());
return Utils;
}());
});
......@@ -37,6 +37,7 @@ from .video_handlers import VideoStudentViewHandlers, VideoStudioViewHandlers
from urlparse import urlparse
def get_ext(filename):
# Prevent incorrectly parsing urls like 'http://abc.com/path/video.mp4?xxxx'.
path = urlparse(filename).path
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment