ConsoleGlobalEnvironmentExtensions.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ConsoleGlobalEnvironmentExtensions.h"
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/NodeList.h>
  13. #include <LibWeb/HTML/Window.h>
  14. namespace WebContent {
  15. ConsoleGlobalEnvironmentExtensions::ConsoleGlobalEnvironmentExtensions(JS::Realm& realm, Web::HTML::Window& window)
  16. : Object(realm, nullptr)
  17. , m_window_object(window)
  18. {
  19. }
  20. void ConsoleGlobalEnvironmentExtensions::initialize(JS::Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. define_native_accessor(realm, "$0", $0_getter, nullptr, 0);
  24. define_native_accessor(realm, "$_", $__getter, nullptr, 0);
  25. define_native_function(realm, "$", $_function, 2, JS::default_attributes);
  26. define_native_function(realm, "$$", $$_function, 2, JS::default_attributes);
  27. }
  28. void ConsoleGlobalEnvironmentExtensions::visit_edges(Visitor& visitor)
  29. {
  30. Base::visit_edges(visitor);
  31. visitor.visit(m_window_object);
  32. }
  33. static JS::ThrowCompletionOr<ConsoleGlobalEnvironmentExtensions*> get_console(JS::VM& vm)
  34. {
  35. auto this_value = vm.this_value();
  36. if (!this_value.is_object() || !is<ConsoleGlobalEnvironmentExtensions>(this_value.as_object()))
  37. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "ConsoleGlobalEnvironmentExtensions");
  38. return &static_cast<ConsoleGlobalEnvironmentExtensions&>(this_value.as_object());
  39. }
  40. JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalEnvironmentExtensions::$0_getter)
  41. {
  42. auto* console_global_object = TRY(get_console(vm));
  43. auto& window = *console_global_object->m_window_object;
  44. auto* inspected_node = window.associated_document().inspected_node();
  45. if (!inspected_node)
  46. return JS::js_undefined();
  47. return inspected_node;
  48. }
  49. JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalEnvironmentExtensions::$__getter)
  50. {
  51. auto* console_global_object = TRY(get_console(vm));
  52. return console_global_object->m_most_recent_result;
  53. }
  54. JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalEnvironmentExtensions::$_function)
  55. {
  56. auto* console_global_object = TRY(get_console(vm));
  57. auto& window = *console_global_object->m_window_object;
  58. auto selector = TRY(vm.argument(0).to_string(vm));
  59. if (vm.argument_count() > 1) {
  60. auto element_value = vm.argument(1);
  61. if (!(element_value.is_object() && is<Web::DOM::ParentNode>(element_value.as_object()))) {
  62. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Node");
  63. }
  64. auto& element = static_cast<Web::DOM::ParentNode&>(element_value.as_object());
  65. return TRY(Web::Bindings::throw_dom_exception_if_needed(vm, [&]() {
  66. return element.query_selector(selector);
  67. }));
  68. }
  69. return TRY(Web::Bindings::throw_dom_exception_if_needed(vm, [&]() {
  70. return window.associated_document().query_selector(selector);
  71. }));
  72. }
  73. JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalEnvironmentExtensions::$$_function)
  74. {
  75. auto* console_global_object = TRY(get_console(vm));
  76. auto& window = *console_global_object->m_window_object;
  77. auto selector = TRY(vm.argument(0).to_string(vm));
  78. Web::DOM::ParentNode* element = &window.associated_document();
  79. if (vm.argument_count() > 1) {
  80. auto element_value = vm.argument(1);
  81. if (!(element_value.is_object() && is<Web::DOM::ParentNode>(element_value.as_object()))) {
  82. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Node");
  83. }
  84. element = static_cast<Web::DOM::ParentNode*>(&element_value.as_object());
  85. }
  86. auto node_list = TRY(Web::Bindings::throw_dom_exception_if_needed(vm, [&]() {
  87. return element->query_selector_all(selector);
  88. }));
  89. auto* array = TRY(JS::Array::create(*vm.current_realm(), node_list->length()));
  90. for (auto i = 0u; i < node_list->length(); ++i) {
  91. TRY(array->create_data_property_or_throw(i, node_list->item_value(i)));
  92. }
  93. return array;
  94. }
  95. }