-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow built-in image paste handling to be disabled (#4874). #4896
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @FlowIT-JIT, and thanks for your contribution! There are however some issues – please see my inline comments for more info.
Additionally, it will be good to add some unit tests – to check if setting this option to false
disables built-in handling of pasting images and if the paste
event is no longer stopped. Could you add such tests?
plugins/clipboard/plugin.js
Outdated
@@ -149,7 +149,7 @@ | |||
// Convert image file (if present) to base64 string for modern browsers except IE<10, as it does not support | |||
// custom MIME types in clipboard (#4612). | |||
// Do it as the first step as the conversion is asynchronous and should hold all further paste processing. | |||
if ( CKEDITOR.plugins.clipboard.isCustomDataTypesSupported || CKEDITOR.plugins.clipboard.isFileApiSupported ) { | |||
if ( ( CKEDITOR.plugins.clipboard.isCustomDataTypesSupported || CKEDITOR.plugins.clipboard.isFileApiSupported ) && editor.config.clipboard_handleImagePasting !== false ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition became too long and therefore unreadable. Could you extract its first part to some variable (e.g. isImagePastedSupported
)?
Additionally editor.config.clipboard_handleImagePasting
alone should be enough to check if the plugin is supposed to handle image pasting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just committet a change that shortens the code by extracting the first part of the condition into an isImagePasteSupported
variable as suggested. I'm not too crazy about the idea having only clipboard_handleImagePasting determine whether to enable image handling, since it will allow a developer to enable the feature on unsupported browsers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too crazy about the idea having only clipboard_handleImagePasting determine whether to enable image handling, since it will allow a developer to enable the feature on unsupported browsers.
Actually, I meant something different, but I see that my message could be ambiguous. I thought about shortening editor.config.clipboard_handleImagePasting !== false
to editor.config.clipboard_handleImagePasting
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, okay 😅😊 Got it!
|
||
1. Paste an image into the editor. | ||
|
||
Observe that the image is inserted and that no "Custom image paste handling" entry is written to "log output" on the page. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd split it into two tests: the one that will test the plugin with handling image pasting and one without it. It should be more readable than putting both of these checks inside one test. Could you split the test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added unit tests to cover scenarios where image paste handling has been disabled using the new clipboard_handleImagePasting
flag - see tests/plugins/clipboard/pasteimagedisabled.js.
Native image paste handling (when enabled) is already covered by tests/plugins/clipboard/pasteimage.js.
Do you still want manual test coverage, or should I just remove it again ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, worth mentioning; I took the liberty to move some code from tests/plugins/clipboard/pasteimage.js into tests/plugins/clipboard/_helpers/pasting.js to reduce duplicate code. I needed the exact same code for the new unit test in tests/plugins/clipboard/pasteimagedisabled.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you still want manual test coverage, or should I just remove it again ?
It's always better to have an additional manual test. For me manual tests are for people and unit ones – for machines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I'll refacter the manual test as you suggested. Thanks 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There. The new manual test (tests/plugins/clipboard/manual/pasteimagedisable.md) is now only about testing the new clipboard_handleImagePasting
option, as support for image pasting is already covered with tests/plugins/clipboard/manual/pasteimage.md.
@Comandeer Thank you for reviewing my PR. I have made some adjustments based on your feedback. Please see my comments to your review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx for the updates!
Your changes work correctly, but it seems that some additional tests will be needed, as the new config variable also disables dragging and dropping images. Could you add a manual test and unit test for this case? You can probably reuse some code from dropimage.js
test. I'm sorry for not noticing it earlier.
It'd be also good to add a unit test for the original case, so checking if some custom paste handler can receive paste
data after disabling image handling. Could you also add such a test?
Thank you for additional feedback. I should probably have noticed drag and drop support myself - sorry about that. I will look into extending test coverage as suggested. |
I have now added a unit test and manual test to cover drag and drop. Furthermore I renamed the new option from Please note that I once again had to do a little refactoring to reduce duplicate code for the new unit test covering image dropping. I moved code from Prior to this change the cleanup was - probably not entirely logical nor correct - performed in setUp(..) as shown below: |
This will allow 3rd party plugins to handle images being pasted.
Also moving code from pasteimage.js into _helpers/pasting.js to allow reuse in new unit test being added.
Co-authored-by: Tomasz Jakut <[email protected]>
Co-authored-by: Tomasz Jakut <[email protected]>
Co-authored-by: Tomasz Jakut <[email protected]>
The option does not only cover pasting but also dropping.
The option does not only cover pasting but also dropping.
A custom image handler plugin must be able to receive images being pasted or dropped when clipboard_handleImages option is false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I've just updated tests a bit:
- drop test should run in IE10+, so I updated check accordingly,
- we use global
assert
in our tests – I've updated asserts accordingly in your tests, - I've renamed
assertDropImage
toassertImageDrop
to achieve better coherency withassertImagePaste
.
Thanks for the PR!
@Comandeer Perfect! Thank you 😊 |
This will allow 3rd party plugins to handle images being pasted.
Related issue: #4874
What is the purpose of this pull request?
Bug fix
Does your PR contain necessary tests?
All patches that change the editor code must include tests. You can always read more
on PR testing,
how to set the testing environment and
how to create tests
in the official CKEditor documentation.
This PR contains
Did you follow the CKEditor 4 code style guide?
Your code should follow the guidelines from the CKEditor 4 code style guide which helps keep the entire codebase consistent.
What is the proposed changelog entry for this pull request?
What changes did you make?
Added a new configuration option to clipboard plugin which allows for the built-in image paste handling to be disabled, so that other plugins can intercept clipboard data and handle images being pasted.
Which issues does your PR resolve?
Closes #4874.