FormData.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #include <LibWeb/XHR/FormDataEntry.h>
  13. namespace Web::XHR {
  14. // https://xhr.spec.whatwg.org/#interface-formdata
  15. class FormData : public Bindings::PlatformObject {
  16. WEB_PLATFORM_OBJECT(FormData, Bindings::PlatformObject);
  17. JS_DECLARE_ALLOCATOR(FormData);
  18. public:
  19. virtual ~FormData() override;
  20. static WebIDL::ExceptionOr<JS::NonnullGCPtr<FormData>> construct_impl(JS::Realm&, Optional<JS::NonnullGCPtr<HTML::HTMLFormElement>> form = {});
  21. static WebIDL::ExceptionOr<JS::NonnullGCPtr<FormData>> construct_impl(JS::Realm&, Vector<FormDataEntry> entry_list);
  22. WebIDL::ExceptionOr<void> append(String const& name, String const& value);
  23. WebIDL::ExceptionOr<void> append(String const& name, JS::NonnullGCPtr<FileAPI::Blob> const& blob_value, Optional<String> const& filename = {});
  24. void delete_(String const& name);
  25. Variant<JS::Handle<FileAPI::File>, String, Empty> get(String const& name);
  26. WebIDL::ExceptionOr<Vector<FormDataEntryValue>> get_all(String const& name);
  27. bool has(String const& name);
  28. WebIDL::ExceptionOr<void> set(String const& name, String const& value);
  29. WebIDL::ExceptionOr<void> set(String const& name, JS::NonnullGCPtr<FileAPI::Blob> const& blob_value, Optional<String> const& filename = {});
  30. Vector<FormDataEntry> const& entry_list() const { return m_entry_list; }
  31. using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, FormDataEntryValue const&)>;
  32. JS::ThrowCompletionOr<void> for_each(ForEachCallback);
  33. private:
  34. friend class FormDataIterator;
  35. explicit FormData(JS::Realm&, Vector<FormDataEntry> entry_list = {});
  36. virtual void initialize(JS::Realm&) override;
  37. WebIDL::ExceptionOr<void> append_impl(String const& name, Variant<JS::NonnullGCPtr<FileAPI::Blob>, String> const& value, Optional<String> const& filename = {});
  38. WebIDL::ExceptionOr<void> set_impl(String const& name, Variant<JS::NonnullGCPtr<FileAPI::Blob>, String> const& value, Optional<String> const& filename = {});
  39. Vector<FormDataEntry> m_entry_list;
  40. };
  41. }