Skip to content
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

Support editor functions after reattach to DOM #4601

Merged
merged 56 commits into from
May 26, 2021
Merged

Support editor functions after reattach to DOM #4601

merged 56 commits into from
May 26, 2021

Conversation

sculpt0r
Copy link
Contributor

@sculpt0r sculpt0r commented Apr 1, 2021

What is the purpose of this pull request?

New feature

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

  • Unit tests
  • Manual tests

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.

  • PR is consistent with the code style guide

What is the proposed changelog entry for this pull request?

* [#4462](https://github.com/ckeditor/ckeditor4/issues/4462): Support editor functions after reattaching to DOM.

What changes did you make?

  • Used MutationObserver to detect whenever the editor is attached to DOM again.
  • Based on that information, recreate the entire editable section. In the case of IE, I have to wait until load event. So only for IE, there is an extra flag used to mark, that editable should be recreated.
  • Added extra flag which preserves iframe element during editable detach process. Used only internally in recreating.
  • Added config option (detachableParent) which allows pass native DOM object which should be observed for detach / attach changes
  • Added new editor.status (recreating) which prevents null references in recreating the process
  • Added manual & unit tests

We won't be able to support IE below 11, because of MutationObserver usage. However, it's mostly for integrations, which doesn't support IE below 11.

Three units test are passing regardless of this fix:

  • 'test reattached editor makes editor data empty with observed invalid DOM object'
  • 'test reattached editor parent makes editor data empty with observed invalid DOM object'
  • 'test reattached editor parent makes editor data empty with observed detached parent element'

They confirm if iframe reloading still makes the editor empty. Also, they show that an invalid configuration still leads to an empty editor.

Fix two tests that create query strings for querySelector() with restricted characters (IE):

Which issues does your PR resolve?

Closes #4462

@sculpt0r
Copy link
Contributor Author

sculpt0r commented Apr 2, 2021

@ckeditor/qa-team Hello there 👋 
Could you look at this feature again? The previous solution (#4481) was discarded and we came up with something better.

Note that it won't work for IE below 11.

There is a manual test with some additional plugins (tests/plugins/wysiwygarea/manual/detachedwithplugins) which may be helpful during some monkey clicking.

@LukaszGudel
Copy link

I've tested this feature using provided manual test and everything seems to work correctly 👍 I did not find anything broken during my testing.

@sculpt0r sculpt0r marked this pull request as ready for review April 6, 2021 12:46
@Dumluregn Dumluregn self-requested a review April 7, 2021 09:24
@Dumluregn Dumluregn self-assigned this Apr 7, 2021
Copy link
Contributor

@Dumluregn Dumluregn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the solution, it looks much clearer now with mutationObserver (no custom attributes on iframe native element, yay!). It looks to work just like before 👍🏻

Besides inline comments, I'd like to ask for explicit tests for the cases that were failing in the previous version of the solution, so the embed test and an image with undo test (and BTW I see the second one is still failing - could you relate to it since you didn't in the original PR?).

tests/plugins/wysiwygarea/manual/detachedtwoeditors.md Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/manual/detachedwithplugins.html Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/manual/detachedwithplugins.html Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/manual/detachedwithplugins.md Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/detached.js Show resolved Hide resolved
plugins/wysiwygarea/plugin.js Outdated Show resolved Hide resolved
plugins/wysiwygarea/plugin.js Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/detached.js Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/detached.js Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/detached.js Outdated Show resolved Hide resolved
@sculpt0r sculpt0r self-assigned this Apr 9, 2021
@sculpt0r
Copy link
Contributor Author

@Dumluregn - ready for review.

  • added missing tests from the previous PR
  • correct inline comments

I try to fix this additional step in history - but without success... TBH I was able to fix it, but at the same time undo was broken if I toggle editor before insert image - so still wrong behaviour - even worse. As you write - could we extract it as another issue? But in this case, we will have one failing manual test.

@sculpt0r sculpt0r removed their assignment Apr 12, 2021
@sculpt0r sculpt0r requested a review from Dumluregn April 12, 2021 22:30
@Dumluregn Dumluregn self-assigned this Apr 13, 2021
Copy link
Contributor

@Dumluregn Dumluregn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some small refactors left.

Besides that - on IE11 I've encountered an error after each toggle:
Screenshot 2021-04-15 at 15 48 38

Looks like Bender issue TBH 🤔 could you verify if it's reproducible outside of testing environment? I didn't experience any influence on editor (all tests pass), but in manual descriptions we write that any error causes the test to fail, so we should take some action.

tests/plugins/wysiwygarea/detached.js Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/detached.js Outdated Show resolved Hide resolved
core/editor.js Outdated Show resolved Hide resolved
plugins/wysiwygarea/plugin.js Outdated Show resolved Hide resolved
plugins/wysiwygarea/plugin.js Outdated Show resolved Hide resolved
plugins/wysiwygarea/plugin.js Outdated Show resolved Hide resolved
plugins/wysiwygarea/plugin.js Outdated Show resolved Hide resolved
plugins/wysiwygarea/plugin.js Outdated Show resolved Hide resolved
plugins/wysiwygarea/plugin.js Outdated Show resolved Hide resolved
@sculpt0r
Copy link
Contributor Author

Rebase on newest major

@sculpt0r
Copy link
Contributor Author

sculpt0r commented Apr 22, 2021

Besides that - on IE11 I've encountered an error after each toggle:
Screenshot 2021-04-15 at 15 48 38

Definitely, it's related to our solution. Because in unit tests which expect empty editor (so didn't call restore logic) this issue doesn't appear for me in IE11. All other units from detached throw this error... 🤔

Interesting... I used the debugger and go line, by line... This error appears after this line:

doc.write( data );
Then it leads to sth like this:
image

And this is the source of our error :] This block code comes from

var bootstrapCode =
'<script id="cke_actscrpt" type="text/javascript"' + ( CKEDITOR.env.ie ? ' defer="defer" ' : '' ) + '>' +
'var wasLoaded=0;' + // It must be always set to 0 as it remains as a window property.
'function onload(){' +
'if(!wasLoaded)' + // FF3.6 calls onload twice when editor.setData. Stop that.
'window.parent.CKEDITOR.tools.callFunction(' + this._.frameLoadedHandler + ',window);' +
'wasLoaded=1;' +
'}' +
( CKEDITOR.env.ie ? 'onload();' : 'document.addEventListener("DOMContentLoaded", onload, false );' ) +
'</script>';

The easiest way is to just suppress the error by adding window.parent.CKEDITOR &&. Kind of a workaround for now, but since this PR is long enough - maybe we can use it? As you said:

I didn't experience any influence on editor (all tests pass),

So it could be a solution for now. It doesn't break anything (at least for now 🙈 ) and this is happening only for IE11 is iframe reloading case - so eventually damage seems to be low?

@sculpt0r
Copy link
Contributor Author

@Dumluregn - ready for review.

@sculpt0r sculpt0r removed their assignment Apr 23, 2021
@sculpt0r sculpt0r requested a review from Dumluregn April 23, 2021 07:00
@Dumluregn Dumluregn self-assigned this Apr 23, 2021
Copy link
Contributor

@Dumluregn Dumluregn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've encountered an issue with undo history which occurs on all browsers and is rather simple to reproduce, so we have to take care of it.

  1. Open any manual test.
  2. Toggle source mode.
  3. Toggle editor (detach+reattach).
  4. Use bold button.

Editor doesn't record any undo step. Even if it looks like it does (after you type anything), you can't access it.

Other than that - mostly small ammendments.

plugins/wysiwygarea/plugin.js Outdated Show resolved Hide resolved
plugins/wysiwygarea/plugin.js Show resolved Hide resolved
tests/plugins/wysiwygarea/detached.js Show resolved Hide resolved
tests/plugins/wysiwygarea/detached.js Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/detached.js Show resolved Hide resolved
tests/plugins/wysiwygarea/manual/detachedtwoeditors.html Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/manual/detached.html Outdated Show resolved Hide resolved
tests/plugins/wysiwygarea/manual/detachedembededimage.md Outdated Show resolved Hide resolved
@sculpt0r
Copy link
Contributor Author

@Comandeer - ready for another review.

  • Added unit test for getSelection()
  • Fix failing tests. They failed because the previous test has still an active observer (the default observed element is the entire document). So now - the editor is destroyed at the end of the test.

@sculpt0r sculpt0r removed their assignment May 24, 2021
@sculpt0r sculpt0r requested a review from Comandeer May 24, 2021 10:37
@Comandeer Comandeer self-assigned this May 26, 2021
Copy link
Member

@Comandeer Comandeer left a 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 API docs for CKEDITOR.editor#getSelection() (it seems that we have them wrong for years 🙈) and simplify it a little bit (replaced editable.editor.status with this.status).

I've also added additional changelog entry for the change in CKEDITOR.editor#getSelection() under API Changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support editor functions after reattach to DOM
5 participants