From 2935e24492e496345e267f85fe09771010571ea2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 1 May 2024 11:10:15 +0200 Subject: [PATCH] LibWeb: Implement XMLHttpRequest.responseURL This was used on https://twinings.co.uk/ so let's support it :^) --- .../expected/XHR/XMLHttpRequest-responseURL.txt | 2 ++ .../input/XHR/XMLHttpRequest-responseURL.html | 15 +++++++++++++++ Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp | 12 ++++++++++++ Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h | 1 + Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl | 2 +- 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 Tests/LibWeb/Text/expected/XHR/XMLHttpRequest-responseURL.txt create mode 100644 Tests/LibWeb/Text/input/XHR/XMLHttpRequest-responseURL.html diff --git a/Tests/LibWeb/Text/expected/XHR/XMLHttpRequest-responseURL.txt b/Tests/LibWeb/Text/expected/XHR/XMLHttpRequest-responseURL.txt new file mode 100644 index 00000000000000..cdecef5659bc2f --- /dev/null +++ b/Tests/LibWeb/Text/expected/XHR/XMLHttpRequest-responseURL.txt @@ -0,0 +1,2 @@ +responseURL before: '' +responseURL after: 'data:text/html,hello' diff --git a/Tests/LibWeb/Text/input/XHR/XMLHttpRequest-responseURL.html b/Tests/LibWeb/Text/input/XHR/XMLHttpRequest-responseURL.html new file mode 100644 index 00000000000000..19a2c72b4e1535 --- /dev/null +++ b/Tests/LibWeb/Text/input/XHR/XMLHttpRequest-responseURL.html @@ -0,0 +1,15 @@ + + diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index 426246e996e490..3d8598fe3f75d3 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -1265,4 +1265,16 @@ JS::ThrowCompletionOr XMLHttpRequest::request_error_steps(FlyString const& return {}; } +// https://xhr.spec.whatwg.org/#the-responseurl-attribute +String XMLHttpRequest::response_url() +{ + // The responseURL getter steps are to return the empty string if this’s response’s URL is null; + // otherwise its serialization with the exclude fragment flag set. + if (!m_response->url().has_value()) + return String {}; + + auto serialized = m_response->url().value().serialize(URL::ExcludeFragment::Yes); + return String::from_utf8_without_validation(serialized.bytes()); +} + } diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h index d5b436d8dac3f3..caf744d3426911 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h @@ -52,6 +52,7 @@ class XMLHttpRequest final : public XMLHttpRequestEventTarget { WebIDL::ExceptionOr> response_xml(); WebIDL::ExceptionOr response(); Bindings::XMLHttpRequestResponseType response_type() const { return m_response_type; } + String response_url(); WebIDL::ExceptionOr open(String const& method, String const& url); WebIDL::ExceptionOr open(String const& method, String const& url, bool async, Optional const& username = Optional {}, Optional const& password = Optional {}); diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl index e6223ddb184e64..bb9dae32300947 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl @@ -41,7 +41,7 @@ interface XMLHttpRequest : XMLHttpRequestEventTarget { undefined abort(); // response - // FIXME: readonly attribute USVString responseURL; + readonly attribute USVString responseURL; readonly attribute unsigned short status; readonly attribute ByteString statusText; ByteString? getResponseHeader(ByteString name);