DOMTreeModel.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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)
  16. {
  17. auto json_or_error = JsonValue::from_string(dom_tree);
  18. if (!json_or_error.has_value())
  19. VERIFY_NOT_REACHED();
  20. return adopt_ref(*new DOMTreeModel(json_or_error.value().as_object()));
  21. }
  22. virtual ~DOMTreeModel() override;
  23. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
  24. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
  25. virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
  26. virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
  27. virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
  28. GUI::ModelIndex index_for_node(i32 node_id) const;
  29. private:
  30. explicit DOMTreeModel(JsonObject);
  31. ALWAYS_INLINE JsonObject const* get_parent(const JsonObject& o) const
  32. {
  33. auto parent_node = m_dom_node_to_parent_map.get(&o);
  34. VERIFY(parent_node.has_value());
  35. return *parent_node;
  36. }
  37. ALWAYS_INLINE static JsonArray const* get_children(const JsonObject& o)
  38. {
  39. if (auto const* maybe_children = o.get_ptr("children"); maybe_children)
  40. return &maybe_children->as_array();
  41. return nullptr;
  42. }
  43. void map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* child);
  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. }