From c3bfb8c91fc04071ca70a5823f7af29fadd0276c Mon Sep 17 00:00:00 2001 From: Sourav Pahwa Date: Mon, 26 Oct 2026 16:37:53 +0530 Subject: [PATCH] Editor: Preserve list structure when switching from Text to Visual mode --- src/js/_enqueues/wp/editor/base.js | 12 +++++ tests/qunit/index.html | 2 + tests/qunit/wp-admin/js/editor.js | 87 ++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 tests/qunit/wp-admin/js/editor.js diff --git a/src/js/_enqueues/wp/editor/base.js b/src/js/_enqueues/wp/editor/base.js index 23b948d860508..bcae80b662cc2 100644 --- a/src/js/_enqueues/wp/editor/base.js +++ b/src/js/_enqueues/wp/editor/base.js @@ -470,6 +470,18 @@ window.wp = window.wp || {}; cursorEnd = isCursorEndInTag.gtPos; } + // Keep bookmarks inside list items. A span between items makes TinyMCE restructure the list. + var beforeStart = content.slice( 0, cursorStart ).match( /<\/li\s*>\s*$/i ), + beforeEnd = content.slice( 0, cursorEnd ).match( /<\/li\s*>\s*$/i ); + + if ( beforeStart ) { + cursorStart = beforeStart.index; + } + + if ( beforeEnd ) { + cursorEnd = beforeEnd.index; + } + var isCursorStartInShortcode = getShortcodeWrapperInfo( content, cursorStart ); if ( isCursorStartInShortcode && ! isCursorStartInShortcode.showAsPlainText ) { /** diff --git a/tests/qunit/index.html b/tests/qunit/index.html index 5054ff954518d..eecbe8c7a5d21 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -2294,6 +2294,8 @@

Preview

+ + diff --git a/tests/qunit/wp-admin/js/editor.js b/tests/qunit/wp-admin/js/editor.js new file mode 100644 index 0000000000000..3362dd3ca2a3a --- /dev/null +++ b/tests/qunit/wp-admin/js/editor.js @@ -0,0 +1,87 @@ +/* global tinymce, switchEditors */ +( function( $ ) { + var editor, textarea, setUserSetting; + + QUnit.module( 'switchEditors: list bookmarks', { + beforeEach: function( assert ) { + var done = assert.async(); + + setUserSetting = window.setUserSetting; + window.setUserSetting = function() {}; + $( '#qunit-fixture' ).append( '
' ); + textarea = document.getElementById( 'list-editor' ); + tinymce.init( { + selector: '#list-editor', + theme: false, + skin: false, + fix_list_elements: true, + init_instance_callback: function( instance ) { + editor = instance; + done(); + } + } ); + }, + afterEach: function() { + editor.remove(); + window.setUserSetting = setUserSetting; + } + } ); + + function switchToVisual( html, start, end ) { + editor.hide(); + textarea.value = html; + textarea.setSelectionRange( start, end === undefined ? start : end ); + switchEditors.go( 'list-editor', 'tmce' ); + } + + [ 'ul', 'ol' ].forEach( function( tag ) { + var html = '<' + tag + '>\n
  • A
  • \n
  • B
  • \n
  • C
  • \n
  • D
  • \n
  • E
  • \n'; + + [ 'A', 'B', 'C', 'D', 'E' ].forEach( function( item ) { + QUnit.test( tag + ': cursor after ' + item + ' closing tag (#66379)', function( assert ) { + var position = html.indexOf( item + '' ) + 6; + + editor.setContent( html ); + var expected = editor.getContent(); + switchToVisual( html, position ); + + assert.strictEqual( editor.getContent(), expected, 'List markup is unchanged.' ); + assert.strictEqual( editor.selection.getNode().textContent, item, 'Cursor remains in the preceding item.' ); + assert.strictEqual( editor.dom.select( '.mce_SELRES_start, .mce_SELRES_end' ).length, 0, 'Bookmarks are removed.' ); + } ); + } ); + + QUnit.test( tag + ': selection between list items (#66379)', function( assert ) { + var start = html.indexOf( 'A' ) + 6, + end = html.indexOf( 'D' ) + 6; + + editor.setContent( html ); + var expected = editor.getContent(); + switchToVisual( html, start, end ); + + assert.strictEqual( editor.getContent(), expected, 'List markup is unchanged.' ); + assert.strictEqual( editor.selection.getContent( { format: 'text' } ).replace( /\s/g, '' ), 'BCD', 'Selected text is preserved.' ); + } ); + } ); + + [ + [ 'Whitespace after an item', '' ], + [ 'Inside a closing tag', '' ], + [ 'Before a closing tag', '' ], + [ 'Nested list', '' ], + [ 'Uppercase tags', '' ], + [ 'Item with attributes', '
    1. A
    2. |
    3. B
    ' ], + [ 'Paragraph control', '

    A

    |

    B

    ' ] + ].forEach( function( testCase ) { + QUnit.test( testCase[0] + ' (#66379)', function( assert ) { + var position = testCase[1].indexOf( '|' ), + html = testCase[1].replace( '|', '' ); + + editor.setContent( html ); + var expected = editor.getContent(); + switchToVisual( html, position ); + + assert.strictEqual( editor.getContent(), expected, 'Content is unchanged.' ); + } ); + } ); +}( jQuery ) );