@@ -1363,18 +1363,54 @@ ZSSEditor.removeVideo = function(videoNodeIdentifier) {
13631363} ;
13641364
13651365ZSSEditor . replaceVideoPressVideosForShortcode = function ( html ) {
1366- // call methods to restore any transformed content from its visual presentation to its source code.
1366+
13671367 var regex = / < v i d e o [ ^ > ] * d a t a - w p v i d e o p r e s s = " ( [ \s \S ] + ?) " [ ^ > ] * > * < \/ v i d e o > / g;
1368+ var str = html . replace ( regex , ZSSEditor . removeVideoPressVisualFormattingCallback ) ;
1369+
1370+ return str ;
1371+ }
1372+
1373+ ZSSEditor . removeVideoPressVisualFormattingCallback = function ( match , content ) {
1374+ return "[wpvideo " + content + "]" ;
1375+ }
1376+
1377+ ZSSEditor . replaceVideosForShortcode = function ( html ) {
1378+
1379+ var regex = / < v i d e o (?: (? ! d a t a - w p v i d e o p r e s s ) .) * > < \/ v i d e o > / g;
13681380 var str = html . replace ( regex , ZSSEditor . removeVideoVisualFormattingCallback ) ;
13691381
13701382 return str ;
13711383}
13721384
13731385ZSSEditor . removeVideoVisualFormattingCallback = function ( match , content ) {
1374- return "[wpvideo " + content + "]" ;
1386+ var videoElement = $ . parseHTML ( match ) [ 0 ] ;
1387+
1388+ // Remove editor playback attributes
1389+ videoElement . removeAttribute ( "onclick" ) ;
1390+ videoElement . removeAttribute ( "controls" ) ;
1391+ videoElement . removeAttribute ( "webkit-playsinline" ) ;
1392+ if ( videoElement . getAttribute ( "preload" ) == "metadata" ) {
1393+ // The "metadata" setting is the WP default and is usually automatically stripped from the shortcode.
1394+ // If it's present, it was probably set by this editor and we should remove it. Even if it wasn't, removing it
1395+ // won't affect anything as it's the default setting for the preload attribute.
1396+ videoElement . removeAttribute ( "preload" ) ;
1397+ }
1398+
1399+ // If filetype attributes exist, the src attribute wasn't there originally and we should remove it
1400+ for ( var i = 0 ; i < ZSSEditor . videoShortcodeFormats . length ; i ++ ) {
1401+ var format = ZSSEditor . videoShortcodeFormats [ i ] ;
1402+ if ( videoElement . hasAttribute ( format ) ) {
1403+ videoElement . removeAttribute ( "src" ) ;
1404+ break ;
1405+ }
1406+ }
1407+
1408+ var shortcode = videoElement . outerHTML . replace ( / < / g, "[" ) ;
1409+ shortcode = shortcode . replace ( / > / g, "]" ) ;
1410+ return shortcode ;
13751411}
13761412
1377- ZSSEditor . applyVideoFormattingCallback = function ( match ) {
1413+ ZSSEditor . applyVideoPressFormattingCallback = function ( match ) {
13781414 if ( match . attrs . numeric . length == 0 ) {
13791415 return match . content ;
13801416 }
@@ -1387,6 +1423,46 @@ ZSSEditor.applyVideoFormattingCallback = function( match ) {
13871423 return out ;
13881424}
13891425
1426+ // Video format tags supported by the [video] shortcode: https://codex.wordpress.org/Video_Shortcode
1427+ // mp4, m4v and webm prioritized since they're supported by the stock player as of Android API 23
1428+ ZSSEditor . videoShortcodeFormats = [ "mp4" , "m4v" , "webm" , "ogv" , "wmv" , "flv" ] ;
1429+
1430+ ZSSEditor . applyVideoFormattingCallback = function ( match ) {
1431+ // Find the tag containing the video source
1432+ var srcTag = "" ;
1433+
1434+ if ( match . attrs . named [ 'src' ] ) {
1435+ srcTag = "src" ;
1436+ } else {
1437+ for ( var i = 0 ; i < ZSSEditor . videoShortcodeFormats . length ; i ++ ) {
1438+ var format = ZSSEditor . videoShortcodeFormats [ i ] ;
1439+ if ( match . attrs . named [ format ] ) {
1440+ srcTag = format ;
1441+ break ;
1442+ }
1443+ }
1444+ }
1445+
1446+ if ( srcTag . length == 0 ) {
1447+ return match . content ;
1448+ }
1449+
1450+ var out = '<video webkit-playsinline src="' + match . attrs . named [ srcTag ] + '"' ;
1451+
1452+ // Preserve all existing tags
1453+ for ( var item in match . attrs . named ) {
1454+ out += ' ' + item + '="' + match . attrs . named [ item ] + '"' ;
1455+ }
1456+
1457+ if ( ! match . attrs . named [ 'preload' ] ) {
1458+ out += ' preload="metadata"' ;
1459+ }
1460+
1461+ out += ' onclick="" controls="controls"></video>' ;
1462+
1463+ return out ;
1464+ }
1465+
13901466/**
13911467 * @brief Sets the VidoPress video URL and poster URL on a video tag.
13921468 * @details When switching between source and visual the wpvideo shortcode are replace by a video tag. Unfortunaly there
@@ -1891,7 +1967,8 @@ ZSSEditor.removeCaptionFormattingCallback = function( match, content ) {
18911967 */
18921968ZSSEditor . applyVisualFormatting = function ( html ) {
18931969 var str = wp . shortcode . replace ( 'caption' , html , ZSSEditor . applyCaptionFormatting ) ;
1894- str = wp . shortcode . replace ( 'wpvideo' , str , ZSSEditor . applyVideoFormattingCallback ) ;
1970+ str = wp . shortcode . replace ( 'wpvideo' , str , ZSSEditor . applyVideoPressFormattingCallback ) ;
1971+ str = wp . shortcode . replace ( 'video' , str , ZSSEditor . applyVideoFormattingCallback ) ;
18951972 return str ;
18961973}
18971974
@@ -1907,6 +1984,7 @@ ZSSEditor.removeVisualFormatting = function( html ) {
19071984 str = ZSSEditor . removeImageSelectionFormattingFromHTML ( str ) ;
19081985 str = ZSSEditor . removeCaptionFormatting ( str ) ;
19091986 str = ZSSEditor . replaceVideoPressVideosForShortcode ( str ) ;
1987+ str = ZSSEditor . replaceVideosForShortcode ( str ) ;
19101988 return str ;
19111989}
19121990
0 commit comments