FormData.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibJS/Runtime/Completion.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/FileAPI/Blob.h>
  10. #include <LibWeb/FileAPI/File.h>
  11. #include <LibWeb/HTML/FormControlInfrastructure.h>
  12. #include <LibWeb/WebIDL/DOMException.h>
  13. #include <LibWeb/XHR/FormData.h>
  14. namespace Web::XHR {
  15. // https://xhr.spec.whatwg.org/#dom-formdata
  16. WebIDL::ExceptionOr<JS::NonnullGCPtr<FormData>> FormData::construct_impl(JS::Realm& realm, Optional<JS::NonnullGCPtr<HTML::HTMLFormElement>> form)
  17. {
  18. HashMap<DeprecatedString, Vector<FormDataEntryValue>> list;
  19. // 1. If form is given, then:
  20. if (form.has_value()) {
  21. // 1. Let list be the result of constructing the entry list for form.
  22. auto entry_list = TRY(construct_entry_list(realm, form.value()));
  23. // 2. If list is null, then throw an "InvalidStateError" DOMException.
  24. if (!entry_list.has_value())
  25. return WebIDL::InvalidStateError::create(realm, "Form element does not contain any entries.");
  26. // 3. Set this’s entry list to list.
  27. list = entry_list.release_value();
  28. }
  29. return construct_impl(realm, move(list));
  30. }
  31. WebIDL::ExceptionOr<JS::NonnullGCPtr<FormData>> FormData::construct_impl(JS::Realm& realm, HashMap<DeprecatedString, Vector<FormDataEntryValue>> entry_list)
  32. {
  33. return MUST_OR_THROW_OOM(realm.heap().allocate<FormData>(realm, realm, move(entry_list)));
  34. }
  35. FormData::FormData(JS::Realm& realm, HashMap<DeprecatedString, Vector<FormDataEntryValue>> entry_list)
  36. : PlatformObject(realm)
  37. , m_entry_list(move(entry_list))
  38. {
  39. set_prototype(&Bindings::ensure_web_prototype<Bindings::FormDataPrototype>(realm, "FormData"));
  40. }
  41. FormData::~FormData() = default;
  42. void FormData::visit_edges(Cell::Visitor& visitor)
  43. {
  44. Base::visit_edges(visitor);
  45. for (auto const& entry : m_entry_list) {
  46. for (auto const& value : entry.value) {
  47. if (auto* file = value.get_pointer<JS::NonnullGCPtr<FileAPI::File>>())
  48. visitor.visit(*file);
  49. }
  50. }
  51. }
  52. // https://xhr.spec.whatwg.org/#dom-formdata-append
  53. WebIDL::ExceptionOr<void> FormData::append(DeprecatedString const& name, DeprecatedString const& value)
  54. {
  55. auto& vm = realm().vm();
  56. return append_impl(TRY_OR_THROW_OOM(vm, String::from_deprecated_string(name)), TRY_OR_THROW_OOM(vm, String::from_deprecated_string(value)));
  57. }
  58. // https://xhr.spec.whatwg.org/#dom-formdata-append-blob
  59. WebIDL::ExceptionOr<void> FormData::append(DeprecatedString const& name, JS::NonnullGCPtr<FileAPI::Blob> const& blob_value, Optional<DeprecatedString> const& filename)
  60. {
  61. auto& vm = realm().vm();
  62. auto inner_filename = filename.has_value() ? TRY_OR_THROW_OOM(vm, String::from_deprecated_string(filename.value())) : Optional<String> {};
  63. return append_impl(TRY_OR_THROW_OOM(vm, String::from_deprecated_string(name)), blob_value, inner_filename);
  64. }
  65. // https://xhr.spec.whatwg.org/#dom-formdata-append
  66. // https://xhr.spec.whatwg.org/#dom-formdata-append-blob
  67. WebIDL::ExceptionOr<void> FormData::append_impl(String const& name, Variant<JS::NonnullGCPtr<FileAPI::Blob>, String> const& value, Optional<String> const& filename)
  68. {
  69. auto& realm = this->realm();
  70. auto& vm = realm.vm();
  71. // 1. Let value be value if given; otherwise blobValue.
  72. // 2. Let entry be the result of creating an entry with name, value, and filename if given.
  73. auto entry = TRY(HTML::create_entry(realm, name, value, filename));
  74. // FIXME: Remove this when our binding generator supports "new string".
  75. auto form_data_entry_value = entry.value.has<String>()
  76. ? FormDataEntryValue { entry.value.get<String>().to_deprecated_string() }
  77. : FormDataEntryValue { entry.value.get<JS::NonnullGCPtr<FileAPI::File>>() };
  78. // 3. Append entry to this’s entry list.
  79. if (auto entries = m_entry_list.get(entry.name.to_deprecated_string()); entries.has_value() && !entries->is_empty())
  80. TRY_OR_THROW_OOM(vm, entries->try_append(form_data_entry_value));
  81. else
  82. TRY_OR_THROW_OOM(vm, m_entry_list.try_set(entry.name.to_deprecated_string(), { form_data_entry_value }));
  83. return {};
  84. }
  85. // https://xhr.spec.whatwg.org/#dom-formdata-delete
  86. void FormData::delete_(DeprecatedString const& name)
  87. {
  88. // The delete(name) method steps are to remove all entries whose name is name from this’s entry list.
  89. m_entry_list.remove(name);
  90. }
  91. // https://xhr.spec.whatwg.org/#dom-formdata-get
  92. Variant<JS::NonnullGCPtr<FileAPI::File>, DeprecatedString, Empty> FormData::get(DeprecatedString const& name)
  93. {
  94. // 1. If there is no entry whose name is name in this’s entry list, then return null.
  95. if (!m_entry_list.contains(name))
  96. return Empty {};
  97. // 2. Return the value of the first entry whose name is name from this’s entry list.
  98. return m_entry_list.get(name)->at(0);
  99. }
  100. // https://xhr.spec.whatwg.org/#dom-formdata-getall
  101. Vector<FormDataEntryValue> FormData::get_all(DeprecatedString const& name)
  102. {
  103. // 1. If there is no entry whose name is name in this’s entry list, then return the empty list.
  104. if (!m_entry_list.contains(name))
  105. return {};
  106. // 2. Return the values of all entries whose name is name, in order, from this’s entry list.
  107. return *m_entry_list.get(name);
  108. }
  109. // https://xhr.spec.whatwg.org/#dom-formdata-has
  110. bool FormData::has(DeprecatedString const& name)
  111. {
  112. // 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.
  113. return m_entry_list.contains(name);
  114. }
  115. // https://xhr.spec.whatwg.org/#dom-formdata-set
  116. WebIDL::ExceptionOr<void> FormData::set(DeprecatedString const& name, DeprecatedString const& value)
  117. {
  118. auto& vm = realm().vm();
  119. return set_impl(TRY_OR_THROW_OOM(vm, String::from_deprecated_string(name)), TRY_OR_THROW_OOM(vm, String::from_deprecated_string(value)));
  120. }
  121. // https://xhr.spec.whatwg.org/#dom-formdata-set-blob
  122. WebIDL::ExceptionOr<void> FormData::set(DeprecatedString const& name, JS::NonnullGCPtr<FileAPI::Blob> const& blob_value, Optional<DeprecatedString> const& filename)
  123. {
  124. auto& vm = realm().vm();
  125. auto inner_filename = filename.has_value() ? TRY_OR_THROW_OOM(vm, String::from_deprecated_string(filename.value())) : Optional<String> {};
  126. return set_impl(TRY_OR_THROW_OOM(vm, String::from_deprecated_string(name)), blob_value, inner_filename);
  127. }
  128. // https://xhr.spec.whatwg.org/#dom-formdata-set
  129. // https://xhr.spec.whatwg.org/#dom-formdata-set-blob
  130. WebIDL::ExceptionOr<void> FormData::set_impl(String const& name, Variant<JS::NonnullGCPtr<FileAPI::Blob>, String> const& value, Optional<String> const& filename)
  131. {
  132. auto& realm = this->realm();
  133. auto& vm = realm.vm();
  134. // 1. Let value be value if given; otherwise blobValue.
  135. // 2. Let entry be the result of creating an entry with name, value, and filename if given.
  136. auto entry = TRY(HTML::create_entry(realm, name, value, filename));
  137. // FIXME: Remove this when our binding generator supports "new string".
  138. auto form_data_entry_value = entry.value.has<String>()
  139. ? FormDataEntryValue { entry.value.get<String>().to_deprecated_string() }
  140. : FormDataEntryValue { entry.value.get<JS::NonnullGCPtr<FileAPI::File>>() };
  141. // 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.
  142. if (auto entries = m_entry_list.get(entry.name.to_deprecated_string()); entries.has_value() && !entries->is_empty()) {
  143. entries->remove(0, entries->size());
  144. TRY_OR_THROW_OOM(vm, entries->try_append(form_data_entry_value));
  145. }
  146. // 4. Otherwise, append entry to this’s entry list.
  147. else {
  148. TRY_OR_THROW_OOM(vm, m_entry_list.try_set(entry.name.to_deprecated_string(), { form_data_entry_value }));
  149. }
  150. return {};
  151. }
  152. }