Skip to content

Truly independent web browser

LibWeb: Give the Rust Document handle its node navigable

Shannon Boothadff22aeb27c
diff --git a/Libraries/LibWeb/Bindings/Rust/src/dom/document.rs b/Libraries/LibWeb/Bindings/Rust/src/dom/document.rs
index 9dd7cc99d56..a06d614f86a 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/dom/document.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/dom/document.rs
@@ -11,10 +11,11 @@ use liburl_rust::Url;
use crate::bindings::cpp_cell::CppCell;
use crate::bindings::interface::{Ref, cell_from_abi, cell_into_abi};
+use crate::html::navigable::CppNavigable;
use crate::navigation_timing::performance_navigation_timing::PerformanceNavigationTiming;
unsafe extern "C" {
- fn web_bindings_document_has_navigable(document: *mut c_void) -> bool;
+ fn web_bindings_document_navigable(document: *mut c_void) -> *mut c_void;
fn web_bindings_document_is_fully_active(document: *mut c_void) -> bool;
fn web_bindings_document_has_focus(document: *mut c_void) -> bool;
fn web_bindings_document_has_been_destroyed(document: *mut c_void) -> bool;
@@ -82,10 +83,12 @@ impl CppDocument {
unsafe { web_bindings_document_has_been_destroyed(self.0.as_ptr()) }
}
- /// Whether the Document's node navigable is not null, https://html.spec.whatwg.org/multipage/document-sequences.html#node-navigable
- pub(crate) fn has_navigable(&self) -> bool {
- // SAFETY: The Document is live.
- unsafe { web_bindings_document_has_navigable(self.0.as_ptr()) }
+ /// https://html.spec.whatwg.org/multipage/document-sequences.html#node-navigable
+ pub(crate) fn navigable(&self) -> Option<CppNavigable> {
+ // SAFETY: The Document is live, and LibWeb returns null or its live node navigable.
+ let navigable = unsafe { web_bindings_document_navigable(self.0.as_ptr()) };
+ // SAFETY: The navigable is live.
+ (!navigable.is_null()).then(|| unsafe { CppNavigable::from_raw(navigable) })
}
/// https://dom.spec.whatwg.org/#concept-document-url
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
index 1ae14a87159..f4b6de4761b 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
@@ -67,6 +67,8 @@ pub(crate) mod message_channel;
#[cfg(not(test))]
pub(crate) mod message_port;
#[cfg(not(test))]
+pub(crate) mod navigable;
+#[cfg(not(test))]
pub(crate) mod numbers;
#[cfg(not(test))]
pub(crate) mod offscreen_canvas;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/navigable.rs b/Libraries/LibWeb/Bindings/Rust/src/html/navigable.rs
new file mode 100644
index 00000000000..017b3e8ba05
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/navigable.rs
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+use core::ffi::c_void;
+
+use libjs_rust::api::Trace;
+
+use crate::bindings::cpp_cell::CppCell;
+
+/// https://html.spec.whatwg.org/multipage/document-sequences.html#navigable
+#[derive(Clone, Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct CppNavigable(CppCell);
+
+impl CppNavigable {
+ /// # Safety
+ ///
+ /// `navigable` must be a live Navigable.
+ pub(crate) unsafe fn from_raw(navigable: *mut c_void) -> Self {
+ // SAFETY: The caller passes a live Navigable.
+ Self(unsafe { CppCell::from_raw(navigable) })
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/storage.rs b/Libraries/LibWeb/Bindings/Rust/src/html/storage.rs
index 85a3247d343..a803e3c4ea4 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/storage.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/storage.rs
@@ -147,7 +147,7 @@ pub(crate) fn dispatch_storage_events(
if storage_type == StorageType::Session {
// NB: It is possible the remote storage may have not been fully teared down immediately at the point it's
// document is made inactive.
- if !window.associated_document().has_navigable() {
+ if window.associated_document().navigable().is_none() {
continue;
}
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index 366659c1541..7644ecd7a50 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -107,7 +107,7 @@ JS::Realm* web_bindings_window_realm(HTML::Window*);
u64 web_bindings_window_page_id(HTML::Window*);
bool web_bindings_window_has_transient_activation(HTML::Window*);
DOM::EventTarget* web_bindings_window_as_event_target(HTML::Window*);
-bool web_bindings_document_has_navigable(DOM::Document*);
+HTML::LocalNavigable* web_bindings_document_navigable(DOM::Document*);
bool web_bindings_document_is_fully_active(DOM::Document*);
bool web_bindings_document_has_focus(DOM::Document*);
bool web_bindings_document_has_been_destroyed(DOM::Document*);
@@ -231,9 +231,9 @@ DOM::EventTarget* web_bindings_window_as_event_target(HTML::Window* window)
return window;
}
-bool web_bindings_document_has_navigable(DOM::Document* document)
+HTML::LocalNavigable* web_bindings_document_navigable(DOM::Document* document)
{
- return document->navigable() != nullptr;
+ return document->navigable().ptr();
}
bool web_bindings_document_is_fully_active(DOM::Document* document)