Opened 2 years ago
Last modified 17 months ago
#56039 new defect (bug)
Classic Editor: Current selection is not preserved when switching Visual to Text editor
Reported by: | shge | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Editor | Keywords: | has-patch |
Focuses: | javascript | Cc: |
Description
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:
var startRegex = new RegExp( '<span[^>]*\\s*class="mce_SELRES_start"[^>]+>\\s*' + selectionID + '[^<]*<\\/span>(\\s*)' );
This works well with:
<span class="mce_SELRES_start" style="display: inline-block; ...">
However, it does not work when "class" is the last attribute:
<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
'<span[^>]*\\s*class="mce_SELRES_start"[^>]+>\\s*' + selectionID + '[^<]*<\\/span>(\\s*)'
↓
'<span[^>]*\\s*class="mce_SELRES_start"[^>]*>\\s*' + selectionID + '[^<]*<\\/span>(\\s*)'
Line 779
'(\\s*)<span[^>]*\\s*class="mce_SELRES_end"[^>]+>\\s*' + selectionID + '[^<]*<\\/span>'
↓
'(\\s*)<span[^>]*\\s*class="mce_SELRES_end"[^>]*>\\s*' + selectionID + '[^<]*<\\/span>'
Attachments (1)
Change History (8)
This ticket was mentioned in PR #2854 on WordPress/wordpress-develop by shge.
2 years ago
#2
#3
@
2 years ago
- Summary changed from Classic Editor: Current selection when switching Visual to Text is not preserved to Classic Editor: Current selection is not preserved when switching Visual to Text editor
#4
follow-up:
↓ 5
@
2 years ago
Did you test this change with other <span>
tags in the content? Is there any way the selection ID could be there without that class?
#5
in reply to:
↑ 4
@
2 years ago
Replying to joyously:
Did you test this change with other
<span>
tags in the content?
Yes. The editor surrounds the current selection with new <span>
tags with mce_SELRES_start
and mce_SELRES_end
classes, so it works well with all tags including <span>
.
Is there any way the selection ID could be there without that class?
No, there isn't.
Also, it should not conflict with existing classes because the selection ID is randomly generated and about 15 digits.
Trac ticket: https://core.trac.wordpress.org/ticket/56039
# 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:
This works well with:
<span class="mce_SELRES_start" style="display: inline-block; ...">
However, it does not work when "class" is the last attribute:
<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.