HTMLLinkAccessible.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "HTMLLinkAccessible.h"
  6. #include "nsCoreUtils.h"
  7. #include "DocAccessible.h"
  8. #include "Role.h"
  9. #include "States.h"
  10. #include "nsContentUtils.h"
  11. #include "mozilla/EventStates.h"
  12. #include "mozilla/dom/Element.h"
  13. using namespace mozilla;
  14. using namespace mozilla::a11y;
  15. ////////////////////////////////////////////////////////////////////////////////
  16. // HTMLLinkAccessible
  17. ////////////////////////////////////////////////////////////////////////////////
  18. HTMLLinkAccessible::
  19. HTMLLinkAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  20. HyperTextAccessibleWrap(aContent, aDoc)
  21. {
  22. }
  23. NS_IMPL_ISUPPORTS_INHERITED0(HTMLLinkAccessible, HyperTextAccessible)
  24. ////////////////////////////////////////////////////////////////////////////////
  25. // nsIAccessible
  26. role
  27. HTMLLinkAccessible::NativeRole()
  28. {
  29. return roles::LINK;
  30. }
  31. uint64_t
  32. HTMLLinkAccessible::NativeState()
  33. {
  34. return HyperTextAccessibleWrap::NativeState() & ~states::READONLY;
  35. }
  36. uint64_t
  37. HTMLLinkAccessible::NativeLinkState() const
  38. {
  39. EventStates eventState = mContent->AsElement()->State();
  40. if (eventState.HasState(NS_EVENT_STATE_UNVISITED))
  41. return states::LINKED;
  42. if (eventState.HasState(NS_EVENT_STATE_VISITED))
  43. return states::LINKED | states::TRAVERSED;
  44. // This is a either named anchor (a link with also a name attribute) or
  45. // it doesn't have any attributes. Check if 'click' event handler is
  46. // registered, otherwise bail out.
  47. return nsCoreUtils::HasClickListener(mContent) ? states::LINKED : 0;
  48. }
  49. uint64_t
  50. HTMLLinkAccessible::NativeInteractiveState() const
  51. {
  52. uint64_t state = HyperTextAccessibleWrap::NativeInteractiveState();
  53. // This is how we indicate it is a named anchor. In other words, this anchor
  54. // can be selected as a location :) There is no other better state to use to
  55. // indicate this.
  56. if (mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::name))
  57. state |= states::SELECTABLE;
  58. return state;
  59. }
  60. void
  61. HTMLLinkAccessible::Value(nsString& aValue)
  62. {
  63. aValue.Truncate();
  64. HyperTextAccessible::Value(aValue);
  65. if (aValue.IsEmpty())
  66. nsContentUtils::GetLinkLocation(mContent->AsElement(), aValue);
  67. }
  68. uint8_t
  69. HTMLLinkAccessible::ActionCount()
  70. {
  71. return IsLinked() ? 1 : HyperTextAccessible::ActionCount();
  72. }
  73. void
  74. HTMLLinkAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName)
  75. {
  76. aName.Truncate();
  77. if (!IsLinked()) {
  78. HyperTextAccessible::ActionNameAt(aIndex, aName);
  79. return;
  80. }
  81. // Action 0 (default action): Jump to link
  82. if (aIndex == eAction_Jump)
  83. aName.AssignLiteral("jump");
  84. }
  85. bool
  86. HTMLLinkAccessible::DoAction(uint8_t aIndex)
  87. {
  88. if (!IsLinked())
  89. return HyperTextAccessible::DoAction(aIndex);
  90. // Action 0 (default action): Jump to link
  91. if (aIndex != eAction_Jump)
  92. return false;
  93. DoCommand();
  94. return true;
  95. }
  96. ////////////////////////////////////////////////////////////////////////////////
  97. // HyperLinkAccessible
  98. bool
  99. HTMLLinkAccessible::IsLink()
  100. {
  101. // Expose HyperLinkAccessible unconditionally.
  102. return true;
  103. }
  104. already_AddRefed<nsIURI>
  105. HTMLLinkAccessible::AnchorURIAt(uint32_t aAnchorIndex)
  106. {
  107. return aAnchorIndex == 0 ? mContent->GetHrefURI() : nullptr;
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////
  110. // Protected members
  111. bool
  112. HTMLLinkAccessible::IsLinked() const
  113. {
  114. EventStates state = mContent->AsElement()->State();
  115. return state.HasAtLeastOneOfStates(NS_EVENT_STATE_VISITED |
  116. NS_EVENT_STATE_UNVISITED);
  117. }