Make WordPress Core

Changeset 38039


Ignore:
Timestamp:
07/12/2016 10:08:28 PM (10 years ago)
Author:
azaozz
Message:

TinyMCE: preserve <script> and <style> tags inside the editor.
Uses image placeholders for the tags and makes then visible. That way the tags can also be deleted from inside the editor.

Props iseulde, azaozz.
Fixes #32923.

Location:
trunk/src
Files:
2 edited

Legend:

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

    r35999 r38039  
    123123                                blocklist2 = blocklist + '|pre',
    124124                                preserve_linebreaks = false,
    125                                 preserve_br = false;
     125                                preserve_br = false,
     126                                preserve = [];
    126127
    127128                        if ( ! html ) {
     
    129130                        }
    130131
    131                         // Protect pre|script tags
    132                         if ( html.indexOf( '<pre' ) !== -1 || html.indexOf( '<script' ) !== -1 ) {
     132                        // Preserve script and style tags.
     133                        if ( html.indexOf( '<script' ) !== -1 || html.indexOf( '<style' ) !== -1 ) {
     134                                html = html.replace( /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function( match ) {
     135                                        preserve.push( match );
     136                                        return '<wp-preserve>';
     137                                } );
     138                        }
     139
     140                        // Protect pre tags.
     141                        if ( html.indexOf( '<pre' ) !== -1 ) {
    133142                                preserve_linebreaks = true;
    134                                 html = html.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
     143                                html = html.replace( /<pre[^>]*>[\s\S]+?<\/pre>/g, function( a ) {
    135144                                        a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-line-break>' );
    136145                                        a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-line-break>' );
     
    204213                        if ( preserve_br ) {
    205214                                html = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
     215                        }
     216
     217                        // Put back preserved tags.
     218                        if ( preserve.length ) {
     219                                html = html.replace( /<wp-preserve>/g, function() {
     220                                        return preserve.shift();
     221                                } );
    206222                        }
    207223
  • trunk/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js

    r37906 r38039  
    9191    });
    9292
    93         // Replace Read More/Next Page tags with images
    9493        editor.on( 'BeforeSetContent', function( event ) {
    9594                var title;
     
    115114                        if ( event.load && event.format !== 'raw' && hasWpautop ) {
    116115                                event.content = wp.editor.autop( event.content );
     116                        }
     117
     118                        if ( event.content.indexOf( '<script' ) !== -1 || event.content.indexOf( '<style' ) !== -1 ) {
     119                                event.content = event.content.replace( /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function( match, tag ) {
     120                                        return '<img ' +
     121                                                'src="' + tinymce.Env.transparentSrc + '" ' +
     122                                                'data-wp-preserve="' + encodeURIComponent( match ) + '" ' +
     123                                                'data-mce-resize="false" ' +
     124                                                'data-mce-placeholder="1" '+
     125                                                'class="mce-object" ' +
     126                                                'width="20" height="20" '+
     127                                                'alt="&lt;' + tag + '&gt;" ' +
     128                                                'title="&lt;' + tag + '&gt;" ' +
     129                                        '/>';
     130                                } );
    117131                        }
    118132
     
    130144        });
    131145
    132         // Replace images with tags
    133         editor.on( 'PostProcess', function( e ) {
    134                 if ( e.get ) {
    135                         e.content = e.content.replace(/<img[^>]+>/g, function( image ) {
    136                                 var match, moretext = '';
     146        editor.on( 'PostProcess', function( event ) {
     147                if ( event.get ) {
     148                        event.content = event.content.replace(/<img[^>]+>/g, function( image ) {
     149                                var match,
     150                                        string,
     151                                        moretext = '';
    137152
    138153                                if ( image.indexOf( 'data-wp-more="more"' ) !== -1 ) {
     
    141156                                        }
    142157
    143                                         image = '<!--more' + moretext + '-->';
     158                                        string = '<!--more' + moretext + '-->';
    144159                                } else if ( image.indexOf( 'data-wp-more="nextpage"' ) !== -1 ) {
    145                                         image = '<!--nextpage-->';
    146                                 }
    147 
    148                                 return image;
     160                                        string = '<!--nextpage-->';
     161                                } else if ( image.indexOf( 'data-wp-preserve' ) !== -1 ) {
     162                                        if ( match = image.match( / data-wp-preserve="([^"]+)"/ ) ) {
     163                                                string = decodeURIComponent( match[1] );
     164                                        }
     165                                }
     166
     167                                return string || image;
    149168                        });
    150169                }
Note: See TracChangeset for help on using the changeset viewer.