In #2510380: Images cannot be linked in CKEditor, we added the ability to link images even when using the CKEditor "widget" for image handling. There seems to be some kind of minor bug in the implementation.

Steps to reproduce:

- Create a piece of content.
- Add a single line of text and insert a new line. There is now one paragraph in the source code.
- Insert an image through the image button. Save the dialog.
- Click on the image in the editor.
- Click the link button. Add a URL and save the dialog.

Note at this point, the tag breadcrumb in CKEditor says body > p > p > img. Inspecting the source with your browser indicates that two <p> tags are nested incorrectly. If you click the "Source" button in CKEditor, the paragraph tags become unnested and you end up with code like this:

<p>First paragraph</p>

<p>&nbsp;</p>

<p><img src="..." /></p>

Note this problem does not exist if you have aligned the imaged (center or right). It does still exist if you add a caption.

Issue fork drupal-2876094

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch created an issue. See original summary.

quicksketch’s picture

My guess is that this issue may be rooted in CKEditor's code, this portion of the image2 plugin seems suspicious:

// Once widget was re-created, it may become an inline element without
// block wrapper (i.e. when unaligned, end not captioned). Let's do some
// sort of autoparagraphing here (#10853).
if ( this.widget.inline && !( new CKEDITOR.dom.elementPath( this.widget.wrapper, editable ).block ) ) {
  var block = doc.createElement( editor.activeEnterMode == CKEDITOR.ENTER_P ? 'p' : 'div' );
  block.replace( this.widget.wrapper );
  this.widget.wrapper.move( block );

This is within image2/plugin.js in the inflate() function: https://github.com/ckeditor/ckeditor-dev/blob/ae5d06af6963b1b3fd7ef93c88...

BUT: This issue does not present itself in CKEditor's widget demo. Only in the Drupal implementation is this problem exhibited. Meaning it's likely we can fix it within our plugin code.

Within our code base, here's the line that triggers the problem:

      var originalInit = widgetDefinition.init;
      widgetDefinition.init = function () {
        originalInit.call(this);
        // Update data.link object with attributes if the link has been
        // discovered.
        // @see plugins/image2/plugin.js/init() in CKEditor; this is similar.
        if (this.parts.link) {
          this.setData('link', CKEDITOR.plugins.image2.getLinkAttributesParser()(editor, this.parts.link));
        }
      };

Removing the this.setData('link', CKEDITOR.plugins.image2.getLinkAttributesParser()(editor, this.parts.link)); prevents the problem (but it also prevents the linking entirely).

quicksketch’s picture

Status: Active » Needs review
FileSize
1.03 KB

So it appears as though this is some weird side-effect of the browser caret location (I think). If the caret is in a contenteditable paragraph, removing that paragraph doesn't seem to work. So what appears to happen is that the parent <p> tag isn't removed, and then another child <p> tag is created. You can't have nested paragraph tags in HTML though, so what ends up happening is the browser then tries to separate the two paragraph tags to make the HTML valid.

The apparent fix is to focus the widget element after it has been wrapped in the anchor tag. With the selection moved to the widget, the previous paragraph tag can be removed (and then re-added) by CKEditor's widget handling. Previously the caret would end up *before* the image (in the old paragraph tag), making the user advance it a few times before they could start typing again. This fix has the added benefit of putting the cursor in a better location. And after linking an image the "Unlink" button immediately highlights, indicating that a link as been created.

quicksketch’s picture

Status: Needs review » Active
FileSize
1.15 KB

Here's a version that doesn't break the image button.

quicksketch’s picture

Status: Active » Needs review
FileSize
1.05 KB

We found one more issue where the image save button would not work some of the time. This patch appears to work in all situations.

Status: Needs review » Needs work

The last submitted patch, 5: drupal-ckeditor_widget_focus-2876094.patch, failed testing.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mrinalini9’s picture

Status: Needs work » Needs review
FileSize
1020 bytes

Rerolled patch to 9.1.x.

ytsurk’s picture

someshver’s picture

Issue is related to https://www.drupal.org/project/drupal/issues/3131055 and solution is there.

someshver’s picture

Issue tags: +https://www.drupal.org/project/drupal/issues/3131055
AaronMcHale’s picture

Issue tags: -https://www.drupal.org/project/drupal/issues/3131055
AaronMcHale’s picture

AaronMcHale’s picture

Issue tags: +Needs tests

Tests needed, as per conversation in #bugsmash on Slack with @lendude.

quietone’s picture

Status: Needs review » Needs work

Setting to NW because this needs tests.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

henry.odiete made their first commit to this issue’s fork.

henry.odiete’s picture

what should the tests be?

henry.odiete’s picture

Test case provided. Issue occurs regardless of whether an image occurs or not. Let me know if more needs to. be done

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
1.84 KB
2.89 KB

Fixed CS issues.

Status: Needs review » Needs work

The last submitted patch, 26: 2876094-linking-an-image-25.patch, failed testing. View results

vsujeetkumar’s picture

Status: Needs work » Needs review
FileSize
1.84 KB
1.78 KB

Fixed test.

4kant’s picture

edit: sorry - I was in the wrong issue queue ;-)

BhumikaVarshney’s picture

FileSize
20.95 KB

Hi @vsujeetkumar ,
Patch works fine for me.
and also issue is resolved i.e Linking an image creates an empty paragraph tag in CKEditor.\
Thanks.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jamesgrobertson made their first commit to this issue’s fork.

jamesgrobertson’s picture

Combined the patches from #13 and #28 and re-rolled for 9.2.x.

Gauravvvv’s picture

Re-rolled patch #34, Attached interdiff for same. Please review

Status: Needs review » Needs work

The last submitted patch, 35: 2876094-35.patch, failed testing. View results

vsujeetkumar’s picture

Status: Needs work » Needs review
FileSize
3.69 KB
2 KB

Code is not appropriate in ES6, Fixed to code as per the standard, Please have a look.

vsujeetkumar’s picture

Fixed "custom command fail issue".

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

FiNeX’s picture

AaronMcHale’s picture

See #3131055-26: Wrapping drupal-media image in a link produces empty P tags after image for some steps which may fix this problem from Drupal 9.4/10.0 onwards by using the new CKEditor 5.

I used similar steps to the ones I documented in that comment and was not able to reproduce this issue using the new CKEditor 5. So unless this issue is resolved before the release of 9.4/10.0 perhaps this could be closed as a won't fix.

sclsweb’s picture

Patch 2876094-38.patch does not fix this issue for me -- still getting empty paragraphs with Drupal 9.3.9.

In practical terms, is there a workaround for this? Putting a link on an image and aligning the image left of some text is a very common use case for the users I support, and using CKEditor 5 doesn't seem to be an option at this point. (Multiple fields using CKEditor 5 on the same node caused data loss on every field except the first.)

leisurman’s picture

Patch 2876094-38.patch does not fix this issue for me either. I still see empty paragraphs with Drupal 9.3.12

leisurman’s picture

https://www.drupal.org/project/drupal/issues/3131055
3131055-17.patch fixes this issue for me with Drupal 9.3.12

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Graber’s picture

Quite a long issue, checked the patch and MR and I just fixed an identical issue on entity_embed in a much more simple way, linking it here, maybe it'll help, although not sure if that's the right approach.

quietone’s picture

Project: Drupal core » CKEditor 4 - WYSIWYG HTML editor
Version: 9.5.x-dev » 1.0.x-dev
Component: ckeditor.module » Code

CKEditor has been removed from core, CKEditor 4 is removed from Drupal Core in 10.0.0

thaddeusmt’s picture

The patch for the entity_embed extension proposed in #3282295: Additional paragraphs added when embedding an entity in CKEditor and linking it by @Graber (a ckeditor config option, actually) seems to be working for me.

danheisel’s picture

The move to 9.5.x brought this issue back for us since CKEditor 4 was moved from core to contrib. So, we had to make a new version specific to Drupal 9.5.x and CKEditor 4 as contrib.

This patch is the same as the one provided here, https://www.drupal.org/project/drupal/issues/3131055, just applied to the contrib CKEditor module.

Status: Needs review » Needs work

The last submitted patch, 49: 2876094-39.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.