accessible/src/base/nsTextEquivUtils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/accessible/src/base/nsTextEquivUtils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,162 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:expandtab:shiftwidth=2:tabstop=2:
     1.6 + */
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#ifndef _nsTextEquivUtils_H_
    1.12 +#define _nsTextEquivUtils_H_
    1.13 +
    1.14 +#include "Accessible.h"
    1.15 +#include "Role.h"
    1.16 +
    1.17 +class nsIContent;
    1.18 +
    1.19 +/**
    1.20 + * Text equivalent computation rules (see nsTextEquivUtils::gRoleToNameRulesMap)
    1.21 + */
    1.22 +enum ETextEquivRule
    1.23 +{
    1.24 +  // No rule.
    1.25 +  eNoNameRule = 0x00,
    1.26 +
    1.27 +  // Walk into subtree only if the currently navigated accessible is not root
    1.28 +  // accessible (i.e. if the accessible is part of text equivalent computation).
    1.29 +  eNameFromSubtreeIfReqRule = 0x01,
    1.30 +
    1.31 +  // Text equivalent computation from subtree is allowed.
    1.32 +  eNameFromSubtreeRule = 0x03,
    1.33 +
    1.34 +  // The accessible allows to append its value to text equivalent.
    1.35 +  // XXX: This is temporary solution. Once we move accessible value of links
    1.36 +  // and linkable accessibles to MSAA part we can remove this.
    1.37 +  eNameFromValueRule = 0x04
    1.38 +};
    1.39 +
    1.40 +/**
    1.41 + * The class provides utils methods to compute the accessible name and
    1.42 + * description.
    1.43 + */
    1.44 +class nsTextEquivUtils
    1.45 +{
    1.46 +public:
    1.47 +  typedef mozilla::a11y::Accessible Accessible;
    1.48 +
    1.49 +  /**
    1.50 +   * Determines if the accessible has a given name rule.
    1.51 +   *
    1.52 +   * @param aAccessible [in] the given accessible
    1.53 +   * @param aRule       [in] a given name rule
    1.54 +   * @return true if the accessible has the rule
    1.55 +   */
    1.56 +  static inline bool HasNameRule(Accessible* aAccessible, ETextEquivRule aRule)
    1.57 +  {
    1.58 +    return (GetRoleRule(aAccessible->Role()) & aRule) == aRule;
    1.59 +  }
    1.60 +
    1.61 +  /**
    1.62 +   * Calculates the name from accessible subtree if allowed.
    1.63 +   *
    1.64 +   * @param aAccessible [in] the given accessible
    1.65 +   * @param aName       [out] accessible name
    1.66 +   */
    1.67 +  static nsresult GetNameFromSubtree(Accessible* aAccessible,
    1.68 +                                     nsAString& aName);
    1.69 +
    1.70 +  /**
    1.71 +   * Calculates text equivalent from the subtree. Similar to GetNameFromSubtree.
    1.72 +   * However it returns not empty result for things like HTML p.
    1.73 +   */
    1.74 +  static void GetTextEquivFromSubtree(Accessible* aAccessible,
    1.75 +                                      nsString& aTextEquiv)
    1.76 +  {
    1.77 +    aTextEquiv.Truncate();
    1.78 +
    1.79 +    AppendFromAccessibleChildren(aAccessible, &aTextEquiv);
    1.80 +    aTextEquiv.CompressWhitespace();
    1.81 +  }
    1.82 +
    1.83 +  /**
    1.84 +   * Calculates text equivalent for the given accessible from its IDRefs
    1.85 +   * attribute (like aria-labelledby or aria-describedby).
    1.86 +   *
    1.87 +   * @param aAccessible  [in] the accessible text equivalent is computed for
    1.88 +   * @param aIDRefsAttr  [in] IDRefs attribute on DOM node of the accessible
    1.89 +   * @param aTextEquiv   [out] result text equivalent
    1.90 +   */
    1.91 +  static nsresult GetTextEquivFromIDRefs(Accessible* aAccessible,
    1.92 +                                         nsIAtom *aIDRefsAttr,
    1.93 +                                         nsAString& aTextEquiv);
    1.94 +
    1.95 +  /**
    1.96 +   * Calculates the text equivalent from the given content and its subtree if
    1.97 +   * allowed and appends it to the given string.
    1.98 +   *
    1.99 +   * @param aInitiatorAcc  [in] the accessible text equivalent is computed for
   1.100 +   *                       in the end (root accessible of text equivalent
   1.101 +   *                       calculation recursion)
   1.102 +   * @param aContent       [in] the given content the text equivalent is
   1.103 +   *                       computed from
   1.104 +   * @param aString        [in, out] the string
   1.105 +   */
   1.106 +  static nsresult AppendTextEquivFromContent(Accessible* aInitiatorAcc,
   1.107 +                                             nsIContent *aContent,
   1.108 +                                             nsAString *aString);
   1.109 +
   1.110 +  /**
   1.111 +   * Calculates the text equivalent from the given text content (may be text
   1.112 +   * node or html:br) and appends it to the given string.
   1.113 +   *
   1.114 +   * @param aContent       [in] the text content
   1.115 +   * @param aString        [in, out] the string
   1.116 +   */
   1.117 +  static nsresult AppendTextEquivFromTextContent(nsIContent *aContent,
   1.118 +                                                 nsAString *aString);
   1.119 +
   1.120 +private:
   1.121 +  /**
   1.122 +   * Iterates accessible children and calculates text equivalent from each
   1.123 +   * child.
   1.124 +   */
   1.125 +  static nsresult AppendFromAccessibleChildren(Accessible* aAccessible,
   1.126 +                                               nsAString *aString);
   1.127 +  
   1.128 +  /**
   1.129 +   * Calculates text equivalent from the given accessible and its subtree if
   1.130 +   * allowed.
   1.131 +   */
   1.132 +  static nsresult AppendFromAccessible(Accessible* aAccessible,
   1.133 +                                       nsAString *aString);
   1.134 +
   1.135 +  /**
   1.136 +   * Calculates text equivalent from the value of given accessible.
   1.137 +   */
   1.138 +  static nsresult AppendFromValue(Accessible* aAccessible,
   1.139 +                                  nsAString *aString);
   1.140 +  /**
   1.141 +   * Iterates DOM children and calculates text equivalent from each child node.
   1.142 +   */
   1.143 +  static nsresult AppendFromDOMChildren(nsIContent *aContent,
   1.144 +                                        nsAString *aString);
   1.145 +
   1.146 +  /**
   1.147 +   * Calculates text equivalent from the given DOM node and its subtree if
   1.148 +   * allowed.
   1.149 +   */
   1.150 +  static nsresult AppendFromDOMNode(nsIContent *aContent, nsAString *aString);
   1.151 +
   1.152 +  /**
   1.153 +   * Concatenates strings and appends space between them. Returns true if
   1.154 +   * text equivalent string was appended.
   1.155 +   */
   1.156 +  static bool AppendString(nsAString *aString,
   1.157 +                             const nsAString& aTextEquivalent);
   1.158 +
   1.159 +  /**
   1.160 +   * Returns the rule (constant of ETextEquivRule) for a given role.
   1.161 +   */
   1.162 +  static uint32_t GetRoleRule(mozilla::a11y::roles::Role aRole);
   1.163 +};
   1.164 +
   1.165 +#endif

mercurial