Line data Source code
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 :
6 : #ifndef nsAccUtils_h_
7 : #define nsAccUtils_h_
8 :
9 : #include "mozilla/a11y/Accessible.h"
10 :
11 : #include "nsAccessibilityService.h"
12 : #include "nsCoreUtils.h"
13 :
14 : #include "nsIDocShell.h"
15 : #include "nsPoint.h"
16 :
17 : namespace mozilla {
18 :
19 : namespace dom {
20 : class Element;
21 : }
22 :
23 : namespace a11y {
24 :
25 : class HyperTextAccessible;
26 : class DocAccessible;
27 :
28 : class nsAccUtils
29 : {
30 : public:
31 : /**
32 : * Returns value of attribute from the given attributes container.
33 : *
34 : * @param aAttributes - attributes container
35 : * @param aAttrName - the name of requested attribute
36 : * @param aAttrValue - value of attribute
37 : */
38 : static void GetAccAttr(nsIPersistentProperties *aAttributes,
39 : nsIAtom *aAttrName,
40 : nsAString& aAttrValue);
41 :
42 : /**
43 : * Set value of attribute for the given attributes container.
44 : *
45 : * @param aAttributes - attributes container
46 : * @param aAttrName - the name of requested attribute
47 : * @param aAttrValue - new value of attribute
48 : */
49 : static void SetAccAttr(nsIPersistentProperties *aAttributes,
50 : nsIAtom *aAttrName,
51 : const nsAString& aAttrValue);
52 :
53 : static void SetAccAttr(nsIPersistentProperties *aAttributes,
54 : nsIAtom* aAttrName,
55 : nsIAtom* aAttrValue);
56 :
57 : /**
58 : * Set group attributes ('level', 'setsize', 'posinset').
59 : */
60 : static void SetAccGroupAttrs(nsIPersistentProperties *aAttributes,
61 : int32_t aLevel, int32_t aSetSize,
62 : int32_t aPosInSet);
63 :
64 : /**
65 : * Get default value of the level for the given accessible.
66 : */
67 : static int32_t GetDefaultLevel(Accessible* aAcc);
68 :
69 : /**
70 : * Return ARIA level value or the default one if ARIA is missed for the
71 : * given accessible.
72 : */
73 : static int32_t GetARIAOrDefaultLevel(Accessible* aAccessible);
74 :
75 : /**
76 : * Compute group level for nsIDOMXULContainerItemElement node.
77 : */
78 : static int32_t GetLevelForXULContainerItem(nsIContent *aContent);
79 :
80 : /**
81 : * Set container-foo live region attributes for the given node.
82 : *
83 : * @param aAttributes where to store the attributes
84 : * @param aStartContent node to start from
85 : * @param aTopContent node to end at
86 : */
87 : static void SetLiveContainerAttributes(nsIPersistentProperties *aAttributes,
88 : nsIContent* aStartContent,
89 : mozilla::dom::Element* aTopEl);
90 :
91 : /**
92 : * Any ARIA property of type boolean or NMTOKEN is undefined if the ARIA
93 : * property is not present, or is "" or "undefined". Do not call
94 : * this method for properties of type string, decimal, IDREF or IDREFS.
95 : *
96 : * Return true if the ARIA property is defined, otherwise false
97 : */
98 : static bool HasDefinedARIAToken(nsIContent *aContent, nsIAtom *aAtom);
99 :
100 : /**
101 : * Return atomic value of ARIA attribute of boolean or NMTOKEN type.
102 : */
103 : static nsIAtom* GetARIAToken(mozilla::dom::Element* aElement, nsIAtom* aAttr);
104 :
105 : /**
106 : * Return document accessible for the given DOM node.
107 : */
108 0 : static DocAccessible* GetDocAccessibleFor(nsINode* aNode)
109 : {
110 0 : nsIPresShell *presShell = nsCoreUtils::GetPresShellFor(aNode);
111 0 : return GetAccService()->GetDocAccessible(presShell);
112 : }
113 :
114 : /**
115 : * Return document accessible for the given docshell.
116 : */
117 0 : static DocAccessible* GetDocAccessibleFor(nsIDocShellTreeItem* aContainer)
118 : {
119 0 : nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aContainer));
120 0 : return GetAccService()->GetDocAccessible(docShell->GetPresShell());
121 : }
122 :
123 : /**
124 : * Return single or multi selectable container for the given item.
125 : *
126 : * @param aAccessible [in] the item accessible
127 : * @param aState [in] the state of the item accessible
128 : */
129 : static Accessible* GetSelectableContainer(Accessible* aAccessible,
130 : uint64_t aState);
131 :
132 : /**
133 : * Return a text container accessible for the given node.
134 : */
135 : static HyperTextAccessible* GetTextContainer(nsINode* aNode);
136 :
137 : static Accessible* TableFor(Accessible* aRow);
138 :
139 : /**
140 : * Return true if the DOM node of given accessible has aria-selected="true"
141 : * attribute.
142 : */
143 : static bool IsARIASelected(Accessible* aAccessible);
144 :
145 : /**
146 : * Converts the given coordinates to coordinates relative screen.
147 : *
148 : * @param aX [in] the given x coord
149 : * @param aY [in] the given y coord
150 : * @param aCoordinateType [in] specifies coordinates origin (refer to
151 : * nsIAccessibleCoordinateType)
152 : * @param aAccessible [in] the accessible if coordinates are given
153 : * relative it.
154 : * @return converted coordinates
155 : */
156 : static nsIntPoint ConvertToScreenCoords(int32_t aX, int32_t aY,
157 : uint32_t aCoordinateType,
158 : Accessible* aAccessible);
159 :
160 : /**
161 : * Converts the given coordinates relative screen to another coordinate
162 : * system.
163 : *
164 : * @param aX [in, out] the given x coord
165 : * @param aY [in, out] the given y coord
166 : * @param aCoordinateType [in] specifies coordinates origin (refer to
167 : * nsIAccessibleCoordinateType)
168 : * @param aAccessible [in] the accessible if coordinates are given
169 : * relative it
170 : */
171 : static void ConvertScreenCoordsTo(int32_t* aX, int32_t* aY,
172 : uint32_t aCoordinateType,
173 : Accessible* aAccessible);
174 :
175 : /**
176 : * Returns coordinates relative screen for the parent of the given accessible.
177 : *
178 : * @param [in] aAccessible the accessible
179 : */
180 : static nsIntPoint GetScreenCoordsForParent(Accessible* aAccessible);
181 :
182 : /**
183 : * Get the 'live' or 'container-live' object attribute value from the given
184 : * ELiveAttrRule constant.
185 : *
186 : * @param aRule [in] rule constant (see ELiveAttrRule in nsAccMap.h)
187 : * @param aValue [out] object attribute value
188 : *
189 : * @return true if object attribute should be exposed
190 : */
191 : static bool GetLiveAttrValue(uint32_t aRule, nsAString& aValue);
192 :
193 : #ifdef DEBUG
194 : /**
195 : * Detect whether the given accessible object implements nsIAccessibleText,
196 : * when it is text or has text child node.
197 : */
198 : static bool IsTextInterfaceSupportCorrect(Accessible* aAccessible);
199 : #endif
200 :
201 : /**
202 : * Return text length of the given accessible, return 0 on failure.
203 : */
204 : static uint32_t TextLength(Accessible* aAccessible);
205 :
206 : /**
207 : * Transform nsIAccessibleStates constants to internal state constant.
208 : */
209 0 : static inline uint64_t To64State(uint32_t aState1, uint32_t aState2)
210 : {
211 0 : return static_cast<uint64_t>(aState1) +
212 0 : (static_cast<uint64_t>(aState2) << 31);
213 : }
214 :
215 : /**
216 : * Transform internal state constant to nsIAccessibleStates constants.
217 : */
218 0 : static inline void To32States(uint64_t aState64,
219 : uint32_t* aState1, uint32_t* aState2)
220 : {
221 0 : *aState1 = aState64 & 0x7fffffff;
222 0 : if (aState2)
223 0 : *aState2 = static_cast<uint32_t>(aState64 >> 31);
224 0 : }
225 :
226 0 : static uint32_t To32States(uint64_t aState, bool* aIsExtra)
227 : {
228 0 : uint32_t extraState = aState >> 31;
229 0 : *aIsExtra = !!extraState;
230 0 : return aState | extraState;
231 : }
232 :
233 : /**
234 : * Return true if the given accessible can't have children. Used when exposing
235 : * to platform accessibility APIs, should the children be pruned off?
236 : */
237 : static bool MustPrune(Accessible* aAccessible);
238 : };
239 :
240 : } // namespace a11y
241 : } // namespace mozilla
242 :
243 : #endif
|