1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:37:43 +00:00

LibWeb: Add hit testing API in internals object

Introduces `internals.hitTest(x, y)` that is going to allow us write
tests for hit testing :)
This commit is contained in:
Aliaksandr Kalenik 2023-08-08 22:44:52 +02:00 committed by Andreas Kling
parent 22a858a0cb
commit bf4e2f3e9c
7 changed files with 60 additions and 0 deletions

View file

@ -7,7 +7,11 @@
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/InternalsPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Internals/Internals.h>
#include <LibWeb/Painting/PaintableBox.h>
namespace Web::Internals {
@ -29,4 +33,21 @@ void Internals::gc()
vm().heap().collect_garbage();
}
JS::Object* Internals::hit_test(double x, double y)
{
auto* active_document = global_object().browsing_context()->top_level_browsing_context().active_document();
// NOTE: Force a layout update just before hit testing. This is because the current layout tree, which is required
// for stacking context traversal, might not exist if this call occurs between the tear_down_layout_tree()
// and update_layout() calls
active_document->update_layout();
auto result = active_document->paintable_box()->hit_test({ x, y }, Painting::HitTestType::Exact);
if (result.has_value()) {
auto hit_tеsting_result = JS::Object::create(realm(), nullptr);
hit_tеsting_result->define_direct_property("node", result->dom_node(), JS::default_attributes);
hit_tеsting_result->define_direct_property("indexInNode", JS::Value(result->index_in_node), JS::default_attributes);
return hit_tеsting_result;
}
return nullptr;
}
}