HTMLElementAccessibles.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "HTMLElementAccessibles.h"
  6. #include "DocAccessible.h"
  7. #include "nsAccUtils.h"
  8. #include "nsIPersistentProperties2.h"
  9. #include "nsTextEquivUtils.h"
  10. #include "Relation.h"
  11. #include "Role.h"
  12. #include "States.h"
  13. #include "mozilla/dom/HTMLLabelElement.h"
  14. #include "mozilla/dom/HTMLDetailsElement.h"
  15. #include "mozilla/dom/HTMLSummaryElement.h"
  16. using namespace mozilla::a11y;
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // HTMLHRAccessible
  19. ////////////////////////////////////////////////////////////////////////////////
  20. role
  21. HTMLHRAccessible::NativeRole()
  22. {
  23. return roles::SEPARATOR;
  24. }
  25. ////////////////////////////////////////////////////////////////////////////////
  26. // HTMLBRAccessible
  27. ////////////////////////////////////////////////////////////////////////////////
  28. role
  29. HTMLBRAccessible::NativeRole()
  30. {
  31. return roles::WHITESPACE;
  32. }
  33. uint64_t
  34. HTMLBRAccessible::NativeState()
  35. {
  36. return states::READONLY;
  37. }
  38. ENameValueFlag
  39. HTMLBRAccessible::NativeName(nsString& aName)
  40. {
  41. aName = static_cast<char16_t>('\n'); // Newline char
  42. return eNameOK;
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////
  45. // HTMLLabelAccessible
  46. ////////////////////////////////////////////////////////////////////////////////
  47. NS_IMPL_ISUPPORTS_INHERITED0(HTMLLabelAccessible, HyperTextAccessible)
  48. ENameValueFlag
  49. HTMLLabelAccessible::NativeName(nsString& aName)
  50. {
  51. nsTextEquivUtils::GetNameFromSubtree(this, aName);
  52. return aName.IsEmpty() ? eNameOK : eNameFromSubtree;
  53. }
  54. Relation
  55. HTMLLabelAccessible::RelationByType(RelationType aType)
  56. {
  57. Relation rel = AccessibleWrap::RelationByType(aType);
  58. if (aType == RelationType::LABEL_FOR) {
  59. dom::HTMLLabelElement* label = dom::HTMLLabelElement::FromContent(mContent);
  60. rel.AppendTarget(mDoc, label->GetControl());
  61. }
  62. return rel;
  63. }
  64. uint8_t
  65. HTMLLabelAccessible::ActionCount()
  66. {
  67. return nsCoreUtils::IsLabelWithControl(mContent) ? 1 : 0;
  68. }
  69. void
  70. HTMLLabelAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName)
  71. {
  72. if (aIndex == 0) {
  73. if (nsCoreUtils::IsLabelWithControl(mContent))
  74. aName.AssignLiteral("click");
  75. }
  76. }
  77. bool
  78. HTMLLabelAccessible::DoAction(uint8_t aIndex)
  79. {
  80. if (aIndex != 0)
  81. return false;
  82. DoCommand();
  83. return true;
  84. }
  85. ////////////////////////////////////////////////////////////////////////////////
  86. // nsHTMLOuputAccessible
  87. ////////////////////////////////////////////////////////////////////////////////
  88. NS_IMPL_ISUPPORTS_INHERITED0(HTMLOutputAccessible, HyperTextAccessible)
  89. Relation
  90. HTMLOutputAccessible::RelationByType(RelationType aType)
  91. {
  92. Relation rel = AccessibleWrap::RelationByType(aType);
  93. if (aType == RelationType::CONTROLLED_BY)
  94. rel.AppendIter(new IDRefsIterator(mDoc, mContent, nsGkAtoms::_for));
  95. return rel;
  96. }
  97. ////////////////////////////////////////////////////////////////////////////////
  98. // HTMLSummaryAccessible
  99. ////////////////////////////////////////////////////////////////////////////////
  100. HTMLSummaryAccessible::
  101. HTMLSummaryAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  102. HyperTextAccessibleWrap(aContent, aDoc)
  103. {
  104. mGenericTypes |= eButton;
  105. }
  106. uint8_t
  107. HTMLSummaryAccessible::ActionCount()
  108. {
  109. return 1;
  110. }
  111. void
  112. HTMLSummaryAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName)
  113. {
  114. if (aIndex != eAction_Click) {
  115. return;
  116. }
  117. dom::HTMLSummaryElement* summary = dom::HTMLSummaryElement::FromContent(mContent);
  118. if (!summary) {
  119. return;
  120. }
  121. dom::HTMLDetailsElement* details = summary->GetDetails();
  122. if (!details) {
  123. return;
  124. }
  125. if (details->Open()) {
  126. aName.AssignLiteral("collapse");
  127. } else {
  128. aName.AssignLiteral("expand");
  129. }
  130. }
  131. bool
  132. HTMLSummaryAccessible::DoAction(uint8_t aIndex)
  133. {
  134. if (aIndex != eAction_Click)
  135. return false;
  136. DoCommand();
  137. return true;
  138. }
  139. uint64_t
  140. HTMLSummaryAccessible::NativeState()
  141. {
  142. uint64_t state = HyperTextAccessibleWrap::NativeState();
  143. dom::HTMLSummaryElement* summary = dom::HTMLSummaryElement::FromContent(mContent);
  144. if (!summary) {
  145. return state;
  146. }
  147. dom::HTMLDetailsElement* details = summary->GetDetails();
  148. if (!details) {
  149. return state;
  150. }
  151. if (details->Open()) {
  152. state |= states::EXPANDED;
  153. } else {
  154. state |= states::COLLAPSED;
  155. }
  156. return state;
  157. }
  158. ////////////////////////////////////////////////////////////////////////////////
  159. // HTMLSummaryAccessible: Widgets
  160. bool
  161. HTMLSummaryAccessible::IsWidget() const
  162. {
  163. return true;
  164. }