WorkerLocation.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <LibWeb/HTML/WorkerGlobalScope.h>
  8. #include <LibWeb/HTML/WorkerLocation.h>
  9. namespace Web::HTML {
  10. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-href
  11. String WorkerLocation::href() const
  12. {
  13. // The href getter steps are to return this's WorkerGlobalScope object's url, serialized.
  14. return m_global_scope.url().serialize();
  15. }
  16. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-origin
  17. String WorkerLocation::origin() const
  18. {
  19. // The origin getter steps are to return the serialization of this's WorkerGlobalScope object's url's origin.
  20. return m_global_scope.url().serialize_origin();
  21. }
  22. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-protocol
  23. String WorkerLocation::protocol() const
  24. {
  25. // The protocol getter steps are to return this's WorkerGlobalScope object's url's scheme, followed by ":".
  26. return String::formatted("{}:", m_global_scope.url().scheme());
  27. }
  28. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-host
  29. String WorkerLocation::host() const
  30. {
  31. // The host getter steps are:
  32. // 1. Let url be this's WorkerGlobalScope object's url.
  33. auto const& url = m_global_scope.url();
  34. // 2. If url's host is null, return the empty string.
  35. if (url.host().is_empty())
  36. return "";
  37. // 3. If url's port is null, return url's host, serialized.
  38. if (!url.port().has_value())
  39. return url.host();
  40. // 4. Return url's host, serialized, followed by ":" and url's port, serialized.
  41. return String::formatted("{}:{}", url.host(), url.port().value());
  42. }
  43. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hostname
  44. String WorkerLocation::hostname() const
  45. {
  46. // The hostname getter steps are:
  47. // 1. Let host be this's WorkerGlobalScope object's url's host.
  48. auto const& host = m_global_scope.url().host();
  49. // 2. If host is null, return the empty string.
  50. if (host.is_empty())
  51. return "";
  52. // 3. Return host, serialized.
  53. return host;
  54. }
  55. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-port
  56. String WorkerLocation::port() const
  57. {
  58. // The port getter steps are:
  59. // 1. Let port be this's WorkerGlobalScope object's url's port.
  60. auto const& port = m_global_scope.url().port();
  61. // 2. If port is null, return the empty string.
  62. if (!port.has_value())
  63. return "";
  64. // 3. Return port, serialized.
  65. return String::number(port.value());
  66. }
  67. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-pathname
  68. String WorkerLocation::pathname() const
  69. {
  70. // The pathname getter steps are to return the result of URL path serializing this's WorkerGlobalScope object's url.
  71. return m_global_scope.url().path();
  72. }
  73. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-search
  74. String WorkerLocation::search() const
  75. {
  76. // The search getter steps are:
  77. // 1. Let query be this's WorkerGlobalScope object's url's query.
  78. auto const& query = m_global_scope.url().query();
  79. // 2. If query is either null or the empty string, return the empty string.
  80. if (query.is_empty())
  81. return "";
  82. // 3. Return "?", followed by query.
  83. return String::formatted("?{}", query);
  84. }
  85. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hash
  86. String WorkerLocation::hash() const
  87. {
  88. // The hash getter steps are:
  89. // 1. Let fragment be this's WorkerGlobalScope object's url's fragment.
  90. auto const& fragment = m_global_scope.url().fragment();
  91. // 2. If fragment is either null or the empty string, return the empty string.
  92. if (fragment.is_empty())
  93. return "";
  94. // 3. Return "#", followed by fragment.
  95. return String::formatted("#{}", fragment);
  96. }
  97. WorkerLocation::WorkerLocation(WorkerGlobalScope& global_scope)
  98. : m_global_scope(global_scope)
  99. {
  100. }
  101. }