FormData.cpp 6.7 KB

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