|
1 | | -( function($) { |
2 | | - var shortcodes = window.syntaxHLcodes || 'sourcecode', |
| 1 | +( function( $ ) { |
| 2 | + const shortcodes = window.syntaxHLcodes || 'sourcecode', |
3 | 3 | regex = new RegExp( '(?:<pre>\\s*)?(\\[(' + shortcodes + ')[^\\]]*\\][\\s\\S]*?\\[\\/\\2\\])(?:\\s*<\\/pre>)?', 'gi' ); |
4 | 4 |
|
5 | 5 | window.syntaxHLescape = {}; |
|
8 | 8 | return; |
9 | 9 | } |
10 | 10 |
|
11 | | - $( document ).on( 'afterPreWpautop.syntaxhighlighter', function( event, obj ) { |
12 | | - if ( obj.data && obj.data.indexOf( '[' ) !== -1 ) { |
| 11 | + // Constants. |
| 12 | + const $DOC = $( document ); |
| 13 | + const PRESERVE = 'PRESERVE'; |
| 14 | + const RESTORE = 'RESTORE'; |
| 15 | + |
| 16 | + // Tags that are removed by the core and their replacement to prevent being removed. |
| 17 | + const tagsToPreserve = [ |
| 18 | + [ 'p', 'wp-p' ], |
| 19 | + [ 'br', 'wp-br' ], |
| 20 | + ]; |
| 21 | + |
| 22 | + function replaceTag( code, from, to ) { |
| 23 | + const tagRegex = new RegExp( `<(\/?)${ from }([>\\s\/]+)`, 'gi' ); |
| 24 | + return code.replace( tagRegex, `<$1${ to }$2` ); |
| 25 | + } |
| 26 | + |
| 27 | + function replaceTagsToPreserve( code, action ) { |
| 28 | + const indexReplaced = action === PRESERVE ? 0 : 1; |
| 29 | + const indexReplacement = action === PRESERVE ? 1 : 0; |
| 30 | + let newCode = code; |
| 31 | + |
| 32 | + tagsToPreserve.forEach( function( tags ) { |
| 33 | + newCode = replaceTag( newCode, tags[ indexReplaced ], tags[ indexReplacement ] ); |
| 34 | + } ); |
| 35 | + |
| 36 | + return newCode; |
| 37 | + } |
| 38 | + |
| 39 | + function preserveTags( code ) { |
| 40 | + return replaceTagsToPreserve( code, PRESERVE ); |
| 41 | + } |
| 42 | + |
| 43 | + function restoreTags( code ) { |
| 44 | + return replaceTagsToPreserve( code, RESTORE ); |
| 45 | + } |
| 46 | + |
| 47 | + function unescapeTags( code ) { |
| 48 | + return code.replace( /</g, '<' ).replace( />/g, '>' ).replace( /&/g, '&' ); |
| 49 | + } |
| 50 | + |
| 51 | + const events = { |
| 52 | + afterPreWpautop: function( event, obj ) { |
| 53 | + if ( obj.data && obj.data.indexOf( '[' ) === -1 ) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
13 | 57 | obj.data = obj.data.replace( regex, function( match, shortcode ) { |
14 | | - return '\n' + shortcode.replace( /</g, '<' ).replace( />/g, '>' ).replace( /&/g, '&' ) + '\n'; |
15 | | - } |
16 | | - ); |
17 | | - } |
18 | | - }).on( 'beforeWpautop.syntaxhighlighter', function( event, obj ) { |
19 | | - if ( obj.data && obj.data.indexOf( '[' ) !== -1 ) { |
20 | | - obj.data = obj.data.replace( regex, '<pre>$1</pre>' ); |
21 | | - } |
22 | | - }).ready( function() { |
23 | | - $( '.wp-editor-wrap.html-active' ).each( function( i, element ) { |
24 | | - var id = $( element ).find( 'textarea.wp-editor-area' ).attr( 'id' ); |
25 | | - |
26 | | - if ( id ) { |
27 | | - window.syntaxHLescape[id] = true; |
| 58 | + return '\n' + restoreTags( unescapeTags( shortcode ) ) + '\n'; |
| 59 | + } ); |
| 60 | + }, |
| 61 | + afterWpautop: function( event, obj ) { |
| 62 | + if ( obj.data && obj.data.indexOf( '[' ) === -1 ) { |
| 63 | + return; |
28 | 64 | } |
29 | | - }); |
30 | | - }); |
31 | | -}( window.jQuery )); |
| 65 | + |
| 66 | + const unfilteredCodes = obj.unfiltered.match( regex ); |
| 67 | + let i = 0; |
| 68 | + |
| 69 | + obj.data = obj.data.replace( regex, function() { |
| 70 | + // Replace by the unfiltered code piece. |
| 71 | + const unfilteredCode = unfilteredCodes[ i++ ]; |
| 72 | + return `<pre>${ preserveTags( unfilteredCode ) }</pre>`; |
| 73 | + } ); |
| 74 | + }, |
| 75 | + documentReady: function() { |
| 76 | + $( '.wp-editor-wrap.html-active' ).each( function( i, element ) { |
| 77 | + const id = $( element ).find( 'textarea.wp-editor-area' ).attr( 'id' ); |
| 78 | + |
| 79 | + if ( id ) { |
| 80 | + window.syntaxHLescape[ id ] = true; |
| 81 | + } |
| 82 | + } ); |
| 83 | + }, |
| 84 | + }; |
| 85 | + |
| 86 | + $DOC.on( 'afterPreWpautop.syntaxhighlighter', events.afterPreWpautop ); |
| 87 | + $DOC.on( 'afterWpautop.syntaxhighlighter', events.afterWpautop ); |
| 88 | + $DOC.ready( events.documentReady ); |
| 89 | +}( window.jQuery ) ); |
0 commit comments