quill-image-resize-module: Image align styles not being retained when they are already set
I cannot get the image styles that are set with the align to be kept when re-displaying the image inside a quill editor.
Use case:
- Add and right align an image in the editor.
- Save the editor innerHTML in a database
- Redisplay the edited HTML in a new quill editor to allow user to update the HTML
Expected: image in the new editor should still be aligned right. Actual: image in new editor has had all the style attributes removed
I’ve updated the demo Plunk to show the issue. In this example, I’ve added the align style attributes to the first image in the HTML (style="display: inline; float: right; margin: 0px 0px 1em 1em;"
). The editor has removed them and the image is not being aligned right.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 24
- Comments: 28
@bdurand , you’re absolutely right! thanks!
here’s my solution:
Have you tried adding
'width
’ to theformats: [...]
option?Solution for Angular app (and other typeScript one), heavily inspired by xeger, MaximusBaton comments
What I do to fix it is rewriting the image format. In order to sanitize style preventing from attacks such as XSS Or unexpected style, I also add white list for style. Environment: React-quill
Like this:
@bogdaniel Had to monkeypatch the imageformat.
had an issue with creating a new Format so went down this route for a quickfix.
Preserving Image Styles In Quill Deltas
I have a working example in Codesandbox if this is TL;DR.
If you want to preserve image styles during Delta⇄HTML conversion, you must do three things:
Image
blot so it can apply the new formats to the HTML DOMwidth
,height
andfloat
with the Parchment registry with the proper scope (INLINE_BLOT
orBLOCK_BLOT
depending on the superclass of your customImage
)width
,height
andfloat
to the list of allowed formats when you instantiate your Quill editorIf you only work with Quill HTML and never with delta, you can skip all of the above. If you only extend
Image
, then itsstatic formats
will allow the new attributes to appear in deltas, but the formats won’t be applied to the DOM. To achieve that final step of applying the formats to HTML DOM when processing a delta, the formats must be registered and allowed.Some subtleties that you also need to pay attention to:
Quill.import
to get theImage
blot, orParchment
or other Quill primitives. If youimport
them directly via ES6 then you will probably not get the right objects!Image
, it is better to import it, extend it with a derived class that callssuper
, and then re-registerformats/image
your derived class. This minimizes the code you write and preserves other customizations from Quill modules.Feel free to contact me with any questions; I’m not a Quill expert, but after 20+ hours of debugging this issue I have a certain level of understanding!
I figured out the core issue is that there is a whitelist of allowed attributes on the image tag and style is not one of them. See https://github.com/quilljs/quill/issues/1556
@vbobroff-app @MidaAiZ I applied this solution but still the problem is arising. Image resizing and alignment that applied with style, is sanitized if I go to edit mode. How can I solve this?
Code which I applied
// Resize module start
I’ve tried everything recommended in this thread but no matter how I override, extend or register ImageFormat, the overridden
format(name, value)
is never entered. I can break intostatic formats(domNode)
and style is being recognized and added to the returnedformats
but passing html with img tags containing style attributes are still stripped of their attributes and then sent through onEditorChange.My most recent attempt was to remove ‘image’ from formats and replace it with my custom blot… No luck
Upon setting editor value, it strips attributes.
If anyone can point me in the right direction, I’d really appreciate it
FYI CodePen with a working example using @MaximusBaton and @bdurand’s work