Skip to content

Commit 0792812

Browse files
kalenikaliaksandrawesomekling
authored andcommitted
LibWeb: Wait until new document becomes active before running scripts
Fixes #22485 With this change WebContent does not crash when `location.reload()` is invoked but `Navigable::reload()` still not working because of spec issue (whatwg/html#9869) so we can't add a test yet.
1 parent 7e8d3e3 commit 0792812

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Userland/Libraries/LibWeb/DOM/Document.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3628,7 +3628,8 @@ void Document::update_for_history_step_application(JS::NonnullGCPtr<HTML::Sessio
36283628
// "process scroll behavior".
36293629
scroll_to_the_fragment();
36303630

3631-
// FIXME: 2. At this point scripts may run for the newly-created document document.
3631+
// 2. At this point scripts may run for the newly-created document document.
3632+
m_ready_to_run_scripts = true;
36323633
}
36333634

36343635
// 7. Otherwise, if documentsEntryChanged is false and doNotReactivate is false, then:

Userland/Libraries/LibWeb/DOM/Document.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ class Document
551551
};
552552
void append_pending_animation_event(PendingAnimationEvent const&);
553553

554+
bool ready_to_run_scripts() const { return m_ready_to_run_scripts; }
555+
554556
protected:
555557
virtual void initialize(JS::Realm&) override;
556558
virtual void visit_edges(Cell::Visitor&) override;
@@ -769,6 +771,9 @@ class Document
769771
Vector<PendingAnimationEvent> m_pending_animation_event_queue;
770772

771773
bool m_needs_to_call_page_did_load { false };
774+
775+
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#scripts-may-run-for-the-newly-created-document
776+
bool m_ready_to_run_scripts { false };
772777
};
773778

774779
template<>

Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,6 +2729,12 @@ void HTMLParser::handle_text(HTMLToken& token)
27292729

27302730
// -> An end tag whose tag name is "script"
27312731
if (token.is_end_tag() && token.tag_name() == HTML::TagNames::script) {
2732+
// https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-html
2733+
// Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be true for document.
2734+
if (!m_document->ready_to_run_scripts()) {
2735+
main_thread_event_loop().spin_until([&] { return m_document->ready_to_run_scripts(); });
2736+
}
2737+
27322738
// FIXME: If the active speculative HTML parser is null and the JavaScript execution context stack is empty, then perform a microtask checkpoint.
27332739

27342740
// Non-standard: Make sure the <script> element has up-to-date text content before preparing the script.

0 commit comments

Comments
 (0)