Make WordPress Core

Changeset 28748


Ignore:
Timestamp:
06/12/2014 02:48:17 AM (12 years ago)
Author:
azaozz
Message:

wpView:

  • Don't wrap single-line URLs in [embed]. Use them directly in generating a view.
  • If the embedding HTML contains a script, "sandbox" it in an iframe to prevent it from changing the editor DOM.
  • Automatically add toolbar and overlay when needed.
  • Try to embed single-line URLs only if they are pasted in an empty paragraph.

Props avryl, see #28195

Location:
trunk/src/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/mce-view.js

    r28689 r28748  
    99window.wp = window.wp || {};
    1010
    11 (function($){
     11( function( $ ) {
     12        'use strict';
     13
    1214        var views = {},
    1315                instances = {},
     
    2830        wp.mce.View = function( options ) {
    2931                options = options || {};
     32                this.type = options.type;
    3033                _.extend( this, _.pick( options, viewOptions ) );
    3134                this.initialize.apply( this, arguments );
     
    3639                getHtml: function() {},
    3740                render: function() {
    38                         var html = this.getHtml();
    39                         // Search all tinymce editor instances and update the placeholders
     41                        this.setContent(
     42                                '<div class="toolbar">' +
     43                                        ( _.isFunction( views[ this.type ].edit ) ? '<div class="dashicons dashicons-edit edit"></div>' : '' ) +
     44                                        '<div class="dashicons dashicons-no-alt remove"></div>' +
     45                                '</div>' +
     46                                '<div class="wpview-content">' +
     47                                        this.getHtml() +
     48                                '</div>' +
     49                                ( this.overlay ? '<div class="wpview-overlay"></div>' : '' ) +
     50                                // The <ins> is used to mark the end of the wrapper div (has to be the last child node).
     51                                // Needed when comparing the content as string for preventing extra undo levels.
     52                                '<ins data-wpview-end="1"></ins>',
     53                                function( self, editor, node ) {
     54                                        $( self ).trigger( 'ready', [ editor, node ] );
     55                                }
     56                        );
     57                },
     58                unbind: function() {},
     59                setContent: function( html, callback, replace ) {
    4060                        _.each( tinymce.editors, function( editor ) {
    4161                                var self = this;
    4262                                if ( editor.plugins.wpview ) {
    43                                         $( editor.getDoc() ).find( '[data-wpview-text="' + this.encodedText + '"]' ).each( function ( i, element ) {
    44                                                 var node = $( element );
    45                                                 // The <ins> is used to mark the end of the wrapper div. Needed when comparing
    46                                                 // the content as string for preventing extra undo levels.
    47                                                 node.html( html ).append( '<ins data-wpview-end="1"></ins>' );
    48                                                 $( self ).trigger( 'ready', element );
    49                                         });
     63                                        $( editor.getBody() )
     64                                        .find( '[data-wpview-text="' + this.encodedText + '"]' )
     65                                        .each( function ( i, element ) {
     66                                                var contentWrap = $( element ).children( '.wpview-content' ),
     67                                                        wrap = element;
     68
     69                                                if ( contentWrap.length ) {
     70                                                        element = contentWrap = contentWrap[0];
     71                                                }
     72
     73                                                if ( _.isString( html ) ) {
     74                                                        if ( replace ) {
     75                                                                element = editor.dom.replace( editor.dom.createFragment( html ), wrap );
     76                                                        } else {
     77                                                                editor.dom.setHTML( element, html );
     78                                                        }
     79                                                } else {
     80                                                        if ( replace ) {
     81                                                                element = editor.dom.replace( html, wrap );
     82                                                        } else {
     83                                                                element.appendChild( html );
     84                                                        }
     85                                                }
     86
     87                                                if ( _.isFunction( callback ) ) {
     88                                                        callback( self, editor, $( element ).children( '.wpview-content' )[0] );
     89                                                }
     90                                        } );
    5091                                }
    5192                        }, this );
    5293                },
    53                 unbind: function() {}
     94                setError: function( message, dashicon ) {
     95                        this.setContent(
     96                                '<div class="wpview-error">' +
     97                                        '<div class="dashicons dashicons-' + ( dashicon ? dashicon : 'no' ) + '"></div>' +
     98                                        '<p>' + message + '</p>' +
     99                                '</div>'
     100                        );
     101                }
    54102        } );
    55103
     
    210258                        if ( ! wp.mce.views.getInstance( encodedText ) ) {
    211259                                viewOptions = options;
     260                                viewOptions.type = viewType;
    212261                                viewOptions.encodedText = encodedText;
    213262                                instance = new view.View( viewOptions );
     
    245294                                result = view.toView( text );
    246295                                viewOptions = result.options;
     296                                viewOptions.type = view.type;
    247297                                viewOptions.encodedText = encodedText;
    248298                                instance = new view.View( viewOptions );
     
    332382
    333383                                return this.template( options );
    334 
    335384                        }
    336385                },
     
    362411
    363412                View: _.extend( {}, wp.media.mixin, {
     413                        overlay: true,
     414
    364415                        initialize: function( options ) {
    365416                                this.players = [];
     
    376427                         * @global MediaElementPlayer
    377428                         *
    378                          * @param {Event} e
     429                         * @param {Event} event
     430                         * @param {Object} editor
    379431                         * @param {HTMLElement} node
    380432                         */
    381                         setPlayer: function(e, node) {
    382                                 // if the ready event fires on an empty node
    383                                 if ( ! node ) {
    384                                         return;
    385                                 }
    386 
     433                        setPlayer: function( event, editor, node ) {
    387434                                var self = this,
    388                                         media,
    389                                         firefox = this.ua.is( 'ff' ),
    390                                         className = '.wp-' +  this.shortcode.tag + '-shortcode';
    391 
    392                                 media = $( node ).find( className );
     435                                        media;
     436
     437                                media = $( node ).find( '.wp-' +  this.shortcode.tag + '-shortcode' );
    393438
    394439                                if ( ! this.isCompatible( media ) ) {
    395440                                        media.closest( '.wpview-wrap' ).addClass( 'wont-play' );
    396                                         if ( ! media.parent().hasClass( 'wpview-wrap' ) ) {
    397                                                 media.parent().replaceWith( media );
    398                                         }
    399441                                        media.replaceWith( '<p>' + media.find( 'source' ).eq(0).prop( 'src' ) + '</p>' );
    400442                                        return;
    401443                                } else {
    402444                                        media.closest( '.wpview-wrap' ).removeClass( 'wont-play' );
    403                                         if ( firefox ) {
     445                                        if ( this.ua.is( 'ff' ) ) {
    404446                                                media.prop( 'preload', 'metadata' );
    405447                                        } else {
     
    509551                View: _.extend( {}, wp.media.mixin, {
    510552                        template:  media.template( 'editor-playlist' ),
     553                        overlay: true,
    511554
    512555                        initialize: function( options ) {
     
    532575                        },
    533576
    534                         setPlaylist: function( event, element ) {
     577                        setPlaylist: function( event, editor, element ) {
    535578                                if ( ! this.data.tracks ) {
    536579                                        return;
     
    635678         * TinyMCE handler for the embed shortcode
    636679         */
     680        wp.mce.embedView = _.extend( {}, wp.media.mixin, {
     681                overlay: true,
     682                initialize: function( options ) {
     683                        this.players = [];
     684                        this.content = options.content;
     685                        this.fetching = false;
     686                        this.parsed = false;
     687                        this.original = options.url || options.shortcode.string();
     688
     689                        if ( options.url ) {
     690                                this.shortcode = '[embed]' + options.url + '[/embed]';
     691                        } else {
     692                                this.shortcode = options.shortcode.string();
     693                        }
     694
     695                        _.bindAll( this, 'setHtml', 'setNode', 'fetch' );
     696                        $( this ).on( 'ready', this.setNode );
     697                },
     698                unbind: function() {
     699                        var self = this;
     700                        _.each( this.players, function ( player ) {
     701                                player.pause();
     702                                self.removePlayer( player );
     703                        } );
     704                        this.players = [];
     705                },
     706                setNode: function () {
     707                        if ( this.parsed ) {
     708                                this.setHtml( this.parsed );
     709                                this.parseMediaShortcodes();
     710                        } else if ( ! this.fetching ) {
     711                                this.fetch();
     712                        }
     713                },
     714                fetch: function () {
     715                        var self = this;
     716
     717                        this.fetching = true;
     718
     719                        wp.ajax.send( 'parse-embed', {
     720                                data: {
     721                                        post_ID: $( '#post_ID' ).val(),
     722                                        content: this.shortcode
     723                                }
     724                        } )
     725                        .done( function( content ) {
     726                                self.fetching = false;
     727
     728                                if ( content.substring( 0, ( '<a href' ).length ) === '<a href' ) {
     729                                        if ( self.type === 'embed' ) {
     730                                                self.setError( self.original + ' failed to embed.', 'admin-media' );
     731                                        } else {
     732                                                self.setContent( self.original, null, true );
     733                                        }
     734                                } else {
     735                                        self.parsed = content;
     736                                        self.setHtml( content );
     737                                }
     738                        } )
     739                        .fail( function() {
     740                                self.fetching = false;
     741                                self.setError( self.original + ' failed to embed due to a server error.', 'admin-media' );
     742                        } );
     743                },
     744                /* jshint scripturl: true */
     745                setHtml: function ( content ) {
     746                        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
     747                                iframe, iframeDoc, i, resize,
     748                                dom = tinymce.DOM;
     749
     750                        if ( content.indexOf( '<script' ) !== -1 ) {
     751                                iframe = dom.create( 'iframe', {
     752                                        src: tinymce.Env.ie ? 'javascript:""' : '',
     753                                        frameBorder: '0',
     754                                        allowTransparency: 'true',
     755                                        style: {
     756                                                width: '100%',
     757                                                display: 'block'
     758                                        }
     759                                } );
     760
     761                                this.setContent( iframe );
     762                                iframeDoc = iframe.contentWindow.document;
     763
     764                                iframeDoc.open();
     765                                iframeDoc.write(
     766                                        '<!DOCTYPE html>' +
     767                                        '<html>' +
     768                                                '<head>' +
     769                                                        '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' +
     770                                                '</head>' +
     771                                                '<body>' +
     772                                                        content +
     773                                                '</body>' +
     774                                        '</html>'
     775                                );
     776                                iframeDoc.close();
     777
     778                                resize = function() {
     779                                        $( iframe ).height( $( iframeDoc ).height() );
     780                                };
     781
     782                                if ( MutationObserver ) {
     783                                        new MutationObserver( _.debounce( function() {
     784                                                resize();
     785                                        }, 100 ) )
     786                                        .observe( iframeDoc.body, {
     787                                                attributes: true,
     788                                                childList: true,
     789                                                subtree: true
     790                                        } );
     791                                } else {
     792                                        for ( i = 1; i < 6; i++ ) {
     793                                                setTimeout( resize, i * 700 );
     794                                        }
     795                                }
     796                        } else {
     797                                this.setContent( content );
     798                        }
     799
     800                        this.parseMediaShortcodes();
     801                },
     802                parseMediaShortcodes: function () {
     803                        var self = this;
     804                        $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) {
     805                                self.players.push( new MediaElementPlayer( element, self.mejsSettings ) );
     806                        } );
     807                },
     808                getHtml: function() {
     809                        return '';
     810                }
     811        } );
     812
    637813        wp.mce.views.register( 'embed', {
    638                 View: _.extend( {}, wp.media.mixin, {
    639                         template: media.template( 'editor-embed' ),
    640                         initialize: function( options ) {
    641                                 this.players = [];
    642                                 this.content = options.content;
    643                                 this.parsed = false;
    644                                 this.shortcode = options.shortcode;
    645                                 _.bindAll( this, 'setHtml', 'setNode', 'fetch' );
    646                                 $( this ).on( 'ready', this.setNode );
    647                         },
    648                         unbind: function() {
    649                                 var self = this;
    650                                 _.each( this.players, function ( player ) {
    651                                         player.pause();
    652                                         self.removePlayer( player );
    653                                 } );
    654                                 this.players = [];
    655                         },
    656                         setNode: function ( e, node ) {
    657                                 this.node = node;
    658                                 if ( this.parsed ) {
    659                                         this.parseMediaShortcodes();
    660                                 } else {
    661                                         this.fetch();
    662                                 }
    663                         },
    664                         fetch: function () {
    665                                 wp.ajax.send( 'parse-embed', {
    666                                         data: {
    667                                                 post_ID: $( '#post_ID' ).val(),
    668                                                 content: this.shortcode.string()
    669                                         }
    670                                 } ).done( this.setHtml );
    671                         },
    672                         setHtml: function ( content ) {
    673                                 this.parsed = content;
    674                                 $( this.node ).html( this.getHtml() );
    675                                 this.parseMediaShortcodes();
    676                         },
    677                         parseMediaShortcodes: function () {
    678                                 var self = this;
    679                                 $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) {
    680                                         self.players.push( new MediaElementPlayer( element, self.mejsSettings ) );
    681                                 } );
    682                         },
    683                         getHtml: function() {
    684                                 if ( ! this.parsed ) {
    685                                         return '';
    686                                 }
    687                                 return this.template({ content: this.parsed });
    688                         }
    689                 } ),
    690                 edit: function() {}
     814                View: wp.mce.embedView
    691815        } );
    692816
     817        wp.mce.views.register( 'embedURL', {
     818                toView: function( content ) {
     819                        var re = /(?:^|<p>)(https?:\/\/[^\s"]+?)(?:<\/p>\s*|$)/gi,
     820                                match = re.exec( tinymce.trim( content ) );
     821
     822                        if ( ! match ) {
     823                                return;
     824                        }
     825
     826                        return {
     827                                index: match.index,
     828                                content: match[0],
     829                                options: {
     830                                        url: match[1]
     831                                }
     832                        };
     833                },
     834                View: wp.mce.embedView
     835        } );
     836
    693837}(jQuery));
  • trunk/src/wp-includes/js/tinymce/plugins/wpview/plugin.js

    r28687 r28748  
    159159        // view wrappers.
    160160        editor.on( 'BeforeSetContent', function( event ) {
     161                var node;
     162
    161163                if ( ! event.content ) {
    162164                        return;
     
    167169                }
    168170
     171                node = editor.selection.getNode();
     172
     173                // When a url is pasted, only try to embed it when pasted in an empty paragrapgh.
     174                if ( event.content.match( /^\s*(https?:\/\/[^\s"]+)\s*$/i ) &&
     175                        ( node.nodeName !== 'P' || node.parentNode !== editor.getBody() || ! editor.dom.isEmpty( node ) ) ) {
     176                        return;
     177                }
     178
    169179                event.content = wp.mce.views.toViews( event.content );
    170180        });
    171 
    172         editor.on( 'PastePreProcess', function( event ) {
    173                 if ( event.content.match( /^\s*(https?:\/\/[^\s"]+)\s*$/im ) ) {
    174                         event.content = '[embed]' + event.content + '[/embed]';
    175                 }
    176         } );
    177181
    178182        // When the editor's content has been updated and the DOM has been
  • trunk/src/wp-includes/media-template.php

    r28683 r28748  
    995995
    996996        <script type="text/html" id="tmpl-editor-gallery">
    997                 <div class="toolbar">
    998                         <div class="dashicons dashicons-edit edit"></div><div class="dashicons dashicons-no-alt remove"></div>
    999                 </div>
    1000997                <# if ( data.attachments ) { #>
    1001998                        <div class="gallery gallery-columns-{{ data.columns }}">
     
    10281025
    10291026        <script type="text/html" id="tmpl-editor-audio">
    1030                 <div class="toolbar">
    1031                         <div class="dashicons dashicons-edit edit"></div>
    1032                         <div class="dashicons dashicons-no-alt remove"></div>
    1033                 </div>
    10341027                <?php wp_underscore_audio_template() ?>
    1035                 <div class="wpview-overlay"></div>
    10361028        </script>
    10371029
    10381030        <script type="text/html" id="tmpl-editor-video">
    1039                 <div class="toolbar">
    1040                         <div class="dashicons dashicons-edit edit"></div>
    1041                         <div class="dashicons dashicons-no-alt remove"></div>
    1042                 </div>
    10431031                <?php wp_underscore_video_template() ?>
    1044                 <div class="wpview-overlay"></div>
    10451032        </script>
    10461033
     
    10481035
    10491036        <script type="text/html" id="tmpl-editor-playlist">
    1050                 <div class="toolbar">
    1051                         <div class="dashicons dashicons-edit edit"></div>
    1052                         <div class="dashicons dashicons-no-alt remove"></div>
    1053                 </div>
    10541037                <# if ( data.tracks ) { #>
    10551038                        <div class="wp-playlist wp-{{ data.type }}-playlist wp-playlist-{{ data.style }}">
     
    10631046                                <div class="wp-playlist-prev"></div>
    10641047                        </div>
    1065                         <div class="wpview-overlay"></div>
    10661048                <# } else { #>
    10671049                        <div class="wpview-error">
     
    10741056                <img class="crop-image" src="{{ data.url }}">
    10751057                <div class="upload-errors"></div>
    1076         </script>
    1077 
    1078         <script type="text/html" id="tmpl-editor-embed">
    1079                 <div class="toolbar">
    1080                         <div class="dashicons dashicons-no-alt remove"></div>
    1081                 </div>
    1082                 {{{ data.content }}}
    1083                 <div class="wpview-overlay"></div>
    10841058        </script>
    10851059
Note: See TracChangeset for help on using the changeset viewer.