Serenity Operating System
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
8#pragma once
9
10#include <AK/HashMap.h>
11#include <AK/JsonObject.h>
12#include <LibGUI/Model.h>
13#include <LibWeb/CSS/Selector.h>
14#include <LibWeb/Forward.h>
15
16namespace WebView {
17
18class DOMTreeModel final : public GUI::Model {
19public:
20 static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree, GUI::TreeView& tree_view)
21 {
22 auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
23 return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), &tree_view));
24 }
25
26 static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree)
27 {
28 auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
29 return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), nullptr));
30 }
31
32 virtual ~DOMTreeModel() override;
33
34 virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
35 virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
36 virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
37 virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
38 virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
39
40 GUI::ModelIndex index_for_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element) const;
41
42private:
43 DOMTreeModel(JsonObject, GUI::TreeView*);
44
45 ALWAYS_INLINE JsonObject const* get_parent(JsonObject const& o) const
46 {
47 auto parent_node = m_dom_node_to_parent_map.get(&o);
48 VERIFY(parent_node.has_value());
49 return *parent_node;
50 }
51
52 ALWAYS_INLINE static Optional<JsonArray const&> const get_children(JsonObject const& o)
53 {
54 return o.get_array("children"sv);
55 }
56
57 void map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* child);
58
59 GUI::TreeView* m_tree_view { nullptr };
60 GUI::Icon m_document_icon;
61 GUI::Icon m_element_icon;
62 GUI::Icon m_text_icon;
63 JsonObject m_dom_tree;
64 HashMap<JsonObject const*, JsonObject const*> m_dom_node_to_parent_map;
65 HashMap<i32, JsonObject const*> m_node_id_to_dom_node_map;
66};
67
68}