URLSearchParams.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/URL.h>
  8. #include <AK/Vector.h>
  9. #include <LibWeb/WebIDL/ExceptionOr.h>
  10. namespace Web::DOMURL {
  11. struct QueryParam {
  12. String name;
  13. String value;
  14. };
  15. ErrorOr<String> url_encode(Vector<QueryParam> const&, StringView encoding = "UTF-8"sv);
  16. ErrorOr<Vector<QueryParam>> url_decode(StringView);
  17. class URLSearchParams : public Bindings::PlatformObject {
  18. WEB_PLATFORM_OBJECT(URLSearchParams, Bindings::PlatformObject);
  19. JS_DECLARE_ALLOCATOR(URLSearchParams);
  20. public:
  21. static WebIDL::ExceptionOr<JS::NonnullGCPtr<URLSearchParams>> create(JS::Realm&, Vector<QueryParam> list);
  22. static WebIDL::ExceptionOr<JS::NonnullGCPtr<URLSearchParams>> construct_impl(JS::Realm&, Variant<Vector<Vector<String>>, OrderedHashMap<String, String>, String> const& init);
  23. virtual ~URLSearchParams() override;
  24. size_t size() const;
  25. WebIDL::ExceptionOr<void> append(String const& name, String const& value);
  26. WebIDL::ExceptionOr<void> delete_(String const& name);
  27. Optional<String> get(String const& name);
  28. WebIDL::ExceptionOr<Vector<String>> get_all(String const& name);
  29. bool has(String const& name);
  30. WebIDL::ExceptionOr<void> set(String const& name, String const& value);
  31. WebIDL::ExceptionOr<void> sort();
  32. WebIDL::ExceptionOr<String> to_string() const;
  33. using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, String const&)>;
  34. JS::ThrowCompletionOr<void> for_each(ForEachCallback);
  35. private:
  36. friend class DOMURL;
  37. friend class URLSearchParamsIterator;
  38. URLSearchParams(JS::Realm&, Vector<QueryParam> list);
  39. virtual void initialize(JS::Realm&) override;
  40. virtual void visit_edges(Cell::Visitor&) override;
  41. WebIDL::ExceptionOr<void> update();
  42. Vector<QueryParam> m_list;
  43. JS::GCPtr<DOMURL> m_url;
  44. };
  45. }