mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-24 09:52:31 +00:00
LibDevTools: Support highlighting DOM nodes hovered in the inspector
This commit is contained in:
parent
2386859e4b
commit
6e8d77ff7f
Notes:
github-actions[bot]
2025-02-24 17:07:00 +00:00
Author: https://github.com/trflynn89
Commit: 6e8d77ff7f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3666
Reviewed-by: https://github.com/AtkinsSJ
10 changed files with 116 additions and 9 deletions
|
@ -35,6 +35,14 @@ void FrameActor::handle_message(StringView type, JsonObject const&)
|
||||||
JsonObject response;
|
JsonObject response;
|
||||||
response.set("from"sv, name());
|
response.set("from"sv, name());
|
||||||
|
|
||||||
|
if (type == "detach"sv) {
|
||||||
|
if (auto tab = m_tab.strong_ref())
|
||||||
|
tab->reset_selected_node();
|
||||||
|
|
||||||
|
send_message(move(response));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (type == "listFrames"sv) {
|
if (type == "listFrames"sv) {
|
||||||
send_message(move(response));
|
send_message(move(response));
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -6,33 +6,58 @@
|
||||||
|
|
||||||
#include <AK/JsonObject.h>
|
#include <AK/JsonObject.h>
|
||||||
#include <LibDevTools/Actors/HighlighterActor.h>
|
#include <LibDevTools/Actors/HighlighterActor.h>
|
||||||
|
#include <LibDevTools/Actors/InspectorActor.h>
|
||||||
|
#include <LibDevTools/Actors/TabActor.h>
|
||||||
|
#include <LibDevTools/Actors/WalkerActor.h>
|
||||||
|
#include <LibDevTools/DevToolsDelegate.h>
|
||||||
|
#include <LibDevTools/DevToolsServer.h>
|
||||||
|
|
||||||
namespace DevTools {
|
namespace DevTools {
|
||||||
|
|
||||||
NonnullRefPtr<HighlighterActor> HighlighterActor::create(DevToolsServer& devtools, String name)
|
NonnullRefPtr<HighlighterActor> HighlighterActor::create(DevToolsServer& devtools, String name, WeakPtr<InspectorActor> inspector)
|
||||||
{
|
{
|
||||||
return adopt_ref(*new HighlighterActor(devtools, move(name)));
|
return adopt_ref(*new HighlighterActor(devtools, move(name), move(inspector)));
|
||||||
}
|
}
|
||||||
|
|
||||||
HighlighterActor::HighlighterActor(DevToolsServer& devtools, String name)
|
HighlighterActor::HighlighterActor(DevToolsServer& devtools, String name, WeakPtr<InspectorActor> inspector)
|
||||||
: Actor(devtools, move(name))
|
: Actor(devtools, move(name))
|
||||||
|
, m_inspector(move(inspector))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
HighlighterActor::~HighlighterActor() = default;
|
HighlighterActor::~HighlighterActor() = default;
|
||||||
|
|
||||||
void HighlighterActor::handle_message(StringView type, JsonObject const&)
|
void HighlighterActor::handle_message(StringView type, JsonObject const& message)
|
||||||
{
|
{
|
||||||
JsonObject response;
|
JsonObject response;
|
||||||
response.set("from"sv, name());
|
response.set("from"sv, name());
|
||||||
|
|
||||||
if (type == "show"sv) {
|
if (type == "show"sv) {
|
||||||
response.set("value"sv, true);
|
auto node = message.get_string("node"sv);
|
||||||
|
if (!node.has_value()) {
|
||||||
|
send_missing_parameter_error("node"sv);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto tab = InspectorActor::tab_for(m_inspector);
|
||||||
|
auto walker = InspectorActor::walker_for(m_inspector);
|
||||||
|
response.set("value"sv, false);
|
||||||
|
|
||||||
|
if (tab && walker) {
|
||||||
|
if (auto const& dom_node = walker->dom_node(*node); dom_node.has_value()) {
|
||||||
|
devtools().delegate().highlight_dom_node(tab->description(), dom_node->id, dom_node->pseudo_element);
|
||||||
|
response.set("value"sv, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
send_message(move(response));
|
send_message(move(response));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == "hide"sv) {
|
if (type == "hide"sv) {
|
||||||
|
if (auto tab = InspectorActor::tab_for(m_inspector))
|
||||||
|
devtools().delegate().clear_highlighted_dom_node(tab->description());
|
||||||
|
|
||||||
send_message(move(response));
|
send_message(move(response));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,14 +15,16 @@ class HighlighterActor final : public Actor {
|
||||||
public:
|
public:
|
||||||
static constexpr auto base_name = "highlighter"sv;
|
static constexpr auto base_name = "highlighter"sv;
|
||||||
|
|
||||||
static NonnullRefPtr<HighlighterActor> create(DevToolsServer&, String name);
|
static NonnullRefPtr<HighlighterActor> create(DevToolsServer&, String name, WeakPtr<InspectorActor>);
|
||||||
virtual ~HighlighterActor() override;
|
virtual ~HighlighterActor() override;
|
||||||
|
|
||||||
virtual void handle_message(StringView type, JsonObject const&) override;
|
virtual void handle_message(StringView type, JsonObject const&) override;
|
||||||
JsonValue serialize_highlighter() const;
|
JsonValue serialize_highlighter() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HighlighterActor(DevToolsServer&, String name);
|
HighlighterActor(DevToolsServer&, String name, WeakPtr<InspectorActor>);
|
||||||
|
|
||||||
|
WeakPtr<InspectorActor> m_inspector;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ void InspectorActor::handle_message(StringView type, JsonObject const& message)
|
||||||
}
|
}
|
||||||
|
|
||||||
auto highlighter = m_highlighters.ensure(*type_name, [&]() -> NonnullRefPtr<HighlighterActor> {
|
auto highlighter = m_highlighters.ensure(*type_name, [&]() -> NonnullRefPtr<HighlighterActor> {
|
||||||
return devtools().register_actor<HighlighterActor>();
|
return devtools().register_actor<HighlighterActor>(*this);
|
||||||
});
|
});
|
||||||
|
|
||||||
response.set("highlighter"sv, highlighter->serialize_highlighter());
|
response.set("highlighter"sv, highlighter->serialize_highlighter());
|
||||||
|
@ -82,12 +82,19 @@ void InspectorActor::handle_message(StringView type, JsonObject const& message)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type == "supportsHighlighters"sv) {
|
||||||
|
response.set("value"sv, true);
|
||||||
|
send_message(move(response));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
send_unrecognized_packet_type_error(type);
|
send_unrecognized_packet_type_error(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InspectorActor::received_dom_tree(JsonObject dom_tree, BlockToken block_token)
|
void InspectorActor::received_dom_tree(JsonObject dom_tree, BlockToken block_token)
|
||||||
{
|
{
|
||||||
auto& walker_actor = devtools().register_actor<WalkerActor>(m_tab, move(dom_tree));
|
auto& walker_actor = devtools().register_actor<WalkerActor>(m_tab, move(dom_tree));
|
||||||
|
m_walker = walker_actor;
|
||||||
|
|
||||||
JsonObject walker;
|
JsonObject walker;
|
||||||
walker.set("actor"sv, walker_actor.name());
|
walker.set("actor"sv, walker_actor.name());
|
||||||
|
@ -99,4 +106,18 @@ void InspectorActor::received_dom_tree(JsonObject dom_tree, BlockToken block_tok
|
||||||
send_message(move(message), move(block_token));
|
send_message(move(message), move(block_token));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RefPtr<TabActor> InspectorActor::tab_for(WeakPtr<InspectorActor> const& weak_inspector)
|
||||||
|
{
|
||||||
|
if (auto inspector = weak_inspector.strong_ref())
|
||||||
|
return inspector->m_tab.strong_ref();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPtr<WalkerActor> InspectorActor::walker_for(WeakPtr<InspectorActor> const& weak_inspector)
|
||||||
|
{
|
||||||
|
if (auto inspector = weak_inspector.strong_ref())
|
||||||
|
return inspector->m_walker.strong_ref();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,12 +21,16 @@ public:
|
||||||
|
|
||||||
virtual void handle_message(StringView type, JsonObject const&) override;
|
virtual void handle_message(StringView type, JsonObject const&) override;
|
||||||
|
|
||||||
|
static RefPtr<TabActor> tab_for(WeakPtr<InspectorActor> const&);
|
||||||
|
static RefPtr<WalkerActor> walker_for(WeakPtr<InspectorActor> const&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
InspectorActor(DevToolsServer&, String name, WeakPtr<TabActor>);
|
InspectorActor(DevToolsServer&, String name, WeakPtr<TabActor>);
|
||||||
|
|
||||||
void received_dom_tree(JsonObject, BlockToken);
|
void received_dom_tree(JsonObject, BlockToken);
|
||||||
|
|
||||||
WeakPtr<TabActor> m_tab;
|
WeakPtr<TabActor> m_tab;
|
||||||
|
WeakPtr<WalkerActor> m_walker;
|
||||||
WeakPtr<PageStyleActor> m_page_style;
|
WeakPtr<PageStyleActor> m_page_style;
|
||||||
HashMap<String, WeakPtr<HighlighterActor>> m_highlighters;
|
HashMap<String, WeakPtr<HighlighterActor>> m_highlighters;
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <AK/JsonObject.h>
|
#include <AK/JsonObject.h>
|
||||||
#include <LibDevTools/Actors/TabActor.h>
|
#include <LibDevTools/Actors/TabActor.h>
|
||||||
#include <LibDevTools/Actors/WatcherActor.h>
|
#include <LibDevTools/Actors/WatcherActor.h>
|
||||||
|
#include <LibDevTools/DevToolsDelegate.h>
|
||||||
#include <LibDevTools/DevToolsServer.h>
|
#include <LibDevTools/DevToolsServer.h>
|
||||||
|
|
||||||
namespace DevTools {
|
namespace DevTools {
|
||||||
|
@ -22,7 +23,10 @@ TabActor::TabActor(DevToolsServer& devtools, String name, TabDescription descrip
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
TabActor::~TabActor() = default;
|
TabActor::~TabActor()
|
||||||
|
{
|
||||||
|
reset_selected_node();
|
||||||
|
}
|
||||||
|
|
||||||
void TabActor::handle_message(StringView type, JsonObject const&)
|
void TabActor::handle_message(StringView type, JsonObject const&)
|
||||||
{
|
{
|
||||||
|
@ -69,4 +73,9 @@ JsonObject TabActor::serialize_description() const
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TabActor::reset_selected_node()
|
||||||
|
{
|
||||||
|
devtools().delegate().clear_highlighted_dom_node(description());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,8 @@ public:
|
||||||
TabDescription const& description() const { return m_description; }
|
TabDescription const& description() const { return m_description; }
|
||||||
JsonObject serialize_description() const;
|
JsonObject serialize_description() const;
|
||||||
|
|
||||||
|
void reset_selected_node();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TabActor(DevToolsServer&, String name, TabDescription);
|
TabActor(DevToolsServer&, String name, TabDescription);
|
||||||
|
|
||||||
|
|
|
@ -228,6 +228,28 @@ JsonValue WalkerActor::serialize_node(JsonObject const& node) const
|
||||||
return serialized;
|
return serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<WalkerActor::DOMNode> WalkerActor::dom_node(StringView actor)
|
||||||
|
{
|
||||||
|
auto maybe_dom_node = m_actor_to_dom_node_map.get(actor);
|
||||||
|
if (!maybe_dom_node.has_value() || !maybe_dom_node.value())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto const& dom_node = *maybe_dom_node.value();
|
||||||
|
|
||||||
|
auto pseudo_element = dom_node.get_integer<UnderlyingType<Web::CSS::Selector::PseudoElement::Type>>("pseudo-element"sv).map([](auto value) {
|
||||||
|
VERIFY(value < to_underlying(Web::CSS::Selector::PseudoElement::Type::KnownPseudoElementCount));
|
||||||
|
return static_cast<Web::CSS::Selector::PseudoElement::Type>(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
Web::UniqueNodeID node_id { 0 };
|
||||||
|
if (pseudo_element.has_value())
|
||||||
|
node_id = dom_node.get_integer<Web::UniqueNodeID::Type>("parent-id"sv).value();
|
||||||
|
else
|
||||||
|
node_id = dom_node.get_integer<Web::UniqueNodeID::Type>("id"sv).value();
|
||||||
|
|
||||||
|
return DOMNode { .node = dom_node, .id = node_id, .pseudo_element = pseudo_element };
|
||||||
|
}
|
||||||
|
|
||||||
Optional<JsonObject const&> WalkerActor::find_node_by_selector(JsonObject const& node, StringView selector)
|
Optional<JsonObject const&> WalkerActor::find_node_by_selector(JsonObject const& node, StringView selector)
|
||||||
{
|
{
|
||||||
auto matches = [&](auto const& candidate) {
|
auto matches = [&](auto const& candidate) {
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
#include <AK/NonnullRefPtr.h>
|
#include <AK/NonnullRefPtr.h>
|
||||||
#include <AK/Optional.h>
|
#include <AK/Optional.h>
|
||||||
#include <LibDevTools/Actor.h>
|
#include <LibDevTools/Actor.h>
|
||||||
|
#include <LibWeb/CSS/Selector.h>
|
||||||
|
#include <LibWeb/Forward.h>
|
||||||
|
|
||||||
namespace DevTools {
|
namespace DevTools {
|
||||||
|
|
||||||
|
@ -26,6 +28,13 @@ public:
|
||||||
static bool is_suitable_for_dom_inspection(JsonValue const&);
|
static bool is_suitable_for_dom_inspection(JsonValue const&);
|
||||||
JsonValue serialize_root() const;
|
JsonValue serialize_root() const;
|
||||||
|
|
||||||
|
struct DOMNode {
|
||||||
|
JsonObject const& node;
|
||||||
|
Web::UniqueNodeID id { 0 };
|
||||||
|
Optional<Web::CSS::Selector::PseudoElement::Type> pseudo_element;
|
||||||
|
};
|
||||||
|
Optional<DOMNode> dom_node(StringView actor);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
WalkerActor(DevToolsServer&, String name, WeakPtr<TabActor>, JsonObject dom_tree);
|
WalkerActor(DevToolsServer&, String name, WeakPtr<TabActor>, JsonObject dom_tree);
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,8 @@
|
||||||
#include <LibDevTools/Actors/CSSPropertiesActor.h>
|
#include <LibDevTools/Actors/CSSPropertiesActor.h>
|
||||||
#include <LibDevTools/Actors/TabActor.h>
|
#include <LibDevTools/Actors/TabActor.h>
|
||||||
#include <LibDevTools/Forward.h>
|
#include <LibDevTools/Forward.h>
|
||||||
|
#include <LibWeb/CSS/Selector.h>
|
||||||
|
#include <LibWeb/Forward.h>
|
||||||
|
|
||||||
namespace DevTools {
|
namespace DevTools {
|
||||||
|
|
||||||
|
@ -25,6 +27,9 @@ public:
|
||||||
|
|
||||||
using OnTabInspectionComplete = Function<void(ErrorOr<JsonValue>)>;
|
using OnTabInspectionComplete = Function<void(ErrorOr<JsonValue>)>;
|
||||||
virtual void inspect_tab(TabDescription const&, OnTabInspectionComplete) const { }
|
virtual void inspect_tab(TabDescription const&, OnTabInspectionComplete) const { }
|
||||||
|
|
||||||
|
virtual void highlight_dom_node(TabDescription const&, Web::UniqueNodeID, Optional<Web::CSS::Selector::PseudoElement::Type>) const { }
|
||||||
|
virtual void clear_highlighted_dom_node(TabDescription const&) const { }
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue