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

@@ -14,6 +14,7 @@
#include <LibWeb/HTML/Parser/ListOfActiveFormattingElements.h>
#include <LibWeb/HTML/Parser/StackOfOpenElements.h>
#include <LibWeb/MimeSniff/MimeType.h>
#include <LibWeb/Platform/Timer.h>
namespace Web::HTML {
@@ -217,6 +218,39 @@ private:
StringBuilder m_character_insertion_builder { StringBuilder::Mode::UTF16 };
};
class HTMLParserEndState final : public JS::Cell {
GC_CELL(HTMLParserEndState, JS::Cell);
GC_DECLARE_ALLOCATOR(HTMLParserEndState);
public:
static GC::Ref<HTMLParserEndState> create(GC::Ref<DOM::Document>, GC::Ptr<HTMLParser>);
void schedule_progress_check();
private:
enum class Phase {
WaitingForDeferredScripts,
WaitingForASAPScripts,
WaitingForLoadEventDelay,
Completed,
};
HTMLParserEndState(GC::Ref<DOM::Document>, GC::Ptr<HTMLParser>);
virtual void visit_edges(Cell::Visitor&) override;
void check_progress();
void advance_to_asap_scripts_phase();
void complete();
Phase m_phase { Phase::WaitingForDeferredScripts };
bool m_check_pending { false };
GC::Ref<DOM::Document> m_document;
GC::Ptr<HTMLParser> m_parser;
GC::Ref<Platform::Timer> m_timeout;
};
RefPtr<CSS::StyleValue const> parse_dimension_value(StringView);
RefPtr<CSS::StyleValue const> parse_nonzero_dimension_value(StringView);
Optional<Color> parse_legacy_color_value(StringView);