1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:47:35 +00:00

LibWebView: Create a TreeModel helper class for DOM/accessibility models

We currently have DOMTreeModel and AccessibilityTreeModel in LibWebView.
These depend on GUI::Model and GUI::ModelIndex, which is the only reason
that the non-Serenity Ladybird chromes require LibGUI. Further, these
classes are very nearly idenitical.

This creates a TreeModel class to provide the base functionality for all
tree-based model types used by all Ladybird chromes. It contains code
common to the DOM / accessibility tree models, and handles the slight
differences between the two (namely, just the formatting of each node's
text for display).

The Qt and Serenity chromes can create thin wrappers around this class
to adapt its interface to their chrome-specific model classes (i.e.
QAbstractItemModel and GUI::Model).
This commit is contained in:
Timothy Flynn 2023-11-04 11:48:23 -04:00 committed by Andreas Kling
parent 2bec281ddc
commit 3999c74237
4 changed files with 280 additions and 0 deletions

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2020, Adam Hodgen <ant1441@gmail.com>
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/JsonValue.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <LibWeb/CSS/Selector.h>
#include <LibWebView/ModelIndex.h>
namespace WebView {
class TreeModel {
public:
enum class Type {
AccessibilityTree,
DOMTree,
};
TreeModel(Type, JsonValue tree);
~TreeModel();
int row_count(ModelIndex const& parent) const;
int column_count(ModelIndex const& parent) const;
ModelIndex index(int row, int column, ModelIndex const& parent) const;
ModelIndex parent(ModelIndex const& index) const;
String text_for_display(ModelIndex const& index) const;
ModelIndex index_for_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> const& pseudo_element) const;
private:
ALWAYS_INLINE JsonObject const* get_parent(JsonObject const& node) const
{
auto parent_node = m_node_to_parent_map.get(&node);
VERIFY(parent_node.has_value());
return *parent_node;
}
void prepare_node_maps(JsonObject const* node, JsonObject const* parent_node);
Type m_type;
JsonValue m_tree;
HashMap<JsonObject const*, JsonObject const*> m_node_to_parent_map;
HashMap<i32, JsonObject const*> m_node_id_to_node_map;
};
}