LocationObject.cpp 17 KB

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