Make WordPress Core

Ticket #40808: 40808.1.diff

File 40808.1.diff, 2.3 KB (added by timmydcrawford, 8 years ago)
  • src/wp-admin/js/widgets/media-video-widget.js

     
    135135                },
    136136
    137137                /**
     138                 * Whether a url is a supported external host.
     139                 *
     140                 * @param {String} url - Video url.
     141                 * @returns {boolean} Whether url is a supported video host.
     142                 */
     143                isHostedVideo: function isHostedVideo( url ) {
     144                        var parsedUrl = document.createElement( 'a' );
     145                        parsedUrl.href = url;
     146                        return /vimeo|youtu\.?be/.test( parsedUrl.host );
     147                },
     148
     149                /**
    138150                 * Render preview.
    139151                 *
    140152                 * @returns {void}
     
    150162                        }
    151163
    152164                        if ( ! attachmentId && attachmentUrl ) {
    153                                 parsedUrl = document.createElement( 'a' );
    154                                 parsedUrl.href = attachmentUrl;
    155                                 isHostedEmbed = /vimeo|youtu\.?be/.test( parsedUrl.host );
     165                                isHostedEmbed = control.isHostedVideo( attachmentUrl );
    156166                        }
    157167
    158168                        if ( isHostedEmbed ) {
  • tests/qunit/wp-admin/js/widgets/test-media-video-widget.js

     
    3232                equal( mappedProps.title, undefined, 'mapMediaToModelProps should ignore title inputs' );
    3333                equal( mappedProps.loop, false, 'mapMediaToModelProps should set loop' );
    3434                equal( mappedProps.preload, 'meta', 'mapMediaToModelProps should set preload' );
     35
     36                // Test isHostedVideo().
     37                equal( videoWidgetControlInstance.isHostedVideo( 'https://www.youtube.com/watch?v=OQSNhk5ICTI'), true, 'isHostedVideo should return true for full YouTube url.' );
     38                equal( videoWidgetControlInstance.isHostedVideo( 'https://youtu.be/OQSNhk5ICTI' ), true, 'isHostedVideo should return true for shortened youtube url' );
     39                equal( videoWidgetControlInstance.isHostedVideo( 'https://vimeo.com/190372437' ), true, 'isHostedVideo should return true for vimeo url.' );
     40                equal( videoWidgetControlInstance.isHostedVideo( 'https://wordpress.org/' ), false, 'isHostedVideo should return false for non-supported video url.' );
    3541        });
    3642
    3743        test( 'video widget control renderPreview', function( assert ) {