Browse Source

LibWeb: Add state field to History

This is the "classic history API state" referenced in the current HTML
spec.
Andrew Kaster 1 year ago
parent
commit
49e0466a3d

+ 12 - 0
Userland/Libraries/LibWeb/HTML/History.cpp

@@ -38,6 +38,7 @@ void History::visit_edges(Cell::Visitor& visitor)
 {
     Base::visit_edges(visitor);
     visitor.visit(m_associated_document);
+    visitor.visit(m_state);
 }
 
 // https://html.spec.whatwg.org/multipage/history.html#dom-history-pushstate
@@ -65,6 +66,17 @@ WebIDL::ExceptionOr<u64> History::length() const
     return m_length;
 }
 
+// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history-state
+WebIDL::ExceptionOr<JS::Value> History::state() const
+{
+    // 1. If this's relevant global object's associated Document is not fully active, then throw a "SecurityError" DOMException.
+    if (!m_associated_document->is_fully_active())
+        return WebIDL::SecurityError::create(realm(), "Cannot perform state on a document that isn't fully active."_fly_string);
+
+    // 2. Return this's state.
+    return m_state;
+}
+
 // https://html.spec.whatwg.org/multipage/history.html#dom-history-go
 WebIDL::ExceptionOr<void> History::go(long delta = 0)
 {

+ 4 - 0
Userland/Libraries/LibWeb/HTML/History.h

@@ -28,10 +28,13 @@ public:
     WebIDL::ExceptionOr<void> back();
     WebIDL::ExceptionOr<void> forward();
     WebIDL::ExceptionOr<u64> length() const;
+    WebIDL::ExceptionOr<JS::Value> state() const;
 
     u64 m_index { 0 };
     u64 m_length { 0 };
 
+    void set_state(JS::Value s) { m_state = s; }
+
 private:
     History(JS::Realm&, DOM::Document&);
 
@@ -41,6 +44,7 @@ private:
     WebIDL::ExceptionOr<void> shared_history_push_replace_state(JS::Value data, Optional<String> const& url, HistoryHandlingBehavior);
 
     JS::NonnullGCPtr<DOM::Document> m_associated_document;
+    JS::Value m_state { JS::js_null() };
 };
 
 bool can_have_its_url_rewritten(DOM::Document const& document, AK::URL const& target_url);

+ 1 - 1
Userland/Libraries/LibWeb/HTML/History.idl

@@ -3,7 +3,7 @@
 interface History {
     readonly attribute unsigned long length;
     // FIXME: attribute ScrollRestoration scrollRestoration;
-    // FIXME: readonly attribute any state;
+    readonly attribute any state;
     undefined go(optional long delta = 0);
     undefined back();
     undefined forward();