Skip to content

Side effects due to tree insertion or removal (script, iframe) #808

Closed
web-platform-tests/wpt
#44308

Description

@annevk

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

  1. bzbarsky commented on Dec 9, 2019

    @bzbarsky

    @emilio has thought about this some as well, I think.

  2. emilio commented on Dec 9, 2019

    @emilio
    Contributor

    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 (via AddScriptRunner).

  3. annevk commented on Dec 9, 2019

    @annevk
    MemberAuthor

    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.

  4. annevk commented on Apr 2, 2020

    @annevk
    MemberAuthor
  5. tkent-google commented on Apr 3, 2020

    @tkent-google
    Contributor

    (I delegate to @mfreed7. I'm not responsible for HTML any longer.)

  6. rniwa commented on Apr 7, 2020

    @rniwa

    In WebKit, insertion and removal are different.

    Newly inserted nodes are notified in two stages: insertedIntoAncestor and didFinishInsertingNodes. 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 as HTMLScriptElement overrides 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.

  7. mfreed7 commented on May 4, 2020

    @mfreed7
    Contributor
  8. josepharhar commented on May 4, 2020

    @josepharhar
    Contributor

    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

    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?

    Chrome and Firefox have this model for script insertion.

    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?

  9. annevk commented on May 5, 2020

    @annevk
    MemberAuthor

    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).

  10. josepharhar commented on Sep 10, 2020

    @josepharhar
    Contributor

    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?

    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.

    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?

  11. bzbarsky commented on Sep 10, 2020

    @bzbarsky

    Are there any examples which would help me understand how these operations run script?

    • Inserting an inline <script> (one without src) runs the script text in it immediately, before the insert call returns.
    • Inserting an iframe with no src attribute set (and maybe with one set to about:blank?) fires a load event synchronously, before the call returns, in Chrome.
    • Removing an iframe synchronously fires beforeunload and unload events on the document inside, last I checked.

    None of that is related to mutation events.

  12. josepharhar commented on Sep 14, 2020

    @josepharhar
    Contributor
    • Inserting an inline <script> (one without src) runs the script text in it immediately, before the insert call returns.
    • Inserting an iframe with no src attribute set (and maybe with one set to about:blank?) fires a load event synchronously, before the call returns, in Chrome.
    • Removing an iframe synchronously fires beforeunload and unload events on the document inside, last I checked.

    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:

    • When inserting an inline <script>, the script will be executed before appendChild returns, just like you said.
    • When inserting an iframe which doesn't have a src attribute, chrome/safari will fire the load event before appendChild returns, whereas firefox will fire the load event after appendChild returns, just like you said.
    • When removing an iframe, the unload event is fired before remove() 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?

  13. bzbarsky commented on Sep 15, 2020

    @bzbarsky

    Are there any other side effects of interest, or is that it?

    That is one of the open questions, yes.

    For the script and unload cases, is the goal really to make script get executed after insertion and make unload fire after remove?

    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.

    For the sync load event case

    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.

  14. flackr commented on Dec 8, 2022

    @flackr

    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: none timing 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.

  15. 18 remaining items

  16. domfarolino commented on Mar 4, 2024

    @domfarolino
    Member

    Seems this was prematurely closed?

  17. annevk commented on Mar 5, 2024

    @annevk
    MemberAuthor

    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.)

  18. added a commit that references this issue on Jun 5, 2024
    0616094
  19. added a commit that references this issue on Jun 20, 2024
    cda03ae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions