Make WordPress Core

Changeset 27542


Ignore:
Timestamp:
03/14/2014 08:25:31 PM (12 years ago)
Author:
wonderboymusic
Message:

In wp.media.mixin, add ua and a compat map to separate and clarify browser detection and support. Leverage Underscore list iterators in isCompatible().

See #27389.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/media-editor.js

    r27539 r27542  
    308308        },
    309309
     310        ua: {
     311            is : function (browser) {
     312                var passes = false, ua = window.navigator.userAgent;
     313
     314                switch ( browser ) {
     315                    case 'oldie':
     316                        passes = ua.match(/MSIE [6-8]/gi) !== null;
     317                    break;
     318                    case 'ie':
     319                        passes = ua.match(/MSIE/gi) !== null;
     320                    break;
     321                    case 'ff':
     322                        passes = ua.match(/firefox/gi) !== null;
     323                    break;
     324                    case 'opera':
     325                        passes = ua.match(/OPR/) !== null;
     326                    break;
     327                    case 'safari':
     328                        passes = ua.match(/safari/gi) !== null && ua.match(/chrome/gi) === null;
     329                    break;
     330                    case 'chrome':
     331                        passes = ua.match(/safari/gi) && ua.match(/chrome/gi) !== null;
     332                    break;
     333                }
     334
     335                return passes;
     336            }
     337        },
     338
     339        compat :{
     340            'opera' : {
     341                audio: ['ogg', 'wav'],
     342                video: ['ogg', 'webm']
     343            },
     344            'chrome' : {
     345                audio: ['ogg', 'mpeg', 'x-ms-wma'],
     346                video: ['ogg', 'webm', 'mp4', 'm4v', 'mpeg']
     347            },
     348            'ff' : {
     349                audio: ['ogg', 'mpeg'],
     350                video: ['ogg', 'webm']
     351            },
     352            'safari' : {
     353                audio: ['mpeg', 'wav'],
     354                video: ['mp4', 'm4v', 'mpeg', 'x-ms-wmv', 'quicktime']
     355            },
     356            'ie' : {
     357                audio: ['mpeg'],
     358                video: ['mp4', 'm4v', 'mpeg']
     359            }
     360        },
     361
    310362        isCompatible: function ( media ) {
    311363            if ( ! media.find( 'source' ).length ) {
     
    313365            }
    314366
    315             var ua = window.navigator.userAgent.toLowerCase(),
    316                 ff, chrome, opera, safari,
    317                 isIE = ua.match(/MSIE/gi) !== null,
    318                 isOpera = window.navigator.userAgent.match(/OPR/) !== null,
    319                 isOldIE = ua.match(/MSIE [6-8]/gi) !== null,
    320                 isChrome = ua.match(/safari/gi) && ua.match(/chrome/gi) !== null,
    321                 isFirefox = ua.match(/firefox/gi) !== null,
    322                 isSafari = ua.match(/safari/gi) !== null && ua.match(/chrome/gi) === null;
    323 
    324             if ( isOldIE || isIE ) {
     367            var ua = this.ua, test = false, found = false, sources;
     368
     369            if ( ua.is( 'oldIE' ) ) {
    325370                return false;
    326371            }
    327372
    328             if ( isOpera ) {
    329                 opera = false;
    330                 media.find( 'source' ).each(function (i, elem) {
    331                     if ( elem.type.match(/video\/(ogv|webm)/gi) !== null ||
    332                         ( elem.type.match(/audio\/(ogg|wav)/gi) !== null ) ) {
    333                         opera = true;
    334                     }
    335                 });
    336 
    337                 return opera;
    338             } else if ( isChrome ) {
    339                 chrome = false;
    340                 media.find( 'source' ).each(function (i, elem) {
    341                     if ( elem.type.match(/video\/(mp4|m4v|mpeg|webm|ogg)/gi) !== null ||
    342                         elem.type.match(/audio\/(ogg|mpeg|x-ms-wma)/gi) !== null ) {
    343                         chrome = true;
    344                     }
    345                 });
    346 
    347                 return chrome;
    348 
    349             } else if ( isFirefox ) {
    350                 ff = false;
    351                 media.find( 'source' ).each(function (i, elem) {
    352                     if ( elem.type.match(/video\/(ogg|webm)/gi) !== null ||
    353                         ( elem.type.match(/audio\/(ogg|mpeg)/gi) !== null && -1 === elem.src.indexOf('.m4a') ) ) {
    354                         ff = true;
    355                     }
    356                 });
    357 
    358                 return ff;
    359 
    360             } else if ( isSafari  ) {
    361                 safari = false;
    362                 media.find( 'source' ).each(function (i, elem) {
    363                     if ( elem.type.match(/video\/(mp4|m4v|mpeg|x-ms-wmv|quicktime)/gi) !== null ||
    364                         ( elem.type.match(/audio\/(mpeg|wav)/gi) !== null ) ) {
    365                         safari = true;
    366                     }
    367                 });
    368 
    369                 return safari;
    370             }
    371 
    372             return false;
     373            sources = media.find( 'source' );
     374
     375            _.find( this.compat, function (supports, browser) {
     376                if ( ua.is( browser ) ) {
     377                    found = true;
     378                    _.each( sources, function (elem) {
     379                        var audio = new RegExp( 'audio\/(' + supports.audio.join('|') + ')', 'gi' ),
     380                            video = new RegExp( 'video\/(' + supports.video.join('|') + ')', 'gi' );
     381
     382                        if ( elem.type.match( video ) !== null || elem.type.match( audio ) !== null ) {
     383                            test = true;
     384                        }
     385                    } );
     386                }
     387
     388                return test || found;
     389            } );
     390
     391            return test;
    373392        },
    374393
Note: See TracChangeset for help on using the changeset viewer.