LibWeb: Implement DOMTreeModel::index_for_node()
This will be used to find the index to select when the DOM Inspector is told to inspect a specific node.
This commit is contained in:
parent
fe820f6d5a
commit
e824454ab4
Notes:
sideshowbarker
2024-07-18 04:52:01 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/e824454ab47 Pull-request: https://github.com/SerenityOS/serenity/pull/9725 Issue: https://github.com/SerenityOS/serenity/issues/8935 Reviewed-by: https://github.com/awesomekling
2 changed files with 21 additions and 0 deletions
|
@ -155,6 +155,7 @@ GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol
|
||||||
void DOMTreeModel::map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* node)
|
void DOMTreeModel::map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* node)
|
||||||
{
|
{
|
||||||
m_dom_node_to_parent_map.set(node, parent);
|
m_dom_node_to_parent_map.set(node, parent);
|
||||||
|
m_node_id_to_dom_node_map.set(node->get("id").to_i32(), node);
|
||||||
|
|
||||||
auto const* children = get_children(*node);
|
auto const* children = get_children(*node);
|
||||||
if (!children)
|
if (!children)
|
||||||
|
@ -166,4 +167,21 @@ void DOMTreeModel::map_dom_nodes_to_parent(JsonObject const* parent, JsonObject
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GUI::ModelIndex DOMTreeModel::index_for_node(i32 node_id) const
|
||||||
|
{
|
||||||
|
auto node = m_node_id_to_dom_node_map.get(node_id).value_or(nullptr);
|
||||||
|
if (node) {
|
||||||
|
auto* parent = get_parent(*node);
|
||||||
|
auto parent_children = get_children(*parent);
|
||||||
|
for (size_t i = 0; i < parent_children->size(); i++) {
|
||||||
|
if (&parent_children->at(i).as_object() == node) {
|
||||||
|
return create_index(i, 0, node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dbgln("Didn't find index for node {}!", node_id);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,8 @@ public:
|
||||||
virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
|
virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
|
||||||
virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
|
virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
|
||||||
|
|
||||||
|
GUI::ModelIndex index_for_node(i32 node_id) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit DOMTreeModel(JsonObject);
|
explicit DOMTreeModel(JsonObject);
|
||||||
|
|
||||||
|
@ -57,6 +59,7 @@ private:
|
||||||
GUI::Icon m_text_icon;
|
GUI::Icon m_text_icon;
|
||||||
JsonObject m_dom_tree;
|
JsonObject m_dom_tree;
|
||||||
HashMap<JsonObject const*, JsonObject const*> m_dom_node_to_parent_map;
|
HashMap<JsonObject const*, JsonObject const*> m_dom_node_to_parent_map;
|
||||||
|
HashMap<i32, JsonObject const*> m_node_id_to_dom_node_map;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue