summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/chromeos/chromevox/walkers/layout_line_walker.js
blob: b46a089df197de8e9956fffd09bd5fcd190aca33 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// 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 A JavaScript class for walking lines consisting of one or more
 * clickable nodes.
 */


goog.provide('cvox.LayoutLineWalker');

goog.require('cvox.AbstractWalker');
goog.require('cvox.StructuralLineWalker');


/**
 * @constructor
 * @extends {cvox.AbstractWalker}
 */
cvox.LayoutLineWalker = function() {
  this.subWalker_ = new cvox.StructuralLineWalker();
};
goog.inherits(cvox.LayoutLineWalker, cvox.AbstractWalker);


/**
 * @override
 */
cvox.LayoutLineWalker.prototype.next = function(sel) {
  // Collapse selection to the directed end.
  var endSel = new cvox.CursorSelection(sel.end, sel.end, sel.isReversed());

  // Sync to the line.
  var sync = this.subWalker_.sync(endSel);
  if (!sync) {
    return null;
  }

  // Compute the next selection.
  var start = this.subWalker_.next(endSel);
  if (!start) {
    return null;
  }
  start.setReversed(sel.isReversed());
  return this.extend_(start).setReversed(false);
};


/**
 * @override
 */
cvox.LayoutLineWalker.prototype.sync = function(sel) {
  var line = this.subWalker_.sync(sel);
  if (!line) {
    return null;
  }

  // Extend to both line breaks (in each direction).
  var end = this.extend_(line);
  var start = this.extend_(line.setReversed(!line.isReversed()));

  return new cvox.CursorSelection(start.end, end.end, sel.isReversed());
};


/**
 * @override
 */
cvox.LayoutLineWalker.prototype.getDescription = function(prevSel, sel) {
  var descriptions = [];
  var prev = prevSel;
  var absSel = sel.clone().setReversed(false);
  var cur = new cvox.CursorSelection(absSel.start, absSel.start);
  cur = this.subWalker_.sync(cur);
  if (!cur) {
    return [];
  }

  // No need to accumulate descriptions.
  if (absSel.start.node == absSel.end.node) {
    return this.subWalker_.getDescription(prevSel, sel);
  }

  // Walk through and collect descriptions for each line.
  while (cur && !cur.end.equals(absSel.end)) {
    descriptions.push.apply(
        descriptions, this.subWalker_.getDescription(prev, cur));
    prev = cur;
    cur = this.subWalker_.next(cur);
  }
  if (cur) {
    descriptions.push.apply(
        descriptions, this.subWalker_.getDescription(prev, cur));
  }
  return descriptions;
};


/**
 * @override
 */
cvox.LayoutLineWalker.prototype.getBraille = function(prevSel, sel) {
  var braille = new cvox.NavBraille({});
  var absSel = this.subWalker_.sync(sel.clone().setReversed(false));
  var layoutSel = this.sync(sel).setReversed(false);
  if (!layoutSel || !absSel) {
    return braille;
  }
  var cur = new cvox.CursorSelection(layoutSel.start, layoutSel.start);
  cur = this.subWalker_.sync(cur);
  if (!cur) {
    return braille;
  }

  // Walk through and collect braille for each line.
  while (cur && !cur.end.equals(layoutSel.end)) {
    this.appendBraille_(prevSel, absSel, cur, braille);
    prevSel = cur;
    cur = this.subWalker_.next(cur);
  }
  if (cur) {
    this.appendBraille_(prevSel, absSel, cur, braille);
  }
  return braille;
};


/**
 * @override
 */
cvox.LayoutLineWalker.prototype.getGranularityMsg = function() {
  return cvox.ChromeVox.msgs.getMsg('layout_line');
};


/**
 * Compares two selections and determines if the lie on the same horizontal
 * line as determined by their bounding rectangles.
 * @param {!cvox.CursorSelection} lSel Left selection.
 * @param {!cvox.CursorSelection} rSel Right selection.
 * @return {boolean} Whether lSel and rSel are on different visual lines.
 * @private
 */
cvox.LayoutLineWalker.prototype.isVisualLineBreak_ = function(lSel, rSel) {
  if (this.wantsOwnLine_(lSel.end.node) ||
      this.wantsOwnLine_(rSel.start.node)) {
    return true;
  }
  var lRect = lSel.getRange().getBoundingClientRect();
  var rRect = rSel.getRange().getBoundingClientRect();

  // Some ranges from the browser give us 0-sized rects (such as in the case of
  // select's). Detect these cases and use a more reliable method (take the
  // bounding rect of the actual element rather than the range).
  if (lRect.width == 0 &&
      lRect.height == 0 &&
      lSel.end.node.nodeType == Node.ELEMENT_NODE) {
    lRect = lSel.end.node.getBoundingClientRect();
  }

  if (rRect.width == 0 &&
      rRect.height == 0 &&
      rSel.start.node.nodeType == Node.ELEMENT_NODE) {
    rRect = rSel.start.node.getBoundingClientRect();
  }
  return lRect.bottom != rRect.bottom;
};


/**
 * Determines if node should force a line break.
 * This is used for elements with unusual semantics, such as multi-line
 * text fields, where the behaviour would otherwise be confusing.
 * @param {!Node} node Node.
 * @return {boolean} True if the node should appear next to a line break.
 * @private
 */
cvox.LayoutLineWalker.prototype.wantsOwnLine_ = function(node) {
  return node instanceof HTMLTextAreaElement ||
      node.parentNode instanceof HTMLTextAreaElement;
};


/**
 * Extends a given cursor selection up to the next visual line break.
 * @param {!cvox.CursorSelection} start The selection.
 * @return {!cvox.CursorSelection} The resulting selection.
 * @private
 */
cvox.LayoutLineWalker.prototype.extend_ = function(start) {
  // Extend the selection up to just before a new visual line break.
  var end = start;
  var next = start;

  do {
    end = next;
    next = this.subWalker_.next(end);
  } while (next && !this.isVisualLineBreak_(end, next));
  return new cvox.CursorSelection(start.start, end.end, start.isReversed());
};


/**
 * Private routine to append braille given three selections.
 * @param {!cvox.CursorSelection} prevSel A previous selection in walker
 * ordering.
 * @param {!cvox.CursorSelection} sel A selection that represents the location
 * of the braille cursor.
 * @param {!cvox.CursorSelection} cur The specific selection to append.
 * @param {!cvox.NavBraille} braille Braille on which to append.
 * @private
 */
cvox.LayoutLineWalker.prototype.appendBraille_ = function(
    prevSel, sel, cur, braille) {
  var item = this.subWalker_.getBraille(prevSel, cur).text;
  var valueSelectionSpan = item.getSpanInstanceOf(cvox.ValueSelectionSpan);

  if (braille.text.getLength() > 0) {
    braille.text.append(cvox.BrailleUtil.ITEM_SEPARATOR);
  }

  // Find the surrounding logical "leaf node".
  // This prevents us from labelling the braille output with the wrong node,
  // such as a text node child of a <textarea>.
  var node = cur.start.node;
  while (node.parentNode && cvox.DomUtil.isLeafNode(node.parentNode)) {
    node = node.parentNode;
  }

  var nodeStart = braille.text.getLength();
  var nodeEnd = nodeStart + item.getLength();
  braille.text.append(item);
  braille.text.setSpan(node, nodeStart, nodeEnd);

  if (sel && cur.absEquals(sel)) {
    if (valueSelectionSpan) {
      braille.startIndex = nodeStart + item.getSpanStart(valueSelectionSpan);
      braille.endIndex = nodeStart + item.getSpanEnd(valueSelectionSpan);
    } else {
      braille.startIndex = nodeStart;
      braille.endIndex = nodeStart + 1;
    }
  }
};