summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2021-09-12 14:59:49 +0100
committerAndreas Kling <kling@serenityos.org>2021-09-12 16:07:24 +0200
commit678dd2d18031e8fabf91281af6e0776b269305d0 (patch)
tree68e0c2c52de04105b199bfd6823d5e8c97d14e85 /Userland
parentee5bac08912dc4cbe565bd31636aa8b962b9f727 (diff)
downloadserenity-678dd2d18031e8fabf91281af6e0776b269305d0.zip
LibWeb: Expose the location object via Document.location
Both Window.location and Document.location use the same instance of the Location object. Some sites use it via Window, some via Document.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/Bindings/WindowObject.cpp7
-rw-r--r--Userland/Libraries/LibWeb/Bindings/WindowObject.h5
-rw-r--r--Userland/Libraries/LibWeb/DOM/Document.cpp12
-rw-r--r--Userland/Libraries/LibWeb/DOM/Document.h2
-rw-r--r--Userland/Libraries/LibWeb/DOM/Document.idl3
5 files changed, 28 insertions, 1 deletions
diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
index 989db89812..640024fff8 100644
--- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
+++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
@@ -92,8 +92,12 @@ void WindowObject::initialize_global_object()
// Legacy
define_native_accessor("event", event_getter, event_setter, JS::Attribute::Enumerable);
+ m_location_object = heap().allocate<LocationObject>(*this, *this);
+
define_direct_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
- define_direct_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
+
+ // NOTE: location is marked as [LegacyUnforgeable], meaning it isn't configurable.
+ define_direct_property("location", m_location_object, JS::Attribute::Enumerable);
// WebAssembly "namespace"
define_direct_property("WebAssembly", heap().allocate<WebAssemblyObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
@@ -108,6 +112,7 @@ WindowObject::~WindowObject()
void WindowObject::visit_edges(Visitor& visitor)
{
GlobalObject::visit_edges(visitor);
+ visitor.visit(m_location_object);
for (auto& it : m_prototypes)
visitor.visit(it.value);
for (auto& it : m_constructors)
diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.h b/Userland/Libraries/LibWeb/Bindings/WindowObject.h
index 7a5f014980..a1074f8a5e 100644
--- a/Userland/Libraries/LibWeb/Bindings/WindowObject.h
+++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h
@@ -30,6 +30,9 @@ public:
Origin origin() const;
+ LocationObject* location_object() { return m_location_object; }
+ LocationObject const* location_object() const { return m_location_object; }
+
JS::Object* web_prototype(const String& class_name) { return m_prototypes.get(class_name).value_or(nullptr); }
JS::NativeFunction* web_constructor(const String& class_name) { return m_constructors.get(class_name).value_or(nullptr); }
@@ -96,6 +99,8 @@ private:
NonnullRefPtr<DOM::Window> m_impl;
+ LocationObject* m_location_object;
+
HashMap<String, JS::Object*> m_prototypes;
HashMap<String, JS::NativeFunction*> m_constructors;
};
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp
index 5c09ab76e6..a6b1c4c856 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.cpp
+++ b/Userland/Libraries/LibWeb/DOM/Document.cpp
@@ -979,4 +979,16 @@ bool Document::is_fully_active() const
return browsing_context() && browsing_context()->active_document() == this && (browsing_context()->is_top_level() || browsing_context()->container_document()->is_fully_active());
}
+// https://html.spec.whatwg.org/multipage/history.html#dom-document-location
+Bindings::LocationObject* Document::location()
+{
+ // The Document object's location attribute's getter must return this Document object's relevant global object's Location object,
+ // if this Document object is fully active, and null otherwise.
+
+ if (!is_fully_active())
+ return nullptr;
+
+ return window().wrapper()->location_object();
+}
+
}
diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h
index 165a6817a5..91970e4e4c 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.h
+++ b/Userland/Libraries/LibWeb/DOM/Document.h
@@ -279,6 +279,8 @@ public:
NonnullRefPtr<HTML::History> history() const { return m_history; }
+ Bindings::LocationObject* location();
+
private:
explicit Document(const URL&);
diff --git a/Userland/Libraries/LibWeb/DOM/Document.idl b/Userland/Libraries/LibWeb/DOM/Document.idl
index d233575e68..ef5cb53e28 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.idl
+++ b/Userland/Libraries/LibWeb/DOM/Document.idl
@@ -2,6 +2,9 @@ interface Document : Node {
constructor();
+ // FIXME: These attributes currently don't do anything.
+ [PutForwards=href, LegacyUnforgeable] readonly attribute Location? location;
+
readonly attribute DOMImplementation implementation;
readonly attribute DOMString characterSet;