AccessibilityTreeModel.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 const* children = get_children(parent_node);
  22. if (!children)
  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 const* grandparent_children = get_children(*grandparent_node);
  43. if (!grandparent_children)
  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 const* children = get_children(node);
  58. return children ? 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("type"sv).as_string_or("unknown"sv);
  68. if (role == GUI::ModelRole::Display) {
  69. if (type == "text")
  70. return node.get("text"sv).as_string();
  71. auto node_role = node.get("role"sv).as_string();
  72. if (type != "element")
  73. return node_role;
  74. StringBuilder builder;
  75. builder.append(node_role.to_lowercase());
  76. return builder.to_deprecated_string();
  77. }
  78. return {};
  79. }
  80. void AccessibilityTreeModel::map_accessibility_nodes_to_parent(JsonObject const* parent, JsonObject const* node)
  81. {
  82. m_accessibility_node_to_parent_map.set(node, parent);
  83. auto const* children = get_children(*node);
  84. if (!children)
  85. return;
  86. children->for_each([&](auto const& child) {
  87. auto const& child_node = child.as_object();
  88. map_accessibility_nodes_to_parent(node, &child_node);
  89. });
  90. }
  91. }