ax_node_position.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2016 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_node_position.h"
  5. #include "build/build_config.h"
  6. #include "ui/accessibility/ax_enums.mojom.h"
  7. #include "ui/base/buildflags.h"
  8. namespace ui {
  9. // On some platforms, most objects are represented in the text of their parents
  10. // with a special "embedded object character" and not with their actual text
  11. // contents. Also on the same platforms, if a node has only ignored descendants,
  12. // i.e., it appears to be empty to assistive software, we need to treat it as a
  13. // character and a word boundary.
  14. AXEmbeddedObjectBehavior g_ax_embedded_object_behavior =
  15. #if BUILDFLAG(IS_WIN) || BUILDFLAG(USE_ATK)
  16. AXEmbeddedObjectBehavior::kExposeCharacter;
  17. #else
  18. AXEmbeddedObjectBehavior::kSuppressCharacter;
  19. #endif // BUILDFLAG(IS_WIN) || BUILDFLAG(USE_ATK)
  20. ScopedAXEmbeddedObjectBehaviorSetter::ScopedAXEmbeddedObjectBehaviorSetter(
  21. AXEmbeddedObjectBehavior behavior) {
  22. prev_behavior_ = g_ax_embedded_object_behavior;
  23. g_ax_embedded_object_behavior = behavior;
  24. }
  25. ScopedAXEmbeddedObjectBehaviorSetter::~ScopedAXEmbeddedObjectBehaviorSetter() {
  26. g_ax_embedded_object_behavior = prev_behavior_;
  27. }
  28. std::string ToString(const AXPositionKind kind) {
  29. static constexpr auto kKindToString =
  30. base::MakeFixedFlatMap<AXPositionKind, const char*>(
  31. {{AXPositionKind::NULL_POSITION, "NullPosition"},
  32. {AXPositionKind::TREE_POSITION, "TreePosition"},
  33. {AXPositionKind::TEXT_POSITION, "TextPosition"}});
  34. const auto* iter = kKindToString.find(kind);
  35. if (iter == std::end(kKindToString))
  36. return std::string();
  37. return iter->second;
  38. }
  39. // static
  40. AXNodePosition::AXPositionInstance AXNodePosition::CreatePosition(
  41. const AXNode& node,
  42. int child_index_or_text_offset,
  43. ax::mojom::TextAffinity affinity) {
  44. if (!node.tree())
  45. return CreateNullPosition();
  46. AXTreeID tree_id = node.tree()->GetAXTreeID();
  47. if (node.IsLeaf()) {
  48. return CreateTextPosition(tree_id, node.id(), child_index_or_text_offset,
  49. affinity);
  50. }
  51. return CreateTreePosition(tree_id, node.id(), child_index_or_text_offset);
  52. }
  53. AXNodePosition::AXNodePosition() = default;
  54. AXNodePosition::~AXNodePosition() = default;
  55. AXNodePosition::AXNodePosition(const AXNodePosition& other)
  56. : AXPosition<AXNodePosition, AXNode>(other) {}
  57. AXNodePosition::AXPositionInstance AXNodePosition::Clone() const {
  58. return AXPositionInstance(new AXNodePosition(*this));
  59. }
  60. } // namespace ui