From b7e5e8adc935c38f2dfe621a411362dea81405e0 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Mon, 13 Apr 2020 20:32:58 -0300 Subject: [PATCH 1/4] Fix new lines appearing when alternating between visual and text mode --- syntaxhighlighter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/syntaxhighlighter.js b/syntaxhighlighter.js index 8a95a8d..8888353 100644 --- a/syntaxhighlighter.js +++ b/syntaxhighlighter.js @@ -15,9 +15,9 @@ } ); } - }).on( 'beforeWpautop.syntaxhighlighter', function( event, obj ) { + }).on( 'afterWpautop.syntaxhighlighter', function( event, obj ) { if ( obj.data && obj.data.indexOf( '[' ) !== -1 ) { - obj.data = obj.data.replace( regex, '
$1
' ); + obj.data = obj.unfiltered.replace( regex, '
$1
' ); } }).ready( function() { $( '.wp-editor-wrap.html-active' ).each( function( i, element ) { From 7d4f5381f386c99af23899d6e32938ffd2d465fe Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Tue, 14 Apr 2020 11:36:31 -0300 Subject: [PATCH 2/4] Add replacer to preserve tags p and br --- syntaxhighlighter.js | 46 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/syntaxhighlighter.js b/syntaxhighlighter.js index 8888353..d703b22 100644 --- a/syntaxhighlighter.js +++ b/syntaxhighlighter.js @@ -4,6 +4,48 @@ window.syntaxHLescape = {}; + var tagsToPreserve = [ + [ 'p', 'wp-p' ], + [ 'br', 'wp-br' ], + ]; + + function replaceTag( code, from, to ) { + var regex = new RegExp( `<(\/?)${ from }([>\\s\/]+)`, 'gi' ); + return code.replace( regex, `<$1${ to }$2` ); + } + + function replaceTagsToPreserve( code, action ) { + var newCode = code, + indexReplaced, + indexReplacement; + + if ( action === 'preserve' ) { + indexReplaced = 0; + indexReplacement = 1; + } else { + indexReplaced = 1; + indexReplacement = 0; + } + + tagsToPreserve.forEach( function( tags ) { + newCode = replaceTag( newCode, tags[ indexReplaced ], tags[ indexReplacement ] ); + } ); + + return newCode; + } + + function preserveTags( code ) { + return replaceTagsToPreserve( code, 'preserve' ); + } + + function restoreTags( code ) { + return replaceTagsToPreserve( code, 'restore' ); + } + + function unescapeTags( code ) { + return code.replace( /</g, '<' ).replace( />/g, '>' ).replace( /&/g, '&' ); + } + if ( typeof $ === 'undefined' ) { return; } @@ -11,13 +53,13 @@ $( document ).on( 'afterPreWpautop.syntaxhighlighter', function( event, obj ) { if ( obj.data && obj.data.indexOf( '[' ) !== -1 ) { obj.data = obj.data.replace( regex, function( match, shortcode ) { - return '\n' + shortcode.replace( /</g, '<' ).replace( />/g, '>' ).replace( /&/g, '&' ) + '\n'; + return '\n' + restoreTags( unescapeTags( shortcode ) ) + '\n'; } ); } }).on( 'afterWpautop.syntaxhighlighter', function( event, obj ) { if ( obj.data && obj.data.indexOf( '[' ) !== -1 ) { - obj.data = obj.unfiltered.replace( regex, '
$1
' ); + obj.data = preserveTags( obj.unfiltered.replace( regex, '
$1
' ) ); } }).ready( function() { $( '.wp-editor-wrap.html-active' ).each( function( i, element ) { From bc3fcd4216fa7f81bc6fb4acb70091285ef48b50 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Tue, 14 Apr 2020 14:33:43 -0300 Subject: [PATCH 3/4] Refactor to use only the code pieces unfiltered --- syntaxhighlighter.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/syntaxhighlighter.js b/syntaxhighlighter.js index d703b22..be0e04d 100644 --- a/syntaxhighlighter.js +++ b/syntaxhighlighter.js @@ -59,7 +59,14 @@ } }).on( 'afterWpautop.syntaxhighlighter', function( event, obj ) { if ( obj.data && obj.data.indexOf( '[' ) !== -1 ) { - obj.data = preserveTags( obj.unfiltered.replace( regex, '
$1
' ) ); + var i = 0; + var unfilteredCodes = obj.unfiltered.match( regex ); + + obj.data = obj.data.replace( regex, function() { + // Replace by the unfiltered code piece. + var unfilteredCode = unfilteredCodes[ i++ ]; + return `
${ preserveTags( unfilteredCode ) }
`; + } ); } }).ready( function() { $( '.wp-editor-wrap.html-active' ).each( function( i, element ) { From 991b49c374bf929de48492204aa1ef9849e114fc Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Tue, 14 Apr 2020 15:49:17 -0300 Subject: [PATCH 4/4] Refactor wpautop code --- syntaxhighlighter.js | 93 ++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/syntaxhighlighter.js b/syntaxhighlighter.js index be0e04d..eae102b 100644 --- a/syntaxhighlighter.js +++ b/syntaxhighlighter.js @@ -1,31 +1,33 @@ -( function($) { - var shortcodes = window.syntaxHLcodes || 'sourcecode', +( function( $ ) { + const shortcodes = window.syntaxHLcodes || 'sourcecode', regex = new RegExp( '(?:
\\s*)?(\\[(' + shortcodes + ')[^\\]]*\\][\\s\\S]*?\\[\\/\\2\\])(?:\\s*<\\/pre>)?', 'gi' );
 
 	window.syntaxHLescape = {};
 
-	var tagsToPreserve = [
+	if ( typeof $ === 'undefined' ) {
+		return;
+	}
+
+	// Constants.
+	const $DOC = $( document );
+	const PRESERVE = 'PRESERVE';
+	const RESTORE = 'RESTORE';
+
+	// Tags that are removed by the core and their replacement to prevent being removed.
+	const tagsToPreserve = [
 		[ 'p', 'wp-p' ],
 		[ 'br', 'wp-br' ],
 	];
 
 	function replaceTag( code, from, to ) {
-		var regex = new RegExp( `<(\/?)${ from }([>\\s\/]+)`, 'gi' );
-		return code.replace( regex, `<$1${ to }$2` );
+		const tagRegex = new RegExp( `<(\/?)${ from }([>\\s\/]+)`, 'gi' );
+		return code.replace( tagRegex, `<$1${ to }$2` );
 	}
 
 	function replaceTagsToPreserve( code, action ) {
-		var newCode = code,
-			indexReplaced,
-			indexReplacement;
-
-		if ( action === 'preserve' ) {
-			indexReplaced = 0;
-			indexReplacement = 1;
-		} else {
-			indexReplaced = 1;
-			indexReplacement = 0;
-		}
+		const indexReplaced = action === PRESERVE ? 0 : 1;
+		const indexReplacement = action === PRESERVE ? 1 : 0;
+		let newCode = code;
 
 		tagsToPreserve.forEach( function( tags ) {
 			newCode = replaceTag( newCode, tags[ indexReplaced ], tags[ indexReplacement ] );
@@ -35,46 +37,53 @@
 	}
 
 	function preserveTags( code ) {
-		return replaceTagsToPreserve( code, 'preserve' );
+		return replaceTagsToPreserve( code, PRESERVE );
 	}
 
 	function restoreTags( code ) {
-		return replaceTagsToPreserve( code, 'restore' );
+		return replaceTagsToPreserve( code, RESTORE );
 	}
 
 	function unescapeTags( code ) {
 		return code.replace( /</g, '<' ).replace( />/g, '>' ).replace( /&/g, '&' );
 	}
 
-	if ( typeof $ === 'undefined' ) {
-		return;
-	}
+	const events = {
+		afterPreWpautop: function( event, obj ) {
+			if ( obj.data && obj.data.indexOf( '[' ) === -1 ) {
+				return;
+			}
 
-	$( document ).on( 'afterPreWpautop.syntaxhighlighter', function( event, obj ) {
-		if ( obj.data && obj.data.indexOf( '[' ) !== -1 ) {
 			obj.data = obj.data.replace( regex, function( match, shortcode ) {
-					return '\n' + restoreTags( unescapeTags( shortcode ) ) + '\n';
-				}
-			);
-		}
-	}).on( 'afterWpautop.syntaxhighlighter', function( event, obj ) {
-		if ( obj.data && obj.data.indexOf( '[' ) !== -1 ) {
-			var i = 0;
-			var unfilteredCodes = obj.unfiltered.match( regex );
+				return '\n' + restoreTags( unescapeTags( shortcode ) ) + '\n';
+			} );
+		},
+		afterWpautop: function( event, obj ) {
+			if ( obj.data && obj.data.indexOf( '[' ) === -1 ) {
+				return;
+			}
+
+			const unfilteredCodes = obj.unfiltered.match( regex );
+			let i = 0;
 
 			obj.data = obj.data.replace( regex, function() {
 				// Replace by the unfiltered code piece.
-				var unfilteredCode = unfilteredCodes[ i++ ];
+				const unfilteredCode = unfilteredCodes[ i++ ];
 				return `
${ preserveTags( unfilteredCode ) }
`; } ); - } - }).ready( function() { - $( '.wp-editor-wrap.html-active' ).each( function( i, element ) { - var id = $( element ).find( 'textarea.wp-editor-area' ).attr( 'id' ); + }, + documentReady: function() { + $( '.wp-editor-wrap.html-active' ).each( function( i, element ) { + const id = $( element ).find( 'textarea.wp-editor-area' ).attr( 'id' ); - if ( id ) { - window.syntaxHLescape[id] = true; - } - }); - }); -}( window.jQuery )); + if ( id ) { + window.syntaxHLescape[ id ] = true; + } + } ); + }, + }; + + $DOC.on( 'afterPreWpautop.syntaxhighlighter', events.afterPreWpautop ); + $DOC.on( 'afterWpautop.syntaxhighlighter', events.afterWpautop ); + $DOC.ready( events.documentReady ); +}( window.jQuery ) );