Location.cpp 17 KB

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