mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:37:35 +00:00
LibWeb: Add FormDataIterator implementation
This adds the FormDataIterator implementation so we can iterate over FormData.{keys(),values(),entries()}.
This commit is contained in:
parent
680e970597
commit
be52e7171a
8 changed files with 133 additions and 2 deletions
|
@ -473,6 +473,7 @@ set(SOURCES
|
||||||
WebSockets/WebSocket.cpp
|
WebSockets/WebSocket.cpp
|
||||||
XHR/EventNames.cpp
|
XHR/EventNames.cpp
|
||||||
XHR/FormData.cpp
|
XHR/FormData.cpp
|
||||||
|
XHR/FormDataIterator.cpp
|
||||||
XHR/ProgressEvent.cpp
|
XHR/ProgressEvent.cpp
|
||||||
XHR/XMLHttpRequest.cpp
|
XHR/XMLHttpRequest.cpp
|
||||||
XHR/XMLHttpRequestEventTarget.cpp
|
XHR/XMLHttpRequestEventTarget.cpp
|
||||||
|
|
|
@ -489,6 +489,7 @@ class WebGLRenderingContextBase;
|
||||||
|
|
||||||
namespace Web::XHR {
|
namespace Web::XHR {
|
||||||
class FormData;
|
class FormData;
|
||||||
|
class FormDataIterator;
|
||||||
class ProgressEvent;
|
class ProgressEvent;
|
||||||
class XMLHttpRequest;
|
class XMLHttpRequest;
|
||||||
class XMLHttpRequestEventTarget;
|
class XMLHttpRequestEventTarget;
|
||||||
|
|
|
@ -171,4 +171,14 @@ WebIDL::ExceptionOr<void> FormData::set_impl(String const& name, Variant<JS::Non
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JS::ThrowCompletionOr<void> FormData::for_each(ForEachCallback callback)
|
||||||
|
{
|
||||||
|
for (auto i = 0u; i < m_entry_list.size(); ++i) {
|
||||||
|
auto& entry = m_entry_list[i];
|
||||||
|
TRY(callback(entry.name, entry.value));
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,12 @@ public:
|
||||||
WebIDL::ExceptionOr<void> set(String const& name, String const& value);
|
WebIDL::ExceptionOr<void> set(String const& name, String const& value);
|
||||||
WebIDL::ExceptionOr<void> set(String const& name, JS::NonnullGCPtr<FileAPI::Blob> const& blob_value, Optional<String> const& filename = {});
|
WebIDL::ExceptionOr<void> set(String const& name, JS::NonnullGCPtr<FileAPI::Blob> const& blob_value, Optional<String> const& filename = {});
|
||||||
|
|
||||||
|
using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, FormDataEntryValue const&)>;
|
||||||
|
JS::ThrowCompletionOr<void> for_each(ForEachCallback);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend class FormDataIterator;
|
||||||
|
|
||||||
explicit FormData(JS::Realm&, Vector<FormDataEntry> entry_list = {});
|
explicit FormData(JS::Realm&, Vector<FormDataEntry> entry_list = {});
|
||||||
|
|
||||||
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
|
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
|
||||||
|
|
|
@ -19,5 +19,5 @@ interface FormData {
|
||||||
boolean has(USVString name);
|
boolean has(USVString name);
|
||||||
undefined set(USVString name, USVString value);
|
undefined set(USVString name, USVString value);
|
||||||
undefined set(USVString name, Blob blobValue, optional USVString filename);
|
undefined set(USVString name, Blob blobValue, optional USVString filename);
|
||||||
// FIXME: iterable<USVString, FormDataEntryValue>;
|
iterable<USVString, FormDataEntryValue>;
|
||||||
};
|
};
|
||||||
|
|
79
Userland/Libraries/LibWeb/XHR/FormDataIterator.cpp
Normal file
79
Userland/Libraries/LibWeb/XHR/FormDataIterator.cpp
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/Array.h>
|
||||||
|
#include <LibJS/Runtime/IteratorOperations.h>
|
||||||
|
#include <LibWeb/Bindings/FormDataIteratorPrototype.h>
|
||||||
|
#include <LibWeb/Bindings/Intrinsics.h>
|
||||||
|
#include <LibWeb/XHR/FormDataIterator.h>
|
||||||
|
|
||||||
|
namespace Web::Bindings {
|
||||||
|
|
||||||
|
template<>
|
||||||
|
void Intrinsics::create_web_prototype_and_constructor<FormDataIteratorPrototype>(JS::Realm& realm)
|
||||||
|
{
|
||||||
|
auto prototype = heap().allocate<FormDataIteratorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
|
||||||
|
m_prototypes.set("FormDataIterator"sv, prototype);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Web::XHR {
|
||||||
|
|
||||||
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<FormDataIterator>> FormDataIterator::create(FormData const& form_data, JS::Object::PropertyKind iterator_kind)
|
||||||
|
{
|
||||||
|
return MUST_OR_THROW_OOM(form_data.heap().allocate<FormDataIterator>(form_data.realm(), form_data, iterator_kind));
|
||||||
|
}
|
||||||
|
|
||||||
|
FormDataIterator::FormDataIterator(Web::XHR::FormData const& form_data, JS::Object::PropertyKind iterator_kind)
|
||||||
|
: PlatformObject(form_data.realm())
|
||||||
|
, m_form_data(form_data)
|
||||||
|
, m_iterator_kind(iterator_kind)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
FormDataIterator::~FormDataIterator() = default;
|
||||||
|
|
||||||
|
JS::ThrowCompletionOr<void> FormDataIterator::initialize(JS::Realm& realm)
|
||||||
|
{
|
||||||
|
MUST_OR_THROW_OOM(Base::initialize(realm));
|
||||||
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::FormDataIteratorPrototype>(realm, "FormDataIterator"));
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void FormDataIterator::visit_edges(Cell::Visitor& visitor)
|
||||||
|
{
|
||||||
|
Base::visit_edges(visitor);
|
||||||
|
visitor.visit(&m_form_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
JS::Object* FormDataIterator::next()
|
||||||
|
{
|
||||||
|
auto& vm = this->vm();
|
||||||
|
|
||||||
|
if (m_index >= m_form_data.m_entry_list.size())
|
||||||
|
return create_iterator_result_object(vm, JS::js_undefined(), true);
|
||||||
|
|
||||||
|
auto& entry = m_form_data.m_entry_list[m_index++];
|
||||||
|
if (m_iterator_kind == JS::Object::PropertyKind::Key)
|
||||||
|
return create_iterator_result_object(vm, JS::PrimitiveString::create(vm, entry.name), false);
|
||||||
|
|
||||||
|
auto entry_value = entry.value.visit(
|
||||||
|
[&](JS::Handle<FileAPI::File> const& file) -> JS::Value {
|
||||||
|
return file.cell();
|
||||||
|
},
|
||||||
|
[&](String const& string) -> JS::Value {
|
||||||
|
return JS::PrimitiveString::create(vm, string);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (m_iterator_kind == JS::Object::PropertyKind::Value)
|
||||||
|
return create_iterator_result_object(vm, entry_value, false);
|
||||||
|
|
||||||
|
return create_iterator_result_object(vm, JS::Array::create_from(realm(), { JS::PrimitiveString::create(vm, entry.name), entry_value }), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
35
Userland/Libraries/LibWeb/XHR/FormDataIterator.h
Normal file
35
Userland/Libraries/LibWeb/XHR/FormDataIterator.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibWeb/Bindings/PlatformObject.h>
|
||||||
|
#include <LibWeb/XHR/FormData.h>
|
||||||
|
|
||||||
|
namespace Web::XHR {
|
||||||
|
|
||||||
|
class FormDataIterator : public Bindings::PlatformObject {
|
||||||
|
WEB_PLATFORM_OBJECT(FormDataIterator, Bindings::PlatformObject);
|
||||||
|
|
||||||
|
public:
|
||||||
|
static WebIDL::ExceptionOr<JS::NonnullGCPtr<FormDataIterator>> create(FormData const&, JS::Object::PropertyKind iterator_kind);
|
||||||
|
|
||||||
|
virtual ~FormDataIterator() override;
|
||||||
|
|
||||||
|
JS::Object* next();
|
||||||
|
|
||||||
|
private:
|
||||||
|
FormDataIterator(FormData const&, JS::Object::PropertyKind iterator_kind);
|
||||||
|
|
||||||
|
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
|
||||||
|
virtual void visit_edges(Cell::Visitor&) override;
|
||||||
|
|
||||||
|
FormData const& m_form_data;
|
||||||
|
JS::Object::PropertyKind m_iterator_kind;
|
||||||
|
size_t m_index { 0 };
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -203,7 +203,7 @@ libweb_js_bindings(WebGL/WebGLContextEvent)
|
||||||
libweb_js_bindings(WebGL/WebGLRenderingContext)
|
libweb_js_bindings(WebGL/WebGLRenderingContext)
|
||||||
libweb_js_bindings(WebIDL/DOMException)
|
libweb_js_bindings(WebIDL/DOMException)
|
||||||
libweb_js_bindings(WebSockets/WebSocket)
|
libweb_js_bindings(WebSockets/WebSocket)
|
||||||
libweb_js_bindings(XHR/FormData)
|
libweb_js_bindings(XHR/FormData ITERABLE)
|
||||||
libweb_js_bindings(XHR/ProgressEvent)
|
libweb_js_bindings(XHR/ProgressEvent)
|
||||||
libweb_js_bindings(XHR/XMLHttpRequest)
|
libweb_js_bindings(XHR/XMLHttpRequest)
|
||||||
libweb_js_bindings(XHR/XMLHttpRequestEventTarget)
|
libweb_js_bindings(XHR/XMLHttpRequestEventTarget)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue