LocationObject.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 {
  13. namespace 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_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()));
  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::reload)
  100. {
  101. auto& window = static_cast<WindowObject&>(global_object);
  102. window.impl().did_call_location_reload({});
  103. return JS::js_undefined();
  104. }
  105. }
  106. }