1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 21:57:35 +00:00

Ladybird: Map the AppKit Inspector's DOM nodes to their IDs and parents

This is in the spirit of commit a4692a6c978a6e66d171e003063449790a6c5879
(and the history behind that commit).

We will need to perform lookups from an integral node ID to the JSON for
that node frequently in the Inspector. We will also need to traverse the
DOM tree from a node, through its ancestors, to the root node. These are
essentially the same maps stored by the Qt Inspector widget.
This commit is contained in:
Timothy Flynn 2023-09-13 19:52:37 -04:00 committed by Andrew Kaster
parent 6304abf94c
commit bd1e35c726

View file

@ -5,6 +5,8 @@
*/
#include <AK/DeprecatedString.h>
#include <AK/HashMap.h>
#include <AK/Traits.h>
#include <LibWeb/CSS/Selector.h>
#include <LibWebView/ViewImplementation.h>
@ -24,6 +26,14 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
static NSString* const CSS_PROPERTY_COLUMN = @"Property";
static NSString* const CSS_VALUE_COLUMN = @"Value";
template<>
struct AK::Traits<NSDictionary*> : public GenericTraits<NSDictionary*> {
static unsigned hash(NSDictionary* dictionary)
{
return [dictionary hash];
}
};
struct Selection {
bool operator==(Selection const& other) const = default;
@ -34,6 +44,9 @@ struct Selection {
@interface Inspector () <NSOutlineViewDataSource, NSOutlineViewDelegate, NSTableViewDataSource>
{
Selection m_selection;
HashMap<NSDictionary*, NSDictionary*> m_dom_node_to_parent_map;
HashMap<i32, NSDictionary*> m_node_id_to_dom_node_map;
}
@property (nonatomic, strong) Tab* tab;
@ -103,6 +116,8 @@ struct Selection {
if (strong_self.dom_tree) {
[strong_self.dom_tree_outline_view reloadItem:nil reloadChildren:YES];
[strong_self.dom_tree_outline_view sizeToFit];
[strong_self prepareDOMNodeMaps:strong_self.dom_tree parentNode:nil];
} else {
strong_self.dom_tree = @{};
}
@ -138,6 +153,9 @@ struct Selection {
{
m_selection = {};
m_dom_node_to_parent_map = {};
m_node_id_to_dom_node_map = {};
self.dom_tree = @{};
[self.dom_tree_outline_view reloadItem:nil reloadChildren:YES];
[self.dom_tree_outline_view sizeToFit];
@ -229,6 +247,20 @@ struct Selection {
}
}
- (void)prepareDOMNodeMaps:(NSDictionary*)dom_node
parentNode:(NSDictionary*)parent_node
{
m_dom_node_to_parent_map.set(dom_node, parent_node);
if (id dom_node_id = [dom_node objectForKey:@"id"]) {
m_node_id_to_dom_node_map.set([dom_node_id intValue], dom_node);
}
for (NSDictionary* child in [dom_node objectForKey:@"children"]) {
[self prepareDOMNodeMaps:child parentNode:dom_node];
}
}
- (void)setSelection:(Selection)selection
{
if (selection == m_selection)