mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-10-24 17:09:43 +00:00
LibDevTools: Pull DOMNode type and related code into new Node.h
Accessibility nodes will share this code, so make it available, and rename DOMNode to Node.
This commit is contained in:
parent
eeb5446c1b
commit
d5fe7f7a98
Notes:
github-actions[bot]
2025-10-20 09:52:37 +00:00
Author: https://github.com/AtkinsSJ
Commit: d5fe7f7a98
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6462
Reviewed-by: https://github.com/trflynn89
5 changed files with 57 additions and 34 deletions
|
|
@ -11,20 +11,12 @@
|
||||||
#include <AK/Traits.h>
|
#include <AK/Traits.h>
|
||||||
#include <LibDevTools/Actor.h>
|
#include <LibDevTools/Actor.h>
|
||||||
#include <LibDevTools/Forward.h>
|
#include <LibDevTools/Forward.h>
|
||||||
|
#include <LibDevTools/Node.h>
|
||||||
#include <LibWeb/CSS/Selector.h>
|
#include <LibWeb/CSS/Selector.h>
|
||||||
#include <LibWeb/Forward.h>
|
#include <LibWeb/Forward.h>
|
||||||
|
|
||||||
namespace DevTools {
|
namespace DevTools {
|
||||||
|
|
||||||
struct DEVTOOLS_API NodeIdentifier {
|
|
||||||
static NodeIdentifier for_node(JsonObject const& node);
|
|
||||||
|
|
||||||
bool operator==(NodeIdentifier const&) const = default;
|
|
||||||
|
|
||||||
Web::UniqueNodeID id { 0 };
|
|
||||||
Optional<Web::CSS::PseudoElement> pseudo_element;
|
|
||||||
};
|
|
||||||
|
|
||||||
class NodeActor final : public Actor {
|
class NodeActor final : public Actor {
|
||||||
public:
|
public:
|
||||||
static constexpr auto base_name = "node"sv;
|
static constexpr auto base_name = "node"sv;
|
||||||
|
|
|
||||||
|
|
@ -388,19 +388,6 @@ JsonValue WalkerActor::serialize_root() const
|
||||||
return serialize_node(m_dom_tree);
|
return serialize_node(m_dom_tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr Web::DOM::NodeType parse_node_type(StringView type)
|
|
||||||
{
|
|
||||||
if (type == "document"sv)
|
|
||||||
return Web::DOM::NodeType::DOCUMENT_NODE;
|
|
||||||
if (type == "element"sv)
|
|
||||||
return Web::DOM::NodeType::ELEMENT_NODE;
|
|
||||||
if (type == "text"sv)
|
|
||||||
return Web::DOM::NodeType::TEXT_NODE;
|
|
||||||
if (type == "comment"sv)
|
|
||||||
return Web::DOM::NodeType::COMMENT_NODE;
|
|
||||||
return Web::DOM::NodeType::INVALID;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonValue WalkerActor::serialize_node(JsonObject const& node) const
|
JsonValue WalkerActor::serialize_node(JsonObject const& node) const
|
||||||
{
|
{
|
||||||
auto tab = m_tab.strong_ref();
|
auto tab = m_tab.strong_ref();
|
||||||
|
|
@ -414,7 +401,7 @@ JsonValue WalkerActor::serialize_node(JsonObject const& node) const
|
||||||
auto name = node.get_string("name"sv).release_value();
|
auto name = node.get_string("name"sv).release_value();
|
||||||
auto type = node.get_string("type"sv).release_value();
|
auto type = node.get_string("type"sv).release_value();
|
||||||
|
|
||||||
auto dom_type = parse_node_type(type);
|
auto dom_type = parse_dom_node_type(type);
|
||||||
JsonValue node_value;
|
JsonValue node_value;
|
||||||
|
|
||||||
auto is_top_level_document = &node == &m_dom_tree;
|
auto is_top_level_document = &node == &m_dom_tree;
|
||||||
|
|
@ -450,7 +437,7 @@ JsonValue WalkerActor::serialize_node(JsonObject const& node) const
|
||||||
|
|
||||||
if (auto parent_actor = m_dom_node_id_to_actor_map.get(parent_id); parent_actor.has_value()) {
|
if (auto parent_actor = m_dom_node_id_to_actor_map.get(parent_id); parent_actor.has_value()) {
|
||||||
if (auto parent_node = WalkerActor::dom_node_for(this, *parent_actor); parent_node.has_value()) {
|
if (auto parent_node = WalkerActor::dom_node_for(this, *parent_actor); parent_node.has_value()) {
|
||||||
dom_type = parse_node_type(parent_node->node.get_string("type"sv).value());
|
dom_type = parse_dom_node_type(parent_node->node.get_string("type"sv).value());
|
||||||
is_displayed = !is_top_level_document && parent_node->node.get_bool("visible"sv).value_or(false);
|
is_displayed = !is_top_level_document && parent_node->node.get_bool("visible"sv).value_or(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -518,14 +505,14 @@ JsonValue WalkerActor::serialize_node(JsonObject const& node) const
|
||||||
return serialized;
|
return serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<WalkerActor::DOMNode> WalkerActor::dom_node_for(WeakPtr<WalkerActor> const& weak_walker, StringView actor)
|
Optional<Node> WalkerActor::dom_node_for(WeakPtr<WalkerActor> const& weak_walker, StringView actor)
|
||||||
{
|
{
|
||||||
if (auto walker = weak_walker.strong_ref())
|
if (auto walker = weak_walker.strong_ref())
|
||||||
return walker->dom_node(actor);
|
return walker->dom_node(actor);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<WalkerActor::DOMNode> WalkerActor::dom_node(StringView actor)
|
Optional<Node> WalkerActor::dom_node(StringView actor)
|
||||||
{
|
{
|
||||||
auto tab = m_tab.strong_ref();
|
auto tab = m_tab.strong_ref();
|
||||||
if (!tab)
|
if (!tab)
|
||||||
|
|
@ -538,7 +525,7 @@ Optional<WalkerActor::DOMNode> WalkerActor::dom_node(StringView actor)
|
||||||
auto const& dom_node = *maybe_dom_node.value();
|
auto const& dom_node = *maybe_dom_node.value();
|
||||||
auto identifier = NodeIdentifier::for_node(dom_node);
|
auto identifier = NodeIdentifier::for_node(dom_node);
|
||||||
|
|
||||||
return DOMNode { .node = dom_node, .identifier = move(identifier), .tab = tab.release_nonnull() };
|
return Node { .node = dom_node, .identifier = move(identifier), .tab = tab.release_nonnull() };
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
#include <LibDevTools/Actor.h>
|
#include <LibDevTools/Actor.h>
|
||||||
#include <LibDevTools/Actors/NodeActor.h>
|
#include <LibDevTools/Actors/NodeActor.h>
|
||||||
#include <LibDevTools/Forward.h>
|
#include <LibDevTools/Forward.h>
|
||||||
|
#include <LibDevTools/Node.h>
|
||||||
#include <LibWeb/Forward.h>
|
#include <LibWeb/Forward.h>
|
||||||
#include <LibWebView/Forward.h>
|
#include <LibWebView/Forward.h>
|
||||||
|
|
||||||
|
|
@ -28,13 +29,8 @@ 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 {
|
static Optional<Node> dom_node_for(WeakPtr<WalkerActor> const&, StringView actor);
|
||||||
JsonObject const& node;
|
Optional<Node> dom_node(StringView actor);
|
||||||
NodeIdentifier identifier;
|
|
||||||
NonnullRefPtr<TabActor> tab;
|
|
||||||
};
|
|
||||||
static Optional<DOMNode> dom_node_for(WeakPtr<WalkerActor> const&, StringView actor);
|
|
||||||
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);
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ class WalkerActor;
|
||||||
class WatcherActor;
|
class WatcherActor;
|
||||||
|
|
||||||
struct CSSProperty;
|
struct CSSProperty;
|
||||||
|
struct Node;
|
||||||
struct ProcessDescription;
|
struct ProcessDescription;
|
||||||
struct TabDescription;
|
struct TabDescription;
|
||||||
|
|
||||||
|
|
|
||||||
47
Libraries/LibDevTools/Node.h
Normal file
47
Libraries/LibDevTools/Node.h
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
|
||||||
|
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/JsonObject.h>
|
||||||
|
#include <LibDevTools/Actors/TabActor.h>
|
||||||
|
#include <LibDevTools/Forward.h>
|
||||||
|
#include <LibWeb/CSS/PseudoElement.h>
|
||||||
|
#include <LibWeb/DOM/NodeType.h>
|
||||||
|
#include <LibWeb/Forward.h>
|
||||||
|
|
||||||
|
namespace DevTools {
|
||||||
|
|
||||||
|
struct DEVTOOLS_API NodeIdentifier {
|
||||||
|
static NodeIdentifier for_node(JsonObject const& node);
|
||||||
|
|
||||||
|
bool operator==(NodeIdentifier const&) const = default;
|
||||||
|
|
||||||
|
Web::UniqueNodeID id { 0 };
|
||||||
|
Optional<Web::CSS::PseudoElement> pseudo_element;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DEVTOOLS_API Node {
|
||||||
|
JsonObject const& node;
|
||||||
|
NodeIdentifier identifier;
|
||||||
|
NonnullRefPtr<TabActor> tab;
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr Web::DOM::NodeType parse_dom_node_type(StringView type)
|
||||||
|
{
|
||||||
|
if (type == "document"sv)
|
||||||
|
return Web::DOM::NodeType::DOCUMENT_NODE;
|
||||||
|
if (type == "element"sv)
|
||||||
|
return Web::DOM::NodeType::ELEMENT_NODE;
|
||||||
|
if (type == "text"sv)
|
||||||
|
return Web::DOM::NodeType::TEXT_NODE;
|
||||||
|
if (type == "comment"sv)
|
||||||
|
return Web::DOM::NodeType::COMMENT_NODE;
|
||||||
|
return Web::DOM::NodeType::INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue