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