ladybird/Libraries/LibWeb/HTML/Parser
Aliaksandr Kalenik df96b69e7a 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.
2026-03-28 23:14:55 +01:00
..
Entities.cpp LibWeb/HTML: Improve data structure of named character reference data 2025-07-14 09:43:08 +02:00
Entities.h LibWeb/HTML: Improve data structure of named character reference data 2025-07-14 09:43:08 +02:00
Entities.json LibWeb: Make named character references more spec-compliant & efficient 2025-03-22 16:03:44 +01:00
HTMLEncodingDetection.cpp LibWeb: Extract some CORS and MIME Fetch helpers to their own files 2025-11-27 14:57:29 +01:00
HTMLEncodingDetection.h LibWeb: Extract some CORS and MIME Fetch helpers to their own files 2025-11-27 14:57:29 +01:00
HTMLParser.cpp LibWeb: Replace spin_until in HTMLParser::the_end() with state machine 2026-03-28 23:14:55 +01:00
HTMLParser.h LibWeb: Replace spin_until in HTMLParser::the_end() with state machine 2026-03-28 23:14:55 +01:00
HTMLToken.cpp LibWeb: Track if element was created from token with dupe attributes 2025-07-09 15:52:54 -06:00
HTMLToken.h LibWeb: Reduce size of Optional<HTMLToken> 2026-03-20 12:03:36 +01:00
HTMLTokenizer.cpp LibJS+LibWeb: Add missing GC marking visits 2026-01-07 12:48:58 +01:00
HTMLTokenizer.h LibJS+LibWeb: Add missing GC marking visits 2026-01-07 12:48:58 +01:00
ListOfActiveFormattingElements.cpp LibWeb: Keep the tokens in ListOfActiveFormattingElements 2025-10-21 23:36:07 +02:00
ListOfActiveFormattingElements.h LibWeb: Keep the tokens in ListOfActiveFormattingElements 2025-10-21 23:36:07 +02:00
StackOfOpenElements.cpp LibWeb: Add HTMLSelectedContentElement for customizable select 2025-12-12 12:06:24 +00:00
StackOfOpenElements.h LibWeb: Add HTMLSelectedContentElement for customizable select 2025-12-12 12:06:24 +00:00