Skip to content

Shadow DOM support (first attempt) - #561

Closed
nazar-pc wants to merge 2 commits into
tinymce:masterfrom
nazar-pc:shadow-dom-support-v1
Closed

nazar-pc wants to merge 2 commits into
tinymce:masterfrom
nazar-pc:shadow-dom-support-v1

Conversation

@nazar-pc

Copy link
Copy Markdown
Contributor

The first attempt to bring TinyMCE under Shadow DOM and make it working there.
To be honest I'm not expecting this to be merged, but I want to show main issues in current architecture and demonstrate that this is indeed possible.
Related feature was requested here, this is just first essential step (and is more generic): http://www.tinymce.com/forum/viewtopic.php?pid=117628

What works

TinyMCE under native Shadow DOM (including inline mode) - selection, cut/paste, all buttons, dropdowns, modals, etc.
I've tested most of plugins, to be specific:

advlist anchor charmap code colorpicker contextmenu fullscreen hr image link lists media nonbreaking noneditable pagebreak paste preview searchreplace tabfocus table textcolor visualblocks visualchars wordcount

This patch includes only JS part, CSS can be easily converted to such that penetrates Shadow Root and thus will work using this script (ideally, we'll need to wrap everything into custom element to avoid using deprecated /deep/, but it works for now and will work for quite long time until we'll implement it properly).

What doesn't work

TinyMCE under Shadow DOM polyfill from WebComponents.js.
This is in progress, I'm planning to implement this as well, but changes in Shadow DOM polyfill will be necessary, so this will take some more time, hopefully not too much, PR will be updated.
Also 2 tests fail, don't know yet why exactly, but they don't seem to be critical indeed (help is welcomed).

Partially working features

caretRangeFromPoint() method is present only on document, but not on shadowRoot (mailing list, relevant Chromium issue, vote, please) and using document.caretRangeFromPoint() gives us just Shadow Root's host element.
Because of this we can't find exact position where content was dropped.
However, shadowRoot.elementFromPoint() method is present and is used as last resort to drop contents to the beginning or the end of string, this was the best alternative I was able to get working and it is not that bad actually.

BC breaks

Current design involves very, VERY bad, horrible idea to reference everything by ID.
Since some elements are under Shadow DOM we need to know where Shadow Root is, for this purpose target property should be passed in settings with textarea (or target element in inline mode), which involves trivial changes for many of TinyMCE's internals and some plugins/themes.
I had some idea about repository of elements like key-value storage id:dom_node_reference to make smaller changes to existing structure from one side and avoid problems with determining current document/shadow root to search for ID in from another side.

Implementation

First 3 commits might be actually separated since they are generic and will not break anything in existing code, the last is heavy with actually implementing all essential things. Please, refer to commit messages for details (tricky parts in code also commented).

What is next?

I expect feedback from core developers and those who are interested about how TinyMCE's core might be adjusted to make this patch less invasive and avoid breaking plugins/themes (settings.target is required now).
Also any other feedback about implementation and/or help with fixing those 2 tests and/or anything else - I'm open for discussion.

@spocke

spocke commented Oct 21, 2015

Copy link
Copy Markdown
Member

This is one big patch but it's nice to see that shadow dom support seems closer. Earlier attempts that I've made failed due to limitations in Chrome the only browser implementing it at the time. The show stopper then was focus it was impossible to sub focus items properly within a shadow root.

There is a reason why we use ID:s on everything. Legacy browser support requires us to not have or at least limit the number of references from JS to DOM to avoid memory leaks on older IE versions. One small reference could cause the whole document to linger in ram on each reload. However we could probably abstract that away better.

Also we really need caretRangeFromPoint in fact in the next 4.3 version we use it a lot more so that is a show stopper if it's buggy.

So I guess we could do this in steps small iterations.

Also the getContatinerElm is a left over from the last attempt I made towards shadow dom:
https://github.com/tinymce/tinymce/blob/master/js/tinymce/classes/ui/Control.js#L159

I in that version tried to render everything within the shadow root to make it self contained.

@nazar-pc

Copy link
Copy Markdown
Contributor Author

Can you clarify what IE versions are officially supported? It seems like IE8+, but I saw some hacks for 6-7 in code. Anyway, some abstraction would be really helpful here.

Chromium is evergreen browser, just help pushing developers in mentioned Chromium issue and in few months after implementation we can use it rather safely. It is not buggy, it is just not available inside Shadow DOM at all, this is the main problem.

I've tried to move everything under Shadow DOM initially as well, but then decided to leave modals and tooltips where they are to simplify changes (also this is my first time touching internals of TinyMCE). In fact in my own custom elements I put tooltips and modals under html, not body, because, for instance, modal on opening add blur effect on body and I need modal content not be blurred. Lastly, it would complicate positioning, so I'd suggest to leave them in main document tree.

@nazar-pc

Copy link
Copy Markdown
Contributor Author

As first steps abstracting elements selection and event targeting fixes might be upstreamed since they'll not hurt anything.
About events targeting I mean using e.path instead of just e.target, since e.path where supported will help to get real target even under Shadow DOM (small caveat here, as far as I remember in latest spec it was renamed to e.deepPath, so when first browser will ship Shadow DOM v1 this must be supported).

@spocke

spocke commented Oct 21, 2015

Copy link
Copy Markdown
Member

I think the e.path/e.deepPath should be added to EventUtils.js logic so all events everywhere gets patched.

@spocke

spocke commented Oct 21, 2015

Copy link
Copy Markdown
Member

@nazar-pc

Copy link
Copy Markdown
Contributor Author

So now IE7 is gone, right?

@nazar-pc
nazar-pc force-pushed the shadow-dom-support-v1 branch from 4057298 to c16c6c0 Compare October 21, 2015 17:47
@nazar-pc

Copy link
Copy Markdown
Contributor Author

Good news on working under polyfill.
There was only one method I found missing shadowRoot.getSelection(), fix already sent to WebComponents.js, if you can ping developers it would help to accelerate its upstreaming: webcomponents/webcomponentsjs#421

No updates about those 2 failing tests.

All other things as far as I've tested work fine, its just a matter of proper CSS pre-processing. Ideally, it should be cooked as Web Component (Polymer element) and stylesheet files should be explicitly included to be properly shimmed (I'll prepare such element soon). Alternatively to use CSS that we obtain from using script I've mentioned before under polyfill we can to patch tinymce.DOM.create so that for link[rel=stylesheet] elements attribute shim-shadowdom is added (I'll personally likely start from this, since it is much easier and already works).

I've thought to hack loadCSS method, but polyfill doesn't cover whole spec, so implementing it according to specs (inserting as <style> ins Shadow Root) would not help that much.

What are your thoughts about upstreaming changes from this PR?
What part and how soon - I'm open for cooperation, because I have some experience of maintaining Shadow DOM-aware forks of various projects - it is not fun at all.
I can split first 3 commits into separate PR since they doesn't contain anything Shadow DOM-specific and should not cause any tests to fail.

@nazar-pc

Copy link
Copy Markdown
Contributor Author

Rebased against master

@nazar-pc

Copy link
Copy Markdown
Contributor Author

Rebased against master and included updated changes from #2459
Build with plugins I'm using is available here: https://github.com/nazar-pc/CleverStyle-CMS/tree/master/components/plugins/TinyMCE/includes/js
One level higher in html directory there are few elements I'm using for TinyMCE.
The use is now extremely simple:

<cs-editor>
    <textarea></textarea>
</cs-editor>
<cs-editor-simple>
    <textarea></textarea>
</cs-editor-simple>
<cs-editor-inline>
    <div></div>
</cs-editor-inline>
<cs-editor-simple-inline>
    <div></div>
</cs-editor-simple-inline>

And it doesn't matter whether it is located under Shadow DOM at any depth or just in main document tree - it works everywhere.

Elements are opinionated and intended to be used withing CleverStyle CMS, but it is possible to create general purpose elements as well. Just wanted to post working example of, probably, first build of fully functional (I hope so, no problems so far) feature-reach WYSIWYG editor working under Shadow DOM ever, as reference point.

@nazar-pc
nazar-pc force-pushed the shadow-dom-support-v1 branch from 119bba4 to 5d73cb6 Compare December 20, 2015 04:00
@nazar-pc

Copy link
Copy Markdown
Contributor Author

Good news, I found the reason of that 2 failed tests and now they are all green!

@spocke

spocke commented Dec 21, 2015

Copy link
Copy Markdown
Member

Will have to schedule some time to bring this into the core. Since if we add it we need to A) Maintain it for ever. B) Add a bunch of unit tests.

We want to improve integrations with web components, react and angular so it's in the backlog.

@nazar-pc
nazar-pc force-pushed the shadow-dom-support-v1 branch from 5d73cb6 to 1e2e9b4 Compare March 19, 2016 02:01
@nazar-pc

Copy link
Copy Markdown
Contributor Author

Just rebased against master once again, fixed one bug appeared after rebase - there was infinite loop when closing modal popups because of recursive .focus() call, here is this line: https://github.com/tinymce/tinymce/pull/561/files#diff-d6fddc50fcd574b2f57384bf85cfe901R1129

…the same specificity, but might be useful in certain scenarios
For this we keep forwarding target element property everywhere to know about Shadow Root when we make searching for element by its ID.
Also in jQuery version `settings.target` is filled automatically, otherwise afterwards it will not be possible to find element under Shadow DOM.
`window.getSelection()` is changed to element's shadow root like `shadowRoot.getSelection()` in inline mode.
Active element detection is fixed, since active element under Shadow DOM should be defined on closest Shadow Root.
Target correction on focus in/out events.
Fix for drag'n'drop in inline mode under Shadow DOM (limited to insertion at the beginning or at the end).
Fix for `element.ownerDocument` might be wrapped by polyfill and thus not be the same as `document`.
Fix for using `document.activeElement` being not-wrapped under polyfill, while it needs to be before usage.
Fix for tests to provide `settings.target` for `getEl()` call.
@fyrkant

fyrkant commented Jan 22, 2018

Copy link
Copy Markdown

The TinyMCE source code has been converted to TypeScript which makes this PR impossible to merge, so I am closing it. Please open up a new PR written in TypeScript if you want us to take a look at it again.

@fyrkant fyrkant closed this Jan 22, 2018
@nazar-pc

Copy link
Copy Markdown
Contributor Author

Man, this is rude! I was working on multiple things from trivial to complex like this one, rebased them multiple times against moving target and now you're just closing it?

@fyrkant

fyrkant commented Jan 22, 2018 •

Copy link
Copy Markdown

It wasn't closed to be rude, just to get a clean slate with the PRs now that the whole project is written in TypeScript.

@rkbhochalya

rkbhochalya commented Jan 22, 2018 •

Copy link
Copy Markdown

I have been watching this PR for long time and now it got closed 😥 . I'm using tinymce in a Chrome extension which inserts an editor in a website and I need to use it in a ShadowDOM for encapsulation. I'm using nazar-pc/tinymce fork for now but that's more than two years old. Chrome and Safari support ShadowDOM natively and Firefox is going to ship with it somewhere in 2018.

@nazar-pc

Copy link
Copy Markdown
Contributor Author

@rkbhochalya, I will try to rebase this soon and will try to submit another PR when ready.

I was rebasing this PR alongside with other tweaks for CleverStyle Framework till March last year (https://github.com/nazar-pc/CleverStyle-Framework/tree/master/modules/TinyMCE/assets/js), but apparently didn't publish rebased branches to my fork, sorry about that.

There is a reason why we use ID:s on everything. Legacy browser support requires us to not have or at least limit the number of references from JS to DOM to avoid memory leaks on older IE versions. One small reference could cause the whole document to linger in ram on each reload. However we could probably abstract that away better.

This was said more than 2 years ago and it would really help, but it doesn't seem to be moving in that direction at all.

I hope smaller PRs like #2928, #2913 and #4163 will be merged without issues very soon, since at least we have some answers from TinyMCE team now.

@kaseyhinton

Copy link
Copy Markdown

Any update on this? I am trying to get tinymce to work in Polymer 2...

@nazar-pc

nazar-pc commented Feb 21, 2018 •

Copy link
Copy Markdown
Contributor Author

Not yet, I'm working on stuff like this in my spare time and since not actively using TinyMCE right now, can't promise any dates besides that I'll make it work with Polymer 2/3 one day in future.

@kaseyhinton

Copy link
Copy Markdown

Okay no worries! Thank you :)

@Afraithe

Copy link
Copy Markdown
Member

Hey nazar-pc, I am the product manager for TinyMCE, and I just want to say I appreciate the work you have done keeping this alive. I know things haven't been moving very fast in this direction from our point of view, we really need it supported in all major browsers before we commit to it fully, as Spocke says, once we enable the support for Shadow DOM, we are stuck supporting it. I hope you understand.

I highly suggest making a new PR, summarize what issues remain etc and we can try work from there.

spocke pushed a commit that referenced this pull request Mar 23, 2018
* commit 'f09d9da389e4f30ae619315b9f145036f539be79':
  TINY-1581: domquery should return empty string if elm.ownerDocument.defaultView is falsy
@jay8t6

jay8t6 commented Apr 9, 2018

Copy link
Copy Markdown

Great work, @nazar-pc!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants