/src/serenity/Userland/Libraries/LibWeb/XHR/FormData.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023-2024, Kenneth Myhra <kennethmyhra@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibWeb/Bindings/FormDataPrototype.h> |
10 | | #include <LibWeb/Bindings/PlatformObject.h> |
11 | | #include <LibWeb/DOMURL/URLSearchParams.h> |
12 | | #include <LibWeb/Forward.h> |
13 | | #include <LibWeb/HTML/HTMLFormElement.h> |
14 | | #include <LibWeb/WebIDL/ExceptionOr.h> |
15 | | #include <LibWeb/XHR/FormDataEntry.h> |
16 | | |
17 | | namespace Web::XHR { |
18 | | |
19 | | // https://xhr.spec.whatwg.org/#interface-formdata |
20 | | class FormData : public Bindings::PlatformObject { |
21 | | WEB_PLATFORM_OBJECT(FormData, Bindings::PlatformObject); |
22 | | JS_DECLARE_ALLOCATOR(FormData); |
23 | | |
24 | | public: |
25 | | virtual ~FormData() override; |
26 | | |
27 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<FormData>> construct_impl(JS::Realm&, JS::GCPtr<HTML::HTMLFormElement> form = {}); |
28 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<FormData>> construct_impl(JS::Realm&, Vector<FormDataEntry> entry_list); |
29 | | |
30 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<FormData>> create(JS::Realm&, Vector<DOMURL::QueryParam> entry_list); |
31 | | |
32 | | WebIDL::ExceptionOr<void> append(String const& name, String const& value); |
33 | | WebIDL::ExceptionOr<void> append(String const& name, JS::NonnullGCPtr<FileAPI::Blob> const& blob_value, Optional<String> const& filename = {}); |
34 | | void delete_(String const& name); |
35 | | Variant<JS::Handle<FileAPI::File>, String, Empty> get(String const& name); |
36 | | WebIDL::ExceptionOr<Vector<FormDataEntryValue>> get_all(String const& name); |
37 | | bool has(String const& name); |
38 | | WebIDL::ExceptionOr<void> set(String const& name, String const& value); |
39 | | WebIDL::ExceptionOr<void> set(String const& name, JS::NonnullGCPtr<FileAPI::Blob> const& blob_value, Optional<String> const& filename = {}); |
40 | | |
41 | 0 | Vector<FormDataEntry> const& entry_list() const { return m_entry_list; } |
42 | | |
43 | | using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, FormDataEntryValue const&)>; |
44 | | JS::ThrowCompletionOr<void> for_each(ForEachCallback); |
45 | | |
46 | | private: |
47 | | friend class FormDataIterator; |
48 | | |
49 | | explicit FormData(JS::Realm&, Vector<FormDataEntry> entry_list = {}); |
50 | | |
51 | | virtual void initialize(JS::Realm&) override; |
52 | | |
53 | | WebIDL::ExceptionOr<void> append_impl(String const& name, Variant<JS::NonnullGCPtr<FileAPI::Blob>, String> const& value, Optional<String> const& filename = {}); |
54 | | WebIDL::ExceptionOr<void> set_impl(String const& name, Variant<JS::NonnullGCPtr<FileAPI::Blob>, String> const& value, Optional<String> const& filename = {}); |
55 | | |
56 | | Vector<FormDataEntry> m_entry_list; |
57 | | }; |
58 | | |
59 | | } |