Location.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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/String.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. JS::GCPtr<DOM::Document> 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. WebIDL::ExceptionOr<String> Location::href() const
  62. {
  63. auto& vm = this->vm();
  64. // 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.
  65. auto const relevant_document = this->relevant_document();
  66. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  67. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"sv);
  68. // 2. Return this's url, serialized.
  69. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url().serialize()));
  70. }
  71. // https://html.spec.whatwg.org/multipage/history.html#the-location-interface:dom-location-href-2
  72. WebIDL::ExceptionOr<void> Location::set_href(String const& new_href)
  73. {
  74. auto& vm = this->vm();
  75. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  76. // 1. If this's relevant Document is null, then return.
  77. auto const relevant_document = this->relevant_document();
  78. if (!relevant_document)
  79. return {};
  80. // 2. Parse the given value relative to the entry settings object. If that failed, throw a TypeError exception.
  81. auto href_url = window.associated_document().parse_url(new_href.to_deprecated_string());
  82. if (!href_url.is_valid())
  83. return vm.throw_completion<JS::URIError>(TRY_OR_THROW_OOM(vm, String::formatted("Invalid URL '{}'", new_href)));
  84. // 3. Location-object navigate given the resulting URL record.
  85. window.did_set_location_href({}, href_url);
  86. return {};
  87. }
  88. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-origin
  89. WebIDL::ExceptionOr<String> Location::origin() const
  90. {
  91. auto& vm = this->vm();
  92. // 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.
  93. auto const relevant_document = this->relevant_document();
  94. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  95. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"sv);
  96. // 2. Return the serialization of this's url's origin.
  97. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url().serialize_origin()));
  98. }
  99. // https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol
  100. WebIDL::ExceptionOr<String> Location::protocol() const
  101. {
  102. auto& vm = this->vm();
  103. // 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. auto const relevant_document = this->relevant_document();
  105. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  106. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"sv);
  107. // 2. Return this's url's scheme, followed by ":".
  108. return TRY_OR_THROW_OOM(vm, String::formatted("{}:", url().scheme()));
  109. }
  110. WebIDL::ExceptionOr<void> Location::set_protocol(String const&)
  111. {
  112. auto& vm = this->vm();
  113. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.protocol setter");
  114. }
  115. // https://html.spec.whatwg.org/multipage/history.html#dom-location-host
  116. WebIDL::ExceptionOr<String> Location::host() const
  117. {
  118. auto& vm = this->vm();
  119. // 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.
  120. auto const relevant_document = this->relevant_document();
  121. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  122. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"sv);
  123. // 2. Let url be this's url.
  124. auto url = this->url();
  125. // 3. If url's host is null, return the empty string.
  126. if (url.host().is_null())
  127. return String {};
  128. // 4. If url's port is null, return url's host, serialized.
  129. if (!url.port().has_value())
  130. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.host()));
  131. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  132. return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.host(), *url.port()));
  133. }
  134. WebIDL::ExceptionOr<void> Location::set_host(String const&)
  135. {
  136. auto& vm = this->vm();
  137. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.host setter");
  138. }
  139. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hostname
  140. WebIDL::ExceptionOr<String> Location::hostname() const
  141. {
  142. auto& vm = this->vm();
  143. // 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.
  144. auto const relevant_document = this->relevant_document();
  145. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  146. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"sv);
  147. auto url = this->url();
  148. // 2. If this's url's host is null, return the empty string.
  149. if (url.host().is_null())
  150. return String {};
  151. // 3. Return this's url's host, serialized.
  152. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.host()));
  153. }
  154. WebIDL::ExceptionOr<void> Location::set_hostname(String const&)
  155. {
  156. auto& vm = this->vm();
  157. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.hostname setter");
  158. }
  159. // https://html.spec.whatwg.org/multipage/history.html#dom-location-port
  160. WebIDL::ExceptionOr<String> Location::port() const
  161. {
  162. auto& vm = this->vm();
  163. // 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.
  164. auto const relevant_document = this->relevant_document();
  165. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  166. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"sv);
  167. auto url = this->url();
  168. // 2. If this's url's port is null, return the empty string.
  169. if (!url.port().has_value())
  170. return String {};
  171. // 3. Return this's url's port, serialized.
  172. return TRY_OR_THROW_OOM(vm, String::number(*url.port()));
  173. }
  174. WebIDL::ExceptionOr<void> Location::set_port(String const&)
  175. {
  176. auto& vm = this->vm();
  177. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.port setter");
  178. }
  179. // https://html.spec.whatwg.org/multipage/history.html#dom-location-pathname
  180. WebIDL::ExceptionOr<String> Location::pathname() const
  181. {
  182. auto& vm = this->vm();
  183. // 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 const relevant_document = this->relevant_document();
  185. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  186. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"sv);
  187. // 2. Return the result of URL path serializing this Location object's url.
  188. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url().path()));
  189. }
  190. WebIDL::ExceptionOr<void> Location::set_pathname(String const&)
  191. {
  192. auto& vm = this->vm();
  193. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.pathname setter");
  194. }
  195. // https://html.spec.whatwg.org/multipage/history.html#dom-location-search
  196. WebIDL::ExceptionOr<String> Location::search() const
  197. {
  198. auto& vm = this->vm();
  199. // 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.
  200. auto const relevant_document = this->relevant_document();
  201. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  202. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"sv);
  203. auto url = this->url();
  204. // 2. If this's url's query is either null or the empty string, return the empty string.
  205. if (url.query().is_empty())
  206. return String {};
  207. // 3. Return "?", followed by this's url's query.
  208. return TRY_OR_THROW_OOM(vm, String::formatted("?{}", url.query()));
  209. }
  210. WebIDL::ExceptionOr<void> Location::set_search(String const&)
  211. {
  212. auto& vm = this->vm();
  213. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.search setter");
  214. }
  215. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hash
  216. WebIDL::ExceptionOr<String> Location::hash() const
  217. {
  218. auto& vm = this->vm();
  219. // 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.
  220. auto const relevant_document = this->relevant_document();
  221. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  222. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"sv);
  223. auto url = this->url();
  224. // 2. If this's url's fragment is either null or the empty string, return the empty string.
  225. if (url.fragment().is_empty())
  226. return String {};
  227. // 3. Return "#", followed by this's url's fragment.
  228. return TRY_OR_THROW_OOM(vm, String::formatted("#{}", url.fragment()));
  229. }
  230. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-hash
  231. WebIDL::ExceptionOr<void> Location::set_hash(String const& value)
  232. {
  233. // The hash setter steps are:
  234. auto const relevant_document = this->relevant_document();
  235. // 1. If this's relevant Document is null, then return.
  236. if (!relevant_document)
  237. return {};
  238. // 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.
  239. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  240. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"sv);
  241. // 3. Let copyURL be a copy of this's url.
  242. auto copy_url = this->url();
  243. // 4. Let input be the given value with a single leading "#" removed, if any.
  244. auto input = value.bytes_as_string_view().trim("#"sv, TrimMode::Left);
  245. // 5. Set copyURL's fragment to the empty string.
  246. copy_url.set_fragment("");
  247. // 6. Basic URL parse input, with copyURL as url and fragment state as state override.
  248. auto result_url = URLParser::parse(input, {}, copy_url, URLParser::State::Fragment);
  249. // 7. If copyURL's fragment is this's url's fragment, then return.
  250. if (copy_url.fragment() == this->url().fragment())
  251. return {};
  252. // 8. Location-object navigate this to copyURL.
  253. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  254. window.did_set_location_href({}, copy_url);
  255. return {};
  256. }
  257. // https://html.spec.whatwg.org/multipage/history.html#dom-location-reload
  258. void Location::reload() const
  259. {
  260. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  261. window.did_call_location_reload({});
  262. }
  263. // https://html.spec.whatwg.org/multipage/history.html#dom-location-replace
  264. void Location::replace(String const& url) const
  265. {
  266. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  267. // FIXME: This needs spec compliance work.
  268. window.did_call_location_replace({}, url.to_deprecated_string());
  269. }
  270. // 7.10.5.1 [[GetPrototypeOf]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-getprototypeof
  271. JS::ThrowCompletionOr<JS::Object*> Location::internal_get_prototype_of() const
  272. {
  273. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ! OrdinaryGetPrototypeOf(this).
  274. if (HTML::is_platform_object_same_origin(*this))
  275. return MUST(JS::Object::internal_get_prototype_of());
  276. // 2. Return null.
  277. return nullptr;
  278. }
  279. // 7.10.5.2 [[SetPrototypeOf]] ( V ), https://html.spec.whatwg.org/multipage/history.html#location-setprototypeof
  280. JS::ThrowCompletionOr<bool> Location::internal_set_prototype_of(Object* prototype)
  281. {
  282. // 1. Return ! SetImmutablePrototype(this, V).
  283. return MUST(set_immutable_prototype(prototype));
  284. }
  285. // 7.10.5.3 [[IsExtensible]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-isextensible
  286. JS::ThrowCompletionOr<bool> Location::internal_is_extensible() const
  287. {
  288. // 1. Return true.
  289. return true;
  290. }
  291. // 7.10.5.4 [[PreventExtensions]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-preventextensions
  292. JS::ThrowCompletionOr<bool> Location::internal_prevent_extensions()
  293. {
  294. // 1. Return false.
  295. return false;
  296. }
  297. // 7.10.5.5 [[GetOwnProperty]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-getownproperty
  298. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> Location::internal_get_own_property(JS::PropertyKey const& property_key) const
  299. {
  300. auto& vm = this->vm();
  301. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  302. if (HTML::is_platform_object_same_origin(*this)) {
  303. // 1. Let desc be OrdinaryGetOwnProperty(this, P).
  304. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  305. // 2. If the value of the [[DefaultProperties]] internal slot of this contains P, then set desc.[[Configurable]] to true.
  306. auto property_key_value = property_key.is_symbol()
  307. ? JS::Value { property_key.as_symbol() }
  308. : JS::PrimitiveString::create(vm, property_key.to_string());
  309. if (m_default_properties.contains_slow(property_key_value))
  310. descriptor->configurable = true;
  311. // 3. Return desc.
  312. return descriptor;
  313. }
  314. // 2. Let property be CrossOriginGetOwnPropertyHelper(this, P).
  315. auto property = HTML::cross_origin_get_own_property_helper(const_cast<Location*>(this), property_key);
  316. // 3. If property is not undefined, then return property.
  317. if (property.has_value())
  318. return property;
  319. // 4. Return ? CrossOriginPropertyFallback(P).
  320. return TRY(HTML::cross_origin_property_fallback(vm, property_key));
  321. }
  322. // 7.10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/history.html#location-defineownproperty
  323. JS::ThrowCompletionOr<bool> Location::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor)
  324. {
  325. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  326. if (HTML::is_platform_object_same_origin(*this)) {
  327. // 1. If the value of the [[DefaultProperties]] internal slot of this contains P, then return false.
  328. // 2. Return ? OrdinaryDefineOwnProperty(this, P, Desc).
  329. return JS::Object::internal_define_own_property(property_key, descriptor);
  330. }
  331. // 2. Throw a "SecurityError" DOMException.
  332. return throw_completion(WebIDL::SecurityError::create(realm(), DeprecatedString::formatted("Can't define property '{}' on cross-origin object", property_key)));
  333. }
  334. // 7.10.5.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-get
  335. JS::ThrowCompletionOr<JS::Value> Location::internal_get(JS::PropertyKey const& property_key, JS::Value receiver) const
  336. {
  337. auto& vm = this->vm();
  338. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryGet(this, P, Receiver).
  339. if (HTML::is_platform_object_same_origin(*this))
  340. return JS::Object::internal_get(property_key, receiver);
  341. // 2. Return ? CrossOriginGet(this, P, Receiver).
  342. return HTML::cross_origin_get(vm, static_cast<JS::Object const&>(*this), property_key, receiver);
  343. }
  344. // 7.10.5.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-set
  345. JS::ThrowCompletionOr<bool> Location::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver)
  346. {
  347. auto& vm = this->vm();
  348. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinarySet(this, P, V, Receiver).
  349. if (HTML::is_platform_object_same_origin(*this))
  350. return JS::Object::internal_set(property_key, value, receiver);
  351. // 2. Return ? CrossOriginSet(this, P, V, Receiver).
  352. return HTML::cross_origin_set(vm, static_cast<JS::Object&>(*this), property_key, value, receiver);
  353. }
  354. // 7.10.5.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-delete
  355. JS::ThrowCompletionOr<bool> Location::internal_delete(JS::PropertyKey const& property_key)
  356. {
  357. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryDelete(this, P).
  358. if (HTML::is_platform_object_same_origin(*this))
  359. return JS::Object::internal_delete(property_key);
  360. // 2. Throw a "SecurityError" DOMException.
  361. return throw_completion(WebIDL::SecurityError::create(realm(), DeprecatedString::formatted("Can't delete property '{}' on cross-origin object", property_key)));
  362. }
  363. // 7.10.5.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-ownpropertykeys
  364. JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> Location::internal_own_property_keys() const
  365. {
  366. // 1. If IsPlatformObjectSameOrigin(this) is true, then return OrdinaryOwnPropertyKeys(this).
  367. if (HTML::is_platform_object_same_origin(*this))
  368. return JS::Object::internal_own_property_keys();
  369. // 2. Return CrossOriginOwnPropertyKeys(this).
  370. return HTML::cross_origin_own_property_keys(this);
  371. }
  372. }