ax_tree_data.cc 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/accessibility/ax_tree_data.h"
  5. #include <set>
  6. #include "base/strings/string_number_conversions.h"
  7. #include "base/strings/string_util.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "ui/accessibility/ax_enum_util.h"
  10. #include "ui/accessibility/ax_enums.mojom.h"
  11. namespace ui {
  12. AXTreeData::AXTreeData()
  13. : sel_anchor_affinity(ax::mojom::TextAffinity::kDownstream),
  14. sel_focus_affinity(ax::mojom::TextAffinity::kDownstream) {}
  15. AXTreeData::AXTreeData(const AXTreeData& other) = default;
  16. AXTreeData::~AXTreeData() = default;
  17. // Note that this includes an initial space character if nonempty, but
  18. // that works fine because this is normally printed by AXTree::ToString.
  19. std::string AXTreeData::ToString() const {
  20. std::string result;
  21. if (tree_id != AXTreeIDUnknown())
  22. result += " tree_id=" + tree_id.ToString().substr(0, 8);
  23. if (parent_tree_id != AXTreeIDUnknown())
  24. result += " parent_tree_id=" + parent_tree_id.ToString().substr(0, 8);
  25. if (focused_tree_id != AXTreeIDUnknown())
  26. result += " focused_tree_id=" + focused_tree_id.ToString().substr(0, 8);
  27. if (!doctype.empty())
  28. result += " doctype=" + doctype;
  29. if (loaded)
  30. result += " loaded=true";
  31. if (loading_progress != 0.0f)
  32. result += " loading_progress=" + base::NumberToString(loading_progress);
  33. if (!mimetype.empty())
  34. result += " mimetype=" + mimetype;
  35. if (!url.empty())
  36. result += " url=" + url;
  37. if (!title.empty())
  38. result += " title=" + title;
  39. if (focus_id != kInvalidAXNodeID)
  40. result += " focus_id=" + base::NumberToString(focus_id);
  41. if (sel_anchor_object_id != kInvalidAXNodeID) {
  42. result +=
  43. (sel_is_backward ? " sel_is_backward=true" : " sel_is_backward=false");
  44. result +=
  45. " sel_anchor_object_id=" + base::NumberToString(sel_anchor_object_id);
  46. result += " sel_anchor_offset=" + base::NumberToString(sel_anchor_offset);
  47. result += " sel_anchor_affinity=";
  48. result += ui::ToString(sel_anchor_affinity);
  49. }
  50. if (sel_focus_object_id != kInvalidAXNodeID) {
  51. result +=
  52. " sel_focus_object_id=" + base::NumberToString(sel_focus_object_id);
  53. result += " sel_focus_offset=" + base::NumberToString(sel_focus_offset);
  54. result += " sel_focus_affinity=";
  55. result += ui::ToString(sel_focus_affinity);
  56. }
  57. if (!metadata.empty()) {
  58. result += "\n<head>\n";
  59. for (const auto& str : metadata)
  60. result += " " + str + "\n";
  61. result += "</head>\n";
  62. }
  63. return result;
  64. }
  65. bool operator==(const AXTreeData& lhs, const AXTreeData& rhs) {
  66. return (lhs.tree_id == rhs.tree_id &&
  67. lhs.parent_tree_id == rhs.parent_tree_id &&
  68. lhs.focused_tree_id == rhs.focused_tree_id &&
  69. lhs.doctype == rhs.doctype && lhs.loaded == rhs.loaded &&
  70. lhs.loading_progress == rhs.loading_progress &&
  71. lhs.mimetype == rhs.mimetype && lhs.title == rhs.title &&
  72. lhs.url == rhs.url && lhs.focus_id == rhs.focus_id &&
  73. lhs.sel_anchor_object_id == rhs.sel_anchor_object_id &&
  74. lhs.sel_anchor_offset == rhs.sel_anchor_offset &&
  75. lhs.sel_anchor_affinity == rhs.sel_anchor_affinity &&
  76. lhs.sel_focus_object_id == rhs.sel_focus_object_id &&
  77. lhs.sel_focus_offset == rhs.sel_focus_offset &&
  78. lhs.sel_focus_affinity == rhs.sel_focus_affinity);
  79. }
  80. bool operator!=(const AXTreeData& lhs, const AXTreeData& rhs) {
  81. return !(lhs == rhs);
  82. }
  83. } // namespace ui