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> </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.
Comment | File | Size | Author |
---|---|---|---|
#49 | 2876094-39.patch | 3.76 KB | danheisel |
#35 | interdiff-2876094-34_35.txt | 792 bytes | Gauravvvv |
#35 | 2876094-35.patch | 3.91 KB | Gauravvvv |
#34 | 287609-drupal-linking_an_image-34.patch | 3.88 KB | jamesgrobertson |
#30 | after-patch.png | 20.95 KB | BhumikaVarshney |
Issue fork drupal-2876094
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:
Comments
Comment #2
quicksketchMy guess is that this issue may be rooted in CKEditor's code, this portion of the image2 plugin seems suspicious:
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:
Removing the
this.setData('link', CKEDITOR.plugins.image2.getLinkAttributesParser()(editor, this.parts.link));
prevents the problem (but it also prevents the linking entirely).Comment #3
quicksketchSo 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.
Comment #4
quicksketchHere's a version that doesn't break the image button.
Comment #5
quicksketchWe found one more issue where the image save button would not work some of the time. This patch appears to work in all situations.
Comment #13
mrinalini9 CreditAttribution: mrinalini9 at Srijan | A Material+ Company for Drupal India Association commentedRerolled patch to 9.1.x.
Comment #14
ytsurkComment #15
someshver CreditAttribution: someshver as a volunteer and commentedIssue is related to https://www.drupal.org/project/drupal/issues/3131055 and solution is there.
Comment #16
someshver CreditAttribution: someshver as a volunteer and commentedComment #17
AaronMcHaleComment #18
AaronMcHaleComment #19
AaronMcHaleTests needed, as per conversation in #bugsmash on Slack with @lendude.
Comment #20
quietone CreditAttribution: quietone as a volunteer commentedSetting to NW because this needs tests.
Comment #23
henry.odiete CreditAttribution: henry.odiete at Portage CyberTech commentedwhat should the tests be?
Comment #25
henry.odiete CreditAttribution: henry.odiete at Portage CyberTech commentedTest case provided. Issue occurs regardless of whether an image occurs or not. Let me know if more needs to. be done
Comment #26
anmolgoyal74 CreditAttribution: anmolgoyal74 at OpenSense Labs for DrupalFit commentedFixed CS issues.
Comment #28
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedFixed test.
Comment #29
4kant CreditAttribution: 4kant commentededit: sorry - I was in the wrong issue queue ;-)
Comment #30
BhumikaVarshney CreditAttribution: BhumikaVarshney as a volunteer and at OpenSense Labs commentedHi @vsujeetkumar ,
Patch works fine for me.
and also issue is resolved i.e Linking an image creates an empty paragraph tag in CKEditor.\
Thanks.
Comment #34
jamesgrobertson CreditAttribution: jamesgrobertson at Atlantic BT for Red Hat commentedCombined the patches from #13 and #28 and re-rolled for 9.2.x.
Comment #35
Gauravvvv CreditAttribution: Gauravvvv at OpenSense Labs for Drupal India Association commentedRe-rolled patch #34, Attached interdiff for same. Please review
Comment #37
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedCode is not appropriate in ES6, Fixed to code as per the standard, Please have a look.
Comment #38
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedFixed "custom command fail issue".
Comment #40
FiNeX CreditAttribution: FiNeX as a volunteer commentedThans for patch #2876094-38: Linking an image creates an empty paragraph tag in CKEditor. It fixed the problem.
Comment #41
AaronMcHaleSee #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.
Comment #42
sclsweb CreditAttribution: sclsweb commentedPatch 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.)
Comment #43
leisurman CreditAttribution: leisurman commentedPatch 2876094-38.patch does not fix this issue for me either. I still see empty paragraphs with Drupal 9.3.12
Comment #44
leisurman CreditAttribution: leisurman commentedhttps://www.drupal.org/project/drupal/issues/3131055
3131055-17.patch fixes this issue for me with Drupal 9.3.12
Comment #46
Graber CreditAttribution: Graber at Tag1 Consulting commentedQuite 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.
Comment #47
quietone CreditAttribution: quietone at PreviousNext commentedCKEditor has been removed from core, CKEditor 4 is removed from Drupal Core in 10.0.0
Comment #48
thaddeusmt CreditAttribution: thaddeusmt commentedThe 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.
Comment #49
danheisel CreditAttribution: danheisel as a volunteer commentedThe 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.