DOMTreeJSONModel.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 DOMTreeJSONModel final : public GUI::Model {
  14. public:
  15. static NonnullRefPtr<DOMTreeJSONModel> 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 DOMTreeJSONModel(json_or_error.value().as_object()));
  21. }
  22. virtual ~DOMTreeJSONModel() 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. private:
  29. explicit DOMTreeJSONModel(JsonObject);
  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::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. };
  49. }