Location.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/DeprecatedFlyString.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibJS/Heap/MarkedVector.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibJS/Runtime/PropertyDescriptor.h>
  12. #include <LibJS/Runtime/PropertyKey.h>
  13. #include <LibWeb/Bindings/LocationPrototype.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
  16. #include <LibWeb/HTML/Location.h>
  17. #include <LibWeb/HTML/Window.h>
  18. #include <LibWeb/WebIDL/DOMException.h>
  19. namespace Web::HTML {
  20. // https://html.spec.whatwg.org/multipage/history.html#the-location-interface
  21. Location::Location(JS::Realm& realm)
  22. : PlatformObject(realm)
  23. {
  24. }
  25. Location::~Location() = default;
  26. void Location::visit_edges(Cell::Visitor& visitor)
  27. {
  28. Base::visit_edges(visitor);
  29. for (auto& property : m_default_properties)
  30. visitor.visit(property);
  31. }
  32. JS::ThrowCompletionOr<void> Location::initialize(JS::Realm& realm)
  33. {
  34. MUST_OR_THROW_OOM(Object::initialize(realm));
  35. set_prototype(&Bindings::ensure_web_prototype<Bindings::LocationPrototype>(realm, "Location"));
  36. // FIXME: Implement steps 2.-4.
  37. // 5. Set the value of the [[DefaultProperties]] internal slot of location to location.[[OwnPropertyKeys]]().
  38. // NOTE: In LibWeb this happens before the ESO is set up, so we must avoid location's custom [[OwnPropertyKeys]].
  39. m_default_properties.extend(MUST(Object::internal_own_property_keys()));
  40. return {};
  41. }
  42. // https://html.spec.whatwg.org/multipage/history.html#relevant-document
  43. DOM::Document const* Location::relevant_document() const
  44. {
  45. // A Location object has an associated relevant Document, which is this Location object's
  46. // relevant global object's browsing context's active document, if this Location object's
  47. // relevant global object's browsing context is non-null, and null otherwise.
  48. auto* browsing_context = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)).browsing_context();
  49. return browsing_context ? browsing_context->active_document() : nullptr;
  50. }
  51. // https://html.spec.whatwg.org/multipage/history.html#concept-location-url
  52. AK::URL Location::url() const
  53. {
  54. // A Location object has an associated url, which is this Location object's relevant Document's URL,
  55. // if this Location object's relevant Document is non-null, and about:blank otherwise.
  56. auto const* relevant_document = this->relevant_document();
  57. return relevant_document ? relevant_document->url() : "about:blank"sv;
  58. }
  59. // https://html.spec.whatwg.org/multipage/history.html#dom-location-href
  60. DeprecatedString Location::href() const
  61. {
  62. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  63. // 2. Return this's url, serialized.
  64. return url().to_deprecated_string();
  65. }
  66. // https://html.spec.whatwg.org/multipage/history.html#the-location-interface:dom-location-href-2
  67. JS::ThrowCompletionOr<void> Location::set_href(DeprecatedString const& new_href)
  68. {
  69. auto& vm = this->vm();
  70. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  71. // FIXME: 1. If this's relevant Document is null, then return.
  72. // 2. Parse the given value relative to the entry settings object. If that failed, throw a TypeError exception.
  73. auto href_url = window.associated_document().parse_url(new_href);
  74. if (!href_url.is_valid())
  75. return vm.throw_completion<JS::URIError>(DeprecatedString::formatted("Invalid URL '{}'", new_href));
  76. // 3. Location-object navigate given the resulting URL record.
  77. window.did_set_location_href({}, href_url);
  78. return {};
  79. }
  80. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-origin
  81. DeprecatedString Location::origin() const
  82. {
  83. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  84. // 2. Return the serialization of this's url's origin.
  85. return url().serialize_origin();
  86. }
  87. // https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol
  88. DeprecatedString Location::protocol() const
  89. {
  90. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  91. // 2. Return this's url's scheme, followed by ":".
  92. return DeprecatedString::formatted("{}:", url().scheme());
  93. }
  94. JS::ThrowCompletionOr<void> Location::set_protocol(DeprecatedString const&)
  95. {
  96. auto& vm = this->vm();
  97. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.protocol setter");
  98. }
  99. // https://html.spec.whatwg.org/multipage/history.html#dom-location-host
  100. DeprecatedString Location::host() const
  101. {
  102. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  103. // 2. Let url be this's url.
  104. auto url = this->url();
  105. // 3. If url's host is null, return the empty string.
  106. if (url.host().is_null())
  107. return DeprecatedString::empty();
  108. // 4. If url's port is null, return url's host, serialized.
  109. if (!url.port().has_value())
  110. return url.host();
  111. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  112. return DeprecatedString::formatted("{}:{}", url.host(), *url.port());
  113. }
  114. JS::ThrowCompletionOr<void> Location::set_host(DeprecatedString const&)
  115. {
  116. auto& vm = this->vm();
  117. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.host setter");
  118. }
  119. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hostname
  120. DeprecatedString Location::hostname() const
  121. {
  122. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  123. auto url = this->url();
  124. // 2. If this's url's host is null, return the empty string.
  125. if (url.host().is_null())
  126. return DeprecatedString::empty();
  127. // 3. Return this's url's host, serialized.
  128. return url.host();
  129. }
  130. JS::ThrowCompletionOr<void> Location::set_hostname(DeprecatedString const&)
  131. {
  132. auto& vm = this->vm();
  133. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.hostname setter");
  134. }
  135. // https://html.spec.whatwg.org/multipage/history.html#dom-location-port
  136. DeprecatedString Location::port() const
  137. {
  138. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  139. auto url = this->url();
  140. // 2. If this's url's port is null, return the empty string.
  141. if (!url.port().has_value())
  142. return DeprecatedString::empty();
  143. // 3. Return this's url's port, serialized.
  144. return DeprecatedString::number(*url.port());
  145. }
  146. JS::ThrowCompletionOr<void> Location::set_port(DeprecatedString const&)
  147. {
  148. auto& vm = this->vm();
  149. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.port setter");
  150. }
  151. // https://html.spec.whatwg.org/multipage/history.html#dom-location-pathname
  152. DeprecatedString Location::pathname() const
  153. {
  154. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  155. // 2. Return the result of URL path serializing this Location object's url.
  156. return url().path();
  157. }
  158. JS::ThrowCompletionOr<void> Location::set_pathname(DeprecatedString const&)
  159. {
  160. auto& vm = this->vm();
  161. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.pathname setter");
  162. }
  163. // https://html.spec.whatwg.org/multipage/history.html#dom-location-search
  164. DeprecatedString Location::search() const
  165. {
  166. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  167. auto url = this->url();
  168. // 2. If this's url's query is either null or the empty string, return the empty string.
  169. if (url.query().is_empty())
  170. return DeprecatedString::empty();
  171. // 3. Return "?", followed by this's url's query.
  172. return DeprecatedString::formatted("?{}", url.query());
  173. }
  174. JS::ThrowCompletionOr<void> Location::set_search(DeprecatedString const&)
  175. {
  176. auto& vm = this->vm();
  177. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.search setter");
  178. }
  179. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hash
  180. DeprecatedString Location::hash() const
  181. {
  182. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  183. auto url = this->url();
  184. // 2. If this's url's fragment is either null or the empty string, return the empty string.
  185. if (url.fragment().is_empty())
  186. return DeprecatedString::empty();
  187. // 3. Return "#", followed by this's url's fragment.
  188. return DeprecatedString::formatted("#{}", url.fragment());
  189. }
  190. JS::ThrowCompletionOr<void> Location::set_hash(DeprecatedString const&)
  191. {
  192. auto& vm = this->vm();
  193. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.hash setter");
  194. }
  195. // https://html.spec.whatwg.org/multipage/history.html#dom-location-reload
  196. void Location::reload() const
  197. {
  198. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  199. window.did_call_location_reload({});
  200. }
  201. // https://html.spec.whatwg.org/multipage/history.html#dom-location-replace
  202. void Location::replace(DeprecatedString url) const
  203. {
  204. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  205. // FIXME: This needs spec compliance work.
  206. window.did_call_location_replace({}, move(url));
  207. }
  208. // 7.10.5.1 [[GetPrototypeOf]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-getprototypeof
  209. JS::ThrowCompletionOr<JS::Object*> Location::internal_get_prototype_of() const
  210. {
  211. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ! OrdinaryGetPrototypeOf(this).
  212. if (HTML::is_platform_object_same_origin(*this))
  213. return MUST(JS::Object::internal_get_prototype_of());
  214. // 2. Return null.
  215. return nullptr;
  216. }
  217. // 7.10.5.2 [[SetPrototypeOf]] ( V ), https://html.spec.whatwg.org/multipage/history.html#location-setprototypeof
  218. JS::ThrowCompletionOr<bool> Location::internal_set_prototype_of(Object* prototype)
  219. {
  220. // 1. Return ! SetImmutablePrototype(this, V).
  221. return MUST(set_immutable_prototype(prototype));
  222. }
  223. // 7.10.5.3 [[IsExtensible]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-isextensible
  224. JS::ThrowCompletionOr<bool> Location::internal_is_extensible() const
  225. {
  226. // 1. Return true.
  227. return true;
  228. }
  229. // 7.10.5.4 [[PreventExtensions]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-preventextensions
  230. JS::ThrowCompletionOr<bool> Location::internal_prevent_extensions()
  231. {
  232. // 1. Return false.
  233. return false;
  234. }
  235. // 7.10.5.5 [[GetOwnProperty]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-getownproperty
  236. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> Location::internal_get_own_property(JS::PropertyKey const& property_key) const
  237. {
  238. auto& vm = this->vm();
  239. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  240. if (HTML::is_platform_object_same_origin(*this)) {
  241. // 1. Let desc be OrdinaryGetOwnProperty(this, P).
  242. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  243. // 2. If the value of the [[DefaultProperties]] internal slot of this contains P, then set desc.[[Configurable]] to true.
  244. auto property_key_value = property_key.is_symbol()
  245. ? JS::Value { property_key.as_symbol() }
  246. : JS::PrimitiveString::create(vm, property_key.to_string());
  247. if (m_default_properties.contains_slow(property_key_value))
  248. descriptor->configurable = true;
  249. // 3. Return desc.
  250. return descriptor;
  251. }
  252. // 2. Let property be CrossOriginGetOwnPropertyHelper(this, P).
  253. auto property = HTML::cross_origin_get_own_property_helper(const_cast<Location*>(this), property_key);
  254. // 3. If property is not undefined, then return property.
  255. if (property.has_value())
  256. return property;
  257. // 4. Return ? CrossOriginPropertyFallback(P).
  258. return TRY(HTML::cross_origin_property_fallback(vm, property_key));
  259. }
  260. // 7.10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/history.html#location-defineownproperty
  261. JS::ThrowCompletionOr<bool> Location::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor)
  262. {
  263. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  264. if (HTML::is_platform_object_same_origin(*this)) {
  265. // 1. If the value of the [[DefaultProperties]] internal slot of this contains P, then return false.
  266. // 2. Return ? OrdinaryDefineOwnProperty(this, P, Desc).
  267. return JS::Object::internal_define_own_property(property_key, descriptor);
  268. }
  269. // 2. Throw a "SecurityError" DOMException.
  270. return throw_completion(WebIDL::SecurityError::create(realm(), DeprecatedString::formatted("Can't define property '{}' on cross-origin object", property_key)));
  271. }
  272. // 7.10.5.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-get
  273. JS::ThrowCompletionOr<JS::Value> Location::internal_get(JS::PropertyKey const& property_key, JS::Value receiver) const
  274. {
  275. auto& vm = this->vm();
  276. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryGet(this, P, Receiver).
  277. if (HTML::is_platform_object_same_origin(*this))
  278. return JS::Object::internal_get(property_key, receiver);
  279. // 2. Return ? CrossOriginGet(this, P, Receiver).
  280. return HTML::cross_origin_get(vm, static_cast<JS::Object const&>(*this), property_key, receiver);
  281. }
  282. // 7.10.5.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-set
  283. JS::ThrowCompletionOr<bool> Location::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver)
  284. {
  285. auto& vm = this->vm();
  286. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinarySet(this, P, V, Receiver).
  287. if (HTML::is_platform_object_same_origin(*this))
  288. return JS::Object::internal_set(property_key, value, receiver);
  289. // 2. Return ? CrossOriginSet(this, P, V, Receiver).
  290. return HTML::cross_origin_set(vm, static_cast<JS::Object&>(*this), property_key, value, receiver);
  291. }
  292. // 7.10.5.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-delete
  293. JS::ThrowCompletionOr<bool> Location::internal_delete(JS::PropertyKey const& property_key)
  294. {
  295. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryDelete(this, P).
  296. if (HTML::is_platform_object_same_origin(*this))
  297. return JS::Object::internal_delete(property_key);
  298. // 2. Throw a "SecurityError" DOMException.
  299. return throw_completion(WebIDL::SecurityError::create(realm(), DeprecatedString::formatted("Can't delete property '{}' on cross-origin object", property_key)));
  300. }
  301. // 7.10.5.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-ownpropertykeys
  302. JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> Location::internal_own_property_keys() const
  303. {
  304. // 1. If IsPlatformObjectSameOrigin(this) is true, then return OrdinaryOwnPropertyKeys(this).
  305. if (HTML::is_platform_object_same_origin(*this))
  306. return JS::Object::internal_own_property_keys();
  307. // 2. Return CrossOriginOwnPropertyKeys(this).
  308. return HTML::cross_origin_own_property_keys(this);
  309. }
  310. }