FormData.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2023-2024, 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. GC_DEFINE_ALLOCATOR(FormData);
  16. // https://xhr.spec.whatwg.org/#dom-formdata
  17. WebIDL::ExceptionOr<GC::Ref<FormData>> FormData::construct_impl(JS::Realm& realm, GC::Ptr<HTML::HTMLFormElement> form)
  18. {
  19. Vector<FormDataEntry> list;
  20. // 1. If form is given, then:
  21. if (form) {
  22. // 1. Let list be the result of constructing the entry list for form.
  23. auto entry_list = TRY(construct_entry_list(realm, *form));
  24. // 2. If list is null, then throw an "InvalidStateError" DOMException.
  25. if (!entry_list.has_value())
  26. return WebIDL::InvalidStateError::create(realm, "Form element does not contain any entries."_string);
  27. // 3. Set this’s entry list to list.
  28. list = entry_list.release_value();
  29. }
  30. return construct_impl(realm, move(list));
  31. }
  32. WebIDL::ExceptionOr<GC::Ref<FormData>> FormData::construct_impl(JS::Realm& realm, Vector<FormDataEntry> entry_list)
  33. {
  34. return realm.create<FormData>(realm, move(entry_list));
  35. }
  36. WebIDL::ExceptionOr<GC::Ref<FormData>> FormData::create(JS::Realm& realm, Vector<DOMURL::QueryParam> entry_list)
  37. {
  38. Vector<FormDataEntry> list;
  39. list.ensure_capacity(entry_list.size());
  40. for (auto& entry : entry_list)
  41. list.unchecked_append({ .name = move(entry.name), .value = move(entry.value) });
  42. return construct_impl(realm, move(list));
  43. }
  44. WebIDL::ExceptionOr<GC::Ref<FormData>> FormData::create(JS::Realm& realm, Vector<FormDataEntry> entry_list)
  45. {
  46. return construct_impl(realm, move(entry_list));
  47. }
  48. FormData::FormData(JS::Realm& realm, Vector<FormDataEntry> entry_list)
  49. : PlatformObject(realm)
  50. , m_entry_list(move(entry_list))
  51. {
  52. }
  53. FormData::~FormData() = default;
  54. void FormData::initialize(JS::Realm& realm)
  55. {
  56. Base::initialize(realm);
  57. WEB_SET_PROTOTYPE_FOR_INTERFACE(FormData);
  58. }
  59. // https://xhr.spec.whatwg.org/#dom-formdata-append
  60. WebIDL::ExceptionOr<void> FormData::append(String const& name, String const& value)
  61. {
  62. return append_impl(name, value);
  63. }
  64. // https://xhr.spec.whatwg.org/#dom-formdata-append-blob
  65. WebIDL::ExceptionOr<void> FormData::append(String const& name, GC::Ref<FileAPI::Blob> const& blob_value, Optional<String> const& filename)
  66. {
  67. auto inner_filename = filename.has_value() ? filename.value() : Optional<String> {};
  68. return append_impl(name, blob_value, inner_filename);
  69. }
  70. // https://xhr.spec.whatwg.org/#dom-formdata-append
  71. // https://xhr.spec.whatwg.org/#dom-formdata-append-blob
  72. WebIDL::ExceptionOr<void> FormData::append_impl(String const& name, Variant<GC::Ref<FileAPI::Blob>, String> const& value, Optional<String> const& filename)
  73. {
  74. auto& realm = this->realm();
  75. auto& vm = realm.vm();
  76. // 1. Let value be value if given; otherwise blobValue.
  77. // 2. Let entry be the result of creating an entry with name, value, and filename if given.
  78. auto entry = TRY(HTML::create_entry(realm, name, value, filename));
  79. // 3. Append entry to this’s entry list.
  80. TRY_OR_THROW_OOM(vm, m_entry_list.try_append(move(entry)));
  81. return {};
  82. }
  83. // https://xhr.spec.whatwg.org/#dom-formdata-delete
  84. void FormData::delete_(String const& name)
  85. {
  86. // The delete(name) method steps are to remove all entries whose name is name from this’s entry list.
  87. m_entry_list.remove_all_matching([&name](FormDataEntry const& entry) {
  88. return entry.name == name;
  89. });
  90. }
  91. // https://xhr.spec.whatwg.org/#dom-formdata-get
  92. Variant<GC::Root<FileAPI::File>, String, Empty> FormData::get(String const& name)
  93. {
  94. // 1. If there is no entry whose name is name in this’s entry list, then return null.
  95. auto entry_iterator = m_entry_list.find_if([&name](FormDataEntry const& entry) {
  96. return entry.name == name;
  97. });
  98. if (entry_iterator.is_end())
  99. return Empty {};
  100. // 2. Return the value of the first entry whose name is name from this’s entry list.
  101. return entry_iterator->value;
  102. }
  103. // https://xhr.spec.whatwg.org/#dom-formdata-getall
  104. WebIDL::ExceptionOr<Vector<FormDataEntryValue>> FormData::get_all(String const& name)
  105. {
  106. // 1. If there is no entry whose name is name in this’s entry list, then return the empty list.
  107. // 2. Return the values of all entries whose name is name, in order, from this’s entry list.
  108. Vector<FormDataEntryValue> values;
  109. for (auto const& entry : m_entry_list) {
  110. if (entry.name == name)
  111. TRY_OR_THROW_OOM(vm(), values.try_append(entry.value));
  112. }
  113. return values;
  114. }
  115. // https://xhr.spec.whatwg.org/#dom-formdata-has
  116. bool FormData::has(String const& name)
  117. {
  118. // 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.
  119. return !m_entry_list.find_if([&name](auto& entry) {
  120. return entry.name == name;
  121. })
  122. .is_end();
  123. }
  124. // https://xhr.spec.whatwg.org/#dom-formdata-set
  125. WebIDL::ExceptionOr<void> FormData::set(String const& name, String const& value)
  126. {
  127. return set_impl(name, value);
  128. }
  129. // https://xhr.spec.whatwg.org/#dom-formdata-set-blob
  130. WebIDL::ExceptionOr<void> FormData::set(String const& name, GC::Ref<FileAPI::Blob> const& blob_value, Optional<String> const& filename)
  131. {
  132. auto inner_filename = filename.has_value() ? filename.value() : Optional<String> {};
  133. return set_impl(name, blob_value, inner_filename);
  134. }
  135. // https://xhr.spec.whatwg.org/#dom-formdata-set
  136. // https://xhr.spec.whatwg.org/#dom-formdata-set-blob
  137. WebIDL::ExceptionOr<void> FormData::set_impl(String const& name, Variant<GC::Ref<FileAPI::Blob>, String> const& value, Optional<String> const& filename)
  138. {
  139. auto& realm = this->realm();
  140. auto& vm = realm.vm();
  141. // 1. Let value be value if given; otherwise blobValue.
  142. // 2. Let entry be the result of creating an entry with name, value, and filename if given.
  143. auto entry = TRY(HTML::create_entry(realm, name, value, filename));
  144. auto existing = m_entry_list.find_if([&name](auto& entry) {
  145. return entry.name == name;
  146. });
  147. // 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.
  148. if (!existing.is_end()) {
  149. existing->value = entry.value;
  150. m_entry_list.remove_all_matching([&name, &existing](auto& entry) {
  151. return &entry != &*existing && entry.name == name;
  152. });
  153. }
  154. // 4. Otherwise, append entry to this’s entry list.
  155. else {
  156. TRY_OR_THROW_OOM(vm, m_entry_list.try_append(move(entry)));
  157. }
  158. return {};
  159. }
  160. JS::ThrowCompletionOr<void> FormData::for_each(ForEachCallback callback)
  161. {
  162. for (auto i = 0u; i < m_entry_list.size(); ++i) {
  163. auto& entry = m_entry_list[i];
  164. TRY(callback(entry.name, entry.value));
  165. }
  166. return {};
  167. }
  168. }