소스 검색

LibWeb/HTML: Port Window.length to IDL

Linus Groh 2 년 전
부모
커밋
baaf891c64

+ 8 - 11
Userland/Libraries/LibWeb/HTML/Window.cpp

@@ -1112,7 +1112,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
     define_native_accessor(realm, "pageXOffset", scroll_x_getter, {}, attr);
     define_native_accessor(realm, "pageXOffset", scroll_x_getter, {}, attr);
     define_native_accessor(realm, "scrollY", scroll_y_getter, {}, attr);
     define_native_accessor(realm, "scrollY", scroll_y_getter, {}, attr);
     define_native_accessor(realm, "pageYOffset", scroll_y_getter, {}, attr);
     define_native_accessor(realm, "pageYOffset", scroll_y_getter, {}, attr);
-    define_native_accessor(realm, "length", length_getter, {}, attr);
 
 
     define_native_function(realm, "scroll", scroll, 2, attr);
     define_native_function(realm, "scroll", scroll, 2, attr);
     define_native_function(realm, "scrollTo", scroll, 2, attr);
     define_native_function(realm, "scrollTo", scroll, 2, attr);
@@ -1245,6 +1244,13 @@ JS::NonnullGCPtr<WindowProxy> Window::frames() const
     return verify_cast<WindowProxy>(relevant_realm(*this).global_environment().global_this_value());
     return verify_cast<WindowProxy>(relevant_realm(*this).global_environment().global_this_value());
 }
 }
 
 
+// https://html.spec.whatwg.org/multipage/window-object.html#dom-length
+u32 Window::length() const
+{
+    // The length getter steps are to return this's associated Document's document-tree child navigables's size.
+    return static_cast<u32>(document_tree_child_browsing_context_count());
+}
+
 // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator
 // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator
 JS::NonnullGCPtr<Navigator> Window::navigator() const
 JS::NonnullGCPtr<Navigator> Window::navigator() const
 {
 {
@@ -1531,7 +1537,7 @@ JS_DEFINE_NATIVE_FUNCTION(Window::focus)
 }
 }
 
 
 // https://html.spec.whatwg.org/multipage/window-object.html#number-of-document-tree-child-browsing-contexts
 // https://html.spec.whatwg.org/multipage/window-object.html#number-of-document-tree-child-browsing-contexts
-JS::ThrowCompletionOr<size_t> Window::document_tree_child_browsing_context_count() const
+size_t Window::document_tree_child_browsing_context_count() const
 {
 {
     // 1. If W's browsing context is null, then return 0.
     // 1. If W's browsing context is null, then return 0.
     auto* this_browsing_context = associated_document().browsing_context();
     auto* this_browsing_context = associated_document().browsing_context();
@@ -1542,15 +1548,6 @@ JS::ThrowCompletionOr<size_t> Window::document_tree_child_browsing_context_count
     return this_browsing_context->document_tree_child_browsing_context_count();
     return this_browsing_context->document_tree_child_browsing_context_count();
 }
 }
 
 
-// https://html.spec.whatwg.org/multipage/window-object.html#dom-length
-JS_DEFINE_NATIVE_FUNCTION(Window::length_getter)
-{
-    auto* impl = TRY(impl_from(vm));
-
-    // The length getter steps are to return the number of document-tree child browsing contexts of this.
-    return TRY(impl->document_tree_child_browsing_context_count());
-}
-
 // https://html.spec.whatwg.org/multipage/browsers.html#dom-top
 // https://html.spec.whatwg.org/multipage/browsers.html#dom-top
 JS_DEFINE_NATIVE_FUNCTION(Window::top_getter)
 JS_DEFINE_NATIVE_FUNCTION(Window::top_getter)
 {
 {

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

@@ -58,7 +58,7 @@ public:
     HTML::BrowsingContext const* browsing_context() const;
     HTML::BrowsingContext const* browsing_context() const;
     HTML::BrowsingContext* browsing_context();
     HTML::BrowsingContext* browsing_context();
 
 
-    JS::ThrowCompletionOr<size_t> document_tree_child_browsing_context_count() const;
+    size_t document_tree_child_browsing_context_count() const;
 
 
     ImportMap const& import_map() const { return m_import_map; }
     ImportMap const& import_map() const { return m_import_map; }
 
 
@@ -146,6 +146,7 @@ public:
     JS::NonnullGCPtr<History> history() const;
     JS::NonnullGCPtr<History> history() const;
 
 
     JS::NonnullGCPtr<WindowProxy> frames() const;
     JS::NonnullGCPtr<WindowProxy> frames() const;
+    u32 length() const;
 
 
     JS::NonnullGCPtr<Navigator> navigator() const;
     JS::NonnullGCPtr<Navigator> navigator() const;
 
 
@@ -217,7 +218,6 @@ public:
     CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }
     CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }
 
 
 private:
 private:
-    JS_DECLARE_NATIVE_FUNCTION(length_getter);
     JS_DECLARE_NATIVE_FUNCTION(top_getter);
     JS_DECLARE_NATIVE_FUNCTION(top_getter);
 
 
     JS_DECLARE_NATIVE_FUNCTION(frame_element_getter);
     JS_DECLARE_NATIVE_FUNCTION(frame_element_getter);

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

@@ -16,6 +16,7 @@ interface Window : EventTarget {
 
 
     // other browsing contexts
     // other browsing contexts
     [Replaceable] readonly attribute WindowProxy frames;
     [Replaceable] readonly attribute WindowProxy frames;
+    [Replaceable] readonly attribute unsigned long length;
 
 
     // the user agent
     // the user agent
     readonly attribute Navigator navigator;
     readonly attribute Navigator navigator;

+ 2 - 2
Userland/Libraries/LibWeb/HTML/WindowProxy.cpp

@@ -72,7 +72,7 @@ JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> WindowProxy::internal_ge
         auto index = property_key.as_number();
         auto index = property_key.as_number();
 
 
         // 2. Let maxProperties be the number of document-tree child browsing contexts of W.
         // 2. Let maxProperties be the number of document-tree child browsing contexts of W.
-        auto max_properties = TRY(m_window->document_tree_child_browsing_context_count());
+        auto max_properties = m_window->document_tree_child_browsing_context_count();
 
 
         // 3. Let value be undefined.
         // 3. Let value be undefined.
         Optional<JS::Value> value;
         Optional<JS::Value> value;
@@ -227,7 +227,7 @@ JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> WindowProxy::internal_own_pro
     auto keys = JS::MarkedVector<JS::Value> { vm.heap() };
     auto keys = JS::MarkedVector<JS::Value> { vm.heap() };
 
 
     // 3. Let maxProperties be the number of document-tree child browsing contexts of W.
     // 3. Let maxProperties be the number of document-tree child browsing contexts of W.
-    auto max_properties = TRY(m_window->document_tree_child_browsing_context_count());
+    auto max_properties = m_window->document_tree_child_browsing_context_count();
 
 
     // 4. Let index be 0.
     // 4. Let index be 0.
     // 5. Repeat while index < maxProperties,
     // 5. Repeat while index < maxProperties,