Internals.cpp 2.0 KB

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