FormData.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. Vector<FormDataEntry> const& entry_list() const { return m_entry_list; }
  35. using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, FormDataEntryValue const&)>;
  36. JS::ThrowCompletionOr<void> for_each(ForEachCallback);
  37. private:
  38. friend class FormDataIterator;
  39. explicit FormData(JS::Realm&, Vector<FormDataEntry> entry_list = {});
  40. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  41. WebIDL::ExceptionOr<void> append_impl(String const& name, Variant<JS::NonnullGCPtr<FileAPI::Blob>, String> const& value, Optional<String> const& filename = {});
  42. WebIDL::ExceptionOr<void> set_impl(String const& name, Variant<JS::NonnullGCPtr<FileAPI::Blob>, String> const& value, Optional<String> const& filename = {});
  43. Vector<FormDataEntry> m_entry_list;
  44. };
  45. }