[wp-trac] [WordPress Trac] #56039: Classic Editor: Current selection when switching Visual to Text is not preserved

WordPress Trac noreply at wordpress.org
Wed Jun 22 11:58:49 UTC 2022


#56039: Classic Editor: Current selection when switching Visual to Text is not
preserved
--------------------------+-----------------------------
 Reporter:  shge          |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Editor        |    Version:
 Severity:  normal        |   Keywords:
  Focuses:  javascript    |
--------------------------+-----------------------------
 Plugin: Classic Editor

 == Problem

 In some cases (when formatting HTML attributes is disabled), the current
 selection when switching Visual to Text editor is not preserved. The
 selected text gets unselected and the editor does not jump to the previous
 selection position.

 This is because the regular expression is not appropriate.

 **Line 774 in `wp-admin/editor.js`**
 The regular expression to find the selection position is:
 {{{#!js
 var startRegex = new RegExp(
   '<span[^>]*\\s*class="mce_SELRES_start"[^>]+>\\s*' + selectionID +
 '[^<]*<\\/span>(\\s*)'
 );
 }}}

 This works well with:
 {{{#!xml
 <span class="mce_SELRES_start" style="display: inline-block; ...">
 }}}

 However, it does not work **when "class" is the last attribute**:
 {{{#!xml
 <span style="display: inline-block; ..." class="mce_SELRES_start">
 }}}

 ----


 == Solution

 Changing `+`(1 or more) to `*`(0 or more) in the regular expression makes
 it work.

 `wp-admin/editor.js`

 **Line 775**
 {{{#!js
 '<span[^>]*\\s*class="mce_SELRES_start"[^>]+>\\s*' + selectionID +
 '[^<]*<\\/span>(\\s*)'
 }}}
 ↓
 {{{#!js
 '<span[^>]*\\s*class="mce_SELRES_start"[^>]*>\\s*' + selectionID +
 '[^<]*<\\/span>(\\s*)'
 }}}

 **Line 779**
 {{{#!js
 '(\\s*)<span[^>]*\\s*class="mce_SELRES_end"[^>]+>\\s*' + selectionID +
 '[^<]*<\\/span>'
 }}}
 ↓
 {{{#!js
 '(\\s*)<span[^>]*\\s*class="mce_SELRES_end"[^>]*>\\s*' + selectionID +
 '[^<]*<\\/span>'
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56039>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list