Internals.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/Window.h>
  14. #include <LibWeb/Internals/Internals.h>
  15. #include <LibWeb/Page/Page.h>
  16. #include <LibWeb/Painting/PaintableBox.h>
  17. namespace Web::Internals {
  18. Internals::Internals(JS::Realm& realm)
  19. : Bindings::PlatformObject(realm)
  20. {
  21. }
  22. Internals::~Internals() = default;
  23. void Internals::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. Object::set_prototype(&Bindings::ensure_web_prototype<Bindings::InternalsPrototype>(realm, "Internals"));
  27. }
  28. void Internals::signal_text_test_is_done()
  29. {
  30. if (auto* page = global_object().browsing_context()->page()) {
  31. page->client().page_did_finish_text_test();
  32. }
  33. }
  34. void Internals::gc()
  35. {
  36. vm().heap().collect_garbage();
  37. }
  38. JS::Object* Internals::hit_test(double x, double y)
  39. {
  40. auto* active_document = global_object().browsing_context()->top_level_browsing_context()->active_document();
  41. // NOTE: Force a layout update just before hit testing. This is because the current layout tree, which is required
  42. // for stacking context traversal, might not exist if this call occurs between the tear_down_layout_tree()
  43. // and update_layout() calls
  44. active_document->update_layout();
  45. auto result = active_document->paintable_box()->hit_test({ x, y }, Painting::HitTestType::Exact);
  46. if (result.has_value()) {
  47. auto hit_tеsting_result = JS::Object::create(realm(), nullptr);
  48. hit_tеsting_result->define_direct_property("node", result->dom_node(), JS::default_attributes);
  49. hit_tеsting_result->define_direct_property("indexInNode", JS::Value(result->index_in_node), JS::default_attributes);
  50. return hit_tеsting_result;
  51. }
  52. return nullptr;
  53. }
  54. WebIDL::ExceptionOr<bool> Internals::dispatch_user_activated_event(DOM::EventTarget& target, DOM::Event& event)
  55. {
  56. event.set_is_trusted(true);
  57. return target.dispatch_event(event);
  58. }
  59. }