mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:07:35 +00:00
LibWeb: Make URLSearchParams iterable
This uses the new support for the iterable IDL property.
This commit is contained in:
parent
14e99b9b68
commit
01417c82c5
7 changed files with 101 additions and 2 deletions
|
@ -243,6 +243,7 @@ set(SOURCES
|
|||
UIEvents/MouseEvent.cpp
|
||||
URL/URL.cpp
|
||||
URL/URLSearchParams.cpp
|
||||
URL/URLSearchParamsIterator.cpp
|
||||
WebAssembly/WebAssemblyInstanceConstructor.cpp
|
||||
WebAssembly/WebAssemblyInstanceObject.cpp
|
||||
WebAssembly/WebAssemblyInstanceObjectPrototype.cpp
|
||||
|
@ -458,7 +459,7 @@ libweb_js_wrapper(XHR/ProgressEvent)
|
|||
libweb_js_wrapper(XHR/XMLHttpRequest)
|
||||
libweb_js_wrapper(XHR/XMLHttpRequestEventTarget)
|
||||
libweb_js_wrapper(URL/URL)
|
||||
libweb_js_wrapper(URL/URLSearchParams)
|
||||
libweb_js_wrapper(URL/URLSearchParams ITERABLE)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT CSS/PropertyID.h
|
||||
|
|
|
@ -251,6 +251,7 @@ class XMLHttpRequestEventTarget;
|
|||
namespace Web::URL {
|
||||
class URL;
|
||||
class URLSearchParams;
|
||||
class URLSearchParamsIterator;
|
||||
}
|
||||
|
||||
namespace Web::Bindings {
|
||||
|
@ -391,4 +392,6 @@ class URLWrapper;
|
|||
class URLSearchParamsConstructor;
|
||||
class URLSearchParamsPrototype;
|
||||
class URLSearchParamsWrapper;
|
||||
class URLSearchParamsIteratorPrototype;
|
||||
class URLSearchParamsIteratorWrapper;
|
||||
}
|
||||
|
|
|
@ -195,4 +195,13 @@ String URLSearchParams::to_string()
|
|||
return url_encode(m_list, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded);
|
||||
}
|
||||
|
||||
void URLSearchParams::for_each(Function<IterationDecision(String const&, String const&)> callback)
|
||||
{
|
||||
for (auto i = 0u; i < m_list.size(); ++i) {
|
||||
auto& query_param = m_list[i]; // We are explicitly iterating over the indices here as the callback might delete items from the list
|
||||
if (callback(query_param.name, query_param.value) == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -42,8 +42,11 @@ public:
|
|||
|
||||
String to_string();
|
||||
|
||||
void for_each(Function<IterationDecision(String const&, String const&)>);
|
||||
|
||||
private:
|
||||
friend class URL;
|
||||
friend class URLSearchParamsIterator;
|
||||
|
||||
explicit URLSearchParams(Vector<QueryParam> list)
|
||||
: m_list(move(list)) {};
|
||||
|
|
|
@ -12,6 +12,6 @@ interface URLSearchParams {
|
|||
|
||||
undefined sort();
|
||||
|
||||
// TODO: iterable<USVString, USVString>;
|
||||
iterable<USVString, USVString>;
|
||||
stringifier;
|
||||
};
|
||||
|
|
37
Userland/Libraries/LibWeb/URL/URLSearchParamsIterator.cpp
Normal file
37
Userland/Libraries/LibWeb/URL/URLSearchParamsIterator.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibJS/Runtime/IteratorOperations.h>
|
||||
#include <LibWeb/Bindings/URLSearchParamsIteratorWrapper.h>
|
||||
#include <LibWeb/Bindings/Wrapper.h>
|
||||
#include <LibWeb/URL/URLSearchParamsIterator.h>
|
||||
|
||||
namespace Web::URL {
|
||||
|
||||
JS::Object* URLSearchParamsIterator::next()
|
||||
{
|
||||
auto& global_object = wrapper()->global_object();
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
if (m_index >= m_url_search_params.m_list.size())
|
||||
return create_iterator_result_object(global_object, JS::js_undefined(), true);
|
||||
|
||||
auto& entry = m_url_search_params.m_list[m_index++];
|
||||
if (m_iteration_kind == JS::Object::PropertyKind::Key)
|
||||
return create_iterator_result_object(global_object, JS::js_string(vm, entry.name), false);
|
||||
else if (m_iteration_kind == JS::Object::PropertyKind::Value)
|
||||
return create_iterator_result_object(global_object, JS::js_string(vm, entry.value), false);
|
||||
|
||||
return create_iterator_result_object(global_object, JS::Array::create_from(global_object, { JS::js_string(vm, entry.name), JS::js_string(vm, entry.value) }), false);
|
||||
}
|
||||
|
||||
void URLSearchParamsIterator::visit_edges(JS::Cell::Visitor& visitor)
|
||||
{
|
||||
visitor.visit(m_url_search_params.wrapper());
|
||||
}
|
||||
|
||||
}
|
46
Userland/Libraries/LibWeb/URL/URLSearchParamsIterator.h
Normal file
46
Userland/Libraries/LibWeb/URL/URLSearchParamsIterator.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "URLSearchParams.h"
|
||||
#include <LibWeb/Bindings/Wrappable.h>
|
||||
#include <LibWeb/URL/URLSearchParams.h>
|
||||
|
||||
namespace Web::URL {
|
||||
|
||||
class URLSearchParamsIterator : public Bindings::Wrappable
|
||||
, public RefCounted<URLSearchParamsIterator> {
|
||||
public:
|
||||
using WrapperType = Bindings::URLSearchParamsIteratorWrapper;
|
||||
|
||||
static NonnullRefPtr<URLSearchParamsIterator> create(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind)
|
||||
{
|
||||
return adopt_ref(*new URLSearchParamsIterator(url_search_params, iteration_kind));
|
||||
}
|
||||
|
||||
JS::Object* next();
|
||||
|
||||
void visit_edges(JS::Cell::Visitor&);
|
||||
|
||||
private:
|
||||
explicit URLSearchParamsIterator(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind)
|
||||
: m_url_search_params(url_search_params)
|
||||
, m_iteration_kind(iteration_kind)
|
||||
{
|
||||
}
|
||||
|
||||
URLSearchParams const& m_url_search_params;
|
||||
JS::Object::PropertyKind m_iteration_kind;
|
||||
size_t m_index { 0 };
|
||||
};
|
||||
}
|
||||
|
||||
namespace Web::Bindings {
|
||||
|
||||
URLSearchParamsIteratorWrapper* wrap(JS::GlobalObject&, URL::URLSearchParamsIterator&);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue