1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef _nsTextEquivUtils_H_
9 #define _nsTextEquivUtils_H_
11 #include "mozilla/a11y/Accessible.h"
12 #include "mozilla/a11y/Role.h"
18 class LocalAccessible
;
20 } // namespace mozilla
23 * Text equivalent computation rules (see nsTextEquivUtils::gRoleToNameRulesMap)
29 // Walk into subtree only if the currently navigated accessible is not root
30 // accessible (i.e. if the accessible is part of text equivalent computation).
31 eNameFromSubtreeIfReqRule
= 0x01,
33 // Text equivalent computation from subtree is allowed.
34 eNameFromSubtreeRule
= 0x03,
36 // The accessible allows to append its value to text equivalent.
37 // XXX: This is temporary solution. Once we move accessible value of links
38 // and linkable accessibles to MSAA part we can remove this.
39 eNameFromValueRule
= 0x04
43 * The class provides utils methods to compute the accessible name and
46 class nsTextEquivUtils
{
48 typedef mozilla::a11y::LocalAccessible LocalAccessible
;
49 typedef mozilla::a11y::Accessible Accessible
;
52 * Determines if the accessible has a given name rule.
54 * @param aAccessible [in] the given accessible
55 * @param aRule [in] a given name rule
56 * @return true if the accessible has the rule
58 static inline bool HasNameRule(Accessible
* aAccessible
,
59 ETextEquivRule aRule
) {
60 return (GetRoleRule(aAccessible
->Role()) & aRule
) == aRule
;
64 * Calculates the name from accessible subtree if allowed.
66 * @param aAccessible [in] the given accessible
67 * @param aName [out] accessible name
69 static nsresult
GetNameFromSubtree(const LocalAccessible
* aAccessible
,
73 * Calculates text equivalent from the subtree. Similar to GetNameFromSubtree.
74 * However it returns not empty result for things like HTML p.
76 static void GetTextEquivFromSubtree(const Accessible
* aAccessible
,
77 nsString
& aTextEquiv
) {
78 aTextEquiv
.Truncate();
80 AppendFromAccessibleChildren(aAccessible
, &aTextEquiv
);
81 aTextEquiv
.CompressWhitespace();
85 * Calculates text equivalent for the given accessible from its IDRefs
86 * attribute (like aria-labelledby or aria-describedby).
88 * @param aAccessible [in] the accessible text equivalent is computed for
89 * @param aIDRefsAttr [in] IDRefs attribute on DOM node of the accessible
90 * @param aTextEquiv [out] result text equivalent
92 static nsresult
GetTextEquivFromIDRefs(const LocalAccessible
* aAccessible
,
94 nsAString
& aTextEquiv
);
97 * Calculates the text equivalent from the given content and its subtree if
98 * allowed and appends it to the given string.
100 * @param aInitiatorAcc [in] the accessible text equivalent is computed for
101 * in the end (root accessible of text equivalent
102 * calculation recursion)
103 * @param aContent [in] the given content the text equivalent is
105 * @param aString [in, out] the string
107 static nsresult
AppendTextEquivFromContent(
108 const LocalAccessible
* aInitiatorAcc
, nsIContent
* aContent
,
112 * Calculates the text equivalent from the given text content (may be text
113 * node or html:br) and appends it to the given string.
115 * @param aContent [in] the text content
116 * @param aString [in, out] the string
118 static nsresult
AppendTextEquivFromTextContent(nsIContent
* aContent
,
122 * Iterates DOM children and calculates text equivalent from each child node.
123 * Then, appends found text to the given string.
125 * @param aContent [in] the node to fetch DOM children from
126 * @param aString [in, out] the string
128 static nsresult
AppendFromDOMChildren(nsIContent
* aContent
,
133 * Iterates accessible children and calculates text equivalent from each
136 static nsresult
AppendFromAccessibleChildren(const Accessible
* aAccessible
,
140 * Calculates text equivalent from the given accessible and its subtree if
143 static nsresult
AppendFromAccessible(Accessible
* aAccessible
,
147 * Calculates text equivalent from the value of given accessible.
149 static nsresult
AppendFromValue(Accessible
* aAccessible
, nsAString
* aString
);
152 * Calculates text equivalent from the given DOM node and its subtree if
155 static nsresult
AppendFromDOMNode(nsIContent
* aContent
, nsAString
* aString
);
158 * Concatenates strings and appends space between them. Returns true if
159 * text equivalent string was appended.
161 static bool AppendString(nsAString
* aString
,
162 const nsAString
& aTextEquivalent
);
165 * Returns the rule (constant of ETextEquivRule) for a given role.
167 static uint32_t GetRoleRule(mozilla::a11y::roles::Role aRole
);
170 * Returns true if a given accessible should be included when calculating
171 * the text equivalent for the initiator's subtree.
173 static bool ShouldIncludeInSubtreeCalculation(Accessible
* aAccessible
);
176 * Returns true if a given accessible is a text leaf containing only
179 static bool IsWhitespaceLeaf(Accessible
* aAccessible
);