FormData.h 2.4 KB

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