LocationObject.cpp 16 KB

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