summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/common/group_util.js
blob: d2a8ff1959b30af8515b30d74ea5dd6a2bd00fe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Some utilities for defining what groups are.
 */


goog.provide('cvox.GroupUtil');

goog.require('cvox.AriaUtil');
goog.require('cvox.DomUtil');


/**
 * If a node contains more characters than this, it should not be treated
 * as a leaf node by the smart navigation algorithm.
 *
 * This number was determined by looking at the average number of
 * characters in a paragraph:
 * http://www.fullondesign.co.uk/design/usability/
 * 285-how-many-characters-per-a-page-is-normal.htm
 * and then trying it out on a few popular websites (CNN, BBC,
 * Google Search, etc.) and making sure it made sense.
 * @type {number}
 * @private
 * @const
 */
cvox.GroupUtil.MAX_CHARCOUNT_ = 1500;


/**
 * If a node contains any of these elements, it should not be treated
 * as a leaf node by the smart navigation algorithm.
 * @type {string}
 * @private
 * @const
 */
cvox.GroupUtil.BREAKOUT_SELECTOR_ = 'blockquote,' +
    'button,' +
    'code,' +
    'form,' +
    'frame,' +
    'h1,' +
    'h2,' +
    'h3,' +
    'h4,' +
    'h5,' +
    'h6,' +
    'hr,' +
    'iframe,' +
    'input,' +
    'object,' +
    'ol,' +
    'p,' +
    'pre,' +
    'select,' +
    'table,' +
    'tr,' +
    'ul,' +
    'math,' +
  // This takes care of MathJax expressions.
    'span.math,' +
// TODO (sorge) Do we want to group all math or only display math?
//    '[mode="display"],' +
    // Aria widget roles
    '[role~="alert ' +
    'alertdialog ' +
    'button ' +
    'checkbox ' +
    'combobox ' +
    'dialog ' +
    'log ' +
    'marquee ' +
    'menubar ' +
    'progressbar ' +
    'radio ' +
    'radiogroup ' +
    'scrollbar ' +
    'slider ' +
    'spinbutton ' +
    'status ' +
    'tab ' +
    'tabpanel ' +
    'textbox ' +
    'toolbar ' +
    'tooltip ' +
    'treeitem ' +
    // Aria structure roles
    'article ' +
    'document ' +
    'group ' +
    'heading ' +
    'img ' +
    'list ' +
    'math ' +
    'region ' +
    'row ' +
    'separator"]';


/**
 * Returns true if this is a leaf node for groups.
 * true for a node => true for all child nodes
 * true if node has no children
 * @param {!Node} node The node to check.
 * @return {boolean} true if this is at the "leaf node" level or lower
 * for this granularity.
 */
cvox.GroupUtil.isLeafNode = function(node) {
  // TODO (stoarca): Write test to make sure that this function satisfies
  // the restriction given above.
  if (node.tagName == 'LABEL') {
    return cvox.DomUtil.isLeafNode(node);
  }
  if (cvox.DomUtil.isLeafNode(node)) {
    return true;
  }

  if (!cvox.DomUtil.isSemanticElt(node)) {
    var breakingNodes = node.querySelectorAll(
        cvox.GroupUtil.BREAKOUT_SELECTOR_);

    for (var i = 0; i < breakingNodes.length; ++i) {
      if (cvox.DomUtil.hasContent(breakingNodes[i])) {
        return false;
      }
    }
  }

  if (cvox.AriaUtil.isCompositeControl(node) &&
      !cvox.DomUtil.isFocusable(node)) {
    return false;
  }

  var content = cvox.DomUtil.collapseWhitespace(
      cvox.DomUtil.getValue(node) + ' ' +
      cvox.DomUtil.getName(node));
  if (content.length > cvox.GroupUtil.MAX_CHARCOUNT_) {
    return false;
  }

  if (content.replace(/\s/g, '') === '') {
    // Text only contains whitespace
    return false;
  }

  return true;
};