Skip to content

Commit

Permalink
LibWeb: Update FormData constructor to reflect the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
shlyakpavel committed Dec 22, 2024
1 parent b6561f5 commit 7785a10
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
30 changes: 26 additions & 4 deletions Libraries/LibWeb/XHR/FormData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/FileAPI/File.h>
#include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/HTML/FormControlInfrastructure.h>
#include <LibWeb/HTML/HTMLFormElement.h>
#include <LibWeb/WebIDL/DOMException.h>
#include <LibWeb/XHR/FormData.h>

Expand All @@ -18,17 +20,37 @@ namespace Web::XHR {
GC_DEFINE_ALLOCATOR(FormData);

// https://xhr.spec.whatwg.org/#dom-formdata
WebIDL::ExceptionOr<GC::Ref<FormData>> FormData::construct_impl(JS::Realm& realm, GC::Ptr<HTML::HTMLFormElement> form)
WebIDL::ExceptionOr<GC::Ref<FormData>> FormData::construct_impl(JS::Realm& realm, GC::Ptr<HTML::HTMLFormElement> form, Optional<GC::Ptr<HTML::HTMLElement>> submitter)
{
Vector<FormDataEntry> list;
// 1. If form is given, then:
if (form) {
// 1. Let list be the result of constructing the entry list for form.
// 1. If submitter is non-null, then:
if (submitter.has_value() && submitter.value()) {
auto& submitter_element = submitter.value();

// 1. If submitter is not a submit button, then throw a TypeError.
if (!is<HTML::FormAssociatedElement>(*submitter_element)) {
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Submitter is not associated with a form."sv };
}
auto* form_associated_element = dynamic_cast<HTML::FormAssociatedElement*>(submitter_element.ptr());

if (!form_associated_element->is_submit_button()) {
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Submitter is not a valid submit button."sv };
}
// 2. If submitter’s form owner is not form, then throw a "NotFoundError" DOMException.
auto* form_owner = form_associated_element->form();
if (form_owner && form_owner != form) {
return WebIDL::NotFoundError::create(realm, "Submitter does not belong to the provided form."_string);
}
}

// 2. Let list be the result of constructing the entry list for form.
auto entry_list = TRY(construct_entry_list(realm, *form));
// 2. If list is null, then throw an "InvalidStateError" DOMException.
// 3. If list is null, then throw an "InvalidStateError" DOMException.
if (!entry_list.has_value())
return WebIDL::InvalidStateError::create(realm, "Form element does not contain any entries."_string);
// 3. Set this’s entry list to list.
// 4. Set this’s entry list to list.
list = entry_list.release_value();
}

Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibWeb/XHR/FormData.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FormData : public Bindings::PlatformObject {
public:
virtual ~FormData() override;

static WebIDL::ExceptionOr<GC::Ref<FormData>> construct_impl(JS::Realm&, GC::Ptr<HTML::HTMLFormElement> form = {});
static WebIDL::ExceptionOr<GC::Ref<FormData>> construct_impl(JS::Realm&, GC::Ptr<HTML::HTMLFormElement> form = {}, Optional<GC::Ptr<HTML::HTMLElement>> submitter = {});
static WebIDL::ExceptionOr<GC::Ref<FormData>> construct_impl(JS::Realm&, Vector<FormDataEntry> entry_list);

static WebIDL::ExceptionOr<GC::Ref<FormData>> create(JS::Realm&, Vector<DOMURL::QueryParam> entry_list);
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibWeb/XHR/FormData.idl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ typedef (File or USVString) FormDataEntryValue;
// https://xhr.spec.whatwg.org/#interface-formdata
[Exposed=(Window,Worker)]
interface FormData {
constructor(optional HTMLFormElement form);
constructor(optional HTMLFormElement form, optional HTMLElement? submitter = null);

undefined append(USVString name, USVString value);
undefined append(USVString name, Blob blobValue, optional USVString filename);
Expand Down

0 comments on commit 7785a10

Please sign in to comment.