Skip to content

Truly independent web browser

LibWeb: Implement window.print()

Andreas Klinge43f64a9de82
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 37c28889612..7dafcbcfe13 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -5989,6 +5989,44 @@ void Document::remove_from_script_blocking_style_sheet_set(Element& element)
schedule_html_parser_end_check();
}
+// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#printing-steps
+void Document::run_the_printing_steps()
+{
+ // 1. The user agent may display a message to the user or return (or both).
+
+ // 2. If the active sandboxing flag set of document has the sandboxed modals flag set, then return.
+ if (has_flag(active_sandboxing_flag_set(), HTML::SandboxingFlagSet::SandboxedModals))
+ return;
+
+ // NB: The events go to the relevant global object of document, and to the active windows of its child navigables
+ // that are in this process.
+ auto fire_print_event = [&](Utf16FlyString const& event_name) {
+ auto& window = HTML::relevant_window(*this);
+ window.dispatch_event(Event::create(event_name, HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(window))));
+ for (auto const& child_navigable : document_tree_child_navigables()) {
+ auto* local_child_navigable = as_if<HTML::LocalNavigable>(*child_navigable);
+ if (!local_child_navigable)
+ continue;
+ if (auto child_window = local_child_navigable->active_window())
+ child_window->dispatch_event(Event::create(event_name, HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(*child_window))));
+ }
+ };
+
+ // 3. The user agent must fire an event named beforeprint at the relevant global object of document, as well as
+ // any child navigable in it.
+ fire_print_event(HTML::EventNames::beforeprint);
+
+ // FIXME: 4. The user agent should offer the user the opportunity to obtain a physical form (or the representation of
+ // a physical form) of document. The user agent may wait for the user to either accept or decline before
+ // returning; if so, the user agent must pause while the method is waiting. Even if the user agent doesn't
+ // wait at this point, the user agent must use the state of the relevant documents as they are at this
+ // point in the algorithm if and when it eventually creates the alternate form.
+
+ // 5. The user agent must fire an event named afterprint at the relevant global object of document, as well as any
+ // child navigables in it.
+ fire_print_event(HTML::EventNames::afterprint);
+}
+
void Document::set_ready_for_post_load_tasks(bool ready)
{
m_ready_for_post_load_tasks = ready;
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index edb802e364a..a98534e4316 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -831,6 +831,12 @@ public:
bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
void set_ready_for_post_load_tasks(bool ready);
+ // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#print-when-loaded
+ bool print_when_loaded() const { return m_print_when_loaded; }
+ void set_print_when_loaded() { m_print_when_loaded = true; }
+
+ void run_the_printing_steps();
+
void completely_finish_loading();
bool completely_loaded_deferred() const { return m_completely_loaded_deferred; }
void queue_navigation_timing_entry();
@@ -1953,6 +1959,7 @@ private:
Optional<Utf16String> m_encoding;
bool m_ready_for_post_load_tasks { false };
+ bool m_print_when_loaded { false };
GC::Ptr<DOMImplementation> m_implementation;
GC::Ptr<FragmentDirective> m_fragment_directive;
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
index d704f84fa05..0dfc2e98bcd 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
@@ -1058,9 +1058,12 @@ void HTMLParserEndState::complete()
// 13. Queue the navigation timing entry for the Document.
document->queue_navigation_timing_entry();
- }));
- // FIXME: 10. If the Document's print when loaded flag is set, then run the printing steps.
+ // 10. If the Document's print when loaded flag is set, then run the printing steps.
+ // NB: This runs inside the task queued above, like step 11; see there.
+ if (document->print_when_loaded())
+ document->run_the_printing_steps();
+ }));
// NB: Step 11 (the Document is now ready for post-load tasks) runs inside the task queued above; see there.
}
diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cpp
index ae90ddab24b..51f46b6b7bb 100644
--- a/Libraries/LibWeb/HTML/Window.cpp
+++ b/Libraries/LibWeb/HTML/Window.cpp
@@ -1570,6 +1570,30 @@ void Window::alert(Utf16String const& message)
page().did_request_alert(message);
}
+// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-print
+void Window::print()
+{
+ // 1. Let document be this's associated Document.
+ auto& document = associated_document();
+
+ // 2. If document is not fully active, then return.
+ if (!document.is_fully_active())
+ return;
+
+ // 3. If document's unload counter is greater than 0, then return.
+ if (document.unload_counter() > 0)
+ return;
+
+ // 4. If document is ready for post-load tasks, then run the printing steps for document.
+ if (document.ready_for_post_load_tasks()) {
+ document.run_the_printing_steps();
+ return;
+ }
+
+ // 5. Otherwise, set document's print when loaded flag.
+ document.set_print_when_loaded();
+}
+
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-confirm
bool Window::confirm(Optional<Utf16String> const& message)
{
diff --git a/Libraries/LibWeb/HTML/Window.h b/Libraries/LibWeb/HTML/Window.h
index 47831843e7d..420cc7ca934 100644
--- a/Libraries/LibWeb/HTML/Window.h
+++ b/Libraries/LibWeb/HTML/Window.h
@@ -219,6 +219,7 @@ public:
[[nodiscard]] GC::Ref<Speech::SpeechSynthesis> speech_synthesis();
void alert(Utf16String const& message = {});
+ void print();
bool confirm(Optional<Utf16String> const& message);
Optional<Utf16String> prompt(Optional<Utf16String> const& message, Optional<Utf16String> const& default_);
diff --git a/Libraries/LibWeb/HTML/Window.idl b/Libraries/LibWeb/HTML/Window.idl
index 27e5ae6bcaf..a7c78794ba0 100644
--- a/Libraries/LibWeb/HTML/Window.idl
+++ b/Libraries/LibWeb/HTML/Window.idl
@@ -47,7 +47,7 @@ interface Window : EventTarget {
undefined alert(DOMString message);
boolean confirm(optional DOMString message = "");
DOMString? prompt(optional DOMString message = "", optional DOMString default = "");
- [FIXME] undefined print();
+ undefined print();
[NeedsCallerRealm] undefined postMessage(any message, USVString targetOrigin, optional sequence<object> transfer = []);
[NeedsCallerRealm, ImplementedInBindings] undefined postMessage(any message, optional WindowPostMessageOptions options = {});
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-during-beforeunload.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-during-beforeunload.txt
index e58fa648ead..8f62911e306 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-during-beforeunload.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-during-beforeunload.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail print() during beforeunload
\ No newline at end of file
+1 Pass
+Pass print() during beforeunload
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-during-unload.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-during-unload.txt
index d30fdb5e526..650f90033a6 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-during-unload.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-during-unload.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail print() during unload
\ No newline at end of file
+1 Pass
+Pass print() during unload
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-in-detached-frame.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-in-detached-frame.txt
index 08cb24081ec..2b6101c7b74 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-in-detached-frame.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/user-prompts/print-in-detached-frame.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail print() in a detached iframe
\ No newline at end of file
+1 Pass
+Pass print() in a detached iframe
\ No newline at end of file