Skip to content

Commit d4738ef

Browse files
LibWeb: Implement window.length
1 parent a0e3289 commit d4738ef

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,28 @@ BrowsingContext* BrowsingContext::choose_a_browsing_context(StringView name, boo
742742
return chosen;
743743
}
744744

745+
// https://html.spec.whatwg.org/multipage/browsers.html#document-tree-child-browsing-context
746+
size_t BrowsingContext::document_tree_child_browsing_context_count() const
747+
{
748+
size_t count = 0;
749+
750+
// A browsing context child is a document-tree child browsing context of parent if child is a child browsing context and child's container is in a document tree.
751+
for_each_child([this, &count](BrowsingContext const& child) {
752+
if (child.is_child_of(*this) && child.container()->in_a_document_tree())
753+
++count;
754+
});
755+
756+
return count;
757+
}
758+
759+
// https://html.spec.whatwg.org/multipage/browsers.html#child-browsing-context
760+
bool BrowsingContext::is_child_of(BrowsingContext const& parent) const
761+
{
762+
// A browsing context child is said to be a child browsing context of another browsing context parent,
763+
// if child's container document is non-null and child's container document's browsing context is parent.
764+
return container_document() && container_document()->browsing_context() == &parent;
765+
}
766+
745767
// https://html.spec.whatwg.org/multipage/dom.html#still-on-its-initial-about:blank-document
746768
bool BrowsingContext::still_on_its_initial_about_blank_document() const
747769
{

Userland/Libraries/LibWeb/HTML/BrowsingContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ class BrowsingContext : public TreeNode<BrowsingContext> {
8080

8181
BrowsingContext* choose_a_browsing_context(StringView name, bool noopener);
8282

83+
size_t document_tree_child_browsing_context_count() const;
84+
85+
bool is_child_of(BrowsingContext const&) const;
86+
8387
HTML::BrowsingContextContainer* container() { return m_container; }
8488
HTML::BrowsingContextContainer const* container() const { return m_container; }
8589

Userland/Libraries/LibWeb/HTML/Window.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ void Window::initialize(JS::Realm& realm)
794794
define_native_accessor(realm, "pageXOffset", scroll_x_getter, {}, attr);
795795
define_native_accessor(realm, "scrollY", scroll_y_getter, {}, attr);
796796
define_native_accessor(realm, "pageYOffset", scroll_y_getter, {}, attr);
797+
define_native_accessor(realm, "length", length_getter, {}, attr);
797798

798799
define_native_function(realm, "scroll", scroll, 2, attr);
799800
define_native_function(realm, "scrollTo", scroll, 2, attr);
@@ -1074,6 +1075,29 @@ JS_DEFINE_NATIVE_FUNCTION(Window::btoa)
10741075
return JS::js_string(vm, move(encoded));
10751076
}
10761077

1078+
// https://html.spec.whatwg.org/multipage/window-object.html#number-of-document-tree-child-browsing-contexts
1079+
JS::ThrowCompletionOr<size_t> Window::document_tree_child_browsing_context_count() const
1080+
{
1081+
auto* impl = TRY(impl_from(vm()));
1082+
1083+
// 1. If W's browsing context is null, then return 0.
1084+
auto* this_browsing_context = impl->associated_document().browsing_context();
1085+
if (!this_browsing_context)
1086+
return 0;
1087+
1088+
// 2. Return the number of document-tree child browsing contexts of W's browsing context.
1089+
return this_browsing_context->document_tree_child_browsing_context_count();
1090+
}
1091+
1092+
// https://html.spec.whatwg.org/multipage/window-object.html#dom-length
1093+
JS_DEFINE_NATIVE_FUNCTION(Window::length_getter)
1094+
{
1095+
auto* impl = TRY(impl_from(vm));
1096+
1097+
// The length getter steps are to return the number of document-tree child browsing contexts of this.
1098+
return TRY(impl->document_tree_child_browsing_context_count());
1099+
}
1100+
10771101
// https://html.spec.whatwg.org/multipage/browsers.html#dom-top
10781102
JS_DEFINE_NATIVE_FUNCTION(Window::top_getter)
10791103
{

Userland/Libraries/LibWeb/HTML/Window.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class Window final
5050
HTML::BrowsingContext const* browsing_context() const;
5151
HTML::BrowsingContext* browsing_context();
5252

53+
JS::ThrowCompletionOr<size_t> document_tree_child_browsing_context_count() const;
54+
5355
void alert_impl(String const&);
5456
bool confirm_impl(String const&);
5557
String prompt_impl(String const&, String const&);
@@ -199,6 +201,7 @@ class Window final
199201
Bindings::CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }
200202

201203
private:
204+
JS_DECLARE_NATIVE_FUNCTION(length_getter);
202205
JS_DECLARE_NATIVE_FUNCTION(top_getter);
203206

204207
JS_DECLARE_NATIVE_FUNCTION(document_getter);

0 commit comments

Comments
 (0)