URL.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/URL/URL.h>
  8. namespace Web::URL {
  9. DOM::ExceptionOr<NonnullRefPtr<URL>> URL::create_with_global_object(Bindings::WindowObject& window_object, String const& url, String const& base)
  10. {
  11. // 1. Let parsedBase be null.
  12. Optional<AK::URL> parsed_base;
  13. // 2. If base is given, then:
  14. if (!base.is_null()) {
  15. // 1. Let parsedBase be the result of running the basic URL parser on base.
  16. parsed_base = base;
  17. // 2. If parsedBase is failure, then throw a TypeError.
  18. if (!parsed_base->is_valid())
  19. return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid base URL" };
  20. }
  21. // 3. Let parsedURL be the result of running the basic URL parser on url with parsedBase.
  22. AK::URL parsed_url;
  23. if (parsed_base.has_value())
  24. parsed_url = parsed_base->complete_url(url);
  25. else
  26. parsed_url = url;
  27. // 4. If parsedURL is failure, then throw a TypeError.
  28. if (!parsed_url.is_valid())
  29. return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid URL" };
  30. // 5. Let query be parsedURL’s query, if that is non-null, and the empty string otherwise.
  31. auto& query = parsed_url.query().is_null() ? String::empty() : parsed_url.query();
  32. // 6. Set this’s URL to parsedURL.
  33. // 7. Set this’s query object to a new URLSearchParams object.
  34. auto query_object = URLSearchParams::create_with_global_object(window_object, query);
  35. VERIFY(!query_object.is_exception()); // The string variant of the constructor can't throw.
  36. // 8. Initialize this’s query object with query.
  37. auto result_url = URL::create(move(parsed_url), query_object.release_value());
  38. // 9. Set this’s query object’s URL object to this.
  39. result_url->m_query->m_url = result_url;
  40. return result_url;
  41. }
  42. String URL::href() const
  43. {
  44. // return the serialization of this’s URL.
  45. return m_url.serialize();
  46. }
  47. String URL::to_json() const
  48. {
  49. // return the serialization of this’s URL.
  50. return m_url.serialize();
  51. }
  52. DOM::ExceptionOr<void> URL::set_href(String const& href)
  53. {
  54. // 1. Let parsedURL be the result of running the basic URL parser on the given value.
  55. AK::URL parsed_url = href;
  56. // 2. If parsedURL is failure, then throw a TypeError.
  57. if (!parsed_url.is_valid())
  58. return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid URL" };
  59. // 3. Set this’s URL to parsedURL.
  60. m_url = move(parsed_url);
  61. // 4. Empty this’s query object’s list.
  62. m_query->m_list.clear();
  63. // 5. Let query be this’s URL’s query.
  64. auto& query = m_url.query();
  65. // 6. If query is non-null, then set this’s query object’s list to the result of parsing query.
  66. if (!query.is_null())
  67. m_query->m_list = url_decode(query);
  68. return {};
  69. }
  70. URLSearchParams const* URL::search_params() const
  71. {
  72. return m_query;
  73. }
  74. }