Skip to content

Run insertion steps after DocumentFragment insertion completes - #576

Closed
annevk wants to merge 1 commit into
masterfrom
annevk/run-scripts-post-insert
Closed

annevk wants to merge 1 commit into
masterfrom
annevk/run-scripts-post-insert

Conversation

@annevk

@annevk annevk commented Feb 20, 2018 •

Copy link
Copy Markdown
Member

Fixes whatwg/html#1127 and fixes #575.


Preview | Diff

@annevk annevk added the needs tests Moving the issue forward requires someone to write tests label Feb 20, 2018
@annevk
annevk requested a review from nox February 21, 2018 10:11
@annevk

annevk commented Feb 21, 2018

Copy link
Copy Markdown
Member Author

@smaug---- would appreciate review from you as well (note that you can click on Diff in OP to see the changes rendered in HTML).

@annevk

annevk commented Feb 21, 2018

Copy link
Copy Markdown
Member Author

@cdumez you might want to review this as well since WebKit has slightly different behavior here. In particular see #575 for various demos I still need to turn into tests.

@smaug----

Copy link
Copy Markdown
Contributor

Looks reasonable to me.
Would be good to have a test for the case where there are two script elements in document fragment, and when the first one runs, it removes the second one from document. Per spec the second one should not run, but don't know what browsers do.

@annevk

annevk commented Feb 21, 2018

Copy link
Copy Markdown
Member Author

@smaug---- see #575 which tests various scenarios along those lines. Unsurprisingly, browsers differ.

@bzbarsky

Copy link
Copy Markdown

This seems like a pretty big behavior change, if all we want to fix is "don't run scripts before the fragment insertion is done". That is, we would need to audit and test all the things in the spec that do insertion steps to make sure that this is OK, and browsers would need to add a bunch of complexity and runtime overhead to various nodes to make this work.

I'm not a huge fan of this last; the runtime overhead of insertion is already bad enough. We do need some sort of delaying mechanism like this specifically for script execution (though possibly not all of "prepare a script"; Gecko does most of "prepare a script" immediately on insertion but defers the "immediately execute the script block" to a safe point, for example), but not necessarily across the board.

@annevk

annevk commented Feb 26, 2018 •

Copy link
Copy Markdown
Member Author

Hmm yeah, you are correct. When I was reading the Firefox code I was led to believe this is in fact the model, but if we inserted a DocumentFragment object consisting of a script element followed by an iframe element this PR would break it as the iframe hasn't had a chance to create a browsing context yet.

So yeah, we need something more akin to that "script queue" concept and define when to empty it.

cc @domenic

@annevk

annevk commented Feb 26, 2018

Copy link
Copy Markdown
Member Author

Other things to test:

  • style.sheet and script

Bonus:

  • Blur in a removed subtree (not entirely related to this, but in Fx it uses the same script runner thingy)

@annevk

annevk commented Feb 26, 2018 •

Copy link
Copy Markdown
Member Author

Except in Edge (and maybe Safari given how it executes scripts while inserting them rather than after they are all inserted), style.sheet initialization is queued similar to scripts. I.e., the following logs null in the Live DOM Viewer:

<script>
 var s1 = document.createElement("style"); s1.textContent = "body { background:lime }";
 var s2 = document.createElement("script"); s2.textContent = "w(s1.sheet)";
 var df = document.createDocumentFragment(); df.appendChild(s2); df.appendChild(s1);
 document.head.appendChild(df);
</script>

@annevk
annevk removed the request for review from nox March 29, 2018 11:21
@annevk annevk added do not merge yet Pull request must not be merged per rationale in comment and removed do not merge labels May 28, 2018
@nox

nox commented Feb 4, 2019

Copy link
Copy Markdown
Member

Except in Edge (and maybe Safari given how it executes scripts while inserting them rather than after they are all inserted)

I confirm that Safari also logs null.

@nox

nox commented Feb 12, 2019

Copy link
Copy Markdown
Member

Except in Edge (and maybe Safari given how it executes scripts while inserting them rather than after they are all inserted), style.sheet initialization is queued similar to scripts.

That's wrong, turns out Chrome already created the stylesheet by the time the script is executed, and the only reason Safari logs null is because the style element isn't even yet in the document tree because it inserts the fragment's children one by one. If you change the sample to use a div instead, Safari logs an existing style sheet.

<script>
 var s1 = document.createElement("style"); s1.textContent = "body { background:lime }";
 var s2 = document.createElement("script"); s2.textContent = "w(s1.sheet)";
 var df = document.createElement("div"); df.appendChild(s2); df.appendChild(s1);
 document.head.appendChild(df);
</script>

@annevk

annevk commented Feb 26, 2019

Copy link
Copy Markdown
Member Author

Superseded by #732.

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

Labels

do not merge yet Pull request must not be merged per rationale in comment needs tests Moving the issue forward requires someone to write tests

Development

Successfully merging this pull request may close these issues.

Hook for SVG and HTML script element insertion Define timing of script execution in relation to DOM mutations better

4 participants