DOMTreeModel.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/CSS/Selector.h>
  12. #include <LibWeb/Forward.h>
  13. namespace Web {
  14. class DOMTreeModel final : public GUI::Model {
  15. public:
  16. static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree, GUI::TreeView& tree_view)
  17. {
  18. auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
  19. return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), tree_view));
  20. }
  21. virtual ~DOMTreeModel() override;
  22. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
  23. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
  24. virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
  25. virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
  26. virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
  27. GUI::ModelIndex index_for_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element) const;
  28. private:
  29. DOMTreeModel(JsonObject, GUI::TreeView&);
  30. ALWAYS_INLINE JsonObject const* get_parent(const JsonObject& o) const
  31. {
  32. auto parent_node = m_dom_node_to_parent_map.get(&o);
  33. VERIFY(parent_node.has_value());
  34. return *parent_node;
  35. }
  36. ALWAYS_INLINE static JsonArray const* get_children(const JsonObject& o)
  37. {
  38. if (auto const* maybe_children = o.get_ptr("children"); maybe_children)
  39. return &maybe_children->as_array();
  40. return nullptr;
  41. }
  42. void map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* child);
  43. GUI::TreeView& m_tree_view;
  44. GUI::Icon m_document_icon;
  45. GUI::Icon m_element_icon;
  46. GUI::Icon m_text_icon;
  47. JsonObject m_dom_tree;
  48. HashMap<JsonObject const*, JsonObject const*> m_dom_node_to_parent_map;
  49. HashMap<i32, JsonObject const*> m_node_id_to_dom_node_map;
  50. };
  51. }