DOMTreeModel.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2018-2020, Adam Hodgen <ant1441@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <AK/JsonObject.h>
  10. #include <LibGUI/Model.h>
  11. #include <LibWeb/Forward.h>
  12. namespace Web {
  13. class DOMTreeModel final : public GUI::Model {
  14. public:
  15. static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree, GUI::TreeView& tree_view)
  16. {
  17. auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
  18. return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), tree_view));
  19. }
  20. virtual ~DOMTreeModel() override;
  21. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
  22. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
  23. virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
  24. virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
  25. virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
  26. GUI::ModelIndex index_for_node(i32 node_id) const;
  27. private:
  28. DOMTreeModel(JsonObject, GUI::TreeView&);
  29. ALWAYS_INLINE JsonObject const* get_parent(const JsonObject& o) const
  30. {
  31. auto parent_node = m_dom_node_to_parent_map.get(&o);
  32. VERIFY(parent_node.has_value());
  33. return *parent_node;
  34. }
  35. ALWAYS_INLINE static JsonArray const* get_children(const JsonObject& o)
  36. {
  37. if (auto const* maybe_children = o.get_ptr("children"); maybe_children)
  38. return &maybe_children->as_array();
  39. return nullptr;
  40. }
  41. void map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* child);
  42. GUI::TreeView& m_tree_view;
  43. GUI::Icon m_document_icon;
  44. GUI::Icon m_element_icon;
  45. GUI::Icon m_text_icon;
  46. JsonObject m_dom_tree;
  47. HashMap<JsonObject const*, JsonObject const*> m_dom_node_to_parent_map;
  48. HashMap<i32, JsonObject const*> m_node_id_to_dom_node_map;
  49. };
  50. }