Allow specifying custom HTMLElement
base in defineCustomElement
#805
nicolo-ribaudo
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Scenario
I need to load some untrusted code in my web app. I'm doing so using an
<iframe>
, with asandbox
attribute that prevents script execution. It can basically load some HTML and CSS.I want, however, to provide to this content some extra elements other than what is provided by plain HTML&CSS, and these extra elements/components are defined by me as Vue SFCs.
Non-working attempts
1:
createApp()
and mount in the iframeThe third-party HTML can provide some elements tagged with some special class/attribute, such as:
In my code I can then do something like the following:
This however doesn't fully work: styles from the SFC are not injected inside the iframe, they are only loaded in the top-level page. I need to somehow manually extract those styles and inject them.
2: custom element created in the main page and moved to the iframe
To avoid the styles problem, I need a solution that relies on self-contained styles. Thankfully Vue provides one, through custom elements and
defineCustomElement
.The third-party HTML page can write the following:
and my JS code would register the custom element inside the iframe:
This however also does not work, because custom elements need to inherit from the
HTMLElement
of the realm/window that owns the document where the custom element is created. To register a class as a custom element in the iframe's registry, it needs to inherit fromiframe.contentWindow.HTMLElement
.3 (working, but ugly): re-create elements in the main document and moving them to the iframe
Instead of what I tried in solution 2, my code needs to do something like this:
Ideal solution
It'd be great if
defineCustomElement
accepted anHTMLElement
option, that it uses as root ancestor:Beta Was this translation helpful? Give feedback.
All reactions