DOMTreeModel.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 WebView {
  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. static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree)
  22. {
  23. auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
  24. return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), nullptr));
  25. }
  26. virtual ~DOMTreeModel() override;
  27. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
  28. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
  29. virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
  30. virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
  31. virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
  32. GUI::ModelIndex index_for_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element) const;
  33. private:
  34. DOMTreeModel(JsonObject, GUI::TreeView*);
  35. ALWAYS_INLINE JsonObject const* get_parent(JsonObject const& o) const
  36. {
  37. auto parent_node = m_dom_node_to_parent_map.get(&o);
  38. VERIFY(parent_node.has_value());
  39. return *parent_node;
  40. }
  41. ALWAYS_INLINE static JsonArray const* get_children(JsonObject const& o)
  42. {
  43. if (auto const* maybe_children = o.get_ptr("children"sv); maybe_children)
  44. return &maybe_children->as_array();
  45. return nullptr;
  46. }
  47. void map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* child);
  48. GUI::TreeView* m_tree_view { nullptr };
  49. GUI::Icon m_document_icon;
  50. GUI::Icon m_element_icon;
  51. GUI::Icon m_text_icon;
  52. JsonObject m_dom_tree;
  53. HashMap<JsonObject const*, JsonObject const*> m_dom_node_to_parent_map;
  54. HashMap<i32, JsonObject const*> m_node_id_to_dom_node_map;
  55. };
  56. }