Skip to content

Commit a89df97

Browse files
Philipp Kitzbergergarvinhicking
authored andcommitted
[BUGFIX] Prevent CKEditor link splitting on partial selection edit
When editing an existing link and selecting only part of the link text (e.g., selecting "word" in "multi word link"), changing the link target would split the link into multiple separate <a> elements. This change automatically expands the selection to encompass the entire link when: - The user clicks the link button in the toolbar while positioned inside or partially selecting a link - The link browser is opened for an existing link The expandSelectionToFullLink() method detects if the selection is within a link and uses findAttributeRange() to expand it to cover the complete link element before any modifications are made. This prevents unwanted link fragmentation and ensures a consistent editing experience. Resolves: #108331 Releases: main, 13.4 Change-Id: Ida981a20db6171e975b057017ef9540f98f98318 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/91897 Tested-by: Garvin Hicking <[email protected]> Reviewed-by: Garvin Hicking <[email protected]> Tested-by: core-ci <[email protected]> Reviewed-by: Andreas Nedbal <[email protected]> Tested-by: Andreas Nedbal <[email protected]>
1 parent d218bf6 commit a89df97

File tree

2 files changed

+38
-2
lines changed
  • Build/Sources/TypeScript/rte-ckeditor/plugin
  • typo3/sysext/rte_ckeditor/Resources/Public/JavaScript/plugin

2 files changed

+38
-2
lines changed

Build/Sources/TypeScript/rte-ckeditor/plugin/typo3-link.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,14 @@ export class Typo3LinkUI extends Core.Plugin {
571571
}
572572

573573
private showUI(): void {
574-
if (!this.getSelectedLinkElement()) {
574+
const selectedLink = this.getSelectedLinkElement();
575+
if (!selectedLink) {
575576
this.showFakeVisualSelection();
576577
this.openLinkBrowser(this.editor);
577578
} else {
579+
// When clicking inside a link, check if we need to expand selection to full link
580+
// This prevents link splitting when editing with partial selection
581+
this.expandSelectionToFullLink();
578582
this.addToolbarView();
579583
this.balloon.showStack('main');
580584
}
@@ -736,11 +740,43 @@ export class Typo3LinkUI extends Core.Plugin {
736740
return position.getAncestors().find((ancestor: any) => LinkUtils.isLinkElement(ancestor));
737741
}
738742

743+
/**
744+
* Expands the current selection to encompass the entire link element.
745+
* This prevents link splitting when editing a link with partial text selection.
746+
*/
747+
private expandSelectionToFullLink(): void {
748+
const model = this.editor.model;
749+
const selection = model.document.selection;
750+
751+
// Only expand if we have a link attribute in the selection
752+
if (!selection.hasAttribute('linkHref')) {
753+
return;
754+
}
755+
756+
const linkHref = selection.getAttribute('linkHref') as string;
757+
if (!linkHref) {
758+
return;
759+
}
760+
761+
model.change(writer => {
762+
const position = selection.getFirstPosition();
763+
// Find the full range of the link using the linkHref attribute
764+
const linkRange = Typing.findAttributeRange(position, 'linkHref', linkHref, model);
765+
766+
if (linkRange) {
767+
// Expand selection to cover the entire link
768+
writer.setSelection(linkRange);
769+
}
770+
});
771+
}
772+
739773
private openLinkBrowser(editor: Core.Editor): void {
740774
const linkCommand = editor.commands.get('link') as unknown as Typo3LinkCommand;
741775
let additionalParameters = '';
742776

777+
// If editing an existing link, expand selection to full link first
743778
if (linkCommand.value) {
779+
this.expandSelectionToFullLink();
744780
additionalParameters += '&P[curUrl][url]=' + encodeURIComponent(linkCommand.value);
745781
for (const [attr, value] of Object.entries(linkCommand.attrs)) {
746782
additionalParameters += '&P[curUrl][' + encodeURIComponent(attr) + ']=' + encodeURIComponent(value);

0 commit comments

Comments
 (0)