XULElementAccessibles.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "XULElementAccessibles.h"
  6. #include "Accessible-inl.h"
  7. #include "BaseAccessibles.h"
  8. #include "DocAccessible-inl.h"
  9. #include "nsAccUtils.h"
  10. #include "nsCoreUtils.h"
  11. #include "nsTextEquivUtils.h"
  12. #include "Relation.h"
  13. #include "Role.h"
  14. #include "States.h"
  15. #include "TextUpdater.h"
  16. #ifdef A11Y_LOG
  17. #include "Logging.h"
  18. #endif
  19. #include "nsIDOMXULDescriptionElement.h"
  20. #include "nsNameSpaceManager.h"
  21. #include "nsNetUtil.h"
  22. #include "nsString.h"
  23. #include "nsTextBoxFrame.h"
  24. using namespace mozilla::a11y;
  25. ////////////////////////////////////////////////////////////////////////////////
  26. // XULLabelAccessible
  27. ////////////////////////////////////////////////////////////////////////////////
  28. XULLabelAccessible::
  29. XULLabelAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  30. HyperTextAccessibleWrap(aContent, aDoc)
  31. {
  32. mType = eXULLabelType;
  33. // If @value attribute is given then it's rendered instead text content. In
  34. // this case we need to create a text leaf accessible to make @value attribute
  35. // accessible.
  36. // XXX: text interface doesn't let you get the text by words.
  37. nsTextBoxFrame* textBoxFrame = do_QueryFrame(mContent->GetPrimaryFrame());
  38. if (textBoxFrame) {
  39. mValueTextLeaf = new XULLabelTextLeafAccessible(mContent, mDoc);
  40. mDoc->BindToDocument(mValueTextLeaf, nullptr);
  41. nsAutoString text;
  42. textBoxFrame->GetCroppedTitle(text);
  43. mValueTextLeaf->SetText(text);
  44. AppendChild(mValueTextLeaf);
  45. }
  46. }
  47. void
  48. XULLabelAccessible::Shutdown()
  49. {
  50. mValueTextLeaf = nullptr;
  51. HyperTextAccessibleWrap::Shutdown();
  52. }
  53. ENameValueFlag
  54. XULLabelAccessible::NativeName(nsString& aName)
  55. {
  56. // if the value attr doesn't exist, the screen reader must get the accessible text
  57. // from the accessible text interface or from the children
  58. if (mValueTextLeaf)
  59. return mValueTextLeaf->Name(aName);
  60. return Accessible::NativeName(aName);
  61. }
  62. role
  63. XULLabelAccessible::NativeRole()
  64. {
  65. return roles::LABEL;
  66. }
  67. uint64_t
  68. XULLabelAccessible::NativeState()
  69. {
  70. // Labels and description have read only state
  71. // They are not focusable or selectable
  72. return HyperTextAccessibleWrap::NativeState() | states::READONLY;
  73. }
  74. Relation
  75. XULLabelAccessible::RelationByType(RelationType aType)
  76. {
  77. Relation rel = HyperTextAccessibleWrap::RelationByType(aType);
  78. if (aType == RelationType::LABEL_FOR) {
  79. // Caption is the label for groupbox
  80. nsIContent* parent = mContent->GetFlattenedTreeParent();
  81. if (parent && parent->IsXULElement(nsGkAtoms::caption)) {
  82. Accessible* parent = Parent();
  83. if (parent && parent->Role() == roles::GROUPING)
  84. rel.AppendTarget(parent);
  85. }
  86. }
  87. return rel;
  88. }
  89. void
  90. XULLabelAccessible::UpdateLabelValue(const nsString& aValue)
  91. {
  92. #ifdef A11Y_LOG
  93. if (logging::IsEnabled(logging::eText)) {
  94. logging::MsgBegin("TEXT", "text may be changed (xul:label @value update)");
  95. logging::Node("container", mContent);
  96. logging::MsgEntry("old text '%s'",
  97. NS_ConvertUTF16toUTF8(mValueTextLeaf->Text()).get());
  98. logging::MsgEntry("new text: '%s'",
  99. NS_ConvertUTF16toUTF8(aValue).get());
  100. logging::MsgEnd();
  101. }
  102. #endif
  103. TextUpdater::Run(mDoc, mValueTextLeaf, aValue);
  104. }
  105. ////////////////////////////////////////////////////////////////////////////////
  106. // XULLabelTextLeafAccessible
  107. ////////////////////////////////////////////////////////////////////////////////
  108. role
  109. XULLabelTextLeafAccessible::NativeRole()
  110. {
  111. return roles::TEXT_LEAF;
  112. }
  113. uint64_t
  114. XULLabelTextLeafAccessible::NativeState()
  115. {
  116. return TextLeafAccessibleWrap::NativeState() | states::READONLY;
  117. }
  118. ////////////////////////////////////////////////////////////////////////////////
  119. // XULTooltipAccessible
  120. ////////////////////////////////////////////////////////////////////////////////
  121. XULTooltipAccessible::
  122. XULTooltipAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  123. LeafAccessible(aContent, aDoc)
  124. {
  125. }
  126. uint64_t
  127. XULTooltipAccessible::NativeState()
  128. {
  129. return LeafAccessible::NativeState() | states::READONLY;
  130. }
  131. role
  132. XULTooltipAccessible::NativeRole()
  133. {
  134. return roles::TOOLTIP;
  135. }
  136. ////////////////////////////////////////////////////////////////////////////////
  137. // XULLinkAccessible
  138. ////////////////////////////////////////////////////////////////////////////////
  139. XULLinkAccessible::
  140. XULLinkAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  141. XULLabelAccessible(aContent, aDoc)
  142. {
  143. }
  144. XULLinkAccessible::~XULLinkAccessible()
  145. {
  146. }
  147. ////////////////////////////////////////////////////////////////////////////////
  148. // XULLinkAccessible: Accessible
  149. void
  150. XULLinkAccessible::Value(nsString& aValue)
  151. {
  152. aValue.Truncate();
  153. mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::href, aValue);
  154. }
  155. ENameValueFlag
  156. XULLinkAccessible::NativeName(nsString& aName)
  157. {
  158. mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::value, aName);
  159. if (!aName.IsEmpty())
  160. return eNameOK;
  161. nsTextEquivUtils::GetNameFromSubtree(this, aName);
  162. return aName.IsEmpty() ? eNameOK : eNameFromSubtree;
  163. }
  164. role
  165. XULLinkAccessible::NativeRole()
  166. {
  167. return roles::LINK;
  168. }
  169. uint64_t
  170. XULLinkAccessible::NativeLinkState() const
  171. {
  172. return states::LINKED;
  173. }
  174. uint8_t
  175. XULLinkAccessible::ActionCount()
  176. {
  177. return 1;
  178. }
  179. void
  180. XULLinkAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName)
  181. {
  182. aName.Truncate();
  183. if (aIndex == eAction_Jump)
  184. aName.AssignLiteral("jump");
  185. }
  186. bool
  187. XULLinkAccessible::DoAction(uint8_t aIndex)
  188. {
  189. if (aIndex != eAction_Jump)
  190. return false;
  191. DoCommand();
  192. return true;
  193. }
  194. ////////////////////////////////////////////////////////////////////////////////
  195. // XULLinkAccessible: HyperLinkAccessible
  196. bool
  197. XULLinkAccessible::IsLink()
  198. {
  199. // Expose HyperLinkAccessible unconditionally.
  200. return true;
  201. }
  202. uint32_t
  203. XULLinkAccessible::StartOffset()
  204. {
  205. // If XUL link accessible is not contained by hypertext accessible then
  206. // start offset matches index in parent because the parent doesn't contains
  207. // a text.
  208. // XXX: accessible parent of XUL link accessible should be a hypertext
  209. // accessible.
  210. if (Accessible::IsLink())
  211. return Accessible::StartOffset();
  212. return IndexInParent();
  213. }
  214. uint32_t
  215. XULLinkAccessible::EndOffset()
  216. {
  217. if (Accessible::IsLink())
  218. return Accessible::EndOffset();
  219. return IndexInParent() + 1;
  220. }
  221. already_AddRefed<nsIURI>
  222. XULLinkAccessible::AnchorURIAt(uint32_t aAnchorIndex)
  223. {
  224. if (aAnchorIndex != 0)
  225. return nullptr;
  226. nsAutoString href;
  227. mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::href, href);
  228. nsCOMPtr<nsIURI> baseURI = mContent->GetBaseURI();
  229. nsIDocument* document = mContent->OwnerDoc();
  230. nsCOMPtr<nsIURI> anchorURI;
  231. NS_NewURI(getter_AddRefs(anchorURI), href,
  232. document->GetDocumentCharacterSet().get(),
  233. baseURI);
  234. return anchorURI.forget();
  235. }