diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index e622477d62..8ea8645172 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -95,6 +95,26 @@ WebIDL::ExceptionOr XMLHttpRequest::response_text() const return get_text_response(); } +// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsetype +WebIDL::ExceptionOr XMLHttpRequest::set_response_type(Bindings::XMLHttpRequestResponseType response_type) +{ + // 1. If the current global object is not a Window object and the given value is "document", then return. + if (!is(HTML::current_global_object()) && response_type == Bindings::XMLHttpRequestResponseType::Document) + return {}; + + // 2. If this’s state is loading or done, then throw an "InvalidStateError" DOMException. + if (m_ready_state == ReadyState::Loading || m_ready_state == ReadyState::Done) + return WebIDL::InvalidStateError::create(realm(), "Can't readyState when XHR is loading or done"); + + // 3. If the current global object is a Window object and this’s synchronous flag is set, then throw an "InvalidAccessError" DOMException. + if (is(HTML::current_global_object()) && m_synchronous) + return WebIDL::InvalidAccessError::create(realm(), "Can't set readyState on synchronous XHR in Window environment"); + + // 4. Set this’s response type to the given value. + m_response_type = response_type; + return {}; +} + // https://xhr.spec.whatwg.org/#response WebIDL::ExceptionOr XMLHttpRequest::response() { diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h index a9db911a40..64d74a03f8 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h @@ -50,7 +50,7 @@ public: WebIDL::ExceptionOr send(Optional body); WebIDL::ExceptionOr set_request_header(String const& header, String const& value); - void set_response_type(Bindings::XMLHttpRequestResponseType type) { m_response_type = type; } + WebIDL::ExceptionOr set_response_type(Bindings::XMLHttpRequestResponseType); String get_response_header(String const& name) { return m_response_headers.get(name).value_or({}); } String get_all_response_headers() const;