1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5/** 6 * @fileoverview Simple class to represent a cursor location in the document. 7 */ 8 9goog.provide('cvox.Cursor'); 10 11/** 12 * A class to represent a cursor location in the document, 13 * like the start position or end position of a selection range. 14 * 15 * Later this may be extended to support "virtual text" for an object, 16 * like the ALT text for an image. 17 * 18 * Note: we cache the text of a particular node at the time we 19 * traverse into it. Later we should add support for dynamically 20 * reloading it. 21 * NOTE: Undefined behavior if node is null 22 * @param {Node} node The DOM node. 23 * @param {number} index The index of the character within the node. 24 * @param {string} text The cached text contents of the node. 25 * @constructor 26 */ 27cvox.Cursor = function(node, index, text) { 28 this.node = node; 29 this.index = index; 30 this.text = text; 31}; 32 33/** 34 * @return {!cvox.Cursor} A new cursor pointing to the same location. 35 */ 36cvox.Cursor.prototype.clone = function() { 37 return new cvox.Cursor(this.node, this.index, this.text); 38}; 39 40/** 41 * Modify this cursor to point to the location that another cursor points to. 42 * @param {!cvox.Cursor} otherCursor The cursor to copy from. 43 */ 44cvox.Cursor.prototype.copyFrom = function(otherCursor) { 45 this.node = otherCursor.node; 46 this.index = otherCursor.index; 47 this.text = otherCursor.text; 48}; 49 50/** 51 * Check for equality. 52 * @param {!cvox.Cursor} rhs The cursor to compare against. 53 * @return {boolean} True if equal. 54 */ 55cvox.Cursor.prototype.equals = function(rhs) { 56 return this.node == rhs.node && 57 this.index == rhs.index && 58 this.text == rhs.text; 59}; 60