From bfd0ac1099fec33585a413da24e9946bd28bb8a5 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 14 Mar 2014 14:35:49 +0000 Subject: [PATCH] The `canPlayType` property for audio and video in JS is so bad that the official valid responses are "probably" and "maybe". There are many cases where we might want to know if an audio|video tag is gonna blow up in our face before even attempting to make a `MediaElementPlayer` instance out of it. The best (and most cautious) way to tackle this is to whitelist types by browser. Imagine that one implemented MEjs in TinyMCE's rich editor mode, this would be very helpful. Add `isCompatible( $media )` to `wp.media.mixin`. Future features will use this. See #27389. git-svn-id: https://develop.svn.wordpress.org/trunk@27539 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/js/media-editor.js | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/wp-includes/js/media-editor.js b/src/wp-includes/js/media-editor.js index a8458fb178..1c82c6f54e 100644 --- a/src/wp-includes/js/media-editor.js +++ b/src/wp-includes/js/media-editor.js @@ -307,6 +307,71 @@ } }, + isCompatible: function ( media ) { + if ( ! media.find( 'source' ).length ) { + return false; + } + + var ua = window.navigator.userAgent.toLowerCase(), + ff, chrome, opera, safari, + isIE = ua.match(/MSIE/gi) !== null, + isOpera = window.navigator.userAgent.match(/OPR/) !== null, + isOldIE = ua.match(/MSIE [6-8]/gi) !== null, + isChrome = ua.match(/safari/gi) && ua.match(/chrome/gi) !== null, + isFirefox = ua.match(/firefox/gi) !== null, + isSafari = ua.match(/safari/gi) !== null && ua.match(/chrome/gi) === null; + + if ( isOldIE || isIE ) { + return false; + } + + if ( isOpera ) { + opera = false; + media.find( 'source' ).each(function (i, elem) { + if ( elem.type.match(/video\/(ogv|webm)/gi) !== null || + ( elem.type.match(/audio\/(ogg|wav)/gi) !== null ) ) { + opera = true; + } + }); + + return opera; + } else if ( isChrome ) { + chrome = false; + media.find( 'source' ).each(function (i, elem) { + if ( elem.type.match(/video\/(mp4|m4v|mpeg|webm|ogg)/gi) !== null || + elem.type.match(/audio\/(ogg|mpeg|x-ms-wma)/gi) !== null ) { + chrome = true; + } + }); + + return chrome; + + } else if ( isFirefox ) { + ff = false; + media.find( 'source' ).each(function (i, elem) { + if ( elem.type.match(/video\/(ogg|webm)/gi) !== null || + ( elem.type.match(/audio\/(ogg|mpeg)/gi) !== null && -1 === elem.src.indexOf('.m4a') ) ) { + ff = true; + } + }); + + return ff; + + } else if ( isSafari ) { + safari = false; + media.find( 'source' ).each(function (i, elem) { + if ( elem.type.match(/video\/(mp4|m4v|mpeg|x-ms-wmv|quicktime)/gi) !== null || + ( elem.type.match(/audio\/(mpeg|wav)/gi) !== null ) ) { + safari = true; + } + }); + + return safari; + } + + return false; + }, + /** * Override the MediaElement method for removing a player. * MediaElement tries to pull the audio/video tag out of