Problem/Motivation
In ckeditor5 the resizeImage is using '%' now and before it was 'px'. This causes the <figcaption> element to push the container box and not match the image width.
Steps to reproduce
MVC: https://jsfiddle.net/jhuzk2tm/
Screenshot for posterity:

<figure class="caption caption-img align-left">
<img alt="Broken with %" src="https://placehold.co/6000x4000" width="46.52%" height="400">
<figcaption>Align left and wrap text - Broken!</figcaption>
</figure>
<figure class="caption caption-img align-left">
<img alt="Works with px" src="https://placehold.co/6000x4000" width="400px" height="400">
<figcaption>Align left and wrap text - Works!</figcaption>
</figure>
<style>
/* Responsive */
img {
max-width: 100%;
height: auto;
}
/* Color to demo */
figure {
background: pink;
}
figcaption {
background: hotpink;
padding: 5px;
}
/**
* @file
* Alignment classes for text and block level elements.
*/
/**
* Alignment classes for block level elements (images, videos, blockquotes, etc.)
*/
.align-left {
float: left;
}
/**
* @file
* Caption filter: default styling for displaying image captions.
*/
/**
* Essentials, based on http://stackoverflow.com/a/13363408.
*/
.caption {
display: table;
}
.caption > * {
display: block;
max-width: 100%;
}
.caption > figcaption {
display: table-caption;
max-width: none;
caption-side: bottom;
}
</style>
Proposed resolution
Change resizeImage to 'px'
OR
If tenable, try to get the px off the image and onto the figure element. From @witeksocha in comment #3348603-12: CKEditor 5 resizes images with % width instead of px width (the CKEditor 4 default): breaks image captions *and* is a regression:
As for the example presented above, the markup is not correct for the figure+figcaption. The width needs to be moved to the
<figure>. The styling we use to make it work can be found here: https://ckeditor.com/docs/ckeditor5/latest/installation/advanced/content...
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #34 | 3348603-32.patch | 478 bytes | lauriii |
| #28 | 3348603-28.patch | 1.64 KB | lauriii |
| #27 | pct-to-px.gif | 811.88 KB | wim leers |
| #23 | 3348603-23.patch | 5.97 KB | lauriii |
| #19 | Screen Shot 2023-03-28 at 15.36.05-fullpage.png | 8.85 MB | joelpittet |
Issue fork drupal-3348603
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:
- 3348603-image-resize-with
changes, plain diff MR !3671
Comments
Comment #3
joelpittetThis is related to #3224652: [drupalImage] Add ckeditor5-image's imageresize plugin to allow image resizing Pulled from
git annotate.Comment #4
joelpittetTo add to the rationale for this change. When does anybody want a photo to be a percent of the container? I could be wrong but I've never had this use-case. Might look good on desktop, but mobile it's a nightmare.
And with percentages,
image_resize_filterdoesn't know what to do. #3348605: CKEditor 5 compatibilityComment #5
joelpittetThe figcaption is not the only thing broken here. If you remove it and add figure around an image without a caption that is broken as well:
See fiddle

https://jsfiddle.net/fuaLqp5m/1/
Screenshot for posterity:
Comment #6
wim leersThis is very strongly connected to #3321296: [upstream] CKEditor 5 breaks website CLS (no image width and height attributes).
Comment #7
wim leers… and even more so to #3336446: EditorFileReference should compute a px <img height> if a % <img width> is specified, even though % <img width> is not allowed in HTML5.
Comment #8
wim leersIIRC CKEditor 5 intentionally chose this, I'll see if I can dig up their research/rationale.
Meanwhile, given @lauriii's expertise on Drupal's CKEditor 5 image integration, assigning to him 🤓
Comment #9
smustgrave commentedConfirmed MR3671 fixed the issue.
Not sure if this could get a test case or if it's possible
Comment #10
catchI think it would be good to have a reference to the ckeditor5 history (link if it's documented, or a summary from the ckeditor5 team if it's not) if we can, so it's easy for anyone wondering why we diverged later to see why we made the decision and not try to revert it back.
Comment #11
wim leersNote that it really was intentional of CKEditor 5 to use
%instead ofpxby default. See https://ckeditor.com/docs/ckeditor5/latest/features/images/images-resizi....Resizing to
pxmatches CKEditor 4's behavior.I've asked them.
AFAICT it was proposed by @Reinmar at https://github.com/ckeditor/ckeditor5/issues/5167#issuecomment-519087175 and decided at https://github.com/ckeditor/ckeditor5/issues/5167#issuecomment-522489212.
The rationale is that this results in responsively sized images by default. But … in Drupal, the CKEditor 5 instance runs in a different theme than the front-end, which I suspect is atypical for most CKEditor 5 users. So I think I agree with this change 😅
Comment #12
witeksochaHey everyone, happy to share some context from the CKEditor 5 side.
A lot of people may not know the exact size of the container in which the content will be used. Often, content is used in multiple destinations at once. With pixels, one gets far less flexibility with how the content will be used in the future.
As for the example presented above, the markup is not correct for the figure+figcaption. The width needs to be moved to the
<figure>. The styling we use to make it work can be found here: https://ckeditor.com/docs/ckeditor5/latest/installation/advanced/content.... Unfortunately, it's quite complex as HTML/CSS poses challenges.Comment #13
catchOne thing from https://github.com/ckeditor/ckeditor5/issues/5167#issuecomment-519087175 I'm not sure I agree with:
A lot of responsive designs will show an image half-width on wide screens, but then make it full width when the screen is narrow, with any captions or etc. moved underneath instead of to the side. That way the image stays close to the same size (or at least closer) while the layout adapts around it. But... I don't know how this relates to the other image placement options.
Comment #14
joelpittet100% agreement with @catch in #13 as it embiggens my point :D Though I'd like to be open to the possibility that this was taken into consideration already in their discussions, and maybe they have a proposal that's better than "the way it was done for decades" perspective that I'm taking.
Thanks very much for posting here @witeksocha and welcome to the Drupal community!
Comment #15
joelpittetI'll add point in #12 to the issue summary about moving the pixels to the figure element.
— @witeksocha in comment #3348603-12: CKEditor 5 resizes images with % width instead of px width (the CKEditor 4 default): breaks image captions *and* is a regression
Comment #16
lauriiiImplemented the proposed solution from #12 for people to test.
Comment #18
joelpittet@lauriii, thanks for the patch, I tried it and for some reason it didn't work on my captions at all (no width on figure element) and without caption it was on the image.
% version from #16
px version from MR 3671
I resized it in ckeditor and copied the source changes on one image to all the variations for this manual test.
Comment #19
joelpittetSorry @lauriii, I should have realized that was only on Olivero theme because of the template change. This will change will be tricky because of the template change that would be needed for Classy/Stable/Stable9/Starterkit/Bartik/Claro/Seven/Umami...
Here's the same test but with that theme, the images without caption suffer from the same problem as above.
Comment #20
joelpittet@lauriii also We need to grant the
styleattribute to the filtered attributes forfigure.I'm more convinced my MR request is the way to go here...
Comment #21
joelpittetAlso we've deemed
styleattribute on afigurea security risk:Comment #22
lauriii#19: I'm not sure I understand what is the problem there? It looks to me that the % version from #16 renders as it was intended to 🤔
#20: Are you running the "Limit allowed HTML tags and correct faulty HTML" after "Caption images" filter?
#21 is a good point. There are certainly some challenges even with this use of the
styleattribute because it could enable XSS through thewidthandheightattributes 🤔Comment #23
lauriiiHere's an attempt to add BC layer for the concerns raised in #19.
Comment #25
lauriiiDiscussed this with @joelpittet in detail.
The potential solutions we discussed:
<figure>(#23)@joelpittet confirmed that approaches 1 and 2 solves the original problems. The discrepancies between the screenshots in #19 were due to an incorrect alignment being set on one of the images.
I'm not entirely convinced that using percentages is more intuitive for content editors. For instance, a 50% width setting may be small on mobile but quite large on desktop. Users are generally familiar with static width units like inches, even if px isn't a perfect measure.
Solution 2 has potential XSS concerns since it involves the
styleattribute. Although we can manage this, it requires conscious effort.Percentage values aren't necessary for responsive design. The
widthattribute can be used for wider screens, whilemax-widthensures responsiveness.From the user perspective it seems that px is the way to go. My main concern with px units is their potential impact on backwards compatibility. I think the ideal solution would be that existing
widthvalues remain unchanged until the image is resized again.Comment #26
joelpittetThanks @lauriii,
To confirm RE #22 That in #20 you are right the order of the filters were in the wrong order, and yes one of the alignments was incorrect. Take the screenshots with a grain of incompetence on my part;)
And a note if we continue down the
%exploration that we need to removeheighton the<figure>because with captions that is just wrong.Yes, both solutions will solve my original issue around the Figure captions not respecting the image width when percents are used.
Re BC layer, currently we have the same BC regression that the
pxMR will produce. Content created/edited before the upgradig to ckeditor5 are usingpx, only modifying that content after will change them to%This is a note to @Wim Leers as I'm sure @lauriii will bring it up with you this week.Comment #27
wim leers@catch in #13 and @joelpittet in #14: Yep, that's because they have fundamentally different assumptions for the default environment CKEditor 5 will run in. See the last paragraph I wrote in #11.
#16 – #23 are implementing and testing what @witeksocha described in #12 what Drupal would need to change to make image captions work correctly with resized images.
#25 summarized the whole issue and potential choices to make, he proposed a plan.
I just discussed #25 in detail with @lauriii. I'm convinced his proposal to switch to
px(like @joelpittet's original MR does) is the way to go 👍Also 100% agreed with @joelpittet in #26 that this is simply an oversight on our part when we brought CKEditor 5 to Drupal core: we didn't sufficiently question whether the shift from % to px was a problem or not. Sorry. 🫣
My only remaining concern: making sure that editing content that is using
<img width="34%">actually gets converted to a pixel value AND that it's not a jarring experience. A simple experiment proves that it works great, out of the box:🤩
… but ideally we'd have functional JS test coverage that proves that that continues to work fine in future versions of CKEditor 5. If they turn out to be possible to write, because we know that anything drag-and-drop related is extremely difficult to simulate with
chromedriver-powered tests 😬 @lauriii will attempt to write such a test next week 🥳Comment #28
lauriiiI tried to write tests for this and it kind of works (assertions pass) but for some reason CKEditor is throwing an error 😬
Comment #29
joelpittet@laurii, what's the error CKEditor is throwing and where?
Comment #30
lauriiiThe error I'm getting when running
\Drupal\Tests\ckeditor5\FunctionalJavascript\ImageTest:testWidthis:However, this is specific to the test. For some reason it also looks like this is not happening on DrupalCI 🤯
Comment #31
wim leersReasons it may not be happening on DrupalCI:
Could you try eliminating those, @lauriii?
Comment #32
catchDiscussed briefly with @lauriii in slack and we both think we should do #28 minus the test coverage, and open a follow-up to add it, so that this can definitely land in 10.1
Comment #33
lauriiiI think we should try to get this bug fix in 10.1.x. More content editors will likely run into this and this isn't something we could fix in a patch release. Here's a patch without the test coverage. We still need to write a CR for the change.
Comment #34
lauriiiAnd here's the patch 😇
Comment #35
lauriiiAdded a draft CR.
Comment #36
catchTagging for follow-up to add the tests, although if it's hard to write a test that passes, we also might just want to skip that.
Comment #37
wim leers+1 for commit without test if test infra is getting in the way — this has been thoroughly manually tested!
Comment #38
lauriiiOpened new issue for the tests: #3362462: Add tests for resizing images in CKEditor 5.
Comment #41
lauriiiCommitted 30a54c9 and pushed to 11.x. Also cherry-picked to 10.1.x. Thanks!