Skip to content

Commit

Permalink
LibWeb: Return a WindowProxy from document.defaultView
Browse files Browse the repository at this point in the history
This aligns our implementation with the most recent specification steps.
  • Loading branch information
tcl3 committed Sep 21, 2024
1 parent c77d9a2 commit bb10fea
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions Tests/LibWeb/Text/expected/DOM/Document-defaultView.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
document.defaultView === window: true
7 changes: 7 additions & 0 deletions Tests/LibWeb/Text/input/DOM/Document-defaultView.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
test(() => {
println(`document.defaultView === window: ${document.defaultView === window}`);
});
</script>
16 changes: 16 additions & 0 deletions Userland/Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,22 @@ WebIDL::ExceptionOr<void> Document::close()
return {};
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-document-defaultview
JS::GCPtr<HTML::WindowProxy> Document::default_view()
{
// If this's browsing context is null, then return null.
if (!browsing_context())
return {};

// 2. Return this's browsing context's WindowProxy object.
return browsing_context()->window_proxy();
}

JS::GCPtr<HTML::WindowProxy const> Document::default_view() const
{
return const_cast<Document*>(this)->default_view();
}

HTML::Origin Document::origin() const
{
return m_origin;
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibWeb/DOM/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ class Document
WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> open(StringView url, StringView name, StringView features);
WebIDL::ExceptionOr<void> close();

HTML::Window* default_view() { return m_window.ptr(); }
HTML::Window const* default_view() const { return m_window.ptr(); }
JS::GCPtr<HTML::WindowProxy const> default_view() const;
JS::GCPtr<HTML::WindowProxy> default_view();

String const& content_type() const { return m_content_type; }
void set_content_type(String content_type) { m_content_type = move(content_type); }
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/DOM/Document.idl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ interface Document : Node {
readonly attribute DOMString inputEncoding;
readonly attribute DOMString contentType;

readonly attribute Window? defaultView;
readonly attribute WindowProxy? defaultView;

[CEReactions] Document open(optional DOMString unused1, optional DOMString unused2);
WindowProxy? open(USVString url, DOMString name, DOMString features);
Expand Down
16 changes: 11 additions & 5 deletions Userland/Libraries/LibWeb/DOM/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,8 @@ double Element::scroll_top() const
return 0.0;

// 3. Let window be the value of document’s defaultView attribute.
auto* window = document.default_view();
// FIXME: The specification expects defaultView to be a Window object, but defaultView actually returns a WindowProxy object.
auto window = document.window();

// 4. If window is null, return zero and terminate these steps.
if (!window)
Expand Down Expand Up @@ -1276,6 +1277,7 @@ double Element::scroll_top() const
return paintable_box()->scroll_offset().y().to_double();
}

// https://drafts.csswg.org/cssom-view/#dom-element-scrollleft
double Element::scroll_left() const
{
// 1. Let document be the element’s node document.
Expand All @@ -1286,7 +1288,8 @@ double Element::scroll_left() const
return 0.0;

// 3. Let window be the value of document’s defaultView attribute.
auto* window = document.default_view();
// FIXME: The specification expects defaultView to be a Window object, but defaultView actually returns a WindowProxy object.
auto window = document.window();

// 4. If window is null, return zero and terminate these steps.
if (!window)
Expand Down Expand Up @@ -1332,7 +1335,8 @@ void Element::set_scroll_left(double x)
return;

// 5. Let window be the value of document’s defaultView attribute.
auto* window = document.default_view();
// FIXME: The specification expects defaultView to be a Window object, but defaultView actually returns a WindowProxy object.
auto window = document.window();

// 6. If window is null, terminate these steps.
if (!window)
Expand Down Expand Up @@ -1388,7 +1392,8 @@ void Element::set_scroll_top(double y)
return;

// 5. Let window be the value of document’s defaultView attribute.
auto* window = document.default_view();
// FIXME: The specification expects defaultView to be a Window object, but defaultView actually returns a WindowProxy object.
auto window = document.window();

// 6. If window is null, terminate these steps.
if (!window)
Expand Down Expand Up @@ -2347,7 +2352,8 @@ void Element::scroll(double x, double y)
return;

// 5. Let window be the value of document’s defaultView attribute.
auto* window = document.default_view();
// FIXME: The specification expects defaultView to be a Window object, but defaultView actually returns a WindowProxy object.
auto window = document.window();

// 6. If window is null, terminate these steps.
if (!window)
Expand Down

0 comments on commit bb10fea

Please sign in to comment.