Skip to content

Don't set force async to false in Fragment scripting mode - #13080

Merged
zcorpan merged 1 commit into
mainfrom
noamr/fragment-force-async
Oct 20, 2026
Merged

zcorpan merged 1 commit into
mainfrom
noamr/fragment-force-async

Conversation

@noamr

@noamr noamr commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

In the Fragment scripting mode (used by createContextualFragment(), setHTMLUnsafe() and the positional *HTMLUnsafe() methods with runScripts: true, and innerHTML/outerHTML/insertAdjacentHTML() when a Trusted Types default policy enables runScripts), the HTML parser does not set the script element's parser document. Previously, it still set force async to false, and because prepare the script element only resets force async to true when parser document is non-null, external scripts without async kept force async set to false once inserted.

All three browser engines (Chromium, Gecko, and WebKit) instead leave force async as true in this case (script.async returns true before insertion, and external scripts without async execute as async once inserted).

Only set force async to false in #scriptTag when scripting mode is not Fragment (i.e., when parser document is set).

Fixes #13079.

(See WHATWG Working Mode: Changes for more details.)


/parsing.html ( diff )
/scripting.html ( diff )

@zcorpan

zcorpan commented Oct 15, 2026

Copy link
Copy Markdown
Member
AI review

Reviewed at a609700, together with wpt#63708 at 6d1f7668. The change is correct and minimal. Only the Fragment scripting mode is affected; document parsing, document.write() and Inert/Disabled parsing still set force async to false. The tests cover:

  • createContextualFragment();
  • the six *HTMLUnsafe() methods with runScripts, on both connected and disconnected targets, plus ShadowRoot;
  • the legacy setters with a Trusted Types default policy enabling runScripts.

Per spec, a default policy's createParserOptions overrides the caller's runScripts, so the "policy sets runScripts: true" variants are valid for innerHTML, outerHTML, insertAdjacentHTML() and the *HTMLUnsafe() methods in the tests.

Suggestions

  • source:68343-68345: the force async definition still says "It is set to false by the HTML parser and the XML parser on script elements they insert". That's no longer true in the Fragment scripting mode. Consider adding "except when the HTML parser's scripting mode is Fragment".
  • source:147051-147054: the first note paragraph explains that Fragment-mode scripts are treated as not parser-inserted, but not what that now means for their timing. Consider adding: "Their force async stays true, so external scripts without an async attribute execute asynchronously once inserted."
  • unsafe-and-trusted-types-external-scripts-async.html:10-23: because of the page-wide require-trusted-types-for 'script' and the default policy's createParserOptions, the "{ runScripts: true }" subtests (:91-148) also get their options from the policy as a TrustedHTMLParserOptions. The SetHTMLUnsafeOptions dictionary path is never exercised. Consider moving them to a file without the CSP and the policy.
  • Both files, setupEvents()/:16-18: if an assertion fails before the await, slow.js is still loading. It then pushes "slow" into the next subtest's window.events and calls that subtest's on_slow, so one failure can cascade into the following subtests. Consider tagging each subtest's scripts with its id and ignoring stale ones. For example, slow.js could check new URL(document.currentScript.src).searchParams.get("id") against the current subtest's id before pushing, and the data: script could embed the id the same way.

@noamr
noamr force-pushed the noamr/fragment-force-async branch from a609700 to 9b897a3 Compare October 16, 2026 15:25
@noamr

noamr commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

AI review

Addressed suggestions and WPT issues.

@zcorpan

zcorpan commented Oct 16, 2026

Copy link
Copy Markdown
Member
AI review

Reviewed at 9b897a3, together with web-platform-tests/wpt#63708 at f4018416. The spec side looks good, and lint and the build pass:

  • the force async definition now has the Fragment-mode exception;
  • the note explains the async consequence;
  • the tests are split, so html-unsafe-external-scripts-async.html exercises the dictionary path without Trusted Types;
  • each subtest's scripts are tagged with its id.

Blockers

  • wpt domparsing/resources/slow.js:1: the top-level const id is a global lexical declaration. The second time a page runs slow.js, it throws "SyntaxError: Identifier 'id' has already been declared", so on_slow is never called. On wpt.fyi PR head (Chrome 158.0.8106), html-unsafe-external-scripts-async.html and unsafe-and-trusted-types-external-scripts-async.html both end with that harness error. In each, the first subtest passes, the second times out, and the rest don't run. createContextualFragment-external-scripts-async.html only loads it once, so it passes. Consider wrapping it in a block:
{
  const id = new URL(document.currentScript.src).searchParams.get('id');
  if (id === String(window.current_test_id)) {
    window.events.push('slow');
    window.on_slow?.();
  }
}

@noamr

noamr commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

AI review

Fixed the WPT issue.

@noamr noamr mentioned this pull request Oct 17, 2026
5 of 6 tasks
@zcorpan
zcorpan merged commit c686714 into main Oct 20, 2026
2 checks passed
@zcorpan
zcorpan deleted the noamr/fragment-force-async branch October 20, 2026 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

createContextualFragment() external scripts without async run async in browsers

2 participants