浏览代码

LibWeb/HTML: Port Window.history to IDL

Linus Groh 2 年之前
父节点
当前提交
c42496187b

+ 5 - 0
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -2068,6 +2068,11 @@ JS::NonnullGCPtr<HTML::History> Document::history()
     return *m_history;
 }
 
+JS::NonnullGCPtr<HTML::History> Document::history() const
+{
+    return const_cast<Document*>(this)->history();
+}
+
 // https://html.spec.whatwg.org/multipage/origin.html#dom-document-domain
 DeprecatedString Document::domain() const
 {

+ 2 - 0
Userland/Libraries/LibWeb/DOM/Document.h

@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -337,6 +338,7 @@ public:
     bool is_active() const;
 
     JS::NonnullGCPtr<HTML::History> history();
+    JS::NonnullGCPtr<HTML::History> history() const;
 
     HTML::Location* location();
 

+ 1 - 0
Userland/Libraries/LibWeb/Forward.h

@@ -249,6 +249,7 @@ class ErrorEvent;
 class EventHandler;
 class EventLoop;
 class FormDataEvent;
+class History;
 class HTMLAnchorElement;
 class HTMLAreaElement;
 class HTMLAudioElement;

+ 7 - 7
Userland/Libraries/LibWeb/HTML/Window.cpp

@@ -1076,7 +1076,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
     define_native_accessor(realm, "top", top_getter, nullptr, JS::Attribute::Enumerable);
     define_native_accessor(realm, "parent", parent_getter, {}, JS::Attribute::Enumerable);
     define_native_accessor(realm, "frameElement", frame_element_getter, {}, JS::Attribute::Enumerable);
-    define_native_accessor(realm, "history", history_getter, {}, JS::Attribute::Enumerable);
     define_native_accessor(realm, "performance", performance_getter, performance_setter, JS::Attribute::Enumerable | JS::Attribute::Configurable);
     define_native_accessor(realm, "crypto", crypto_getter, {}, JS::Attribute::Enumerable);
     define_native_accessor(realm, "screen", screen_getter, screen_setter, JS::Attribute::Enumerable | JS::Attribute::Configurable);
@@ -1232,6 +1231,13 @@ JS::NonnullGCPtr<Location> Window::location() const
     return *m_location;
 }
 
+// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-history
+JS::NonnullGCPtr<History> Window::history() const
+{
+    // The history getter steps are to return this's associated Document's history object.
+    return associated_document().history();
+}
+
 // https://html.spec.whatwg.org/multipage/window-object.html#dom-frames
 JS::NonnullGCPtr<WindowProxy> Window::frames() const
 {
@@ -1825,12 +1831,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::scroll_by)
     return JS::js_undefined();
 }
 
-JS_DEFINE_NATIVE_FUNCTION(Window::history_getter)
-{
-    auto* impl = TRY(impl_from(vm));
-    return impl->associated_document().history();
-}
-
 JS_DEFINE_NATIVE_FUNCTION(Window::screen_left_getter)
 {
     auto* impl = TRY(impl_from(vm));

+ 1 - 2
Userland/Libraries/LibWeb/HTML/Window.h

@@ -143,6 +143,7 @@ public:
     String name() const;
     void set_name(String const&);
     JS::NonnullGCPtr<Location> location() const;
+    JS::NonnullGCPtr<History> history() const;
 
     JS::NonnullGCPtr<WindowProxy> frames() const;
 
@@ -226,8 +227,6 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(performance_getter);
     JS_DECLARE_NATIVE_FUNCTION(performance_setter);
 
-    JS_DECLARE_NATIVE_FUNCTION(history_getter);
-
     JS_DECLARE_NATIVE_FUNCTION(screen_getter);
     JS_DECLARE_NATIVE_FUNCTION(screen_setter);
 

+ 1 - 0
Userland/Libraries/LibWeb/HTML/Window.idl

@@ -12,6 +12,7 @@ interface Window : EventTarget {
     [LegacyUnforgeable] readonly attribute Document document;
     attribute DOMString name;
     [PutForwards=href, LegacyUnforgeable] readonly attribute Location location;
+    readonly attribute History history;
 
     // other browsing contexts
     [Replaceable] readonly attribute WindowProxy frames;