2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-08-24 16:04:30 +00:00
|
|
|
* Copyright (c) 2018-2020, Adam Hodgen <ant1441@gmail.com>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-11-09 10:31:03 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-08-24 16:04:30 +00:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/JsonObject.h>
|
2020-02-06 19:33:02 +00:00
|
|
|
#include <LibGUI/Model.h>
|
2022-03-04 16:29:05 +00:00
|
|
|
#include <LibWeb/CSS/Selector.h>
|
2020-07-26 17:37:56 +00:00
|
|
|
#include <LibWeb/Forward.h>
|
2019-11-09 10:31:03 +00:00
|
|
|
|
2022-04-30 09:13:33 +00:00
|
|
|
namespace WebView {
|
2020-03-07 09:27:02 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
class DOMTreeModel final : public GUI::Model {
|
2019-11-09 10:31:03 +00:00
|
|
|
public:
|
2021-11-02 18:32:26 +00:00
|
|
|
static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree, GUI::TreeView& tree_view)
|
2019-11-09 10:31:03 +00:00
|
|
|
{
|
2021-11-15 00:46:51 +00:00
|
|
|
auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
|
2022-09-25 10:11:02 +00:00
|
|
|
return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), &tree_view));
|
|
|
|
}
|
|
|
|
|
|
|
|
static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree)
|
|
|
|
{
|
|
|
|
auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
|
|
|
|
return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), nullptr));
|
2019-11-09 10:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~DOMTreeModel() override;
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
|
|
|
|
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
|
2020-08-16 14:00:07 +00:00
|
|
|
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
|
2020-02-02 14:07:41 +00:00
|
|
|
virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
|
|
|
|
virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
|
2019-11-09 10:31:03 +00:00
|
|
|
|
2022-03-04 16:29:05 +00:00
|
|
|
GUI::ModelIndex index_for_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element) const;
|
2021-08-27 16:36:10 +00:00
|
|
|
|
2019-11-09 10:31:03 +00:00
|
|
|
private:
|
2022-09-25 10:11:02 +00:00
|
|
|
DOMTreeModel(JsonObject, GUI::TreeView*);
|
2021-08-24 16:04:30 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE JsonObject const* get_parent(JsonObject const& o) const
|
2021-08-24 16:04:30 +00:00
|
|
|
{
|
|
|
|
auto parent_node = m_dom_node_to_parent_map.get(&o);
|
|
|
|
VERIFY(parent_node.has_value());
|
|
|
|
return *parent_node;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE static JsonArray const* get_children(JsonObject const& o)
|
2021-08-24 16:04:30 +00:00
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
if (auto const* maybe_children = o.get_ptr("children"sv); maybe_children)
|
2021-08-24 16:04:30 +00:00
|
|
|
return &maybe_children->as_array();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-09 10:31:03 +00:00
|
|
|
|
2021-08-24 16:04:30 +00:00
|
|
|
void map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* child);
|
2019-11-09 10:31:03 +00:00
|
|
|
|
2022-09-25 10:11:02 +00:00
|
|
|
GUI::TreeView* m_tree_view { nullptr };
|
2020-03-06 23:02:21 +00:00
|
|
|
GUI::Icon m_document_icon;
|
|
|
|
GUI::Icon m_element_icon;
|
|
|
|
GUI::Icon m_text_icon;
|
2021-08-24 16:04:30 +00:00
|
|
|
JsonObject m_dom_tree;
|
|
|
|
HashMap<JsonObject const*, JsonObject const*> m_dom_node_to_parent_map;
|
2021-08-27 16:36:10 +00:00
|
|
|
HashMap<i32, JsonObject const*> m_node_id_to_dom_node_map;
|
2019-11-09 10:31:03 +00:00
|
|
|
};
|
2020-03-07 09:27:02 +00:00
|
|
|
|
|
|
|
}
|