Description
Elements that trigger a script-executing side effect due to insertion or removal need to do so using some kind of queue if we ever want to remove mutation events fully (see #305). In particular, I think it's important that tree mutation succeeds and that any script is executed afterwards (but before custom element reactions, see web-platform-tests/wpt#20660).
I'd like to use this thread for discussion, since currently it's spread among various places (#575, #732, whatwg/html#4354, whatwg/html#4611, whatwg/html#4965).
Chrome and Firefox have this model for script insertion. (Though there are differences, if two scripts are inserted and the first removes the second, Firefox will execute the second, Chrome will not. They also react differently to changes to children of the script element, with Chrome only caring about insertions there.)
Chrome has this model for iframe insertion as well, but not removal. Firefox only has it for removal (insertion cannot trigger script in Firefox as it does not have a synchronous load event; note that this means that Chrome and Firefox create nested browsing contexts at different times).
Firefox has this model for style insertion, but since style cannot trigger script I don't think we should follow that.
@nox @bzbarsky @tkent-google @rniwa thoughts? Do we all share the overall goal of executing script at a "safe" point? How would you like to see this broken down further?
Activity
bzbarsky commentedon Dec 9, 2019
@emilio has thought about this some as well, I think.
emilio commentedon Dec 9, 2019
Gecko has a slightly different (and somewhat weird) model for
<iframe>, but it has some sort of model.Gecko loads iframes from the outermost call
Document::EndUpdate, which gets called after ever DOM mutation and such. If it's safe to run script there, then it does it synchronously, otherwise it adds to the same queue<style>uses (viaAddScriptRunner).annevk commentedon Dec 9, 2019
As noted by @nox
<meta http-equiv=default-style>is also deferred in Chrome (and Safari it seems). Filed https://bugzilla.mozilla.org/show_bug.cgi?id=1602476 on Firefox not handling it dynamically.annevk commentedon Apr 2, 2020
@mfreed7 @tkent-google @rniwa @cdumez ping.
tkent-google commentedon Apr 3, 2020
(I delegate to @mfreed7. I'm not responsible for HTML any longer.)
rniwa commentedon Apr 7, 2020
In WebKit, insertion and removal are different.
Newly inserted nodes are notified in two stages:
insertedIntoAncestoranddidFinishInsertingNodes. The former is responsible for updating internal node's states and not allowed to execute any scripts. The latter is called on a subset of nodes which may execute scripts after node insertion. A subset of elements such asHTMLScriptElementoverrides it to execute scripts. Both of these things happen synchronously as we remove nodes.During removal, we disconnect all subframes first (during which introduction of nested browsing contexts are prohibited) and then remove nodes but we don't allow any scripts to execute as a result of removing nodes once we start removing nodes. All of this happens synchronously.
mfreed7 commentedon May 4, 2020
@josepharhar
josepharhar commentedon May 4, 2020
By some kind of queue, do you mean that they need to fire events and run script asynchronously by posting a task instead of synchronously? I wasn't familiar with mutation events before this and I'm still not super familiar with mutation observer - are mutation events synchronous and is mutation observer async?
By this model, do you mean they do sync as opposed to async? Should script tags have their script executed async to when they are appended?
annevk commentedon May 5, 2020
No, insertion/removal runs "synchronous callbacks" for multiple elements. After those callbacks, we run another set of "synchronous callbacks" to execute script for impacted elements. There's no tasks involved. Queue is a reference to https://infra.spec.whatwg.org/#queue (a type of list).
josepharhar commentedon Sep 10, 2020
Is the goal here simply to remove mutation events? Based on tkent's doc I found in #305, it sounds like we would already have to contact sites to get them to stop using mutation events in order to change their behavior - why not skip the change in behavior and just get rid of them altogether in one step?
Are there any examples which would help me understand how these operations run script? Is it all just mutation events or is it something else?
bzbarsky commentedon Sep 10, 2020
<script>(one withoutsrc) runs the script text in it immediately, before the insert call returns.about:blank?) fires a load event synchronously, before the call returns, in Chrome.None of that is related to mutation events.
josepharhar commentedon Sep 14, 2020
Thanks for listing these examples!
I tried reproducing all of them here: https://dom-side-effects.glitch.me
After running it in each browser, these are my observations:
<script>, the script will be executed beforeappendChildreturns, just like you said.srcattribute, chrome/safari will fire theloadevent beforeappendChildreturns, whereas firefox will fire the load event afterappendChildreturns, just like you said.unloadevent is fired beforeremove()returns, just like you said.Are there any other side effects of interest, or is that it?
For the script and unload cases, is the goal really to make script get executed after insertion and make unload fire after remove? I don't really see the benefit and I feel like we would just be breaking websites, especially since the behavior is already the same in chrome firefox and safari.
For the sync load event case, I suppose it is worth looking into changing in chrome, although I think this could be very challenging to implement and I'm afraid of breaking websites. Does anyone know if there is already a crbug open for this?
bzbarsky commentedon Sep 15, 2020
That is one of the open questions, yes.
I believe the intent is to define exactly when during the insertion or removal the side effects happen. For example, if you insert a subtree into the DOM that contains two
<script>tags, we need to define when exactly those run and what the state of the DOM looks like when they do. Right now the spec is not very clear on that.Please see whatwg/html#4965 for previous discussion on that topic. Your fears are not unjustified, but we still need to figure out how to spec something that is web-compatible (the current spec is not, afaict) and then get everyone to converge on that behavior.
And again, if the behavior here ends up "sync load event", then you have to define exactly when it fires. Again, consider inserting a subtree with several iframes and script tags in it; the precise order of all the script executions and the DOM state they see needs to be defined. Including in the face of the executions further mutating the DOM.
flackr commentedon Dec 8, 2022
Would it be possible to defer removal side effects until the next style/layout update (typically the next rendering update). This would align nicely with
display: nonetiming and also could support cases where developers move nodes without needing to add new APIs as long as they leave the node attached for each rendering opportunity.18 remaining items
DOM: Add iframe insertion & removal steps WPTs
DOM: Add iframe insertion & removal steps WPTs
Bug 1877607 [wpt PR 44308] - DOM: Add iframe insertion & removal step…
Bug 1877607 [wpt PR 44308] - DOM: Add iframe insertion & removal step…
domfarolino commentedon Mar 4, 2024
Seems this was prematurely closed?
annevk commentedon Mar 5, 2024
Yeah, don't write "resolve" followed by a link to an issue in your commit message if they don't actually resolve it. (It was initially not a problem as the bot doesn't have access to this repository, but some other people including rniwa do.)
Introduce DOM post-connection steps
Introduce DOM post-connection steps
Use DOM's post-connection steps for script elements
Use DOM's post-connection steps for script elements
Use DOM's post-connection steps for script elements
Bug 1877607 [wpt PR 44308] - DOM: Add iframe insertion & removal step…