OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview Class which allows construction of annotated strings. | 6 * @fileoverview Class which allows construction of annotated strings. |
7 */ | 7 */ |
8 | 8 |
9 goog.provide('cvox.Spannable'); | 9 goog.provide('cvox.Spannable'); |
10 | 10 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 * @param {*} value Annotation. | 62 * @param {*} value Annotation. |
63 * @param {number} start Starting index (inclusive). | 63 * @param {number} start Starting index (inclusive). |
64 * @param {number} end Ending index (exclusive). | 64 * @param {number} end Ending index (exclusive). |
65 */ | 65 */ |
66 cvox.Spannable.prototype.setSpan = function(value, start, end) { | 66 cvox.Spannable.prototype.setSpan = function(value, start, end) { |
67 this.removeSpan(value); | 67 this.removeSpan(value); |
68 if (0 <= start && start <= end && end <= this.string_.length) { | 68 if (0 <= start && start <= end && end <= this.string_.length) { |
69 // Zero-length spans are explicitly allowed, because it is possible to | 69 // Zero-length spans are explicitly allowed, because it is possible to |
70 // query for position by annotation as well as the reverse. | 70 // query for position by annotation as well as the reverse. |
71 this.spans_.push({ value: value, start: start, end: end }); | 71 this.spans_.push({ value: value, start: start, end: end }); |
| 72 this.spans_.sort(function(a, b) { |
| 73 var ret = a.start - b.start; |
| 74 if (ret == 0) |
| 75 ret = a.end - b.end; |
| 76 return ret; |
| 77 }); |
72 } else { | 78 } else { |
73 throw new RangeError('span out of range (start=' + start + | 79 throw new RangeError('span out of range (start=' + start + |
74 ', end=' + end + ', len=' + this.string_.length + ')'); | 80 ', end=' + end + ', len=' + this.string_.length + ')'); |
75 } | 81 } |
76 }; | 82 }; |
77 | 83 |
78 | 84 |
79 /** | 85 /** |
80 * Removes a span. | 86 * Removes a span. |
81 * @param {*} value Annotation. | 87 * @param {*} value Annotation. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 for (var i = 0; i < this.spans_.length; i++) { | 140 for (var i = 0; i < this.spans_.length; i++) { |
135 var span = this.spans_[i]; | 141 var span = this.spans_[i]; |
136 if (span.value instanceof constructor) { | 142 if (span.value instanceof constructor) { |
137 return span.value; | 143 return span.value; |
138 } | 144 } |
139 } | 145 } |
140 }; | 146 }; |
141 | 147 |
142 /** | 148 /** |
143 * Returns all span values which are an instance of a given constructor. | 149 * Returns all span values which are an instance of a given constructor. |
| 150 * Spans are returned in the order of their starting index and ending index |
| 151 * for spans with equals tarting indices. |
144 * @param {!Function} constructor Constructor. | 152 * @param {!Function} constructor Constructor. |
145 * @return {!Array<Object>} Array of object. | 153 * @return {!Array<Object>} Array of object. |
146 */ | 154 */ |
147 cvox.Spannable.prototype.getSpansInstanceOf = function(constructor) { | 155 cvox.Spannable.prototype.getSpansInstanceOf = function(constructor) { |
148 var ret = []; | 156 var ret = []; |
149 for (var i = 0; i < this.spans_.length; i++) { | 157 for (var i = 0; i < this.spans_.length; i++) { |
150 var span = this.spans_[i]; | 158 var span = this.spans_[i]; |
151 if (span.value instanceof constructor) { | 159 if (span.value instanceof constructor) { |
152 ret.push(span.value); | 160 ret.push(span.value); |
153 } | 161 } |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 * @private | 425 * @private |
418 */ | 426 */ |
419 cvox.Spannable.SerializedSpan_; | 427 cvox.Spannable.SerializedSpan_; |
420 | 428 |
421 /** | 429 /** |
422 * Maps type names to serialization info objects. | 430 * Maps type names to serialization info objects. |
423 * @type {Object<string, cvox.Spannable.SerializeInfo_>} | 431 * @type {Object<string, cvox.Spannable.SerializeInfo_>} |
424 * @private | 432 * @private |
425 */ | 433 */ |
426 cvox.Spannable.serializableSpansByName_ = {}; | 434 cvox.Spannable.serializableSpansByName_ = {}; |
OLD | NEW |