2023-02-03 17:50:05 -03:00
|
|
|
|
/*
|
2024-07-22 08:49:07 -03:00
|
|
|
|
* Copyright (c) 2023-2024, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2023-02-03 17:50:05 -03:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <AK/TypeCasts.h>
|
|
|
|
|
|
#include <LibJS/Runtime/Completion.h>
|
|
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
|
|
|
|
|
#include <LibWeb/FileAPI/Blob.h>
|
|
|
|
|
|
#include <LibWeb/FileAPI/File.h>
|
2024-12-20 14:26:49 -03:00
|
|
|
|
#include <LibWeb/HTML/FormAssociatedElement.h>
|
2023-02-03 17:50:05 -03:00
|
|
|
|
#include <LibWeb/HTML/FormControlInfrastructure.h>
|
2024-12-20 14:26:49 -03:00
|
|
|
|
#include <LibWeb/HTML/HTMLFormElement.h>
|
2023-02-03 17:50:05 -03:00
|
|
|
|
#include <LibWeb/WebIDL/DOMException.h>
|
|
|
|
|
|
#include <LibWeb/XHR/FormData.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Web::XHR {
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC_DEFINE_ALLOCATOR(FormData);
|
2023-11-19 15:47:52 -03:00
|
|
|
|
|
2023-02-03 17:50:05 -03:00
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata
|
2024-12-20 14:26:49 -03:00
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<FormData>> FormData::construct_impl(JS::Realm& realm, GC::Ptr<HTML::HTMLFormElement> form, GC::Ptr<HTML::HTMLElement> submitter)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
2026-05-19 15:55:11 -03:00
|
|
|
|
GC::ConservativeVector<FormDataEntry> list;
|
2023-02-03 17:50:31 -03:00
|
|
|
|
// 1. If form is given, then:
|
2024-04-07 10:10:36 -03:00
|
|
|
|
if (form) {
|
2024-12-20 14:26:49 -03:00
|
|
|
|
// 1. If submitter is non-null, then:
|
|
|
|
|
|
if (submitter) {
|
|
|
|
|
|
// 1. If submitter is not a submit button, then throw a TypeError.
|
2025-08-22 07:59:47 -03:00
|
|
|
|
auto form_associated_element = as_if<HTML::FormAssociatedElement>(*submitter);
|
|
|
|
|
|
if (!form_associated_element) {
|
2024-12-20 14:26:49 -03:00
|
|
|
|
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Submitter is not associated with a form."sv };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2025-08-07 20:31:52 -03:00
|
|
|
|
return WebIDL::NotFoundError::create(realm, "Submitter does not belong to the provided form."_utf16);
|
2024-12-20 14:26:49 -03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-15 11:48:55 -03:00
|
|
|
|
// 2. Let list be the result of constructing the entry list for form and submitter.
|
|
|
|
|
|
auto entry_list = TRY(construct_entry_list(realm, *form, submitter));
|
2024-12-20 14:26:49 -03:00
|
|
|
|
// 3. If list is null, then throw an "InvalidStateError" DOMException.
|
2023-02-03 17:50:31 -03:00
|
|
|
|
if (!entry_list.has_value())
|
2025-08-07 20:31:52 -03:00
|
|
|
|
return WebIDL::InvalidStateError::create(realm, "Form element does not contain any entries."_utf16);
|
2024-12-20 14:26:49 -03:00
|
|
|
|
// 4. Set this’s entry list to list.
|
2025-12-26 10:01:53 -03:00
|
|
|
|
list = move(entry_list.release_value());
|
2023-02-03 17:50:31 -03:00
|
|
|
|
}
|
2023-02-03 17:50:05 -03:00
|
|
|
|
|
2023-02-03 17:50:31 -03:00
|
|
|
|
return construct_impl(realm, move(list));
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 10:01:53 -03:00
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<FormData>> FormData::construct_impl(JS::Realm& realm, GC::ConservativeVector<FormDataEntry> entry_list)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
2024-11-13 13:50:17 -03:00
|
|
|
|
return realm.create<FormData>(realm, move(entry_list));
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<FormData>> FormData::create(JS::Realm& realm, Vector<DOMURL::QueryParam> entry_list)
|
2024-07-22 08:49:07 -03:00
|
|
|
|
{
|
2026-05-19 15:55:11 -03:00
|
|
|
|
GC::ConservativeVector<FormDataEntry> list;
|
2024-07-22 08:49:07 -03:00
|
|
|
|
list.ensure_capacity(entry_list.size());
|
|
|
|
|
|
for (auto& entry : entry_list)
|
|
|
|
|
|
list.unchecked_append({ .name = move(entry.name), .value = move(entry.value) });
|
|
|
|
|
|
|
|
|
|
|
|
return construct_impl(realm, move(list));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 10:01:53 -03:00
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<FormData>> FormData::create(JS::Realm& realm, GC::ConservativeVector<FormDataEntry> entry_list)
|
2024-12-25 00:11:33 -03:00
|
|
|
|
{
|
|
|
|
|
|
return construct_impl(realm, move(entry_list));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 10:01:53 -03:00
|
|
|
|
FormData::FormData(JS::Realm& realm, GC::ConservativeVector<FormDataEntry> entry_list)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
: PlatformObject(realm)
|
2025-12-26 10:01:53 -03:00
|
|
|
|
, m_entry_list(entry_list)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FormData::~FormData() = default;
|
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
|
void FormData::initialize(JS::Realm& realm)
|
2023-02-12 16:29:30 -03:00
|
|
|
|
{
|
2024-03-16 09:13:08 -03:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(FormData);
|
2025-04-20 11:22:57 -03:00
|
|
|
|
Base::initialize(realm);
|
2023-02-12 16:29:30 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 10:01:53 -03:00
|
|
|
|
void FormData::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
|
{
|
|
|
|
|
|
Base::visit_edges(visitor);
|
2026-04-14 14:03:06 -03:00
|
|
|
|
for (auto const& entry : m_entry_list)
|
|
|
|
|
|
visitor.visit(entry.value);
|
2025-12-26 10:01:53 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-03 17:50:05 -03:00
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-append
|
2023-02-17 15:57:58 -03:00
|
|
|
|
WebIDL::ExceptionOr<void> FormData::append(String const& name, String const& value)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
2023-02-17 15:57:58 -03:00
|
|
|
|
return append_impl(name, value);
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-append-blob
|
2024-11-14 12:01:23 -03:00
|
|
|
|
WebIDL::ExceptionOr<void> FormData::append(String const& name, GC::Ref<FileAPI::Blob> const& blob_value, Optional<String> const& filename)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
2023-02-17 15:57:58 -03:00
|
|
|
|
auto inner_filename = filename.has_value() ? filename.value() : Optional<String> {};
|
|
|
|
|
|
return append_impl(name, blob_value, inner_filename);
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-append
|
|
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-append-blob
|
2024-11-14 12:01:23 -03:00
|
|
|
|
WebIDL::ExceptionOr<void> FormData::append_impl(String const& name, Variant<GC::Ref<FileAPI::Blob>, String> const& value, Optional<String> const& filename)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
|
|
|
|
|
auto& realm = this->realm();
|
|
|
|
|
|
auto& vm = realm.vm();
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Let value be value if given; otherwise blobValue.
|
|
|
|
|
|
// 2. Let entry be the result of creating an entry with name, value, and filename if given.
|
|
|
|
|
|
auto entry = TRY(HTML::create_entry(realm, name, value, filename));
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Append entry to this’s entry list.
|
2023-02-17 15:57:58 -03:00
|
|
|
|
TRY_OR_THROW_OOM(vm, m_entry_list.try_append(move(entry)));
|
2023-02-03 17:50:05 -03:00
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-delete
|
2023-02-17 15:57:58 -03:00
|
|
|
|
void FormData::delete_(String const& name)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
|
|
|
|
|
// The delete(name) method steps are to remove all entries whose name is name from this’s entry list.
|
2023-02-17 15:57:58 -03:00
|
|
|
|
m_entry_list.remove_all_matching([&name](FormDataEntry const& entry) {
|
|
|
|
|
|
return entry.name == name;
|
|
|
|
|
|
});
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-get
|
2025-12-26 03:18:39 -03:00
|
|
|
|
Variant<GC::Ref<FileAPI::File>, String, Empty> FormData::get(String const& name)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
|
|
|
|
|
// 1. If there is no entry whose name is name in this’s entry list, then return null.
|
2023-02-17 15:57:58 -03:00
|
|
|
|
auto entry_iterator = m_entry_list.find_if([&name](FormDataEntry const& entry) {
|
|
|
|
|
|
return entry.name == name;
|
|
|
|
|
|
});
|
|
|
|
|
|
if (entry_iterator.is_end())
|
2023-02-03 17:50:05 -03:00
|
|
|
|
return Empty {};
|
|
|
|
|
|
// 2. Return the value of the first entry whose name is name from this’s entry list.
|
2023-02-17 15:57:58 -03:00
|
|
|
|
return entry_iterator->value;
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-getall
|
2023-02-17 15:57:58 -03:00
|
|
|
|
WebIDL::ExceptionOr<Vector<FormDataEntryValue>> FormData::get_all(String const& name)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
|
|
|
|
|
// 1. If there is no entry whose name is name in this’s entry list, then return the empty list.
|
|
|
|
|
|
// 2. Return the values of all entries whose name is name, in order, from this’s entry list.
|
2023-02-17 15:57:58 -03:00
|
|
|
|
Vector<FormDataEntryValue> values;
|
|
|
|
|
|
for (auto const& entry : m_entry_list) {
|
|
|
|
|
|
if (entry.name == name)
|
|
|
|
|
|
TRY_OR_THROW_OOM(vm(), values.try_append(entry.value));
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-has
|
2023-02-17 15:57:58 -03:00
|
|
|
|
bool FormData::has(String const& name)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
|
|
|
|
|
// The has(name) method steps are to return true if there is an entry whose name is name in this’s entry list; otherwise false.
|
2023-02-17 15:57:58 -03:00
|
|
|
|
return !m_entry_list.find_if([&name](auto& entry) {
|
|
|
|
|
|
return entry.name == name;
|
|
|
|
|
|
})
|
|
|
|
|
|
.is_end();
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-set
|
2023-02-17 15:57:58 -03:00
|
|
|
|
WebIDL::ExceptionOr<void> FormData::set(String const& name, String const& value)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
2023-02-17 15:57:58 -03:00
|
|
|
|
return set_impl(name, value);
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-set-blob
|
2024-11-14 12:01:23 -03:00
|
|
|
|
WebIDL::ExceptionOr<void> FormData::set(String const& name, GC::Ref<FileAPI::Blob> const& blob_value, Optional<String> const& filename)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
2023-02-17 15:57:58 -03:00
|
|
|
|
auto inner_filename = filename.has_value() ? filename.value() : Optional<String> {};
|
|
|
|
|
|
return set_impl(name, blob_value, inner_filename);
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 10:01:53 -03:00
|
|
|
|
GC::ConservativeVector<FormDataEntry> FormData::entry_list() const
|
|
|
|
|
|
{
|
2026-05-19 15:55:11 -03:00
|
|
|
|
return GC::ConservativeVector<FormDataEntry> { m_entry_list };
|
2025-12-26 10:01:53 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-03 17:50:05 -03:00
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-set
|
|
|
|
|
|
// https://xhr.spec.whatwg.org/#dom-formdata-set-blob
|
2024-11-14 12:01:23 -03:00
|
|
|
|
WebIDL::ExceptionOr<void> FormData::set_impl(String const& name, Variant<GC::Ref<FileAPI::Blob>, String> const& value, Optional<String> const& filename)
|
2023-02-03 17:50:05 -03:00
|
|
|
|
{
|
|
|
|
|
|
auto& realm = this->realm();
|
|
|
|
|
|
auto& vm = realm.vm();
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Let value be value if given; otherwise blobValue.
|
|
|
|
|
|
// 2. Let entry be the result of creating an entry with name, value, and filename if given.
|
|
|
|
|
|
auto entry = TRY(HTML::create_entry(realm, name, value, filename));
|
|
|
|
|
|
|
2023-02-17 15:57:58 -03:00
|
|
|
|
auto existing = m_entry_list.find_if([&name](auto& entry) {
|
|
|
|
|
|
return entry.name == name;
|
|
|
|
|
|
});
|
2023-02-03 17:50:05 -03:00
|
|
|
|
|
|
|
|
|
|
// 3. If there are entries in this’s entry list whose name is name, then replace the first such entry with entry and remove the others.
|
2023-02-17 15:57:58 -03:00
|
|
|
|
if (!existing.is_end()) {
|
|
|
|
|
|
existing->value = entry.value;
|
|
|
|
|
|
m_entry_list.remove_all_matching([&name, &existing](auto& entry) {
|
|
|
|
|
|
return &entry != &*existing && entry.name == name;
|
|
|
|
|
|
});
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
// 4. Otherwise, append entry to this’s entry list.
|
|
|
|
|
|
else {
|
2023-02-17 15:57:58 -03:00
|
|
|
|
TRY_OR_THROW_OOM(vm, m_entry_list.try_append(move(entry)));
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-05 13:29:11 -03:00
|
|
|
|
JS::ThrowCompletionOr<void> FormData::for_each(ForEachCallback callback)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto i = 0u; i < m_entry_list.size(); ++i) {
|
|
|
|
|
|
auto& entry = m_entry_list[i];
|
|
|
|
|
|
TRY(callback(entry.name, entry.value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-03 17:50:05 -03:00
|
|
|
|
}
|