Internals.cpp 1.9 KB

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