FormData.cpp 7.0 KB

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