LocationObject.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/FlyString.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibWeb/Bindings/LocationObject.h>
  10. #include <LibWeb/Bindings/WindowObject.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/Window.h>
  13. namespace Web::Bindings {
  14. LocationObject::LocationObject(JS::GlobalObject& global_object)
  15. : Object(*global_object.object_prototype())
  16. {
  17. }
  18. void LocationObject::initialize(JS::GlobalObject& global_object)
  19. {
  20. Object::initialize(global_object);
  21. u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable;
  22. define_native_accessor("href", href_getter, href_setter, attr);
  23. define_native_accessor("host", host_getter, {}, attr);
  24. define_native_accessor("hostname", hostname_getter, {}, attr);
  25. define_native_accessor("pathname", pathname_getter, {}, attr);
  26. define_native_accessor("hash", hash_getter, {}, attr);
  27. define_native_accessor("search", search_getter, {}, attr);
  28. define_native_accessor("protocol", protocol_getter, {}, attr);
  29. define_native_accessor("port", port_getter, {}, attr);
  30. define_native_function("reload", reload, 0, JS::Attribute::Enumerable);
  31. }
  32. LocationObject::~LocationObject()
  33. {
  34. }
  35. JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_getter)
  36. {
  37. auto& window = static_cast<WindowObject&>(global_object);
  38. return JS::js_string(vm, window.impl().associated_document().url().to_string());
  39. }
  40. JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_setter)
  41. {
  42. auto& window = static_cast<WindowObject&>(global_object);
  43. auto new_href = vm.argument(0).to_string(global_object);
  44. if (vm.exception())
  45. return {};
  46. auto href_url = window.impl().associated_document().parse_url(new_href);
  47. if (!href_url.is_valid()) {
  48. vm.throw_exception<JS::URIError>(global_object, String::formatted("Invalid URL '{}'", new_href));
  49. return {};
  50. }
  51. window.impl().did_set_location_href({}, href_url);
  52. return JS::js_undefined();
  53. }
  54. JS_DEFINE_NATIVE_FUNCTION(LocationObject::pathname_getter)
  55. {
  56. auto& window = static_cast<WindowObject&>(global_object);
  57. return JS::js_string(vm, window.impl().associated_document().url().path());
  58. }
  59. JS_DEFINE_NATIVE_FUNCTION(LocationObject::hostname_getter)
  60. {
  61. auto& window = static_cast<WindowObject&>(global_object);
  62. return JS::js_string(vm, window.impl().associated_document().url().host());
  63. }
  64. JS_DEFINE_NATIVE_FUNCTION(LocationObject::host_getter)
  65. {
  66. auto& window = static_cast<WindowObject&>(global_object);
  67. auto url = window.impl().associated_document().url();
  68. return JS::js_string(vm, String::formatted("{}:{}", url.host(), url.port_or_default()));
  69. }
  70. JS_DEFINE_NATIVE_FUNCTION(LocationObject::hash_getter)
  71. {
  72. auto& window = static_cast<WindowObject&>(global_object);
  73. auto fragment = window.impl().associated_document().url().fragment();
  74. if (!fragment.length())
  75. return JS::js_string(vm, "");
  76. StringBuilder builder;
  77. builder.append('#');
  78. builder.append(fragment);
  79. return JS::js_string(vm, builder.to_string());
  80. }
  81. JS_DEFINE_NATIVE_FUNCTION(LocationObject::search_getter)
  82. {
  83. auto& window = static_cast<WindowObject&>(global_object);
  84. auto query = window.impl().associated_document().url().query();
  85. if (!query.length())
  86. return JS::js_string(vm, "");
  87. StringBuilder builder;
  88. builder.append('?');
  89. builder.append(query);
  90. return JS::js_string(vm, builder.to_string());
  91. }
  92. JS_DEFINE_NATIVE_FUNCTION(LocationObject::protocol_getter)
  93. {
  94. auto& window = static_cast<WindowObject&>(global_object);
  95. StringBuilder builder;
  96. builder.append(window.impl().associated_document().url().protocol());
  97. builder.append(':');
  98. return JS::js_string(vm, builder.to_string());
  99. }
  100. JS_DEFINE_NATIVE_FUNCTION(LocationObject::port_getter)
  101. {
  102. auto& window = static_cast<WindowObject&>(global_object);
  103. return JS::Value(window.impl().associated_document().url().port_or_default());
  104. }
  105. JS_DEFINE_NATIVE_FUNCTION(LocationObject::reload)
  106. {
  107. auto& window = static_cast<WindowObject&>(global_object);
  108. window.impl().did_call_location_reload({});
  109. return JS::js_undefined();
  110. }
  111. // https://html.spec.whatwg.org/multipage/history.html#location-setprototypeof
  112. JS::ThrowCompletionOr<bool> LocationObject::internal_set_prototype_of(Object* prototype)
  113. {
  114. // 1. Return ! SetImmutablePrototype(this, V).
  115. return set_immutable_prototype(prototype);
  116. }
  117. // https://html.spec.whatwg.org/multipage/history.html#location-isextensible
  118. bool LocationObject::internal_is_extensible() const
  119. {
  120. // 1. Return true.
  121. return true;
  122. }
  123. // https://html.spec.whatwg.org/multipage/history.html#location-preventextensions
  124. bool LocationObject::internal_prevent_extensions()
  125. {
  126. // 1. Return false.
  127. return false;
  128. }
  129. }