1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:17:44 +00:00

LibWeb: Add origin property to window object

This commit is contained in:
Alex M 2022-02-28 09:50:11 -04:00 committed by Linus Groh
parent 39f92fa131
commit f0f2009170
3 changed files with 35 additions and 0 deletions

View file

@ -115,6 +115,7 @@ void WindowObject::initialize_global_object()
define_direct_property("CSS", heap().allocate<CSSNamespace>(*this, *this), 0); define_direct_property("CSS", heap().allocate<CSSNamespace>(*this, *this), 0);
define_native_accessor("localStorage", local_storage_getter, {}, attr); define_native_accessor("localStorage", local_storage_getter, {}, attr);
define_native_accessor("origin", origin_getter, {}, attr);
// Legacy // Legacy
define_native_accessor("event", event_getter, event_setter, JS::Attribute::Enumerable); define_native_accessor("event", event_getter, event_setter, JS::Attribute::Enumerable);
@ -651,6 +652,13 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::post_message)
return JS::js_undefined(); return JS::js_undefined();
} }
// https://html.spec.whatwg.org/multipage/webappapis.html#dom-origin
JS_DEFINE_NATIVE_FUNCTION(WindowObject::origin_getter)
{
auto* impl = TRY(impl_from(vm, global_object));
return JS::js_string(vm, impl->associated_document().origin().serialize());
}
JS_DEFINE_NATIVE_FUNCTION(WindowObject::local_storage_getter) JS_DEFINE_NATIVE_FUNCTION(WindowObject::local_storage_getter)
{ {
auto* impl = TRY(impl_from(vm, global_object)); auto* impl = TRY(impl_from(vm, global_object));

View file

@ -97,6 +97,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(post_message); JS_DECLARE_NATIVE_FUNCTION(post_message);
JS_DECLARE_NATIVE_FUNCTION(local_storage_getter); JS_DECLARE_NATIVE_FUNCTION(local_storage_getter);
JS_DECLARE_NATIVE_FUNCTION(origin_getter);
JS_DECLARE_NATIVE_FUNCTION(alert); JS_DECLARE_NATIVE_FUNCTION(alert);
JS_DECLARE_NATIVE_FUNCTION(confirm); JS_DECLARE_NATIVE_FUNCTION(confirm);

View file

@ -66,6 +66,32 @@ public:
return false; return false;
} }
// https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin
String serialize()
{
// 1. If origin is an opaque origin, then return "null"
if (is_opaque())
return "null";
// 2. Otherwise, let result be origin's scheme.
StringBuilder result;
result.append(protocol());
// 3. Append "://" to result.
result.append("://");
// 4. Append origin's host, serialized, to result.
result.append(host());
// 5. If origin's port is non-null, append a U+003A COLON character (:), and origin's port, serialized, to result.
if (port() != 0) {
result.append(":");
result.append(String::number(port()));
}
// 6. Return result
return result.to_string();
}
bool operator==(Origin const& other) const { return is_same_origin(other); } bool operator==(Origin const& other) const { return is_same_origin(other); }
bool operator!=(Origin const& other) const { return !is_same_origin(other); } bool operator!=(Origin const& other) const { return !is_same_origin(other); }