mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-05 23:59:49 +00:00
LibDevTools+LibWebView: Implement requests to delete DOM nodes
This commit is contained in:
parent
bdb31b029a
commit
29122786b9
Notes:
github-actions[bot]
2025-03-08 00:27:06 +00:00
Author: https://github.com/trflynn89
Commit: 29122786b9
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3850
5 changed files with 152 additions and 0 deletions
|
@ -134,6 +134,37 @@ void WalkerActor::handle_message(StringView type, JsonObject const& message)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type == "isInDOMTree"sv) {
|
||||||
|
auto node = message.get_string("node"sv);
|
||||||
|
if (!node.has_value()) {
|
||||||
|
send_missing_parameter_error("node"sv);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
response.set("attached"sv, m_actor_to_dom_node_map.contains(*node));
|
||||||
|
send_message(move(response));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == "previousSibling"sv) {
|
||||||
|
auto node = message.get_string("node"sv);
|
||||||
|
if (!node.has_value()) {
|
||||||
|
send_missing_parameter_error("node"sv);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonValue previous_sibling;
|
||||||
|
|
||||||
|
if (auto dom_node = WalkerActor::dom_node_for(*this, *node); dom_node.has_value()) {
|
||||||
|
if (auto previous_sibling_node = previous_sibling_for_node(dom_node->node); previous_sibling_node.has_value())
|
||||||
|
previous_sibling = serialize_node(*previous_sibling_node);
|
||||||
|
}
|
||||||
|
|
||||||
|
response.set("node"sv, move(previous_sibling));
|
||||||
|
send_message(move(response));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (type == "querySelector"sv) {
|
if (type == "querySelector"sv) {
|
||||||
auto node = message.get_string("node"sv);
|
auto node = message.get_string("node"sv);
|
||||||
if (!node.has_value()) {
|
if (!node.has_value()) {
|
||||||
|
@ -165,6 +196,49 @@ void WalkerActor::handle_message(StringView type, JsonObject const& message)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type == "removeNode"sv) {
|
||||||
|
auto node = message.get_string("node"sv);
|
||||||
|
if (!node.has_value()) {
|
||||||
|
send_missing_parameter_error("node"sv);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto dom_node = WalkerActor::dom_node_for(*this, *node); dom_node.has_value()) {
|
||||||
|
JsonValue next_sibling;
|
||||||
|
if (auto next_sibling_node = next_sibling_for_node(dom_node->node); next_sibling_node.has_value())
|
||||||
|
next_sibling = serialize_node(*next_sibling_node);
|
||||||
|
|
||||||
|
auto parent_node = remove_node(dom_node->node);
|
||||||
|
if (!parent_node.has_value())
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto block_token = block_responses();
|
||||||
|
|
||||||
|
devtools().delegate().remove_dom_node(
|
||||||
|
dom_node->tab->description(), dom_node->identifier.id,
|
||||||
|
[weak_self = make_weak_ptr<WalkerActor>(), next_sibling = move(next_sibling), block_token = move(block_token)](ErrorOr<Web::UniqueNodeID> node_id) mutable {
|
||||||
|
if (node_id.is_error()) {
|
||||||
|
dbgln_if(DEVTOOLS_DEBUG, "Unable to edit DOM node: {}", node_id.error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto self = weak_self.strong_ref()) {
|
||||||
|
JsonObject message;
|
||||||
|
message.set("from"sv, self->name());
|
||||||
|
message.set("nextSibling"sv, move(next_sibling));
|
||||||
|
self->send_message(move(message), move(block_token));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == "retainNode"sv) {
|
||||||
|
send_message(move(response));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (type == "watchRootNode"sv) {
|
if (type == "watchRootNode"sv) {
|
||||||
response.set("type"sv, "root-available"sv);
|
response.set("type"sv, "root-available"sv);
|
||||||
response.set("node"sv, serialize_root());
|
response.set("node"sv, serialize_root());
|
||||||
|
@ -352,6 +426,71 @@ Optional<JsonObject const&> WalkerActor::find_node_by_selector(JsonObject const&
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum class Direction {
|
||||||
|
Previous,
|
||||||
|
Next,
|
||||||
|
};
|
||||||
|
static Optional<JsonObject const&> sibling_for_node(JsonObject const& parent, JsonObject const& node, Direction direction)
|
||||||
|
{
|
||||||
|
auto children = parent.get_array("children"sv);
|
||||||
|
VERIFY(children.has_value());
|
||||||
|
|
||||||
|
auto index = children->values().find_first_index_if([&](auto const& child) {
|
||||||
|
return &child.as_object() == &node;
|
||||||
|
});
|
||||||
|
VERIFY(index.has_value());
|
||||||
|
|
||||||
|
switch (direction) {
|
||||||
|
case Direction::Previous:
|
||||||
|
if (*index == 0)
|
||||||
|
return {};
|
||||||
|
index = *index - 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Direction::Next:
|
||||||
|
if (*index == children->size() - 1)
|
||||||
|
return {};
|
||||||
|
index = *index + 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return children->at(*index).as_object();
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<JsonObject const&> WalkerActor::previous_sibling_for_node(JsonObject const& node)
|
||||||
|
{
|
||||||
|
auto parent = m_dom_node_to_parent_map.get(&node);
|
||||||
|
if (!parent.has_value() || !parent.value())
|
||||||
|
return {};
|
||||||
|
return sibling_for_node(*parent.value(), node, Direction::Previous);
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<JsonObject const&> WalkerActor::next_sibling_for_node(JsonObject const& node)
|
||||||
|
{
|
||||||
|
auto parent = m_dom_node_to_parent_map.get(&node);
|
||||||
|
if (!parent.has_value() || !parent.value())
|
||||||
|
return {};
|
||||||
|
return sibling_for_node(*parent.value(), node, Direction::Next);
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<JsonObject const&> WalkerActor::remove_node(JsonObject const& node)
|
||||||
|
{
|
||||||
|
auto maybe_parent = m_dom_node_to_parent_map.get(&node);
|
||||||
|
if (!maybe_parent.has_value() || !maybe_parent.value())
|
||||||
|
return {};
|
||||||
|
auto const& parent = *maybe_parent.value();
|
||||||
|
|
||||||
|
auto children = parent.get_array("children"sv);
|
||||||
|
VERIFY(children.has_value());
|
||||||
|
|
||||||
|
const_cast<JsonArray&>(*children).values().remove_first_matching([&](auto const& child) {
|
||||||
|
return &child.as_object() == &node;
|
||||||
|
});
|
||||||
|
|
||||||
|
populate_dom_tree_cache();
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
void WalkerActor::new_dom_node_mutation(WebView::Mutation mutation)
|
void WalkerActor::new_dom_node_mutation(WebView::Mutation mutation)
|
||||||
{
|
{
|
||||||
auto serialized_target = JsonValue::from_string(mutation.serialized_target);
|
auto serialized_target = JsonValue::from_string(mutation.serialized_target);
|
||||||
|
|
|
@ -43,6 +43,10 @@ private:
|
||||||
JsonValue serialize_node(JsonObject const&) const;
|
JsonValue serialize_node(JsonObject const&) const;
|
||||||
Optional<JsonObject const&> find_node_by_selector(JsonObject const& node, StringView selector);
|
Optional<JsonObject const&> find_node_by_selector(JsonObject const& node, StringView selector);
|
||||||
|
|
||||||
|
Optional<JsonObject const&> previous_sibling_for_node(JsonObject const& node);
|
||||||
|
Optional<JsonObject const&> next_sibling_for_node(JsonObject const& node);
|
||||||
|
Optional<JsonObject const&> remove_node(JsonObject const& node);
|
||||||
|
|
||||||
void new_dom_node_mutation(WebView::Mutation);
|
void new_dom_node_mutation(WebView::Mutation);
|
||||||
JsonValue serialize_mutations();
|
JsonValue serialize_mutations();
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,7 @@ public:
|
||||||
virtual void set_dom_node_tag(TabDescription const&, Web::UniqueNodeID, String, OnDOMNodeEditComplete) const { }
|
virtual void set_dom_node_tag(TabDescription const&, Web::UniqueNodeID, String, OnDOMNodeEditComplete) const { }
|
||||||
virtual void add_dom_node_attributes(TabDescription const&, Web::UniqueNodeID, Vector<WebView::Attribute>, OnDOMNodeEditComplete) const { }
|
virtual void add_dom_node_attributes(TabDescription const&, Web::UniqueNodeID, Vector<WebView::Attribute>, OnDOMNodeEditComplete) const { }
|
||||||
virtual void replace_dom_node_attribute(TabDescription const&, Web::UniqueNodeID, String, Vector<WebView::Attribute>, OnDOMNodeEditComplete) const { }
|
virtual void replace_dom_node_attribute(TabDescription const&, Web::UniqueNodeID, String, Vector<WebView::Attribute>, OnDOMNodeEditComplete) const { }
|
||||||
|
virtual void remove_dom_node(TabDescription const&, Web::UniqueNodeID, OnDOMNodeEditComplete) const { }
|
||||||
|
|
||||||
using OnScriptEvaluationComplete = Function<void(ErrorOr<JsonValue>)>;
|
using OnScriptEvaluationComplete = Function<void(ErrorOr<JsonValue>)>;
|
||||||
virtual void evaluate_javascript(TabDescription const&, String, OnScriptEvaluationComplete) const { }
|
virtual void evaluate_javascript(TabDescription const&, String, OnScriptEvaluationComplete) const { }
|
||||||
|
|
|
@ -486,6 +486,13 @@ void Application::replace_dom_node_attribute(DevTools::TabDescription const& des
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Application::remove_dom_node(DevTools::TabDescription const& description, Web::UniqueNodeID node_id, OnDOMNodeEditComplete on_complete) const
|
||||||
|
{
|
||||||
|
edit_dom_node(description, move(on_complete), [&](auto& view) {
|
||||||
|
view.remove_dom_node(node_id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void Application::evaluate_javascript(DevTools::TabDescription const& description, String script, OnScriptEvaluationComplete on_complete) const
|
void Application::evaluate_javascript(DevTools::TabDescription const& description, String script, OnScriptEvaluationComplete on_complete) const
|
||||||
{
|
{
|
||||||
auto view = ViewImplementation::find_view_by_id(description.id);
|
auto view = ViewImplementation::find_view_by_id(description.id);
|
||||||
|
|
|
@ -102,6 +102,7 @@ private:
|
||||||
virtual void set_dom_node_tag(DevTools::TabDescription const&, Web::UniqueNodeID, String, OnDOMNodeEditComplete) const override;
|
virtual void set_dom_node_tag(DevTools::TabDescription const&, Web::UniqueNodeID, String, OnDOMNodeEditComplete) const override;
|
||||||
virtual void add_dom_node_attributes(DevTools::TabDescription const&, Web::UniqueNodeID, Vector<Attribute>, OnDOMNodeEditComplete) const override;
|
virtual void add_dom_node_attributes(DevTools::TabDescription const&, Web::UniqueNodeID, Vector<Attribute>, OnDOMNodeEditComplete) const override;
|
||||||
virtual void replace_dom_node_attribute(DevTools::TabDescription const&, Web::UniqueNodeID, String, Vector<Attribute>, OnDOMNodeEditComplete) const override;
|
virtual void replace_dom_node_attribute(DevTools::TabDescription const&, Web::UniqueNodeID, String, Vector<Attribute>, OnDOMNodeEditComplete) const override;
|
||||||
|
virtual void remove_dom_node(DevTools::TabDescription const&, Web::UniqueNodeID, OnDOMNodeEditComplete) const override;
|
||||||
virtual void evaluate_javascript(DevTools::TabDescription const&, String, OnScriptEvaluationComplete) const override;
|
virtual void evaluate_javascript(DevTools::TabDescription const&, String, OnScriptEvaluationComplete) const override;
|
||||||
virtual void listen_for_console_messages(DevTools::TabDescription const&, OnConsoleMessageAvailable, OnReceivedConsoleMessages) const override;
|
virtual void listen_for_console_messages(DevTools::TabDescription const&, OnConsoleMessageAvailable, OnReceivedConsoleMessages) const override;
|
||||||
virtual void stop_listening_for_console_messages(DevTools::TabDescription const&) const override;
|
virtual void stop_listening_for_console_messages(DevTools::TabDescription const&) const override;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue