diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-06-30 08:35:07 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-30 15:13:18 +0200 |
commit | 067f7ae60ab0cb7a4d95209df9e1992b353fa0de (patch) | |
tree | 1ff598470cc203b91af721c5e6fff2b62f121d45 /Userland/Libraries | |
parent | 7604c2f38ed92137457b0475a188722352011a5a (diff) | |
download | serenity-067f7ae60ab0cb7a4d95209df9e1992b353fa0de.zip |
LibWeb: Store JSON pointers in the OOPWV DOM Inspector model indices
Currently, the DOM Inspector stores a numeric ID for each DOM node. This
is used to look up the data for that node in the JSON representation of
the DOM. The method to do this search performs a depth-first search
through the JSON value, and is invoked quite frequently.
Instead, we can just store a pointer to the JSON value in the index, and
avoid this search altogether. This is similar to how the IPWV stores a
pointer to the DOM node.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/DOMTreeJSONModel.cpp | 56 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOMTreeJSONModel.h | 5 |
2 files changed, 9 insertions, 52 deletions
diff --git a/Userland/Libraries/LibWeb/DOMTreeJSONModel.cpp b/Userland/Libraries/LibWeb/DOMTreeJSONModel.cpp index efd0fea541..2ec8991518 100644 --- a/Userland/Libraries/LibWeb/DOMTreeJSONModel.cpp +++ b/Userland/Libraries/LibWeb/DOMTreeJSONModel.cpp @@ -27,17 +27,16 @@ DOMTreeJSONModel::~DOMTreeJSONModel() GUI::ModelIndex DOMTreeJSONModel::index(int row, int column, const GUI::ModelIndex& parent) const { if (!parent.is_valid()) { - return create_index(row, column, (void*)get_internal_id(m_dom_tree)); + return create_index(row, column, &m_dom_tree); } - auto const& parent_node = find_node(parent); + auto const& parent_node = *static_cast<JsonObject const*>(parent.internal_data()); auto const* children = get_children(parent_node); if (!children) - return create_index(row, column, (void*)get_internal_id(m_dom_tree)); + return create_index(row, column, &m_dom_tree); auto const& child_node = children->at(row).as_object(); - auto child_internal_id = (void*)get_internal_id(child_node); - return create_index(row, column, child_internal_id); + return create_index(row, column, &child_node); } GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) const @@ -47,7 +46,7 @@ GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) con if (!index.is_valid()) return {}; - auto const& node = find_node(index); + auto const& node = *static_cast<JsonObject const*>(index.internal_data()); auto node_internal_id = get_internal_id(node); auto const* parent_node = find_parent_of_child_with_internal_id(node_internal_id); @@ -57,7 +56,7 @@ GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) con // If the parent is the root document, we know it has index 0, 0 auto parent_node_internal_id = get_internal_id(*parent_node); if (parent_node_internal_id == get_internal_id(m_dom_tree)) { - return create_index(0, 0, (void*)parent_node_internal_id); + return create_index(0, 0, parent_node); } // Otherwise, we need to find the grandparent, to find the index of parent within that @@ -71,7 +70,7 @@ GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) con for (size_t grandparent_child_index = 0; grandparent_child_index < grandparent_children->size(); ++grandparent_child_index) { auto const& child = grandparent_children->at(grandparent_child_index).as_object(); if (get_internal_id(child) == parent_node_internal_id) - return create_index(grandparent_child_index, 0, (void*)(parent_node_internal_id)); + return create_index(grandparent_child_index, 0, parent_node); } return {}; @@ -82,7 +81,7 @@ int DOMTreeJSONModel::row_count(const GUI::ModelIndex& index) const if (!index.is_valid()) return 1; - auto const& node = find_node(index); + auto const& node = *static_cast<JsonObject const*>(index.internal_data()); auto const* children = get_children(node); return children ? children->size() : 0; } @@ -115,7 +114,7 @@ static String with_whitespace_collapsed(const StringView& string) GUI::Variant DOMTreeJSONModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const { - auto const& node = find_node(index); + auto const& node = *static_cast<JsonObject const*>(index.internal_data()); auto node_name = node.get("name").as_string(); auto type = node.get("type").as_string_or("unknown"); @@ -183,32 +182,6 @@ JsonObject const* DOMTreeJSONModel::find_parent_of_child_with_internal_id(JsonOb return nullptr; } -JsonObject const* DOMTreeJSONModel::find_child_with_internal_id(size_t internal_id) const -{ - return find_child_with_internal_id(m_dom_tree, internal_id); -} - -JsonObject const* DOMTreeJSONModel::find_child_with_internal_id(JsonObject const& node, size_t internal_id) const -{ - auto node_internal_id = get_internal_id(node); - if (node_internal_id == internal_id) { - return &node; - } - - auto const* children = get_children(node); - if (!children) - return nullptr; - - for (size_t i = 0; i < children->size(); ++i) { - auto const& child = children->at(i).as_object(); - - if (auto const* maybe_node = find_child_with_internal_id(child, internal_id); maybe_node) - return maybe_node; - } - - return nullptr; -} - size_t DOMTreeJSONModel::get_internal_id(JsonObject const& o) { return o.get("internal_id").as_u32(); @@ -221,15 +194,4 @@ JsonArray const* DOMTreeJSONModel::get_children(JsonObject const& o) return nullptr; } -JsonObject const& DOMTreeJSONModel::find_node(GUI::ModelIndex const& index) const -{ - auto internal_id = (size_t)(index.internal_data()); - - if (auto const* maybe_node = find_child_with_internal_id(internal_id); maybe_node) - return *maybe_node; - - dbgln("Failed to find node with internal_id={}", internal_id); - VERIFY_NOT_REACHED(); -} - } diff --git a/Userland/Libraries/LibWeb/DOMTreeJSONModel.h b/Userland/Libraries/LibWeb/DOMTreeJSONModel.h index d81896a9d1..e8a65800a2 100644 --- a/Userland/Libraries/LibWeb/DOMTreeJSONModel.h +++ b/Userland/Libraries/LibWeb/DOMTreeJSONModel.h @@ -39,11 +39,6 @@ private: JsonObject const* find_parent_of_child_with_internal_id(size_t) const; JsonObject const* find_parent_of_child_with_internal_id(JsonObject const&, size_t) const; - JsonObject const* find_child_with_internal_id(size_t) const; - JsonObject const* find_child_with_internal_id(JsonObject const&, size_t) const; - - JsonObject const& find_node(GUI::ModelIndex const&) const; - static size_t get_internal_id(const JsonObject& o); static JsonArray const* get_children(const JsonObject& o); |