From bd1e35c7263e2d730b6bd24717469ffddf004b3c Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 13 Sep 2023 19:52:37 -0400 Subject: [PATCH] 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. --- Ladybird/AppKit/UI/Inspector.mm | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Ladybird/AppKit/UI/Inspector.mm b/Ladybird/AppKit/UI/Inspector.mm index 6b3ca0a201..b956a6f0e3 100644 --- a/Ladybird/AppKit/UI/Inspector.mm +++ b/Ladybird/AppKit/UI/Inspector.mm @@ -5,6 +5,8 @@ */ #include +#include +#include #include #include @@ -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 : public GenericTraits { + 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 () { Selection m_selection; + + HashMap m_dom_node_to_parent_map; + HashMap 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)