Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/accessible/base/nsCoreUtils.h
Line
Count
Source (jump to first uncovered line)
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 nsCoreUtils_h_
7
#define nsCoreUtils_h_
8
9
#include "mozilla/EventForwards.h"
10
#include "mozilla/dom/Element.h"
11
#include "nsIAccessibleEvent.h"
12
#include "nsIContent.h"
13
#include "nsIDocument.h" // for GetShell()
14
#include "nsIPresShell.h"
15
16
#include "nsPoint.h"
17
#include "nsTArray.h"
18
19
class nsRange;
20
class nsTreeColumn;
21
class nsIBoxObject;
22
class nsIFrame;
23
class nsIDocShell;
24
class nsITreeBoxObject;
25
class nsIWidget;
26
27
/**
28
 * Core utils.
29
 */
30
class nsCoreUtils
31
{
32
public:
33
  /**
34
   * Return true if the given node is a label of a control.
35
   */
36
  static bool IsLabelWithControl(nsIContent *aContent);
37
38
  /**
39
   * Return true if the given node has registered click, mousedown or mouseup
40
   * event listeners.
41
   */
42
  static bool HasClickListener(nsIContent *aContent);
43
44
  /**
45
   * Dispatch click event to XUL tree cell.
46
   *
47
   * @param  aTreeBoxObj  [in] tree box object
48
   * @param  aRowIndex    [in] row index
49
   * @param  aColumn      [in] column object
50
   * @param  aPseudoElm   [in] pseudo elemenet inside the cell, see
51
   *                       nsITreeBoxObject for available values
52
   */
53
  static void DispatchClickEvent(nsITreeBoxObject *aTreeBoxObj,
54
                                 int32_t aRowIndex, nsTreeColumn *aColumn,
55
                                 const nsAString& aPseudoElt = EmptyString());
56
57
  /**
58
   * Send mouse event to the given element.
59
   *
60
   * @param aMessage     [in] an event message (see EventForwards.h)
61
   * @param aX           [in] x coordinate in dev pixels
62
   * @param aY           [in] y coordinate in dev pixels
63
   * @param aContent     [in] the element
64
   * @param aFrame       [in] frame of the element
65
   * @param aPresShell   [in] the presshell for the element
66
   * @param aRootWidget  [in] the root widget of the element
67
   */
68
  static void DispatchMouseEvent(mozilla::EventMessage aMessage,
69
                                 int32_t aX, int32_t aY,
70
                                 nsIContent *aContent, nsIFrame *aFrame,
71
                                 nsIPresShell *aPresShell, nsIWidget *aRootWidget);
72
73
  /**
74
   * Send a touch event with a single touch point to the given element.
75
   *
76
   * @param aMessage     [in] an event message (see EventForwards.h)
77
   * @param aX           [in] x coordinate in dev pixels
78
   * @param aY           [in] y coordinate in dev pixels
79
   * @param aContent     [in] the element
80
   * @param aFrame       [in] frame of the element
81
   * @param aPresShell   [in] the presshell for the element
82
   * @param aRootWidget  [in] the root widget of the element
83
   */
84
  static void DispatchTouchEvent(mozilla::EventMessage aMessage,
85
                                 int32_t aX, int32_t aY,
86
                                 nsIContent* aContent, nsIFrame* aFrame,
87
                                 nsIPresShell* aPresShell, nsIWidget* aRootWidget);
88
89
  /**
90
   * Return an accesskey registered on the given element by
91
   * EventStateManager or 0 if there is no registered accesskey.
92
   *
93
   * @param aContent - the given element.
94
   */
95
  static uint32_t GetAccessKeyFor(nsIContent *aContent);
96
97
  /**
98
   * Return DOM element related with the given node, i.e.
99
   * a) itself if it is DOM element
100
   * b) parent element if it is text node
101
   * c) otherwise nullptr
102
   *
103
   * @param aNode  [in] the given DOM node
104
   */
105
  static nsIContent* GetDOMElementFor(nsIContent *aContent);
106
107
  /**
108
   * Return DOM node for the given DOM point.
109
   */
110
  static nsINode *GetDOMNodeFromDOMPoint(nsINode *aNode, uint32_t aOffset);
111
112
  /**
113
   * Is the first passed in node an ancestor of the second?
114
   * Note: A node is not considered to be the ancestor of itself.
115
   *
116
   * @param  aPossibleAncestorNode   [in] node to test for ancestor-ness of
117
   *                                   aPossibleDescendantNode
118
   * @param  aPossibleDescendantNode [in] node to test for descendant-ness of
119
   *                                   aPossibleAncestorNode
120
   * @param  aRootNode               [in, optional] the root node that search
121
   *                                   search should be performed within
122
   * @return true                     if aPossibleAncestorNode is an ancestor of
123
   *                                   aPossibleDescendantNode
124
   */
125
   static bool IsAncestorOf(nsINode *aPossibleAncestorNode,
126
                              nsINode *aPossibleDescendantNode,
127
                              nsINode *aRootNode = nullptr);
128
129
  /**
130
   * Helper method to scroll range into view, used for implementation of
131
   * nsIAccessibleText::scrollSubstringTo().
132
   *
133
   * @param aFrame        the frame for accessible the range belongs to.
134
   * @param aRange    the range to scroll to
135
   * @param aScrollType   the place a range should be scrolled to
136
   */
137
  static nsresult ScrollSubstringTo(nsIFrame* aFrame, nsRange* aRange,
138
                                    uint32_t aScrollType);
139
140
  /** Helper method to scroll range into view, used for implementation of
141
   * nsIAccessibleText::scrollSubstringTo[Point]().
142
   *
143
   * @param aFrame        the frame for accessible the range belongs to.
144
   * @param aRange    the range to scroll to
145
   * @param aVertical     how to align vertically, specified in percents, and when.
146
   * @param aHorizontal     how to align horizontally, specified in percents, and when.
147
   */
148
  static nsresult ScrollSubstringTo(nsIFrame* aFrame, nsRange* aRange,
149
                                    nsIPresShell::ScrollAxis aVertical,
150
                                    nsIPresShell::ScrollAxis aHorizontal);
151
152
  /**
153
   * Scrolls the given frame to the point, used for implememntation of
154
   * nsIAccessible::scrollToPoint and nsIAccessibleText::scrollSubstringToPoint.
155
   *
156
   * @param aScrollableFrame  the scrollable frame
157
   * @param aFrame            the frame to scroll
158
   * @param aPoint            the point scroll to
159
   */
160
  static void ScrollFrameToPoint(nsIFrame *aScrollableFrame,
161
                                 nsIFrame *aFrame, const nsIntPoint& aPoint);
162
163
  /**
164
   * Converts scroll type constant defined in nsIAccessibleScrollType to
165
   * vertical and horizontal parameters.
166
   */
167
  static void ConvertScrollTypeToPercents(uint32_t aScrollType,
168
                                          nsIPresShell::ScrollAxis *aVertical,
169
                                          nsIPresShell::ScrollAxis *aHorizontal);
170
171
  /**
172
   * Returns coordinates in device pixels relative screen for the top level
173
   * window.
174
   *
175
   * @param aNode  the DOM node hosted in the window.
176
   */
177
  static nsIntPoint GetScreenCoordsForWindow(nsINode *aNode);
178
179
  /**
180
   * Return document shell for the given DOM node.
181
   */
182
  static already_AddRefed<nsIDocShell> GetDocShellFor(nsINode *aNode);
183
184
  /**
185
   * Return true if the given document is root document.
186
   */
187
  static bool IsRootDocument(nsIDocument *aDocument);
188
189
  /**
190
   * Return true if the given document is content document (not chrome).
191
   */
192
  static bool IsContentDocument(nsIDocument *aDocument);
193
194
  /**
195
   * Return true if the given document node is for tab document accessible.
196
   */
197
  static bool IsTabDocument(nsIDocument* aDocumentNode);
198
199
  /**
200
   * Return true if the given document is an error page.
201
   */
202
  static bool IsErrorPage(nsIDocument *aDocument);
203
204
  /**
205
   * Return presShell for the document containing the given DOM node.
206
   */
207
  static nsIPresShell *GetPresShellFor(nsINode *aNode)
208
0
  {
209
0
    return aNode->OwnerDoc()->GetShell();
210
0
  }
211
212
  /**
213
   * Get the ID for an element, in some types of XML this may not be the ID attribute
214
   * @param aContent  Node to get the ID for
215
   * @param aID       Where to put ID string
216
   * @return          true if there is an ID set for this node
217
   */
218
  static bool GetID(nsIContent *aContent, nsAString& aID);
219
220
  /**
221
   * Convert attribute value of the given node to positive integer. If no
222
   * attribute or wrong value then false is returned.
223
   */
224
  static bool GetUIntAttr(nsIContent *aContent, nsAtom *aAttr,
225
                          int32_t* aUInt);
226
227
  /**
228
   * Returns language for the given node.
229
   *
230
   * @param aContent     [in] the given node
231
   * @param aRootContent [in] container of the given node
232
   * @param aLanguage    [out] language
233
   */
234
  static void GetLanguageFor(nsIContent *aContent, nsIContent *aRootContent,
235
                             nsAString& aLanguage);
236
237
  /**
238
   * Return box object for XUL treechildren element by tree box object.
239
   */
240
  static already_AddRefed<nsIBoxObject>
241
    GetTreeBodyBoxObject(nsITreeBoxObject *aTreeBoxObj);
242
243
  /**
244
   * Return tree box object from any levels DOMNode under the XUL tree.
245
   */
246
  static already_AddRefed<nsITreeBoxObject>
247
    GetTreeBoxObject(nsIContent* aContent);
248
249
  /**
250
   * Return first sensible column for the given tree box object.
251
   */
252
  static already_AddRefed<nsTreeColumn>
253
    GetFirstSensibleColumn(nsITreeBoxObject *aTree);
254
255
  /**
256
   * Return sensible columns count for the given tree box object.
257
   */
258
  static uint32_t GetSensibleColumnCount(nsITreeBoxObject *aTree);
259
260
  /**
261
   * Return sensible column at the given index for the given tree box object.
262
   */
263
  static already_AddRefed<nsTreeColumn>
264
    GetSensibleColumnAt(nsITreeBoxObject *aTree, uint32_t aIndex);
265
266
  /**
267
   * Return next sensible column for the given column.
268
   */
269
  static already_AddRefed<nsTreeColumn>
270
    GetNextSensibleColumn(nsTreeColumn *aColumn);
271
272
  /**
273
   * Return previous sensible column for the given column.
274
   */
275
  static already_AddRefed<nsTreeColumn>
276
    GetPreviousSensibleColumn(nsTreeColumn *aColumn);
277
278
  /**
279
   * Return true if the given column is hidden (i.e. not sensible).
280
   */
281
  static bool IsColumnHidden(nsTreeColumn *aColumn);
282
283
  /**
284
   * Scroll content into view.
285
   */
286
  static void ScrollTo(nsIPresShell* aPresShell, nsIContent* aContent,
287
                       uint32_t aScrollType);
288
289
  /**
290
   * Return true if the given node is table header element.
291
   */
292
  static bool IsHTMLTableHeader(nsIContent *aContent)
293
0
  {
294
0
    return aContent->NodeInfo()->Equals(nsGkAtoms::th) ||
295
0
      (aContent->IsElement() &&
296
0
       aContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::scope));
297
0
  }
298
299
  /**
300
   * Returns true if the given string is empty or contains whitespace symbols
301
   * only. In contrast to nsWhitespaceTokenizer class it takes into account
302
   * non-breaking space (0xa0).
303
   */
304
  static bool IsWhitespaceString(const nsAString& aString);
305
306
  /**
307
   * Returns true if the given character is whitespace symbol.
308
   */
309
  static bool IsWhitespace(char16_t aChar)
310
0
  {
311
0
    return aChar == ' ' || aChar == '\n' ||
312
0
      aChar == '\r' || aChar == '\t' || aChar == 0xa0;
313
0
  }
314
315
  /*
316
   * Return true if there are any observers of accessible events.
317
   */
318
  static bool AccEventObserversExist();
319
320
  /**
321
   * Notify accessible event observers of an event.
322
   */
323
  static void DispatchAccEvent(RefPtr<nsIAccessibleEvent> aEvent);
324
};
325
326
#endif