Skip to content

Experiment: blind redesign of streaming HTML insertion methods - #1

Closed
zcorpan wants to merge 1 commit into
mainfrom
zcorpan/redesign-12758
Closed

zcorpan wants to merge 1 commit into
mainfrom
zcorpan/redesign-12758

Conversation

@zcorpan

@zcorpan zcorpan commented Nov 6, 2026

Copy link
Copy Markdown
Owner

An experiment for a possible redesign option in the spec-pr-review skill (whatwg/spec-ai). An AI agent rewrote the spec change of whatwg/html#12758 from its base (a7f6aed), given only a brief: the intent, the constraints, the problems raised in review, and the tests. It didn't see the PR's text or thread. Not for merging.

The diff here is against the base. Diff against the merged text (289f9189).

Verdict: structurally simpler, but not better. It reintroduces problems that review had already fixed, so a rewrite like this is input to a comparison, not a replacement.

Simpler:

  • One parser field, one struct for the stream's state, and one Document field, vs seven parser fields plus a per-script intended document.
  • About 40 hunks vs 73, deleting 142 lines of existing text vs 425.
  • The merged text has about 20 abort and staleness checks across element creation, the insertion helpers, and the adoption agency algorithm, which every future tree-construction change has to remember. This states those rules once.
  • The XML parser and XSLT are untouched.

Regressions, each a case the merged text handles:

  • Scripts parsed with createContextualFragment() in a scripting-disabled document never run, even after insertion into a normal document. This changes non-streaming behavior.
  • In an adoption agency case with a stale foster-parenting location, it removes content already in the live DOM.
  • The halt rule is ambiguous. Read narrowly, it loses an abort protection; read broadly, an abort from a synchronous iframe load event leaves custom element reaction queues unbalanced.
  • A ready parser-blocking script can run inside write() rather than from a task, so what's inserted after abort() depends on network timing.

The cause was the brief: it recorded those problems as fixed, without saying how, or which neighboring cases the fix had to cover.

Possible bug in the merged text: the resume and deferred-script tasks are queued on the original document's global. If the target moves to another document and the original document's iframe is then removed, they never run, so close() never settles. (Custom elements in content dropped at a Document target can still be constructed, but document parsing does the same.)

Worth porting to the merged text as follow-ups:

  • a per-Document script execution document instead of the per-script field, undoing the XML and XSLT edits;
  • the stream-state struct;
  • "dropped" as a null insertion location;
  • an early return in "the end" for fragment parsers.

For the skill: offer a blind redesign only as one option for a design question, always followed by a comparison and port-back into the existing text. The brief has to give each fixed problem as a must-pass case, with the generalization review settled on. Running the tests would catch some of the regressions, but needs an implementation.

Add streamHTML(), streamAppendHTML(), streamPrependHTML(), streamBeforeHTML(), streamAfterHTML(), and streamReplaceWithHTML(), and their Unsafe counterparts. Each returns a WritableStream whose string chunks are parsed incrementally by an HTML fragment parser that inserts top-level nodes directly at a position fixed when the method is called. The stream errors if the node that content is inserted before stops being a child of the target. With runScripts, streamed scripts are parser-inserted and run in the target's document like during document loading: parser-blocking scripts pause the parser without blocking writes, and defer and module scripts run after close() settles.

To support this, the HTML fragment parsing algorithm's setup is factored out into "create an HTML fragment parser", which every fragment parser (and only those) uses. The stream parser's own inert document holds its pending parsing-blocking script and deferred scripts, and a new script execution document on Document lets the existing cross-document check in "prepare the script element" compare against the target's document. A halted stream parser stops before its next tokenizer or tree construction step.

This also changes existing behavior:

* Fragment parsing creates top-level elements directly in the target's node document (or, when sanitizing, in the inert document of the fragment) rather than in an inert document from which they are adopted. This fixes the non-sanitizing top-level case of whatwg#13068.
* Fragment parsers no longer run "the end", so fragment parsing never spins the event loop.
* Scripts created by a fragment parser whose context document has scripting disabled are marked as already started, so that their clones don't run.
* The HTML parser only processes an SVG script element at its end tag if the parser runs scripts and the element is connected and in the document the parser's scripts run in. In particular, document parsing no longer processes an SVG script element whose ancestor was removed, or that was moved to another document, before its end tag.

Tests: web-platform-tests/wpt PR 61748.

Fixes whatwg#2142.
@zcorpan

zcorpan commented Nov 10, 2026

Copy link
Copy Markdown
Owner Author

Closing since this was is an artifact to help create whatwg/spec-ai#12 . The new issue found was a bonus.

@zcorpan zcorpan closed this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant