DOMTreeJSONModel.h 2.0 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/JsonObject.h>
  9. #include <LibGUI/Model.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web {
  12. class DOMTreeJSONModel final : public GUI::Model {
  13. public:
  14. static NonnullRefPtr<DOMTreeJSONModel> create(StringView dom_tree)
  15. {
  16. auto json_or_error = JsonValue::from_string(dom_tree);
  17. if (!json_or_error.has_value())
  18. VERIFY_NOT_REACHED();
  19. return adopt_ref(*new DOMTreeJSONModel(json_or_error.value().as_object()));
  20. }
  21. virtual ~DOMTreeJSONModel() 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. virtual void update() override;
  28. private:
  29. explicit DOMTreeJSONModel(JsonObject);
  30. ALWAYS_INLINE JsonObject const* find_parent_of_child_with_internal_id(size_t internal_id) const
  31. {
  32. return find_parent_of_child_with_internal_id(m_dom_tree, internal_id);
  33. }
  34. JsonObject const* find_parent_of_child_with_internal_id(JsonObject const&, size_t) const;
  35. ALWAYS_INLINE static size_t get_internal_id(const JsonObject& o)
  36. {
  37. return o.get("internal_id").as_u32();
  38. }
  39. ALWAYS_INLINE static JsonArray const* get_children(const JsonObject& o)
  40. {
  41. if (auto const* maybe_children = o.get_ptr("children"); maybe_children)
  42. return &maybe_children->as_array();
  43. return nullptr;
  44. }
  45. GUI::Icon m_document_icon;
  46. GUI::Icon m_element_icon;
  47. GUI::Icon m_text_icon;
  48. JsonObject m_dom_tree;
  49. };
  50. }