Internals.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/VM.h>
  7. #include <LibWeb/Bindings/InternalsPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/Event.h>
  11. #include <LibWeb/DOM/EventTarget.h>
  12. #include <LibWeb/HTML/BrowsingContext.h>
  13. #include <LibWeb/HTML/HTMLElement.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/Internals/Internals.h>
  16. #include <LibWeb/Page/Page.h>
  17. #include <LibWeb/Painting/PaintableBox.h>
  18. #include <LibWeb/Painting/ViewportPaintable.h>
  19. namespace Web::Internals {
  20. JS_DEFINE_ALLOCATOR(Internals);
  21. Internals::Internals(JS::Realm& realm)
  22. : Bindings::PlatformObject(realm)
  23. {
  24. }
  25. Internals::~Internals() = default;
  26. void Internals::initialize(JS::Realm& realm)
  27. {
  28. Base::initialize(realm);
  29. WEB_SET_PROTOTYPE_FOR_INTERFACE(Internals);
  30. }
  31. void Internals::signal_text_test_is_done()
  32. {
  33. global_object().browsing_context()->page().client().page_did_finish_text_test();
  34. }
  35. void Internals::gc()
  36. {
  37. vm().heap().collect_garbage();
  38. }
  39. JS::Object* Internals::hit_test(double x, double y)
  40. {
  41. auto* active_document = global_object().browsing_context()->top_level_browsing_context()->active_document();
  42. // NOTE: Force a layout update just before hit testing. This is because the current layout tree, which is required
  43. // for stacking context traversal, might not exist if this call occurs between the tear_down_layout_tree()
  44. // and update_layout() calls
  45. active_document->update_layout();
  46. auto result = active_document->paintable_box()->hit_test({ x, y }, Painting::HitTestType::Exact);
  47. if (result.has_value()) {
  48. auto hit_tеsting_result = JS::Object::create(realm(), nullptr);
  49. hit_tеsting_result->define_direct_property("node", result->dom_node(), JS::default_attributes);
  50. hit_tеsting_result->define_direct_property("indexInNode", JS::Value(result->index_in_node), JS::default_attributes);
  51. return hit_tеsting_result;
  52. }
  53. return nullptr;
  54. }
  55. void Internals::send_text(HTML::HTMLElement& target, String const& text)
  56. {
  57. auto& page = global_object().browsing_context()->page();
  58. target.focus();
  59. for (auto code_point : text.code_points())
  60. page.handle_keydown(code_point_to_key_code(code_point), 0, code_point);
  61. }
  62. void Internals::commit_text()
  63. {
  64. global_object().browsing_context()->page().handle_keydown(Key_Return, 0, 0);
  65. }
  66. void Internals::click(double x, double y)
  67. {
  68. auto& page = global_object().browsing_context()->page();
  69. page.handle_mousedown({ x, y }, { x, y }, 1, 0, 0);
  70. page.handle_mouseup({ x, y }, { x, y }, 1, 0, 0);
  71. }
  72. void Internals::wheel(double x, double y, double delta_x, double delta_y)
  73. {
  74. auto& page = global_object().browsing_context()->page();
  75. page.handle_mousewheel({ x, y }, { x, y }, 0, 0, 0, delta_x, delta_y);
  76. }
  77. WebIDL::ExceptionOr<bool> Internals::dispatch_user_activated_event(DOM::EventTarget& target, DOM::Event& event)
  78. {
  79. event.set_is_trusted(true);
  80. return target.dispatch_event(event);
  81. }
  82. }