URLSearchParams.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/Vector.h>
  8. #include <LibURL/URL.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&, StringView);
  22. static WebIDL::ExceptionOr<JS::NonnullGCPtr<URLSearchParams>> create(JS::Realm&, Vector<QueryParam> list);
  23. static WebIDL::ExceptionOr<JS::NonnullGCPtr<URLSearchParams>> construct_impl(JS::Realm&, Variant<Vector<Vector<String>>, OrderedHashMap<String, String>, String> const& init);
  24. virtual ~URLSearchParams() override;
  25. size_t size() const;
  26. WebIDL::ExceptionOr<void> append(String const& name, String const& value);
  27. WebIDL::ExceptionOr<void> delete_(String const& name);
  28. Optional<String> get(String const& name);
  29. WebIDL::ExceptionOr<Vector<String>> get_all(String const& name);
  30. bool has(String const& name);
  31. WebIDL::ExceptionOr<void> set(String const& name, String const& value);
  32. WebIDL::ExceptionOr<void> sort();
  33. WebIDL::ExceptionOr<String> to_string() const;
  34. using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, String const&)>;
  35. JS::ThrowCompletionOr<void> for_each(ForEachCallback);
  36. private:
  37. friend class DOMURL;
  38. friend class URLSearchParamsIterator;
  39. URLSearchParams(JS::Realm&, Vector<QueryParam> list);
  40. virtual void initialize(JS::Realm&) override;
  41. virtual void visit_edges(Cell::Visitor&) override;
  42. WebIDL::ExceptionOr<void> update();
  43. Vector<QueryParam> m_list;
  44. JS::GCPtr<DOMURL> m_url;
  45. };
  46. }