diff --git a/src/js/_enqueues/wp/editor/base.js b/src/js/_enqueues/wp/editor/base.js index 23b948d860508..139df6c1f2645 100644 --- a/src/js/_enqueues/wp/editor/base.js +++ b/src/js/_enqueues/wp/editor/base.js @@ -1127,6 +1127,8 @@ window.wp = window.wp || {}; return b.replace( /[\n\t ]+/, ' ' ); }); + a = a.replace( /(]*>)\s*\n\s*/g, '$1' ); + return a.replace( /\s*\n\s*/g, '' ); }); } diff --git a/tests/qunit/index.html b/tests/qunit/index.html index f9033a776151c..e87e6fe72f21c 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -145,6 +145,7 @@ + + diff --git a/tests/qunit/wp-admin/js/editor.js b/tests/qunit/wp-admin/js/editor.js new file mode 100644 index 0000000000000..e5204adbd999b --- /dev/null +++ b/tests/qunit/wp-admin/js/editor.js @@ -0,0 +1,60 @@ +( function( QUnit, wp ) { + QUnit.module( 'editor' ); + + QUnit.test( 'wp.editor.autop handles line breaks inside [caption] shortcodes correctly (#66373)', function( assert ) { + var tests = [ + { + message: 'Preserves
\\n inside [caption] without doubling to

', + input: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
\nCaption line 2[/caption]', + expected: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
Caption line 2[/caption]\n' + }, + { + message: 'Preserves
\\n inside [caption] without doubling to

', + input: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
\nCaption line 2[/caption]', + expected: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
Caption line 2[/caption]\n' + }, + { + message: 'Preserves
\\r\\n (Windows newlines) inside [caption] without doubling', + input: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
\r\nCaption line 2[/caption]', + expected: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
Caption line 2[/caption]\n' + }, + { + message: 'Preserves attributes on
followed by newline inside [caption]', + input: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
\nCaption line 2[/caption]', + expected: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
Caption line 2[/caption]\n' + }, + { + message: 'Preserves
without trailing newline inside [caption]', + input: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
Caption line 2[/caption]', + expected: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
Caption line 2[/caption]\n' + }, + { + message: 'Converts plain newline \\n inside [caption] to
', + input: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1\nCaption line 2[/caption]', + expected: '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
Caption line 2[/caption]\n' + }, + { + message: 'Handles multiple lines with
\\n inside [caption]', + input: '[caption id="attachment_123" align="alignnone" width="300"] Line 1
\nLine 2
\nLine 3[/caption]', + expected: '[caption id="attachment_123" align="alignnone" width="300"] Line 1
Line 2
Line 3[/caption]\n' + } + ]; + + _.each( tests, function( test ) { + assert.equal( wp.editor.autop( test.input ), test.expected, test.message ); + } ); + } ); + + QUnit.test( 'wp.editor.autop maintains stability across repeated autop and removep cycles (#66373)', function( assert ) { + var content = '[caption id="attachment_123" align="alignnone" width="300"] Caption line 1
\nCaption line 2[/caption]'; + var firstPass = wp.editor.autop( content ); + + // Simulating repeated toggling between Text and Visual editors. + var current = firstPass; + for ( var i = 0; i < 5; i++ ) { + current = wp.editor.autop( wp.editor.removep( current ) ); + } + + assert.equal( current, firstPass, 'Content does not accumulate extra
tags over multiple editor toggle cycles' ); + } ); +} )( window.QUnit, window.wp );