summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/chromevox/injected/navigation_history.js
blob: 343fc30571efe09bf78f0582b568d2ed94baa0a8 (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
// 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 Navigation history tracks recently visited nodes. The
 * state of this class (the node history), is used to ensure the user is
 * navigating to and from valid nodes.
 * NOTE: The term "valid node" is simply a heuristic, defined in isValidNode_.
 *
 */


goog.provide('cvox.NavigationHistory');

goog.require('cvox.DomUtil');


/**
 * @constructor
 */
cvox.NavigationHistory = function() {
  this.reset_();
};


/**
 * The maximum length of history tracked for recently visited nodes.
 * @const
 * @type {number}
 * @private
 */
cvox.NavigationHistory.MAX_HISTORY_LEN_ = 30;


/**
 * Resets the navigation history.
 * @private
 */
cvox.NavigationHistory.prototype.reset_ = function() {
  var startNode = document.body;

  /**
   * An array of nodes ordered from newest to oldest in the history.
   * The most recent nodes are at the start of the array.
   * @type {Array.<Node>}
   * @private
   */
  this.history_ = [startNode];

  /**
   * A flag to keep track of whether the last node added to the history was
   * valid or not. If false, something strange might be going on, and we
   * can react to this in the code.
   * @type {boolean}
   * @private
   */
  this.arrivedValid_ = true;

};


/**
 * Update the navigation history with the current element.
 * The most recent elements are at the start of the array.
 * @param {Node} newNode The new node to update the history with.
 */
cvox.NavigationHistory.prototype.update = function(newNode) {
  var previousNode = this.history_[0];

  // Avoid pushing consecutive duplicate elements.
  if (newNode && newNode != previousNode) {
    this.history_.unshift(newNode);
  }

  // If list is too long, pop the last (oldest) item.
  if (this.history_.length >
      cvox.NavigationHistory.MAX_HISTORY_LEN_) {
    this.history_.pop();
  }

  // Check if the node is valid upon arrival. If not, set a flag because
  // something fishy is probably going on.
  this.arrivedValid_ = this.isValidNode_(newNode);
};


/**
 * Routinely clean out history and determine if the given node has become
 * invalid since we arrived there (during the update call). If the node
 * was already invalid, we will return false.
 * @param {Node} node The node to check for validity change.
 * @return {boolean} True if node changed state to invalid.
 */
cvox.NavigationHistory.prototype.becomeInvalid = function(node) {
  // Remove any invalid nodes from history_.
  this.clean_();

  // If node was somehow already invalid on arrival, the page was probably
  // changing very quickly. Be defensive here and allow the default
  // navigation action by returning true.
  if (!this.arrivedValid_) {
    this.arrivedValid_ = true; // Reset flag.
    return false;
  }

  // Run the validation method on the given node.
  return !this.isValidNode_(node);
};


/**
 * Determine a valid reversion for the current navigation track. A reversion
 * provides both a current node to sync to and a previous node as context.
 * @param {function(Node)=} opt_predicate A function that takes in a node and
 *     returns true if it is a valid recovery candidate. Nodes that do not
 *     match the predicate are removed as we search for a match. If no
 *     predicate is provided, return the two most recent nodes.
 * @return {{current: ?Node, previous: ?Node}}
 *     The two nodes to override default navigation behavior with. Returning
 *     null or undefined means the history is empty.
 */
cvox.NavigationHistory.prototype.revert = function(opt_predicate) {
  // If the currently active element is valid, it is probably the best
  // recovery target. Add it to the history before computing the reversion.
  var active = document.activeElement;
  if (active != document.body && this.isValidNode_(active)) {
    this.update(active);
  }

  // Remove the most-recent-nodes that do not match the predicate.
  if (opt_predicate) {
    while (this.history_.length > 0) {
      var node = this.history_[0];
      if (opt_predicate(node)) {
        break;
      }
      this.history_.shift();
    }
  }

  // The reversion is just the first two nodes in the history.
  return {current: this.history_[0], previous: this.history_[1]};
};


/**
 * Remove any and all nodes from history_ that are no longer valid.
 * @return {boolean} True if any changes were made to the history.
 * @private
 */
cvox.NavigationHistory.prototype.clean_ = function() {
  var changed = false;
  for (var i = this.history_.length - 1; i >= 0; i--) {
    var valid = this.isValidNode_(this.history_[i]);
    if (!valid) {
      this.history_.splice(i, 1);
      changed = true;
    }
  }
  return changed;
};


/**
 * Determine if the given node is valid based on a heuristic.
 * A valid node must be attached to the DOM and visible.
 * @param {Node} node The node to validate.
 * @return {boolean} True if node is valid.
 * @private
 */
cvox.NavigationHistory.prototype.isValidNode_ = function(node) {
  // Confirm that the element is in the DOM.
  if (!cvox.DomUtil.isAttachedToDocument(node)) {
    return false;
  }

  // TODO (adu): In the future we may change this to just let users know the
  // node is invisible instead of restoring focus.
  if (!cvox.DomUtil.isVisible(node)) {
    return false;
  }

  return true;
};