AccessibilityTreeModel.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2022, Jonah Shafran <jonahshafran@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWebView/AccessibilityTreeModel.h>
  7. namespace WebView {
  8. AccessibilityTreeModel::AccessibilityTreeModel(JsonObject accessibility_tree, GUI::TreeView* tree_view)
  9. : m_tree_view(tree_view)
  10. , m_accessibility_tree(move(accessibility_tree))
  11. {
  12. map_accessibility_nodes_to_parent(nullptr, &m_accessibility_tree);
  13. }
  14. AccessibilityTreeModel::~AccessibilityTreeModel() = default;
  15. GUI::ModelIndex AccessibilityTreeModel::index(int row, int column, GUI::ModelIndex const& parent) const
  16. {
  17. if (!parent.is_valid()) {
  18. return create_index(row, column, &m_accessibility_tree);
  19. }
  20. auto const& parent_node = *static_cast<JsonObject const*>(parent.internal_data());
  21. auto children = get_children(parent_node);
  22. if (!children.has_value())
  23. return create_index(row, column, &m_accessibility_tree);
  24. auto const& child_node = children->at(row).as_object();
  25. return create_index(row, column, &child_node);
  26. }
  27. GUI::ModelIndex AccessibilityTreeModel::parent_index(GUI::ModelIndex const& index) const
  28. {
  29. if (!index.is_valid())
  30. return {};
  31. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  32. auto const* parent_node = get_parent(node);
  33. if (!parent_node)
  34. return {};
  35. // If the parent is the root document, we know it has index 0, 0
  36. if (parent_node == &m_accessibility_tree) {
  37. return create_index(0, 0, parent_node);
  38. }
  39. // Otherwise, we need to find the grandparent, to find the index of parent within that
  40. auto const* grandparent_node = get_parent(*parent_node);
  41. VERIFY(grandparent_node);
  42. auto grandparent_children = get_children(*grandparent_node);
  43. if (!grandparent_children.has_value())
  44. return {};
  45. for (size_t grandparent_child_index = 0; grandparent_child_index < grandparent_children->size(); ++grandparent_child_index) {
  46. auto const& child = grandparent_children->at(grandparent_child_index).as_object();
  47. if (&child == parent_node)
  48. return create_index(grandparent_child_index, 0, parent_node);
  49. }
  50. return {};
  51. }
  52. int AccessibilityTreeModel::row_count(GUI::ModelIndex const& index) const
  53. {
  54. if (!index.is_valid())
  55. return 1;
  56. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  57. auto children = get_children(node);
  58. return children.has_value() ? children->size() : 0;
  59. }
  60. int AccessibilityTreeModel::column_count(const GUI::ModelIndex&) const
  61. {
  62. return 1;
  63. }
  64. GUI::Variant AccessibilityTreeModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
  65. {
  66. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  67. auto type = node.get_deprecated_string("type"sv).value_or("unknown"sv);
  68. if (role == GUI::ModelRole::Display) {
  69. if (type == "text")
  70. return node.get_deprecated_string("text"sv).value();
  71. auto node_role = node.get_deprecated_string("role"sv).value();
  72. if (type != "element")
  73. return node_role;
  74. auto name = node.get_deprecated_string("name"sv).value_or("");
  75. auto description = node.get_deprecated_string("description"sv).value_or("");
  76. StringBuilder builder;
  77. builder.append(node_role.to_lowercase());
  78. builder.appendff(" name: \"{}\", description: \"{}\"", name, description);
  79. return builder.to_deprecated_string();
  80. }
  81. return {};
  82. }
  83. void AccessibilityTreeModel::map_accessibility_nodes_to_parent(JsonObject const* parent, JsonObject const* node)
  84. {
  85. m_accessibility_node_to_parent_map.set(node, parent);
  86. auto children = get_children(*node);
  87. if (!children.has_value())
  88. return;
  89. children->for_each([&](auto const& child) {
  90. auto const& child_node = child.as_object();
  91. map_accessibility_nodes_to_parent(node, &child_node);
  92. });
  93. }
  94. }