LibWeb: Replace spin_until in HTMLParser::the_end() with state machine

HTMLParser::the_end() had three spin_until calls that blocked the event
loop: step 5 (deferred scripts), step 7 (ASAP scripts), and step 8
(load event delay). This replaces them with an HTMLParserEndState state
machine that progresses asynchronously via callbacks.

The state machine has three phases matching the three spin_until calls:
- WaitingForDeferredScripts: loops executing ready deferred scripts
- WaitingForASAPScripts: waits for ASAP script lists to empty
- WaitingForLoadEventDelay: waits for nothing to delay the load event

Notification triggers re-evaluate the state machine when conditions
change: HTMLScriptElement::mark_as_ready, stylesheet unblocking in
StyleElementBase/HTMLLinkElement, did_stop_being_active_document, and
DocumentLoadEventDelayer decrements. NavigableContainer state changes
(session history readiness, content navigable cleared, lazy load flag)
also trigger re-evaluation of the load event delay check.

Key design decisions and why:

1. Microtask checkpoint in schedule_progress_check(): The old spin_until
   called perform_a_microtask_checkpoint() before checking conditions.
   This is critical because HTMLImageElement::update_the_image_data step
   8 queues a microtask that creates the DocumentLoadEventDelayer.
   Without the checkpoint, check_progress() would see zero delayers and
   complete before images start delaying the load event.

2. deferred_invoke in schedule_progress_check():
   I tried Core::Timer (0ms), queue_global_task, and synchronous calls.
   Timers caused non-deterministic ordering with the HTML event loop's
   task processing timer, leading to image layout tests failing (wrong
   subtest pass/fail patterns). Synchronous calls fired too early during
   image load processing before dimensions were set, causing 0-height
   images in layout tests. queue_global_task had task ordering issues
   with the session history traversal queue. deferred_invoke runs after
   the current callback returns but within the same event loop pump,
   giving the right balance.

3. Navigation load event guard (m_navigation_load_event_guard): During
   cross-document navigation, finalize_a_cross_document_navigation step
   2 calls set_delaying_load_events(false) before the session history
   traversal activates the new document. This creates a transient state
   where the parent's load event delay check sees the about:blank (which
   has ready_for_post_load_tasks=true) as the active document and
   completes prematurely.
This commit is contained in:
Aliaksandr Kalenik
2026-03-28 09:39:51 +01:00
committed by Alexander Kalenik
parent b542617e09
commit df96b69e7a
Notes: github-actions[bot] 2026-03-28 22:15:52 +00:00
12 changed files with 235 additions and 40 deletions

View File

@@ -358,6 +358,16 @@ void Navigable::set_delaying_load_events(bool value)
}
}
void Navigable::set_navigation_load_event_guard(DOM::Document& parent_doc)
{
m_navigation_load_event_guard.emplace(parent_doc);
}
void Navigable::clear_navigation_load_event_guard()
{
m_navigation_load_event_guard.clear();
}
GC::Ptr<Navigable> Navigable::navigable_with_active_document(GC::Ref<DOM::Document> document)
{
for (auto navigable : all_navigables()) {
@@ -2564,6 +2574,13 @@ void finalize_a_cross_document_navigation(GC::Ref<Navigable> navigable, HistoryH
// 1. FIXME: Assert: this is running on navigable's traversable navigable's session history traversal queue.
// 2. Set navigable's is delaying load events to false.
// AD-HOC: Without this guard, decrementing the navigable's delay counter triggers schedule_load_event_delay_check
// on the parent, which can see the about:blank (ready_for_post_load_tasks=true) before the session
// history traversal activates the new document. The guard is cleared when the new document becomes ready
// for post-load tasks (via set_ready_for_post_load_tasks).
if (auto container_doc = navigable->container_document(); container_doc && history_entry->document())
navigable->set_navigation_load_event_guard(*container_doc);
navigable->set_delaying_load_events(false);
// 3. If historyEntry's document is null, then return.