LocationObject.cpp 4.7 KB

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