From 4036f15728b50d0df2eed1b8264282eb28aff8c8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 8 Apr 2020 21:18:41 +0200 Subject: [PATCH] LibWeb: Support relative URL's in XMLHttpRequest In order to complete a relative URL, we need a Document. Fix this by giving XMLHttpRequest a pointer to its window object. Then we can go from the window to the document, and then we're home free. :^) --- Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp | 3 ++- Libraries/LibWeb/DOM/XMLHttpRequest.cpp | 7 +++++-- Libraries/LibWeb/DOM/XMLHttpRequest.h | 6 ++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp index e4d7b9238b..9f8bb9131f 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp @@ -50,7 +50,8 @@ JS::Value XMLHttpRequestConstructor::call(JS::Interpreter& interpreter) JS::Value XMLHttpRequestConstructor::construct(JS::Interpreter& interpreter) { - return interpreter.heap().allocate(XMLHttpRequest::create()); + auto& window = static_cast(interpreter.global_object()); + return interpreter.heap().allocate(XMLHttpRequest::create(window.impl())); } } diff --git a/Libraries/LibWeb/DOM/XMLHttpRequest.cpp b/Libraries/LibWeb/DOM/XMLHttpRequest.cpp index d28ad8935d..82caf5b71b 100644 --- a/Libraries/LibWeb/DOM/XMLHttpRequest.cpp +++ b/Libraries/LibWeb/DOM/XMLHttpRequest.cpp @@ -28,14 +28,17 @@ #include #include #include +#include #include #include +#include #include #include namespace Web { -XMLHttpRequest::XMLHttpRequest() +XMLHttpRequest::XMLHttpRequest(Window& window) + : m_window(window) { } @@ -59,7 +62,7 @@ void XMLHttpRequest::open(const String& method, const String& url) void XMLHttpRequest::send() { ResourceLoader::the().load( - URL(m_url), + m_window->document().complete_url(m_url), [weak_this = make_weak_ptr()](auto& data) { if (!weak_this) return; diff --git a/Libraries/LibWeb/DOM/XMLHttpRequest.h b/Libraries/LibWeb/DOM/XMLHttpRequest.h index f2924977aa..88c197be07 100644 --- a/Libraries/LibWeb/DOM/XMLHttpRequest.h +++ b/Libraries/LibWeb/DOM/XMLHttpRequest.h @@ -42,7 +42,7 @@ class XMLHttpRequest final public: using WrapperType = Bindings::XMLHttpRequestWrapper; - static NonnullRefPtr create() { return adopt(*new XMLHttpRequest); } + static NonnullRefPtr create(Window& window) { return adopt(*new XMLHttpRequest(window)); } virtual ~XMLHttpRequest() override; @@ -58,7 +58,9 @@ private: virtual void unref_event_target() override { unref(); } virtual void dispatch_event(NonnullRefPtr) override; - XMLHttpRequest(); + explicit XMLHttpRequest(Window&); + + NonnullRefPtr m_window; String m_method; String m_url;