-
Notifications
You must be signed in to change notification settings - Fork 297
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
MutationObserver
flag to observe mutations in (open) shadow trees to facilitate polyfills of HTML syntax
#1287
Comments
As author of all polyfills from v0 to current state, I fully support this proposal. My workaround is not super slow but it requires me patching the global On a side note: the closed shadow might still be addressed somehow and I feel like the elephant in the room here is that we don't have a way for Websites authors to decide what is trusted as script that could patch natives, and what isn't ... CSP is not a great answer to this disambiguation, but maybe this discussion should be done in other venues. |
An alternative to this might be to be able to observe the composed tree, because in some cases we might want to know the nodes that will participate in the final output: I've had to implement composed tree tracking so that webgl-rendered custom elements would render correct results when composed via slots into shadow roots. I'd like to release some of these tools separate libs, maybe it'll help ideate. Even without custom rendering, it might be beneficial to react to composed nodes, avoiding extra work for trees that are not active. F.e. suppose a client-side route switcher modifies slot attributes to show/hide content, and we want to observe only content in the active tree. |
Over in @ox-harris implements Not only is the usage simpler than with |
@LeaVerou could you explain a bit more what is the use case here? |
Sure, what is unclear in my examples? |
"to implement polyfills of certain features that affect HTML syntax across both light and shadow DOM because it requires observing mutations in shadow trees as well" ... "Any new HTML element or attribute" So what issue there exactly are you trying to solve? |
To implement polyfills of new HTML syntax, you need to be able to react to relevant changes in the DOM. E.g. for a new attribute |
I see, you want to polyfill new global attributes basically. |
That's one use case. Or new native elements. Or new attributes on native elements. |
Just realized, this wouldn't only help with polyfilling HTML syntax, but also attributes like |
Thanks @trusktr for the mention. Re: the above, there's been a clear misfit between what I do and what the MutationObserver APi does. For example, I sometimes just want to say:
(A reall case in point: implementing the upcoming OOHTML proposal.) It turns out to be extremely difficult and mostly impossible to use the MutationObserver API in those scenarios. Writing the above referenced library - RealDOM - a realtime, and more declarative, DOM API, was the logical answer. Now, while I didn't pursue that as a proposal, perhaps some of ideas there could help in the ideation in here. PSS: I didn't pursue that as a proposal because I was unsure how much typical my usecase was. But I think it's more of a moving story now. |
The regression is caused by the visible flag no longer changing the polling type to RAF. This solves the case reported in the related bug when changes within shadow roots are tracked with visible=true. The underlying issue is that MutationPoller does not consider shadow roots at all. There is a related spec issue to support shadow roots in MutationObserver but as long as it is not supported we need a client side solution. This PR adds a test for that case as well as a test when visibility is changed without DOM mutations. Refs: #13152 Related: whatwg/dom#1287 Drive-by: adds a test for mutations inside shadow roots.
The regression is caused by the visible flag no longer changing the polling type to RAF. This solves the case reported in the related bug when changes within shadow roots are tracked with visible=true. The underlying issue is that MutationPoller does not consider shadow roots at all. There is a related spec issue to support shadow roots in MutationObserver but as long as it is not supported we need a client side solution. This PR adds a test for that case as well as a test when visibility is changed without DOM mutations. Refs: #13152 Related: whatwg/dom#1287 Drive-by: adds a test for mutations inside shadow roots.
The regression is caused by the visible flag no longer changing the polling type to RAF. This solves the case reported in the related bug when changes within shadow roots are tracked with visible=true. The underlying issue is that MutationPoller does not consider shadow roots at all. There is a related spec issue to support shadow roots in MutationObserver but as long as it is not supported we need a client side solution. This PR adds a test for that case as well as a test when visibility is changed without DOM mutations. Refs: #13152 Related: whatwg/dom#1287 Drive-by: adds a test for mutations inside shadow roots.
The regression is caused by the visible flag no longer changing the polling type to RAF. This solves the case reported in the related bug when changes within shadow roots are tracked with visible=true. The underlying issue is that MutationPoller does not consider shadow roots at all. There is a related spec issue to support shadow roots in MutationObserver but as long as it is not supported we need a client side solution. This PR adds a test for that case as well as a test when visibility is changed without DOM mutations. Refs: #13152 Related: whatwg/dom#1287 Drive-by: adds a test for mutations inside shadow roots.
Just in case another use case could inspire why this could be compelling... Let's say I have a component auto-loader (like shoelace autoload) that detects custom elements being inserted into the DOM using MutationObserver. It'd be swell if some of my custom components (or perhaps 3rd party components) that use shoelace components in open shadowTrees could also trigger the auto-loading mechanism. |
As WebKit / Safari is landing scoped Custom Elements we should be careful with polyfills or observing custom elements in there because the inner registry might already know, or take care of, those elements ... so that |
What problem are you trying to solve?
It is currently exceedingly difficult (and slow) to implement polyfills of certain features that affect HTML syntax across both light and shadow DOM because it requires observing mutations in shadow trees as well, which is currently incredibly difficult, and all solutions have prohibitive performance.
Some examples:
What solutions exist today?
The current options are:
HTMLElement.prototype.attachShadow()
with code that callsmutationObserver.observe()
on it, like so:shadowRoot
and callobserve()
on that too. Loop over all existing elements and attach mutation observers to the shadow roots of those with one.As you can see, all of these are pretty terrible. They are slow, obtrusive, and wasteful. Can we do better?
How would you solve it?
Option 1
This is the most straightforward, lowest effort option: Simply add a new option to
MutationObserverInit
to observe (open) shadow trees. Name TBB, some ideas might beshadow
,shadowRoots
.The MVP could be a boolean option, which could later be extended to more granularity (e.g. how many levels deep? What types of shadow trees (
open
,open-stylable
etc)).Option 2
It could be argued that
MutationObserver.prototype.observe()
is the wrong tool for the job. It was designed to observe mutations under a specific root, and has been since contorted to cater to these types of use cases. Perhaps a different method could be used to signal "observe this everywhere you can", e.g.MutationObserver.prototype.globalObserve()
.Anything else?
No response
The text was updated successfully, but these errors were encountered: