Problem/Motivation
config.image.resizeUnit to '%' not working in the backend editor after changes to images were made and saved.
Front end working as expected, but when going back to the editor to make more updates, images sizes not showing properly.
When inspecting element image, style width has both % and px added to the width number.

Steps to reproduce
1. add image
2. resize image
3. save
4. check in the front end. It should look good
5. go back to the editor. Image is not shown in the correct size. when inspected: style="width:50%px;"
6. Image in the front end saved and shown correctly style="width:50%;"
7. problem only in the editor. in the backend
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #43 | 3249592-43-d10.patch | 22.04 KB | lauriii |
| #24 | Screen Shot 2022-01-20 at 5.39.59 PM.png | 294.82 KB | hooroomoo |
| #11 | ckeditor5_resize_unit_issue_3249592.patch | 19.5 KB | vlyalko |
| Screen Shot 2021-11-15 at 6.50.26 PM.png | 98.95 KB | vlyalko |
Issue fork drupal-3249592
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
vlyalko commentedComment #4
wim leers@lauriii responded in the
#ckeditor5Drupal Slack channel, where you asked aobut this. He thinks it's it's related to https://git.drupalcode.org/project/drupal/-/blob/9.3.x/core/modules/cked... — credited him for that.This would be consistent with the steps to reproduce you posted: it only happens if you start editing already existing content.
I understand this blocks #3224652: [drupalImage] Add ckeditor5-image's imageresize plugin to allow image resizing, or at least your use of it in #3247974: [drupalImage] Image resize, Image with text wrapped shown correctly on front end and not correctly shown in backend. So tagging .
This will need tests.
Since this could be considered data loss, marking critical. I hope to start working on this tomorrow (today is already a very full day). So, self-assigning, but if somebody starts working on this before 09:00 CEST Nov 18 2021, feel free to assign to yourself.
Comment #5
wim leersThat code was introduced in #3222847: <img width height>.
Comment #6
vlyalko commented@wim-leers, This is what i changed in js/ckeditor5_plugins/drupalImage/src/drupalimageediting.js.
I compiled it and image resize is working with resizeUnit set to either '%' or 'px'
Block of code stats at line: #468
Comment #8
vlyalko commentedComment #9
andypostyou need to fix .es6 files as https://www.drupal.org/about/core/policies/core-change-policies/frontend... and .js files will be genareated
Comment #10
vlyalko commentedComment #11
vlyalko commentedThis patch is for the ckeditor5 contrib module only https://www.drupal.org/project/ckeditor5.
Comment #13
wim leersComment #16
hooroomooApplied changes by vlyalko onto a new 9.4.x MR
Comment #17
wim leersVery nice of you to respect the commit credit in the merge request, @hooroomoo! 🤩👏❤️
Comment #18
wim leersLooks like I reviewed the wrong MR 😅 🙈
None of the review comments between #16 and #17 are relevant. Reviewed the correct MR now!
Comment #19
hooroomoo@Wim Leers @lauriii How do you resize an image without the plugin? Is it something with HTML tags? I originally thought to change the style attribute directly inside of my test but realized later that's probably not the way to do it.
Comment #20
wim leersThis should work:
<img width>to for examplewidth="33%".Your width of 33% should persist..
Comment #21
hooroomooremoved needs test tag
Comment #22
hooroomooThe test was passing for me, then I removed the two image dependencies that I had originally added and saw now that the test failed.
I added back the image.imageResize dependency because the test seems to fail without it. The element doesn't get the width attribute that is added inside the Source so the size that shows up in the editor is wrong.
Not sure if that makes this blocked until the dependency is added in #3224652: [drupalImage] Add ckeditor5-image's imageresize plugin to allow image resizing.
Comment #23
wim leersHm … I'd like to understand why it fails. What did your digging unveil? Where/how does the
widthattribute value you manually specified get lost? Is it in CKEditor 5's code or is it in Drupal'sDrupalImageCKEditor 5 plugin (incore/modules/ckeditor5/js/ckeditor5_plugins/drupalImage/*)? 🤔We may need to block this on #3224652: [drupalImage] Add ckeditor5-image's imageresize plugin to allow image resizing. But even then we'd want to understand why we need that additional plugin for this to work correctly.
So either way, the investigation/digging that I mentioned at the beginning of this comment is necessary IMHO.
Comment #24
hooroomoo1. So I tried changing the width through source editing on CKE5's demo page and confirmed that it doesn't resize the image there either so I think it's safe to assume it's not because of Drupal's DrupalImage.
2. Then I found these lines of code 116-118 in CKE5's resize plugin that looks it is what's setting the width to the figure from the image.
3. Also I am including this comment I found from someone who works on CKEditor 5 which makes it sound like the ImageResize plugin is necessary for the figure style to also be updated. (Also Wim I was pleasantly surprised to see your avatar right above)
So this is what I was able to find in my investigation. I am not 100% confident but my guess is that the width attribute manually specified doesn't bubble up to the figure element without the resize plugin that sets the styling for width.
Comment #25
wim leersAlso just found https://github.com/ckeditor/ckeditor5/issues/10838, which seems to further confirm this is a limitation of CKE5's
Imageplugin.Comment #26
lauriiiThis is something that we tried to work around in our code but I'm not sure we have test coverage for that. That's what the width/height upcast and downcast are supposed to solve because I think earlier we decided that we should preserve width/height attributes even if the resize plugin isn't enabled. So I think we should come up with a bug fix and test coverage regardless of what happens with #3224652: [drupalImage] Add ckeditor5-image's imageresize plugin to allow image resizing.
Comment #27
wim leersI think @lauriii's comment confirms that this is already Drupal-owned/specific, which means the fix for this can also be Drupal-specific. So this is not blocked on #3224652: [drupalImage] Add ckeditor5-image's imageresize plugin to allow image resizing.
@lauriii, could you maybe leave some pointers for @hooroomoo to dive into
core/modules/ckeditor5/js/ckeditor5_plugins/drupalImage/*?Comment #28
wim leersPer #26 and #27.
Comment #29
hooroomooComment #30
hooroomooData is upcasted properly now with the expected units but the image itself does not resize. Created another issue #3260479: [drupalImage] <img width> not visible in editing view (because only for data downcast) since it is out of scope for this specific issue.
Comment #31
wim leersReviewed. This is definitely on the right track, but still has a problem — which is super easy to spot now thanks to the test coverage @hooroomoo wrote! 👍😄
Thanks also for opening #3260479: [drupalImage] <img width> not visible in editing view (because only for data downcast) :)
Finally … in my review on the merge request I pointed to https://html.spec.whatwg.org/multipage/embedded-content-other.html#attr-... … but curiously, that did not mention relative units. Turns out that the HTML spec does not allow specifying units in there. Technically
<img width="33%">is therefore invalid HTML. But, of course, HTML being HTML … there's an exception: in HTML 4.01 this used to be valid: https://www.w3.org/TR/html401/struct/objects.html#h-13.7.1Since we don't want to break pre-existing content, we do have to support this scenario. Retitling for clarity
Comment #32
lauriiiIt seems like CKEditor is outputting the px unit when the resize unit is set to px. I guess that's something that we should report upstream, and is the likely reason why we implemented this the way it is.
I checked what CKEditor 5 expects for the module attribute, and it expects that the unit is always set. So what we want to do is ensure on upcast that width and height with px unit, include px when set to the model attribute. However, that px unit should be removed when downcasting the data.
Comment #33
wim leers#32:
And I bet it's because they use
style… and not even on<img>, but on the wrapping<figure>😅. That's also what https://ckeditor.com/docs/ckeditor5/latest/features/images/images-resizi... shows.Worth bringing up in tomorrow's meeting with CKEditor 5 folks?
But definitely out of scope here! Tagging for that new issue, not yet creating it so we can include their upstream commentary/guidance right from the start 👍
Comment #34
lauriiiBased on #33, I don't think there's problem with the upstream behavior. I think it's fine for them to output the value in
styleattribute on the<figure>element. Removing the needs followup tag based on that.Comment #35
lauriiiComment #36
wim leersI pushed 8320cf55, 0c085d8f, b4861511 and 8d74730f to fix nits.
The only actual change I made is 31d1db8c, which just simplifies the test.
All of the actual work here was done by @hooroomoo. Hence I feel comfortable RTBC'ing this :)
Comment #37
lauriiiComment #38
wim leersOohhhh … really good point, @lauriii! I totally forgot about that.
After you do that, @hooroomoo, this'll be RTBC again :)
Comment #39
hooroomooComment #40
wim leersComment #41
hooroomooComment #42
wim leers🚢
Comment #43
lauriiiComment #46
lauriiiCommitted 27af96d and pushed to 10.0.x. Also committed the MR to 9.4.x Thanks!
Leaving open for 9.3.x backport once the code freeze has ended.
Comment #49
lauriiiCherry-picked to 9.3.x.