LibWeb: Implement XMLHttpRequest.requestType setter according to spec

This commit is contained in:
Andreas Kling 2022-10-24 15:30:54 +02:00
parent e448c74736
commit 455167008d
2 changed files with 21 additions and 1 deletions

View file

@ -95,6 +95,26 @@ WebIDL::ExceptionOr<String> XMLHttpRequest::response_text() const
return get_text_response(); return get_text_response();
} }
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsetype
WebIDL::ExceptionOr<void> 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::Window>(HTML::current_global_object()) && response_type == Bindings::XMLHttpRequestResponseType::Document)
return {};
// 2. If thiss 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 thiss synchronous flag is set, then throw an "InvalidAccessError" DOMException.
if (is<HTML::Window>(HTML::current_global_object()) && m_synchronous)
return WebIDL::InvalidAccessError::create(realm(), "Can't set readyState on synchronous XHR in Window environment");
// 4. Set thiss response type to the given value.
m_response_type = response_type;
return {};
}
// https://xhr.spec.whatwg.org/#response // https://xhr.spec.whatwg.org/#response
WebIDL::ExceptionOr<JS::Value> XMLHttpRequest::response() WebIDL::ExceptionOr<JS::Value> XMLHttpRequest::response()
{ {

View file

@ -50,7 +50,7 @@ public:
WebIDL::ExceptionOr<void> send(Optional<Fetch::XMLHttpRequestBodyInit> body); WebIDL::ExceptionOr<void> send(Optional<Fetch::XMLHttpRequestBodyInit> body);
WebIDL::ExceptionOr<void> set_request_header(String const& header, String const& value); WebIDL::ExceptionOr<void> set_request_header(String const& header, String const& value);
void set_response_type(Bindings::XMLHttpRequestResponseType type) { m_response_type = type; } WebIDL::ExceptionOr<void> set_response_type(Bindings::XMLHttpRequestResponseType);
String get_response_header(String const& name) { return m_response_headers.get(name).value_or({}); } String get_response_header(String const& name) { return m_response_headers.get(name).value_or({}); }
String get_all_response_headers() const; String get_all_response_headers() const;